Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit d969f96

Browse files
committed
Copy the properties of http.IncomingMessage.prototype to request object by deep cloning
1 parent 47fb657 commit d969f96

File tree

6 files changed

+68
-3
lines changed

6 files changed

+68
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ sls-next-build
1212
.DS_Store
1313
yarn.lock
1414
dist
15+
.vscode

packages/apigw-lambda-compat/lib/__tests__/compatLayer.request.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const create = require("../compatLayer");
2+
const http = require("http");
23

34
describe("compatLayer.request", () => {
45
it("request url path", () => {
@@ -276,4 +277,33 @@ describe("compatLayer.request", () => {
276277
expect(req.connection).toEqual({});
277278
done();
278279
});
280+
281+
it("request preserve http.IncomingMessage.prototype property", () => {
282+
const exampleProperty = "I'm an example property";
283+
http.IncomingMessage.prototype.exampleProperty = exampleProperty;
284+
const { req } = create({
285+
requestContext: {
286+
path: ""
287+
}
288+
});
289+
290+
expect(typeof req.exampleProperty !== "undefined").toEqual(true);
291+
expect(req.exampleProperty).toEqual(exampleProperty);
292+
});
293+
294+
it("request preserve http.IncomingMessage.prototype function", () => {
295+
const exampleFunction = function() {
296+
return "I'm an example function.";
297+
};
298+
http.IncomingMessage.prototype.exampleFunction = exampleFunction;
299+
const { req } = create({
300+
requestContext: {
301+
path: ""
302+
}
303+
});
304+
305+
expect(typeof req.exampleFunction === "function").toEqual(true);
306+
expect(req.exampleFunction()).toEqual(exampleFunction());
307+
expect(req.exampleFunction.toString()).toEqual(exampleFunction.toString());
308+
});
279309
});

packages/apigw-lambda-compat/lib/compatLayer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Stream = require("stream");
22
const queryString = require("querystring");
3+
const http = require("http");
34

45
const reqResMapper = (event, callback) => {
56
const base64Support = process.env.BINARY_SUPPORT === "yes";
@@ -11,7 +12,8 @@ const reqResMapper = (event, callback) => {
1112
};
1213
let responsePromise;
1314

14-
const req = new Stream.Readable();
15+
const newStream = new Stream.Readable();
16+
const req = Object.assign(newStream, http.IncomingMessage.prototype);
1517
req.url =
1618
(event.requestContext.path || event.path || "").replace(
1719
new RegExp("^/" + event.requestContext.stage),

packages/lambda-at-edge-compat/__tests__/next-aws-cloudfront.request.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const create = require("../next-aws-cloudfront");
2+
const http = require("http");
23

34
const { SPECIAL_NODE_HEADERS } = create;
45

@@ -194,4 +195,33 @@ describe("Request Tests", () => {
194195
expect(req.connection).toEqual({});
195196
done();
196197
});
198+
199+
it("request preserve http.IncomingMessage.prototype property", () => {
200+
const exampleProperty = "I'm an example property";
201+
http.IncomingMessage.prototype.exampleProperty = exampleProperty;
202+
const { req } = create({
203+
request: {
204+
uri: ""
205+
}
206+
});
207+
208+
expect(typeof req.exampleProperty !== "undefined").toEqual(true);
209+
expect(req.exampleProperty).toEqual(exampleProperty);
210+
});
211+
212+
it("request preserve http.IncomingMessage.prototype function", () => {
213+
const exampleFunction = function() {
214+
return "I'm an example function.";
215+
};
216+
http.IncomingMessage.prototype.exampleFunction = exampleFunction;
217+
const { req } = create({
218+
request: {
219+
uri: ""
220+
}
221+
});
222+
223+
expect(typeof req.exampleFunction === "function").toEqual(true);
224+
expect(req.exampleFunction()).toEqual(exampleFunction());
225+
expect(req.exampleFunction.toString()).toEqual(exampleFunction.toString());
226+
});
197227
});

packages/lambda-at-edge-compat/next-aws-cloudfront.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Stream = require("stream");
22
const zlib = require("zlib");
3+
const http = require("http");
34

45
const specialNodeHeaders = [
56
"age",
@@ -89,7 +90,8 @@ const handler = event => {
8990
headers: {}
9091
};
9192

92-
const req = new Stream.Readable();
93+
const newStream = new Stream.Readable();
94+
const req = Object.assign(newStream, http.IncomingMessage.prototype);
9395
req.url = cfRequest.uri;
9496
req.method = cfRequest.method;
9597
req.rawHeaders = [];

packages/lambda-at-edge-compat/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)