@octokit/webhooks 10.6.2 → 10.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +1 -0
  2. package/dist-node/index.js +440 -106
  3. package/dist-node/index.js.map +6 -1
  4. package/dist-src/createLogger.js +11 -6
  5. package/dist-src/event-handler/index.js +23 -16
  6. package/dist-src/event-handler/on.js +31 -22
  7. package/dist-src/event-handler/receive.js +51 -49
  8. package/dist-src/event-handler/remove-listener.js +18 -15
  9. package/dist-src/event-handler/wrap-error-handler.js +17 -18
  10. package/dist-src/generated/webhook-names.js +260 -257
  11. package/dist-src/index.js +40 -32
  12. package/dist-src/middleware/node/get-missing-headers.js +8 -6
  13. package/dist-src/middleware/node/get-payload.js +26 -27
  14. package/dist-src/middleware/node/index.js +19 -10
  15. package/dist-src/middleware/node/middleware.js +86 -86
  16. package/dist-src/middleware/node/on-unhandled-request-default.js +12 -7
  17. package/dist-src/sign.js +8 -2
  18. package/dist-src/to-normalized-json-string.js +8 -8
  19. package/dist-src/verify-and-receive.js +22 -16
  20. package/dist-src/verify.js +9 -2
  21. package/dist-types/event-handler/on.d.ts +1 -1
  22. package/dist-types/event-handler/remove-listener.d.ts +1 -1
  23. package/dist-types/generated/webhook-names.d.ts +1 -1
  24. package/dist-types/index.d.ts +2 -2
  25. package/dist-types/middleware/node/get-payload.d.ts +1 -1
  26. package/dist-types/middleware/node/index.d.ts +2 -2
  27. package/dist-types/middleware/node/middleware.d.ts +2 -2
  28. package/dist-types/middleware/node/types.d.ts +1 -1
  29. package/dist-types/types.d.ts +2 -2
  30. package/dist-types/verify-and-receive.d.ts +1 -1
  31. package/dist-web/index.js +606 -569
  32. package/dist-web/index.js.map +6 -1
  33. package/package.json +19 -21
  34. package/dist-src/middleware/node/types.js +0 -1
  35. package/dist-src/types.js +0 -1
package/dist-web/index.js CHANGED
@@ -1,631 +1,668 @@
1
- import AggregateError from 'aggregate-error';
2
- import { sign as sign$1, verify as verify$1 } from '@octokit/webhooks-methods';
3
-
4
- const createLogger = (logger) => ({
5
- debug: () => { },
6
- info: () => { },
7
- warn: console.warn.bind(console),
8
- error: console.error.bind(console),
9
- ...logger,
1
+ // pkg/dist-src/createLogger.js
2
+ var createLogger = (logger) => ({
3
+ debug: () => {
4
+ },
5
+ info: () => {
6
+ },
7
+ warn: console.warn.bind(console),
8
+ error: console.error.bind(console),
9
+ ...logger
10
10
  });
11
11
 
12
- // THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY
13
- // make edits in scripts/generate-types.ts
14
- const emitterEventNames = [
15
- "branch_protection_rule",
16
- "branch_protection_rule.created",
17
- "branch_protection_rule.deleted",
18
- "branch_protection_rule.edited",
19
- "check_run",
20
- "check_run.completed",
21
- "check_run.created",
22
- "check_run.requested_action",
23
- "check_run.rerequested",
24
- "check_suite",
25
- "check_suite.completed",
26
- "check_suite.requested",
27
- "check_suite.rerequested",
28
- "code_scanning_alert",
29
- "code_scanning_alert.appeared_in_branch",
30
- "code_scanning_alert.closed_by_user",
31
- "code_scanning_alert.created",
32
- "code_scanning_alert.fixed",
33
- "code_scanning_alert.reopened",
34
- "code_scanning_alert.reopened_by_user",
35
- "commit_comment",
36
- "commit_comment.created",
37
- "create",
38
- "delete",
39
- "dependabot_alert",
40
- "dependabot_alert.created",
41
- "dependabot_alert.dismissed",
42
- "dependabot_alert.fixed",
43
- "dependabot_alert.reintroduced",
44
- "dependabot_alert.reopened",
45
- "deploy_key",
46
- "deploy_key.created",
47
- "deploy_key.deleted",
48
- "deployment",
49
- "deployment.created",
50
- "deployment_status",
51
- "deployment_status.created",
52
- "discussion",
53
- "discussion.answered",
54
- "discussion.category_changed",
55
- "discussion.created",
56
- "discussion.deleted",
57
- "discussion.edited",
58
- "discussion.labeled",
59
- "discussion.locked",
60
- "discussion.pinned",
61
- "discussion.transferred",
62
- "discussion.unanswered",
63
- "discussion.unlabeled",
64
- "discussion.unlocked",
65
- "discussion.unpinned",
66
- "discussion_comment",
67
- "discussion_comment.created",
68
- "discussion_comment.deleted",
69
- "discussion_comment.edited",
70
- "fork",
71
- "github_app_authorization",
72
- "github_app_authorization.revoked",
73
- "gollum",
74
- "installation",
75
- "installation.created",
76
- "installation.deleted",
77
- "installation.new_permissions_accepted",
78
- "installation.suspend",
79
- "installation.unsuspend",
80
- "installation_repositories",
81
- "installation_repositories.added",
82
- "installation_repositories.removed",
83
- "issue_comment",
84
- "issue_comment.created",
85
- "issue_comment.deleted",
86
- "issue_comment.edited",
87
- "issues",
88
- "issues.assigned",
89
- "issues.closed",
90
- "issues.deleted",
91
- "issues.demilestoned",
92
- "issues.edited",
93
- "issues.labeled",
94
- "issues.locked",
95
- "issues.milestoned",
96
- "issues.opened",
97
- "issues.pinned",
98
- "issues.reopened",
99
- "issues.transferred",
100
- "issues.unassigned",
101
- "issues.unlabeled",
102
- "issues.unlocked",
103
- "issues.unpinned",
104
- "label",
105
- "label.created",
106
- "label.deleted",
107
- "label.edited",
108
- "marketplace_purchase",
109
- "marketplace_purchase.cancelled",
110
- "marketplace_purchase.changed",
111
- "marketplace_purchase.pending_change",
112
- "marketplace_purchase.pending_change_cancelled",
113
- "marketplace_purchase.purchased",
114
- "member",
115
- "member.added",
116
- "member.edited",
117
- "member.removed",
118
- "membership",
119
- "membership.added",
120
- "membership.removed",
121
- "merge_group",
122
- "merge_group.checks_requested",
123
- "meta",
124
- "meta.deleted",
125
- "milestone",
126
- "milestone.closed",
127
- "milestone.created",
128
- "milestone.deleted",
129
- "milestone.edited",
130
- "milestone.opened",
131
- "org_block",
132
- "org_block.blocked",
133
- "org_block.unblocked",
134
- "organization",
135
- "organization.deleted",
136
- "organization.member_added",
137
- "organization.member_invited",
138
- "organization.member_removed",
139
- "organization.renamed",
140
- "package",
141
- "package.published",
142
- "package.updated",
143
- "page_build",
144
- "ping",
145
- "project",
146
- "project.closed",
147
- "project.created",
148
- "project.deleted",
149
- "project.edited",
150
- "project.reopened",
151
- "project_card",
152
- "project_card.converted",
153
- "project_card.created",
154
- "project_card.deleted",
155
- "project_card.edited",
156
- "project_card.moved",
157
- "project_column",
158
- "project_column.created",
159
- "project_column.deleted",
160
- "project_column.edited",
161
- "project_column.moved",
162
- "projects_v2_item",
163
- "projects_v2_item.archived",
164
- "projects_v2_item.converted",
165
- "projects_v2_item.created",
166
- "projects_v2_item.deleted",
167
- "projects_v2_item.edited",
168
- "projects_v2_item.reordered",
169
- "projects_v2_item.restored",
170
- "public",
171
- "pull_request",
172
- "pull_request.assigned",
173
- "pull_request.auto_merge_disabled",
174
- "pull_request.auto_merge_enabled",
175
- "pull_request.closed",
176
- "pull_request.converted_to_draft",
177
- "pull_request.dequeued",
178
- "pull_request.edited",
179
- "pull_request.labeled",
180
- "pull_request.locked",
181
- "pull_request.opened",
182
- "pull_request.queued",
183
- "pull_request.ready_for_review",
184
- "pull_request.reopened",
185
- "pull_request.review_request_removed",
186
- "pull_request.review_requested",
187
- "pull_request.synchronize",
188
- "pull_request.unassigned",
189
- "pull_request.unlabeled",
190
- "pull_request.unlocked",
191
- "pull_request_review",
192
- "pull_request_review.dismissed",
193
- "pull_request_review.edited",
194
- "pull_request_review.submitted",
195
- "pull_request_review_comment",
196
- "pull_request_review_comment.created",
197
- "pull_request_review_comment.deleted",
198
- "pull_request_review_comment.edited",
199
- "pull_request_review_thread",
200
- "pull_request_review_thread.resolved",
201
- "pull_request_review_thread.unresolved",
202
- "push",
203
- "registry_package",
204
- "registry_package.published",
205
- "registry_package.updated",
206
- "release",
207
- "release.created",
208
- "release.deleted",
209
- "release.edited",
210
- "release.prereleased",
211
- "release.published",
212
- "release.released",
213
- "release.unpublished",
214
- "repository",
215
- "repository.archived",
216
- "repository.created",
217
- "repository.deleted",
218
- "repository.edited",
219
- "repository.privatized",
220
- "repository.publicized",
221
- "repository.renamed",
222
- "repository.transferred",
223
- "repository.unarchived",
224
- "repository_dispatch",
225
- "repository_import",
226
- "repository_vulnerability_alert",
227
- "repository_vulnerability_alert.create",
228
- "repository_vulnerability_alert.dismiss",
229
- "repository_vulnerability_alert.reopen",
230
- "repository_vulnerability_alert.resolve",
231
- "secret_scanning_alert",
232
- "secret_scanning_alert.created",
233
- "secret_scanning_alert.reopened",
234
- "secret_scanning_alert.resolved",
235
- "security_advisory",
236
- "security_advisory.performed",
237
- "security_advisory.published",
238
- "security_advisory.updated",
239
- "security_advisory.withdrawn",
240
- "sponsorship",
241
- "sponsorship.cancelled",
242
- "sponsorship.created",
243
- "sponsorship.edited",
244
- "sponsorship.pending_cancellation",
245
- "sponsorship.pending_tier_change",
246
- "sponsorship.tier_changed",
247
- "star",
248
- "star.created",
249
- "star.deleted",
250
- "status",
251
- "team",
252
- "team.added_to_repository",
253
- "team.created",
254
- "team.deleted",
255
- "team.edited",
256
- "team.removed_from_repository",
257
- "team_add",
258
- "watch",
259
- "watch.started",
260
- "workflow_dispatch",
261
- "workflow_job",
262
- "workflow_job.completed",
263
- "workflow_job.in_progress",
264
- "workflow_job.queued",
265
- "workflow_run",
266
- "workflow_run.completed",
267
- "workflow_run.in_progress",
268
- "workflow_run.requested",
12
+ // pkg/dist-src/generated/webhook-names.js
13
+ var emitterEventNames = [
14
+ "branch_protection_rule",
15
+ "branch_protection_rule.created",
16
+ "branch_protection_rule.deleted",
17
+ "branch_protection_rule.edited",
18
+ "check_run",
19
+ "check_run.completed",
20
+ "check_run.created",
21
+ "check_run.requested_action",
22
+ "check_run.rerequested",
23
+ "check_suite",
24
+ "check_suite.completed",
25
+ "check_suite.requested",
26
+ "check_suite.rerequested",
27
+ "code_scanning_alert",
28
+ "code_scanning_alert.appeared_in_branch",
29
+ "code_scanning_alert.closed_by_user",
30
+ "code_scanning_alert.created",
31
+ "code_scanning_alert.fixed",
32
+ "code_scanning_alert.reopened",
33
+ "code_scanning_alert.reopened_by_user",
34
+ "commit_comment",
35
+ "commit_comment.created",
36
+ "create",
37
+ "delete",
38
+ "dependabot_alert",
39
+ "dependabot_alert.created",
40
+ "dependabot_alert.dismissed",
41
+ "dependabot_alert.fixed",
42
+ "dependabot_alert.reintroduced",
43
+ "dependabot_alert.reopened",
44
+ "deploy_key",
45
+ "deploy_key.created",
46
+ "deploy_key.deleted",
47
+ "deployment",
48
+ "deployment.created",
49
+ "deployment_status",
50
+ "deployment_status.created",
51
+ "discussion",
52
+ "discussion.answered",
53
+ "discussion.category_changed",
54
+ "discussion.created",
55
+ "discussion.deleted",
56
+ "discussion.edited",
57
+ "discussion.labeled",
58
+ "discussion.locked",
59
+ "discussion.pinned",
60
+ "discussion.transferred",
61
+ "discussion.unanswered",
62
+ "discussion.unlabeled",
63
+ "discussion.unlocked",
64
+ "discussion.unpinned",
65
+ "discussion_comment",
66
+ "discussion_comment.created",
67
+ "discussion_comment.deleted",
68
+ "discussion_comment.edited",
69
+ "fork",
70
+ "github_app_authorization",
71
+ "github_app_authorization.revoked",
72
+ "gollum",
73
+ "installation",
74
+ "installation.created",
75
+ "installation.deleted",
76
+ "installation.new_permissions_accepted",
77
+ "installation.suspend",
78
+ "installation.unsuspend",
79
+ "installation_repositories",
80
+ "installation_repositories.added",
81
+ "installation_repositories.removed",
82
+ "installation_target",
83
+ "installation_target.renamed",
84
+ "issue_comment",
85
+ "issue_comment.created",
86
+ "issue_comment.deleted",
87
+ "issue_comment.edited",
88
+ "issues",
89
+ "issues.assigned",
90
+ "issues.closed",
91
+ "issues.deleted",
92
+ "issues.demilestoned",
93
+ "issues.edited",
94
+ "issues.labeled",
95
+ "issues.locked",
96
+ "issues.milestoned",
97
+ "issues.opened",
98
+ "issues.pinned",
99
+ "issues.reopened",
100
+ "issues.transferred",
101
+ "issues.unassigned",
102
+ "issues.unlabeled",
103
+ "issues.unlocked",
104
+ "issues.unpinned",
105
+ "label",
106
+ "label.created",
107
+ "label.deleted",
108
+ "label.edited",
109
+ "marketplace_purchase",
110
+ "marketplace_purchase.cancelled",
111
+ "marketplace_purchase.changed",
112
+ "marketplace_purchase.pending_change",
113
+ "marketplace_purchase.pending_change_cancelled",
114
+ "marketplace_purchase.purchased",
115
+ "member",
116
+ "member.added",
117
+ "member.edited",
118
+ "member.removed",
119
+ "membership",
120
+ "membership.added",
121
+ "membership.removed",
122
+ "merge_group",
123
+ "merge_group.checks_requested",
124
+ "meta",
125
+ "meta.deleted",
126
+ "milestone",
127
+ "milestone.closed",
128
+ "milestone.created",
129
+ "milestone.deleted",
130
+ "milestone.edited",
131
+ "milestone.opened",
132
+ "org_block",
133
+ "org_block.blocked",
134
+ "org_block.unblocked",
135
+ "organization",
136
+ "organization.deleted",
137
+ "organization.member_added",
138
+ "organization.member_invited",
139
+ "organization.member_removed",
140
+ "organization.renamed",
141
+ "package",
142
+ "package.published",
143
+ "package.updated",
144
+ "page_build",
145
+ "ping",
146
+ "project",
147
+ "project.closed",
148
+ "project.created",
149
+ "project.deleted",
150
+ "project.edited",
151
+ "project.reopened",
152
+ "project_card",
153
+ "project_card.converted",
154
+ "project_card.created",
155
+ "project_card.deleted",
156
+ "project_card.edited",
157
+ "project_card.moved",
158
+ "project_column",
159
+ "project_column.created",
160
+ "project_column.deleted",
161
+ "project_column.edited",
162
+ "project_column.moved",
163
+ "projects_v2_item",
164
+ "projects_v2_item.archived",
165
+ "projects_v2_item.converted",
166
+ "projects_v2_item.created",
167
+ "projects_v2_item.deleted",
168
+ "projects_v2_item.edited",
169
+ "projects_v2_item.reordered",
170
+ "projects_v2_item.restored",
171
+ "public",
172
+ "pull_request",
173
+ "pull_request.assigned",
174
+ "pull_request.auto_merge_disabled",
175
+ "pull_request.auto_merge_enabled",
176
+ "pull_request.closed",
177
+ "pull_request.converted_to_draft",
178
+ "pull_request.dequeued",
179
+ "pull_request.edited",
180
+ "pull_request.labeled",
181
+ "pull_request.locked",
182
+ "pull_request.opened",
183
+ "pull_request.queued",
184
+ "pull_request.ready_for_review",
185
+ "pull_request.reopened",
186
+ "pull_request.review_request_removed",
187
+ "pull_request.review_requested",
188
+ "pull_request.synchronize",
189
+ "pull_request.unassigned",
190
+ "pull_request.unlabeled",
191
+ "pull_request.unlocked",
192
+ "pull_request_review",
193
+ "pull_request_review.dismissed",
194
+ "pull_request_review.edited",
195
+ "pull_request_review.submitted",
196
+ "pull_request_review_comment",
197
+ "pull_request_review_comment.created",
198
+ "pull_request_review_comment.deleted",
199
+ "pull_request_review_comment.edited",
200
+ "pull_request_review_thread",
201
+ "pull_request_review_thread.resolved",
202
+ "pull_request_review_thread.unresolved",
203
+ "push",
204
+ "registry_package",
205
+ "registry_package.published",
206
+ "registry_package.updated",
207
+ "release",
208
+ "release.created",
209
+ "release.deleted",
210
+ "release.edited",
211
+ "release.prereleased",
212
+ "release.published",
213
+ "release.released",
214
+ "release.unpublished",
215
+ "repository",
216
+ "repository.archived",
217
+ "repository.created",
218
+ "repository.deleted",
219
+ "repository.edited",
220
+ "repository.privatized",
221
+ "repository.publicized",
222
+ "repository.renamed",
223
+ "repository.transferred",
224
+ "repository.unarchived",
225
+ "repository_dispatch",
226
+ "repository_import",
227
+ "repository_vulnerability_alert",
228
+ "repository_vulnerability_alert.create",
229
+ "repository_vulnerability_alert.dismiss",
230
+ "repository_vulnerability_alert.reopen",
231
+ "repository_vulnerability_alert.resolve",
232
+ "secret_scanning_alert",
233
+ "secret_scanning_alert.created",
234
+ "secret_scanning_alert.reopened",
235
+ "secret_scanning_alert.resolved",
236
+ "security_advisory",
237
+ "security_advisory.performed",
238
+ "security_advisory.published",
239
+ "security_advisory.updated",
240
+ "security_advisory.withdrawn",
241
+ "sponsorship",
242
+ "sponsorship.cancelled",
243
+ "sponsorship.created",
244
+ "sponsorship.edited",
245
+ "sponsorship.pending_cancellation",
246
+ "sponsorship.pending_tier_change",
247
+ "sponsorship.tier_changed",
248
+ "star",
249
+ "star.created",
250
+ "star.deleted",
251
+ "status",
252
+ "team",
253
+ "team.added_to_repository",
254
+ "team.created",
255
+ "team.deleted",
256
+ "team.edited",
257
+ "team.removed_from_repository",
258
+ "team_add",
259
+ "watch",
260
+ "watch.started",
261
+ "workflow_dispatch",
262
+ "workflow_job",
263
+ "workflow_job.completed",
264
+ "workflow_job.in_progress",
265
+ "workflow_job.queued",
266
+ "workflow_run",
267
+ "workflow_run.completed",
268
+ "workflow_run.in_progress",
269
+ "workflow_run.requested"
269
270
  ];
270
271
 
272
+ // pkg/dist-src/event-handler/on.js
271
273
  function handleEventHandlers(state, webhookName, handler) {
272
- if (!state.hooks[webhookName]) {
273
- state.hooks[webhookName] = [];
274
- }
275
- state.hooks[webhookName].push(handler);
274
+ if (!state.hooks[webhookName]) {
275
+ state.hooks[webhookName] = [];
276
+ }
277
+ state.hooks[webhookName].push(handler);
276
278
  }
277
279
  function receiverOn(state, webhookNameOrNames, handler) {
278
- if (Array.isArray(webhookNameOrNames)) {
279
- webhookNameOrNames.forEach((webhookName) => receiverOn(state, webhookName, handler));
280
- return;
281
- }
282
- if (["*", "error"].includes(webhookNameOrNames)) {
283
- const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames;
284
- const message = `Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.on${webhookName.charAt(0).toUpperCase() + webhookName.slice(1)}() method instead`;
285
- throw new Error(message);
286
- }
287
- if (!emitterEventNames.includes(webhookNameOrNames)) {
288
- state.log.warn(`"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)`);
289
- }
290
- handleEventHandlers(state, webhookNameOrNames, handler);
280
+ if (Array.isArray(webhookNameOrNames)) {
281
+ webhookNameOrNames.forEach(
282
+ (webhookName) => receiverOn(state, webhookName, handler)
283
+ );
284
+ return;
285
+ }
286
+ if (["*", "error"].includes(webhookNameOrNames)) {
287
+ const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames;
288
+ const message = `Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.on${webhookName.charAt(0).toUpperCase() + webhookName.slice(1)}() method instead`;
289
+ throw new Error(message);
290
+ }
291
+ if (!emitterEventNames.includes(webhookNameOrNames)) {
292
+ state.log.warn(
293
+ `"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)`
294
+ );
295
+ }
296
+ handleEventHandlers(state, webhookNameOrNames, handler);
291
297
  }
292
298
  function receiverOnAny(state, handler) {
293
- handleEventHandlers(state, "*", handler);
299
+ handleEventHandlers(state, "*", handler);
294
300
  }
295
301
  function receiverOnError(state, handler) {
296
- handleEventHandlers(state, "error", handler);
302
+ handleEventHandlers(state, "error", handler);
297
303
  }
298
304
 
299
- // Errors thrown or rejected Promises in "error" event handlers are not handled
300
- // as they are in the webhook event handlers. If errors occur, we log a
301
- // "Fatal: Error occurred" message to stdout
305
+ // pkg/dist-src/event-handler/receive.js
306
+ import AggregateError from "aggregate-error";
307
+
308
+ // pkg/dist-src/event-handler/wrap-error-handler.js
302
309
  function wrapErrorHandler(handler, error) {
303
- let returnValue;
304
- try {
305
- returnValue = handler(error);
306
- }
307
- catch (error) {
308
- console.log('FATAL: Error occurred in "error" event handler');
309
- console.log(error);
310
- }
311
- if (returnValue && returnValue.catch) {
312
- returnValue.catch((error) => {
313
- console.log('FATAL: Error occurred in "error" event handler');
314
- console.log(error);
315
- });
316
- }
310
+ let returnValue;
311
+ try {
312
+ returnValue = handler(error);
313
+ } catch (error2) {
314
+ console.log('FATAL: Error occurred in "error" event handler');
315
+ console.log(error2);
316
+ }
317
+ if (returnValue && returnValue.catch) {
318
+ returnValue.catch((error2) => {
319
+ console.log('FATAL: Error occurred in "error" event handler');
320
+ console.log(error2);
321
+ });
322
+ }
317
323
  }
318
324
 
319
- // @ts-ignore to address #245
325
+ // pkg/dist-src/event-handler/receive.js
320
326
  function getHooks(state, eventPayloadAction, eventName) {
321
- const hooks = [state.hooks[eventName], state.hooks["*"]];
322
- if (eventPayloadAction) {
323
- hooks.unshift(state.hooks[`${eventName}.${eventPayloadAction}`]);
324
- }
325
- return [].concat(...hooks.filter(Boolean));
327
+ const hooks = [state.hooks[eventName], state.hooks["*"]];
328
+ if (eventPayloadAction) {
329
+ hooks.unshift(state.hooks[`${eventName}.${eventPayloadAction}`]);
330
+ }
331
+ return [].concat(...hooks.filter(Boolean));
326
332
  }
327
- // main handler function
328
333
  function receiverHandle(state, event) {
329
- const errorHandlers = state.hooks.error || [];
330
- if (event instanceof Error) {
331
- const error = Object.assign(new AggregateError([event]), {
332
- event,
333
- errors: [event],
334
- });
335
- errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
336
- return Promise.reject(error);
337
- }
338
- if (!event || !event.name) {
339
- throw new AggregateError(["Event name not passed"]);
340
- }
341
- if (!event.payload) {
342
- throw new AggregateError(["Event payload not passed"]);
334
+ const errorHandlers = state.hooks.error || [];
335
+ if (event instanceof Error) {
336
+ const error = Object.assign(new AggregateError([event]), {
337
+ event,
338
+ errors: [event]
339
+ });
340
+ errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
341
+ return Promise.reject(error);
342
+ }
343
+ if (!event || !event.name) {
344
+ throw new AggregateError(["Event name not passed"]);
345
+ }
346
+ if (!event.payload) {
347
+ throw new AggregateError(["Event payload not passed"]);
348
+ }
349
+ const hooks = getHooks(
350
+ state,
351
+ "action" in event.payload ? event.payload.action : null,
352
+ event.name
353
+ );
354
+ if (hooks.length === 0) {
355
+ return Promise.resolve();
356
+ }
357
+ const errors = [];
358
+ const promises = hooks.map((handler) => {
359
+ let promise = Promise.resolve(event);
360
+ if (state.transform) {
361
+ promise = promise.then(state.transform);
343
362
  }
344
- // flatten arrays of event listeners and remove undefined values
345
- const hooks = getHooks(state, "action" in event.payload ? event.payload.action : null, event.name);
346
- if (hooks.length === 0) {
347
- return Promise.resolve();
363
+ return promise.then((event2) => {
364
+ return handler(event2);
365
+ }).catch((error) => errors.push(Object.assign(error, { event })));
366
+ });
367
+ return Promise.all(promises).then(() => {
368
+ if (errors.length === 0) {
369
+ return;
348
370
  }
349
- const errors = [];
350
- const promises = hooks.map((handler) => {
351
- let promise = Promise.resolve(event);
352
- if (state.transform) {
353
- promise = promise.then(state.transform);
354
- }
355
- return promise
356
- .then((event) => {
357
- return handler(event);
358
- })
359
- .catch((error) => errors.push(Object.assign(error, { event })));
360
- });
361
- return Promise.all(promises).then(() => {
362
- if (errors.length === 0) {
363
- return;
364
- }
365
- const error = new AggregateError(errors);
366
- Object.assign(error, {
367
- event,
368
- errors,
369
- });
370
- errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
371
- throw error;
371
+ const error = new AggregateError(errors);
372
+ Object.assign(error, {
373
+ event,
374
+ errors
372
375
  });
376
+ errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
377
+ throw error;
378
+ });
373
379
  }
374
380
 
381
+ // pkg/dist-src/event-handler/remove-listener.js
375
382
  function removeListener(state, webhookNameOrNames, handler) {
376
- if (Array.isArray(webhookNameOrNames)) {
377
- webhookNameOrNames.forEach((webhookName) => removeListener(state, webhookName, handler));
378
- return;
379
- }
380
- if (!state.hooks[webhookNameOrNames]) {
381
- return;
382
- }
383
- // remove last hook that has been added, that way
384
- // it behaves the same as removeListener
385
- for (let i = state.hooks[webhookNameOrNames].length - 1; i >= 0; i--) {
386
- if (state.hooks[webhookNameOrNames][i] === handler) {
387
- state.hooks[webhookNameOrNames].splice(i, 1);
388
- return;
389
- }
383
+ if (Array.isArray(webhookNameOrNames)) {
384
+ webhookNameOrNames.forEach(
385
+ (webhookName) => removeListener(state, webhookName, handler)
386
+ );
387
+ return;
388
+ }
389
+ if (!state.hooks[webhookNameOrNames]) {
390
+ return;
391
+ }
392
+ for (let i = state.hooks[webhookNameOrNames].length - 1; i >= 0; i--) {
393
+ if (state.hooks[webhookNameOrNames][i] === handler) {
394
+ state.hooks[webhookNameOrNames].splice(i, 1);
395
+ return;
390
396
  }
397
+ }
391
398
  }
392
399
 
400
+ // pkg/dist-src/event-handler/index.js
393
401
  function createEventHandler(options) {
394
- const state = {
395
- hooks: {},
396
- log: createLogger(options && options.log),
397
- };
398
- if (options && options.transform) {
399
- state.transform = options.transform;
400
- }
401
- return {
402
- on: receiverOn.bind(null, state),
403
- onAny: receiverOnAny.bind(null, state),
404
- onError: receiverOnError.bind(null, state),
405
- removeListener: removeListener.bind(null, state),
406
- receive: receiverHandle.bind(null, state),
407
- };
402
+ const state = {
403
+ hooks: {},
404
+ log: createLogger(options && options.log)
405
+ };
406
+ if (options && options.transform) {
407
+ state.transform = options.transform;
408
+ }
409
+ return {
410
+ on: receiverOn.bind(null, state),
411
+ onAny: receiverOnAny.bind(null, state),
412
+ onError: receiverOnError.bind(null, state),
413
+ removeListener: removeListener.bind(null, state),
414
+ receive: receiverHandle.bind(null, state)
415
+ };
408
416
  }
409
417
 
410
- /**
411
- * GitHub sends its JSON with no indentation and no line break at the end
412
- */
418
+ // pkg/dist-src/sign.js
419
+ import { sign as signMethod } from "@octokit/webhooks-methods";
420
+
421
+ // pkg/dist-src/to-normalized-json-string.js
413
422
  function toNormalizedJsonString(payload) {
414
- const payloadString = JSON.stringify(payload);
415
- return payloadString.replace(/[^\\]\\u[\da-f]{4}/g, (s) => {
416
- return s.substr(0, 3) + s.substr(3).toUpperCase();
417
- });
423
+ const payloadString = JSON.stringify(payload);
424
+ return payloadString.replace(/[^\\]\\u[\da-f]{4}/g, (s) => {
425
+ return s.substr(0, 3) + s.substr(3).toUpperCase();
426
+ });
418
427
  }
419
428
 
429
+ // pkg/dist-src/sign.js
420
430
  async function sign(secret, payload) {
421
- return sign$1(secret, typeof payload === "string" ? payload : toNormalizedJsonString(payload));
431
+ return signMethod(
432
+ secret,
433
+ typeof payload === "string" ? payload : toNormalizedJsonString(payload)
434
+ );
422
435
  }
423
436
 
437
+ // pkg/dist-src/verify.js
438
+ import { verify as verifyMethod } from "@octokit/webhooks-methods";
424
439
  async function verify(secret, payload, signature) {
425
- return verify$1(secret, typeof payload === "string" ? payload : toNormalizedJsonString(payload), signature);
440
+ return verifyMethod(
441
+ secret,
442
+ typeof payload === "string" ? payload : toNormalizedJsonString(payload),
443
+ signature
444
+ );
426
445
  }
427
446
 
447
+ // pkg/dist-src/verify-and-receive.js
448
+ import { verify as verify2 } from "@octokit/webhooks-methods";
428
449
  async function verifyAndReceive(state, event) {
429
- // verify will validate that the secret is not undefined
430
- const matchesSignature = await verify$1(state.secret, typeof event.payload === "object"
431
- ? toNormalizedJsonString(event.payload)
432
- : event.payload, event.signature);
433
- if (!matchesSignature) {
434
- const error = new Error("[@octokit/webhooks] signature does not match event payload and secret");
435
- return state.eventHandler.receive(Object.assign(error, { event, status: 400 }));
436
- }
437
- return state.eventHandler.receive({
438
- id: event.id,
439
- name: event.name,
440
- payload: typeof event.payload === "string"
441
- ? JSON.parse(event.payload)
442
- : event.payload,
443
- });
450
+ const matchesSignature = await verify2(
451
+ state.secret,
452
+ typeof event.payload === "object" ? toNormalizedJsonString(event.payload) : event.payload,
453
+ event.signature
454
+ );
455
+ if (!matchesSignature) {
456
+ const error = new Error(
457
+ "[@octokit/webhooks] signature does not match event payload and secret"
458
+ );
459
+ return state.eventHandler.receive(
460
+ Object.assign(error, { event, status: 400 })
461
+ );
462
+ }
463
+ return state.eventHandler.receive({
464
+ id: event.id,
465
+ name: event.name,
466
+ payload: typeof event.payload === "string" ? JSON.parse(event.payload) : event.payload
467
+ });
444
468
  }
445
469
 
446
- const WEBHOOK_HEADERS = [
447
- "x-github-event",
448
- "x-hub-signature-256",
449
- "x-github-delivery",
470
+ // pkg/dist-src/middleware/node/get-missing-headers.js
471
+ var WEBHOOK_HEADERS = [
472
+ "x-github-event",
473
+ "x-hub-signature-256",
474
+ "x-github-delivery"
450
475
  ];
451
- // https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#delivery-headers
452
476
  function getMissingHeaders(request) {
453
- return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
477
+ return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
454
478
  }
455
479
 
456
- // @ts-ignore to address #245
480
+ // pkg/dist-src/middleware/node/get-payload.js
481
+ import AggregateError2 from "aggregate-error";
457
482
  function getPayload(request) {
458
- // If request.body already exists we can stop here
459
- // See https://github.com/octokit/webhooks.js/pull/23
460
- if (request.body) {
461
- if (typeof request.body !== "string") {
462
- console.warn("[@octokit/webhooks] Passing the payload as a JSON object in `request.body` is deprecated and will be removed in a future release of `@octokit/webhooks`, please pass it as a a `string` instead.");
463
- }
464
- return Promise.resolve(request.body);
483
+ if (request.body) {
484
+ if (typeof request.body !== "string") {
485
+ console.warn(
486
+ "[@octokit/webhooks] Passing the payload as a JSON object in `request.body` is deprecated and will be removed in a future release of `@octokit/webhooks`, please pass it as a a `string` instead."
487
+ );
465
488
  }
466
- return new Promise((resolve, reject) => {
467
- let data = "";
468
- request.setEncoding("utf8");
469
- // istanbul ignore next
470
- request.on("error", (error) => reject(new AggregateError([error])));
471
- request.on("data", (chunk) => (data += chunk));
472
- request.on("end", () => {
473
- try {
474
- // Call JSON.parse() only to check if the payload is valid JSON
475
- JSON.parse(data);
476
- resolve(data);
477
- }
478
- catch (error) {
479
- error.message = "Invalid JSON";
480
- error.status = 400;
481
- reject(new AggregateError([error]));
482
- }
483
- });
489
+ return Promise.resolve(request.body);
490
+ }
491
+ return new Promise((resolve, reject) => {
492
+ let data = "";
493
+ request.setEncoding("utf8");
494
+ request.on("error", (error) => reject(new AggregateError2([error])));
495
+ request.on("data", (chunk) => data += chunk);
496
+ request.on("end", () => {
497
+ try {
498
+ JSON.parse(data);
499
+ resolve(data);
500
+ } catch (error) {
501
+ error.message = "Invalid JSON";
502
+ error.status = 400;
503
+ reject(new AggregateError2([error]));
504
+ }
484
505
  });
506
+ });
485
507
  }
486
508
 
509
+ // pkg/dist-src/middleware/node/middleware.js
487
510
  async function middleware(webhooks, options, request, response, next) {
488
- let pathname;
489
- try {
490
- pathname = new URL(request.url, "http://localhost").pathname;
491
- }
492
- catch (error) {
493
- response.writeHead(422, {
494
- "content-type": "application/json",
495
- });
496
- response.end(JSON.stringify({
497
- error: `Request URL could not be parsed: ${request.url}`,
498
- }));
499
- return;
500
- }
501
- const isUnknownRoute = request.method !== "POST" || pathname !== options.path;
502
- const isExpressMiddleware = typeof next === "function";
503
- if (isUnknownRoute) {
504
- if (isExpressMiddleware) {
505
- return next();
506
- }
507
- else {
508
- return options.onUnhandledRequest(request, response);
509
- }
510
- }
511
- // Check if the Content-Type header is `application/json` and allow for charset to be specified in it
512
- // Otherwise, return a 415 Unsupported Media Type error
513
- // See https://github.com/octokit/webhooks.js/issues/158
514
- if (!request.headers["content-type"] ||
515
- !request.headers["content-type"].startsWith("application/json")) {
516
- response.writeHead(415, {
517
- "content-type": "application/json",
518
- accept: "application/json",
519
- });
520
- response.end(JSON.stringify({
521
- error: `Unsupported "Content-Type" header value. Must be "application/json"`,
522
- }));
523
- return;
524
- }
525
- const missingHeaders = getMissingHeaders(request).join(", ");
526
- if (missingHeaders) {
527
- response.writeHead(400, {
528
- "content-type": "application/json",
529
- });
530
- response.end(JSON.stringify({
531
- error: `Required headers missing: ${missingHeaders}`,
532
- }));
533
- return;
534
- }
535
- const eventName = request.headers["x-github-event"];
536
- const signatureSHA256 = request.headers["x-hub-signature-256"];
537
- const id = request.headers["x-github-delivery"];
538
- options.log.debug(`${eventName} event received (id: ${id})`);
539
- // GitHub will abort the request if it does not receive a response within 10s
540
- // See https://github.com/octokit/webhooks.js/issues/185
541
- let didTimeout = false;
542
- const timeout = setTimeout(() => {
543
- didTimeout = true;
544
- response.statusCode = 202;
545
- response.end("still processing\n");
546
- }, 9000).unref();
547
- try {
548
- const payload = await getPayload(request);
549
- await webhooks.verifyAndReceive({
550
- id: id,
551
- name: eventName,
552
- payload: payload,
553
- signature: signatureSHA256,
554
- });
555
- clearTimeout(timeout);
556
- if (didTimeout)
557
- return;
558
- response.end("ok\n");
559
- }
560
- catch (error) {
561
- clearTimeout(timeout);
562
- if (didTimeout)
563
- return;
564
- const err = Array.from(error)[0];
565
- const errorMessage = err.message
566
- ? `${err.name}: ${err.message}`
567
- : "Error: An Unspecified error occurred";
568
- response.statusCode = typeof err.status !== "undefined" ? err.status : 500;
569
- options.log.error(error);
570
- response.end(JSON.stringify({
571
- error: errorMessage,
572
- }));
511
+ let pathname;
512
+ try {
513
+ pathname = new URL(request.url, "http://localhost").pathname;
514
+ } catch (error) {
515
+ response.writeHead(422, {
516
+ "content-type": "application/json"
517
+ });
518
+ response.end(
519
+ JSON.stringify({
520
+ error: `Request URL could not be parsed: ${request.url}`
521
+ })
522
+ );
523
+ return;
524
+ }
525
+ const isUnknownRoute = request.method !== "POST" || pathname !== options.path;
526
+ const isExpressMiddleware = typeof next === "function";
527
+ if (isUnknownRoute) {
528
+ if (isExpressMiddleware) {
529
+ return next();
530
+ } else {
531
+ return options.onUnhandledRequest(request, response);
573
532
  }
533
+ }
534
+ if (!request.headers["content-type"] || !request.headers["content-type"].startsWith("application/json")) {
535
+ response.writeHead(415, {
536
+ "content-type": "application/json",
537
+ accept: "application/json"
538
+ });
539
+ response.end(
540
+ JSON.stringify({
541
+ error: `Unsupported "Content-Type" header value. Must be "application/json"`
542
+ })
543
+ );
544
+ return;
545
+ }
546
+ const missingHeaders = getMissingHeaders(request).join(", ");
547
+ if (missingHeaders) {
548
+ response.writeHead(400, {
549
+ "content-type": "application/json"
550
+ });
551
+ response.end(
552
+ JSON.stringify({
553
+ error: `Required headers missing: ${missingHeaders}`
554
+ })
555
+ );
556
+ return;
557
+ }
558
+ const eventName = request.headers["x-github-event"];
559
+ const signatureSHA256 = request.headers["x-hub-signature-256"];
560
+ const id = request.headers["x-github-delivery"];
561
+ options.log.debug(`${eventName} event received (id: ${id})`);
562
+ let didTimeout = false;
563
+ const timeout = setTimeout(() => {
564
+ didTimeout = true;
565
+ response.statusCode = 202;
566
+ response.end("still processing\n");
567
+ }, 9e3).unref();
568
+ try {
569
+ const payload = await getPayload(request);
570
+ await webhooks.verifyAndReceive({
571
+ id,
572
+ name: eventName,
573
+ payload,
574
+ signature: signatureSHA256
575
+ });
576
+ clearTimeout(timeout);
577
+ if (didTimeout)
578
+ return;
579
+ response.end("ok\n");
580
+ } catch (error) {
581
+ clearTimeout(timeout);
582
+ if (didTimeout)
583
+ return;
584
+ const err = Array.from(error)[0];
585
+ const errorMessage = err.message ? `${err.name}: ${err.message}` : "Error: An Unspecified error occurred";
586
+ response.statusCode = typeof err.status !== "undefined" ? err.status : 500;
587
+ options.log.error(error);
588
+ response.end(
589
+ JSON.stringify({
590
+ error: errorMessage
591
+ })
592
+ );
593
+ }
574
594
  }
575
595
 
596
+ // pkg/dist-src/middleware/node/on-unhandled-request-default.js
576
597
  function onUnhandledRequestDefault(request, response) {
577
- response.writeHead(404, {
578
- "content-type": "application/json",
579
- });
580
- response.end(JSON.stringify({
581
- error: `Unknown route: ${request.method} ${request.url}`,
582
- }));
598
+ response.writeHead(404, {
599
+ "content-type": "application/json"
600
+ });
601
+ response.end(
602
+ JSON.stringify({
603
+ error: `Unknown route: ${request.method} ${request.url}`
604
+ })
605
+ );
583
606
  }
584
607
 
585
- function createNodeMiddleware(webhooks, { path = "/api/github/webhooks", onUnhandledRequest = onUnhandledRequestDefault, log = createLogger(), } = {}) {
586
- const deprecateOnUnhandledRequest = (request, response) => {
587
- console.warn("[@octokit/webhooks] `onUnhandledRequest()` is deprecated and will be removed in a future release of `@octokit/webhooks`");
588
- return onUnhandledRequest(request, response);
589
- };
590
- return middleware.bind(null, webhooks, {
591
- path,
592
- onUnhandledRequest: deprecateOnUnhandledRequest,
593
- log,
594
- });
608
+ // pkg/dist-src/middleware/node/index.js
609
+ function createNodeMiddleware(webhooks, {
610
+ path = "/api/github/webhooks",
611
+ onUnhandledRequest = onUnhandledRequestDefault,
612
+ log = createLogger()
613
+ } = {}) {
614
+ const deprecateOnUnhandledRequest = (request, response) => {
615
+ console.warn(
616
+ "[@octokit/webhooks] `onUnhandledRequest()` is deprecated and will be removed in a future release of `@octokit/webhooks`"
617
+ );
618
+ return onUnhandledRequest(request, response);
619
+ };
620
+ return middleware.bind(null, webhooks, {
621
+ path,
622
+ onUnhandledRequest: deprecateOnUnhandledRequest,
623
+ log
624
+ });
595
625
  }
596
626
 
597
- // U holds the return value of `transform` function in Options
598
- class Webhooks {
599
- constructor(options) {
600
- if (!options || !options.secret) {
601
- throw new Error("[@octokit/webhooks] options.secret required");
602
- }
603
- const state = {
604
- eventHandler: createEventHandler(options),
605
- secret: options.secret,
606
- hooks: {},
607
- log: createLogger(options.log),
608
- };
609
- this.sign = sign.bind(null, options.secret);
610
- this.verify = (eventPayload, signature) => {
611
- if (typeof eventPayload === "object") {
612
- console.warn("[@octokit/webhooks] Passing a JSON payload object to `verify()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`");
613
- }
614
- return verify(options.secret, eventPayload, signature);
615
- };
616
- this.on = state.eventHandler.on;
617
- this.onAny = state.eventHandler.onAny;
618
- this.onError = state.eventHandler.onError;
619
- this.removeListener = state.eventHandler.removeListener;
620
- this.receive = state.eventHandler.receive;
621
- this.verifyAndReceive = (options) => {
622
- if (typeof options.payload === "object") {
623
- console.warn("[@octokit/webhooks] Passing a JSON payload object to `verifyAndReceive()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`");
624
- }
625
- return verifyAndReceive(state, options);
626
- };
627
+ // pkg/dist-src/index.js
628
+ var Webhooks = class {
629
+ constructor(options) {
630
+ if (!options || !options.secret) {
631
+ throw new Error("[@octokit/webhooks] options.secret required");
627
632
  }
628
- }
629
-
630
- export { Webhooks, createEventHandler, createNodeMiddleware, emitterEventNames };
631
- //# sourceMappingURL=index.js.map
633
+ const state = {
634
+ eventHandler: createEventHandler(options),
635
+ secret: options.secret,
636
+ hooks: {},
637
+ log: createLogger(options.log)
638
+ };
639
+ this.sign = sign.bind(null, options.secret);
640
+ this.verify = (eventPayload, signature) => {
641
+ if (typeof eventPayload === "object") {
642
+ console.warn(
643
+ "[@octokit/webhooks] Passing a JSON payload object to `verify()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`"
644
+ );
645
+ }
646
+ return verify(options.secret, eventPayload, signature);
647
+ };
648
+ this.on = state.eventHandler.on;
649
+ this.onAny = state.eventHandler.onAny;
650
+ this.onError = state.eventHandler.onError;
651
+ this.removeListener = state.eventHandler.removeListener;
652
+ this.receive = state.eventHandler.receive;
653
+ this.verifyAndReceive = (options2) => {
654
+ if (typeof options2.payload === "object") {
655
+ console.warn(
656
+ "[@octokit/webhooks] Passing a JSON payload object to `verifyAndReceive()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`"
657
+ );
658
+ }
659
+ return verifyAndReceive(state, options2);
660
+ };
661
+ }
662
+ };
663
+ export {
664
+ Webhooks,
665
+ createEventHandler,
666
+ createNodeMiddleware,
667
+ emitterEventNames
668
+ };