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

example code fails...seems hopeless... is wasmer-js broken? #450

Open
onexdata opened this issue Jan 6, 2025 · 5 comments
Open

example code fails...seems hopeless... is wasmer-js broken? #450

onexdata opened this issue Jan 6, 2025 · 5 comments

Comments

@onexdata
Copy link

onexdata commented Jan 6, 2025

I tried the example code on the website. I tried Node 18.x, 20.x, 22.x, they all say "TypeError": fetch failed" and "Error: not implemented... yet..."

For reference, here is the code I'm talking about, it fails at very next line from importinging, await.init()...

import { init, Wasmer } from "@wasmer/sdk";

await init();

const pkg = await Wasmer.fromRegistry("python/python");
const instance = await pkg.entrypoint.run({
    args: ["-c", "print('Hello, World!')"],
});

const { code, stdout } = await instance.wait();
console.log(`Python exited with ${code}: ${stdout}`);

Here is the error...

node:internal/deps/undici/undici:13185
      Error.captureStackTrace(err);
            ^
                                                                                                      TypeError: fetch failed                                                                                   at node:internal/deps/undici/undici:13185:13                                                          at async v_ (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:47648)
    at async R_ (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:48139)
    at async file:///C:/code/onexdata/wasm/wasmer/node-server/example.js:3:1 {
  [cause]: Error: not implemented... yet...
      at makeNetworkError (node:internal/deps/undici/undici:8968:35)
      at schemeFetch (node:internal/deps/undici/undici:10363:34)
      at node:internal/deps/undici/undici:10212:26
      at mainFetch (node:internal/deps/undici/undici:10231:11)
      at fetching (node:internal/deps/undici/undici:10179:7)
      at fetch (node:internal/deps/undici/undici:10048:20)
      at fetch (node:internal/deps/undici/undici:13183:10)
      at fetch (node:internal/bootstrap/web/exposed-window-or-worker:72:12)
      at v_ (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:46947)
      at R_ (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:48145)
}

Node.js v20.18.0

When I rewrite everything and use a polyfill for fetch, I produce this...

import { init, Wasmer } from "@wasmer/sdk";
import fetch from 'node-fetch';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import fs from 'fs/promises';

const require = createRequire(import.meta.url);

// Custom fetch implementation that handles both HTTP and file protocols
global.fetch = async (urlOrRequest, options) => {
    const url = urlOrRequest instanceof URL ? urlOrRequest.href : urlOrRequest.toString();
    
    if (url.startsWith('file:')) {
        const filePath = fileURLToPath(url);
        const buffer = await fs.readFile(filePath);
        return new Response(buffer);
    }
    return fetch(url, options);
};

try {
    await init();
    
    const pkg = await Wasmer.fromRegistry("python/python");
    const instance = await pkg.entrypoint.run({
        args: ["-c", "print('Hello, World!')"],
    });

    const { code, stdout } = await instance.wait();
    console.log(`Python exited with ${code}: ${stdout}`);
} catch (error) {
    console.error('Error:', error);
}

Which generates this error, indicating python/python doesn't actually exist, is this accurate?

`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:
 TypeError [ERR_WEBASSEMBLY_RESPONSE]: WebAssembly response has unsupported MIME type 'null'
    at new NodeError (node:internal/errors:405:5)
    at node:internal/wasm_web_api:30:13 {
  code: 'ERR_WEBASSEMBLY_RESPONSE'
}
Error: Error: Unable to find "python/python" in the registry
    at e.wbg.__wbg_new_28c511d9baebfa89 (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:35384)
    at wasm://wasm/015c03b2:wasm-function[1466]:0x2fbfb8
    at wasm://wasm/015c03b2:wasm-function[6951]:0x4230c1
    at wasm://wasm/015c03b2:wasm-function[337]:0x10a2da
    at wasm://wasm/015c03b2:wasm-function[2038]:0x34ba1e
    at wasm://wasm/015c03b2:wasm-function[10511]:0x449902
    at wasm://wasm/015c03b2:wasm-function[9401]:0x44074e
    at v (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:2534)
    at o (file:///C:/code/onexdata/wasm/wasmer/node-server/node_modules/@wasmer/sdk/dist/index.mjs:11:2374) {
  detailedMessage: 'Unable to find "python/python" in the registry\n' +
    '\n' +
    'Caused by:\n' +
    "    0: failed to query package 'python/python': Could not fetch 'https://registry.wasmer.io/graphql'\n" +
    "    1: Could not fetch 'https://registry.wasmer.io/graphql'\n" +
    '    2: Invalid URL',
  causes: [
    "failed to query package 'python/python': Could not fetch 'https://registry.wasmer.io/graphql'",  
    "Could not fetch 'https://registry.wasmer.io/graphql'",
    'Invalid URL'
  ]
}
@onexdata
Copy link
Author

onexdata commented Jan 6, 2025

also, wasmer run python/python -- -c "print('Hello World')" works... so it seems to be a problem in the codebase?

@onexdata
Copy link
Author

onexdata commented Jan 6, 2025

I can also call wasmer cli from node, i.e. this works...

import { exec } from 'child_process';
import { promisify } from 'util';

const execAsync = promisify(exec);

async function runPython(code) {
    try {
        console.log('Running Python with Wasmer CLI...');
        // Escape the quotes properly
        const escapedCode = code.replace(/"/g, '\\"');
        const { stdout, stderr } = await execAsync(`wasmer run python/python -- -c "${escapedCode}"`);
        
        if (stderr) {
            console.error('Stderr:', stderr);
        }
        
        console.log('Stdout:', stdout);
        return { code: 0, stdout, stderr };
    } catch (error) {
        console.error('Error running Python:', error);
        return { 
            code: error.code || 1, 
            stdout: error.stdout, 
            stderr: error.stderr 
        };
    }
}

try {
    const result = await runPython('print("Hello, World!")');
    console.log(`Process exited with code ${result.code}`);
} catch (error) {
    console.error('Execution error:', error);
}

@onexdata
Copy link
Author

onexdata commented Jan 6, 2025

ok, it really seems this SDK just doesn't work, or I am really missing something basic...?

I found these issues so far, being unable to run anything from @wasmer/sdk , yet wasmer cli works fine.

Fetch not used properly:
The SDK can't handle file:// URLs natively in Node.js (fetch issues)
Required a custom fetch handler to load the initial WASM file

Runtime Management Issues:
The error "Expected a runtime" suggests unclear runtime initialization requirements
Runtime.global(true) and new Runtime() both had issues

GraphQL/Registry Communication:
Issues with the GraphQL endpoint communications
The SDK struggled with request/response handling for registry queries, yet if I write them myself, I can also get past this.

Required special handling of headers and content types for me to get past this.

WebC Format Handling:
Problems with WebC format downloads and parsing... the SDK has Content-type mismatches between what the SDK expects and what the CDN serves. I had to rewrite this myself to get past it.

Error Handling:
Cryptic error messages ("oneshot canceled")... Can't seem to get past this, even with AI assistance.

@sigmaSd
Copy link
Contributor

sigmaSd commented Jan 8, 2025

I run this with bun and it worked, I tried version 0.9.0 (latest currently)

import { init, Wasmer } from "@wasmer/[email protected]";

await init();

const pkg = await Wasmer.fromRegistry("python/python");
const instance = await pkg.entrypoint.run({
    args: ["-c", "print('Hello, World!')"],
});

const { code, stdout } = await instance.wait();
console.log(`Python exited with ${code}: ${stdout}`);

@geersch
Copy link

geersch commented Jan 18, 2025

Experiencing the same issues with the node example in this repository using node 20.

TypeError: fetch failed
    at node:internal/deps/undici/undici:13392:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    ...
starter/node_modules/@wasmer/sdk/dist/index.mjs:11:47648)
  ...
  [cause]: Error: not implemented... yet...

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants