Skip to content

hello-wasm sample not works #768

Open

Description

@liweijian

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?

Activity

  1. meldron commented on Jul 25, 2023

    @meldron

    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
    
  2. SagarM21 commented on Aug 27, 2024

    @SagarM21

    @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
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions