Use streams to download attachments directly to disk
Co-authored-by: trevor-signal <131492920+trevor-signal@users.noreply.github.com>
This commit is contained in:
parent
2da49456c6
commit
99b2bc304e
48 changed files with 2297 additions and 356 deletions
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { Transform } from 'stream';
|
||||
import type { Readable } from 'stream';
|
||||
|
||||
import * as Bytes from '../Bytes';
|
||||
|
@ -59,3 +60,46 @@ export function getStreamWithTimeout(
|
|||
|
||||
return promise;
|
||||
}
|
||||
|
||||
export function getTimeoutStream({
|
||||
name,
|
||||
timeout,
|
||||
abortController,
|
||||
}: OptionsType): Transform {
|
||||
const timeoutStream = new Transform();
|
||||
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
const clearTimer = () => {
|
||||
clearTimeoutIfNecessary(timer);
|
||||
timer = undefined;
|
||||
};
|
||||
|
||||
const reset = () => {
|
||||
clearTimer();
|
||||
|
||||
timer = setTimeout(() => {
|
||||
abortController.abort();
|
||||
timeoutStream.emit(
|
||||
'error',
|
||||
new StreamTimeoutError(`getStreamWithTimeout(${name}) timed out`)
|
||||
);
|
||||
clearTimer();
|
||||
}, timeout);
|
||||
};
|
||||
|
||||
timeoutStream._transform = function transform(chunk, _encoding, done) {
|
||||
try {
|
||||
reset();
|
||||
} catch (error) {
|
||||
return done(error);
|
||||
}
|
||||
|
||||
this.push(chunk);
|
||||
|
||||
done();
|
||||
};
|
||||
|
||||
reset();
|
||||
|
||||
return timeoutStream;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue