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

feat: add impl for node sockets (wip) #214

Merged
merged 66 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
20ccc39
feat: add initial impl for node sockets
manekinekko Oct 17, 2023
3ee45c7
chore: update sockets tests
manekinekko Oct 18, 2023
3e532de
feat: add TcpSocketImpl
manekinekko Oct 18, 2023
02ccff8
chore: temp disable eslint in lib/sockets/**
manekinekko Oct 18, 2023
79e570d
chore: refactor instanceNetwork + dropNetwork
manekinekko Oct 18, 2023
b9ad223
chore: tcp socket impl wip
manekinekko Oct 19, 2023
877c5ff
chore: createTcpSocket/startBind/finishBind
manekinekko Oct 19, 2023
daacaec
fix: dupe-class-members
manekinekko Oct 19, 2023
a4c4322
chore: add throws entry to jsdoc
manekinekko Oct 19, 2023
a76897d
chore: improve startBind() logic
manekinekko Oct 22, 2023
0867ff2
chore: improve startConnect() + test
manekinekko Oct 22, 2023
0e978a8
chore: improve tcp-socket impl
manekinekko Oct 22, 2023
f4abc22
refactor: start-* finish-* methods to match specs
manekinekko Oct 23, 2023
1a17836
chore: use assertion statement in tcp-socket-impl
manekinekko Oct 23, 2023
4d42d3e
refactor: tcp-socket-impl
manekinekko Oct 23, 2023
b171226
chore: use err.code to catch sockets errors
manekinekko Oct 23, 2023
b86f718
chore: migrate tcp-socket-impl to use tcp_wrap
manekinekko Oct 24, 2023
e112b10
chore: mock listen and connect calls
manekinekko Oct 24, 2023
5a844a0
fix: bind connectReq.oncomplete to class method
manekinekko Oct 24, 2023
e9e87bc
chore: reorder test suites
manekinekko Oct 25, 2023
e241b8a
chore: add e2e test for tcp-socket-impl
manekinekko Oct 25, 2023
8e08102
chore: hook up internal events
manekinekko Oct 25, 2023
991a336
chore: update impl to match latest wit specs
manekinekko Oct 25, 2023
111ab21
fix: support bind6 and connect6
manekinekko Oct 25, 2023
28db1f3
chore: move assert to own file
manekinekko Oct 27, 2023
d229465
chore: use assert
manekinekko Oct 27, 2023
104c8f8
chore: use ipv6 in tests
manekinekko Oct 27, 2023
d84b70b
chore: add streams to tcp-socket-impl (wip)
manekinekko Oct 30, 2023
de6e962
fix: add error code -49 address-not-bindable
manekinekko Oct 30, 2023
a62c6b3
feat: add udp-socket-impl method signatures
manekinekko Oct 30, 2023
dd0ed90
fix: handle error -99
manekinekko Oct 30, 2023
2b29f5c
chore: document udp errors
manekinekko Nov 3, 2023
46be9ea
chore: wip
manekinekko Nov 3, 2023
701a546
feat(udp): wip impl
manekinekko Nov 7, 2023
417e6d9
chore: clean imports
manekinekko Nov 9, 2023
48f98cb
chore: use new folder structure
manekinekko Nov 15, 2023
e73db11
chore(udp): use Symbols for local state
manekinekko Nov 15, 2023
a051016
chore: sync impl with lastest specs
manekinekko Nov 15, 2023
c2d1846
chore: remove useless logs
manekinekko Nov 15, 2023
80e2d32
chore(udp): fix according to conformance tests
manekinekko Nov 16, 2023
5c81f1c
fix(tcp): removed deprecated methods
manekinekko Nov 16, 2023
18990d4
chore: try to reuse an existing udp socket (wip)
manekinekko Nov 18, 2023
78c7b8b
fix: make conformance preview2_udp_states pass
manekinekko Nov 19, 2023
484598f
feat: add ip-name-lookup (wip)
manekinekko Nov 21, 2023
0e8eb1f
chore: socket resolve addresses (#1)
guybedford Nov 21, 2023
e134d09
chore: add missing import from node:dns/promises
manekinekko Nov 21, 2023
888f873
chore: resolve ipv6 :: and ::1
manekinekko Nov 21, 2023
b9e43d7
fix(udp): make preview2_udp_sockopts apss
manekinekko Nov 23, 2023
ef33ccb
chore: delete unused code
manekinekko Nov 23, 2023
01f3683
chore: add comments
manekinekko Nov 23, 2023
e55f43b
chore: more conformance tests passing!
manekinekko Nov 23, 2023
3a3869e
chore: use enums for socket conn state
manekinekko Nov 23, 2023
58e9bce
chore: make preview2_tcp_states pass
manekinekko Nov 23, 2023
c0c2839
fix: make preview2_tcp_connect pass (wip)
manekinekko Nov 24, 2023
9f77ac1
fix: refactor isUnicastIpAddress
manekinekko Nov 24, 2023
881baa6
fix: make preview2_tcp_bind pass
manekinekko Nov 24, 2023
ab406e7
fix: make preview2_tcp_connect pass
manekinekko Nov 24, 2023
5871ad4
chore: refactor code
manekinekko Nov 26, 2023
39949de
chore: preview2_tcp_sample_application wip
manekinekko Nov 26, 2023
bf5965c
fix: preview2_tcp_bind and preview2_udp_bind
manekinekko Nov 26, 2023
9320d5d
Merge branch 'main' into feat/nodejs-socket
manekinekko Nov 27, 2023
6e2bf22
chore: stashing wip fixes
manekinekko Nov 27, 2023
908297a
fix: improve tests and add more comments for hop limits
manekinekko Nov 27, 2023
dead3c0
chore: rename errorState property
manekinekko Nov 27, 2023
d3ce7c0
fix: make unit tests pass
manekinekko Nov 28, 2023
5371db2
chore: fix linting
manekinekko Nov 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/preview2-shim/lib/common/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function assert(condition, tag, _val) {
if (condition) {
// TODO: throw meaningful errors
// NOTE: wasmtime conformance tests are expecting a string here (a tag)
throw tag;
}
}
5 changes: 5 additions & 0 deletions packages/preview2-shim/lib/io/calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ export const HTTP_CREATE_REQUEST = ++call_id << 24;
export const CLOCKS_NOW = ++call_id << 24;
export const CLOCKS_DURATION_SUBSCRIBE = ++call_id << 24;
export const CLOCKS_INSTANT_SUBSCRIBE = ++call_id << 24;

// Sockets
export const SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST = ++call_id << 24;
export const SOCKET_RESOLVE_ADDRESS_GET_AND_DISPOSE_REQUEST = ++call_id << 24;
export const SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST = ++call_id << 24;
1 change: 1 addition & 0 deletions packages/preview2-shim/lib/io/stream-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export const STDIN = ++cnt;
export const STDOUT = ++cnt;
export const STDERR = ++cnt;
export const FILE = ++cnt;
export const SOCKET = ++cnt;
export const INCOMING_BODY = ++cnt;
export const OUTGOING_BODY = ++cnt;
44 changes: 37 additions & 7 deletions packages/preview2-shim/lib/io/worker-thread.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { runAsWorker } from "../synckit/index.js";
import { FILE, STDOUT, STDERR, STDIN } from "./stream-types.js";
import { resolve } from "node:dns/promises";
import { createReadStream, createWriteStream } from "node:fs";
import { Readable } from "node:stream";
import { hrtime } from "node:process";
import { Readable } from "node:stream";
import { runAsWorker } from "../synckit/index.js";
import {
CALL_MASK,
CALL_SHIFT,
CALL_TYPE_MASK,
CLOCKS_DURATION_SUBSCRIBE,
CLOCKS_INSTANT_SUBSCRIBE,
CLOCKS_NOW,
FUTURE_DISPOSE_AND_GET_VALUE,
FUTURE_DISPOSE,
FUTURE_DISPOSE_AND_GET_VALUE,
HTTP_CREATE_REQUEST,
INPUT_STREAM_BLOCKING_READ,
INPUT_STREAM_BLOCKING_SKIP,
Expand All @@ -30,12 +30,16 @@ import {
OUTPUT_STREAM_FLUSH,
OUTPUT_STREAM_SPLICE,
OUTPUT_STREAM_SUBSCRIBE,
OUTPUT_STREAM_WRITE_ZEROES,
OUTPUT_STREAM_WRITE,
POLL_POLL_LIST,
OUTPUT_STREAM_WRITE_ZEROES,
POLL_POLLABLE_BLOCK,
POLL_POLLABLE_READY,
POLL_POLL_LIST,
SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST,
SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST,
SOCKET_RESOLVE_ADDRESS_GET_AND_DISPOSE_REQUEST,
} from "./calls.js";
import { FILE, SOCKET, STDERR, STDIN, STDOUT } from "./stream-types.js";

let streamCnt = 0,
pollCnt = 0;
Expand Down Expand Up @@ -120,6 +124,30 @@ function handle(call, id, payload) {
return createFuture(createHttpRequest(method, url, headers, body));
}

// Sockets
case SOCKET_RESOLVE_ADDRESS_CREATE_REQUEST:
return createFuture(resolve(payload.hostname));
case SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST:
return void unfinishedFutures.delete(id);
case SOCKET_RESOLVE_ADDRESS_GET_AND_DISPOSE_REQUEST: {
const future = unfinishedFutures.get(id);
if (!future) {
// future not ready yet
if (unfinishedPolls.get(id)) {
throw 'would-block';
}
throw new Error("future already got and dropped");
}
unfinishedFutures.delete(id);
return future;
}
case OUTPUT_STREAM_CREATE | SOCKET: {
// TODO: implement
}
case INPUT_STREAM_CREATE | SOCKET: {
// TODO: implement
}

// Stdio
case OUTPUT_STREAM_DISPOSE | STDOUT:
case OUTPUT_STREAM_DISPOSE | STDERR:
Expand Down Expand Up @@ -434,14 +462,16 @@ function handle(call, id, payload) {
}

// poll promises must always resolve and never error
// once the future is resolved, it is removed from unfinishedPolls
function createPoll(promise) {
const pollId = ++pollCnt;
unfinishedPolls.set(
pollId,
promise.then(
() => void unfinishedPolls.delete(pollId),
() => {
(err) => {
process._rawDebug("Unexpected poll error");
process._rawDebug(err);
process.exit(1);
}
)
Expand Down
3 changes: 3 additions & 0 deletions packages/preview2-shim/lib/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ export {
sockets,
cli
}

export { WasiSockets } from "./sockets/wasi-sockets.js";

213 changes: 13 additions & 200 deletions packages/preview2-shim/lib/nodejs/sockets.js
Original file line number Diff line number Diff line change
@@ -1,200 +1,13 @@
export const instanceNetwork = {
instanceNetwork () {
console.log(`[sockets] instance network`);
}
};

export const ipNameLookup = {
dropResolveAddressStream () {

},
subscribe () {

},
resolveAddresses () {

},
resolveNextAddress () {

},
nonBlocking () {

},
setNonBlocking () {

},
};

export const network = {
dropNetwork () {

}
};

export const tcpCreateSocket = {
createTcpSocket () {

}
};

export const tcp = {
subscribe () {

},
dropTcpSocket() {

},
bind() {

},
connect() {

},
listen() {

},
accept() {

},
localAddress() {

},
remoteAddress() {

},
addressFamily() {

},
ipv6Only() {

},
setIpv6Only() {

},
setListenBacklogSize() {

},
keepAlive() {

},
setKeepAlive() {

},
noDelay() {

},
setNoDelay() {

},
unicastHopLimit() {

},
setUnicastHopLimit() {

},
receiveBufferSize() {

},
setReceiveBufferSize() {

},
sendBufferSize() {

},
setSendBufferSize() {

},
nonBlocking() {

},
setNonBlocking() {

},
shutdown() {

}
};

export const udp = {
subscribe () {

},

dropUdpSocket () {

},

bind () {

},

connect () {

},

receive () {

},

send () {

},

localAddress () {

},

remoteAddress () {

},

addressFamily () {

},

ipv6Only () {

},

setIpv6Only () {

},

unicastHopLimit () {

},

setUnicastHopLimit () {

},

receiveBufferSize () {

},

setReceiveBufferSize () {

},

sendBufferSize () {

},

setSendBufferSize () {

},

nonBlocking () {

},

setNonBlocking () {

}
};

export const udpCreateSocket = {
createTcpSocket () {

}
};
/* eslint-disable no-unused-vars */

import { WasiSockets } from "./sockets/wasi-sockets.js";

export const {
ipNameLookup,
instanceNetwork,
network,
tcpCreateSocket,
udpCreateSocket,
tcp,
udp,
} = new WasiSockets();;
Loading