adding dirrectory helper

This commit is contained in:
MathMan05 2025-03-10 11:46:28 -05:00
parent 7c94c45bf5
commit 31fe95b7a4
4 changed files with 134 additions and 40 deletions

View file

@ -26,7 +26,6 @@ class I18n {
this.translations = translations;
res();
console.log(proxyClass.permissions.descriptions.CREATE_INSTANT_INVITE());
}
static getTranslation(msg: string, ...params: string[]): string {
let str: string | undefined;

View file

@ -0,0 +1,13 @@
//This is *only* for webkit, and it really sucks
//If webkit starts supporting the more sain way, let me know so I can remove this after a year or two of them supporting it
onmessage = async (e) => {
const [file, content, rand] = e.data as [FileSystemFileHandle, ArrayBuffer, number];
try {
const handle = await file.createSyncAccessHandle();
handle.write(content);
handle.close();
postMessage([rand, true]);
} catch {
postMessage([rand, false]);
}
};

View file

@ -155,6 +155,96 @@ export class Specialuser {
localStorage.setItem("userinfos", JSON.stringify(info));
}
}
class Directory {
static home = this.createHome();
handle: FileSystemDirectoryHandle;
writeWorker?: Worker;
private constructor(handle: FileSystemDirectoryHandle) {
this.handle = handle;
}
static async createHome(): Promise<Directory> {
navigator.storage.persist();
const home = new Directory(await navigator.storage.getDirectory());
return home;
}
async *getAllInDir() {
for await (const [name, handle] of this.handle.entries()) {
if (handle instanceof FileSystemDirectoryHandle) {
yield [name, new Directory(handle)] as [string, Directory];
} else if (handle instanceof FileSystemFileHandle) {
yield [name, await handle.getFile()] as [string, File];
} else {
console.log(handle, "oops :3");
}
}
console.log("done");
}
async getRawFileHandler(name: string) {
return await this.handle.getFileHandle(name);
}
async getRawFile(name: string) {
try {
return await (await this.handle.getFileHandle(name)).getFile();
} catch {
return undefined;
}
}
async getString(name: string): Promise<string | undefined> {
try {
return await (await this.getRawFile(name))!.text();
} catch {
return undefined;
}
}
initWorker() {
if (this.writeWorker) return this.writeWorker;
this.writeWorker = new Worker("/utils/dirrWorker.js");
this.writeWorker.onmessage = (event) => {
const res = this.wMap.get(event.data[0]);
this.wMap.delete(event.data[0]);
if (!res) throw new Error("Res is not defined here somehow");
res(event.data[1]);
};
return this.writeWorker;
}
wMap = new Map<number, (input: boolean) => void>();
async setStringWorker(name: FileSystemFileHandle, value: ArrayBuffer) {
const worker = this.initWorker();
const random = Math.random();
worker.postMessage([name, value, random]);
return new Promise<boolean>((res) => {
this.wMap.set(random, res);
});
}
async setString(name: string, value: string): Promise<boolean> {
const file = await this.handle.getFileHandle(name, {create: true});
const contents = new TextEncoder().encode(value);
if (file.createWritable as unknown) {
const stream = await file.createWritable({keepExistingData: false});
await stream.write(contents);
await stream.close();
return true;
} else {
//Curse you webkit!
return await this.setStringWorker(file, contents);
}
}
async getDir(name: string) {
return new Directory(await this.handle.getDirectoryHandle(name, {create: true}));
}
}
Directory.home.then(async (home) => {
const dir = await home.getDir("dir");
console.log(await dir.getString("test3"), dir);
await dir.setString("test3", "webkit sucks");
console.log(await dir.getString("test3"));
for await (const thing of home.getAllInDir()) {
console.log(thing);
}
});
export {Directory};
const mobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
const iOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
export {mobile, iOS};

View file

@ -1,41 +1,33 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"importHelpers": false,
"incremental": true,
"lib": [
"esnext",
"DOM",
"webworker"
],
"module": "ESNext",
"moduleResolution": "Bundler",
"newLine": "lf",
"noEmitHelpers": false,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"pretty": true,
"removeComments": false,
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "ESNext",
"useDefineForClassFields": true,
"resolvePackageJsonImports": true,
"skipLibCheck": true,
"outDir": "./dist"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"importHelpers": false,
"incremental": true,
"lib": ["esnext", "DOM", "webworker", "DOM.AsyncIterable"],
"module": "ESNext",
"moduleResolution": "Bundler",
"newLine": "lf",
"noEmitHelpers": false,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"pretty": true,
"removeComments": false,
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "es2022",
"useDefineForClassFields": true,
"resolvePackageJsonImports": true,
"skipLibCheck": true,
"outDir": "./dist"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}