From 5ce98ee75e46c767ef3f7a64f9da79d921f5acfe Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Tue, 24 Oct 2023 12:11:22 -0700 Subject: [PATCH 1/4] refactor(daemon): Extract SocketPowers --- packages/daemon/src/daemon-node-powers.js | 22 ++++++++++++++++------ packages/daemon/src/types.d.ts | 5 ++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/daemon/src/daemon-node-powers.js b/packages/daemon/src/daemon-node-powers.js index 31d737d637..327a368ec2 100644 --- a/packages/daemon/src/daemon-node-powers.js +++ b/packages/daemon/src/daemon-node-powers.js @@ -179,13 +179,9 @@ export const makeHttpPowers = ({ http, ws }) => { /** * @param {object} modules * @param {typeof import('net')} modules.net - * @param {typeof import('http')} modules.http - * @param {typeof import('ws')} modules.ws - * @returns {import('./types.js').NetworkPowers} + * @returns {import('./types.js').SocketPowers} */ -export const makeNetworkPowers = ({ http, ws, net }) => { - const { servePortHttp } = makeHttpPowers({ http, ws }); - +export const makeSocketPowers = ({ net }) => { const serveListener = async (listen, cancelled) => { const [ /** @type {Reader} */ readFrom, @@ -260,6 +256,20 @@ export const makeNetworkPowers = ({ http, ws, net }) => { ); }, cancelled); + return { servePort, servePath }; +}; + +/** + * @param {object} modules + * @param {typeof import('net')} modules.net + * @param {typeof import('http')} modules.http + * @param {typeof import('ws')} modules.ws + * @returns {import('./types.js').NetworkPowers} + */ +export const makeNetworkPowers = ({ http, ws, net }) => { + const { servePortHttp } = makeHttpPowers({ http, ws }); + const { servePort, servePath } = makeSocketPowers({ net }); + const connectionNumbers = (function* generateNumbers() { let n = 0; for (;;) { diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index 38ffb872ff..21ea1f2acc 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -321,7 +321,7 @@ export type PetStorePowers = { ) => Promise>; }; -export type NetworkPowers = { +export type SocketPowers = { servePath: (args: { path: string; host?: string; @@ -332,6 +332,9 @@ export type NetworkPowers = { host?: string; cancelled: Promise; }) => Promise>; +}; + +export type NetworkPowers = SocketPowers & { servePortHttp: (args: { port: number; host?: string; From d0a9168f79595e1fb9ef05566003acf5d994f589 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Mon, 1 Jan 2024 22:30:15 -0800 Subject: [PATCH 2/4] feat(daemon): Node socket server exposes assigned port --- packages/daemon/src/daemon-node-powers.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/daemon/src/daemon-node-powers.js b/packages/daemon/src/daemon-node-powers.js index 327a368ec2..28514dad09 100644 --- a/packages/daemon/src/daemon-node-powers.js +++ b/packages/daemon/src/daemon-node-powers.js @@ -215,7 +215,12 @@ export const makeSocketPowers = ({ net }) => { void writeTo.next({ reader, writer, closed }); }); - return readFrom; + const port = await listening; + + return harden({ + port, + connections: readFrom, + }); }; /** @@ -228,7 +233,7 @@ export const makeSocketPowers = ({ net }) => { serveListener( server => new Promise(resolve => - server.listen(port, host, () => resolve(undefined)), + server.listen(port, host, () => resolve(server.address().port)), ), cancelled, ); @@ -238,8 +243,8 @@ export const makeSocketPowers = ({ net }) => { * @param {string} args.path * @param {Promise} args.cancelled */ - const servePath = async ({ path, cancelled }) => - serveListener(server => { + const servePath = async ({ path, cancelled }) => { + const { connections } = await serveListener(server => { return new Promise((resolve, reject) => server.listen({ path }, error => { if (error) { @@ -255,6 +260,8 @@ export const makeSocketPowers = ({ net }) => { }), ); }, cancelled); + return connections; + }; return { servePort, servePath }; }; From 82c45af49fabcf484aa3837e3a24bcb05b0e9806 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Mon, 1 Jan 2024 22:30:43 -0800 Subject: [PATCH 3/4] feat(daemon): Add TCP connect to Node powers --- packages/daemon/src/daemon-node-powers.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/daemon/src/daemon-node-powers.js b/packages/daemon/src/daemon-node-powers.js index 28514dad09..e9e44dadf2 100644 --- a/packages/daemon/src/daemon-node-powers.js +++ b/packages/daemon/src/daemon-node-powers.js @@ -238,6 +238,24 @@ export const makeSocketPowers = ({ net }) => { cancelled, ); + const connectPort = ({ port, host, cancelled }) => + new Promise((resolve, reject) => { + const conn = net.connect(port, host, err => { + if (err) { + reject(err); + return; + } + const reader = makeNodeReader(conn); + const writer = makeNodeWriter(conn); + const closed = new Promise(close => conn.on('close', close)); + resolve({ + reader, + writer, + closed, + }); + }); + }); + /** * @param {object} args * @param {string} args.path @@ -263,7 +281,7 @@ export const makeSocketPowers = ({ net }) => { return connections; }; - return { servePort, servePath }; + return { servePort, servePath, connectPort }; }; /** @@ -275,7 +293,7 @@ export const makeSocketPowers = ({ net }) => { */ export const makeNetworkPowers = ({ http, ws, net }) => { const { servePortHttp } = makeHttpPowers({ http, ws }); - const { servePort, servePath } = makeSocketPowers({ net }); + const { servePort, servePath, connectPort } = makeSocketPowers({ net }); const connectionNumbers = (function* generateNumbers() { let n = 0; @@ -338,6 +356,7 @@ export const makeNetworkPowers = ({ http, ws, net }) => { servePortHttp, servePort, servePath, + connectPort, makePrivatePathService, makePrivateHttpService, }); From cb844215679dcdbd7f55c9b7d84f19f71dc5279a Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 13 Feb 2024 00:06:26 -1000 Subject: [PATCH 4/4] chore(daemon): improve types for SocketPowers --- packages/daemon/src/daemon-node-powers.js | 14 +++----------- packages/daemon/src/types.d.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/daemon/src/daemon-node-powers.js b/packages/daemon/src/daemon-node-powers.js index e9e44dadf2..b6af7e6fff 100644 --- a/packages/daemon/src/daemon-node-powers.js +++ b/packages/daemon/src/daemon-node-powers.js @@ -223,12 +223,7 @@ export const makeSocketPowers = ({ net }) => { }); }; - /** - * @param {object} args - * @param {number} args.port - * @param {string} [args.host] - * @param {Promise} args.cancelled - */ + /** @type {import('./types.js').SocketPowers['servePort']} */ const servePort = async ({ port, host = '0.0.0.0', cancelled }) => serveListener( server => @@ -238,6 +233,7 @@ export const makeSocketPowers = ({ net }) => { cancelled, ); + /** @type {import('./types.js').SocketPowers['connectPort']} */ const connectPort = ({ port, host, cancelled }) => new Promise((resolve, reject) => { const conn = net.connect(port, host, err => { @@ -256,11 +252,7 @@ export const makeSocketPowers = ({ net }) => { }); }); - /** - * @param {object} args - * @param {string} args.path - * @param {Promise} args.cancelled - */ + /** @type {import('./types.js').SocketPowers['servePath']} */ const servePath = async ({ path, cancelled }) => { const { connections } = await serveListener(server => { return new Promise((resolve, reject) => diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index 21ea1f2acc..6c3b3ba83b 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -322,15 +322,22 @@ export type PetStorePowers = { }; export type SocketPowers = { - servePath: (args: { - path: string; + servePort: (args: { + port: number; host?: string; cancelled: Promise; - }) => Promise>; - servePort: (args: { + }) => Promise<{ + port: number; + connections: Reader; + }>; + connectPort: (args: { port: number; host?: string; cancelled: Promise; + }) => Promise; + servePath: (args: { + path: string; + cancelled: Promise; }) => Promise>; };