mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 17:27:08 +08:00
284 lines
10 KiB
JavaScript
284 lines
10 KiB
JavaScript
let engineModule = null;
|
|
|
|
export function createEngineWorker() {
|
|
if (nativeHost) return nativeHost.worker();
|
|
const worker = new Worker("./ofdgo_wasm.js");
|
|
worker.addEventListener("message", event => {
|
|
if (event.data.type !== "module") return;
|
|
event.stopImmediatePropagation();
|
|
engineModule ||= WebAssembly.compileStreaming(fetch("./ofdgo.wasm")).catch(error => { engineModule = null; throw error; });
|
|
engineModule.then(module => worker.postMessage({ type: "module", module }), error => worker.postMessage({ type: "module", error: error.message }));
|
|
});
|
|
return worker;
|
|
}
|
|
|
|
export async function documentBytes(file) {
|
|
return file?.nativeFile ? file : new Uint8Array(await file.arrayBuffer());
|
|
}
|
|
|
|
export function saveFilePicker(options) {
|
|
return nativeHost ? nativeHost.savePicker(options) : window.showSaveFilePicker ? window.showSaveFilePicker(options) : null;
|
|
}
|
|
|
|
export function directoryPicker() {
|
|
return nativeHost ? nativeHost.directoryPicker() : window.showDirectoryPicker({mode:"readwrite"});
|
|
}
|
|
|
|
class NativeFile {
|
|
constructor(value) {
|
|
Object.assign(this, value);
|
|
if (value.handle) this.handle = new NativeHandle(nativeHost, value.handle);
|
|
this.byteLength = this.size;
|
|
this.type ||= "application/octet-stream";
|
|
if (value.temporary) nativeHost?.resources?.register(this, value.nativeFile);
|
|
}
|
|
|
|
async arrayBuffer(signal) {
|
|
const response = await fetch(this.url, {signal});
|
|
if (!response.ok) throw new Error("读取文件失败");
|
|
return response.arrayBuffer();
|
|
}
|
|
|
|
toJSON() {
|
|
return {nativeFile:this.nativeFile, size:this.size, name:this.name};
|
|
}
|
|
|
|
async text() {
|
|
return new TextDecoder().decode(await this.arrayBuffer());
|
|
}
|
|
}
|
|
|
|
class NativeHandle {
|
|
constructor(host, value) {
|
|
this.host = host;
|
|
Object.assign(this, value);
|
|
}
|
|
|
|
toJSON() {
|
|
return {nativeHandle:this.nativeHandle, name:this.name, kind:this.kind};
|
|
}
|
|
|
|
async getFile() {
|
|
return new NativeFile(await this.host.call("file", {handle:this.nativeHandle}));
|
|
}
|
|
|
|
async getFileHandle(name, options = {}) {
|
|
return new NativeHandle(this.host, await this.host.call("child", {handle:this.nativeHandle, name, create:!!options.create, directory:false}));
|
|
}
|
|
|
|
async getDirectoryHandle(name, options = {}) {
|
|
return new NativeHandle(this.host, await this.host.call("child", {handle:this.nativeHandle, name, create:!!options.create, directory:true}));
|
|
}
|
|
|
|
removeEntry(name, options = {}) {
|
|
return this.host.call("remove", {handle:this.nativeHandle, name, recursive:!!options.recursive});
|
|
}
|
|
|
|
async createWritable() {
|
|
const {writer} = await this.host.call("write.begin", {handle:this.nativeHandle});
|
|
return {
|
|
write: data => this.host.call("write.data", {writer, data}),
|
|
close: () => this.host.call("write.commit", {writer}),
|
|
abort: () => this.host.call("write.abort", {writer}),
|
|
};
|
|
}
|
|
}
|
|
|
|
class NativeHost {
|
|
constructor(bridge) {
|
|
this.bridge = bridge;
|
|
this.parallelism = 1;
|
|
this.cacheBytes = 32 * 1024 * 1024;
|
|
this.reading = 0;
|
|
this.readQueue = [];
|
|
this.resources = typeof FinalizationRegistry === "function" ? new FinalizationRegistry(file => {
|
|
this.call("release", {file}).catch(() => {});
|
|
}) : null;
|
|
}
|
|
|
|
call(name, value, signal) {
|
|
return this.bridge.call(`ofdgo.host.${name}`, value, {signal});
|
|
}
|
|
|
|
async readResource(file, signal) {
|
|
if (this.reading >= this.parallelism) await new Promise(resolve => this.readQueue.push(resolve));
|
|
else this.reading++;
|
|
try {
|
|
signal?.throwIfAborted();
|
|
return await file.arrayBuffer(signal);
|
|
} finally {
|
|
try { await this.call("release", {file:file.nativeFile}); }
|
|
finally {
|
|
const next = this.readQueue.shift();
|
|
if (next) next();
|
|
else this.reading--;
|
|
}
|
|
}
|
|
}
|
|
|
|
async decode(value, name = "", key = "", signal) {
|
|
const reads = [];
|
|
const visit = (item, field, replace) => {
|
|
if (!item || typeof item !== "object" || item instanceof Blob || item instanceof ArrayBuffer || ArrayBuffer.isView(item)) return;
|
|
if (item.nativeFile) {
|
|
const file = new NativeFile(item);
|
|
if (field === "file" || name === "ofdgoConvertPDF" && field === "bytes") replace(file);
|
|
else reads.push(this.readResource(file, signal).then(bytes => replace(field === "blob" ? new Blob([bytes], {type:item.type}) : new Uint8Array(bytes))));
|
|
return;
|
|
}
|
|
for (const [child, content] of Object.entries(item)) {
|
|
if (content && typeof content === "object") visit(content, Array.isArray(item) ? "" : child, result => item[child] = result);
|
|
}
|
|
};
|
|
visit(value, key, result => value = result);
|
|
const results = await Promise.allSettled(reads);
|
|
const failed = results.find(result => result.status === "rejected");
|
|
if (failed) throw failed.reason;
|
|
return value;
|
|
}
|
|
|
|
async release(value) {
|
|
if (value instanceof Blob || value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return;
|
|
if (value?.nativeFile && value.temporary) {
|
|
await this.call("release", {file:value.nativeFile}).catch(() => {});
|
|
return;
|
|
}
|
|
if (value && typeof value === "object") {
|
|
for (const item of Object.values(value)) await this.release(item);
|
|
}
|
|
}
|
|
|
|
worker() {
|
|
const host = this;
|
|
let closed = false;
|
|
let unsubscribe;
|
|
let pending = Promise.resolve();
|
|
const readers = new Set();
|
|
const requests = new Map();
|
|
const completions = new Map();
|
|
const emit = data => {
|
|
if (data.type === "result") { completions.get(data.id)?.(data.result); return; }
|
|
if (!closed) worker.onmessage?.({data});
|
|
};
|
|
const ready = host.call("engine.open", {}).then(({engine, parallelism, cacheBytes}) => {
|
|
if (closed) return engine;
|
|
host.parallelism = Math.max(1, parallelism || 1);
|
|
host.cacheBytes = cacheBytes || host.cacheBytes;
|
|
unsubscribe = host.bridge.on(`ofdgo.engine.${engine}`, emit);
|
|
emit({type:"ready"});
|
|
return engine;
|
|
}).catch(error => { if (!closed) worker.onerror?.({message:error.message}); throw error; });
|
|
const worker = {
|
|
onmessage:null,
|
|
onerror:null,
|
|
postMessage(data) {
|
|
if (closed) return;
|
|
if (data.type === "cancel") {
|
|
requests.get(data.id)?.abort();
|
|
return;
|
|
}
|
|
if (data.type === "font") return;
|
|
const controller = new AbortController();
|
|
requests.set(data.id, controller);
|
|
const parallel = data.name === "ofdgoRenderPage";
|
|
const barrier = parallel ? pending : Promise.all([pending, ...readers]);
|
|
const operation = barrier.then(async () => {
|
|
const engine = await ready;
|
|
if (closed) return;
|
|
controller.signal.throwIfAborted();
|
|
const args = {engine, id:data.id, name:data.name, args:data.args};
|
|
let result;
|
|
if (data.name === "ofdgoConvertFile" || data.name === "ofdgoPackFiles") {
|
|
const completed = new Promise(resolve => completions.set(data.id, resolve));
|
|
const cancel = () => host.call("engine.cancel", {engine, id:data.id}).catch(() => {});
|
|
controller.signal.addEventListener("abort", cancel, {once:true});
|
|
try {
|
|
await host.call("engine.start", args);
|
|
if (controller.signal.aborted) cancel();
|
|
result = await completed;
|
|
await host.call("engine.finish", {engine, id:data.id});
|
|
} finally {
|
|
controller.signal.removeEventListener("abort", cancel);
|
|
completions.delete(data.id);
|
|
}
|
|
} else result = await host.call("engine.call", args, controller.signal);
|
|
if (closed) { await host.release(result); return; }
|
|
let decoded;
|
|
try {
|
|
controller.signal.throwIfAborted();
|
|
decoded = await host.decode(result, data.name, "", controller.signal);
|
|
controller.signal.throwIfAborted();
|
|
}
|
|
catch (error) { await host.release(result); throw error; }
|
|
if (closed) { await host.release(result); return; }
|
|
emit({id:data.id, ...decoded});
|
|
}).catch(error => emit({id:data.id, ok:false, error:error.message, code:error.code, canceled:error.name === "AbortError"})).finally(() => requests.delete(data.id));
|
|
if (parallel) {
|
|
readers.add(operation);
|
|
operation.finally(() => readers.delete(operation));
|
|
} else if (data.name !== "ofdgoSaveSnapshot") pending = operation;
|
|
},
|
|
terminate() {
|
|
if (closed) return;
|
|
closed = true;
|
|
for (const controller of requests.values()) controller.abort();
|
|
ready.then(engine => host.call("engine.close", {engine})).catch(() => {}).finally(() => {
|
|
unsubscribe?.();
|
|
for (const resolve of completions.values()) resolve({ok:false, canceled:true, error:"会话已关闭"});
|
|
completions.clear();
|
|
});
|
|
},
|
|
};
|
|
ready.catch(() => {});
|
|
return worker;
|
|
}
|
|
|
|
async savePicker(options) {
|
|
return new NativeHandle(this, await this.call("save", options));
|
|
}
|
|
|
|
async directoryPicker() {
|
|
return new NativeHandle(this, await this.call("directory", {}));
|
|
}
|
|
|
|
async fonts() {
|
|
return (await this.call("fonts", {})).map(({file, ...font}) => ({...font, nativeFile:new NativeFile(file)}));
|
|
}
|
|
|
|
async start(onFiles, onError) {
|
|
this.bridge.on("ofdgo.files", values => onFiles(values.map(value => new NativeFile(value))));
|
|
this.bridge.on("ofdgo.error", message => onError(new Error(message)));
|
|
const title = document.querySelector("title");
|
|
if (title) new MutationObserver(() => this.call("title", {title:document.title}).catch(onError)).observe(title, {childList:true});
|
|
await this.call("ready", {});
|
|
}
|
|
|
|
installFileInputs(onError) {
|
|
document.addEventListener("click", async event => {
|
|
const input = event.target;
|
|
if (!(input instanceof HTMLInputElement) || input.type !== "file" || input.disabled) return;
|
|
event.preventDefault();
|
|
try {
|
|
const files = (await this.call("open", {multiple:input.multiple, directory:input.webkitdirectory, accept:input.accept})).map(value => new NativeFile(value));
|
|
if (!files.length) return;
|
|
const transfer = new DataTransfer();
|
|
for (const file of files) transfer.items.add(new File([], file.name, {type:file.type}));
|
|
delete input.files;
|
|
input.files = transfer.files;
|
|
Object.defineProperty(input, "files", {configurable:true, get:() => input.value ? files : []});
|
|
input.dispatchEvent(new Event("input", {bubbles:true}));
|
|
input.dispatchEvent(new Event("change", {bubbles:true}));
|
|
} catch (error) { if (error.name !== "AbortError") onError(error); }
|
|
}, true);
|
|
}
|
|
|
|
async download(data, mime, name) {
|
|
const handle = await this.savePicker({suggestedName:name});
|
|
const writer = await handle.createWritable();
|
|
try { await writer.write(data); await writer.close(); }
|
|
catch (error) { await writer.abort().catch(() => {}); throw error; }
|
|
}
|
|
}
|
|
|
|
export const nativeHost = globalThis.guigo ? new NativeHost(globalThis.guigo) : null;
|