Skip to content

Commit

Permalink
Create Chunk Promise when Chunk is created.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrevino-virtru committed Dec 8, 2023
1 parent b6caf22 commit 490b4f9
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type Chunk = {
encryptedOffset: number;
encryptedSegmentSize?: number;
decryptedChunk?: null | DecryptResult;
promise: Promise<unknown>;
_resolve?: (value: unknown) => void;
_reject?: (value: unknown) => void;
};
Expand Down Expand Up @@ -1052,11 +1053,21 @@ export async function readStream(cfg: DecryptConfiguration) {
let mapOfRequestsOffset = 0;
const chunkMap = new Map(
segments.map(({ hash, encryptedSegmentSize = encryptedSegmentSizeDefault }) => {
const result = {
hash,
encryptedOffset: mapOfRequestsOffset,
encryptedSegmentSize,
} as Chunk;
const result = (() => {
let _resolve, _reject;
const chunk: Chunk = {
hash,
encryptedOffset: mapOfRequestsOffset,
encryptedSegmentSize,
promise: new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
}),
};
chunk._resolve = _resolve;
chunk._reject = _reject;
return chunk;
})();
mapOfRequestsOffset += encryptedSegmentSize || encryptedSegmentSizeDefault;
return [hash, result];
})
Expand Down Expand Up @@ -1085,10 +1096,7 @@ export async function readStream(cfg: DecryptConfiguration) {

const [hash, chunk] = chunkMap.entries().next().value;
if (!chunk.decryptedChunk) {
await new Promise((resolve, reject) => {
chunk._resolve = resolve;
chunk._reject = reject;
});
await chunk.promise;
}
const decryptedSegment = chunk.decryptedChunk;

Expand Down

0 comments on commit 490b4f9

Please sign in to comment.