Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware): oauth-app has no onUnhandledRequest option #337

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ Middleware for Node's built in http server or [`express`](https://expressjs.com/

```js
const { App, createNodeMiddleware } = require("@octokit/app");

const app = new App({
appId: 123,
privateKey: "-----BEGIN PRIVATE KEY-----\n...",
Expand All @@ -334,11 +335,20 @@ const app = new App({
});

const middleware = createNodeMiddleware(app);

require("http").createServer(middleware).listen(3000);
require("http")
.createServer(async (req, res) => {
// `middleware` returns `false` when `req` is unhandled (beyond `/api/github`)
if (await middleware(req, res)) return;
res.writeHead(404);
res.end();
})
.listen(3000);
// can now receive user authorization callbacks at /api/github/*
```

The middleware returned from `createNodeMiddleware` can also serve as an
`Express.js` middleware directly.

<table width="100%">
<thead align=left>
<tr>
Expand Down Expand Up @@ -391,31 +401,6 @@ Used for internal logging. Defaults to [`console`](https://developer.mozilla.org

</td>
</tr>
<tr>
<th>
<code>options.onUnhandledRequest</code>
</th>
<th>
<code>function</code>
</th>
<td>

Defaults to

```js
function onUnhandledRequest(request, response) {
response.writeHead(400, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: error.message,
})
);
}
```

</td></tr>
</tbody>
</table>

Expand Down
77 changes: 57 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"@octokit/auth-app": "^4.0.0",
"@octokit/auth-unauthenticated": "^3.0.0",
"@octokit/core": "^4.0.0",
"@octokit/oauth-app": "^4.0.7",
"@octokit/oauth-app": "5.0.0-beta.1",
"@octokit/plugin-paginate-rest": "^4.0.0",
"@octokit/types": "^7.0.0",
"@octokit/webhooks": "^10.0.0"
"@octokit/webhooks": "11.0.0-beta.1"
},
"devDependencies": {
"@pika/pack": "^0.3.7",
Expand Down
54 changes: 26 additions & 28 deletions src/middleware/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
type IncomingMessage = any;
type ServerResponse = any;

import { createNodeMiddleware as oauthNodeMiddleware } from "@octokit/oauth-app";
import {
createNodeMiddleware as oauthNodeMiddleware,
sendNodeResponse,
unknownRouteResponse,
} from "@octokit/oauth-app";
import { createNodeMiddleware as webhooksNodeMiddleware } from "@octokit/webhooks";

import { App } from "../../index";
import { onUnhandledRequestDefault } from "./on-unhandled-request-default";
import { Options } from "../../types";

export type MiddlewareOptions = {
pathPrefix?: string;
log?: Options["log"];
onUnhandledRequest?: (
request: IncomingMessage,
response: ServerResponse
) => void;
};

function noop() {}
Expand All @@ -37,7 +36,6 @@ export function createNodeMiddleware(
);

const optionsWithDefaults = {
onUnhandledRequest: onUnhandledRequestDefault,
pathPrefix: "/api/github",
...options,
log,
Expand All @@ -46,41 +44,41 @@ export function createNodeMiddleware(
const webhooksMiddleware = webhooksNodeMiddleware(app.webhooks, {
path: optionsWithDefaults.pathPrefix + "/webhooks",
log,
onUnhandledRequest: optionsWithDefaults.onUnhandledRequest,
});

const oauthMiddleware = oauthNodeMiddleware(app.oauth, {
pathPrefix: optionsWithDefaults.pathPrefix + "/oauth",
onUnhandledRequest: optionsWithDefaults.onUnhandledRequest,
});

return middleware.bind(null, optionsWithDefaults, {
return middleware.bind(
null,
optionsWithDefaults.pathPrefix,
webhooksMiddleware,
oauthMiddleware,
});
oauthMiddleware
);
}

export async function middleware(
options: Required<MiddlewareOptions>,
{ webhooksMiddleware, oauthMiddleware }: any,
pathPrefix: string,
webhooksMiddleware: any,
oauthMiddleware: any,
request: IncomingMessage,
response: ServerResponse,
next?: Function
) {
): Promise<boolean> {
const { pathname } = new URL(request.url as string, "http://localhost");

if (pathname === `${options.pathPrefix}/webhooks`) {
return webhooksMiddleware(request, response, next);
}
if (pathname.startsWith(`${options.pathPrefix}/oauth/`)) {
return oauthMiddleware(request, response, next);
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;
}

const isExpressMiddleware = typeof next === "function";
if (isExpressMiddleware) {
// @ts-ignore `next` must be a function as we check two lines above
return next();
}

return options.onUnhandledRequest(request, response);
}
19 changes: 0 additions & 19 deletions src/middleware/node/on-unhandled-request-default.ts

This file was deleted.

Loading