From de8356ce3261e5ac8562afeed53d69ff3fa167b3 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Tue, 31 Dec 2024 20:57:28 -0600 Subject: [PATCH] fix: fix possible non-termination in `ingest_drop()`. --- src/sideload/ingest_drop.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/sideload/ingest_drop.ts b/src/sideload/ingest_drop.ts index 123b0f9..03e3767 100644 --- a/src/sideload/ingest_drop.ts +++ b/src/sideload/ingest_drop.ts @@ -45,6 +45,13 @@ export type IngestDropOpts< decodeStreamAuthorisationToken: StreamDecoder; }; +async function* terminatingFifoIterator(fifo: FIFO) { + for await (const chunk of fifo) { + if (!chunk) break; + yield chunk; + } +} + /** Decrypt an encrypted [drop](https://willowprotocol.org/specs/sideloading/index.html#drop) and ingest its contents into a {@linkcode Store} of the corresponding namespace. * * @returns The {@linkcode Store} which ingested the drop's contents. @@ -135,13 +142,16 @@ export async function ingestDrop< ); } - const fifo = new FIFO(); + const fifo = new FIFO(); - const payloadIngestPromise = store.ingestPayload({ - path: entry.path, - subspace: entry.subspaceId, - timestamp: entry.timestamp, - }, fifo); + const payloadIngestPromise = store.ingestPayload( + { + path: entry.path, + subspace: entry.subspaceId, + timestamp: entry.timestamp, + }, + terminatingFifoIterator(fifo) + ); let remainingBytes = entry.payloadLength; @@ -165,6 +175,8 @@ export async function ingestDrop< } } + fifo.push(undefined); + const payloadIngestResult = await payloadIngestPromise; if (payloadIngestResult.kind === "failure") {