@octokit/webhooks 11.0.0-beta.6 → 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,589 +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
- "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.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"
269
272
  ];
270
273
 
274
+ // pkg/dist-src/event-handler/on.js
271
275
  function handleEventHandlers(state, webhookName, handler) {
272
- if (!state.hooks[webhookName]) {
273
- state.hooks[webhookName] = [];
274
- }
275
- state.hooks[webhookName].push(handler);
276
+ if (!state.hooks[webhookName]) {
277
+ state.hooks[webhookName] = [];
278
+ }
279
+ state.hooks[webhookName].push(handler);
276
280
  }
277
281
  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);
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);
291
299
  }
292
300
  function receiverOnAny(state, handler) {
293
- handleEventHandlers(state, "*", handler);
301
+ handleEventHandlers(state, "*", handler);
294
302
  }
295
303
  function receiverOnError(state, handler) {
296
- handleEventHandlers(state, "error", handler);
304
+ handleEventHandlers(state, "error", handler);
297
305
  }
298
306
 
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
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
302
311
  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
- }
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
+ }
317
325
  }
318
326
 
319
- // @ts-ignore to address #245
327
+ // pkg/dist-src/event-handler/receive.js
320
328
  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));
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));
326
334
  }
327
- // main handler function
328
335
  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"]);
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);
343
364
  }
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();
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;
348
372
  }
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;
373
+ const error = new AggregateError(errors);
374
+ Object.assign(error, {
375
+ event,
376
+ errors
372
377
  });
378
+ errorHandlers.forEach((handler) => wrapErrorHandler(handler, error));
379
+ throw error;
380
+ });
373
381
  }
374
382
 
383
+ // pkg/dist-src/event-handler/remove-listener.js
375
384
  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
- }
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;
390
398
  }
399
+ }
391
400
  }
392
401
 
402
+ // pkg/dist-src/event-handler/index.js
393
403
  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
- };
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
+ };
408
418
  }
409
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";
410
425
  async function verifyAndReceive(state, event) {
411
- // verify will validate that the secret is not undefined
412
- const matchesSignature = await verify(state.secret, event.payload, event.signature);
413
- if (!matchesSignature) {
414
- const error = new Error("[@octokit/webhooks] signature does not match event payload and secret");
415
- return state.eventHandler.receive(Object.assign(error, { event, status: 400 }));
416
- }
417
- return state.eventHandler.receive({
418
- id: event.id,
419
- name: event.name,
420
- payload: JSON.parse(event.payload),
421
- });
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
+ });
422
444
  }
423
445
 
424
- const WEBHOOK_HEADERS = [
425
- "x-github-event",
426
- "x-hub-signature-256",
427
- "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"
428
451
  ];
429
- // https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#delivery-headers
430
452
  function getMissingHeaders(request) {
431
- return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
453
+ return WEBHOOK_HEADERS.filter((header) => !(header in request.headers));
432
454
  }
433
455
 
434
- // @ts-ignore to address #245
456
+ // pkg/dist-src/middleware/node/get-payload.js
457
+ import AggregateError2 from "aggregate-error";
435
458
  function getPayload(request) {
436
- // If request.body already exists we can stop here
437
- // See https://github.com/octokit/webhooks.js/pull/23
438
- if (request.body)
439
- return Promise.resolve(request.body);
440
- return new Promise((resolve, reject) => {
441
- let data = "";
442
- request.setEncoding("utf8");
443
- // istanbul ignore next
444
- request.on("error", (error) => reject(new AggregateError([error])));
445
- request.on("data", (chunk) => (data += chunk));
446
- request.on("end", () => {
447
- try {
448
- // Call JSON.parse() only to check if the payload is valid JSON
449
- JSON.parse(data);
450
- resolve(data);
451
- }
452
- catch (error) {
453
- error.message = "Invalid JSON";
454
- error.status = 400;
455
- reject(new AggregateError([error]));
456
- }
457
- });
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
+ }
458
475
  });
476
+ });
459
477
  }
460
478
 
479
+ // pkg/dist-src/middleware/node/on-unhandled-request-default.js
461
480
  function onUnhandledRequestDefault(request, response) {
462
- response.writeHead(404, {
463
- "content-type": "application/json",
464
- });
465
- response.end(JSON.stringify({
466
- error: `Unknown route: ${request.method} ${request.url}`,
467
- }));
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
+ );
468
489
  }
469
490
 
491
+ // pkg/dist-src/middleware/node/middleware.js
470
492
  async function middleware(webhooks, options, request, response, next) {
471
- let pathname;
472
- try {
473
- pathname = new URL(request.url, "http://localhost").pathname;
474
- }
475
- catch (error) {
476
- response.writeHead(422, {
477
- "content-type": "application/json",
478
- });
479
- response.end(JSON.stringify({
480
- error: `Request URL could not be parsed: ${request.url}`,
481
- }));
482
- return true;
483
- }
484
- if (pathname !== options.path) {
485
- next?.();
486
- return false;
487
- }
488
- else if (request.method !== "POST") {
489
- onUnhandledRequestDefault(request, response);
490
- return true;
491
- }
492
- // Check if the Content-Type header is `application/json` and allow for charset to be specified in it
493
- // Otherwise, return a 415 Unsupported Media Type error
494
- // See https://github.com/octokit/webhooks.js/issues/158
495
- if (!request.headers["content-type"].startsWith("application/json")) {
496
- response.writeHead(415, {
497
- "content-type": "application/json",
498
- accept: "application/json",
499
- });
500
- response.end(JSON.stringify({
501
- error: `Unsupported "Content-Type" header value. Must be "application/json"`,
502
- }));
503
- return true;
504
- }
505
- const missingHeaders = getMissingHeaders(request).join(", ");
506
- if (missingHeaders) {
507
- response.writeHead(400, {
508
- "content-type": "application/json",
509
- });
510
- response.end(JSON.stringify({
511
- error: `Required headers missing: ${missingHeaders}`,
512
- }));
513
- return true;
514
- }
515
- const eventName = request.headers["x-github-event"];
516
- const signatureSHA256 = request.headers["x-hub-signature-256"];
517
- const id = request.headers["x-github-delivery"];
518
- options.log.debug(`${eventName} event received (id: ${id})`);
519
- // GitHub will abort the request if it does not receive a response within 10s
520
- // See https://github.com/octokit/webhooks.js/issues/185
521
- let didTimeout = false;
522
- const timeout = setTimeout(() => {
523
- didTimeout = true;
524
- response.statusCode = 202;
525
- response.end("still processing\n");
526
- }, 9000).unref();
527
- try {
528
- const payload = await getPayload(request);
529
- await webhooks.verifyAndReceive({
530
- id: id,
531
- name: eventName,
532
- payload,
533
- signature: signatureSHA256,
534
- });
535
- clearTimeout(timeout);
536
- if (didTimeout)
537
- return true;
538
- response.end("ok\n");
539
- return true;
540
- }
541
- catch (error) {
542
- clearTimeout(timeout);
543
- if (didTimeout)
544
- return true;
545
- const err = Array.from(error)[0];
546
- const errorMessage = err.message
547
- ? `${err.name}: ${err.message}`
548
- : "Error: An Unspecified error occurred";
549
- response.statusCode = typeof err.status !== "undefined" ? err.status : 500;
550
- options.log.error(error);
551
- response.end(JSON.stringify({
552
- error: errorMessage,
553
- }));
554
- return true;
555
- }
556
- }
557
-
558
- function createNodeMiddleware(webhooks, { path = "/api/github/webhooks", log = createLogger(), } = {}) {
559
- return middleware.bind(null, webhooks, {
560
- path,
561
- 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"
562
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
+ }
563
576
  }
564
577
 
565
- // U holds the return value of `transform` function in Options
566
- class Webhooks {
567
- constructor(options) {
568
- if (!options || !options.secret) {
569
- throw new Error("[@octokit/webhooks] options.secret required");
570
- }
571
- const state = {
572
- eventHandler: createEventHandler(options),
573
- secret: options.secret,
574
- hooks: {},
575
- log: createLogger(options.log),
576
- };
577
- this.sign = sign.bind(null, options.secret);
578
- this.verify = verify.bind(null, options.secret);
579
- this.on = state.eventHandler.on;
580
- this.onAny = state.eventHandler.onAny;
581
- this.onError = state.eventHandler.onError;
582
- this.removeListener = state.eventHandler.removeListener;
583
- this.receive = state.eventHandler.receive;
584
- this.verifyAndReceive = verifyAndReceive.bind(null, state);
585
- }
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
+ });
586
587
  }
587
588
 
588
- export { Webhooks, createEventHandler, createNodeMiddleware, emitterEventNames };
589
- //# 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
+ };