Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Invalid URL string. Used initWasm(). #288

Open
tsuki-lab opened this issue Jan 11, 2024 · 6 comments
Open

TypeError: Invalid URL string. Used initWasm(). #288

tsuki-lab opened this issue Jan 11, 2024 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@tsuki-lab
Copy link

Hello, An error occurred while developing a dynamic image generation function using @resvg/wasm and satori on Remix.

[initResvg] TypeError: Invalid URL string.
    at checkURL2 (y6vx947fiz.js:51:50)
    at Object.apply (y6vx947fiz.js:68:5)
    at __wbg_init (y6vx947fiz.js:46269:154)
    at initWasm (y6vx947fiz.js:46278:9)
    at async initResvg (y6vx947fiz.js:46339:5)
    at async Promise.all (index 1)
    at async loader (y6vx947fiz.js:46345:3)
    at async callRouteLoaderRR (y6vx947fiz.js:3589:16)
    at async callLoaderOrAction (y6vx947fiz.js:2669:16)
    at async Promise.all (index 0) {
  stack: TypeError: Invalid URL string.
    at checkURL2 (y…iz.js:2669:16)
    at async Promise.all (index 0),

https://github.com/tsuki-lab/remix-satori-resvg-cloudflare/blob/main/app/routes/og.tsx#L24 .

Error reproduction environment

dependency version
Remix 2.4.1
satori 0.10.11
yoga-wasm-web 0.3.3
@resvg/resvg-wasm 2.6.0

Deploy to cloudflare pages. And reproduce it locally.

Repository

https://github.com/tsuki-lab/remix-satori-resvg-cloudflare

@tsuki-lab tsuki-lab changed the title TypeError: Invalid URL string. used initWasm(). TypeError: Invalid URL string. Used initWasm(). Jan 11, 2024
@tsuki-lab tsuki-lab changed the title TypeError: Invalid URL string. Used initWasm(). TypeError: Invalid URL string. Used initWasm(). Jan 11, 2024
@yisibl
Copy link
Member

yisibl commented Jan 11, 2024

Try: import wasm from '@resvg/resvg-wasm/index_bg.wasm'

See: https://jross.me/improving-wasm-imports-with-wrangler-and-cloudflare-workers/

@yisibl yisibl added the help wanted Extra attention is needed label Jan 11, 2024
@tsuki-lab
Copy link
Author

Try: import wasm from '@resvg/resvg-wasm/index_bg.wasm'

See: https://jross.me/improving-wasm-imports-with-wrangler-and-cloudflare-workers/

@yisibl
I tried it, but the result did not change.

@tsuki-lab
Copy link
Author

The version of wrangler I was using was an older version (3.8.0).
I updated the version to the latest version (3.22.4) and when I checked, the error content had changed.

[initResvg] TypeError: Fetch API cannot load: /build/_assets/index_bg-FX7W6XMT.wasm

      at __wbg_init (file:///UsersDir/repo/node_modules/.pnpm/@[email protected]/node_modules/@resvg/resvg-wasm/index.mjs:528:13)
      at initWasm (file:///UsersDir/repo/node_modules/.pnpm/@[email protected]/node_modules/@resvg/resvg-wasm/index.mjs:542:9)
      at async initResvg (file:///UsersDir/repo/app/routes/og.tsx:23:5)
      at [object Object]
      at async loader (file:///UsersDir/repoapp/routes/og.tsx:31:3)
      at async callRouteLoaderRR (file:///UsersDir/repo/node_modules/.pnpm/@[email protected][email protected]/node_modules/@remix-run/server-runtime/dist/esm/data.js:48:16)
      at async callLoaderOrAction (file:///UsersDir/repo/node_modules/.pnpm/@[email protected]/node_modules/@remix-run/router/router.ts:4011:16)
      at [object Object]
      at async loadRouteData (file:///UsersDir/repo/node_modules/.pnpm/@[email protected]/node_modules/@remix-run/router/router.ts:3298:19)
      at async queryImpl (file:///UsersDir/repo/node_modules/.pnpm/@[email protected]/node_modules/@remix-run/router/router.ts:3076:20)

@davidtranjs
Copy link

I'm using this:

const wasmResponse = await fetch("https://unpkg.com/@resvg/[email protected]/index_bg.wasm");
const wasmArrayBuffer = await wasmResponse.arrayBuffer();
await initWasm(wasmArrayBuffer);

@wouterds
Copy link

wouterds commented Mar 23, 2024

Try: import wasm from '@resvg/resvg-wasm/index_bg.wasm'

See: https://jross.me/improving-wasm-imports-with-wrangler-and-cloudflare-workers/

Seems like the worker size gets too big and is unable to deploy then, I tried loading it from a remote URL but then I get stuck with [wrangler:err] CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder.

Code looks something like this

const initResvgWasm = async () => {
  try {
    await initResvg(
      // (await import('@resvg/resvg-wasm/index_bg.wasm')).default
      await fetch('https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm').then((response) =>
        response.arrayBuffer(),
      ),
    );
  } catch (e) {
    if (e instanceof Error && !e.message.includes('Already initialized')) {
      throw e;
    }
  }
};

const initYogaWasm = async () => {
  const yoga = await initYoga(
    // (await import('yoga-wasm-web/dist/yoga.wasm')).default
    await fetch('https://unpkg.com/yoga-wasm-web/dist/yoga.wasm').then((response) =>
      response.arrayBuffer(),
    ),
  );
  initSatori(yoga);
};

And worker logs look something like this:

{
  "outcome": "exception",
  "scriptName": "pages-worker--2053183-preview",
  "diagnosticsChannelEvents": [],
  "exceptions": [
    {
      "name": "CompileError",
      "message": "WebAssembly.instantiate(): Wasm code generation disallowed by embedder",
      "timestamp": 1711227269214
    }
  ],
  "logs": [],
  "eventTimestamp": 1711227269168,
  "event": {
    "request": {
      [....]
    },
    "response": {
      "status": 500
    }
  },
  "id": 0
}

@wilsonowilson
Copy link

wilsonowilson commented Jun 1, 2024

Using version 2.6.2 worked for me

import { initWasm } from '@resvg/resvg-wasm';

let initialized = false;

export async function initResvgWasm() {
	try {
		if (initialized) return;
		const wasmResponse = await fetch('https://unpkg.com/@resvg/[email protected]/index_bg.wasm');
		const wasmArrayBuffer = await wasmResponse.arrayBuffer();
		await initWasm(wasmArrayBuffer);
		initialized = true;
	} catch (error) {
		console.error('Resvg wasm not initialized', error);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants