GramJS: Fix HTTP fetch timeout (#3640)
This commit is contained in:
parent
1293f2308f
commit
732424a879
@ -1,4 +1,11 @@
|
|||||||
const closeError = new Error('HttpStream was closed');
|
const closeError = new Error('HttpStream was closed');
|
||||||
|
const REQUEST_TIMEOUT = 10000;
|
||||||
|
|
||||||
|
AbortSignal.timeout ??= function timeout(ms) {
|
||||||
|
const ctrl = new AbortController();
|
||||||
|
setTimeout(() => ctrl.abort(), ms);
|
||||||
|
return ctrl.signal;
|
||||||
|
};
|
||||||
|
|
||||||
class HttpStream {
|
class HttpStream {
|
||||||
private url: string | undefined;
|
private url: string | undefined;
|
||||||
@ -54,6 +61,7 @@ class HttpStream {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: Buffer.from([]),
|
body: Buffer.from([]),
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
|
||||||
});
|
});
|
||||||
|
|
||||||
this.isClosed = false;
|
this.isClosed = false;
|
||||||
@ -69,13 +77,13 @@ class HttpStream {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: data,
|
body: data,
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
|
||||||
}).then(async (response) => {
|
}).then(async (response) => {
|
||||||
if (this.isClosed) {
|
if (this.isClosed) {
|
||||||
this.handleDisconnect();
|
this.handleDisconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
this.handleDisconnect();
|
|
||||||
throw closeError;
|
throw closeError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,6 +91,9 @@ class HttpStream {
|
|||||||
|
|
||||||
this.stream = this.stream.concat(Buffer.from(arrayBuffer));
|
this.stream = this.stream.concat(Buffer.from(arrayBuffer));
|
||||||
if (this.resolveRead && !this.isClosed) this.resolveRead();
|
if (this.resolveRead && !this.isClosed) this.resolveRead();
|
||||||
|
}).catch((err) => {
|
||||||
|
this.handleDisconnect();
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user