@fission-codes/homestar
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -8,11 +8,14 @@ /** | ||
* @typedef {{subscription: string, result: number[]}} SubscriptionNotification | ||
* @typedef {import('./types.js').HomestarEvents} HomestarEvents | ||
* @typedef {import('./types.js').HomestarOptions} HomestarOptions | ||
*/ | ||
/** | ||
* @class Homestar | ||
* @extends {Emittery<HomestarEvents>} | ||
*/ | ||
export class Homestar { | ||
export class Homestar extends Emittery<import("./types.js").HomestarEvents, import("./types.js").HomestarEvents & import("emittery").OmnipresentEventData, never> { | ||
/** | ||
* @param {import("./types.js").HomestarOptions} opts | ||
* @param {HomestarOptions} opts | ||
*/ | ||
constructor(opts: import("./types.js").HomestarOptions); | ||
constructor(opts: HomestarOptions); | ||
opts: import("./types.js").HomestarOptions; | ||
@@ -37,8 +40,7 @@ /** | ||
* | ||
* @param {any} workflow | ||
* @param {Schemas.Workflow} workflow | ||
* @param {(data: MaybeResult<Schemas.WorkflowNotification, Schemas.WorkflowNotificationError>)=>void} [receiptCb] - Callback for workflow notifications | ||
*/ | ||
runWorkflow(workflow: any, receiptCb?: ((data: MaybeResult<Schemas.WorkflowNotification, Schemas.WorkflowNotificationError>) => void) | undefined): Promise<{ | ||
runWorkflow(workflow: Schemas.Workflow, receiptCb?: ((data: MaybeResult<Schemas.WorkflowNotification, Schemas.WorkflowNotificationError>) => void) | undefined): Promise<{ | ||
result: string; | ||
unsubscribe: import("emittery").UnsubscribeFunction | undefined; | ||
error?: undefined; | ||
@@ -48,4 +50,7 @@ } | { | ||
result?: undefined; | ||
unsubscribe?: undefined; | ||
}>; | ||
/** | ||
* Close homestar channel and clean listeners | ||
*/ | ||
close(): void; | ||
#private; | ||
@@ -58,3 +63,6 @@ } | ||
}; | ||
export type HomestarEvents = import('./types.js').HomestarEvents; | ||
export type HomestarOptions = import('./types.js').HomestarOptions; | ||
import Emittery from 'emittery'; | ||
import * as Schemas from './schemas.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -11,2 +11,5 @@ import type { ZodTypeAny } from 'zod'; | ||
} | ||
export interface HomestarEvents { | ||
error: Error; | ||
} | ||
export type Result<Out> = ['ok' | 'error', Out]; | ||
@@ -13,0 +16,0 @@ export interface TemplateWorkflow<Args extends any[] = any[]> extends Workflow { |
{ | ||
"name": "@fission-codes/homestar", | ||
"type": "module", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Homestart Client.", | ||
@@ -58,3 +58,3 @@ "author": "Hugo Dias <hugomrdias@gmail.com> (hugodias.me)", | ||
"emittery": "^1.0.1", | ||
"iso-websocket": "^0.1.5", | ||
"iso-websocket": "^0.1.6", | ||
"multiformats": "^12.1.3", | ||
@@ -65,6 +65,8 @@ "object-path": "^0.11.8", | ||
"devDependencies": { | ||
"@bytecodealliance/componentize-js": "^0.4.1", | ||
"@types/get-value": "^3.0.5", | ||
"@types/json-templates": "^3.0.3", | ||
"@types/node": "^20.8.9", | ||
"@types/node": "^20.9.0", | ||
"@types/object-path": "^0.11.4", | ||
"iso-base": "^2.0.1", | ||
"p-defer": "^4.0.0", | ||
@@ -71,0 +73,0 @@ "playwright-test": "^12.4.3", |
@@ -34,7 +34,14 @@ # Homestar | ||
} | ||
hs.close() | ||
``` | ||
## Docs | ||
Check <https://fission-codes.github.io/stack> | ||
## TODO | ||
- count workflow tasks and match receipts then unsub with unsubscribe_run_workflow and client events | ||
- try base64 encoding the payload | ||
@@ -44,7 +51,2 @@ ```rust | ||
pub(crate) const HEALTH_ENDPOINT: &str = "health"; | ||
/// Metrics endpoint for prometheus / openmetrics polling. | ||
pub(crate) const METRICS_ENDPOINT: &str = "metrics"; | ||
/// Run a workflow and subscribe to that workflow's events. | ||
#[cfg(feature = "websocket-notify")] | ||
pub(crate) const SUBSCRIBE_RUN_WORKFLOW_ENDPOINT: &str = "subscribe_run_workflow"; | ||
/// Unsubscribe from a workflow's events. | ||
@@ -51,0 +53,0 @@ #[cfg(feature = "websocket-notify")] |
@@ -33,2 +33,3 @@ /* eslint-disable unicorn/no-null */ | ||
this.opts.transport.on('response', this.#handleResponse) | ||
this.opts.transport.on('error', this.#handleError) | ||
} | ||
@@ -56,3 +57,8 @@ | ||
#handleError = (/** @type {Error} */ error) => { | ||
this.emit('error', new Error('Transport Error', { cause: error })) | ||
} | ||
/** | ||
* Handle response from transport | ||
* | ||
@@ -69,3 +75,3 @@ * @param {string | ArrayBuffer} data | ||
if (id === undefined && decoded.error) { | ||
this.emit('error', new Error('Codec Error', { cause: decoded })) | ||
this.emit('error', new Error('Codec Error', { cause: decoded.error })) | ||
} | ||
@@ -96,5 +102,5 @@ | ||
this.#queue.clear() | ||
this.opts.transport.off('response', this.#handleResponse) | ||
this.opts.transport.close() | ||
this.clearListeners() | ||
} | ||
} |
@@ -54,2 +54,3 @@ import Emittery from 'emittery' | ||
this.#ws.close() | ||
this.clearListeners() | ||
} | ||
@@ -56,0 +57,0 @@ |
import { decode } from '@ipld/dag-json' | ||
import Emittery from 'emittery' | ||
import { Channel } from './channel/channel.js' | ||
@@ -14,2 +15,4 @@ import { JsonRpcCodec } from './channel/codecs/jsonrpc.js' | ||
* @typedef {{subscription: string, result: number[]}} SubscriptionNotification | ||
* @typedef {import('./types.js').HomestarEvents} HomestarEvents | ||
* @typedef {import('./types.js').HomestarOptions} HomestarOptions | ||
*/ | ||
@@ -19,10 +22,12 @@ | ||
* @class Homestar | ||
* @extends {Emittery<HomestarEvents>} | ||
*/ | ||
export class Homestar { | ||
export class Homestar extends Emittery { | ||
/** @type {import('./channel/types.js').IChannel<import('./channel/codecs/types.js').IJsonRpcCodec>} */ | ||
#channel | ||
/** | ||
* @param {import("./types.js").HomestarOptions} opts | ||
* @param {HomestarOptions} opts | ||
*/ | ||
constructor(opts) { | ||
super() | ||
this.opts = opts | ||
@@ -34,2 +39,6 @@ | ||
}) | ||
this.#channel.on('error', (error) => { | ||
this.emit('error', error) | ||
}) | ||
} | ||
@@ -61,3 +70,3 @@ | ||
* | ||
* @param {any} workflow | ||
* @param {Schemas.Workflow} workflow | ||
* @param {(data: MaybeResult<Schemas.WorkflowNotification, Schemas.WorkflowNotificationError>)=>void} [receiptCb] - Callback for workflow notifications | ||
@@ -69,2 +78,3 @@ */ | ||
method: 'subscribe_run_workflow', | ||
// @ts-ignore | ||
params: [workflow], | ||
@@ -75,3 +85,6 @@ }) | ||
if (res.result !== undefined) { | ||
const tasksCount = workflow.workflow.tasks.length | ||
let receiptCount = 0 | ||
const subId = res.result | ||
/** @type {import('emittery').UnsubscribeFunction} */ | ||
let unsub | ||
@@ -85,2 +98,3 @@ | ||
if (data.subscription === subId) { | ||
receiptCount++ | ||
const decoded = decode(new Uint8Array(data.result)) | ||
@@ -93,2 +107,21 @@ const r = Schemas.WorkflowNotification.safeParse(decoded) | ||
} | ||
if (tasksCount === receiptCount) { | ||
unsub() | ||
this.#channel | ||
.request({ | ||
method: 'unsubscribe_run_workflow', | ||
params: [subId], | ||
}) | ||
.then( | ||
(res) => { | ||
if (res.error) { | ||
return this.emit('error', res.error) | ||
} | ||
}, | ||
(error) => { | ||
this.emit('error', error) | ||
} | ||
) | ||
} | ||
} | ||
@@ -100,3 +133,2 @@ } | ||
result: subId, | ||
unsubscribe: unsub, | ||
} | ||
@@ -107,2 +139,10 @@ } | ||
} | ||
/** | ||
* Close homestar channel and clean listeners | ||
*/ | ||
close() { | ||
this.#channel.close() | ||
this.clearListeners() | ||
} | ||
} |
@@ -15,2 +15,6 @@ import type { ZodTypeAny } from 'zod' | ||
export interface HomestarEvents { | ||
error: Error | ||
} | ||
export type Result<Out> = ['ok' | 'error', Out] | ||
@@ -17,0 +21,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69372
1789
87
11
Updatediso-websocket@^0.1.6