@octokit/webhooks 11.0.0-beta.7 → 11.0.0-beta.8

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.
package/dist-web/index.js CHANGED
@@ -1,592 +1,616 @@
1
- import AggregateError from 'aggregate-error';
2
- import { verify, sign } 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
- "installation_target",
84
- "installation_target.renamed",
85
- "issue_comment",
86
- "issue_comment.created",
87
- "issue_comment.deleted",
88
- "issue_comment.edited",
89
- "issues",
90
- "issues.assigned",
91
- "issues.closed",
92
- "issues.deleted",
93
- "issues.demilestoned",
94
- "issues.edited",
95
- "issues.labeled",
96
- "issues.locked",
97
- "issues.milestoned",
98
- "issues.opened",
99
- "issues.pinned",
100
- "issues.reopened",
101
- "issues.transferred",
102
- "issues.unassigned",
103
- "issues.unlabeled",
104
- "issues.unlocked",
105
- "issues.unpinned",
106
- "label",
107
- "label.created",
108
- "label.deleted",
109
- "label.edited",
110
- "marketplace_purchase",
111
- "marketplace_purchase.cancelled",
112
- "marketplace_purchase.changed",
113
- "marketplace_purchase.pending_change",
114
- "marketplace_purchase.pending_change_cancelled",
115
- "marketplace_purchase.purchased",
116
- "member",
117
- "member.added",
118
- "member.edited",
119
- "member.removed",
120
- "membership",
121
- "membership.added",
122
- "membership.removed",
123
- "merge_group",
124
- "merge_group.checks_requested",
125
- "meta",
126
- "meta.deleted",
127
- "milestone",
128
- "milestone.closed",
129
- "milestone.created",
130
- "milestone.deleted",
131
- "milestone.edited",
132
- "milestone.opened",
133
- "org_block",
134
- "org_block.blocked",
135
- "org_block.unblocked",
136
- "organization",
137
- "organization.deleted",
138
- "organization.member_added",
139
- "organization.member_invited",
140
- "organization.member_removed",
141
- "organization.renamed",
142
- "package",
143
- "package.published",
144
- "package.updated",
145
- "page_build",
146
- "ping",
147
- "project",
148
- "project.closed",
149
- "project.created",
150
- "project.deleted",
151
- "project.edited",
152
- "project.reopened",
153
- "project_card",
154
- "project_card.converted",
155
- "project_card.created",
156
- "project_card.deleted",
157
- "project_card.edited",
158
- "project_card.moved",
159
- "project_column",
160
- "project_column.created",
161
- "project_column.deleted",
162
- "project_column.edited",
163
- "project_column.moved",
164
- "projects_v2_item",
165
- "projects_v2_item.archived",
166
- "projects_v2_item.converted",
167
- "projects_v2_item.created",
168
- "projects_v2_item.deleted",
169
- "projects_v2_item.edited",
170
- "projects_v2_item.reordered",
171
- "projects_v2_item.restored",
172
- "public",
173
- "pull_request",
174
- "pull_request.assigned",
175
- "pull_request.auto_merge_disabled",
176
- "pull_request.auto_merge_enabled",
177
- "pull_request.closed",
178
- "pull_request.converted_to_draft",
179
- "pull_request.dequeued",
180
- "pull_request.edited",
181
- "pull_request.labeled",
182
- "pull_request.locked",
183
- "pull_request.opened",
184
- "pull_request.queued",
185
- "pull_request.ready_for_review",
186
- "pull_request.reopened",
187
- "pull_request.review_request_removed",
188
- "pull_request.review_requested",
189
- "pull_request.synchronize",
190
- "pull_request.unassigned",
191
- "pull_request.unlabeled",
192
- "pull_request.unlocked",
193
- "pull_request_review",
194
- "pull_request_review.dismissed",
195
- "pull_request_review.edited",
196
- "pull_request_review.submitted",
197
- "pull_request_review_comment",
198
- "pull_request_review_comment.created",
199
- "pull_request_review_comment.deleted",
200
- "pull_request_review_comment.edited",
201
- "pull_request_review_thread",
202
- "pull_request_review_thread.resolved",
203
- "pull_request_review_thread.unresolved",
204
- "push",
205
- "registry_package",
206
- "registry_package.published",
207
- "registry_package.updated",
208
- "release",
209
- "release.created",
210
- "release.deleted",
211
- "release.edited",
212
- "release.prereleased",
213
- "release.published",
214
- "release.released",
215
- "release.unpublished",
216
- "repository",
217
- "repository.archived",
218
- "repository.created",
219
- "repository.deleted",
220
- "repository.edited",
221
- "repository.privatized",
222
- "repository.publicized",
223
- "repository.renamed",
224
- "repository.transferred",
225
- "repository.unarchived",
226
- "repository_dispatch",
227
- "repository_import",
228
- "repository_vulnerability_alert",
229
- "repository_vulnerability_alert.create",
230
- "repository_vulnerability_alert.dismiss",
231
- "repository_vulnerability_alert.reopen",
232
- "repository_vulnerability_alert.resolve",
233
- "secret_scanning_alert",
234
- "secret_scanning_alert.created",
235
- "secret_scanning_alert.reopened",
236
- "secret_scanning_alert.resolved",
237
- "security_advisory",
238
- "security_advisory.performed",
239
- "security_advisory.published",
240
- "security_advisory.updated",
241
- "security_advisory.withdrawn",
242
- "sponsorship",
243
- "sponsorship.cancelled",
244
- "sponsorship.created",
245
- "sponsorship.edited",
246
- "sponsorship.pending_cancellation",
247
- "sponsorship.pending_tier_change",
248
- "sponsorship.tier_changed",
249
- "star",
250
- "star.created",
251
- "star.deleted",
252
- "status",
253
- "team",
254
- "team.added_to_repository",
255
- "team.created",
256
- "team.deleted",
257
- "team.edited",
258
- "team.removed_from_repository",
259
- "team_add",
260
- "watch",
261
- "watch.started",
262
- "workflow_dispatch",
263
- "workflow_job",
264
- "workflow_job.completed",
265
- "workflow_job.in_progress",
266
- "workflow_job.queued",
267
- "workflow_run",
268
- "workflow_run.completed",
269
- "workflow_run.in_progress",
270
- "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.demilestoned",
179
+ "pull_request.dequeued",
180
+ "pull_request.edited",
181
+ "pull_request.labeled",
182
+ "pull_request.locked",
183
+ "pull_request.milestoned",
184
+ "pull_request.opened",
185
+ "pull_request.queued",
186
+ "pull_request.ready_for_review",
187
+ "pull_request.reopened",
188
+ "pull_request.review_request_removed",
189
+ "pull_request.review_requested",
190
+ "pull_request.synchronize",
191
+ "pull_request.unassigned",
192
+ "pull_request.unlabeled",
193
+ "pull_request.unlocked",
194
+ "pull_request_review",
195
+ "pull_request_review.dismissed",
196
+ "pull_request_review.edited",
197
+ "pull_request_review.submitted",
198
+ "pull_request_review_comment",
199
+ "pull_request_review_comment.created",
200
+ "pull_request_review_comment.deleted",
201
+ "pull_request_review_comment.edited",
202
+ "pull_request_review_thread",
203
+ "pull_request_review_thread.resolved",
204
+ "pull_request_review_thread.unresolved",
205
+ "push",
206
+ "registry_package",
207
+ "registry_package.published",
208
+ "registry_package.updated",
209
+ "release",
210
+ "release.created",
211
+ "release.deleted",
212
+ "release.edited",
213
+ "release.prereleased",
214
+ "release.published",
215
+ "release.released",
216
+ "release.unpublished",
217
+ "repository",
218
+ "repository.archived",
219
+ "repository.created",
220
+ "repository.deleted",
221
+ "repository.edited",
222
+ "repository.privatized",
223
+ "repository.publicized",
224
+ "repository.renamed",
225
+ "repository.transferred",
226
+ "repository.unarchived",
227
+ "repository_dispatch",
228
+ "repository_import",
229
+ "repository_vulnerability_alert",
230
+ "repository_vulnerability_alert.create",
231
+ "repository_vulnerability_alert.dismiss",
232
+ "repository_vulnerability_alert.reopen",
233
+ "repository_vulnerability_alert.resolve",
234
+ "secret_scanning_alert",
235
+ "secret_scanning_alert.created",
236
+ "secret_scanning_alert.reopened",
237
+ "secret_scanning_alert.resolved",
238
+ "security_advisory",
239
+ "security_advisory.performed",
240
+ "security_advisory.published",
241
+ "security_advisory.updated",
242
+ "security_advisory.withdrawn",
243
+ "sponsorship",
244
+ "sponsorship.cancelled",
245
+ "sponsorship.created",
246
+ "sponsorship.edited",
247
+ "sponsorship.pending_cancellation",
248
+ "sponsorship.pending_tier_change",
249
+ "sponsorship.tier_changed",
250
+ "star",
251
+ "star.created",
252
+ "star.deleted",
253
+ "status",
254
+ "team",
255
+ "team.added_to_repository",
256
+ "team.created",
257
+ "team.deleted",
258
+ "team.edited",
259
+ "team.removed_from_repository",
260
+ "team_add",
261
+ "watch",
262
+ "watch.started",
263
+ "workflow_dispatch",
264
+ "workflow_job",
265
+ "workflow_job.completed",
266
+ "workflow_job.in_progress",
267
+ "workflow_job.queued",
268
+ "workflow_run",
269
+ "workflow_run.completed",
270
+ "workflow_run.in_progress",
271
+ "workflow_run.requested"
271
272
  ];
272
273
 
274
+ // pkg/dist-src/event-handler/on.js
273
275
  function handleEventHandlers(state, webhookName, handler) {
274
- if (!state.hooks[webhookName]) {
275
- state.hooks[webhookName] = [];
276
- }
277
- state.hooks[webhookName].push(handler);
276
+ if (!state.hooks[webhookName]) {
277
+ state.hooks[webhookName] = [];
278
+ }
279
+ state.hooks[webhookName].push(handler);
278
280
  }
279
281
  function receiverOn(state, webhookNameOrNames, handler) {
280
- if (Array.isArray(webhookNameOrNames)) {
281
- webhookNameOrNames.forEach((webhookName) => receiverOn(state, webhookName, handler));
282
- return;
283
- }
284
- if (["*", "error"].includes(webhookNameOrNames)) {
285
- const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames;
286
- 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`;
287
- throw new Error(message);
288
- }
289
- if (!emitterEventNames.includes(webhookNameOrNames)) {
290
- state.log.warn(`"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)`);
291
- }
292
- handleEventHandlers(state, webhookNameOrNames, handler);
282
+ if (Array.isArray(webhookNameOrNames)) {
283
+ webhookNameOrNames.forEach(
284
+ (webhookName) => receiverOn(state, webhookName, handler)
285
+ );
286
+ return;
287
+ }
288
+ if (["*", "error"].includes(webhookNameOrNames)) {
289
+ const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames;
290
+ 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`;
291
+ throw new Error(message);
292
+ }
293
+ if (!emitterEventNames.includes(webhookNameOrNames)) {
294
+ state.log.warn(
295
+ `"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)`
296
+ );
297
+ }
298
+ handleEventHandlers(state, webhookNameOrNames, handler);
293
299
  }
294
300
  function receiverOnAny(state, handler) {
295
- handleEventHandlers(state, "*", handler);
301
+ handleEventHandlers(state, "*", handler);
296
302
  }
297
303
  function receiverOnError(state, handler) {
298
- handleEventHandlers(state, "error", handler);
304
+ handleEventHandlers(state, "error", handler);
299
305
  }
300
306
 
301
- // Errors thrown or rejected Promises in "error" event handlers are not handled
302
- // as they are in the webhook event handlers. If errors occur, we log a
303
- // "Fatal: Error occurred" message to stdout
307
+ // pkg/dist-src/event-handler/receive.js
308
+ import AggregateError from "aggregate-error";
309
+
310
+ // pkg/dist-src/event-handler/wrap-error-handler.js
304
311
  function wrapErrorHandler(handler, error) {
305
- let returnValue;
306
- try {
307
- returnValue = handler(error);
308
- }
309
- catch (error) {
310
- console.log('FATAL: Error occurred in "error" event handler');
311
- console.log(error);
312
- }
313
- if (returnValue && returnValue.catch) {
314
- returnValue.catch((error) => {
315
- console.log('FATAL: Error occurred in "error" event handler');
316
- console.log(error);
317
- });
318
- }
312
+ let returnValue;
313
+ try {
314
+ returnValue = handler(error);
315
+ } catch (error2) {
316
+ console.log('FATAL: Error occurred in "error" event handler');
317
+ console.log(error2);
318
+ }
319
+ if (returnValue && returnValue.catch) {
320
+ returnValue.catch((error2) => {
321
+ console.log('FATAL: Error occurred in "error" event handler');
322
+ console.log(error2);
323
+ });
324
+ }
319
325
  }
320
326
 
321
- // @ts-ignore to address #245
327
+ // pkg/dist-src/event-handler/receive.js
322
328
  function getHooks(state, eventPayloadAction, eventName) {
323
- const hooks = [state.hooks[eventName], state.hooks["*"]];
324
- if (eventPayloadAction) {
325
- hooks.unshift(state.hooks[`${eventName}.${eventPayloadAction}`]);
326
- }
327
- return [].concat(...hooks.filter(Boolean));
329
+ const hooks = [state.hooks[eventName], state.hooks["*"]];
330
+ if (eventPayloadAction) {
331
+ hooks.unshift(state.hooks[`${eventName}.${eventPayloadAction}`]);
332
+ }
333
+ return [].concat(...hooks.filter(Boolean));
328
334
  }
329
- // main handler function
330
335
  function receiverHandle(state, event) {
331
- const errorHandlers = state.hooks.error || [];
332
- if (event instanceof Error) {
333
- const error = Object.assign(new AggregateError([event]), {
334
- event,
335
- errors: [event],
336
- });
337
- errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
338
- return Promise.reject(error);
339
- }
340
- if (!event || !event.name) {
341
- throw new AggregateError(["Event name not passed"]);
342
- }
343
- if (!event.payload) {
344
- throw new AggregateError(["Event payload not passed"]);
336
+ const errorHandlers = state.hooks.error || [];
337
+ if (event instanceof Error) {
338
+ const error = Object.assign(new AggregateError([event]), {
339
+ event,
340
+ errors: [event]
341
+ });
342
+ errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
343
+ return Promise.reject(error);
344
+ }
345
+ if (!event || !event.name) {
346
+ throw new AggregateError(["Event name not passed"]);
347
+ }
348
+ if (!event.payload) {
349
+ throw new AggregateError(["Event payload not passed"]);
350
+ }
351
+ const hooks = getHooks(
352
+ state,
353
+ "action" in event.payload ? event.payload.action : null,
354
+ event.name
355
+ );
356
+ if (hooks.length === 0) {
357
+ return Promise.resolve();
358
+ }
359
+ const errors = [];
360
+ const promises = hooks.map((handler) => {
361
+ let promise = Promise.resolve(event);
362
+ if (state.transform) {
363
+ promise = promise.then(state.transform);
345
364
  }
346
- // flatten arrays of event listeners and remove undefined values
347
- const hooks = getHooks(state, "action" in event.payload ? event.payload.action : null, event.name);
348
- if (hooks.length === 0) {
349
- return Promise.resolve();
365
+ return promise.then((event2) => {
366
+ return handler(event2);
367
+ }).catch((error) => errors.push(Object.assign(error, { event })));
368
+ });
369
+ return Promise.all(promises).then(() => {
370
+ if (errors.length === 0) {
371
+ return;
350
372
  }
351
- const errors = [];
352
- const promises = hooks.map((handler) => {
353
- let promise = Promise.resolve(event);
354
- if (state.transform) {
355
- promise = promise.then(state.transform);
356
- }
357
- return promise
358
- .then((event) => {
359
- return handler(event);
360
- })
361
- .catch((error) => errors.push(Object.assign(error, { event })));
362
- });
363
- return Promise.all(promises).then(() => {
364
- if (errors.length === 0) {
365
- return;
366
- }
367
- const error = new AggregateError(errors);
368
- Object.assign(error, {
369
- event,
370
- errors,
371
- });
372
- errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
373
- throw error;
373
+ const error = new AggregateError(errors);
374
+ Object.assign(error, {
375
+ event,
376
+ errors
374
377
  });
378
+ errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
379
+ throw error;
380
+ });
375
381
  }
376
382
 
383
+ // pkg/dist-src/event-handler/remove-listener.js
377
384
  function removeListener(state, webhookNameOrNames, handler) {
378
- if (Array.isArray(webhookNameOrNames)) {
379
- webhookNameOrNames.forEach((webhookName) => removeListener(state, webhookName, handler));
380
- return;
381
- }
382
- if (!state.hooks[webhookNameOrNames]) {
383
- return;
384
- }
385
- // remove last hook that has been added, that way
386
- // it behaves the same as removeListener
387
- for (let i = state.hooks[webhookNameOrNames].length - 1; i >= 0; i--) {
388
- if (state.hooks[webhookNameOrNames][i] === handler) {
389
- state.hooks[webhookNameOrNames].splice(i, 1);
390
- return;
391
- }
385
+ if (Array.isArray(webhookNameOrNames)) {
386
+ webhookNameOrNames.forEach(
387
+ (webhookName) => removeListener(state, webhookName, handler)
388
+ );
389
+ return;
390
+ }
391
+ if (!state.hooks[webhookNameOrNames]) {
392
+ return;
393
+ }
394
+ for (let i = state.hooks[webhookNameOrNames].length - 1; i >= 0; i--) {
395
+ if (state.hooks[webhookNameOrNames][i] === handler) {
396
+ state.hooks[webhookNameOrNames].splice(i, 1);
397
+ return;
392
398
  }
399
+ }
393
400
  }
394
401
 
402
+ // pkg/dist-src/event-handler/index.js
395
403
  function createEventHandler(options) {
396
- const state = {
397
- hooks: {},
398
- log: createLogger(options && options.log),
399
- };
400
- if (options && options.transform) {
401
- state.transform = options.transform;
402
- }
403
- return {
404
- on: receiverOn.bind(null, state),
405
- onAny: receiverOnAny.bind(null, state),
406
- onError: receiverOnError.bind(null, state),
407
- removeListener: removeListener.bind(null, state),
408
- receive: receiverHandle.bind(null, state),
409
- };
404
+ const state = {
405
+ hooks: {},
406
+ log: createLogger(options && options.log)
407
+ };
408
+ if (options && options.transform) {
409
+ state.transform = options.transform;
410
+ }
411
+ return {
412
+ on: receiverOn.bind(null, state),
413
+ onAny: receiverOnAny.bind(null, state),
414
+ onError: receiverOnError.bind(null, state),
415
+ removeListener: removeListener.bind(null, state),
416
+ receive: receiverHandle.bind(null, state)
417
+ };
410
418
  }
411
419
 
420
+ // pkg/dist-src/index.js
421
+ import { sign, verify as verify2 } from "@octokit/webhooks-methods";
422
+
423
+ // pkg/dist-src/verify-and-receive.js
424
+ import { verify } from "@octokit/webhooks-methods";
412
425
  async function verifyAndReceive(state, event) {
413
- // verify will validate that the secret is not undefined
414
- const matchesSignature = await verify(state.secret, event.payload, event.signature);
415
- if (!matchesSignature) {
416
- const error = new Error("[@octokit/webhooks] signature does not match event payload and secret");
417
- return state.eventHandler.receive(Object.assign(error, { event, status: 400 }));
418
- }
419
- return state.eventHandler.receive({
420
- id: event.id,
421
- name: event.name,
422
- payload: JSON.parse(event.payload),
423
- });
426
+ const matchesSignature = await verify(
427
+ state.secret,
428
+ event.payload,
429
+ event.signature
430
+ );
431
+ if (!matchesSignature) {
432
+ const error = new Error(
433
+ "[@octokit/webhooks] signature does not match event payload and secret"
434
+ );
435
+ return state.eventHandler.receive(
436
+ Object.assign(error, { event, status: 400 })
437
+ );
438
+ }
439
+ return state.eventHandler.receive({
440
+ id: event.id,
441
+ name: event.name,
442
+ payload: JSON.parse(event.payload)
443
+ });
424
444
  }
425
445
 
426
- const WEBHOOK_HEADERS = [
427
- "x-github-event",
428
- "x-hub-signature-256",
429
- "x-github-delivery",
446
+ // pkg/dist-src/middleware/node/get-missing-headers.js
447
+ var WEBHOOK_HEADERS = [
448
+ "x-github-event",
449
+ "x-hub-signature-256",
450
+ "x-github-delivery"
430
451
  ];
431
- // https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#delivery-headers
432
452
  function getMissingHeaders(request) {
433
- return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
453
+ return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
434
454
  }
435
455
 
436
- // @ts-ignore to address #245
456
+ // pkg/dist-src/middleware/node/get-payload.js
457
+ import AggregateError2 from "aggregate-error";
437
458
  function getPayload(request) {
438
- // If request.body already exists we can stop here
439
- // See https://github.com/octokit/webhooks.js/pull/23
440
- if (request.body)
441
- return Promise.resolve(request.body);
442
- return new Promise((resolve, reject) => {
443
- let data = "";
444
- request.setEncoding("utf8");
445
- // istanbul ignore next
446
- request.on("error", (error) => reject(new AggregateError([error])));
447
- request.on("data", (chunk) => (data += chunk));
448
- request.on("end", () => {
449
- try {
450
- // Call JSON.parse() only to check if the payload is valid JSON
451
- JSON.parse(data);
452
- resolve(data);
453
- }
454
- catch (error) {
455
- error.message = "Invalid JSON";
456
- error.status = 400;
457
- reject(new AggregateError([error]));
458
- }
459
- });
459
+ if (request.body)
460
+ return Promise.resolve(request.body);
461
+ return new Promise((resolve, reject) => {
462
+ let data = "";
463
+ request.setEncoding("utf8");
464
+ request.on("error", (error) => reject(new AggregateError2([error])));
465
+ request.on("data", (chunk) => data += chunk);
466
+ request.on("end", () => {
467
+ try {
468
+ JSON.parse(data);
469
+ resolve(data);
470
+ } catch (error) {
471
+ error.message = "Invalid JSON";
472
+ error.status = 400;
473
+ reject(new AggregateError2([error]));
474
+ }
460
475
  });
476
+ });
461
477
  }
462
478
 
479
+ // pkg/dist-src/middleware/node/on-unhandled-request-default.js
463
480
  function onUnhandledRequestDefault(request, response) {
464
- response.writeHead(404, {
465
- "content-type": "application/json",
466
- });
467
- response.end(JSON.stringify({
468
- error: `Unknown route: ${request.method} ${request.url}`,
469
- }));
481
+ response.writeHead(404, {
482
+ "content-type": "application/json"
483
+ });
484
+ response.end(
485
+ JSON.stringify({
486
+ error: `Unknown route: ${request.method} ${request.url}`
487
+ })
488
+ );
470
489
  }
471
490
 
491
+ // pkg/dist-src/middleware/node/middleware.js
472
492
  async function middleware(webhooks, options, request, response, next) {
473
- let pathname;
474
- try {
475
- pathname = new URL(request.url, "http://localhost").pathname;
476
- }
477
- catch (error) {
478
- response.writeHead(422, {
479
- "content-type": "application/json",
480
- });
481
- response.end(JSON.stringify({
482
- error: `Request URL could not be parsed: ${request.url}`,
483
- }));
484
- return true;
485
- }
486
- if (pathname !== options.path) {
487
- next?.();
488
- return false;
489
- }
490
- else if (request.method !== "POST") {
491
- onUnhandledRequestDefault(request, response);
492
- return true;
493
- }
494
- // Check if the Content-Type header is `application/json` and allow for charset to be specified in it
495
- // Otherwise, return a 415 Unsupported Media Type error
496
- // See https://github.com/octokit/webhooks.js/issues/158
497
- if (!request.headers["content-type"] ||
498
- !request.headers["content-type"].startsWith("application/json")) {
499
- response.writeHead(415, {
500
- "content-type": "application/json",
501
- accept: "application/json",
502
- });
503
- response.end(JSON.stringify({
504
- error: `Unsupported "Content-Type" header value. Must be "application/json"`,
505
- }));
506
- return true;
507
- }
508
- const missingHeaders = getMissingHeaders(request).join(", ");
509
- if (missingHeaders) {
510
- response.writeHead(400, {
511
- "content-type": "application/json",
512
- });
513
- response.end(JSON.stringify({
514
- error: `Required headers missing: ${missingHeaders}`,
515
- }));
516
- return true;
517
- }
518
- const eventName = request.headers["x-github-event"];
519
- const signatureSHA256 = request.headers["x-hub-signature-256"];
520
- const id = request.headers["x-github-delivery"];
521
- options.log.debug(`${eventName} event received (id: ${id})`);
522
- // GitHub will abort the request if it does not receive a response within 10s
523
- // See https://github.com/octokit/webhooks.js/issues/185
524
- let didTimeout = false;
525
- const timeout = setTimeout(() => {
526
- didTimeout = true;
527
- response.statusCode = 202;
528
- response.end("still processing\n");
529
- }, 9000).unref();
530
- try {
531
- const payload = await getPayload(request);
532
- await webhooks.verifyAndReceive({
533
- id: id,
534
- name: eventName,
535
- payload,
536
- signature: signatureSHA256,
537
- });
538
- clearTimeout(timeout);
539
- if (didTimeout)
540
- return true;
541
- response.end("ok\n");
542
- return true;
543
- }
544
- catch (error) {
545
- clearTimeout(timeout);
546
- if (didTimeout)
547
- return true;
548
- const err = Array.from(error)[0];
549
- const errorMessage = err.message
550
- ? `${err.name}: ${err.message}`
551
- : "Error: An Unspecified error occurred";
552
- response.statusCode = typeof err.status !== "undefined" ? err.status : 500;
553
- options.log.error(error);
554
- response.end(JSON.stringify({
555
- error: errorMessage,
556
- }));
557
- return true;
558
- }
559
- }
560
-
561
- function createNodeMiddleware(webhooks, { path = "/api/github/webhooks", log = createLogger(), } = {}) {
562
- return middleware.bind(null, webhooks, {
563
- path,
564
- log,
493
+ let pathname;
494
+ try {
495
+ pathname = new URL(request.url, "http://localhost").pathname;
496
+ } catch (error) {
497
+ response.writeHead(422, {
498
+ "content-type": "application/json"
499
+ });
500
+ response.end(
501
+ JSON.stringify({
502
+ error: `Request URL could not be parsed: ${request.url}`
503
+ })
504
+ );
505
+ return true;
506
+ }
507
+ if (pathname !== options.path) {
508
+ next?.();
509
+ return false;
510
+ } else if (request.method !== "POST") {
511
+ onUnhandledRequestDefault(request, response);
512
+ return true;
513
+ }
514
+ if (!request.headers["content-type"] || !request.headers["content-type"].startsWith("application/json")) {
515
+ response.writeHead(415, {
516
+ "content-type": "application/json",
517
+ accept: "application/json"
518
+ });
519
+ response.end(
520
+ JSON.stringify({
521
+ error: `Unsupported "Content-Type" header value. Must be "application/json"`
522
+ })
523
+ );
524
+ return true;
525
+ }
526
+ const missingHeaders = getMissingHeaders(request).join(", ");
527
+ if (missingHeaders) {
528
+ response.writeHead(400, {
529
+ "content-type": "application/json"
565
530
  });
531
+ response.end(
532
+ JSON.stringify({
533
+ error: `Required headers missing: ${missingHeaders}`
534
+ })
535
+ );
536
+ return true;
537
+ }
538
+ const eventName = request.headers["x-github-event"];
539
+ const signatureSHA256 = request.headers["x-hub-signature-256"];
540
+ const id = request.headers["x-github-delivery"];
541
+ options.log.debug(`${eventName} event received (id: ${id})`);
542
+ let didTimeout = false;
543
+ const timeout = setTimeout(() => {
544
+ didTimeout = true;
545
+ response.statusCode = 202;
546
+ response.end("still processing\n");
547
+ }, 9e3).unref();
548
+ try {
549
+ const payload = await getPayload(request);
550
+ await webhooks.verifyAndReceive({
551
+ id,
552
+ name: eventName,
553
+ payload,
554
+ signature: signatureSHA256
555
+ });
556
+ clearTimeout(timeout);
557
+ if (didTimeout)
558
+ return true;
559
+ response.end("ok\n");
560
+ return true;
561
+ } catch (error) {
562
+ clearTimeout(timeout);
563
+ if (didTimeout)
564
+ return true;
565
+ const err = Array.from(error)[0];
566
+ const errorMessage = err.message ? `${err.name}: ${err.message}` : "Error: An Unspecified error occurred";
567
+ response.statusCode = typeof err.status !== "undefined" ? err.status : 500;
568
+ options.log.error(error);
569
+ response.end(
570
+ JSON.stringify({
571
+ error: errorMessage
572
+ })
573
+ );
574
+ return true;
575
+ }
566
576
  }
567
577
 
568
- // U holds the return value of `transform` function in Options
569
- class Webhooks {
570
- constructor(options) {
571
- if (!options || !options.secret) {
572
- throw new Error("[@octokit/webhooks] options.secret required");
573
- }
574
- const state = {
575
- eventHandler: createEventHandler(options),
576
- secret: options.secret,
577
- hooks: {},
578
- log: createLogger(options.log),
579
- };
580
- this.sign = sign.bind(null, options.secret);
581
- this.verify = verify.bind(null, options.secret);
582
- this.on = state.eventHandler.on;
583
- this.onAny = state.eventHandler.onAny;
584
- this.onError = state.eventHandler.onError;
585
- this.removeListener = state.eventHandler.removeListener;
586
- this.receive = state.eventHandler.receive;
587
- this.verifyAndReceive = verifyAndReceive.bind(null, state);
588
- }
578
+ // pkg/dist-src/middleware/node/index.js
579
+ function createNodeMiddleware(webhooks, {
580
+ path = "/api/github/webhooks",
581
+ log = createLogger()
582
+ } = {}) {
583
+ return middleware.bind(null, webhooks, {
584
+ path,
585
+ log
586
+ });
589
587
  }
590
588
 
591
- export { Webhooks, createEventHandler, createNodeMiddleware, emitterEventNames };
592
- //# sourceMappingURL=index.js.map
589
+ // pkg/dist-src/index.js
590
+ var Webhooks = class {
591
+ constructor(options) {
592
+ if (!options || !options.secret) {
593
+ throw new Error("[@octokit/webhooks] options.secret required");
594
+ }
595
+ const state = {
596
+ eventHandler: createEventHandler(options),
597
+ secret: options.secret,
598
+ hooks: {},
599
+ log: createLogger(options.log)
600
+ };
601
+ this.sign = sign.bind(null, options.secret);
602
+ this.verify = verify2.bind(null, options.secret);
603
+ this.on = state.eventHandler.on;
604
+ this.onAny = state.eventHandler.onAny;
605
+ this.onError = state.eventHandler.onError;
606
+ this.removeListener = state.eventHandler.removeListener;
607
+ this.receive = state.eventHandler.receive;
608
+ this.verifyAndReceive = verifyAndReceive.bind(null, state);
609
+ }
610
+ };
611
+ export {
612
+ Webhooks,
613
+ createEventHandler,
614
+ createNodeMiddleware,
615
+ emitterEventNames
616
+ };