File tree Expand file tree Collapse file tree 4 files changed +37
-15
lines changed Expand file tree Collapse file tree 4 files changed +37
-15
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import { env } from './utils';
12
12
env ( ) ;
13
13
14
14
const PORT = 3000 ;
15
- const WAIT_FOR_TASK_FINISH_TIMEOUT = 60_000 ;
15
+ const WAIT_FOR_TASK_FINISH_TIMEOUT = 3 * 60_000 ;
16
16
17
17
// Environment ---------------------------------------------------------------
18
18
@@ -57,7 +57,7 @@ async function main() {
57
57
58
58
const event = await verifyWebhookEventSignature (
59
59
{
60
- evt : body ,
60
+ body,
61
61
signature,
62
62
timestamp,
63
63
} ,
Original file line number Diff line number Diff line change @@ -115,11 +115,20 @@ export const listen = new Command('listen')
115
115
}
116
116
}
117
117
118
- if ( queue . current . length === 0 ) {
119
- return ;
120
- }
121
-
122
- const promises = queue . current . map ( async ( update ) => {
118
+ // Send Events
119
+
120
+ const events : ( Webhook & { internal ?: true } ) [ ] = [
121
+ // NOTE: We push the ping request on every tick to ensure the webhook is alive.
122
+ {
123
+ type : 'test' ,
124
+ timestamp : new Date ( ) . toISOString ( ) ,
125
+ payload : { test : 'ok' } ,
126
+ internal : true ,
127
+ } ,
128
+ ...queue . current ,
129
+ ] ;
130
+
131
+ const promises = events . map ( async ( update ) => {
123
132
const body = JSON . stringify ( update ) ;
124
133
125
134
const signature = createWebhookSignature ( {
@@ -155,7 +164,9 @@ export const listen = new Command('listen')
155
164
const delivery = await Promise . all ( promises ) ;
156
165
157
166
// NOTE: We preserve the rejected updates so we can retry them.
158
- queue . current = delivery . filter ( ( d ) => d . delivery === 'rejected' ) . map ( ( d ) => d . update ) ;
167
+ queue . current = delivery
168
+ . filter ( ( d ) => d . delivery === 'rejected' && d . update . internal !== true )
169
+ . map ( ( d ) => d . update ) ;
159
170
} , 1_000 ) ;
160
171
161
172
console . log ( `Forwarding updates to: ${ localTargetEndpoint } !` ) ;
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ export type WebhookTestPayload = z.infer<typeof zWebhookTestPayload>;
18
18
19
19
export const zWebhookTest = z . object ( {
20
20
type : z . literal ( 'test' ) ,
21
- timestamp : z . iso . datetime ( { offset : true } ) ,
21
+ timestamp : zWebhookTimestamp ,
22
22
payload : zWebhookTestPayload ,
23
23
} ) ;
24
24
@@ -66,14 +66,15 @@ export type Webhook = z.infer<typeof zWebhookSchema>;
66
66
*/
67
67
export async function verifyWebhookEventSignature (
68
68
evt : {
69
- evt : string ;
69
+ body : string | object ;
70
70
signature : string ;
71
71
timestamp : string ;
72
72
} ,
73
73
cfg : { secret : string } ,
74
74
) : Promise < { ok : true ; event : Webhook } | { ok : false } > {
75
75
try {
76
- const event = await zWebhookSchema . safeParseAsync ( JSON . parse ( evt . evt ) ) ;
76
+ const json = typeof evt . body === 'string' ? JSON . parse ( evt . body ) : evt . body ;
77
+ const event = await zWebhookSchema . safeParseAsync ( json ) ;
77
78
78
79
if ( event . success === false ) {
79
80
return { ok : false } ;
Original file line number Diff line number Diff line change @@ -79,9 +79,18 @@ describe('webhooks', () => {
79
79
timestamp,
80
80
} ) ;
81
81
82
- const valid = await verifyWebhookEventSignature (
82
+ const validJSON = await verifyWebhookEventSignature (
83
83
{
84
- evt : JSON . stringify ( MOCK ) ,
84
+ body : MOCK ,
85
+ signature : signature ,
86
+ timestamp,
87
+ } ,
88
+ { secret : 'secret' } ,
89
+ ) ;
90
+
91
+ const validString = await verifyWebhookEventSignature (
92
+ {
93
+ body : JSON . stringify ( MOCK ) ,
85
94
signature : signature ,
86
95
timestamp,
87
96
} ,
@@ -90,14 +99,15 @@ describe('webhooks', () => {
90
99
91
100
const invalid = await verifyWebhookEventSignature (
92
101
{
93
- evt : JSON . stringify ( MOCK ) ,
102
+ body : JSON . stringify ( MOCK ) ,
94
103
signature : 'invalid' ,
95
104
timestamp,
96
105
} ,
97
106
{ secret : 'secret' } ,
98
107
) ;
99
108
100
- expect ( valid . ok ) . toBe ( true ) ;
109
+ expect ( validJSON . ok ) . toBe ( true ) ;
110
+ expect ( validString . ok ) . toBe ( true ) ;
101
111
expect ( invalid . ok ) . toBe ( false ) ;
102
112
} ) ;
103
113
} ) ;
You can’t perform that action at this time.
0 commit comments