Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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
Expand Up @@ -11,29 +11,44 @@ onmessage = async function (event: MessageEvent) {
const content: string = event.data.content;
const responsePort = event.ports[0];

const pathParts = filePath.split('/').filter((p) => p.length > 0);
try {
const pathParts = filePath.split('/').filter((p) => p.length > 0);

const fileName = pathParts.pop();
if (fileName === undefined) {
throw new Error(`Invalid path: '${filePath}'`);
}
const fileName = pathParts.pop();
if (fileName === undefined) {
throw new Error(`Invalid path: '${filePath}'`);
}

let parentDirHandle = await navigator.storage.getDirectory();
for (const part of pathParts) {
parentDirHandle = await parentDirHandle.getDirectoryHandle(part);
}
let parentDirHandle = await navigator.storage.getDirectory();
for (const part of pathParts) {
parentDirHandle = await parentDirHandle.getDirectoryHandle(part);
}

const fileHandle = await parentDirHandle.getFileHandle(fileName, {
create: true,
});
const fileHandle = await parentDirHandle.getFileHandle(fileName, {
create: true,
});

const syncAccessHandle = await fileHandle.createSyncAccessHandle();
try {
const encodedContent = new TextEncoder().encode(content);
syncAccessHandle.truncate(0);
syncAccessHandle.write(encodedContent);
responsePort.postMessage('done');
} finally {
syncAccessHandle.close();
const syncAccessHandle = await fileHandle.createSyncAccessHandle();
try {
const encodedContent = new TextEncoder().encode(content);
syncAccessHandle.truncate(0);
syncAccessHandle.write(encodedContent);
responsePort.postMessage('done');
} finally {
syncAccessHandle.close();
}
} catch (error) {
responsePort.postMessage({
type: 'error',
path: filePath,
error:
error instanceof Error
? {
name: error.name,
message: error.message,
stack: error.stack,
}
: error,
});
}
Comment thread
adamziel marked this conversation as resolved.
};
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ async function opfsWriteFile(path: string, content: string) {
channel.port1.onmessage = function (event: MessageEvent) {
if (event.data === 'done') {
resolve();
} else if (event.data?.type === 'error') {
logger.error('Error in OPFS write worker.', event.data);
reject(
new Error(
`The browser storage worker failed while writing ${path}. See the preceding OPFS worker log for details.`,
{ cause: event.data.error }
)
);
Comment thread
adamziel marked this conversation as resolved.
} else {
reject(
new Error(
Expand Down
Loading