@@ -11,6 +11,7 @@ const REQUIRED_MATRIX_PACKAGES = [
|
11 | 11 | "@matrix-org/matrix-sdk-crypto-wasm", |
12 | 12 | ]; |
13 | 13 | const MIN_MATRIX_CRYPTO_NATIVE_BINDING_BYTES = 1_000_000; |
| 14 | +export const MATRIX_COMMAND_OUTPUT_TAIL_BYTES = 64 * 1024; |
14 | 15 | |
15 | 16 | type MatrixCryptoRuntimeDeps = { |
16 | 17 | requireFn?: (id: string) => unknown; |
@@ -56,7 +57,28 @@ type CommandResult = {
|
56 | 57 | |
57 | 58 | let defaultMatrixCryptoRuntimeEnsurePromise: Promise<void> | null = null; |
58 | 59 | |
59 | | -async function runFixedCommandWithTimeout(params: { |
| 60 | +function appendBoundedOutputTail(current: string, chunk: Buffer | string): string { |
| 61 | +const chunkBuffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); |
| 62 | +if (chunkBuffer.byteLength >= MATRIX_COMMAND_OUTPUT_TAIL_BYTES) { |
| 63 | +return chunkBuffer |
| 64 | +.subarray(chunkBuffer.byteLength - MATRIX_COMMAND_OUTPUT_TAIL_BYTES) |
| 65 | +.toString("utf8"); |
| 66 | +} |
| 67 | + |
| 68 | +const currentBuffer = Buffer.from(current); |
| 69 | +const nextBytes = currentBuffer.byteLength + chunkBuffer.byteLength; |
| 70 | +if (nextBytes <= MATRIX_COMMAND_OUTPUT_TAIL_BYTES) { |
| 71 | +return `${current}${chunkBuffer.toString("utf8")}`; |
| 72 | +} |
| 73 | + |
| 74 | +const currentTailBytes = MATRIX_COMMAND_OUTPUT_TAIL_BYTES - chunkBuffer.byteLength; |
| 75 | +const currentTail = currentBuffer.subarray(currentBuffer.byteLength - currentTailBytes); |
| 76 | +return Buffer.concat([currentTail, chunkBuffer], MATRIX_COMMAND_OUTPUT_TAIL_BYTES).toString( |
| 77 | +"utf8", |
| 78 | +); |
| 79 | +} |
| 80 | + |
| 81 | +export async function runFixedCommandWithTimeout(params: { |
60 | 82 | argv: string[]; |
61 | 83 | cwd: string; |
62 | 84 | timeoutMs: number; |
@@ -103,10 +125,10 @@ async function runFixedCommandWithTimeout(params: {
|
103 | 125 | process.once("exit", killChildOnExit); |
104 | 126 | |
105 | 127 | proc.stdout?.on("data", (chunk: Buffer | string) => { |
106 | | -stdout += chunk.toString(); |
| 128 | +stdout = appendBoundedOutputTail(stdout, chunk); |
107 | 129 | }); |
108 | 130 | proc.stderr?.on("data", (chunk: Buffer | string) => { |
109 | | -stderr += chunk.toString(); |
| 131 | +stderr = appendBoundedOutputTail(stderr, chunk); |
110 | 132 | }); |
111 | 133 | |
112 | 134 | timer = setTimeout(() => { |
|