iii-sdk 0.8.3 → 0.10.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/README.md +15 -11
- package/dist/index.cjs +278 -142
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +177 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +177 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +259 -125
- package/dist/index.mjs.map +1 -1
- package/dist/state.cjs +7 -5
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +38 -13
- package/dist/state.d.cts.map +1 -1
- package/dist/state.d.mts +38 -13
- package/dist/state.d.mts.map +1 -1
- package/dist/state.mjs +6 -5
- package/dist/state.mjs.map +1 -1
- package/dist/stream-C1zUjhzk.d.mts +145 -0
- package/dist/stream-C1zUjhzk.d.mts.map +1 -0
- package/dist/stream-lxenNA3s.d.cts +145 -0
- package/dist/stream-lxenNA3s.d.cts.map +1 -0
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.mts +1 -1
- package/dist/telemetry.cjs +6 -5
- package/dist/telemetry.d.cts +10 -11
- package/dist/telemetry.d.cts.map +1 -1
- package/dist/telemetry.d.mts +10 -11
- package/dist/telemetry.d.mts.map +1 -1
- package/dist/telemetry.mjs +1 -1
- package/dist/utils-BvWlFlLq.d.cts +839 -0
- package/dist/utils-BvWlFlLq.d.cts.map +1 -0
- package/dist/{utils-BJTjoUdq.cjs → utils-C6yTT4Js.cjs} +311 -282
- package/dist/utils-C6yTT4Js.cjs.map +1 -0
- package/dist/utils-_zSeatp1.d.mts +839 -0
- package/dist/utils-_zSeatp1.d.mts.map +1 -0
- package/dist/{utils-coGqiBHT.mjs → utils-xBUm8n1P.mjs} +246 -217
- package/dist/utils-xBUm8n1P.mjs.map +1 -0
- package/package.json +5 -3
- package/typedoc.json +8 -0
- package/vitest.config.ts +4 -4
- package/dist/stream-BEp3rjfm.d.cts +0 -97
- package/dist/stream-BEp3rjfm.d.cts.map +0 -1
- package/dist/stream-Bzpo5JNV.d.mts +0 -97
- package/dist/stream-Bzpo5JNV.d.mts.map +0 -1
- package/dist/utils-BJTjoUdq.cjs.map +0 -1
- package/dist/utils-BaGgUfjl.d.cts +0 -524
- package/dist/utils-BaGgUfjl.d.cts.map +0 -1
- package/dist/utils-CMrMD5Ij.d.mts +0 -524
- package/dist/utils-CMrMD5Ij.d.mts.map +0 -1
- package/dist/utils-coGqiBHT.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
|
-
import { A as EngineTriggers,
|
|
1
|
+
import { A as EngineTriggers, D as DEFAULT_BRIDGE_RECONNECTION_CONFIG, O as DEFAULT_INVOCATION_TIMEOUT_MS, T as stopWorkerGauges, a as SpanKind$1, b as injectBaggage, c as getMeter, d as shutdownOtel, f as withSpan, g as extractContext, i as SeverityNumber$1, k as EngineFunctions, l as getTracer, m as currentTraceId, n as isChannelRef, p as currentSpanId, s as getLogger, t as http, u as initOtel, w as registerWorkerGauges, x as injectTraceparent } from "./utils-xBUm8n1P.mjs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
|
+
import { Readable, Writable } from "node:stream";
|
|
4
|
+
import { WebSocket } from "ws";
|
|
3
5
|
import { context, trace } from "@opentelemetry/api";
|
|
4
6
|
import * as os from "node:os";
|
|
5
|
-
import { WebSocket } from "ws";
|
|
6
|
-
import { Readable, Writable } from "node:stream";
|
|
7
7
|
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
8
8
|
|
|
9
9
|
//#region src/channels.ts
|
|
10
|
+
/**
|
|
11
|
+
* Write end of a streaming channel. Provides both a Node.js `Writable` stream
|
|
12
|
+
* and a `sendMessage` method for sending structured text messages.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const channel = await iii.createChannel()
|
|
17
|
+
*
|
|
18
|
+
* // Stream binary data
|
|
19
|
+
* channel.writer.stream.write(Buffer.from('hello'))
|
|
20
|
+
* channel.writer.stream.end()
|
|
21
|
+
*
|
|
22
|
+
* // Or send text messages
|
|
23
|
+
* channel.writer.sendMessage(JSON.stringify({ type: 'event', data: 'test' }))
|
|
24
|
+
* channel.writer.close()
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
10
27
|
var ChannelWriter = class ChannelWriter {
|
|
11
28
|
static {
|
|
12
29
|
this.FRAME_SIZE = 64 * 1024;
|
|
@@ -26,9 +43,12 @@ var ChannelWriter = class ChannelWriter {
|
|
|
26
43
|
callback();
|
|
27
44
|
return;
|
|
28
45
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
const doClose = () => {
|
|
47
|
+
if (this.ws) this.ws.close(1e3, "stream_complete");
|
|
48
|
+
callback();
|
|
49
|
+
};
|
|
50
|
+
if (this.wsReady) setTimeout(doClose, 10);
|
|
51
|
+
else this.ws.on("open", () => setTimeout(doClose, 10));
|
|
32
52
|
},
|
|
33
53
|
destroy: (err, callback) => {
|
|
34
54
|
if (this.ws) this.ws.terminate();
|
|
@@ -51,16 +71,21 @@ var ChannelWriter = class ChannelWriter {
|
|
|
51
71
|
if (!this.stream.destroyed) this.stream.destroy();
|
|
52
72
|
});
|
|
53
73
|
}
|
|
74
|
+
/** Send a text message through the channel. */
|
|
54
75
|
sendMessage(msg) {
|
|
55
76
|
this.ensureConnected();
|
|
56
77
|
this.sendRaw(msg, (err) => {
|
|
57
78
|
if (err) this.stream.destroy(err);
|
|
58
79
|
});
|
|
59
80
|
}
|
|
81
|
+
/** Close the channel writer. */
|
|
60
82
|
close() {
|
|
61
83
|
if (!this.ws) return;
|
|
62
|
-
|
|
63
|
-
|
|
84
|
+
const doClose = () => {
|
|
85
|
+
if (this.ws) this.ws.close(1e3, "channel_close");
|
|
86
|
+
};
|
|
87
|
+
if (this.wsReady) doClose();
|
|
88
|
+
else this.ws.on("open", () => doClose());
|
|
64
89
|
}
|
|
65
90
|
sendChunked(data, callback) {
|
|
66
91
|
let offset = 0;
|
|
@@ -89,6 +114,21 @@ var ChannelWriter = class ChannelWriter {
|
|
|
89
114
|
});
|
|
90
115
|
}
|
|
91
116
|
};
|
|
117
|
+
/**
|
|
118
|
+
* Read end of a streaming channel. Provides both a Node.js `Readable` stream
|
|
119
|
+
* for binary data and an `onMessage` callback for structured text messages.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const channel = await iii.createChannel()
|
|
124
|
+
*
|
|
125
|
+
* // Stream binary data
|
|
126
|
+
* channel.reader.stream.on('data', (chunk) => console.log(chunk))
|
|
127
|
+
*
|
|
128
|
+
* // Or receive text messages
|
|
129
|
+
* channel.reader.onMessage((msg) => console.log('Got:', msg))
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
92
132
|
var ChannelReader = class {
|
|
93
133
|
constructor(engineWsBase, ref) {
|
|
94
134
|
this.ws = null;
|
|
@@ -131,9 +171,19 @@ var ChannelReader = class {
|
|
|
131
171
|
this.stream.destroy(err);
|
|
132
172
|
});
|
|
133
173
|
}
|
|
174
|
+
/** Register a callback to receive text messages from the channel. */
|
|
134
175
|
onMessage(callback) {
|
|
135
176
|
this.messageCallbacks.push(callback);
|
|
136
177
|
}
|
|
178
|
+
async readAll() {
|
|
179
|
+
this.ensureConnected();
|
|
180
|
+
const chunks = [];
|
|
181
|
+
for await (const chunk of this.stream) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
182
|
+
return Buffer.concat(chunks);
|
|
183
|
+
}
|
|
184
|
+
close() {
|
|
185
|
+
if (this.ws && this.ws.readyState !== WebSocket.CLOSED) this.ws.close(1e3, "channel_close");
|
|
186
|
+
}
|
|
137
187
|
};
|
|
138
188
|
function buildChannelUrl(engineWsBase, channelId, accessKey, direction) {
|
|
139
189
|
return `${engineWsBase.replace(/\/$/, "")}/ws/channels/${channelId}?key=${encodeURIComponent(accessKey)}&dir=${direction}`;
|
|
@@ -141,19 +191,19 @@ function buildChannelUrl(engineWsBase, channelId, accessKey, direction) {
|
|
|
141
191
|
|
|
142
192
|
//#endregion
|
|
143
193
|
//#region src/iii-types.ts
|
|
144
|
-
let MessageType = /* @__PURE__ */ function(MessageType
|
|
145
|
-
MessageType
|
|
146
|
-
MessageType
|
|
147
|
-
MessageType
|
|
148
|
-
MessageType
|
|
149
|
-
MessageType
|
|
150
|
-
MessageType
|
|
151
|
-
MessageType
|
|
152
|
-
MessageType
|
|
153
|
-
MessageType
|
|
154
|
-
MessageType
|
|
155
|
-
MessageType
|
|
156
|
-
return MessageType
|
|
194
|
+
let MessageType = /* @__PURE__ */ function(MessageType) {
|
|
195
|
+
MessageType["RegisterFunction"] = "registerfunction";
|
|
196
|
+
MessageType["UnregisterFunction"] = "unregisterfunction";
|
|
197
|
+
MessageType["RegisterService"] = "registerservice";
|
|
198
|
+
MessageType["InvokeFunction"] = "invokefunction";
|
|
199
|
+
MessageType["InvocationResult"] = "invocationresult";
|
|
200
|
+
MessageType["RegisterTriggerType"] = "registertriggertype";
|
|
201
|
+
MessageType["RegisterTrigger"] = "registertrigger";
|
|
202
|
+
MessageType["UnregisterTrigger"] = "unregistertrigger";
|
|
203
|
+
MessageType["UnregisterTriggerType"] = "unregistertriggertype";
|
|
204
|
+
MessageType["TriggerRegistrationResult"] = "triggerregistrationresult";
|
|
205
|
+
MessageType["WorkerRegistered"] = "workerregistered";
|
|
206
|
+
return MessageType;
|
|
157
207
|
}({});
|
|
158
208
|
|
|
159
209
|
//#endregion
|
|
@@ -175,11 +225,9 @@ var Sdk = class {
|
|
|
175
225
|
this.triggers = /* @__PURE__ */ new Map();
|
|
176
226
|
this.triggerTypes = /* @__PURE__ */ new Map();
|
|
177
227
|
this.functionsAvailableCallbacks = /* @__PURE__ */ new Set();
|
|
178
|
-
this.logCallbacks = /* @__PURE__ */ new Map();
|
|
179
228
|
this.messagesToSend = [];
|
|
180
229
|
this.reconnectAttempt = 0;
|
|
181
230
|
this.connectionState = "disconnected";
|
|
182
|
-
this.stateCallbacks = /* @__PURE__ */ new Set();
|
|
183
231
|
this.isShuttingDown = false;
|
|
184
232
|
this.registerTriggerType = (triggerType, handler) => {
|
|
185
233
|
this.sendMessage(MessageType.RegisterTriggerType, triggerType, true);
|
|
@@ -191,9 +239,6 @@ var Sdk = class {
|
|
|
191
239
|
handler
|
|
192
240
|
});
|
|
193
241
|
};
|
|
194
|
-
this.on = (event, callback) => {
|
|
195
|
-
this.ws?.on(event, callback);
|
|
196
|
-
};
|
|
197
242
|
this.unregisterTriggerType = (triggerType) => {
|
|
198
243
|
this.sendMessage(MessageType.UnregisterTriggerType, triggerType, true);
|
|
199
244
|
this.triggerTypes.delete(triggerType.id);
|
|
@@ -264,14 +309,21 @@ var Sdk = class {
|
|
|
264
309
|
};
|
|
265
310
|
};
|
|
266
311
|
this.registerService = (message) => {
|
|
267
|
-
|
|
268
|
-
this.services.set(message.id, {
|
|
312
|
+
const msg = {
|
|
269
313
|
...message,
|
|
314
|
+
name: message.name ?? message.id
|
|
315
|
+
};
|
|
316
|
+
this.sendMessage(MessageType.RegisterService, msg, true);
|
|
317
|
+
this.services.set(message.id, {
|
|
318
|
+
...msg,
|
|
270
319
|
message_type: MessageType.RegisterService
|
|
271
320
|
});
|
|
272
321
|
};
|
|
273
322
|
this.createChannel = async (bufferSize) => {
|
|
274
|
-
const result = await this.
|
|
323
|
+
const result = await this.trigger({
|
|
324
|
+
function_id: "engine::channels::create",
|
|
325
|
+
payload: { buffer_size: bufferSize }
|
|
326
|
+
});
|
|
275
327
|
return {
|
|
276
328
|
writer: new ChannelWriter(this.address, result.writer),
|
|
277
329
|
reader: new ChannelReader(this.address, result.reader),
|
|
@@ -279,11 +331,24 @@ var Sdk = class {
|
|
|
279
331
|
readerRef: result.reader
|
|
280
332
|
};
|
|
281
333
|
};
|
|
282
|
-
this.trigger = async (
|
|
334
|
+
this.trigger = async (request) => {
|
|
335
|
+
const { function_id, payload, action, timeoutMs } = request;
|
|
336
|
+
const effectiveTimeout = timeoutMs ?? this.invocationTimeoutMs;
|
|
337
|
+
if (action?.type === "void") {
|
|
338
|
+
const traceparent = injectTraceparent();
|
|
339
|
+
const baggage = injectBaggage();
|
|
340
|
+
this.sendMessage(MessageType.InvokeFunction, {
|
|
341
|
+
function_id,
|
|
342
|
+
data: payload,
|
|
343
|
+
traceparent,
|
|
344
|
+
baggage,
|
|
345
|
+
action
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
283
349
|
const invocation_id = crypto.randomUUID();
|
|
284
350
|
const traceparent = injectTraceparent();
|
|
285
351
|
const baggage = injectBaggage();
|
|
286
|
-
const effectiveTimeout = timeoutMs ?? this.invocationTimeoutMs;
|
|
287
352
|
return new Promise((resolve, reject) => {
|
|
288
353
|
const timeout = setTimeout(() => {
|
|
289
354
|
if (this.invocations.get(invocation_id)) {
|
|
@@ -305,29 +370,30 @@ var Sdk = class {
|
|
|
305
370
|
this.sendMessage(MessageType.InvokeFunction, {
|
|
306
371
|
invocation_id,
|
|
307
372
|
function_id,
|
|
308
|
-
data,
|
|
373
|
+
data: payload,
|
|
309
374
|
traceparent,
|
|
310
|
-
baggage
|
|
375
|
+
baggage,
|
|
376
|
+
action
|
|
311
377
|
});
|
|
312
378
|
});
|
|
313
379
|
};
|
|
314
|
-
this.triggerVoid = (function_id, data) => {
|
|
315
|
-
const traceparent = injectTraceparent();
|
|
316
|
-
const baggage = injectBaggage();
|
|
317
|
-
this.sendMessage(MessageType.InvokeFunction, {
|
|
318
|
-
function_id,
|
|
319
|
-
data,
|
|
320
|
-
traceparent,
|
|
321
|
-
baggage
|
|
322
|
-
});
|
|
323
|
-
};
|
|
324
|
-
this.call = async (function_id, data, timeoutMs) => this.trigger(function_id, data, timeoutMs);
|
|
325
|
-
this.callVoid = (function_id, data) => this.triggerVoid(function_id, data);
|
|
326
380
|
this.listFunctions = async () => {
|
|
327
|
-
return (await this.trigger(
|
|
381
|
+
return (await this.trigger({
|
|
382
|
+
function_id: EngineFunctions.LIST_FUNCTIONS,
|
|
383
|
+
payload: {}
|
|
384
|
+
})).functions;
|
|
328
385
|
};
|
|
329
386
|
this.listWorkers = async () => {
|
|
330
|
-
return (await this.trigger(
|
|
387
|
+
return (await this.trigger({
|
|
388
|
+
function_id: EngineFunctions.LIST_WORKERS,
|
|
389
|
+
payload: {}
|
|
390
|
+
})).workers;
|
|
391
|
+
};
|
|
392
|
+
this.listTriggers = async (includeInternal = false) => {
|
|
393
|
+
return (await this.trigger({
|
|
394
|
+
function_id: EngineFunctions.LIST_TRIGGERS,
|
|
395
|
+
payload: { include_internal: includeInternal }
|
|
396
|
+
})).triggers;
|
|
331
397
|
};
|
|
332
398
|
this.createStream = (streamName, stream) => {
|
|
333
399
|
this.registerFunction({ id: `stream::get(${streamName})` }, stream.get.bind(stream));
|
|
@@ -361,48 +427,6 @@ var Sdk = class {
|
|
|
361
427
|
}
|
|
362
428
|
};
|
|
363
429
|
};
|
|
364
|
-
this.onLog = (callback, config) => {
|
|
365
|
-
const effectiveConfig = config ?? { level: "all" };
|
|
366
|
-
this.logCallbacks.set(callback, effectiveConfig);
|
|
367
|
-
if (!this.logTrigger) {
|
|
368
|
-
if (!this.logFunctionPath) this.logFunctionPath = `engine.on_log.${crypto.randomUUID()}`;
|
|
369
|
-
const function_id = this.logFunctionPath;
|
|
370
|
-
if (!this.functions.has(function_id)) this.registerFunction({ id: function_id }, async (log) => {
|
|
371
|
-
this.logCallbacks.forEach((cfg, handler) => {
|
|
372
|
-
try {
|
|
373
|
-
const minSeverity = this.severityTextToNumber(cfg.level ?? "all");
|
|
374
|
-
if (cfg.level === "all" || log.severity_number >= minSeverity) handler(log);
|
|
375
|
-
} catch (error) {
|
|
376
|
-
this.logError("Log callback handler threw an exception", error);
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
return null;
|
|
380
|
-
});
|
|
381
|
-
this.logTrigger = this.registerTrigger({
|
|
382
|
-
type: EngineTriggers.LOG,
|
|
383
|
-
function_id,
|
|
384
|
-
config: {
|
|
385
|
-
level: "all",
|
|
386
|
-
severity_min: 0
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
return () => {
|
|
391
|
-
this.logCallbacks.delete(callback);
|
|
392
|
-
if (this.logCallbacks.size === 0 && this.logTrigger) {
|
|
393
|
-
this.logTrigger.unregister();
|
|
394
|
-
this.logTrigger = void 0;
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
};
|
|
398
|
-
this.getConnectionState = () => {
|
|
399
|
-
return this.connectionState;
|
|
400
|
-
};
|
|
401
|
-
this.onConnectionStateChange = (callback) => {
|
|
402
|
-
this.stateCallbacks.add(callback);
|
|
403
|
-
callback(this.connectionState);
|
|
404
|
-
return () => this.stateCallbacks.delete(callback);
|
|
405
|
-
};
|
|
406
430
|
this.shutdown = async () => {
|
|
407
431
|
this.isShuttingDown = true;
|
|
408
432
|
this.stopMetricsReporting();
|
|
@@ -418,12 +442,11 @@ var Sdk = class {
|
|
|
418
442
|
this.ws.close();
|
|
419
443
|
this.ws = void 0;
|
|
420
444
|
}
|
|
421
|
-
this.stateCallbacks.clear();
|
|
422
445
|
this.setConnectionState("disconnected");
|
|
423
446
|
};
|
|
424
447
|
this.workerName = options?.workerName ?? getDefaultWorkerName();
|
|
425
448
|
this.metricsReportingEnabled = options?.enableMetricsReporting ?? true;
|
|
426
|
-
this.invocationTimeoutMs = options?.invocationTimeoutMs ??
|
|
449
|
+
this.invocationTimeoutMs = options?.invocationTimeoutMs ?? 3e4;
|
|
427
450
|
this.reconnectionConfig = {
|
|
428
451
|
...DEFAULT_BRIDGE_RECONNECTION_CONFIG,
|
|
429
452
|
...options?.reconnectionConfig
|
|
@@ -437,29 +460,26 @@ var Sdk = class {
|
|
|
437
460
|
registerWorkerMetadata() {
|
|
438
461
|
const telemetryOpts = this.options?.telemetry;
|
|
439
462
|
const language = telemetryOpts?.language ?? Intl.DateTimeFormat().resolvedOptions().locale ?? process.env.LANG?.split(".")[0];
|
|
440
|
-
this.
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
463
|
+
this.trigger({
|
|
464
|
+
function_id: EngineFunctions.REGISTER_WORKER,
|
|
465
|
+
payload: {
|
|
466
|
+
runtime: "node",
|
|
467
|
+
version: SDK_VERSION,
|
|
468
|
+
name: this.workerName,
|
|
469
|
+
os: getOsInfo(),
|
|
470
|
+
pid: process.pid,
|
|
471
|
+
telemetry: {
|
|
472
|
+
language,
|
|
473
|
+
project_name: telemetryOpts?.project_name,
|
|
474
|
+
framework: telemetryOpts?.framework,
|
|
475
|
+
amplitude_api_key: telemetryOpts?.amplitude_api_key
|
|
476
|
+
}
|
|
477
|
+
},
|
|
478
|
+
action: { type: "void" }
|
|
452
479
|
});
|
|
453
480
|
}
|
|
454
481
|
setConnectionState(state) {
|
|
455
|
-
if (this.connectionState !== state)
|
|
456
|
-
this.connectionState = state;
|
|
457
|
-
for (const callback of this.stateCallbacks) try {
|
|
458
|
-
callback(state);
|
|
459
|
-
} catch (error) {
|
|
460
|
-
this.logError("Error in connection state callback", error);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
482
|
+
if (this.connectionState !== state) this.connectionState = state;
|
|
463
483
|
}
|
|
464
484
|
connect() {
|
|
465
485
|
if (this.isShuttingDown) return;
|
|
@@ -604,18 +624,6 @@ var Sdk = class {
|
|
|
604
624
|
});
|
|
605
625
|
else console.error(`[iii] ${message}`, error ?? "");
|
|
606
626
|
}
|
|
607
|
-
severityTextToNumber(level) {
|
|
608
|
-
switch (level) {
|
|
609
|
-
case "trace": return 1;
|
|
610
|
-
case "debug": return 5;
|
|
611
|
-
case "info": return 9;
|
|
612
|
-
case "warn": return 13;
|
|
613
|
-
case "error": return 17;
|
|
614
|
-
case "fatal": return 21;
|
|
615
|
-
case "all": return 0;
|
|
616
|
-
default: return 0;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
627
|
onInvocationResult(invocation_id, result, error) {
|
|
620
628
|
const invocation = this.invocations.get(invocation_id);
|
|
621
629
|
if (invocation) {
|
|
@@ -658,12 +666,14 @@ var Sdk = class {
|
|
|
658
666
|
baggage: getResponseBaggage()
|
|
659
667
|
});
|
|
660
668
|
} catch (error) {
|
|
669
|
+
const isError = error instanceof Error;
|
|
661
670
|
this.sendMessage(MessageType.InvocationResult, {
|
|
662
671
|
invocation_id,
|
|
663
672
|
function_id,
|
|
664
673
|
error: {
|
|
665
674
|
code: "invocation_failed",
|
|
666
|
-
message: error.message
|
|
675
|
+
message: isError ? error.message : String(error),
|
|
676
|
+
stacktrace: isError ? error.stack : void 0
|
|
667
677
|
},
|
|
668
678
|
traceparent: getResponseTraceparent(),
|
|
669
679
|
baggage: getResponseBaggage()
|
|
@@ -749,10 +759,82 @@ var Sdk = class {
|
|
|
749
759
|
}
|
|
750
760
|
}
|
|
751
761
|
};
|
|
762
|
+
/**
|
|
763
|
+
* Factory object that constructs routing actions for {@link ISdk.trigger}.
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* ```typescript
|
|
767
|
+
* import { TriggerAction } from 'iii-sdk'
|
|
768
|
+
*
|
|
769
|
+
* // Enqueue to a named queue
|
|
770
|
+
* iii.trigger({
|
|
771
|
+
* function_id: 'process',
|
|
772
|
+
* payload: { data: 'hello' },
|
|
773
|
+
* action: TriggerAction.Enqueue({ queue: 'jobs' }),
|
|
774
|
+
* })
|
|
775
|
+
*
|
|
776
|
+
* // Fire-and-forget
|
|
777
|
+
* iii.trigger({
|
|
778
|
+
* function_id: 'notify',
|
|
779
|
+
* payload: {},
|
|
780
|
+
* action: TriggerAction.Void(),
|
|
781
|
+
* })
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
784
|
+
const TriggerAction = {
|
|
785
|
+
Enqueue: (opts) => ({
|
|
786
|
+
type: "enqueue",
|
|
787
|
+
...opts
|
|
788
|
+
}),
|
|
789
|
+
Void: () => ({ type: "void" })
|
|
790
|
+
};
|
|
791
|
+
/**
|
|
792
|
+
* Creates and returns a connected SDK instance. The WebSocket connection is
|
|
793
|
+
* established automatically -- there is no separate `connect()` call.
|
|
794
|
+
*
|
|
795
|
+
* @param address - WebSocket URL of the III engine (e.g. `ws://localhost:49134`).
|
|
796
|
+
* @param options - Optional {@link InitOptions} for worker name, timeouts, reconnection, and OTel.
|
|
797
|
+
* @returns A connected {@link ISdk} instance.
|
|
798
|
+
*
|
|
799
|
+
* @example
|
|
800
|
+
* ```typescript
|
|
801
|
+
* import { registerWorker } from 'iii-sdk'
|
|
802
|
+
*
|
|
803
|
+
* const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134', {
|
|
804
|
+
* workerName: 'my-worker',
|
|
805
|
+
* })
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
752
808
|
const registerWorker = (address, options) => new Sdk(address, options);
|
|
753
809
|
|
|
754
810
|
//#endregion
|
|
755
811
|
//#region src/logger.ts
|
|
812
|
+
/**
|
|
813
|
+
* Structured logger that emits logs as OpenTelemetry LogRecords.
|
|
814
|
+
*
|
|
815
|
+
* Every log call automatically captures the active trace and span context,
|
|
816
|
+
* correlating your logs with distributed traces without any manual wiring.
|
|
817
|
+
* When OTel is not initialized, Logger gracefully falls back to `console.*`.
|
|
818
|
+
*
|
|
819
|
+
* Pass structured data as the second argument to any log method. Using an
|
|
820
|
+
* object of key-value pairs (instead of string interpolation) lets you
|
|
821
|
+
* filter, aggregate, and build dashboards in your observability backend.
|
|
822
|
+
*
|
|
823
|
+
* @example
|
|
824
|
+
* ```typescript
|
|
825
|
+
* import { Logger } from 'iii-sdk'
|
|
826
|
+
*
|
|
827
|
+
* const logger = new Logger()
|
|
828
|
+
*
|
|
829
|
+
* // Basic logging — trace context is injected automatically
|
|
830
|
+
* logger.info('Worker connected')
|
|
831
|
+
*
|
|
832
|
+
* // Structured context for dashboards and alerting
|
|
833
|
+
* logger.info('Order processed', { orderId: 'ord_123', amount: 49.99, currency: 'USD' })
|
|
834
|
+
* logger.warn('Retry attempt', { attempt: 3, maxRetries: 5, endpoint: '/api/charge' })
|
|
835
|
+
* logger.error('Payment failed', { orderId: 'ord_123', gateway: 'stripe', errorCode: 'card_declined' })
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
756
838
|
var Logger = class {
|
|
757
839
|
get otelLogger() {
|
|
758
840
|
if (!this._otelLogger) this._otelLogger = getLogger();
|
|
@@ -793,20 +875,72 @@ var Logger = class {
|
|
|
793
875
|
default: console.log(message, data);
|
|
794
876
|
}
|
|
795
877
|
}
|
|
878
|
+
/**
|
|
879
|
+
* Log an info-level message.
|
|
880
|
+
*
|
|
881
|
+
* @param message - Human-readable log message.
|
|
882
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
883
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
884
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
885
|
+
*
|
|
886
|
+
* @example
|
|
887
|
+
* ```typescript
|
|
888
|
+
* logger.info('Order processed', { orderId: 'ord_123', status: 'completed' })
|
|
889
|
+
* ```
|
|
890
|
+
*/
|
|
796
891
|
info(message, data) {
|
|
797
892
|
this.emit(message, SeverityNumber.INFO, data);
|
|
798
893
|
}
|
|
894
|
+
/**
|
|
895
|
+
* Log a warning-level message.
|
|
896
|
+
*
|
|
897
|
+
* @param message - Human-readable log message.
|
|
898
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
899
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
900
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
901
|
+
*
|
|
902
|
+
* @example
|
|
903
|
+
* ```typescript
|
|
904
|
+
* logger.warn('Retry attempt', { attempt: 3, maxRetries: 5, endpoint: '/api/charge' })
|
|
905
|
+
* ```
|
|
906
|
+
*/
|
|
799
907
|
warn(message, data) {
|
|
800
908
|
this.emit(message, SeverityNumber.WARN, data);
|
|
801
909
|
}
|
|
910
|
+
/**
|
|
911
|
+
* Log an error-level message.
|
|
912
|
+
*
|
|
913
|
+
* @param message - Human-readable log message.
|
|
914
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
915
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
916
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* ```typescript
|
|
920
|
+
* logger.error('Payment failed', { orderId: 'ord_123', gateway: 'stripe', errorCode: 'card_declined' })
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
802
923
|
error(message, data) {
|
|
803
924
|
this.emit(message, SeverityNumber.ERROR, data);
|
|
804
925
|
}
|
|
926
|
+
/**
|
|
927
|
+
* Log a debug-level message.
|
|
928
|
+
*
|
|
929
|
+
* @param message - Human-readable log message.
|
|
930
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
931
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
932
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
933
|
+
*
|
|
934
|
+
* @example
|
|
935
|
+
* ```typescript
|
|
936
|
+
* logger.debug('Cache lookup', { key: 'user:42', hit: false })
|
|
937
|
+
* ```
|
|
938
|
+
*/
|
|
805
939
|
debug(message, data) {
|
|
806
940
|
this.emit(message, SeverityNumber.DEBUG, data);
|
|
807
941
|
}
|
|
808
942
|
};
|
|
809
943
|
|
|
810
944
|
//#endregion
|
|
811
|
-
export { ChannelReader, ChannelWriter, Logger, http, registerWorker };
|
|
945
|
+
export { ChannelReader, ChannelWriter, Logger, TriggerAction, http, registerWorker };
|
|
812
946
|
//# sourceMappingURL=index.mjs.map
|