Files
codeql-action/node_modules/@octokit/app/dist-src/middleware/node/index.js
2025-02-19 11:13:12 -08:00

58 lines
1.5 KiB
JavaScript

import {
createNodeMiddleware as oauthNodeMiddleware,
sendNodeResponse,
unknownRouteResponse
} from "@octokit/oauth-app";
import { createNodeMiddleware as webhooksNodeMiddleware } from "@octokit/webhooks";
function noop() {
}
function createNodeMiddleware(app, options = {}) {
const log = Object.assign(
{
debug: noop,
info: noop,
warn: console.warn.bind(console),
error: console.error.bind(console)
},
options.log
);
const optionsWithDefaults = {
pathPrefix: "/api/github",
...options,
log
};
const webhooksMiddleware = webhooksNodeMiddleware(app.webhooks, {
path: optionsWithDefaults.pathPrefix + "/webhooks",
log
});
const oauthMiddleware = oauthNodeMiddleware(app.oauth, {
pathPrefix: optionsWithDefaults.pathPrefix + "/oauth"
});
return middleware.bind(
null,
optionsWithDefaults.pathPrefix,
webhooksMiddleware,
oauthMiddleware
);
}
async function middleware(pathPrefix, webhooksMiddleware, oauthMiddleware, request, response, next) {
const { pathname } = new URL(request.url, "http://localhost");
if (pathname.startsWith(`${pathPrefix}/`)) {
if (pathname === `${pathPrefix}/webhooks`) {
webhooksMiddleware(request, response);
} else if (pathname.startsWith(`${pathPrefix}/oauth/`)) {
oauthMiddleware(request, response);
} else {
sendNodeResponse(unknownRouteResponse(request), response);
}
return true;
} else {
next?.();
return false;
}
}
export {
createNodeMiddleware,
middleware
};