AK-21/Graphite-Industrial-Intelligence
0
1'use strict';2 3// We define these manually to ensure they're always copied4// even if they would move up the prototype chain5// https://nodejs.org/api/http.html#http_class_http_incomingmessage6const 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: false51 };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};78 