boggin.
01/09/2024, 7:36 PM"CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder"
I found this "workaround" which does not seem to work:
https://github.com/cloudflare/workers-sdk/issues/1366
which defines a wasm Memory
to a 512 pagesize to be able to work on Cloudflare.
The wasm package I'm trying to use is Resvg. which I import via fetch using Vite's ..wasm?url
directive because Vite doesn't support ESM wasm modules especially in SSR.
I tried using vite plugins for directly importing wasm modules (like vite-plugin-wasm
and also mentioned here https://github.com/vitejs/vite/issues/8882) with no success because then it throws a cannot find module 'wbg'
error.
I understand there are tons of factors frameworks and tools associated with this but if anyone can help me at all tha'd be amazingJames
01/09/2024, 7:38 PMresvg-wasm
without issue in Workers when I import it directly via import wasm from '@resvg/resvg-wasm/index_bg.wasm'
and do so in a couple projects. I don't know how/if that'll work when it interacts with Vite though.boggin.
01/09/2024, 7:39 PMwbg
which i know nothing about and it seems specific to how resvg is packing their wasmboggin.
01/09/2024, 7:40 PMJames
01/09/2024, 7:40 PMwrangler dev
directly or via some build step with Vite? It works perfectly fine for me in a couple Workers projects, though I'm just doing them directly and using wrangler
or esbuild
instead of another build stepboggin.
01/09/2024, 7:43 PM'@resvg/resvg-wasm/index_bg.wasm'
Error: Cannot find module 'wbg' imported from 'C:/Users/.../node_modules/@resvg/resvg-wasm/index_bg.wasm'
I'm not using wrangler dev. just normal build with Vite, using the sveltekit adapter for cloudflareboggin.
01/09/2024, 7:44 PMJames
01/09/2024, 7:45 PMJames
01/09/2024, 7:45 PMboggin.
01/09/2024, 7:46 PMboggin.
01/09/2024, 7:48 PMimport { Resvg, initWasm } from '@resvg/resvg-wasm';
import resvgwasm from '@resvg/resvg-wasm/index_bg.wasm'
James
01/09/2024, 7:48 PMboggin.
01/09/2024, 11:14 PMboggin.
01/09/2024, 11:16 PMimport { Resvg, initWasm } from '@resvg/resvg-wasm';
import resvgwasm from '@resvg/resvg-wasm/index_bg.wasm'
export default {
async fetch(request: Request): Promise<Response> {
const reqBody = await request.text();
await initWasm(resvgwasm)
const resvg = new Resvg(reqBody, {
fitTo: {
mode: 'width',
value: 1200
}
});
const image = resvg.render();
return new Response(image.asPng(), {
headers: {
'content-type': 'image/png'
}
});
},
};
I get an error :
[wrangler:err] AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
(0, import_assert5.default)(prepareStackTrace !== void 0)
at getSourceMapper (C:\Users\...\node_modules\miniflare\dist\src\index.js:5749:30)
boggin.
01/09/2024, 11:16 PMboggin.
01/09/2024, 11:19 PM@resvg/resvg-wasm/index.js
file to use the Resvg constructor and to initiate the wasm.
If you have experience using this, what did you do?James
01/09/2024, 11:36 PMtypescript
import { Resvg, initWasm } from '@resvg/resvg-wasm';
function wasmInit(wasm: WebAssembly.Module) {
return initWasm(wasm).catch((err) => {
if ('message' in err && err.message?.startsWith?.('Already initialized')) {
return;
}
throw err;
});
}
// and then inside a request handler:
const { default: resvgwasm } = await import('@resvg/resvg-wasm/index_bg.wasm');
await wasmInit(resvgwasm);
const resvg = new Resvg(svgString, {
fitTo: {
mode: 'width',
value: 512,
},
});
const pngData = resvg.render();
const pngBuffer = pngData.asPng();
const response = new Response(pngBuffer, {
headers: {
'content-type': 'image/png',
},
});
James
01/09/2024, 11:36 PMJames
01/09/2024, 11:37 PMboggin.
01/10/2024, 7:09 PMboggin.
01/10/2024, 7:11 PMJames
01/10/2024, 7:12 PMJames
01/10/2024, 7:12 PMboggin.
01/10/2024, 7:13 PMJames
01/10/2024, 7:13 PMJames
01/10/2024, 7:14 PMJames
01/10/2024, 7:14 PMJames
01/10/2024, 7:15 PMboggin.
01/10/2024, 7:17 PMJames
01/10/2024, 7:18 PMJames
01/10/2024, 7:19 PMJames
01/10/2024, 7:19 PMboggin.
01/10/2024, 9:01 PMboggin.
01/10/2024, 9:01 PMJames
01/10/2024, 9:27 PM