Skip to content

Commit

Permalink
feat(daemon): Add TCP net layer (merge #2060)
Browse files Browse the repository at this point in the history
- breaks out `SocketPowers` from `NetworkPowers`
- adds `connectPort` for tcp net layer to be utilized later
  • Loading branch information
rekmarks authored Feb 14, 2024
2 parents e0de1ff + cb84421 commit 7c303a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
70 changes: 49 additions & 21 deletions packages/daemon/src/daemon-node-powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<import('./types.js').Connection>} */ readFrom,
Expand Down Expand Up @@ -219,31 +215,46 @@ export const makeNetworkPowers = ({ http, ws, net }) => {
void writeTo.next({ reader, writer, closed });
});

return readFrom;
const port = await listening;

return harden({
port,
connections: readFrom,
});
};

/**
* @param {object} args
* @param {number} args.port
* @param {string} [args.host]
* @param {Promise<never>} args.cancelled
*/
/** @type {import('./types.js').SocketPowers['servePort']} */
const servePort = async ({ port, host = '0.0.0.0', cancelled }) =>
serveListener(
server =>
new Promise(resolve =>
server.listen(port, host, () => resolve(undefined)),
server.listen(port, host, () => resolve(server.address().port)),
),
cancelled,
);

/**
* @param {object} args
* @param {string} args.path
* @param {Promise<never>} args.cancelled
*/
const servePath = async ({ path, cancelled }) =>
serveListener(server => {
/** @type {import('./types.js').SocketPowers['connectPort']} */
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,
});
});
});

/** @type {import('./types.js').SocketPowers['servePath']} */
const servePath = async ({ path, cancelled }) => {
const { connections } = await serveListener(server => {
return new Promise((resolve, reject) =>
server.listen({ path }, error => {
if (error) {
Expand All @@ -259,6 +270,22 @@ export const makeNetworkPowers = ({ http, ws, net }) => {
}),
);
}, cancelled);
return connections;
};

return { servePort, servePath, connectPort };
};

/**
* @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, connectPort } = makeSocketPowers({ net });

const connectionNumbers = (function* generateNumbers() {
let n = 0;
Expand Down Expand Up @@ -321,6 +348,7 @@ export const makeNetworkPowers = ({ http, ws, net }) => {
servePortHttp,
servePort,
servePath,
connectPort,
makePrivatePathService,
makePrivateHttpService,
});
Expand Down
20 changes: 15 additions & 5 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,27 @@ export type PetStorePowers = {
) => Promise<FarRef<PetStore>>;
};

export type NetworkPowers = {
servePath: (args: {
path: string;
export type SocketPowers = {
servePort: (args: {
port: number;
host?: string;
cancelled: Promise<never>;
}) => Promise<AsyncIterableIterator<Connection>>;
servePort: (args: {
}) => Promise<{
port: number;
connections: Reader<Connection>;
}>;
connectPort: (args: {
port: number;
host?: string;
cancelled: Promise<never>;
}) => Promise<Connection>;
servePath: (args: {
path: string;
cancelled: Promise<never>;
}) => Promise<AsyncIterableIterator<Connection>>;
};

export type NetworkPowers = SocketPowers & {
servePortHttp: (args: {
port: number;
host?: string;
Expand Down

0 comments on commit 7c303a1

Please sign in to comment.