Skip to content

Commit

Permalink
refactor(daemon): Extract SocketPowers
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored and kumavis committed Feb 13, 2024
1 parent c3bc771 commit 5ce98ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 16 additions & 6 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 @@ -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 (;;) {
Expand Down
5 changes: 4 additions & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export type PetStorePowers = {
) => Promise<FarRef<PetStore>>;
};

export type NetworkPowers = {
export type SocketPowers = {
servePath: (args: {
path: string;
host?: string;
Expand All @@ -332,6 +332,9 @@ export type NetworkPowers = {
host?: string;
cancelled: Promise<never>;
}) => Promise<AsyncIterableIterator<Connection>>;
};

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

0 comments on commit 5ce98ee

Please sign in to comment.