Stream: jco

Topic: sockets on node.js


view this post on Zulip Wassim Chegham (Oct 17 2023 at 22:55):

Yay! I managed to create a new topic!

view this post on Zulip Wassim Chegham (Oct 17 2023 at 22:56):

@Guy Bedford please let me know If I am heading in the right direction: https://github.com/bytecodealliance/jco/pull/214

A work in progress PR that adds support for node sockets.

view this post on Zulip Guy Bedford (Oct 17 2023 at 23:05):

@Wassim Chegham nice start that's definitely the right track!

view this post on Zulip Wassim Chegham (Oct 18 2023 at 08:50):

awesome! Thanks @Guy Bedford

view this post on Zulip Ralph (Oct 18 2023 at 08:51):

bless you people

view this post on Zulip Wassim Chegham (Oct 18 2023 at 08:52):

Q: are we supposed to write our own implementations for wasi-sockets, or use node:net, node:dgram, etc?

view this post on Zulip Wassim Chegham (Oct 18 2023 at 08:53):

Ralph said:

bless you people

Hi @Ralph :wave:

view this post on Zulip Yoshua Wuyts (Oct 18 2023 at 11:12):

Wassim Chegham said:

Q: are we supposed to write our own implementations for wasi-sockets, or use node:net, node:dgram, etc?

We should be reusing node:net, node:dgram, etc. for this. The way we're implementing this is by "virtualizing" the Wasi Preview 2 syscalls, backing them with Node's built-ins.

view this post on Zulip Yoshua Wuyts (Oct 18 2023 at 11:15):

Eventually the plan is to move these bindings from being a virtualized JS layer to the uvwasi project directly. But that will require some work to implement, upstream, and validate. It's all C++, and we'd need to not only land this in uvwasi but also upstream it to Node.js proper. So we're essentially punting on that and thinking of it as just an optimization.

WASI syscall API built atop libuv. Contribute to nodejs/uvwasi development by creating an account on GitHub.

view this post on Zulip Yoshua Wuyts (Oct 18 2023 at 11:17):

@Wassim Chegham oh by the way, the sockets API is one of the biggest surface areas of Wasi (52 methods). Doing it all in a single PR might be a lot, so please feel free to like file multiple PRs. That might make it easier to validate, review, test, etc.

view this post on Zulip Wassim Chegham (Oct 18 2023 at 13:46):

Thanks @Yoshua Wuyts that was my understanding from what @Guy Bedford said on the call. I was double-checking ^_^

view this post on Zulip Guy Bedford (Oct 18 2023 at 16:45):

yeah, that's exactly the approach - using the Node.js sync but streaming APIs where appropriate. When we have the asyncify work we will be able to call promise-based builtins, but for now perhaps we can see how far we can get while that work is still outstanding.

view this post on Zulip Guy Bedford (Oct 18 2023 at 17:03):

the other stopgap we have for not having asyncify support in Node.js is the approach we use here to syncify async APIs - https://github.com/bytecodealliance/jco/blob/main/packages/preview2-shim/lib/http/wasi-http.js#L20

view this post on Zulip Guy Bedford (Oct 18 2023 at 17:04):

the asyncify work will be a little while out so we do just have to work around that for now

view this post on Zulip Wassim Chegham (Oct 19 2023 at 08:15):

is there a reason why IP addresses are typed as tuples?

type ipv4-address = tuple<u8, u8, u8, u8>
type ipv6-address = tuple<u16, u16, u16, u16, u16, u16, u16, u16>

which translate to:

export type Ipv4Address = [number, number, number, number];
export type Ipv6Address = [number, number, number, number, number, number, number, number];

view this post on Zulip Wassim Chegham (Oct 19 2023 at 08:18):

follwing that interface, a localAdress object would look like:

        const localAddress = {
          tag: sockets.network.IpAddressFamily.ipv4,
          val: {
            address: [0, 0, 0, 0],
            port: 0,
          },
        };

view this post on Zulip Wassim Chegham (Oct 19 2023 at 08:19):

is that what we are expecting?

view this post on Zulip Guy Bedford (Oct 19 2023 at 16:35):

Yes in JS that's the representation of a tuple

view this post on Zulip Wassim Chegham (Oct 19 2023 at 22:17):

Great! thank you!

view this post on Zulip Wassim Chegham (Oct 24 2023 at 09:34):

I am trying to write integration tests for wasi-sockets (nodejs impl) inside of ./test/preview2.js, but it looks like I am missing the obj/ folder. I assumed this folder gets generate after building the projet. I tried to run npm run build from the root and I am getting these errors. Did I miss something?

image.png

view this post on Zulip Guy Bedford (Oct 24 2023 at 17:24):

@Wassim Chegham make sure you are on the latest Rust toolchain - rustup update and then with wasm32-unknown-unknown and wasm32-wasi targets installed. npm install && npm run build should then be all you need.

view this post on Zulip Wassim Chegham (Oct 25 2023 at 09:13):

good catch @Guy Bedford I was missing the wasm32-wasi target 🤦🏽 now it's building. Thanks

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:49):

I've updated https://github.com/bytecodealliance/jco/pull/214 to match the latest WIT updates

A work in progress PR that adds support for node sockets. Closes #154 Done instance-network.instance-network ip-name-lookup.resolve-addresses ip-name-lookup.resolve-next-address ip-name-lookup...

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:51):

Also, @Guy Bedford I've changed the impl to use node.js's internal libuv bindings (tcp_wrap). That was the only way for us to be able to implement the specified WIT ressources.

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:52):

https://github.com/bytecodealliance/jco/pull/214/files#diff-73e7565cdc5599fda2d180adcdb08293c5116cfb26eef129343727c94c0f4052R15-R20

A work in progress PR that adds support for node sockets. Closes #154 Done instance-network.instance-network ip-name-lookup.resolve-addresses ip-name-lookup.resolve-next-address ip-name-lookup...

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:55):

nice, I guess that will mean we don't support other JS runtimes like Deno or Bun, but we can investigate deno and Bun-specific implementations instead I think then?

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:55):

But (yes there is a but), I could not figure out what should be the impl of:

accept: func() -> result<tuple<tcp-socket, input-stream, output-stream>, error-code>;
receive-buffer-size: func() -> result<u64, error-code>;
set-receive-buffer-size: func(value: u64) -> result<_, error-code>;
send-buffer-size: func() -> result<u64, error-code>;
set-send-buffer-size: func(value: u64) -> result<_, error-code>;
unicast-hop-limit: func() -> result<u8, error-code>;
set-unicast-hop-limit: func(value: u8) -> result<_, error-code>;

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:55):

it seems to me like the correct approach

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:56):

there have been a number of changes to the sockets API

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:56):

it can be worth keeping an eye on https://github.com/bytecodealliance/wasmtime/pulls?q=is%3Apr+sockets+is%3Aclosed

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:56):

yes, I went through them and updated the impl

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:56):

to see the history and upcoming changes as well

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:57):

those seem like network configurations

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:57):

Will do

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:57):

perhaps if we don't have those hooks in Node.js we do just need noops

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:57):

it's looking really great though, congrats

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:57):

the resources update is the current big one I'm working on for HTTP

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:58):

I guess we will need similar updates on sockets

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:58):

perhaps have a look at my next HTTP PR I'm hoping to finish up today or tomorrow

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:58):

Thanks!

Guy Bedford said:

it's looking really great though, congrats

It's missing reads and writes thouhg :smile:

view this post on Zulip Guy Bedford (Oct 25 2023 at 16:58):

right, perhaps that will be related to the streams updates then

view this post on Zulip Wassim Chegham (Oct 25 2023 at 16:59):

In my last commit, I update the impl to remove all the IDs https://github.com/bytecodealliance/jco/pull/214/commits/54ab5cc3fe948f547f2f107cf1cd03f77d388e04

A work in progress PR that adds support for node sockets. Closes #154 Done instance-network.instance-network ip-name-lookup.resolve-addresses ip-name-lookup.resolve-next-address ip-name-lookup...

view this post on Zulip Wassim Chegham (Oct 25 2023 at 17:01):

I am planning to start working on upd next :+1:

view this post on Zulip Guy Bedford (Oct 25 2023 at 17:02):

ah nice to see you figured that out

view this post on Zulip Guy Bedford (Oct 25 2023 at 17:03):

if you want to verify the interface against the types in https://github.com/bytecodealliance/jco/blob/main/packages/preview2-shim/types/interfaces/wasi-sockets-tcp.d.ts that should roughly match what is the expected shape

view this post on Zulip Wassim Chegham (Oct 25 2023 at 17:07):

Yes. That what I did. I see you updated the type def :+1:

view this post on Zulip Guy Bedford (Oct 25 2023 at 17:09):

yeah it was a bunch of diffs, thank you for figuring it out there, that wasn't obvious I know

view this post on Zulip Wassim Chegham (Oct 25 2023 at 21:40):

Guy Bedford said:

yeah it was a bunch of diffs, thank you for figuring it out there, that wasn't obvious I know

haha! I am used to reading diffs :grinning:

view this post on Zulip Wassim Chegham (Oct 25 2023 at 21:41):

Wassim Chegham said:

I am planning to start working on upd next :+1:

obviously I meant UDP 🤦🏽

view this post on Zulip Guy Bedford (Oct 26 2023 at 22:51):

@Wassim Chegham I've been implementing private fields on the other classes, based on what you were doing

view this post on Zulip Guy Bedford (Oct 26 2023 at 22:51):

thanks for the idea on that one!

view this post on Zulip Wassim Chegham (Nov 03 2023 at 21:04):

@Guy Bedford I applied the latest changes from the main branch to my WIP branch and the FS test is failing because of the new Symbol.dispose: TypeError: stream[Symbol.dispose] is not a function. Any idea why is that?
image.png

view this post on Zulip Guy Bedford (Nov 03 2023 at 21:04):

perhaps try deleting node_modules and reinstalling?

view this post on Zulip Wassim Chegham (Nov 03 2023 at 21:05):

still failing

view this post on Zulip Wassim Chegham (Nov 03 2023 at 21:06):

can I like cargo xtask clean build?

view this post on Zulip Guy Bedford (Nov 03 2023 at 21:06):

sounds like it might be a bug only exposed on your branch then

view this post on Zulip Guy Bedford (Nov 03 2023 at 21:06):

would you like to pair on it?

view this post on Zulip Wassim Chegham (Nov 03 2023 at 21:08):

sure. Let me switch computer. WIll ping you back.

view this post on Zulip Wassim Chegham (Nov 03 2023 at 21:15):

Sent you a DM on Twitter

view this post on Zulip Wassim Chegham (Nov 17 2023 at 09:27):

Adds a new --jco-debug option to jco run which doesn't delete the temporary files (so they can be debugged) and also outputs the trace calls. (https://github.com/bytecodealliance/jco/pull/266)

Thank you so much @Guy Bedford for this! It will save me a lot of headaches. I was currently basically manually compiling each test program and copying it around so I could debug it

./src/jco.js new ./submodules/wasmtime/target/wasm32-wasi/release/preview2_udp_connect.wasm --wasi-command -o __test.wasm
./src/jco.js transpile __test.wasm  -o ./

view this post on Zulip Wassim Chegham (Nov 17 2023 at 09:35):

Yay! Made my 1st conformance test pass!!
image.png

view this post on Zulip Wassim Chegham (Nov 23 2023 at 14:55):

Moar conformace tests passing for wasi-socket
image.png

view this post on Zulip Wassim Chegham (Nov 24 2023 at 10:19):

Hi, I just opened an issue in the wasi-socket repo about a question I have, would love if any of you can help https://github.com/WebAssembly/wasi-sockets/issues/82 (cc @Pat Hickey )

Hi, I am currently implementing wasi-sockets for jco, and I am encountering an unspecificed behavior. The test_tcp_connect_dual_stack test inside of the test programm preview2_tcp_connect.rs, creat...

view this post on Zulip Wassim Chegham (Nov 28 2023 at 15:23):

@Guy Bedford as discussed yesterday, the wasi-sockets impl for node.js PR is ready for merge: https://github.com/bytecodealliance/jco/pull/214

A work in progress PR that adds support for node sockets. Closes #154 (check issue for progress)

view this post on Zulip Wassim Chegham (Nov 28 2023 at 15:23):

@Guy Bedford as discussed yesterday, the wasi-sockets impl for node.js PR is ready for merge: https://github.com/bytecodealliance/jco/pull/214

A work in progress PR that adds support for node sockets. Closes #154 (check issue for progress)

view this post on Zulip Wassim Chegham (Dec 12 2023 at 16:34):

@Guy Bedford The wasi-sockets UDP implem is fully complete (minus a few edge cases - see TODOs in the code) https://github.com/bytecodealliance/jco/pull/306 - I am planning to tackle wasi-sockets TCP next.

preview2_udp_bind test_udp_bind_ephemeral_port(&net, IpAddress::IPV4_LOOPBACK); test_udp_bind_ephemeral_port(&net, IpAddress::IPV6_LOOPBACK); test_udp_bind_ephemeral_port(&net, IpAddress::IPV4_...

view this post on Zulip Ralph (Dec 12 2023 at 16:36):

yay!!!!

view this post on Zulip Guy Bedford (Dec 12 2023 at 16:38):

fantastic, will review shortly

view this post on Zulip Wassim Chegham (Dec 14 2023 at 11:01):

Heya @Guy Bedford is there an issue with the recent changes on main? The test runner is now broken. Do we now need to build the tests separatly?

[cause]: 'Unable to read file \x1B[1m./tests/rundir/preview2_udp_bind.component.wasm\x1B[22m'

image.png

view this post on Zulip Guy Bedford (Dec 14 2023 at 17:10):

run cargo xtask generate tests

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:29):

anyone knows why socket.getRecvbufferSize() would report a different value than the one is set. Eg. in the preview2_udp_sockopts.rs we are setting a buffer size of 0x10000 (66kb) but when reading the value, we get 213kb?

thread 'main' panicked at crates/test-programs/src/bin/preview2_udp_sockopts.rs:61:5:
assertion `left == right` failed
  left: 212992
 right: 65536
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

image.png

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:42):

@Wassim Chegham perhaps you need to conver the BigInt to a number when setting it

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:43):

Number(size)

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:44):

Sure. I tried that but that wasn't the issue :smile:
image.png

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:45):

While browsing the Internet, I found this affirmation: "In this system, by default, each socket will get a receive buffer with a size of 213 KB. Additionally, the rmem_max value of 213 KB restricts the maximum allowable receive buffer size to 213 KB during socket creation."
https://www.baeldung.com/linux/udp-socket-buffer

Learn about UDP sockets in Linux.

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:46):

so does this mean that setting a value has no effect?!

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:46):

so I guess it's something that should have an isolated replication

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:47):

trying now... will report

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:48):

var s = new dgram.Socket('udp4')
s.bind();
s.setRecvBufferSize(1000);
s.getRecvBufferSize()

definitely seems to work for me

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:48):

and 65536 also works

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:48):

perhaps enable the worker-io call logging to verify its definitely being set

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:49):

or log around the call sites for the set and get

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:49):

or maybe the replication is more specific and is socket-specific

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:51):

interesting. Your exact code gave me a EBADF error.

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:52):

Hummm
image.png

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:53):

@Guy Bedford can you try this code please?

import { Socket } from "node:dgram";
var s = new Socket('udp4')
s.bind(() => {
    console.log('set 1000');
    s.setRecvBufferSize(1000);
    console.log('get', s.getRecvBufferSize())
});

I get this on WSL:

set 1000
get 2304

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:53):

so on my machine, I'm getting the same value in get

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:53):

on that exact case

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:54):

macOS?

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:54):

so perhaps this is system specific or something? are you on Windows?

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:54):

Yes, WSL (ubuntu)

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:54):

it might be specific to that environment somehow, I'm not sure

view this post on Zulip Wassim Chegham (Dec 14 2023 at 22:54):

ok will add a comment and push so that CI runs

view this post on Zulip Guy Bedford (Dec 14 2023 at 22:55):

set 1000
get 1000

view this post on Zulip Wassim Chegham (Dec 15 2023 at 23:28):

Heya. So I've submitted a bunch of PRs that are migrating TCP impl to worker thread:

I am tracking the remaining tests to be fixed in https://github.com/bytecodealliance/jco/issues/315. Will work on it over the weekend (once 320 is merged).

So now I am gonna get some Zzzzzzzzz :smile:

✅preview2_tcp_bind ✅ preview2_tcp_sockopts ✅ preview2_tcp_states ❌ preview2_tcp_connect ❌ preview2_tcp_sample_application Closes #154
✅preview2_tcp_bind ❌ preview2_tcp_connect ❌ preview2_tcp_sample_application ❌ preview2_tcp_sockopts ❌ preview2_tcp_states * Failing tests are due to missing stream impl Closes #315
JavaScript tooling for working with WebAssembly Components - chore: fix tcp lint by manekinekko · Pull Request #319 · bytecodealliance/jco
Both preview2_tcp_states and preview2_tcp_sockopts are passing. Related #315

view this post on Zulip Guy Bedford (Dec 15 2023 at 23:50):

amazing!!!! thank you Wassim this is phenomenal progress, and yes please get some rest there!

view this post on Zulip Wassim Chegham (Dec 17 2023 at 02:00):

Just submitted a new PR that makes preview2_tcp_connect pass! https://github.com/bytecodealliance/jco/pull/321

Related #315

Last updated: Dec 23 2024 at 13:07 UTC