Skip to content

Commit bc84845

Browse files
authored
feat!: modernise, ESM, promises, update deps, GHA, auto-release (#58)
1 parent f96bc0d commit bc84845

9 files changed

Lines changed: 391 additions & 252 deletions

File tree

.github/dependabot.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: 'github-actions'
4+
directory: '/'
5+
schedule:
6+
interval: 'weekly'
7+
commit-message:
8+
prefix: 'chore'
9+
include: 'scope'
10+
cooldown:
11+
default-days: 5
12+
- package-ecosystem: 'npm'
13+
directory: '/'
14+
schedule:
15+
interval: 'weekly'
16+
commit-message:
17+
prefix: 'chore'
18+
include: 'scope'
19+
cooldown:
20+
default-days: 5
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Test & Maybe Release
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
node: [lts/*, current]
10+
os: [macos-latest, ubuntu-latest, windows-latest]
11+
runs-on: ${{ matrix.os }}
12+
steps:
13+
- name: Checkout Repository
14+
uses: actions/checkout@v6
15+
- name: Use Node.js ${{ matrix.node }}
16+
uses: actions/setup-node@v6
17+
with:
18+
node-version: ${{ matrix.node }}
19+
- name: Install Dependencies
20+
run: npm install --no-progress
21+
- name: Check build is up to date
22+
run: |
23+
npm run build
24+
git diff --exit-code || (echo "::error::Build artifacts not committed. Run 'npm run build' and commit the changes." && exit 1)
25+
- name: Run tests
26+
run: npm test
27+
28+
release:
29+
name: Release
30+
needs: test
31+
runs-on: ubuntu-latest
32+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
33+
permissions:
34+
contents: write
35+
issues: write
36+
pull-requests: write
37+
id-token: write
38+
steps:
39+
- name: Checkout
40+
uses: actions/checkout@v6
41+
with:
42+
fetch-depth: 0
43+
- name: Setup Node.js
44+
uses: actions/setup-node@v6
45+
with:
46+
node-version: lts/*
47+
registry-url: 'https://registry.npmjs.org'
48+
- name: Install dependencies
49+
run: npm install --no-progress --no-package-lock --no-save
50+
- name: Build
51+
run: npm run build
52+
- name: Release
53+
env:
54+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55+
NPM_CONFIG_PROVENANCE: true
56+
run: npx semantic-release

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.history
3-
.vscode
3+
.vscode
4+
package-lock.json

.travis.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# github-webhook-handler
22

3-
[![Build Status](https://travis-ci.org/rvagg/github-webhook-handler.svg?branch=master)](https://travis-ci.org/rvagg/github-webhook-handler)
4-
5-
[![NPM](https://nodei.co/npm/github-webhook-handler.svg)](https://nodei.co/npm/github-webhook-handler/)
3+
[![NPM](https://nodei.co/npm/github-webhook-handler.svg?style=flat&data=n,v&color=blue)](https://nodei.co/npm/github-webhook-handler/)
64

75
GitHub allows you to register **[Webhooks](https://developer.github.com/webhooks/)** for your repositories. Each time an event occurs on your repository, whether it be pushing code, filling issues or creating pull requests, the webhook address you register can be configured to be pinged with details.
86

97
This library is a small handler (or "middleware" if you must) for Node.js web servers that handles all the logic of receiving and verifying webhook requests from GitHub.
108

9+
## Requirements
10+
11+
Node.js >= 20
12+
1113
## Tips
1214

1315
In Github Webhooks settings, Content type must be `application/json`.
@@ -17,28 +19,29 @@ In Github Webhooks settings, Content type must be `application/json`.
1719
## Example
1820

1921
```js
20-
var http = require('http')
21-
var createHandler = require('github-webhook-handler')
22-
var handler = createHandler({ path: '/webhook', secret: 'myhashsecret' })
22+
import http from 'node:http'
23+
import createHandler from 'github-webhook-handler'
2324

24-
http.createServer(function (req, res) {
25-
handler(req, res, function (err) {
25+
const handler = createHandler({ path: '/webhook', secret: 'myhashsecret' })
26+
27+
http.createServer((req, res) => {
28+
handler(req, res, (err) => {
2629
res.statusCode = 404
2730
res.end('no such location')
2831
})
2932
}).listen(7777)
3033

31-
handler.on('error', function (err) {
34+
handler.on('error', (err) => {
3235
console.error('Error:', err.message)
3336
})
3437

35-
handler.on('push', function (event) {
38+
handler.on('push', (event) => {
3639
console.log('Received a push event for %s to %s',
3740
event.payload.repository.name,
3841
event.payload.ref)
3942
})
4043

41-
handler.on('issues', function (event) {
44+
handler.on('issues', (event) => {
4245
console.log('Received an issue event for %s action=%s: #%d %s',
4346
event.payload.repository.name,
4447
event.payload.action,
@@ -47,11 +50,11 @@ handler.on('issues', function (event) {
4750
})
4851
```
4952

50-
for multiple handlers, please see [multiple-handlers-issue](https://github.com/rvagg/github-webhook-handler/pull/22#issuecomment-274301907).
53+
For multiple handlers, see [multiple-handlers-issue](https://github.com/rvagg/github-webhook-handler/pull/22#issuecomment-274301907).
5154

5255
## API
5356

54-
github-webhook-handler exports a single function, use this function to *create* a webhook handler by passing in an *options* object. Your options object should contain:
57+
github-webhook-handler exports a single function. Use this function to *create* a webhook handler by passing in an *options* object. Your options object should contain:
5558

5659
* `"path"`: the complete case sensitive path/route to match when looking at `req.url` for incoming requests. Any request not matching this path will cause the callback function to the handler to be called (sometimes called the `next` handler).
5760
* `"secret"`: this is a hash key used for creating the SHA-1 HMAC signature of the JSON blob sent by GitHub. You should register the same secret key with GitHub. Any request not delivering a `X-Hub-Signature` that matches the signature generated using this key against the blob will be rejected and cause an `'error'` event (also the callback will be called with an `Error` object).
@@ -66,13 +69,14 @@ See the [GitHub Webhooks documentation](https://developer.github.com/webhooks/)
6669
Included in the distribution is an *events.json* file which maps the event names to descriptions taken from the API:
6770

6871
```js
69-
var events = require('github-webhook-handler/events')
70-
Object.keys(events).forEach(function (event) {
71-
console.log(event, '=', events[event])
72-
})
72+
import events from 'github-webhook-handler/events.json' with { type: 'json' }
73+
74+
for (const [event, description] of Object.entries(events)) {
75+
console.log(event, '=', description)
76+
}
7377
```
7478

75-
Additionally, there is a special `'*'` even you can listen to in order to receive _everything_.
79+
Additionally, there is a special `'*'` event you can listen to in order to receive _everything_.
7680

7781
## License
7882

github-webhook-handler.d.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
///<reference types="node" />
1+
/// <reference types="node" />
22

3-
import { IncomingMessage, ServerResponse } from "http";
4-
import { EventEmitter } from "events";
3+
import { IncomingMessage, ServerResponse } from 'node:http'
4+
import { EventEmitter } from 'node:events'
55

66
interface CreateHandlerOptions {
7-
path: string;
8-
secret: string;
9-
events?: string | string[];
7+
path: string
8+
secret: string
9+
events?: string | string[]
1010
}
1111

12-
interface handler extends EventEmitter {
13-
(req: IncomingMessage, res: ServerResponse, callback: (err: Error) => void): void;
12+
interface Handler extends EventEmitter {
13+
(req: IncomingMessage, res: ServerResponse, callback: (err?: Error) => void): void
14+
sign(data: string | Buffer): string
15+
verify(signature: string, data: string | Buffer): boolean
1416
}
1517

16-
declare function createHandler(options: CreateHandlerOptions|CreateHandlerOptions[]): handler;
18+
declare function createHandler (options: CreateHandlerOptions | CreateHandlerOptions[]): Handler
1719

18-
export = createHandler;
20+
export default createHandler

github-webhook-handler.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const EventEmitter = require('events')
2-
const crypto = require('crypto')
3-
const bl = require('bl')
1+
import { EventEmitter } from 'node:events'
2+
import crypto from 'node:crypto'
3+
import bl from 'bl'
44

55
function findHandler (url, arr) {
66
if (!Array.isArray(arr)) {
@@ -23,17 +23,16 @@ function checkType (options) {
2323
}
2424

2525
if (typeof options.path !== 'string') {
26-
throw new TypeError('must provide a \'path\' option')
26+
throw new TypeError("must provide a 'path' option")
2727
}
2828

2929
if (typeof options.secret !== 'string') {
30-
throw new TypeError('must provide a \'secret\' option')
30+
throw new TypeError("must provide a 'secret' option")
3131
}
3232
}
3333

3434
function create (initOptions) {
3535
let options
36-
// validate type of options
3736
if (Array.isArray(initOptions)) {
3837
for (let i = 0; i < initOptions.length; i++) {
3938
checkType(initOptions[i])
@@ -42,7 +41,6 @@ function create (initOptions) {
4241
checkType(initOptions)
4342
}
4443

45-
// make it an EventEmitter
4644
Object.setPrototypeOf(handler, EventEmitter.prototype)
4745
EventEmitter.call(handler)
4846

@@ -130,8 +128,8 @@ function create (initOptions) {
130128
res.end('{"ok":true}')
131129

132130
const emitData = {
133-
event: event,
134-
id: id,
131+
event,
132+
id,
135133
payload: obj,
136134
protocol: req.protocol,
137135
host: req.headers.host,
@@ -145,4 +143,4 @@ function create (initOptions) {
145143
}
146144
}
147145

148-
module.exports = create
146+
export default create

0 commit comments

Comments
 (0)