@veilux/sdk 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -10,4 +10,5 @@ export * from "./encoding.js";
10
10
  export * from "./identity.js";
11
11
  export * from "./client.js";
12
12
  export * from "./ids.js";
13
+ export * from "./subscription.js";
13
14
  export * as builders from "./builders.js";
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export * from "./encoding.js";
10
10
  export * from "./identity.js";
11
11
  export * from "./client.js";
12
12
  export * from "./ids.js";
13
+ export * from "./subscription.js";
13
14
  export * as builders from "./builders.js";
@@ -0,0 +1,28 @@
1
+ import type { BlockNotification } from "./types.js";
2
+ export interface Subscription {
3
+ /** Close the underlying WebSocket and stop receiving notifications. */
4
+ close(): void;
5
+ }
6
+ export interface SubscribeHandlers {
7
+ /** Called for every committed block. */
8
+ onBlock?: (block: BlockNotification) => void;
9
+ /** Called once the subscription is established. */
10
+ onOpen?: () => void;
11
+ /** Called on socket error. */
12
+ onError?: (err: unknown) => void;
13
+ /** Called when the socket closes. */
14
+ onClose?: () => void;
15
+ }
16
+ /**
17
+ * Subscribe to real-time block notifications over WebSocket.
18
+ *
19
+ * Works in the browser and in Node.js 20+ (both expose a global `WebSocket`).
20
+ *
21
+ * ```ts
22
+ * const sub = subscribeBlocks("ws://127.0.0.1:8646", {
23
+ * onBlock: (b) => console.log("new block", b.height, b.hash),
24
+ * });
25
+ * // later: sub.close();
26
+ * ```
27
+ */
28
+ export declare function subscribeBlocks(wsUrl: string, handlers: SubscribeHandlers): Subscription;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Subscribe to real-time block notifications over WebSocket.
3
+ *
4
+ * Works in the browser and in Node.js 20+ (both expose a global `WebSocket`).
5
+ *
6
+ * ```ts
7
+ * const sub = subscribeBlocks("ws://127.0.0.1:8646", {
8
+ * onBlock: (b) => console.log("new block", b.height, b.hash),
9
+ * });
10
+ * // later: sub.close();
11
+ * ```
12
+ */
13
+ export function subscribeBlocks(wsUrl, handlers) {
14
+ const WS = typeof WebSocket !== "undefined" ? WebSocket : globalThis.WebSocket;
15
+ if (!WS) {
16
+ throw new Error("No global WebSocket available. On older Node, pass a polyfill via globalThis.WebSocket.");
17
+ }
18
+ const ws = new WS(wsUrl);
19
+ ws.onopen = () => handlers.onOpen?.();
20
+ ws.onerror = (e) => handlers.onError?.(e);
21
+ ws.onclose = () => handlers.onClose?.();
22
+ ws.onmessage = (ev) => {
23
+ let parsed;
24
+ try {
25
+ parsed = JSON.parse(typeof ev.data === "string" ? ev.data : String(ev.data));
26
+ }
27
+ catch {
28
+ return;
29
+ }
30
+ const msg = parsed;
31
+ if (msg.type === "block" && handlers.onBlock) {
32
+ handlers.onBlock(msg);
33
+ }
34
+ };
35
+ return {
36
+ close() {
37
+ ws.close();
38
+ },
39
+ };
40
+ }
package/dist/types.d.ts CHANGED
@@ -56,6 +56,16 @@ export interface StateResult {
56
56
  export interface EstimateResult {
57
57
  cost: number;
58
58
  }
59
+ /** Real-time notification pushed over WebSocket when a block is committed. */
60
+ export interface BlockNotification {
61
+ type: "block";
62
+ height: number;
63
+ hash: string;
64
+ state_root: string;
65
+ command_count: number;
66
+ event_count: number;
67
+ timestamp: number;
68
+ }
59
69
  export declare const RPC_METHODS: {
60
70
  readonly nodeInfo: "veilux_nodeInfo";
61
71
  readonly submit: "veilux_submit";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veilux/sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "VEILUX TypeScript SDK — build, sign, and submit commands to a VEILUX node",
5
5
  "license": "MIT",
6
6
  "author": "nathan <nathan@winnode.xyz>",