-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
const ogg: OggDec | ||
export default ogg | ||
|
||
interface OggDec { | ||
decodeOggData(data: ArrayBuffer): ArrayBuffer | ||
decodeOggData(data: ArrayBuffer): Promise<AudioBuffer> | ||
} | ||
|
||
export default oggdec = OggDec |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// hacky type stuff | ||
interface SecureFileSystemFileHandle extends FileSystemHandle { | ||
readonly kind: "file" | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle) */ | ||
createSyncAccessHandle(): Promise<FileSystemSyncAccessHandle> | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemFileHandle/createWritable) */ | ||
createWritable( | ||
options?: FileSystemCreateWritableOptions | ||
): Promise<FileSystemWritableFileStream> | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemFileHandle/getFile) */ | ||
getFile(): Promise<File> | ||
} | ||
|
||
interface FileSystemSyncAccessHandle { | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemSyncAccessHandle/close) */ | ||
close(): void | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemSyncAccessHandle/flush) */ | ||
flush(): void | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemSyncAccessHandle/getSize) */ | ||
getSize(): number | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemSyncAccessHandle/read) */ | ||
read( | ||
buffer: AllowSharedBufferSource, | ||
options?: FileSystemReadWriteOptions | ||
): number | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemSyncAccessHandle/truncate) */ | ||
truncate(newSize: number): void | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/FileSystemSyncAccessHandle/write) */ | ||
write( | ||
buffer: AllowSharedBufferSource, | ||
options?: FileSystemReadWriteOptions | ||
): number | ||
} | ||
|
||
interface FileSystemReadWriteOptions { | ||
at?: number | ||
} | ||
|
||
onmessage = async event => { | ||
const [id, path, buffer] = event.data | ||
const fileHandle = await getFileHandle(path) | ||
if (!fileHandle) { | ||
postMessage({ | ||
id, | ||
success: false, | ||
error: "Couldn't locate file", | ||
}) | ||
return | ||
} | ||
const accessHandle = await fileHandle.createSyncAccessHandle() | ||
accessHandle.write(buffer) | ||
accessHandle.flush() | ||
accessHandle.close() | ||
postMessage({ | ||
id, | ||
success: true, | ||
}) | ||
} | ||
|
||
async function getDirectoryHandle( | ||
path: string, | ||
options?: FileSystemGetFileOptions, | ||
dir?: FileSystemDirectoryHandle | ||
): Promise<FileSystemDirectoryHandle | undefined> { | ||
dir ||= await navigator.storage.getDirectory() | ||
if (path == "" || path == ".") return dir | ||
const pathParts = resolvePath(path).split("/") | ||
const dirname = pathParts.shift()! | ||
try { | ||
const dirHandle = await dir.getDirectoryHandle(dirname, options) | ||
if (!dirHandle) return undefined | ||
if (pathParts.length == 0) return dirHandle | ||
return getDirectoryHandle(pathParts.join("/"), options, dirHandle) | ||
} catch (err) { | ||
console.error(`Failed to get directory ${path} (${dirname}): ` + err) | ||
return undefined | ||
} | ||
} | ||
|
||
async function getFileHandle( | ||
path: string, | ||
options?: FileSystemGetFileOptions | ||
): Promise<SecureFileSystemFileHandle | undefined> { | ||
try { | ||
const pathParts = resolvePath(path).split("/") | ||
const filename = pathParts.pop()! | ||
const dirHandle = await getDirectoryHandle(pathParts.join("/"), options) | ||
if (!dirHandle) return undefined | ||
return (await dirHandle.getFileHandle( | ||
filename, | ||
options | ||
)) as SecureFileSystemFileHandle | ||
} catch (err) { | ||
console.error("Failed to get file " + path + ": " + err) | ||
return undefined | ||
} | ||
} | ||
|
||
function resolvePath(path: string): string { | ||
let pathParts = path.split("/") | ||
pathParts = pathParts.filter(item => item != "." && item != "") | ||
while (pathParts.indexOf("..") > -1) { | ||
const ind = pathParts.indexOf("..") | ||
if (ind == 0) { | ||
throw Error("Path" + pathParts.join("/") + "is invalid!") | ||
} | ||
pathParts.splice(ind - 1, 2) | ||
} | ||
return pathParts.join("/") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export class SafariFileWriter { | ||
private static worker = new Worker( | ||
new URL("./SafariFileWorker.ts", import.meta.url), | ||
{ | ||
type: "module", | ||
} | ||
) | ||
private static workID = 0 | ||
private static map: Map< | ||
number, | ||
[(value: void | PromiseLike<void>) => void, (reason?: any) => void] | ||
> = new Map() | ||
static { | ||
this.worker.onmessage = event => { | ||
const data = event.data | ||
console.log("finished job " + data.id) | ||
console.log(data) | ||
if (data.success) { | ||
this.map.get(data.id)![0]() | ||
} else { | ||
this.map.get(data.id)![1](data.reason) | ||
} | ||
this.map.delete(data.id) | ||
} | ||
} | ||
|
||
static async writeHandle(path: string, data: Blob | string) { | ||
const id = this.workID++ | ||
console.log("Starting work write " + path + ", id: " + id) | ||
const promise = new Promise<void>((resolve, reject) => | ||
this.map.set(id, [resolve, reject]) | ||
) | ||
const encode = new TextEncoder() | ||
const buffer = | ||
typeof data == "string" ? encode.encode(data) : await data.arrayBuffer() | ||
this.worker.postMessage([id, path, buffer], [buffer]) | ||
return promise | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters