Skip to content

Commit aac11fd

Browse files
committed
fix: handle gcf, verify already provided request.body
1 parent 8423803 commit aac11fd

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/middleware/node/get-payload.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ type IncomingMessage = any;
1414
export function getPayload(request: IncomingMessage): Promise<string> {
1515
// If request.body already exists we can stop here
1616
// See https://github.com/octokit/webhooks.js/pull/23
17-
18-
if (request.body) return Promise.resolve(request.body);
19-
2017
return new Promise((resolve, reject) => {
2118
let data = "";
2219

src/middleware/node/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ export async function middleware(
9393
}, 9000).unref();
9494

9595
try {
96-
const payload = await getPayload(request);
96+
const payload = request.body || (await getPayload(request));
9797

9898
await webhooks.verifyAndReceive({
9999
id: id,
100100
name: eventName as any,
101-
payload,
101+
payload: typeof payload === "string" ? payload : JSON.stringify(payload),
102102
signature: signatureSHA256,
103103
});
104104
clearTimeout(timeout);

test/integration/node-middleware.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,51 @@ describe("createNodeMiddleware(webhooks)", () => {
107107
server.close();
108108
});
109109

110+
test("request.body is already an Object (e.g. GCF)", async () => {
111+
expect.assertions(3);
112+
113+
const webhooks = new Webhooks({
114+
secret: "mySecret",
115+
});
116+
const dataChunks: any[] = [];
117+
const middleware = createNodeMiddleware(webhooks);
118+
119+
const server = createServer((req, res) => {
120+
req.once("data", (chunk) => dataChunks.push(chunk));
121+
req.once("end", () => {
122+
// @ts-expect-error - TS2339: Property 'body' does not exist on type 'IncomingMessage'.
123+
req.body = JSON.parse(Buffer.concat(dataChunks).toString());
124+
middleware(req, res);
125+
});
126+
}).listen();
127+
128+
webhooks.on("push", (event) => {
129+
expect(event.id).toBe("123e4567-e89b-12d3-a456-426655440000");
130+
});
131+
132+
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface
133+
const { port } = server.address();
134+
135+
const response = await fetch(
136+
`http://localhost:${port}/api/github/webhooks`,
137+
{
138+
method: "POST",
139+
headers: {
140+
"Content-Type": "application/json",
141+
"X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000",
142+
"X-GitHub-Event": "push",
143+
"X-Hub-Signature-256": signatureSha256,
144+
},
145+
body: pushEventPayload,
146+
},
147+
);
148+
149+
expect(response.status).toEqual(200);
150+
expect(await response.text()).toEqual("ok\n");
151+
152+
server.close();
153+
});
154+
110155
test("Handles invalid Content-Type", async () => {
111156
const webhooks = new Webhooks({
112157
secret: "mySecret",

0 commit comments

Comments
 (0)