Skip to content

Commit 6027c6e

Browse files
committed
feat(middleware): PR fixes
1 parent fa5cbe4 commit 6027c6e

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

README.md

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ Returns a promise. Runs all handlers set with [`webhooks.on()`](#webhookson) in
370370

371371
The `.receive()` method belongs to the `event-handler` module which can be used [standalone](src/event-handler/).
372372

373-
The `originalRequest` is an optional parameter, if it is set, it will be available in the `on` functions.
373+
The `originalRequest` is an optional parameter, if it is set, it will be available in the `on` functions.
374374

375375
### webhooks.on()
376376

@@ -581,44 +581,32 @@ createServer(middleware).listen(3000);
581581
<td>
582582
<code>path</code>
583583
<em>
584-
string
584+
string | RegEx
585585
</em>
586586
</td>
587587
<td>
588588
Custom path to match requests against. Defaults to <code>/api/github/webhooks</code>.
589-
</td>
590-
</tr>
591-
<tr>
592-
<td>
593-
<code>pathRegex</code>
594-
<em>
595-
RegEx
596-
</em>
597-
</td>
598-
<td>
599589

600-
Custom path matcher. If set, overrides the <code>path</code> option.
601-
Can be used as;
590+
Can be used as a regular expression;
602591

603592
```js
604-
const middleware = createNodeMiddleware(
605-
webhooks,
606-
{ pathRegex: /^\/api\/github\/webhooks/ }
607-
);
593+
const middleware = createNodeMiddleware(webhooks, {
594+
path: /^\/api\/github\/webhooks/,
595+
});
608596
```
609597

610598
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)!
611599

612600
</td>
613-
</tr>
614-
<tr>
615-
<td>
616-
<code>log</code>
617-
<em>
618-
object
619-
</em>
620-
</td>
621-
<td>
601+
</tr>
602+
<tr>
603+
<td>
604+
<code>log</code>
605+
<em>
606+
object
607+
</em>
608+
</td>
609+
<td>
622610

623611
Used for internal logging. Defaults to [`console`](https://developer.mozilla.org/en-US/docs/Web/API/console) with `debug` and `info` doing nothing.
624612

src/middleware/node/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ export function createNodeMiddleware(
88
webhooks: Webhooks,
99
{
1010
path = "/api/github/webhooks",
11-
pathRegex = undefined,
1211
onUnhandledRequest = onUnhandledRequestDefault,
1312
log = createLogger(),
1413
}: MiddlewareOptions = {}
1514
) {
1615
return middleware.bind(null, webhooks, {
1716
path,
18-
pathRegex,
1917
onUnhandledRequest,
2018
log,
2119
} as Required<MiddlewareOptions>);

src/middleware/node/middleware.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ export async function middleware(
4444
);
4545
return;
4646
}
47-
const pathMatch = options.pathRegex
48-
? options.pathRegex.test(pathname)
49-
: pathname === options.path;
47+
const pathMatch =
48+
options.path instanceof RegExp
49+
? options.path.test(pathname)
50+
: pathname === options.path;
5051
const isUnknownRoute = request.method !== "POST" || !pathMatch;
5152
const isExpressMiddleware = typeof next === "function";
5253
if (isUnknownRoute) {
@@ -87,6 +88,11 @@ export async function middleware(
8788
response.end("still processing\n");
8889
}, 9000);
8990

91+
/* istanbul ignore else */
92+
if (typeof timeout.unref === "function") {
93+
timeout.unref();
94+
}
95+
9096
try {
9197
const payload = await getPayload(request);
9298

src/middleware/node/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { Logger } from "../../createLogger";
66
import { IncomingMessage, ServerResponse } from "./middleware";
77

88
export type MiddlewareOptions = {
9-
path?: string;
10-
pathRegex?: RegExp;
9+
path?: string | RegExp;
1110
log?: Logger;
1211
onUnhandledRequest?: (
1312
request: IncomingMessage,

test/integration/node-middleware.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe("createNodeMiddleware(webhooks)", () => {
6262
server.close();
6363
});
6464

65-
test("pathRegex match", async () => {
65+
test("path match with regex", async () => {
6666
expect.assertions(7);
6767

6868
const webhooks = new Webhooks({
@@ -71,7 +71,7 @@ describe("createNodeMiddleware(webhooks)", () => {
7171

7272
const server = createServer(
7373
createNodeMiddleware(webhooks, {
74-
pathRegex: /^\/api\/github\/webhooks/,
74+
path: /^\/api\/github\/webhooks/,
7575
})
7676
).listen();
7777

@@ -129,7 +129,7 @@ describe("createNodeMiddleware(webhooks)", () => {
129129
server.close();
130130
});
131131

132-
test("original request passed by as intented", async () => {
132+
test("original request passed by as intended", async () => {
133133
expect.assertions(6);
134134

135135
const webhooks = new Webhooks({
@@ -138,7 +138,7 @@ describe("createNodeMiddleware(webhooks)", () => {
138138

139139
const server = createServer(
140140
createNodeMiddleware(webhooks, {
141-
pathRegex: /^\/api\/github\/webhooks/,
141+
path: /^\/api\/github\/webhooks/,
142142
})
143143
).listen();
144144

0 commit comments

Comments
 (0)