summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/node_modules/mimic-response/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'includes/external/addressbook/node_modules/mimic-response/index.js')
-rw-r--r--includes/external/addressbook/node_modules/mimic-response/index.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/includes/external/addressbook/node_modules/mimic-response/index.js b/includes/external/addressbook/node_modules/mimic-response/index.js
new file mode 100644
index 0000000..a2e898f
--- /dev/null
+++ b/includes/external/addressbook/node_modules/mimic-response/index.js
@@ -0,0 +1,75 @@
+// We define these manually to ensure they're always copied
+// even if they would move up the prototype chain
+// https://nodejs.org/api/http.html#http_class_http_incomingmessage
+const knownProperties = [
+ 'aborted',
+ 'complete',
+ 'headers',
+ 'httpVersion',
+ 'httpVersionMinor',
+ 'httpVersionMajor',
+ 'method',
+ 'rawHeaders',
+ 'rawTrailers',
+ 'setTimeout',
+ 'socket',
+ 'statusCode',
+ 'statusMessage',
+ 'trailers',
+ 'url',
+];
+
+export default function mimicResponse(fromStream, toStream) {
+ if (toStream._readableState.autoDestroy) {
+ throw new Error('The second stream must have the `autoDestroy` option set to `false`');
+ }
+
+ const fromProperties = new Set([...Object.keys(fromStream), ...knownProperties]);
+
+ const properties = {};
+
+ for (const property of fromProperties) {
+ // Don't overwrite existing properties.
+ if (property in toStream) {
+ continue;
+ }
+
+ properties[property] = {
+ get() {
+ const value = fromStream[property];
+ const isFunction = typeof value === 'function';
+
+ return isFunction ? value.bind(fromStream) : value;
+ },
+ set(value) {
+ fromStream[property] = value;
+ },
+ enumerable: true,
+ configurable: false,
+ };
+ }
+
+ Object.defineProperties(toStream, properties);
+
+ fromStream.once('aborted', () => {
+ toStream.destroy();
+
+ toStream.emit('aborted');
+ });
+
+ fromStream.once('close', () => {
+ if (fromStream.complete) {
+ if (toStream.readable) {
+ toStream.once('end', () => {
+ toStream.emit('close');
+ });
+ } else {
+ toStream.emit('close');
+ }
+ } else {
+ toStream.emit('close');
+ }
+ });
+
+ return toStream;
+}