Skip to content

Commit

Permalink
feat: add node method to fetch node info
Browse files Browse the repository at this point in the history
feat: add node method to fetch node info
  • Loading branch information
avivash authored Mar 20, 2024
1 parent 00bedd2 commit adb1b5e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/homestar/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as T from './types.js'
* @typedef {T.HomestarEvents} HomestarEvents
* @typedef {T.HomestarOptions} HomestarOptions
* @typedef {T.Metrics} Metrics
* @typedef {T.Node} Node
* @typedef {T.Health} Health
* @typedef {T.WorkflowNotification} WorkflowNotification
* @typedef {T.EventNotification} EventNotification
Expand Down Expand Up @@ -91,6 +92,15 @@ export class Homestar extends Emittery {
})
}

/**
* Homestar Node info
*/
async node() {
return this.#channel.request({
method: 'node',
})
}

/**
* Homestar Health info
*/
Expand Down
10 changes: 6 additions & 4 deletions packages/homestar/src/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CID as _CID } from 'multiformats/cid'
/**
* @typedef {import('zod').z.infer<typeof Receipt>} Receipt
* @typedef {import('zod').z.infer<typeof Metrics>} Metrics
* @typedef {import('zod').z.infer<typeof Node>} Node
* @typedef {import('zod').z.infer<typeof Health>} Health
* @typedef {import('zod').z.infer<typeof Invocation>} Invocation
* @typedef {import('zod').z.infer<typeof Task>} Task
Expand All @@ -30,10 +31,11 @@ export const Metrics = z.object({

export const Health = z.object({
healthy: z.boolean(),
// nodeInfo: z.object({
// static: z.object({ peer_id: z.string() }),
// dynamic: z.object({ listeners: z.array(z.string()) }),
// }),
})

export const Node = z.object({
static: z.object({ peer_id: z.string() }),
dynamic: z.object({ listeners: z.array(z.string()) }),
})

export const CID = /** @type {typeof z.custom<import('multiformats').CID>} */ (
Expand Down
1 change: 1 addition & 0 deletions packages/homestar/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Receipt<Out> extends Schemas.Receipt {
export type HomestarService = Service<
[
IO<{ method: 'metrics' }, Schemas.Metrics>,
IO<{ method: 'node' }, Schemas.Node>,
IO<{ method: 'health' }, Schemas.Health>,
IO<{ method: 'subscribe_run_workflow'; params: Workflow[] }, string>,
IO<{ method: 'subscribe_network_events' }, string>,
Expand Down
28 changes: 25 additions & 3 deletions packages/homestar/test/homestar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ test(
}
)

test(
'should fetch node info from homestar',
async function () {
const hs = new Homestar({
transport: new WebsocketTransport(HS1_URL, {
ws: WebSocket,
}),
})

const { error, result } = await hs.node()

if (error) {
return assert.fail(error)
}

assert.ok(typeof result.static.peer_id === 'string')
assert.ok(Array.isArray(result.dynamic.listeners))
hs.close()
},
{
timeout: 30_000,
}
)

test(
'should fetch health from homestar',
async function () {
Expand All @@ -69,9 +93,7 @@ test(
}

assert.equal(result.healthy, true)
// assert.ok(result.nodeInfo)
// assert.ok(typeof result.nodeInfo.static.peer_id === 'string')
// assert.ok(Array.isArray(result.nodeInfo.dynamic.listeners))

hs.close()
},
{
Expand Down

0 comments on commit adb1b5e

Please # to comment.