Sha256: b1ed466b82833281b1919e82798d9453f72dea6159a5ce85e203f6dcb8aba119

Contents?: true

Size: 1.55 KB

Versions: 25

Compression:

Stored size: 1.55 KB

Contents

// 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;
}

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
immosquare-cleaner-0.1.52 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.51 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.50 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.49 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.48 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.47 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.46 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.45 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.44 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.43 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.42 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.41 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.40 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.39 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.38 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.32 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.31 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.30 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.29 node_modules/mimic-response/index.js
immosquare-cleaner-0.1.28 node_modules/mimic-response/index.js