forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.ts
More file actions
164 lines (140 loc) · 4.42 KB
/
functions.ts
File metadata and controls
164 lines (140 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { extname, join } from "path";
import type HTTPRequest from "browserfs/dist/node/backend/HTTPRequest";
import type IndexedDBFileSystem from "browserfs/dist/node/backend/IndexedDB";
import type OverlayFS from "browserfs/dist/node/backend/OverlayFS";
import type InMemoryFileSystem from "browserfs/dist/node/backend/InMemory";
import { type FileSystemObserver } from "contexts/fileSystem/useFileSystemContextState";
import { FS_HANDLES, MOUNTABLE_EXTENSIONS } from "utils/constants";
import {
type ExtendedEmscriptenFileSystem,
type Mount,
type RootFileSystem,
} from "contexts/fileSystem/useAsyncFs";
import {
KEYVAL_STORE_NAME,
getFileSystemHandles,
getKeyValStore,
supportsIndexedDB,
} from "contexts/fileSystem/core";
const KNOWN_IDB_DBS = [
"/classicube",
"/data/saves",
"ejs-bios",
"ejs-roms",
"ejs-romsdata",
"ejs-states",
"ejs-system",
"js-dos-cache (emulators-ui-saves)",
"keyval-store",
];
export const isMountedFolder = (mount?: Mount): boolean =>
typeof mount === "object" &&
(mount.getName() === "FileSystemAccess" ||
(mount as ExtendedEmscriptenFileSystem)._FS?.DB_STORE_NAME === "FILE_DATA");
const observers = new Map<string, FileSystemObserver>();
export const addFileSystemHandle = async (
directory: string,
handle: FileSystemDirectoryHandle,
mappedName: string,
observer?: FileSystemObserver
): Promise<void> => {
if (!(await supportsIndexedDB())) return;
const db = await getKeyValStore();
const dirPath = join(directory, mappedName);
try {
await db.put(
KEYVAL_STORE_NAME,
{
...(await getFileSystemHandles()),
[dirPath]: handle,
},
FS_HANDLES
);
if (observer) observers.set(dirPath, observer);
} catch {
// Ignore errors storing handle
}
};
export const removeFileSystemHandle = async (
directory: string
): Promise<void> => {
if (!(await supportsIndexedDB())) return;
const { [directory]: _removedHandle, ...handles } =
await getFileSystemHandles();
const db = await getKeyValStore();
try {
await db.put(KEYVAL_STORE_NAME, handles, FS_HANDLES);
observers.get(directory)?.disconnect();
observers.delete(directory);
} catch {
// Ignore errors storing handle
}
};
export const requestPermission = async (
url: string
): Promise<PermissionState | false> => {
const fsHandles = await getFileSystemHandles();
const handle = fsHandles[url];
if (handle) {
const currentPermissions = await handle.queryPermission();
if (currentPermissions === "prompt") {
await handle.requestPermission();
} else if (currentPermissions === "granted") {
throw new Error("Permission already granted");
}
return handle.queryPermission();
}
return false;
};
export const resetStorage = (rootFs?: RootFileSystem): Promise<void> =>
new Promise((resolve, reject) => {
setTimeout(reject, 750);
window.localStorage.clear();
window.sessionStorage.clear();
const clearFs = (): void => {
const overlayFs = rootFs?._getFs("/")?.fs as OverlayFS;
const overlayedFileSystems = overlayFs?.getOverlayedFileSystems();
const readable = overlayedFileSystems?.readable as HTTPRequest;
const writable = overlayedFileSystems?.writable as
| IndexedDBFileSystem
| InMemoryFileSystem;
readable?.empty();
if (writable?.getName() === "InMemory" || !writable?.empty) {
resolve();
} else {
writable.empty((apiError) => (apiError ? reject(apiError) : resolve()));
}
};
if (window.indexedDB) {
import("idb").then(({ deleteDB }) => {
if (window.indexedDB.databases) {
window.indexedDB
.databases()
.then((databases) =>
databases
.filter(({ name }) => name && name !== "browserfs")
.forEach(({ name }) => deleteDB(name as string))
)
.then(clearFs)
.catch(clearFs);
} else {
KNOWN_IDB_DBS.forEach((name) => deleteDB(name));
clearFs();
}
});
} else {
clearFs();
}
});
export const getMountUrl = (
url: string,
mntMap: Record<string, Mount>
): string | undefined => {
if (url === "/") return "";
if (mntMap[url] || MOUNTABLE_EXTENSIONS.has(extname(url))) return url;
return Object.keys(mntMap)
.filter((mountedUrl) => mountedUrl !== "/")
.find(
(mountedUrl) => url === mountedUrl || url.startsWith(`${mountedUrl}/`)
);
};