c0c99a9672
The server still never sees a photo, so the model has to run in the page. Real-ESRGAN x4v3 ships as a 4.9MB ONNX in public/models and is loaded lazily on the first export that actually needs it; the wasm runtime is copied next to CanvasKit at build time and stays lazily fetched, cached for 30 days. Vite is told onnxruntime-web is external-wasm so no 28MB asset lands in the bundle. UNCHANGED keeps the old path and the tier cap; 2K/4K/custom upscale only when the request is larger than the photo being edited, otherwise they resize down. Guests keep UNCHANGED and 2K. Tiling is 256px with an 8px overlap, so memory follows the target size rather than four times it.
32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
// canvaskit and onnxruntime resolve their wasm at runtime, not through the
|
|
// bundler, so the binaries have to be served assets. 20MB of wasm in the repo is
|
|
// worse than 20MB copied out of node_modules at build time (the Docker build
|
|
// stage does the same).
|
|
import { copyFileSync, mkdirSync, existsSync } from 'node:fs';
|
|
import { dirname } from 'node:path';
|
|
|
|
const copies = [
|
|
['node_modules/canvaskit-wasm/bin/full/canvaskit.wasm', 'public/wasm/canvaskit.wasm'],
|
|
// onnxruntime-web's default entry (its own bundled glue) looks for the jsep
|
|
// build of the runtime in `env.wasm.wasmPaths`; that build carries both the
|
|
// WebGPU and the plain WebAssembly execution provider, so one file is enough.
|
|
[
|
|
'node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm',
|
|
'public/wasm/ort/ort-wasm-simd-threaded.jsep.wasm',
|
|
],
|
|
[
|
|
'node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.mjs',
|
|
'public/wasm/ort/ort-wasm-simd-threaded.jsep.mjs',
|
|
],
|
|
];
|
|
|
|
for (const [from, to] of copies) {
|
|
if (!existsSync(from)) {
|
|
console.error(`missing ${from} — run npm install first`);
|
|
process.exit(1);
|
|
}
|
|
mkdirSync(dirname(to), { recursive: true });
|
|
copyFileSync(from, to);
|
|
console.log(`copied ${from} -> ${to}`);
|
|
}
|