Skip to content

Commit

Permalink
fix: accessHandle method return Promise on chrome 106
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfenghen committed Jun 15, 2024
1 parent 68647e6 commit 6935956
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export interface FileSystemSyncAccessHandle {
read: (container: ArrayBuffer, opts: { at: number }) => number;
write: (data: ArrayBuffer | ArrayBufferView, opts?: { at: number }) => number;
flush: () => void;
close: () => void;
truncate: (newSize: number) => void;
getSize: () => number;
read: (container: ArrayBuffer, opts: { at: number }) => Promise<number>;
write: (
data: ArrayBuffer | ArrayBufferView,
opts?: { at: number }
) => Promise<number>;
flush: () => Promise<void>;
close: () => Promise<void>;
truncate: (newSize: number) => Promise<void>;
getSize: () => Promise<number>;
}

export function parsePath(path: string) {
Expand Down
15 changes: 8 additions & 7 deletions src/opfs-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@ self.onmessage = async (e) => {
accessHandle = await fh.createSyncAccessHandle();
fileAccesserMap[args.filePath] = accessHandle;
} else if (evtType === 'close') {
accessHandle.close();
await accessHandle.close();
delete fileAccesserMap[args.filePath];
} else if (evtType === 'truncate') {
accessHandle.truncate(args.newSize);
await accessHandle.truncate(args.newSize);
} else if (evtType === 'write') {
const { data, opts } = e.data.args;
returnVal = accessHandle.write(data, opts);
returnVal = await accessHandle.write(data, opts);
} else if (evtType === 'read') {
const { offset, size } = e.data.args;
const buf = new ArrayBuffer(size);
const readLen = accessHandle.read(buf, { at: offset });
const uint8Buf = new Uint8Array(size);
const readLen = await accessHandle.read(uint8Buf, { at: offset });
const buf = uint8Buf.buffer;
returnVal =
readLen === size
? buf
: // @ts-expect-error transfer support by chrome 114
buf.transfer?.(readLen) ?? buf.slice(0, readLen);
trans.push(returnVal);
} else if (evtType === 'getSize') {
returnVal = accessHandle.getSize();
returnVal = await accessHandle.getSize();
} else if (evtType === 'flush') {
accessHandle.flush();
await accessHandle.flush();
}

self.postMessage(
Expand Down

0 comments on commit 6935956

Please sign in to comment.