UNPKG

probot

Version:

A framework for building GitHub Apps to automate and improve your workflow

68 lines (67 loc) • 2.37 kB
const textDecoder = new TextDecoder("utf-8", { fatal: false }); const decode = textDecoder.decode.bind(textDecoder); function concatUint8Array(data) { if (data.length === 0) { // no data received return new Uint8Array(0); } let totalLength = 0; for (let i = 0; i < data.length; i++) { totalLength += data[i].length; } if (totalLength === 0) { // no data received return new Uint8Array(0); } const result = new Uint8Array(totalLength); let offset = 0; for (let i = 0; i < data.length; i++) { result.set(data[i], offset); offset += data[i].length; } return result; } export async function getPayload(request) { if (typeof request.body === "object" && "rawBody" in request && request.rawBody instanceof Uint8Array) { // The body is already an Object and rawBody is a Buffer/Uint8Array (e.g. GCF) return decode(request.rawBody); } else if (typeof request.body === "string") { // The body is a String (e.g. Lambda) return request.body; } // We need to load the payload from the request (normal case of Node.js server) const payload = await getPayloadFromRequestStream(request); return decode(payload); } export function getPayloadFromRequestStream(request) { // We need to load the payload from the request (normal case of Node.js server) return new Promise((resolve, reject) => { const data = []; let settled = false; const rejectRequest = (message, cause) => { if (settled) return; settled = true; data.length = 0; const error = cause || new Error(message); reject(new AggregateError([error], error.message)); }; request.on("error", (error) => rejectRequest(error.message, error)); request.on("aborted", () => rejectRequest("request aborted")); request.on("close", () => rejectRequest("request close")); request.on("data", (chunk) => { if (!settled) data.push(chunk); }); request.on("end", () => { if (settled) return; settled = true; const result = concatUint8Array(data); queueMicrotask(() => resolve(result)); }); }); }