Skip to content

Commit d6e2f3c

Browse files
committed
web/storage: more stringent opfs check
1 parent 1c30445 commit d6e2f3c

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

web/src/lib/storage/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import type { AbstractStorage } from "./storage";
22
import { MemoryStorage } from "./memory";
33
import { OPFSStorage } from "./opfs";
44

5-
export function init(expectedSize?: number): Promise<AbstractStorage> {
6-
if (OPFSStorage.isAvailable()) {
5+
export async function init(expectedSize?: number): Promise<AbstractStorage> {
6+
if (await OPFSStorage.isAvailable()) {
77
return OPFSStorage.init();
88
}
99

10-
if (MemoryStorage.isAvailable()) {
10+
if (await MemoryStorage.isAvailable()) {
1111
return MemoryStorage.init(expectedSize || 0);
1212
}
1313

web/src/lib/storage/memory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class MemoryStorage extends AbstractStorage {
8585
this.#chunks = [];
8686
}
8787

88-
static isAvailable() {
88+
static async isAvailable() {
8989
return true;
9090
}
9191
}

web/src/lib/storage/opfs.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export class OPFSStorage extends AbstractStorage {
77
#handle;
88
#io;
99

10+
static #isAvailable?: boolean;
11+
1012
constructor(root: FileSystemDirectoryHandle, handle: FileSystemFileHandle, reader: FileSystemSyncAccessHandle) {
1113
super();
1214
this.#root = root;
@@ -38,16 +40,33 @@ export class OPFSStorage extends AbstractStorage {
3840
await this.#root.removeEntry(this.#handle.name);
3941
}
4042

41-
static isAvailable() {
43+
static async #computeIsAvailable() {
4244
if (typeof navigator === 'undefined')
4345
return false;
4446

45-
return 'storage' in navigator && 'getDirectory' in navigator.storage;
47+
if ('storage' in navigator && 'getDirectory' in navigator.storage) {
48+
try {
49+
await navigator.storage.getDirectory();
50+
return true;
51+
} catch {
52+
return false;
53+
}
54+
}
55+
56+
return false;
57+
}
58+
59+
static async isAvailable() {
60+
if (this.#isAvailable === undefined) {
61+
this.#isAvailable = await this.#computeIsAvailable();
62+
}
63+
64+
return this.#isAvailable;
4665
}
4766
}
4867

4968
export const removeFromFileStorage = async (filename: string) => {
50-
if (OPFSStorage.isAvailable()) {
69+
if (await OPFSStorage.isAvailable()) {
5170
const root = await navigator.storage.getDirectory();
5271

5372
try {
@@ -60,7 +79,7 @@ export const removeFromFileStorage = async (filename: string) => {
6079
}
6180

6281
export const clearFileStorage = async () => {
63-
if (OPFSStorage.isAvailable()) {
82+
if (await OPFSStorage.isAvailable()) {
6483
const root = await navigator.storage.getDirectory();
6584
try {
6685
await root.removeEntry(COBALT_PROCESSING_DIR, { recursive: true });

web/src/lib/storage/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export abstract class AbstractStorage {
33
throw "init() call on abstract implementation";
44
}
55

6-
static isAvailable(): boolean {
6+
static async isAvailable(): Promise<boolean> {
77
return false;
88
}
99

0 commit comments

Comments
 (0)