Add stream timeout for attachment downloads
This commit is contained in:
parent
7d17158add
commit
c6ee6a038e
3 changed files with 235 additions and 5 deletions
62
ts/util/getStreamWithTimeout.ts
Normal file
62
ts/util/getStreamWithTimeout.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { Readable } from 'stream';
|
||||
|
||||
import * as Bytes from '../Bytes';
|
||||
import { explodePromise } from './explodePromise';
|
||||
|
||||
export type OptionsType = Readonly<{
|
||||
name: string;
|
||||
timeout: number;
|
||||
abortController: { abort(): void };
|
||||
}>;
|
||||
|
||||
export class StreamTimeoutError extends Error {}
|
||||
|
||||
export function getStreamWithTimeout(
|
||||
stream: Readable,
|
||||
{ name, timeout, abortController }: OptionsType
|
||||
): Promise<Uint8Array> {
|
||||
const { promise, resolve, reject } = explodePromise<Uint8Array>();
|
||||
|
||||
const chunks = new Array<Uint8Array>();
|
||||
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
|
||||
const clearTimer = () => {
|
||||
if (timer !== undefined) {
|
||||
clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const reset = () => {
|
||||
clearTimer();
|
||||
|
||||
timer = setTimeout(() => {
|
||||
abortController.abort();
|
||||
reject(new StreamTimeoutError(`getStreamWithTimeout(${name}) timed out`));
|
||||
}, timeout);
|
||||
};
|
||||
|
||||
stream.on('data', chunk => {
|
||||
reset();
|
||||
|
||||
chunks.push(chunk);
|
||||
});
|
||||
|
||||
stream.on('end', () => {
|
||||
clearTimer();
|
||||
resolve(Bytes.concatenate(chunks));
|
||||
});
|
||||
|
||||
stream.on('error', error => {
|
||||
clearTimer();
|
||||
reject(error);
|
||||
});
|
||||
|
||||
reset();
|
||||
|
||||
return promise;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue