Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/sideload/ingest_drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export type IngestDropOpts<
decodeStreamAuthorisationToken: StreamDecoder<AuthorisationToken>;
};

async function* terminatingFifoIterator<T>(fifo: FIFO<T | undefined>) {
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.
Expand Down Expand Up @@ -135,13 +142,16 @@ export async function ingestDrop<
);
}

const fifo = new FIFO<Uint8Array>();
const fifo = new FIFO<Uint8Array | undefined>();

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;

Expand All @@ -165,6 +175,8 @@ export async function ingestDrop<
}
}

fifo.push(undefined);

const payloadIngestResult = await payloadIngestPromise;

if (payloadIngestResult.kind === "failure") {
Expand Down