iii-browser-sdk 0.17.0-next.1 → 0.18.0-next.1

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.
@@ -0,0 +1,30 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_utils = require('./utils-Cq61lImK.cjs');
3
+
4
+ //#region src/helpers.ts
5
+ /**
6
+ * Create a streaming channel pair for worker-to-worker data transfer.
7
+ *
8
+ * Free-function form of the previous `ISdk.createChannel` instance method.
9
+ */
10
+ function createChannel(iii, bufferSize) {
11
+ return iii.__helpers_create_channel(bufferSize);
12
+ }
13
+ /**
14
+ * Register a custom stream implementation by wiring its 5 callable methods
15
+ * to `stream::get/set/delete/list/list_groups`.
16
+ *
17
+ * Free-function form of the previous `ISdk.createStream` instance method.
18
+ */
19
+ function createStream(iii, streamName, stream) {
20
+ iii.__helpers_create_stream(streamName, stream);
21
+ }
22
+
23
+ //#endregion
24
+ exports.ChannelDirection = require_utils.ChannelDirection;
25
+ exports.ChannelItem = require_utils.ChannelItem;
26
+ exports.createChannel = createChannel;
27
+ exports.createStream = createStream;
28
+ exports.extractChannelRefs = require_utils.extractChannelRefs;
29
+ exports.isChannelRef = require_utils.isChannelRef;
30
+ //# sourceMappingURL=helpers.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.cjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["/**\n * Helper free functions that operate on an {@link ISdk} instance.\n *\n * These were previously instance methods on the SDK. They take the iii\n * instance as the first argument so the public API surface of `ISdk` stays\n * focused on the core lifecycle and registration methods.\n */\nimport type { Channel, ISdk } from './types'\nimport type { IStream } from './stream'\n\nexport { ChannelDirection, ChannelItem } from './channels'\nexport { extractChannelRefs, isChannelRef } from './utils'\n\ntype IIIWithHelperShims = ISdk & {\n __helpers_create_channel(bufferSize?: number): Promise<Channel>\n __helpers_create_stream<T>(name: string, stream: IStream<T>): void\n}\n\n/**\n * Create a streaming channel pair for worker-to-worker data transfer.\n *\n * Free-function form of the previous `ISdk.createChannel` instance method.\n */\nexport function createChannel(iii: ISdk, bufferSize?: number): Promise<Channel> {\n return (iii as IIIWithHelperShims).__helpers_create_channel(bufferSize)\n}\n\n/**\n * Register a custom stream implementation by wiring its 5 callable methods\n * to `stream::get/set/delete/list/list_groups`.\n *\n * Free-function form of the previous `ISdk.createStream` instance method.\n */\nexport function createStream<TData>(iii: ISdk, streamName: string, stream: IStream<TData>): void {\n ;(iii as IIIWithHelperShims).__helpers_create_stream(streamName, stream)\n}\n"],"mappings":";;;;;;;;;AAuBA,SAAgB,cAAc,KAAW,YAAuC;AAC9E,QAAQ,IAA2B,yBAAyB,WAAW;;;;;;;;AASzE,SAAgB,aAAoB,KAAW,YAAoB,QAA8B;AAC9F,CAAC,IAA2B,wBAAwB,YAAY,OAAO"}
@@ -0,0 +1,38 @@
1
+ import { L as StreamChannelRef, a as ISdk, b as ChannelItem, r as Channel, y as ChannelDirection } from "./types-CP-lPyex.cjs";
2
+ import { IStream } from "./stream.cjs";
3
+
4
+ //#region src/utils.d.ts
5
+ /**
6
+ * Type guard that checks if a value is a {@link StreamChannelRef}.
7
+ *
8
+ * @param value - Value to check.
9
+ * @returns `true` if the value is a valid `StreamChannelRef`.
10
+ */
11
+ declare const isChannelRef: (value: unknown) => value is StreamChannelRef;
12
+ /**
13
+ * Recursively extract all {@link StreamChannelRef} values from a JSON-like
14
+ * input, returning each match paired with its dotted/bracketed path. Mirrors
15
+ * the Rust SDK's `extract_channel_refs`.
16
+ *
17
+ * @param data - Arbitrary JSON-like value.
18
+ * @returns Array of `[path, ref]` tuples. Empty when no refs are found.
19
+ */
20
+ declare const extractChannelRefs: (data: unknown) => Array<[string, StreamChannelRef]>;
21
+ //#endregion
22
+ //#region src/helpers.d.ts
23
+ /**
24
+ * Create a streaming channel pair for worker-to-worker data transfer.
25
+ *
26
+ * Free-function form of the previous `ISdk.createChannel` instance method.
27
+ */
28
+ declare function createChannel(iii: ISdk, bufferSize?: number): Promise<Channel>;
29
+ /**
30
+ * Register a custom stream implementation by wiring its 5 callable methods
31
+ * to `stream::get/set/delete/list/list_groups`.
32
+ *
33
+ * Free-function form of the previous `ISdk.createStream` instance method.
34
+ */
35
+ declare function createStream<TData>(iii: ISdk, streamName: string, stream: IStream<TData>): void;
36
+ //#endregion
37
+ export { ChannelDirection, ChannelItem, createChannel, createStream, extractChannelRefs, isChannelRef };
38
+ //# sourceMappingURL=helpers.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/utils.ts","../src/helpers.ts"],"mappings":";;;;;;;;;;cAmCa,YAAA,GAAgB,KAAA,cAAiB,KAAA,IAAS,gBAAA;AAkBvD;;;;;;;;AAAA,cAAa,kBAAA,GAAsB,IAAA,cAAgB,KAAA,UAAe,gBAAA;;;;;AAAlE;;;iBC9BgB,aAAA,CAAc,GAAA,EAAK,IAAA,EAAM,UAAA,YAAsB,OAAA,CAAQ,OAAA;;;;;;;iBAUvD,YAAA,OAAA,CAAoB,GAAA,EAAK,IAAA,EAAM,UAAA,UAAoB,MAAA,EAAQ,OAAA,CAAQ,KAAA"}
@@ -0,0 +1,38 @@
1
+ import { L as StreamChannelRef, a as ISdk, b as ChannelItem, r as Channel, y as ChannelDirection } from "./types-Bw88g_Jj.mjs";
2
+ import { IStream } from "./stream.mjs";
3
+
4
+ //#region src/utils.d.ts
5
+ /**
6
+ * Type guard that checks if a value is a {@link StreamChannelRef}.
7
+ *
8
+ * @param value - Value to check.
9
+ * @returns `true` if the value is a valid `StreamChannelRef`.
10
+ */
11
+ declare const isChannelRef: (value: unknown) => value is StreamChannelRef;
12
+ /**
13
+ * Recursively extract all {@link StreamChannelRef} values from a JSON-like
14
+ * input, returning each match paired with its dotted/bracketed path. Mirrors
15
+ * the Rust SDK's `extract_channel_refs`.
16
+ *
17
+ * @param data - Arbitrary JSON-like value.
18
+ * @returns Array of `[path, ref]` tuples. Empty when no refs are found.
19
+ */
20
+ declare const extractChannelRefs: (data: unknown) => Array<[string, StreamChannelRef]>;
21
+ //#endregion
22
+ //#region src/helpers.d.ts
23
+ /**
24
+ * Create a streaming channel pair for worker-to-worker data transfer.
25
+ *
26
+ * Free-function form of the previous `ISdk.createChannel` instance method.
27
+ */
28
+ declare function createChannel(iii: ISdk, bufferSize?: number): Promise<Channel>;
29
+ /**
30
+ * Register a custom stream implementation by wiring its 5 callable methods
31
+ * to `stream::get/set/delete/list/list_groups`.
32
+ *
33
+ * Free-function form of the previous `ISdk.createStream` instance method.
34
+ */
35
+ declare function createStream<TData>(iii: ISdk, streamName: string, stream: IStream<TData>): void;
36
+ //#endregion
37
+ export { ChannelDirection, ChannelItem, createChannel, createStream, extractChannelRefs, isChannelRef };
38
+ //# sourceMappingURL=helpers.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/utils.ts","../src/helpers.ts"],"mappings":";;;;;;;;;;cAmCa,YAAA,GAAgB,KAAA,cAAiB,KAAA,IAAS,gBAAA;AAkBvD;;;;;;;;AAAA,cAAa,kBAAA,GAAsB,IAAA,cAAgB,KAAA,UAAe,gBAAA;;;;;AAAlE;;;iBC9BgB,aAAA,CAAc,GAAA,EAAK,IAAA,EAAM,UAAA,YAAsB,OAAA,CAAQ,OAAA;;;;;;;iBAUvD,YAAA,OAAA,CAAoB,GAAA,EAAK,IAAA,EAAM,UAAA,UAAoB,MAAA,EAAQ,OAAA,CAAQ,KAAA"}
@@ -0,0 +1,24 @@
1
+ import { i as ChannelItem, n as isChannelRef, r as ChannelDirection, t as extractChannelRefs } from "./utils-CwkqVmBx.mjs";
2
+
3
+ //#region src/helpers.ts
4
+ /**
5
+ * Create a streaming channel pair for worker-to-worker data transfer.
6
+ *
7
+ * Free-function form of the previous `ISdk.createChannel` instance method.
8
+ */
9
+ function createChannel(iii, bufferSize) {
10
+ return iii.__helpers_create_channel(bufferSize);
11
+ }
12
+ /**
13
+ * Register a custom stream implementation by wiring its 5 callable methods
14
+ * to `stream::get/set/delete/list/list_groups`.
15
+ *
16
+ * Free-function form of the previous `ISdk.createStream` instance method.
17
+ */
18
+ function createStream(iii, streamName, stream) {
19
+ iii.__helpers_create_stream(streamName, stream);
20
+ }
21
+
22
+ //#endregion
23
+ export { ChannelDirection, ChannelItem, createChannel, createStream, extractChannelRefs, isChannelRef };
24
+ //# sourceMappingURL=helpers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.mjs","names":[],"sources":["../src/helpers.ts"],"sourcesContent":["/**\n * Helper free functions that operate on an {@link ISdk} instance.\n *\n * These were previously instance methods on the SDK. They take the iii\n * instance as the first argument so the public API surface of `ISdk` stays\n * focused on the core lifecycle and registration methods.\n */\nimport type { Channel, ISdk } from './types'\nimport type { IStream } from './stream'\n\nexport { ChannelDirection, ChannelItem } from './channels'\nexport { extractChannelRefs, isChannelRef } from './utils'\n\ntype IIIWithHelperShims = ISdk & {\n __helpers_create_channel(bufferSize?: number): Promise<Channel>\n __helpers_create_stream<T>(name: string, stream: IStream<T>): void\n}\n\n/**\n * Create a streaming channel pair for worker-to-worker data transfer.\n *\n * Free-function form of the previous `ISdk.createChannel` instance method.\n */\nexport function createChannel(iii: ISdk, bufferSize?: number): Promise<Channel> {\n return (iii as IIIWithHelperShims).__helpers_create_channel(bufferSize)\n}\n\n/**\n * Register a custom stream implementation by wiring its 5 callable methods\n * to `stream::get/set/delete/list/list_groups`.\n *\n * Free-function form of the previous `ISdk.createStream` instance method.\n */\nexport function createStream<TData>(iii: ISdk, streamName: string, stream: IStream<TData>): void {\n ;(iii as IIIWithHelperShims).__helpers_create_stream(streamName, stream)\n}\n"],"mappings":";;;;;;;;AAuBA,SAAgB,cAAc,KAAW,YAAuC;AAC9E,QAAQ,IAA2B,yBAAyB,WAAW;;;;;;;;AASzE,SAAgB,aAAoB,KAAW,YAAoB,QAA8B;AAC9F,CAAC,IAA2B,wBAAwB,YAAY,OAAO"}
package/dist/index.cjs CHANGED
@@ -1,167 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_utils = require('./utils-Cq61lImK.cjs');
2
3
 
3
- //#region src/channels.ts
4
- /**
5
- * Write end of a streaming channel. Uses native browser WebSocket.
6
- *
7
- * @example
8
- * ```typescript
9
- * const channel = await iii.createChannel()
10
- *
11
- * channel.writer.sendMessage(JSON.stringify({ type: 'event', data: 'test' }))
12
- * channel.writer.sendBinary(new Uint8Array([1, 2, 3]))
13
- * channel.writer.close()
14
- * ```
15
- */
16
- var ChannelWriter = class ChannelWriter {
17
- static {
18
- this.FRAME_SIZE = 64 * 1024;
19
- }
20
- constructor(engineWsBase, ref) {
21
- this.ws = null;
22
- this.wsReady = false;
23
- this.pendingMessages = [];
24
- this.url = buildChannelUrl(engineWsBase, ref.channel_id, ref.access_key, "write");
25
- }
26
- ensureConnected() {
27
- if (this.ws) return;
28
- this.ws = new WebSocket(this.url);
29
- this.ws.binaryType = "arraybuffer";
30
- this.ws.addEventListener("open", () => {
31
- this.wsReady = true;
32
- for (const { data, resolve, reject } of this.pendingMessages) try {
33
- this.ws?.send(data);
34
- resolve();
35
- } catch (err) {
36
- reject(err instanceof Error ? err : new Error(String(err)));
37
- }
38
- this.pendingMessages.length = 0;
39
- });
40
- this.ws.addEventListener("error", () => {
41
- for (const { reject } of this.pendingMessages) reject(/* @__PURE__ */ new Error("WebSocket error"));
42
- this.pendingMessages.length = 0;
43
- });
44
- }
45
- /** Send a text message through the channel. */
46
- sendMessage(msg) {
47
- this.ensureConnected();
48
- this.sendRaw(msg);
49
- }
50
- /** Send binary data through the channel. */
51
- sendBinary(data) {
52
- this.ensureConnected();
53
- let offset = 0;
54
- while (offset < data.length) {
55
- const end = Math.min(offset + ChannelWriter.FRAME_SIZE, data.length);
56
- const chunk = data.subarray(offset, end);
57
- const buffer = chunk.buffer instanceof ArrayBuffer ? chunk.buffer : new ArrayBuffer(chunk.byteLength);
58
- if (!(chunk.buffer instanceof ArrayBuffer)) new Uint8Array(buffer).set(chunk);
59
- this.sendRaw(buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength));
60
- offset = end;
61
- }
62
- }
63
- /** Close the channel writer. */
64
- close() {
65
- if (!this.ws) return;
66
- const doClose = () => {
67
- if (this.ws && this.ws.readyState === WebSocket.OPEN) this.ws.close(1e3, "channel_close");
68
- };
69
- if (this.wsReady) doClose();
70
- else this.ws.addEventListener("open", () => doClose());
71
- }
72
- sendRaw(data) {
73
- if (this.wsReady && this.ws && this.ws.readyState === WebSocket.OPEN) this.ws.send(data);
74
- else {
75
- this.ensureConnected();
76
- this.pendingMessages.push({
77
- data,
78
- resolve: () => {},
79
- reject: () => {
80
- console.error("Failed to send message");
81
- }
82
- });
83
- }
84
- }
85
- };
86
- /**
87
- * Read end of a streaming channel. Uses native browser WebSocket.
88
- *
89
- * @example
90
- * ```typescript
91
- * const channel = await iii.createChannel()
92
- *
93
- * channel.reader.onMessage((msg) => console.log('Got:', msg))
94
- * channel.reader.onBinary((data) => console.log('Binary:', data.byteLength))
95
- * ```
96
- */
97
- var ChannelReader = class {
98
- constructor(engineWsBase, ref) {
99
- this.ws = null;
100
- this.connected = false;
101
- this.messageCallbacks = [];
102
- this.binaryCallbacks = [];
103
- this.url = buildChannelUrl(engineWsBase, ref.channel_id, ref.access_key, "read");
104
- }
105
- ensureConnected() {
106
- if (this.connected) return;
107
- this.connected = true;
108
- this.ws = new WebSocket(this.url);
109
- this.ws.binaryType = "arraybuffer";
110
- this.ws.addEventListener("message", (event) => {
111
- if (event.data instanceof ArrayBuffer) {
112
- const data = new Uint8Array(event.data);
113
- for (const cb of this.binaryCallbacks) cb(data);
114
- } else if (typeof event.data === "string") for (const cb of this.messageCallbacks) cb(event.data);
115
- });
116
- this.ws.addEventListener("close", () => {
117
- this.ws = null;
118
- });
119
- this.ws.addEventListener("error", () => {
120
- this.ws = null;
121
- });
122
- }
123
- /** Register a callback to receive text messages from the channel. */
124
- onMessage(callback) {
125
- this.messageCallbacks.push(callback);
126
- this.ensureConnected();
127
- }
128
- /** Register a callback to receive binary data from the channel. */
129
- onBinary(callback) {
130
- this.binaryCallbacks.push(callback);
131
- this.ensureConnected();
132
- }
133
- /** Read all binary data from the channel until it closes. */
134
- async readAll() {
135
- this.ensureConnected();
136
- const chunks = [];
137
- return new Promise((resolve) => {
138
- const onData = (data) => {
139
- chunks.push(data);
140
- };
141
- this.binaryCallbacks.push(onData);
142
- const originalWs = this.ws;
143
- if (originalWs) originalWs.addEventListener("close", () => {
144
- const totalLength = chunks.reduce((sum, c) => sum + c.length, 0);
145
- const result = new Uint8Array(totalLength);
146
- let offset = 0;
147
- for (const chunk of chunks) {
148
- result.set(chunk, offset);
149
- offset += chunk.length;
150
- }
151
- resolve(result);
152
- });
153
- });
154
- }
155
- /** Close the channel reader. */
156
- close() {
157
- if (this.ws && this.ws.readyState !== WebSocket.CLOSED) this.ws.close(1e3, "channel_close");
158
- }
159
- };
160
- function buildChannelUrl(engineWsBase, channelId, accessKey, direction) {
161
- return `${engineWsBase.replace(/\/$/, "")}/ws/channels/${channelId}?key=${encodeURIComponent(accessKey)}&dir=${direction}`;
162
- }
163
-
164
- //#endregion
165
4
  //#region src/iii-constants.ts
166
5
  /**
167
6
  * Constants for the III module.
@@ -215,20 +54,6 @@ let MessageType = /* @__PURE__ */ function(MessageType) {
215
54
  return MessageType;
216
55
  }({});
217
56
 
218
- //#endregion
219
- //#region src/utils.ts
220
- /**
221
- * Type guard that checks if a value is a {@link StreamChannelRef}.
222
- *
223
- * @param value - Value to check.
224
- * @returns `true` if the value is a valid `StreamChannelRef`.
225
- */
226
- const isChannelRef = (value) => {
227
- if (typeof value !== "object" || value === null) return false;
228
- const maybe = value;
229
- return typeof maybe.channel_id === "string" && typeof maybe.access_key === "string" && (maybe.direction === "read" || maybe.direction === "write");
230
- };
231
-
232
57
  //#endregion
233
58
  //#region src/iii.ts
234
59
  var Sdk = class {
@@ -322,14 +147,14 @@ var Sdk = class {
322
147
  }
323
148
  };
324
149
  };
325
- this.createChannel = async (bufferSize) => {
150
+ this.__helpers_create_channel = async (bufferSize) => {
326
151
  const result = await this.trigger({
327
152
  function_id: "engine::channels::create",
328
153
  payload: { buffer_size: bufferSize }
329
154
  });
330
155
  return {
331
- writer: new ChannelWriter(this.address, result.writer),
332
- reader: new ChannelReader(this.address, result.reader),
156
+ writer: new require_utils.ChannelWriter(this.address, result.writer),
157
+ reader: new require_utils.ChannelReader(this.address, result.reader),
333
158
  writerRef: result.writer,
334
159
  readerRef: result.reader
335
160
  };
@@ -372,7 +197,7 @@ var Sdk = class {
372
197
  });
373
198
  });
374
199
  };
375
- this.createStream = (streamName, stream) => {
200
+ this.__helpers_create_stream = (streamName, stream) => {
376
201
  this.registerFunction(`stream::get(${streamName})`, stream.get.bind(stream));
377
202
  this.registerFunction(`stream::set(${streamName})`, stream.set.bind(stream));
378
203
  this.registerFunction(`stream::delete(${streamName})`, stream.delete.bind(stream));
@@ -550,7 +375,7 @@ var Sdk = class {
550
375
  this.invocations.delete(invocation_id);
551
376
  }
552
377
  resolveChannelValue(value) {
553
- if (isChannelRef(value)) return value.direction === "read" ? new ChannelReader(this.address, value) : new ChannelWriter(this.address, value);
378
+ if (require_utils.isChannelRef(value)) return value.direction === "read" ? new require_utils.ChannelReader(this.address, value) : new require_utils.ChannelWriter(this.address, value);
554
379
  if (Array.isArray(value)) return value.map((item) => this.resolveChannelValue(item));
555
380
  if (value !== null && typeof value === "object") {
556
381
  const out = {};
@@ -715,8 +540,8 @@ const TriggerAction = {
715
540
  const registerWorker = (address, options) => new Sdk(address, options);
716
541
 
717
542
  //#endregion
718
- exports.ChannelReader = ChannelReader;
719
- exports.ChannelWriter = ChannelWriter;
543
+ exports.ChannelReader = require_utils.ChannelReader;
544
+ exports.ChannelWriter = require_utils.ChannelWriter;
720
545
  exports.EngineFunctions = EngineFunctions;
721
546
  exports.EngineTriggers = EngineTriggers;
722
547
  exports.TriggerAction = TriggerAction;