iii-sdk 0.17.0 → 0.18.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/helpers.cjs +30 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.cts +20 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.mts +20 -0
- package/dist/helpers.d.mts.map +1 -0
- package/dist/helpers.mjs +24 -0
- package/dist/helpers.mjs.map +1 -0
- package/dist/index.cjs +11 -301
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -718
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -718
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -264
- package/dist/index.mjs.map +1 -1
- package/dist/state.d.cts +1 -1
- package/dist/state.d.mts +1 -1
- package/dist/stream.d.cts +267 -2
- package/dist/stream.d.cts.map +1 -0
- package/dist/stream.d.mts +267 -2
- package/dist/stream.d.mts.map +1 -0
- package/dist/utils-CuS1Knym.cjs +401 -0
- package/dist/utils-CuS1Knym.cjs.map +1 -0
- package/dist/utils-DXL7JI0q.mjs +319 -0
- package/dist/utils-DXL7JI0q.mjs.map +1 -0
- package/dist/utils-DcbZmefT.d.cts +707 -0
- package/dist/utils-DcbZmefT.d.cts.map +1 -0
- package/dist/utils-tcJ0Rzg-.d.mts +707 -0
- package/dist/utils-tcJ0Rzg-.d.mts.map +1 -0
- package/package.json +7 -2
- package/dist/stream-CNxp3Hhu.d.cts +0 -267
- package/dist/stream-CNxp3Hhu.d.cts.map +0 -1
- package/dist/stream-CPQKQv-x.d.mts +0 -267
- package/dist/stream-CPQKQv-x.d.mts.map +0 -1
package/dist/helpers.cjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_utils = require('./utils-CuS1Knym.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,20 @@
|
|
|
1
|
+
import { S as ChannelItem, o as Channel, r as isChannelRef, t as extractChannelRefs, u as ISdk, x as ChannelDirection } from "./utils-DcbZmefT.cjs";
|
|
2
|
+
import { IStream } from "./stream.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/helpers.d.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
|
+
declare function createChannel(iii: ISdk, bufferSize?: number): Promise<Channel>;
|
|
11
|
+
/**
|
|
12
|
+
* Register a custom stream implementation by wiring its 5 callable methods
|
|
13
|
+
* to `stream::get/set/delete/list/list_groups`.
|
|
14
|
+
*
|
|
15
|
+
* Free-function form of the previous `ISdk.createStream` instance method.
|
|
16
|
+
*/
|
|
17
|
+
declare function createStream<TData>(iii: ISdk, streamName: string, stream: IStream<TData>): void;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ChannelDirection, ChannelItem, createChannel, createStream, extractChannelRefs, isChannelRef };
|
|
20
|
+
//# sourceMappingURL=helpers.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.cts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;;;;;iBAuBgB,aAAA,CAAc,GAAA,EAAK,IAAA,EAAM,UAAA,YAAsB,OAAA,CAAQ,OAAA;;;AAUvE;;;;iBAAgB,YAAA,OAAA,CAAoB,GAAA,EAAK,IAAA,EAAM,UAAA,UAAoB,MAAA,EAAQ,OAAA,CAAQ,KAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { S as ChannelItem, o as Channel, r as isChannelRef, t as extractChannelRefs, u as ISdk, x as ChannelDirection } from "./utils-tcJ0Rzg-.mjs";
|
|
2
|
+
import { IStream } from "./stream.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/helpers.d.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
|
+
declare function createChannel(iii: ISdk, bufferSize?: number): Promise<Channel>;
|
|
11
|
+
/**
|
|
12
|
+
* Register a custom stream implementation by wiring its 5 callable methods
|
|
13
|
+
* to `stream::get/set/delete/list/list_groups`.
|
|
14
|
+
*
|
|
15
|
+
* Free-function form of the previous `ISdk.createStream` instance method.
|
|
16
|
+
*/
|
|
17
|
+
declare function createStream<TData>(iii: ISdk, streamName: string, stream: IStream<TData>): void;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ChannelDirection, ChannelItem, createChannel, createStream, extractChannelRefs, isChannelRef };
|
|
20
|
+
//# sourceMappingURL=helpers.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.mts","names":[],"sources":["../src/helpers.ts"],"mappings":";;;;;;;;;iBAuBgB,aAAA,CAAc,GAAA,EAAK,IAAA,EAAM,UAAA,YAAsB,OAAA,CAAQ,OAAA;;;AAUvE;;;;iBAAgB,YAAA,OAAA,CAAoB,GAAA,EAAK,IAAA,EAAM,UAAA,UAAoB,MAAA,EAAQ,OAAA,CAAQ,KAAA"}
|
package/dist/helpers.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { a as ChannelDirection, i as isChannelRef, n as extractChannelRefs, o as ChannelItem } from "./utils-DXL7JI0q.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,228 +1,13 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
var __create = Object.create;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
-
key = keys[i];
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
-
__defProp(to, key, {
|
|
15
|
-
get: ((k) => from[k]).bind(null, key),
|
|
16
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
-
value: mod,
|
|
25
|
-
enumerable: true
|
|
26
|
-
}) : target, mod));
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
let node_stream = require("node:stream");
|
|
2
|
+
const require_utils = require('./utils-CuS1Knym.cjs');
|
|
30
3
|
let ws = require("ws");
|
|
31
4
|
let _opentelemetry_api = require("@opentelemetry/api");
|
|
32
5
|
let node_module = require("node:module");
|
|
33
6
|
let node_os = require("node:os");
|
|
34
|
-
node_os = __toESM(node_os);
|
|
7
|
+
node_os = require_utils.__toESM(node_os);
|
|
35
8
|
let _iii_dev_observability = require("@iii-dev/observability");
|
|
36
9
|
let _iii_dev_observability_internal = require("@iii-dev/observability/internal");
|
|
37
|
-
let node_fs = require("node:fs");
|
|
38
|
-
node_fs = __toESM(node_fs);
|
|
39
|
-
let node_path = require("node:path");
|
|
40
|
-
node_path = __toESM(node_path);
|
|
41
10
|
|
|
42
|
-
//#region src/channels.ts
|
|
43
|
-
/**
|
|
44
|
-
* Write end of a streaming channel. Provides both a Node.js `Writable` stream
|
|
45
|
-
* and a `sendMessage` method for sending structured text messages.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```typescript
|
|
49
|
-
* const channel = await iii.createChannel()
|
|
50
|
-
*
|
|
51
|
-
* // Stream binary data
|
|
52
|
-
* channel.writer.stream.write(Buffer.from('hello'))
|
|
53
|
-
* channel.writer.stream.end()
|
|
54
|
-
*
|
|
55
|
-
* // Or send text messages
|
|
56
|
-
* channel.writer.sendMessage(JSON.stringify({ type: 'event', data: 'test' }))
|
|
57
|
-
* channel.writer.close()
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
var ChannelWriter = class ChannelWriter {
|
|
61
|
-
static {
|
|
62
|
-
this.FRAME_SIZE = 64 * 1024;
|
|
63
|
-
}
|
|
64
|
-
constructor(engineWsBase, ref) {
|
|
65
|
-
this.ws = null;
|
|
66
|
-
this.wsReady = false;
|
|
67
|
-
this.pendingMessages = [];
|
|
68
|
-
this.url = buildChannelUrl(engineWsBase, ref.channel_id, ref.access_key, "write");
|
|
69
|
-
this.stream = new node_stream.Writable({
|
|
70
|
-
write: (chunk, _encoding, callback) => {
|
|
71
|
-
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
72
|
-
this.sendChunked(buf, callback);
|
|
73
|
-
},
|
|
74
|
-
final: (callback) => {
|
|
75
|
-
if (!this.ws) {
|
|
76
|
-
callback();
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const doClose = () => {
|
|
80
|
-
if (this.ws) this.ws.close(1e3, "stream_complete");
|
|
81
|
-
callback();
|
|
82
|
-
};
|
|
83
|
-
if (this.wsReady) setTimeout(doClose, 10);
|
|
84
|
-
else this.ws.on("open", () => setTimeout(doClose, 10));
|
|
85
|
-
},
|
|
86
|
-
destroy: (err, callback) => {
|
|
87
|
-
if (this.ws) this.ws.terminate();
|
|
88
|
-
callback(err);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
ensureConnected() {
|
|
93
|
-
if (this.ws) return;
|
|
94
|
-
this.ws = new ws.WebSocket(this.url);
|
|
95
|
-
this.ws.on("open", () => {
|
|
96
|
-
this.wsReady = true;
|
|
97
|
-
for (const { data, callback } of this.pendingMessages) this.ws?.send(data, callback);
|
|
98
|
-
this.pendingMessages.length = 0;
|
|
99
|
-
});
|
|
100
|
-
this.ws.on("error", (err) => {
|
|
101
|
-
this.stream.destroy(err);
|
|
102
|
-
});
|
|
103
|
-
this.ws.on("close", () => {
|
|
104
|
-
if (!this.stream.destroyed) this.stream.destroy();
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
/** Send a text message through the channel. */
|
|
108
|
-
sendMessage(msg) {
|
|
109
|
-
this.ensureConnected();
|
|
110
|
-
this.sendRaw(msg, (err) => {
|
|
111
|
-
if (err) this.stream.destroy(err);
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
/** Close the channel writer. */
|
|
115
|
-
close() {
|
|
116
|
-
if (!this.ws) return;
|
|
117
|
-
const doClose = () => {
|
|
118
|
-
if (this.ws) this.ws.close(1e3, "channel_close");
|
|
119
|
-
};
|
|
120
|
-
if (this.wsReady) doClose();
|
|
121
|
-
else this.ws.on("open", () => doClose());
|
|
122
|
-
}
|
|
123
|
-
sendChunked(data, callback) {
|
|
124
|
-
let offset = 0;
|
|
125
|
-
const sendNext = (err) => {
|
|
126
|
-
if (err) {
|
|
127
|
-
callback(err);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
if (offset >= data.length) {
|
|
131
|
-
callback(null);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const end = Math.min(offset + ChannelWriter.FRAME_SIZE, data.length);
|
|
135
|
-
const part = data.subarray(offset, end);
|
|
136
|
-
offset = end;
|
|
137
|
-
this.sendRaw(part, sendNext);
|
|
138
|
-
};
|
|
139
|
-
sendNext(null);
|
|
140
|
-
}
|
|
141
|
-
sendRaw(data, callback) {
|
|
142
|
-
this.ensureConnected();
|
|
143
|
-
if (this.wsReady && this.ws) this.ws.send(data, (err) => callback(err ?? null));
|
|
144
|
-
else this.pendingMessages.push({
|
|
145
|
-
data,
|
|
146
|
-
callback
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
/**
|
|
151
|
-
* Read end of a streaming channel. Provides both a Node.js `Readable` stream
|
|
152
|
-
* for binary data and an `onMessage` callback for structured text messages.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* const channel = await iii.createChannel()
|
|
157
|
-
*
|
|
158
|
-
* // Stream binary data
|
|
159
|
-
* channel.reader.stream.on('data', (chunk) => console.log(chunk))
|
|
160
|
-
*
|
|
161
|
-
* // Or receive text messages
|
|
162
|
-
* channel.reader.onMessage((msg) => console.log('Got:', msg))
|
|
163
|
-
* ```
|
|
164
|
-
*/
|
|
165
|
-
var ChannelReader = class {
|
|
166
|
-
constructor(engineWsBase, ref) {
|
|
167
|
-
this.ws = null;
|
|
168
|
-
this.connected = false;
|
|
169
|
-
this.messageCallbacks = [];
|
|
170
|
-
this.url = buildChannelUrl(engineWsBase, ref.channel_id, ref.access_key, "read");
|
|
171
|
-
const self = this;
|
|
172
|
-
this.stream = new node_stream.Readable({
|
|
173
|
-
read() {
|
|
174
|
-
self.ensureConnected();
|
|
175
|
-
if (self.ws) self.ws.resume();
|
|
176
|
-
},
|
|
177
|
-
destroy(err, callback) {
|
|
178
|
-
if (self.ws && self.ws.readyState !== ws.WebSocket.CLOSED) self.ws.terminate();
|
|
179
|
-
self.ws = null;
|
|
180
|
-
callback(err);
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
ensureConnected() {
|
|
185
|
-
if (this.connected) return;
|
|
186
|
-
this.connected = true;
|
|
187
|
-
this.ws = new ws.WebSocket(this.url);
|
|
188
|
-
this.ws.on("open", () => {
|
|
189
|
-
this.ws.binaryType = "nodebuffer";
|
|
190
|
-
});
|
|
191
|
-
this.ws.on("message", (data, isBinary) => {
|
|
192
|
-
if (isBinary) {
|
|
193
|
-
if (!this.stream.push(data)) this.ws?.pause();
|
|
194
|
-
} else {
|
|
195
|
-
const msg = data.toString("utf-8");
|
|
196
|
-
for (const cb of this.messageCallbacks) cb(msg);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
this.ws.on("close", () => {
|
|
200
|
-
this.ws = null;
|
|
201
|
-
if (!this.stream.destroyed) this.stream.push(null);
|
|
202
|
-
});
|
|
203
|
-
this.ws.on("error", (err) => {
|
|
204
|
-
this.stream.destroy(err);
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
/** Register a callback to receive text messages from the channel. */
|
|
208
|
-
onMessage(callback) {
|
|
209
|
-
this.messageCallbacks.push(callback);
|
|
210
|
-
}
|
|
211
|
-
async readAll() {
|
|
212
|
-
this.ensureConnected();
|
|
213
|
-
const chunks = [];
|
|
214
|
-
for await (const chunk of this.stream) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
215
|
-
return Buffer.concat(chunks);
|
|
216
|
-
}
|
|
217
|
-
close() {
|
|
218
|
-
if (this.ws && this.ws.readyState !== ws.WebSocket.CLOSED) this.ws.close(1e3, "channel_close");
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
function buildChannelUrl(engineWsBase, channelId, accessKey, direction) {
|
|
222
|
-
return `${engineWsBase.replace(/\/$/, "")}/ws/channels/${channelId}?key=${encodeURIComponent(accessKey)}&dir=${direction}`;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
//#endregion
|
|
226
11
|
//#region src/errors.ts
|
|
227
12
|
var IIIInvocationError = class extends Error {
|
|
228
13
|
constructor(init) {
|
|
@@ -302,81 +87,6 @@ let MessageType = /* @__PURE__ */ function(MessageType) {
|
|
|
302
87
|
return MessageType;
|
|
303
88
|
}({});
|
|
304
89
|
|
|
305
|
-
//#endregion
|
|
306
|
-
//#region src/utils.ts
|
|
307
|
-
/**
|
|
308
|
-
* Returns a project identifier for telemetry, derived from the current working
|
|
309
|
-
* directory. Reads `package.json` `name` if present at `cwd`; otherwise falls
|
|
310
|
-
* back to the basename of `cwd`. Returns `undefined` only when both signals
|
|
311
|
-
* are unavailable (e.g. cwd is the filesystem root).
|
|
312
|
-
*
|
|
313
|
-
* No directory walking — only inspects `cwd` itself, so the SDK never reads
|
|
314
|
-
* files outside the user's explicit working directory.
|
|
315
|
-
*/
|
|
316
|
-
function detectProjectName(cwd = process.cwd()) {
|
|
317
|
-
try {
|
|
318
|
-
const manifest = node_path.join(cwd, "package.json");
|
|
319
|
-
if (node_fs.existsSync(manifest)) {
|
|
320
|
-
const parsed = JSON.parse(node_fs.readFileSync(manifest, "utf8"));
|
|
321
|
-
if (typeof parsed.name === "string") {
|
|
322
|
-
const trimmed = parsed.name.trim();
|
|
323
|
-
if (trimmed) return trimmed;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
} catch {}
|
|
327
|
-
return node_path.basename(cwd).trim() || void 0;
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Helper that wraps an HTTP-style handler (with separate `req`/`res` arguments)
|
|
331
|
-
* into the function handler format expected by the SDK.
|
|
332
|
-
*
|
|
333
|
-
* @param callback - Async handler receiving an {@link HttpRequest} and {@link HttpResponse}.
|
|
334
|
-
* @returns A function handler compatible with {@link ISdk.registerFunction}.
|
|
335
|
-
*
|
|
336
|
-
* @example
|
|
337
|
-
* ```typescript
|
|
338
|
-
* import { http } from 'iii-sdk'
|
|
339
|
-
*
|
|
340
|
-
* iii.registerFunction(
|
|
341
|
-
* 'my-api',
|
|
342
|
-
* http(async (req, res) => {
|
|
343
|
-
* res.status(200)
|
|
344
|
-
* res.headers({ 'content-type': 'application/json' })
|
|
345
|
-
* res.stream.end(JSON.stringify({ hello: 'world' }))
|
|
346
|
-
* res.close()
|
|
347
|
-
* }),
|
|
348
|
-
* )
|
|
349
|
-
* ```
|
|
350
|
-
*/
|
|
351
|
-
const http = (callback) => {
|
|
352
|
-
return async (req) => {
|
|
353
|
-
const { response, ...request } = req;
|
|
354
|
-
return callback(request, {
|
|
355
|
-
status: (status_code) => response.sendMessage(JSON.stringify({
|
|
356
|
-
type: "set_status",
|
|
357
|
-
status_code
|
|
358
|
-
})),
|
|
359
|
-
headers: (headers) => response.sendMessage(JSON.stringify({
|
|
360
|
-
type: "set_headers",
|
|
361
|
-
headers
|
|
362
|
-
})),
|
|
363
|
-
stream: response.stream,
|
|
364
|
-
close: () => response.close()
|
|
365
|
-
});
|
|
366
|
-
};
|
|
367
|
-
};
|
|
368
|
-
/**
|
|
369
|
-
* Type guard that checks if a value is a {@link StreamChannelRef}.
|
|
370
|
-
*
|
|
371
|
-
* @param value - Value to check.
|
|
372
|
-
* @returns `true` if the value is a valid `StreamChannelRef`.
|
|
373
|
-
*/
|
|
374
|
-
const isChannelRef = (value) => {
|
|
375
|
-
if (typeof value !== "object" || value === null) return false;
|
|
376
|
-
const maybe = value;
|
|
377
|
-
return typeof maybe.channel_id === "string" && typeof maybe.access_key === "string" && (maybe.direction === "read" || maybe.direction === "write");
|
|
378
|
-
};
|
|
379
|
-
|
|
380
90
|
//#endregion
|
|
381
91
|
//#region src/iii.ts
|
|
382
92
|
const { version: SDK_VERSION } = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href)("../package.json");
|
|
@@ -536,14 +246,14 @@ var Sdk = class {
|
|
|
536
246
|
}
|
|
537
247
|
};
|
|
538
248
|
};
|
|
539
|
-
this.
|
|
249
|
+
this.__helpers_create_channel = async (bufferSize) => {
|
|
540
250
|
const result = await this.trigger({
|
|
541
251
|
function_id: "engine::channels::create",
|
|
542
252
|
payload: { buffer_size: bufferSize }
|
|
543
253
|
});
|
|
544
254
|
return {
|
|
545
|
-
writer: new ChannelWriter(this.address, result.writer),
|
|
546
|
-
reader: new ChannelReader(this.address, result.reader),
|
|
255
|
+
writer: new require_utils.ChannelWriter(this.address, result.writer),
|
|
256
|
+
reader: new require_utils.ChannelReader(this.address, result.reader),
|
|
547
257
|
writerRef: result.writer,
|
|
548
258
|
readerRef: result.reader
|
|
549
259
|
};
|
|
@@ -599,7 +309,7 @@ var Sdk = class {
|
|
|
599
309
|
});
|
|
600
310
|
});
|
|
601
311
|
};
|
|
602
|
-
this.
|
|
312
|
+
this.__helpers_create_stream = (streamName, stream) => {
|
|
603
313
|
this.registerFunction(`stream::get(${streamName})`, stream.get.bind(stream));
|
|
604
314
|
this.registerFunction(`stream::set(${streamName})`, stream.set.bind(stream));
|
|
605
315
|
this.registerFunction(`stream::delete(${streamName})`, stream.delete.bind(stream));
|
|
@@ -653,7 +363,7 @@ var Sdk = class {
|
|
|
653
363
|
isolation: process.env.III_ISOLATION || null,
|
|
654
364
|
telemetry: {
|
|
655
365
|
language,
|
|
656
|
-
project_name: telemetryOpts?.project_name ?? detectProjectName(),
|
|
366
|
+
project_name: telemetryOpts?.project_name ?? require_utils.detectProjectName(),
|
|
657
367
|
framework: telemetryOpts?.framework?.trim() || "iii-node",
|
|
658
368
|
amplitude_api_key: telemetryOpts?.amplitude_api_key
|
|
659
369
|
}
|
|
@@ -835,7 +545,7 @@ var Sdk = class {
|
|
|
835
545
|
});
|
|
836
546
|
}
|
|
837
547
|
resolveChannelValue(value) {
|
|
838
|
-
if (isChannelRef(value)) return value.direction === "read" ? new ChannelReader(this.address, value) : new ChannelWriter(this.address, value);
|
|
548
|
+
if (require_utils.isChannelRef(value)) return value.direction === "read" ? new require_utils.ChannelReader(this.address, value) : new require_utils.ChannelWriter(this.address, value);
|
|
839
549
|
if (Array.isArray(value)) return value.map((item) => this.resolveChannelValue(item));
|
|
840
550
|
if (value !== null && typeof value === "object") {
|
|
841
551
|
const out = {};
|
|
@@ -1017,8 +727,8 @@ const TriggerAction = {
|
|
|
1017
727
|
const registerWorker = (address, options) => new Sdk(address, options);
|
|
1018
728
|
|
|
1019
729
|
//#endregion
|
|
1020
|
-
exports.ChannelReader = ChannelReader;
|
|
1021
|
-
exports.ChannelWriter = ChannelWriter;
|
|
730
|
+
exports.ChannelReader = require_utils.ChannelReader;
|
|
731
|
+
exports.ChannelWriter = require_utils.ChannelWriter;
|
|
1022
732
|
exports.EngineFunctions = EngineFunctions;
|
|
1023
733
|
exports.EngineTriggers = EngineTriggers;
|
|
1024
734
|
exports.IIIInvocationError = IIIInvocationError;
|
|
@@ -1029,6 +739,6 @@ Object.defineProperty(exports, 'Logger', {
|
|
|
1029
739
|
}
|
|
1030
740
|
});
|
|
1031
741
|
exports.TriggerAction = TriggerAction;
|
|
1032
|
-
exports.http = http;
|
|
742
|
+
exports.http = require_utils.http;
|
|
1033
743
|
exports.registerWorker = registerWorker;
|
|
1034
744
|
//# sourceMappingURL=index.cjs.map
|