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:
Scott Nonnenberg 2023-10-30 09:24:28 -07:00 committed by GitHub
parent 2da49456c6
commit 99b2bc304e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 2297 additions and 356 deletions

View file

@ -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;
}