source: node_modules/mimic-response/index.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1'use strict';
2
3// We define these manually to ensure they're always copied
4// even if they would move up the prototype chain
5// https://nodejs.org/api/http.html#http_class_http_incomingmessage
6const knownProperties = [
7 'aborted',
8 'complete',
9 'headers',
10 'httpVersion',
11 'httpVersionMinor',
12 'httpVersionMajor',
13 'method',
14 'rawHeaders',
15 'rawTrailers',
16 'setTimeout',
17 'socket',
18 'statusCode',
19 'statusMessage',
20 'trailers',
21 'url'
22];
23
24module.exports = (fromStream, toStream) => {
25 if (toStream._readableState.autoDestroy) {
26 throw new Error('The second stream must have the `autoDestroy` option set to `false`');
27 }
28
29 const fromProperties = new Set(Object.keys(fromStream).concat(knownProperties));
30
31 const properties = {};
32
33 for (const property of fromProperties) {
34 // Don't overwrite existing properties.
35 if (property in toStream) {
36 continue;
37 }
38
39 properties[property] = {
40 get() {
41 const value = fromStream[property];
42 const isFunction = typeof value === 'function';
43
44 return isFunction ? value.bind(fromStream) : value;
45 },
46 set(value) {
47 fromStream[property] = value;
48 },
49 enumerable: true,
50 configurable: false
51 };
52 }
53
54 Object.defineProperties(toStream, properties);
55
56 fromStream.once('aborted', () => {
57 toStream.destroy();
58
59 toStream.emit('aborted');
60 });
61
62 fromStream.once('close', () => {
63 if (fromStream.complete) {
64 if (toStream.readable) {
65 toStream.once('end', () => {
66 toStream.emit('close');
67 });
68 } else {
69 toStream.emit('close');
70 }
71 } else {
72 toStream.emit('close');
73 }
74 });
75
76 return toStream;
77};
Note: See TracBrowser for help on using the repository browser.