nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.46k stars 279 forks source link

WebAssembly (WASM) with HTTP network requests failing #2758

Closed 3goats closed 1 year ago

3goats commented 4 years ago

What steps will reproduce the bug?

I'm importing a WebAssembly (WASM) component into my node app which is working to a point. However, the component needs to access the network to perform HTTP requests. Since the fetch API is not enabled I've installed the node-fetch module and made it available globally.

However this is causing a kernel panic when the WebAssembly tries to connect. e.g.

panic: syscall/js: call of Value.Invoke on undefined

goroutine 1 [running]:
syscall/js.Value.New(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x81aaa8)
    /usr/local/go/src/syscall/js/js.go:436 +0xa
net/http.(*Transport).RoundTrip(0x870a00, 0x8c2100, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/http/roundtrip_js.go:80 +0x25
net/http.send(0x8c2000, 0x10c120, 0x870a00, 0xbfaeb9cb920f69c0, 0x6fc3f7301, 0x4156c0, 0x0, 0x80c090, 0x8253b0, 0x1)
    /usr/local/go/src/net/http/client.go:252 +0x5c
net/http.(*Client).send(0x825380, 0x8c2000, 0xbfaeb9cb920f69c0, 0x6fc3f7301, 0x4156c0, 0x80c090, 0x0, 0x1, 0xa84bd)
    /usr/local/go/src/net/http/client.go:176 +0x13
net/http.(*Client).do(0x825380, 0x8c2000, 0x0, 0x0, 0x0)
    /usr/local/go/src/net/http/client.go:699 +0x38
net/http.(*Client).Do(...)
    /usr/local/go/src/net/http/client.go:567
github.com/Venafi/vcert/pkg/venafi/tpp.(*Connector).request(0x8284e0, 0xa7618, 0x4, 0xab35a, 0x11, 0x68e20, 0x80b000, 0x0, 0x0, 0x0, ...)
    /Users/cbourne/development/godev/src/github.com/Venafi/vcert/pkg/venafi/tpp/tpp.go:367 +0x35
github.com/Venafi/vcert/pkg/venafi/tpp.processAuthData(0x8284e0, 0xab35a, 0x11, 0x68e20, 0x80b000, 0x23, 0x23, 0x1c, 0xb1fd9)
    /Users/cbourne/development/godev/src/github.com/Venafi/vcert/pkg/venafi/tpp/connector.go:210 +0x2
github.com/Venafi/vcert/pkg/venafi/tpp.(*Connector).Authenticate(0x8284e0, 0x888200, 0x0, 0x0)
    /Users/cbourne/development/godev/src/github.com/Venafi/vcert/pkg/venafi/tpp/connector.go:119 +0x28
github.com/Venafi/vcert.(*Config).NewClient(0x82a640, 0x1010101007e0005, 0x10, 0x21cb0007, 0xa)
    /Users/cbourne/development/godev/src/github.com/Venafi/vcert/client.go:62 +0x13
github.com/Venafi/vcert.NewClient(...)
    /Users/cbourne/development/godev/src/github.com/Venafi/vcert/client.go:71
main.main()
    /Users/cbourne/development/godev/src/github.com/carlskii/vcert-wasm/main.go:51 +0x4
exit code: 2

This is the code I'm using for my test:

const fetch = require('node-fetch');

if (!globalThis.fetch) {
    globalThis.fetch = fetch;
}

import * as _ from "./wasm_exec.js";
const fs = require('fs');
const buf = fs.readFileSync('/Users/cbourne/development/godev/src/github.com/carlskii/vcert-wasm/vcert.wasm');

const go = new Go();

async function run() {
    const inst = await WebAssembly.instantiate(buf, go.importObject);

    go.run(inst.instance);
}
run()

If I remove node-fetch you can see it's failing to connect. e.g.

vCert: 2020/06/05 17:35:21 could not connect to endpoint: vcert error: your data contains problems: auth error: Post "https://server/authorize/": dial tcp: Protocol not available

This works fine in a browser and also using deno. Not sure if its a bug or something I'm missing.

For reference, here is the deno code and output:

import * as _ from "./wasm_exec.js";
const go = new window.Go();
const f = await Deno.open("/Users/cbourne/development/godev/src/github.com/carlskii/vcert-wasm/vcert.wasm")
const buf = await Deno.readAll(f);
const inst = await WebAssembly.instantiate(buf, go.importObject);
go.run(inst.instance);

Output:

deno run --allow-read --allow-net deno-test.js
vCert: 2020/06/05 17:40:50 Successfully submitted certificate request. Will pickup certificate by ID \VED\Policy\vcert\carlskii.example.com
vCert: 2020/06/05 17:40:54 Successfully picked up certificate for carlskii.example.com
vCert: 2020/06/05 17:40:54 {
    "Certificate": "-----BEGIN CERTIFICATE-----\nMII.........
addaleax commented 4 years ago

This sounds like some kind of incompatibility between whatever provides the Go class and node-fetch?

3goats commented 4 years ago

Hmm yes - Since it works in the browser and with Deno, I'm not sure where to start looking.

github-actions[bot] commented 1 year ago

There has been no activity on this issue for 3 years and it may no longer be relevant. It will be closed 1 month after the last non-automated comment.

github-actions[bot] commented 1 year ago

There has been no activity on this issue and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.