@mswjs/interceptors 0.13.5 → 0.13.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,4 +4,7 @@ export declare const IS_CLONE: unique symbol;
4
4
  export interface ClonedIncomingMessage extends IncomingMessage {
5
5
  [IS_CLONE]: boolean;
6
6
  }
7
+ /**
8
+ * Clones a given `http.IncomingMessage` instance.
9
+ */
7
10
  export declare function cloneIncomingMessage(message: IncomingMessage): ClonedIncomingMessage;
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __read = (this && this.__read) || function (o, n) {
14
3
  var m = typeof Symbol === "function" && o[Symbol.iterator];
15
4
  if (!m) return o;
@@ -44,20 +33,60 @@ var __values = (this && this.__values) || function(o) {
44
33
  };
45
34
  Object.defineProperty(exports, "__esModule", { value: true });
46
35
  exports.cloneIncomingMessage = exports.IS_CLONE = void 0;
36
+ var http_1 = require("http");
47
37
  var stream_1 = require("stream");
48
38
  exports.IS_CLONE = Symbol('isClone');
39
+ /**
40
+ * Clones a given `http.IncomingMessage` instance.
41
+ */
49
42
  function cloneIncomingMessage(message) {
43
+ var clone = message.pipe(new stream_1.PassThrough());
44
+ // Inherit all direct "IncomingMessage" properties.
45
+ inheritProperties(message, clone);
46
+ // Deeply inherit the message prototypes (Readable, Stream, EventEmitter, etc.).
47
+ var clonedPrototype = Object.create(http_1.IncomingMessage.prototype);
48
+ getPrototypes(clone).forEach(function (prototype) {
49
+ inheritProperties(prototype, clonedPrototype);
50
+ });
51
+ Object.setPrototypeOf(clone, clonedPrototype);
52
+ Object.defineProperty(clone, exports.IS_CLONE, {
53
+ enumerable: true,
54
+ value: true,
55
+ });
56
+ return clone;
57
+ }
58
+ exports.cloneIncomingMessage = cloneIncomingMessage;
59
+ /**
60
+ * Returns a list of all prototypes the given object extends.
61
+ */
62
+ function getPrototypes(source) {
63
+ var prototypes = [];
64
+ var current = source;
65
+ while ((current = Object.getPrototypeOf(current))) {
66
+ prototypes.push(current);
67
+ }
68
+ return prototypes;
69
+ }
70
+ /**
71
+ * Inherits a given target object properties and symbols
72
+ * onto the given source object.
73
+ * @param source Object which should acquire properties.
74
+ * @param target Object to inherit the properties from.
75
+ */
76
+ function inheritProperties(source, target) {
50
77
  var e_1, _a;
51
- var stream = message.pipe(new stream_1.PassThrough());
52
- var properties = __spreadArray(__spreadArray([], __read(Object.getOwnPropertyNames(message))), __read(Object.getOwnPropertySymbols(message)));
78
+ var properties = __spreadArray(__spreadArray([], __read(Object.getOwnPropertyNames(source))), __read(Object.getOwnPropertySymbols(source)));
53
79
  try {
54
80
  for (var properties_1 = __values(properties), properties_1_1 = properties_1.next(); !properties_1_1.done; properties_1_1 = properties_1.next()) {
55
- var propertyName = properties_1_1.value;
56
- if (stream.hasOwnProperty(propertyName)) {
81
+ var property = properties_1_1.value;
82
+ if (target.hasOwnProperty(property)) {
83
+ continue;
84
+ }
85
+ var descriptor = Object.getOwnPropertyDescriptor(source, property);
86
+ if (!descriptor) {
57
87
  continue;
58
88
  }
59
- var propertyDescriptor = Object.getOwnPropertyDescriptor(message, propertyName);
60
- Object.defineProperty(stream, propertyName, __assign(__assign({}, propertyDescriptor), { value: message[propertyName] }));
89
+ Object.defineProperty(target, property, descriptor);
61
90
  }
62
91
  }
63
92
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -67,11 +96,5 @@ function cloneIncomingMessage(message) {
67
96
  }
68
97
  finally { if (e_1) throw e_1.error; }
69
98
  }
70
- Object.defineProperty(stream, exports.IS_CLONE, {
71
- enumerable: true,
72
- value: true,
73
- });
74
- return stream;
75
99
  }
76
- exports.cloneIncomingMessage = cloneIncomingMessage;
77
100
  //# sourceMappingURL=cloneIncomingMessage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cloneIncomingMessage.js","sourceRoot":"","sources":["../../../../src/interceptors/ClientRequest/utils/cloneIncomingMessage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,iCAAoC;AAEvB,QAAA,QAAQ,GAAG,MAAM,CAAC,SAAS,CAAC,CAAA;AAMzC,SAAgB,oBAAoB,CAClC,OAAwB;;IAExB,IAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,oBAAW,EAAE,CAAC,CAAA;IAC9C,IAAM,UAAU,GAAG,uCACd,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,WACnC,MAAM,CAAC,qBAAqB,CAAC,OAAO,CAAC,EACT,CAAA;;QAEjC,KAA2B,IAAA,eAAA,SAAA,UAAU,CAAA,sCAAA,8DAAE;YAAlC,IAAM,YAAY,uBAAA;YACrB,IAAI,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC,EAAE;gBACvC,SAAQ;aACT;YAED,IAAM,kBAAkB,GAAG,MAAM,CAAC,wBAAwB,CACxD,OAAO,EACP,YAAY,CACb,CAAA;YAED,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,YAAY,wBACrC,kBAAkB,KACrB,KAAK,EAAE,OAAO,CAAC,YAAY,CAAC,IAC5B,CAAA;SACH;;;;;;;;;IAED,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,gBAAQ,EAAE;QACtC,UAAU,EAAE,IAAI;QAChB,KAAK,EAAE,IAAI;KACZ,CAAC,CAAA;IAEF,OAAO,MAA0C,CAAA;AACnD,CAAC;AA/BD,oDA+BC"}
1
+ {"version":3,"file":"cloneIncomingMessage.js","sourceRoot":"","sources":["../../../../src/interceptors/ClientRequest/utils/cloneIncomingMessage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6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AAU,CAAA,sCAAA,8DAAE;YAA9B,IAAM,QAAQ,uBAAA;YACjB,IAAI,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE;gBACnC,SAAQ;aACT;YAED,IAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;YACpE,IAAI,CAAC,UAAU,EAAE;gBACf,SAAQ;aACT;YAED,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAA;SACpD;;;;;;;;;AACH,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mswjs/interceptors",
3
3
  "description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
4
- "version": "0.13.5",
4
+ "version": "0.13.6",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "author": "Artem Zakharchenko",
@@ -1,13 +1,20 @@
1
1
  import { Socket } from 'net'
2
2
  import { IncomingMessage } from 'http'
3
+ import { Stream, Readable, EventEmitter } from 'stream'
3
4
  import { cloneIncomingMessage, IS_CLONE } from './cloneIncomingMessage'
4
5
 
5
6
  test('clones a given IncomingMessage', () => {
6
- const source = new IncomingMessage(new Socket())
7
- source.statusCode = 200
8
- source.statusMessage = 'OK'
9
- source.headers = { 'x-powered-by': 'msw' }
10
- const clone = cloneIncomingMessage(source)
7
+ const message = new IncomingMessage(new Socket())
8
+ message.statusCode = 200
9
+ message.statusMessage = 'OK'
10
+ message.headers = { 'x-powered-by': 'msw' }
11
+ const clone = cloneIncomingMessage(message)
12
+
13
+ // Prototypes must be preserved.
14
+ expect(clone).toBeInstanceOf(IncomingMessage)
15
+ expect(clone).toBeInstanceOf(EventEmitter)
16
+ expect(clone).toBeInstanceOf(Stream)
17
+ expect(clone).toBeInstanceOf(Readable)
11
18
 
12
19
  expect(clone.statusCode).toEqual(200)
13
20
  expect(clone.statusMessage).toEqual('OK')
@@ -15,6 +22,4 @@ test('clones a given IncomingMessage', () => {
15
22
 
16
23
  // Cloned IncomingMessage must be marked respectively.
17
24
  expect(clone[IS_CLONE]).toEqual(true)
18
-
19
- expect(clone).toHaveProperty('_events')
20
25
  })
@@ -7,35 +7,68 @@ export interface ClonedIncomingMessage extends IncomingMessage {
7
7
  [IS_CLONE]: boolean
8
8
  }
9
9
 
10
+ /**
11
+ * Clones a given `http.IncomingMessage` instance.
12
+ */
10
13
  export function cloneIncomingMessage(
11
14
  message: IncomingMessage
12
15
  ): ClonedIncomingMessage {
13
- const stream = message.pipe(new PassThrough())
14
- const properties = [
15
- ...Object.getOwnPropertyNames(message),
16
- ...Object.getOwnPropertySymbols(message),
17
- ] as Array<keyof IncomingMessage>
18
-
19
- for (const propertyName of properties) {
20
- if (stream.hasOwnProperty(propertyName)) {
21
- continue
22
- }
16
+ const clone = message.pipe(new PassThrough())
23
17
 
24
- const propertyDescriptor = Object.getOwnPropertyDescriptor(
25
- message,
26
- propertyName
27
- )
18
+ // Inherit all direct "IncomingMessage" properties.
19
+ inheritProperties(message, clone)
28
20
 
29
- Object.defineProperty(stream, propertyName, {
30
- ...propertyDescriptor,
31
- value: message[propertyName],
32
- })
33
- }
21
+ // Deeply inherit the message prototypes (Readable, Stream, EventEmitter, etc.).
22
+ const clonedPrototype = Object.create(IncomingMessage.prototype)
23
+ getPrototypes(clone).forEach((prototype) => {
24
+ inheritProperties(prototype, clonedPrototype)
25
+ })
26
+ Object.setPrototypeOf(clone, clonedPrototype)
34
27
 
35
- Object.defineProperty(stream, IS_CLONE, {
28
+ Object.defineProperty(clone, IS_CLONE, {
36
29
  enumerable: true,
37
30
  value: true,
38
31
  })
39
32
 
40
- return stream as unknown as ClonedIncomingMessage
33
+ return clone as unknown as ClonedIncomingMessage
34
+ }
35
+
36
+ /**
37
+ * Returns a list of all prototypes the given object extends.
38
+ */
39
+ function getPrototypes(source: object): object[] {
40
+ const prototypes: object[] = []
41
+ let current = source
42
+
43
+ while ((current = Object.getPrototypeOf(current))) {
44
+ prototypes.push(current)
45
+ }
46
+
47
+ return prototypes
48
+ }
49
+
50
+ /**
51
+ * Inherits a given target object properties and symbols
52
+ * onto the given source object.
53
+ * @param source Object which should acquire properties.
54
+ * @param target Object to inherit the properties from.
55
+ */
56
+ function inheritProperties(source: object, target: object): void {
57
+ const properties = [
58
+ ...Object.getOwnPropertyNames(source),
59
+ ...Object.getOwnPropertySymbols(source),
60
+ ]
61
+
62
+ for (const property of properties) {
63
+ if (target.hasOwnProperty(property)) {
64
+ continue
65
+ }
66
+
67
+ const descriptor = Object.getOwnPropertyDescriptor(source, property)
68
+ if (!descriptor) {
69
+ continue
70
+ }
71
+
72
+ Object.defineProperty(target, property, descriptor)
73
+ }
41
74
  }