Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const sessions = new Map();
const maxWriteBlockBytes = 1024 * 1024;

self.onmessage = async (event) => {
const { id, command, payload } = event.data ?? {};
Expand Down Expand Up @@ -59,12 +60,7 @@ async function appendChunksAsync(databaseName, blobKey, chunks) {

for (const chunk of Array.isArray(chunks) ? chunks : []) {
const data = chunk.data instanceof Uint8Array ? chunk.data : new Uint8Array(chunk.data ?? []);
const written = session.accessHandle.write(data, { at: session.position });
if (written !== data.byteLength) {
throw new Error(`Short OPFS write for ${blobKey}: wrote ${written} of ${data.byteLength} bytes.`);
}

session.position += written;
writeAllBytes(session, blobKey, data);
}
}

Expand Down Expand Up @@ -156,6 +152,37 @@ function getSessionKey(databaseName, blobKey) {
return `${databaseName}::${blobKey}`;
}

function writeAllBytes(session, blobKey, data) {
let offset = 0;

while (offset < data.byteLength) {
const bytesRemaining = data.byteLength - offset;
const bytesToWrite = Math.min(bytesRemaining, maxWriteBlockBytes);
const slice = data.subarray(offset, offset + bytesToWrite);
const written = normalizeBytesWritten(
session.accessHandle.write(slice, { at: session.position }),
bytesToWrite,
blobKey,
session.position);

offset += written;
session.position += written;
}
}

function normalizeBytesWritten(value, expectedBytes, blobKey, position) {
if (!Number.isFinite(value)) {
throw new Error(`Invalid OPFS write result for ${blobKey} at ${position}: ${String(value)}.`);
}

const written = Math.trunc(value);
if (written <= 0 || written > expectedBytes) {
throw new Error(`Invalid OPFS write result for ${blobKey} at ${position}: wrote ${written} of ${expectedBytes} bytes.`);
}

return written;
}

async function getDatabaseDirectoryAsync(databaseName, create) {
const root = await navigator.storage.getDirectory();
return await root.getDirectoryHandle(getDatabaseDirectoryName(databaseName), { create });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@
MimeType = "application/octet-stream"
});

if (result.IsFailed)
{
largeOutput = $"generated:{stream.Position}";
status = $"large-save-failed:{result.Problem?.Detail}";
return;
}

if (!stream.IsCompleted)
{
largeOutput = $"generated:{stream.Position}";
Expand All @@ -145,7 +152,7 @@
var crc = stream.CompletedCrc;

largeOutput = $"expected:{length}:{crc}";
status = result.IsSuccess ? $"large-saved:{length}:{crc}" : $"large-save-failed:{result.Problem?.Detail}";
status = $"large-saved:{length}:{crc}";
Logger.LogInformation("Completed browser storage save for {FileName} ({Bytes} bytes, crc {Crc}) in {ElapsedMilliseconds} ms.",
resolvedFileName,
length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@
MimeType = "application/octet-stream"
});

if (result.IsFailed)
{
largeOutput = $"generated:{stream.Position}";
status = $"large-save-failed:{result.Problem?.Detail}";
return;
}

if (!stream.IsCompleted)
{
largeOutput = $"generated:{stream.Position}";
Expand All @@ -144,7 +151,7 @@
var crc = stream.CompletedCrc;

largeOutput = $"expected:{length}:{crc}";
status = result.IsSuccess ? $"large-saved:{length}:{crc}" : $"large-save-failed:{result.Problem?.Detail}";
status = $"large-saved:{length}:{crc}";
Logger.LogInformation("Completed browser storage save for {FileName} ({Bytes} bytes, crc {Crc}) in {ElapsedMilliseconds} ms.",
resolvedFileName,
length,
Expand Down
Loading