workerd icon indicating copy to clipboard operation
workerd copied to clipboard

hello-wasm sample not works

Open liweijian opened this issue 2 years ago • 2 comments

I install workerd from npm

After entering samples/hello-wasm, I ran:

❯ cargo install worker-build
    Updating crates.io index
  Downloaded worker-build v0.0.9
...
  Installed package `worker-build v0.0.9` (executable `worker-build`)

❯ worker-build --release
Installing wasm-pack...
    Updating crates.io index
  Downloaded wasm-pack v0.11.1
...
   Compiling hello-wasm v0.1.0 (/Users/tli/Downloads/toys/workerd/samples/hello-wasm)
    Finished release [optimized] target(s) in 11.26s
[INFO]: ⬇️  Installing wasm-bindgen...
...
[INFO]: 📦   Your wasm pkg is ready to publish at /Users/tli/Downloads/toys/workerd/samples/hello-wasm/build.
Installing esbuild...

  shim.mjs  14.2kb

⚡ Done in 11ms

And there're index.wasm and shim.mjs:

❯ ls build/worker
index.wasm	shim.mjs

According to the README file:

❯ workerd serve config.capnp
❯ echo $?
0

It just exit, the 8080 web server not work, just wonder if anything I'd missed?

liweijian avatar Jun 14 '23 07:06 liweijian

It starts for me, but the /worker-version route crashes:

workerd/server/server.c++:2420: error: Uncaught exception: workerd/io/io-context.c++:1265: failed: remote.jsg.Error: The script will never generate a response.
stack: workerd@19186f0 workerd@19195d0 workerd@1919c00 workerd@2116be0 workerd@2119d10

meldron avatar Jul 25 '23 08:07 meldron

@liweijian you need to provide the js code to workerd binary to run the wasm file, like this

import mod from './add.wasm';

const instance = await WebAssembly.instantiate(mod);

export default {
  async fetch(req) {
    console.log('req', req);
    const retval = instance.exports.add(1000, 2);
    return new Response(`Success: ${retval}`);
  }
}

This code will run, if you will provide wasm file generated through rust code. By the way I am also facing one problem which is: the above code works but when you generate the wasm file through code written in golang, you need a wasm_exec.js file to the js code, and I am doing that as well, but the error I get is weird, don't know where I am wrong, see if you can help.

import './wasm_exec.js';

export default {
  async fetch(req, env) {
    try {
      const go = new Go();
      const wasmBytes = await env["main.wasm"].arrayBuffer();
      console.log("WASM bytes loaded, length:", wasmBytes.byteLength);
      
      const { instance } = await WebAssembly.instantiate(wasmBytes, go.importObject);
      console.log("WASM instantiated successfully");

      go.run(instance);
      console.log("Go runtime started");

      const addResult = instance.exports.add(1000, 2);
      console.log("Add function called, result:", addResult);

      return new Response(`Success: ${addResult}`);
    } catch (e) {
      console.error("Error in worker:", e);
      return new Response(`Error: ${e.message}`, { status: 500 });
    }
  }
}

CAPNP file:

using Workerd = import "/workerd/workerd.capnp";

const config :Workerd.Config = (
  services = [
    (name = "YPYoXk", worker = .vYPYoXkWorker),
  ],

  sockets = [
    (
      name = "YPYoXk",
      http = (),
      service = "YPYoXk",
    ),
  ]

);

const vYPYoXkWorker :Workerd.Worker = (
  serviceWorkerScript = embed "src/entry.js",
  modules = [
    (name = "wasm_exec.js", esModule = embed "src/wasm_exec.js"),
    
    (name = "main.wasm", wasm = embed "src/main.wasm"),
    
    (name = "entry.js", esModule = embed "src/entry.js"),
  ],
  compatibilityDate = "2024-04-03",
  compatibilityFlags = ["nodejs_compat"],
  bindings = [
    
  ],
);

Exact error I am getting:

workerd/io/worker-entrypoint.c++:338: error: e = kj/async-io-unix.c++:1645: failed: connect() blocked by restrictPeers()
stack: 102fd9000 102fd9950 102fcba90 102fcd570 101747d6c 102faaf60 102fb5820 100d152e0 1005262a0 100b1e1a0; sentryErrorContext = workerEntrypoint
workerd/server/server.c++:3360: error: Uncaught exception: kj/async-io-unix.c++:1645: failed: worker_do_not_log; Request failed due to internal error
stack: 102fd9000 102fd9950 102fcba90 102fcd570 101747d6c 102faaf60 102fb5820 100d152e0 1005262a0 100b1e1a0 10054c80c 101749b27 10174ad68 10174b7bf 10170ebf0

SagarM21 avatar Aug 27 '24 07:08 SagarM21