Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ sls-next-build
.DS_Store
yarn.lock
dist
.vscode
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const create = require("../compatLayer");
const http = require("http");

describe("compatLayer.request", () => {
it("request url path", () => {
Expand Down Expand Up @@ -276,4 +277,33 @@ describe("compatLayer.request", () => {
expect(req.connection).toEqual({});
done();
});

it("request preserve http.IncomingMessage.prototype property", () => {
const exampleProperty = "I'm an example property";
http.IncomingMessage.prototype.exampleProperty = exampleProperty;
const { req } = create({
requestContext: {
path: ""
}
});

expect(typeof req.exampleProperty !== "undefined").toEqual(true);
expect(req.exampleProperty).toEqual(exampleProperty);
});

it("request preserve http.IncomingMessage.prototype function", () => {
const exampleFunction = function() {
return "I'm an example function.";
};
http.IncomingMessage.prototype.exampleFunction = exampleFunction;
const { req } = create({
requestContext: {
path: ""
}
});

expect(typeof req.exampleFunction === "function").toEqual(true);
expect(req.exampleFunction()).toEqual(exampleFunction());
expect(req.exampleFunction.toString()).toEqual(exampleFunction.toString());
});
});
4 changes: 3 additions & 1 deletion packages/apigw-lambda-compat/lib/compatLayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Stream = require("stream");
const queryString = require("querystring");
const http = require("http");

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

const req = new Stream.Readable();
const newStream = new Stream.Readable();
const req = Object.assign(newStream, http.IncomingMessage.prototype);
req.url =
(event.requestContext.path || event.path || "").replace(
new RegExp("^/" + event.requestContext.stage),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const create = require("../next-aws-cloudfront");
const http = require("http");

const { SPECIAL_NODE_HEADERS } = create;

Expand Down Expand Up @@ -194,4 +195,33 @@ describe("Request Tests", () => {
expect(req.connection).toEqual({});
done();
});

it("request preserve http.IncomingMessage.prototype property", () => {
const exampleProperty = "I'm an example property";
http.IncomingMessage.prototype.exampleProperty = exampleProperty;
const { req } = create({
request: {
uri: ""
}
});

expect(typeof req.exampleProperty !== "undefined").toEqual(true);
expect(req.exampleProperty).toEqual(exampleProperty);
});

it("request preserve http.IncomingMessage.prototype function", () => {
const exampleFunction = function() {
return "I'm an example function.";
};
http.IncomingMessage.prototype.exampleFunction = exampleFunction;
const { req } = create({
request: {
uri: ""
}
});

expect(typeof req.exampleFunction === "function").toEqual(true);
expect(req.exampleFunction()).toEqual(exampleFunction());
expect(req.exampleFunction.toString()).toEqual(exampleFunction.toString());
});
});
4 changes: 3 additions & 1 deletion packages/lambda-at-edge-compat/next-aws-cloudfront.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Stream = require("stream");
const zlib = require("zlib");
const http = require("http");

const specialNodeHeaders = [
"age",
Expand Down Expand Up @@ -89,7 +90,8 @@ const handler = event => {
headers: {}
};

const req = new Stream.Readable();
const newStream = new Stream.Readable();
const req = Object.assign(newStream, http.IncomingMessage.prototype);
req.url = cfRequest.uri;
req.method = cfRequest.method;
req.rawHeaders = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/lambda-at-edge-compat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.