// pkg/dist-src/index.js import { Octokit as OctokitCore } from "@octokit/core"; import { createAppAuth as createAppAuth3 } from "@octokit/auth-app"; import { OAuthApp } from "@octokit/oauth-app"; // pkg/dist-src/version.js var VERSION = "15.1.4"; // pkg/dist-src/webhooks.js import { createAppAuth } from "@octokit/auth-app"; import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated"; import { Webhooks } from "@octokit/webhooks"; function webhooks(appOctokit, options) { return new Webhooks({ secret: options.secret, transform: async (event) => { if (!("installation" in event.payload) || typeof event.payload.installation !== "object") { const octokit2 = new appOctokit.constructor({ authStrategy: createUnauthenticatedAuth, auth: { reason: `"installation" key missing in webhook event payload` } }); return { ...event, octokit: octokit2 }; } const installationId = event.payload.installation.id; const octokit = await appOctokit.auth({ type: "installation", installationId, factory(auth) { return new auth.octokit.constructor({ ...auth.octokitOptions, authStrategy: createAppAuth, ...{ auth: { ...auth, installationId } } }); } }); octokit.hook.before("request", (options2) => { options2.headers["x-github-delivery"] = event.id; }); return { ...event, octokit }; } }); } // pkg/dist-src/each-installation.js import { composePaginateRest } from "@octokit/plugin-paginate-rest"; // pkg/dist-src/get-installation-octokit.js import { createAppAuth as createAppAuth2 } from "@octokit/auth-app"; async function getInstallationOctokit(app, installationId) { return app.octokit.auth({ type: "installation", installationId, factory(auth) { const options = { ...auth.octokitOptions, authStrategy: createAppAuth2, ...{ auth: { ...auth, installationId } } }; return new auth.octokit.constructor(options); } }); } // pkg/dist-src/each-installation.js function eachInstallationFactory(app) { return Object.assign(eachInstallation.bind(null, app), { iterator: eachInstallationIterator.bind(null, app) }); } async function eachInstallation(app, callback) { const i = eachInstallationIterator(app)[Symbol.asyncIterator](); let result = await i.next(); while (!result.done) { await callback(result.value); result = await i.next(); } } function eachInstallationIterator(app) { return { async *[Symbol.asyncIterator]() { const iterator = composePaginateRest.iterator( app.octokit, "GET /app/installations" ); for await (const { data: installations } of iterator) { for (const installation of installations) { const installationOctokit = await getInstallationOctokit( app, installation.id ); yield { octokit: installationOctokit, installation }; } } } }; } // pkg/dist-src/each-repository.js import { composePaginateRest as composePaginateRest2 } from "@octokit/plugin-paginate-rest"; function eachRepositoryFactory(app) { return Object.assign(eachRepository.bind(null, app), { iterator: eachRepositoryIterator.bind(null, app) }); } async function eachRepository(app, queryOrCallback, callback) { const i = eachRepositoryIterator( app, callback ? queryOrCallback : void 0 )[Symbol.asyncIterator](); let result = await i.next(); while (!result.done) { if (callback) { await callback(result.value); } else { await queryOrCallback(result.value); } result = await i.next(); } } function singleInstallationIterator(app, installationId) { return { async *[Symbol.asyncIterator]() { yield { octokit: await app.getInstallationOctokit(installationId) }; } }; } function eachRepositoryIterator(app, query) { return { async *[Symbol.asyncIterator]() { const iterator = query ? singleInstallationIterator(app, query.installationId) : app.eachInstallation.iterator(); for await (const { octokit } of iterator) { const repositoriesIterator = composePaginateRest2.iterator( octokit, "GET /installation/repositories" ); for await (const { data: repositories } of repositoriesIterator) { for (const repository of repositories) { yield { octokit, repository }; } } } } }; } // pkg/dist-src/get-installation-url.js function getInstallationUrlFactory(app) { let installationUrlBasePromise; return async function getInstallationUrl(options = {}) { if (!installationUrlBasePromise) { installationUrlBasePromise = getInstallationUrlBase(app); } const installationUrlBase = await installationUrlBasePromise; const installationUrl = new URL(installationUrlBase); if (options.target_id !== void 0) { installationUrl.pathname += "/permissions"; installationUrl.searchParams.append( "target_id", options.target_id.toFixed() ); } if (options.state !== void 0) { installationUrl.searchParams.append("state", options.state); } return installationUrl.href; }; } async function getInstallationUrlBase(app) { const { data: appInfo } = await app.octokit.request("GET /app"); if (!appInfo) { throw new Error("[@octokit/app] unable to fetch metadata for app"); } return `${appInfo.html_url}/installations/new`; } // pkg/dist-src/middleware/node/index.js 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; } } // pkg/dist-src/index.js var App = class { static VERSION = VERSION; static defaults(defaults) { const AppWithDefaults = class extends this { constructor(...args) { super({ ...defaults, ...args[0] }); } }; return AppWithDefaults; } octokit; // @ts-ignore calling app.webhooks will throw a helpful error when options.webhooks is not set webhooks; // @ts-ignore calling app.oauth will throw a helpful error when options.oauth is not set oauth; getInstallationOctokit; eachInstallation; eachRepository; getInstallationUrl; log; constructor(options) { const Octokit = options.Octokit || OctokitCore; const authOptions = Object.assign( { appId: options.appId, privateKey: options.privateKey }, options.oauth ? { clientId: options.oauth.clientId, clientSecret: options.oauth.clientSecret } : {} ); const octokitOptions = { authStrategy: createAppAuth3, auth: authOptions }; if ("log" in options && typeof options.log !== "undefined") { octokitOptions.log = options.log; } this.octokit = new Octokit(octokitOptions); this.log = Object.assign( { debug: () => { }, info: () => { }, warn: console.warn.bind(console), error: console.error.bind(console) }, options.log ); if (options.webhooks) { this.webhooks = webhooks(this.octokit, options.webhooks); } else { Object.defineProperty(this, "webhooks", { get() { throw new Error("[@octokit/app] webhooks option not set"); } }); } if (options.oauth) { this.oauth = new OAuthApp({ ...options.oauth, clientType: "github-app", Octokit }); } else { Object.defineProperty(this, "oauth", { get() { throw new Error( "[@octokit/app] oauth.clientId / oauth.clientSecret options are not set" ); } }); } this.getInstallationOctokit = getInstallationOctokit.bind( null, this ); this.eachInstallation = eachInstallationFactory( this ); this.eachRepository = eachRepositoryFactory( this ); this.getInstallationUrl = getInstallationUrlFactory(this); } }; export { App, createNodeMiddleware };