New attachment storage system
This commit is contained in:
parent
273e1ccb15
commit
28664a606f
161 changed files with 2418 additions and 1562 deletions
26
ts/util/trimPadding.ts
Normal file
26
ts/util/trimPadding.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { Transform } from 'node:stream';
|
||||
|
||||
/**
|
||||
* Truncates the stream to the target size.
|
||||
*/
|
||||
export function trimPadding(size: number): Transform {
|
||||
let total = 0;
|
||||
return new Transform({
|
||||
transform(chunk, _encoding, callback) {
|
||||
const chunkSize = chunk.byteLength;
|
||||
const sizeLeft = size - total;
|
||||
if (sizeLeft >= chunkSize) {
|
||||
total += chunkSize;
|
||||
callback(null, chunk);
|
||||
} else if (sizeLeft > 0) {
|
||||
total += sizeLeft;
|
||||
callback(null, chunk.subarray(0, sizeLeft));
|
||||
} else {
|
||||
callback(null, null);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue