UNPKG

@octokit/webhooks

Version:

GitHub webhook events toolset for Node.js

32 lines (31 loc) 1.27 kB
// @ts-ignore to address #245 import AggregateError from "aggregate-error"; export function getPayload(request) { // If request.body already exists we can stop here // See https://github.com/octokit/webhooks.js/pull/23 if (request.body) { if (typeof request.body !== "string") { console.warn("[@octokit/webhooks] Passing the payload as a JSON object in `request.body` is deprecated and will be removed in a future release of `@octokit/webhooks`, please pass it as a a `string` instead."); } return Promise.resolve(request.body); } return new Promise((resolve, reject) => { let data = ""; request.setEncoding("utf8"); // istanbul ignore next request.on("error", (error) => reject(new AggregateError([error]))); request.on("data", (chunk) => (data += chunk)); request.on("end", () => { try { // Call JSON.parse() only to check if the payload is valid JSON JSON.parse(data); resolve(data); } catch (error) { error.message = "Invalid JSON"; error.status = 400; reject(new AggregateError([error])); } }); }); }