Upgrade octokit to v4.1.2

This commit is contained in:
Angela P Wen 2025-02-19 11:13:12 -08:00
parent dbbcbe019d
commit c1745a9831
1214 changed files with 160765 additions and 0 deletions

View file

@ -0,0 +1,11 @@
const WEBHOOK_HEADERS = [
"x-github-event",
"x-hub-signature-256",
"x-github-delivery"
];
function getMissingHeaders(request) {
return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
}
export {
getMissingHeaders
};

View file

@ -0,0 +1,29 @@
function getPayload(request) {
if (typeof request.body === "object" && "rawBody" in request && request.rawBody instanceof Buffer) {
return Promise.resolve(request.rawBody.toString("utf8"));
} else if (typeof request.body === "string") {
return Promise.resolve(request.body);
}
return new Promise((resolve, reject) => {
let data = [];
request.on(
"error",
(error) => reject(new AggregateError([error], error.message))
);
request.on("data", (chunk) => data.push(chunk));
request.on(
"end",
() => (
// setImmediate improves the throughput by reducing the pressure from
// the event loop
setImmediate(
resolve,
data.length === 1 ? data[0].toString("utf8") : Buffer.concat(data).toString("utf8")
)
)
);
});
}
export {
getPayload
};

View file

@ -0,0 +1,14 @@
import { createLogger } from "../../createLogger.js";
import { middleware } from "./middleware.js";
function createNodeMiddleware(webhooks, {
path = "/api/github/webhooks",
log = createLogger()
} = {}) {
return middleware.bind(null, webhooks, {
path,
log
});
}
export {
createNodeMiddleware
};

View file

@ -0,0 +1,89 @@
import { getMissingHeaders } from "./get-missing-headers.js";
import { getPayload } from "./get-payload.js";
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.js";
async function middleware(webhooks, options, request, response, next) {
let pathname;
try {
pathname = new URL(request.url, "http://localhost").pathname;
} catch (error) {
response.writeHead(422, {
"content-type": "application/json"
});
response.end(
JSON.stringify({
error: `Request URL could not be parsed: ${request.url}`
})
);
return true;
}
if (pathname !== options.path) {
next?.();
return false;
} else if (request.method !== "POST") {
onUnhandledRequestDefault(request, response);
return true;
}
if (!request.headers["content-type"] || !request.headers["content-type"].startsWith("application/json")) {
response.writeHead(415, {
"content-type": "application/json",
accept: "application/json"
});
response.end(
JSON.stringify({
error: `Unsupported "Content-Type" header value. Must be "application/json"`
})
);
return true;
}
const missingHeaders = getMissingHeaders(request).join(", ");
if (missingHeaders) {
response.writeHead(400, {
"content-type": "application/json"
});
response.end(
JSON.stringify({
error: `Required headers missing: ${missingHeaders}`
})
);
return true;
}
const eventName = request.headers["x-github-event"];
const signatureSHA256 = request.headers["x-hub-signature-256"];
const id = request.headers["x-github-delivery"];
options.log.debug(`${eventName} event received (id: ${id})`);
let didTimeout = false;
const timeout = setTimeout(() => {
didTimeout = true;
response.statusCode = 202;
response.end("still processing\n");
}, 9e3).unref();
try {
const payload = await getPayload(request);
await webhooks.verifyAndReceive({
id,
name: eventName,
payload,
signature: signatureSHA256
});
clearTimeout(timeout);
if (didTimeout) return true;
response.end("ok\n");
return true;
} catch (error) {
clearTimeout(timeout);
if (didTimeout) return true;
const err = Array.from(error.errors)[0];
const errorMessage = err.message ? `${err.name}: ${err.message}` : "Error: An Unspecified error occurred";
response.statusCode = typeof err.status !== "undefined" ? err.status : 500;
options.log.error(error);
response.end(
JSON.stringify({
error: errorMessage
})
);
return true;
}
}
export {
middleware
};

View file

@ -0,0 +1,13 @@
function onUnhandledRequestDefault(request, response) {
response.writeHead(404, {
"content-type": "application/json"
});
response.end(
JSON.stringify({
error: `Unknown route: ${request.method} ${request.url}`
})
);
}
export {
onUnhandledRequestDefault
};