@@ -26,8 +26,8 @@ const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token" as const;
26
26
// const adapters = { ...nativeAdapters, callback: callbackAdapter };
27
27
28
28
export interface WebhookOptions {
29
- /** An optional strategy to handle timeouts (default: ' throw') */
30
- onTimeout ?: "throw" | "ignore" | ( ( ...args : any [ ] ) => unknown ) ;
29
+ /** An optional callback to handle timeouts. If undefined, it will throw an error. */
30
+ onTimeout ?: ( ...args : any [ ] ) => unknown | Promise < unknown > ;
31
31
/** An optional number of timeout milliseconds (default: 10_000) */
32
32
timeoutMilliseconds ?: number ;
33
33
/** An optional string to compare to X-Telegram-Bot-Api-Secret-Token */
@@ -170,7 +170,7 @@ function webhookCallback<
170
170
options ?: WebhookOptions ,
171
171
) : ( ...args : Parameters < T > ) => Promise < ReturnType < T > > {
172
172
const {
173
- onTimeout = "throw" ,
173
+ onTimeout,
174
174
timeoutMilliseconds = 10_000 ,
175
175
secretToken,
176
176
} = options ?? { } ;
@@ -237,10 +237,8 @@ function webhookCallback<
237
237
} ;
238
238
await timeoutIfNecessary (
239
239
bot . handleUpdate ( updateData , webhookReplyEnvelope ) ,
240
- typeof onTimeout === "function"
241
- ? ( ) => onTimeout ( ...args )
242
- : onTimeout ,
243
240
timeoutMilliseconds ,
241
+ onTimeout !== undefined ? ( ) => onTimeout ( ...args ) : undefined ,
244
242
) ;
245
243
246
244
if ( ! usedWebhookReply ) return ok ( ...args ) ;
@@ -252,27 +250,30 @@ function webhookCallback<
252
250
253
251
function timeoutIfNecessary (
254
252
task : Promise < void > ,
255
- onTimeout : "throw" | "ignore" | ( ( ) => unknown ) ,
256
253
timeout : number ,
254
+ onTimeout ?: ( ) => unknown | Promise < unknown > ,
257
255
) : Promise < void > {
258
256
if ( timeout === Infinity ) return task ;
259
- return new Promise ( ( resolve , reject ) => {
260
- const handle = setTimeout ( ( ) => {
261
- debugErr ( `Request timed out after ${ timeout } ms` ) ;
262
- if ( onTimeout === "throw" ) {
263
- reject ( new Error ( `Request timed out after ${ timeout } ms` ) ) ;
264
- } else {
265
- if ( typeof onTimeout === "function" ) onTimeout ( ) ;
266
- resolve ( ) ;
267
- }
268
- const now = Date . now ( ) ;
269
- task . finally ( ( ) => {
270
- const diff = Date . now ( ) - now ;
271
- debugErr ( `Request completed ${ diff } ms after timeout!` ) ;
272
- } ) ;
273
- } , timeout ) ;
274
- task . then ( resolve )
275
- . catch ( reject )
276
- . finally ( ( ) => clearTimeout ( handle ) ) ;
277
- } ) ;
257
+ const { promise, resolve, reject } = Promise . withResolvers < void > ( ) ;
258
+
259
+ const handle = setTimeout ( async ( ) => {
260
+ debugErr ( `Request timed out after ${ timeout } ms` ) ;
261
+ if ( onTimeout !== undefined ) {
262
+ await onTimeout ( ) ;
263
+ resolve ( ) ;
264
+ } else {
265
+ reject ( new Error ( `Request timed out after ${ timeout } ms` ) ) ;
266
+ }
267
+ const now = Date . now ( ) ;
268
+ task . finally ( ( ) => {
269
+ const diff = Date . now ( ) - now ;
270
+ debugErr ( `Request completed ${ diff } ms after timeout!` ) ;
271
+ } ) ;
272
+ } , timeout ) ;
273
+
274
+ task . then ( resolve )
275
+ . catch ( reject )
276
+ . finally ( ( ) => clearTimeout ( handle ) ) ;
277
+
278
+ return promise ;
278
279
}
0 commit comments