Skip to content

Commit

Permalink
feat: v9 (#533)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `createWebhooksApi()` has been removed. Use `new Webhooks()` instead

BREAKING CHANGE: `webhooks.middleware` has been removed. Use `createNodeMiddleware()` instead

BREAKING CHANGE: `createMiddleware` has been removed. Use `createNodeMiddleware()` instead

BREAKING CHANGE: deprecated `path` option for `Webhooks` constructor has been removed. Use `createNodeMiddleware(webhooks, { path })` instead

BREAKING CHANGE: all usage of [`debug`](https://www.npmjs.com/package/debug) has been removed. Use the `log` option instead

BREAKING CHANGE: `webhooks.sign` now default to `sha256` algorithm. In order to continue to use `sha1`, replace

```js
webhooks.sign(secret, payload)
```

with

```js
webhooks.sign({ secret, algorith: "sha1" }, payload)
```

BREAKING CHANGE: `webhooks.sign()` and `webhooks.verify()` are now asynchronous

BREAKING CHANGE: static `sign` and `verify` methods are no longer exported. Use `@octokit/webhooks-methods` package instead
  • Loading branch information
gr2m authored Apr 14, 2021
1 parent 5bd1901 commit 024e3ee
Show file tree
Hide file tree
Showing 32 changed files with 161 additions and 1,440 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ webhooks.sign(eventPayload);

Returns a `signature` string. Throws error if `eventPayload` is not passed.

Can also be used [standalone](src/sign/).
The `sign` method can be imported as static method from [`@octokit/webhooks-methods`](https://github.com/octokit/webhooks-methods.js/#readme).

### webhooks.verify()

Expand Down Expand Up @@ -216,7 +216,7 @@ webhooks.verify(eventPayload, signature);

Returns `true` or `false`. Throws error if `eventPayload` or `signature` not passed.

Can also be used [standalone](src/verify/).
The `verify` method can be imported as static method from [`@octokit/webhooks-methods`](https://github.com/octokit/webhooks-methods.js/#readme).

### webhooks.verifyAndReceive()

Expand Down Expand Up @@ -306,7 +306,7 @@ eventHandler
id: request.headers["x-github-delivery"],
name: request.headers["x-github-event"],
payload: request.body,
signature: request.headers["x-hub-signature"],
signature: request.headers["x-hub-signature-256"],
})
.catch(handleErrorsFromHooks);
```
Expand Down
66 changes: 46 additions & 20 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
"dependencies": {
"@octokit/request-error": "^2.0.2",
"@octokit/webhooks-definitions": "3.67.3",
"aggregate-error": "^3.1.0",
"debug": "^4.0.0"
"@octokit/webhooks-methods": "^1.0.0",
"aggregate-error": "^3.1.0"
},
"devDependencies": {
"@jest/types": "^26.6.2",
"@octokit/tsconfig": "^1.0.1",
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.9.2",
"@pika/plugin-build-web": "^0.9.2",
"@pika/plugin-ts-standard-pkg": "^0.9.2",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.9",
"@types/json-schema": "^7.0.7",
"@types/node": "^14.0.14",
Expand Down Expand Up @@ -57,6 +57,9 @@
],
[
"@pika/plugin-build-node"
],
[
"@pika/plugin-build-web"
]
]
},
Expand Down
65 changes: 9 additions & 56 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { IncomingMessage, ServerResponse } from "http";
import { sign, verify } from "@octokit/webhooks-methods";

import { createLogger } from "./createLogger";
import { createEventHandler } from "./event-handler/index";
import { createMiddleware } from "./middleware-legacy/index";
import { middleware } from "./middleware-legacy/middleware";
import { verifyAndReceive } from "./middleware-legacy/verify-and-receive";
import { sign } from "./sign/index";
import { verifyAndReceive } from "./verify-and-receive";
import {
EmitterWebhookEvent,
EmitterWebhookEventName,
Expand All @@ -14,14 +12,16 @@ import {
WebhookError,
WebhookEventHandlerError,
} from "./types";
import { verify } from "./verify/index";

export { createNodeMiddleware } from "./middleware/node/index";

// U holds the return value of `transform` function in Options
class Webhooks<TTransformed = unknown> {
public sign: (payload: string | object) => string;
public verify: (eventPayload: string | object, signature: string) => boolean;
public sign: (payload: string | object) => Promise<string>;
public verify: (
eventPayload: string | object,
signature: string
) => Promise<boolean>;
public on: <E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
Expand All @@ -37,34 +37,18 @@ class Webhooks<TTransformed = unknown> {
options: EmitterWebhookEvent & { signature: string }
) => Promise<void>;

/**
* @deprecated use `createNodeMiddleware(webhooks)` instead
*/
public middleware: (
request: IncomingMessage,
response: ServerResponse,
next?: (err?: any) => void
) => void | Promise<void>;

constructor(options: Options<TTransformed> & { secret: string }) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State & { secret: string } = {
eventHandler: createEventHandler(options),
path: options.path || "/",
secret: options.secret,
hooks: {},
log: createLogger(options.log),
};

if ("path" in options) {
state.log.warn(
"[@octokit/webhooks] `path` option is deprecated and will be removed in a future release of `@octokit/webhooks`. Please use `createNodeMiddleware(webhooks, { path })` instead"
);
}

this.sign = sign.bind(null, options.secret);
this.verify = verify.bind(null, options.secret);
this.on = state.eventHandler.on;
Expand All @@ -73,38 +57,7 @@ class Webhooks<TTransformed = unknown> {
this.removeListener = state.eventHandler.removeListener;
this.receive = state.eventHandler.receive;
this.verifyAndReceive = verifyAndReceive.bind(null, state);

this.middleware = function deprecatedMiddleware(
request: IncomingMessage,
response: ServerResponse,
next?: (err?: any) => void
) {
state.log.warn(
"[@octokit/webhooks] `webhooks.middleware` is deprecated and will be removed in a future release of `@octokit/webhooks`. Please use `createNodeMiddleware(webhooks)` instead"
);
return middleware(state, request, response, next);
};
}
}

/** @deprecated `createWebhooksApi()` is deprecated and will be removed in a future release of `@octokit/webhooks`, please use the `Webhooks` class instead */
const createWebhooksApi = <TTransformed>(
options: Options<TTransformed> & { secret: string }
) => {
const log = createLogger(options.log);
log.warn(
"[@octokit/webhooks] `createWebhooksApi()` is deprecated and will be removed in a future release of `@octokit/webhooks`, please use the `Webhooks` class instead"
);
return new Webhooks<TTransformed>(options);
};

export {
createEventHandler,
createMiddleware,
createWebhooksApi,
Webhooks,
EmitterWebhookEvent,
WebhookError,
sign,
verify,
};
export { createEventHandler, Webhooks, EmitterWebhookEvent, WebhookError };
21 changes: 0 additions & 21 deletions src/middleware-legacy/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions src/middleware-legacy/get-missing-headers.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/middleware-legacy/get-payload.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/middleware-legacy/index.ts

This file was deleted.

Loading

0 comments on commit 024e3ee

Please sign in to comment.