Skip to content

Commit

Permalink
feat: add FileSystemStore
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Nov 8, 2021
1 parent 53725be commit edab6ef
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@danmarshall/deckgl-typings": "^4.3.10",
"@types/node": "^14.14.5",
"@types/react-dom": "^17.0.0",
"@types/wicg-file-system-access": "^2020.9.4",
"@vitejs/plugin-react": "^1.0.1",
"prettier": "^2.2.0",
"typescript": "^4.4.3",
Expand Down
45 changes: 45 additions & 0 deletions src/fsstore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { AsyncStore } from 'zarr/types/storage/types';
import { KeyError } from 'zarr';

async function getFileHandle(root: FileSystemDirectoryHandle, key: string) {
const dirs = key.split('/');
const fname = dirs.pop()!;
for (const dir of dirs) {
root = await root.getDirectoryHandle(dir);
}
return root.getFileHandle(fname);
}

export class FileSystemStore implements AsyncStore<ArrayBuffer> {
constructor(public root: FileSystemDirectoryHandle) {}

static async fromDirectoryPicker() {
return new FileSystemStore(await window.showDirectoryPicker());
}

async getItem(key: string) {
const fileHandle = await getFileHandle(this.root, key).catch((_) => {
throw new KeyError(key);
});
const file = await fileHandle.getFile();
return file.arrayBuffer();
}

containsItem(key: string) {
return getFileHandle(this.root, key)
.then((_) => true)
.catch((_) => false);
}

keys() {
return Promise.resolve([]);
}

deleteItem(key: string): never {
throw new Error('deleteItem not implemented');
}

setItem(key: string, value: ArrayBuffer): never {
throw new Error('setItem not implemented');
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"zarr": ["node_modules/zarr"]
},
"types": [
"vite-plugin-pwa/client"
"vite-plugin-pwa/client",
"@types/wicg-file-system-access"
]
}
}

0 comments on commit edab6ef

Please sign in to comment.