diff --git a/README.md b/README.md index 915d9283..0dc64acc 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ const webhooks = new Webhooks({ secret: "mysecret", }); -webhooks.onAny(({ id, name, payload }) => { +webhooks.onAny(({ id, name, payload, extraData }) => { console.log(name, "event received"); }); @@ -223,7 +223,7 @@ The `verify` method can be imported as static method from [`@octokit/webhooks-me ### webhooks.verifyAndReceive() ```js -webhooks.verifyAndReceive({ id, name, payload, signature }); +webhooks.verifyAndReceive({ id, name, payload, extraData, signature }); ```
path
- string
+ string | RegEx
|
Custom path to match requests against. Defaults to /api/github/webhooks .
- |
-
-
- log
-
- object
-
- |
- + +Can be used as a regular expression; + +```js +const middleware = createNodeMiddleware(webhooks, { + path: /^\/api\/github\/webhooks/, +}); +``` + +Test the regex before usage, the `g` and `y` flags [makes it stateful](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test)! + + | +
+log
+
+object
+
+ |
+
Used for internal logging. Defaults to [`console`](https://developer.mozilla.org/en-US/docs/Web/API/console) with `debug` and `info` doing nothing.
@@ -721,7 +734,7 @@ A union of all possible events and event/action combinations supported by the ev
### `EmitterWebhookEvent`
-The object that is emitted by `@octokit/webhooks` as an event; made up of an `id`, `name`, and `payload` properties.
+The object that is emitted by `@octokit/webhooks` as an event; made up of an `id`, `name`, and `payload` properties, with an optional `extraData`.
An optional generic parameter can be passed to narrow the type of the `name` and `payload` properties based on event names or event/action combinations, e.g. `EmitterWebhookEvent<"check_run" | "code_scanning_alert.fixed">`.
## License
diff --git a/src/middleware/node/middleware.ts b/src/middleware/node/middleware.ts
index 95a25cf3..bd46651d 100644
--- a/src/middleware/node/middleware.ts
+++ b/src/middleware/node/middleware.ts
@@ -33,8 +33,11 @@ export async function middleware(
);
return;
}
-
- const isUnknownRoute = request.method !== "POST" || pathname !== options.path;
+ const pathMatch =
+ options.path instanceof RegExp
+ ? options.path.test(pathname)
+ : pathname === options.path;
+ const isUnknownRoute = request.method !== "POST" || !pathMatch;
const isExpressMiddleware = typeof next === "function";
if (isUnknownRoute) {
if (isExpressMiddleware) {
@@ -72,7 +75,12 @@ export async function middleware(
didTimeout = true;
response.statusCode = 202;
response.end("still processing\n");
- }, 9000).unref();
+ }, 9000);
+
+ /* istanbul ignore else */
+ if (typeof timeout.unref === "function") {
+ timeout.unref();
+ }
try {
const payload = await getPayload(request);
@@ -82,6 +90,7 @@ export async function middleware(
name: eventName as any,
payload: payload as any,
signature: signatureSHA256,
+ extraData: request,
});
clearTimeout(timeout);
diff --git a/src/middleware/node/types.ts b/src/middleware/node/types.ts
index 81c4e0ed..b8b49069 100644
--- a/src/middleware/node/types.ts
+++ b/src/middleware/node/types.ts
@@ -7,7 +7,7 @@ type ServerResponse = any;
import { Logger } from "../../createLogger";
export type MiddlewareOptions = {
- path?: string;
+ path?: string | RegExp;
log?: Logger;
onUnhandledRequest?: (
request: IncomingMessage,
diff --git a/src/types.ts b/src/types.ts
index a2eedbe4..2e96619c 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -12,6 +12,8 @@ export type EmitterWebhookEvent<
> = TEmitterEvent extends `${infer TWebhookEvent}.${infer TAction}`
? BaseWebhookEvent |