Skip to content

Commit

Permalink
INT-790: Add rejection chunk handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrevino-virtru committed Dec 5, 2023
1 parent 39cafdc commit 9b22c67
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Chunk = {
encryptedSegmentSize?: number;
decryptedChunk?: null | DecryptResult;
_resolve?: (value: unknown) => void;
_reject?: (value: unknown) => void;
};

export type TDFConfiguration = {
Expand Down Expand Up @@ -946,7 +947,7 @@ async function updateChunkQueue(
bufferSize
);
if (buffer) {
sliceAndDecrypt({
await sliceAndDecrypt({
buffer,
cryptoService,
reconstructedKeyBinary,
Expand Down Expand Up @@ -982,25 +983,27 @@ export async function sliceAndDecrypt({
segmentIntegrityAlgorithm: IntegrityAlgorithm;
}) {
for (const index in slice) {
const { encryptedOffset, encryptedSegmentSize } = slice[index];
const { encryptedOffset, encryptedSegmentSize, _resolve, _reject } = slice[index];

const offset =
slice[0].encryptedOffset === 0 ? encryptedOffset : encryptedOffset % slice[0].encryptedOffset;
const encryptedChunk = new Uint8Array(
buffer.slice(offset, offset + (encryptedSegmentSize as number))
);

slice[index].decryptedChunk = await decryptChunk(
await decryptChunk(
encryptedChunk,
reconstructedKeyBinary,
slice[index]['hash'],
cipher,
segmentIntegrityAlgorithm,
cryptoService
);
if (slice[index]._resolve) {
(slice[index]._resolve as (value: unknown) => void)(null);
}
)
.then(result => {
slice[index].decryptedChunk = result;
return null;
})
.then(_resolve, _reject);
}
}

Expand Down Expand Up @@ -1082,8 +1085,9 @@ export async function readStream(cfg: DecryptConfiguration) {

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

0 comments on commit 9b22c67

Please sign in to comment.