iii-sdk 0.8.2 → 0.9.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 +234 -105
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +182 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +182 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +234 -106
- package/dist/index.mjs.map +1 -1
- package/dist/state.cjs +1 -0
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +43 -1
- package/dist/state.d.cts.map +1 -1
- package/dist/state.d.mts +43 -1
- package/dist/state.d.mts.map +1 -1
- package/dist/state.mjs +1 -0
- package/dist/state.mjs.map +1 -1
- package/dist/stream-BAjVneSg.d.mts +176 -0
- package/dist/stream-BAjVneSg.d.mts.map +1 -0
- package/dist/stream-DUNsytJU.d.cts +176 -0
- package/dist/stream-DUNsytJU.d.cts.map +1 -0
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.mts +1 -1
- package/dist/telemetry.cjs +1 -1
- 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-coGqiBHT.mjs → utils-5jD8F3OE.mjs} +241 -212
- package/dist/utils-5jD8F3OE.mjs.map +1 -0
- package/dist/{utils-CMrMD5Ij.d.mts → utils-BRn3Iff5.d.mts} +586 -210
- package/dist/utils-BRn3Iff5.d.mts.map +1 -0
- package/dist/{utils-BaGgUfjl.d.cts → utils-D6RPYBhs.d.cts} +586 -210
- package/dist/utils-D6RPYBhs.d.cts.map +1 -0
- package/dist/{utils-BJTjoUdq.cjs → utils-bsZVduQi.cjs} +240 -211
- package/dist/utils-bsZVduQi.cjs.map +1 -0
- package/package.json +4 -2
- 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.map +0 -1
- 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-5jD8F3OE.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;
|
|
@@ -51,12 +68,14 @@ var ChannelWriter = class ChannelWriter {
|
|
|
51
68
|
if (!this.stream.destroyed) this.stream.destroy();
|
|
52
69
|
});
|
|
53
70
|
}
|
|
71
|
+
/** Send a text message through the channel. */
|
|
54
72
|
sendMessage(msg) {
|
|
55
73
|
this.ensureConnected();
|
|
56
74
|
this.sendRaw(msg, (err) => {
|
|
57
75
|
if (err) this.stream.destroy(err);
|
|
58
76
|
});
|
|
59
77
|
}
|
|
78
|
+
/** Close the channel writer. */
|
|
60
79
|
close() {
|
|
61
80
|
if (!this.ws) return;
|
|
62
81
|
if (this.wsReady) this.ws.close(1e3, "channel_close");
|
|
@@ -89,6 +108,21 @@ var ChannelWriter = class ChannelWriter {
|
|
|
89
108
|
});
|
|
90
109
|
}
|
|
91
110
|
};
|
|
111
|
+
/**
|
|
112
|
+
* Read end of a streaming channel. Provides both a Node.js `Readable` stream
|
|
113
|
+
* for binary data and an `onMessage` callback for structured text messages.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const channel = await iii.createChannel()
|
|
118
|
+
*
|
|
119
|
+
* // Stream binary data
|
|
120
|
+
* channel.reader.stream.on('data', (chunk) => console.log(chunk))
|
|
121
|
+
*
|
|
122
|
+
* // Or receive text messages
|
|
123
|
+
* channel.reader.onMessage((msg) => console.log('Got:', msg))
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
92
126
|
var ChannelReader = class {
|
|
93
127
|
constructor(engineWsBase, ref) {
|
|
94
128
|
this.ws = null;
|
|
@@ -131,9 +165,19 @@ var ChannelReader = class {
|
|
|
131
165
|
this.stream.destroy(err);
|
|
132
166
|
});
|
|
133
167
|
}
|
|
168
|
+
/** Register a callback to receive text messages from the channel. */
|
|
134
169
|
onMessage(callback) {
|
|
135
170
|
this.messageCallbacks.push(callback);
|
|
136
171
|
}
|
|
172
|
+
async readAll() {
|
|
173
|
+
this.ensureConnected();
|
|
174
|
+
const chunks = [];
|
|
175
|
+
for await (const chunk of this.stream) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
176
|
+
return Buffer.concat(chunks);
|
|
177
|
+
}
|
|
178
|
+
close() {
|
|
179
|
+
if (this.ws && this.ws.readyState !== WebSocket.CLOSED) this.ws.close(1e3, "channel_close");
|
|
180
|
+
}
|
|
137
181
|
};
|
|
138
182
|
function buildChannelUrl(engineWsBase, channelId, accessKey, direction) {
|
|
139
183
|
return `${engineWsBase.replace(/\/$/, "")}/ws/channels/${channelId}?key=${encodeURIComponent(accessKey)}&dir=${direction}`;
|
|
@@ -175,11 +219,9 @@ var Sdk = class {
|
|
|
175
219
|
this.triggers = /* @__PURE__ */ new Map();
|
|
176
220
|
this.triggerTypes = /* @__PURE__ */ new Map();
|
|
177
221
|
this.functionsAvailableCallbacks = /* @__PURE__ */ new Set();
|
|
178
|
-
this.logCallbacks = /* @__PURE__ */ new Map();
|
|
179
222
|
this.messagesToSend = [];
|
|
180
223
|
this.reconnectAttempt = 0;
|
|
181
224
|
this.connectionState = "disconnected";
|
|
182
|
-
this.stateCallbacks = /* @__PURE__ */ new Set();
|
|
183
225
|
this.isShuttingDown = false;
|
|
184
226
|
this.registerTriggerType = (triggerType, handler) => {
|
|
185
227
|
this.sendMessage(MessageType.RegisterTriggerType, triggerType, true);
|
|
@@ -191,9 +233,6 @@ var Sdk = class {
|
|
|
191
233
|
handler
|
|
192
234
|
});
|
|
193
235
|
};
|
|
194
|
-
this.on = (event, callback) => {
|
|
195
|
-
this.ws?.on(event, callback);
|
|
196
|
-
};
|
|
197
236
|
this.unregisterTriggerType = (triggerType) => {
|
|
198
237
|
this.sendMessage(MessageType.UnregisterTriggerType, triggerType, true);
|
|
199
238
|
this.triggerTypes.delete(triggerType.id);
|
|
@@ -264,14 +303,21 @@ var Sdk = class {
|
|
|
264
303
|
};
|
|
265
304
|
};
|
|
266
305
|
this.registerService = (message) => {
|
|
267
|
-
|
|
268
|
-
this.services.set(message.id, {
|
|
306
|
+
const msg = {
|
|
269
307
|
...message,
|
|
308
|
+
name: message.name ?? message.id
|
|
309
|
+
};
|
|
310
|
+
this.sendMessage(MessageType.RegisterService, msg, true);
|
|
311
|
+
this.services.set(message.id, {
|
|
312
|
+
...msg,
|
|
270
313
|
message_type: MessageType.RegisterService
|
|
271
314
|
});
|
|
272
315
|
};
|
|
273
316
|
this.createChannel = async (bufferSize) => {
|
|
274
|
-
const result = await this.
|
|
317
|
+
const result = await this.trigger({
|
|
318
|
+
function_id: "engine::channels::create",
|
|
319
|
+
payload: { buffer_size: bufferSize }
|
|
320
|
+
});
|
|
275
321
|
return {
|
|
276
322
|
writer: new ChannelWriter(this.address, result.writer),
|
|
277
323
|
reader: new ChannelReader(this.address, result.reader),
|
|
@@ -279,11 +325,24 @@ var Sdk = class {
|
|
|
279
325
|
readerRef: result.reader
|
|
280
326
|
};
|
|
281
327
|
};
|
|
282
|
-
this.trigger = async (
|
|
328
|
+
this.trigger = async (request) => {
|
|
329
|
+
const { function_id, payload, action, timeoutMs } = request;
|
|
330
|
+
const effectiveTimeout = timeoutMs ?? this.invocationTimeoutMs;
|
|
331
|
+
if (action?.type === "void") {
|
|
332
|
+
const traceparent$1 = injectTraceparent();
|
|
333
|
+
const baggage$1 = injectBaggage();
|
|
334
|
+
this.sendMessage(MessageType.InvokeFunction, {
|
|
335
|
+
function_id,
|
|
336
|
+
data: payload,
|
|
337
|
+
traceparent: traceparent$1,
|
|
338
|
+
baggage: baggage$1,
|
|
339
|
+
action
|
|
340
|
+
});
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
283
343
|
const invocation_id = crypto.randomUUID();
|
|
284
344
|
const traceparent = injectTraceparent();
|
|
285
345
|
const baggage = injectBaggage();
|
|
286
|
-
const effectiveTimeout = timeoutMs ?? this.invocationTimeoutMs;
|
|
287
346
|
return new Promise((resolve, reject) => {
|
|
288
347
|
const timeout = setTimeout(() => {
|
|
289
348
|
if (this.invocations.get(invocation_id)) {
|
|
@@ -305,29 +364,30 @@ var Sdk = class {
|
|
|
305
364
|
this.sendMessage(MessageType.InvokeFunction, {
|
|
306
365
|
invocation_id,
|
|
307
366
|
function_id,
|
|
308
|
-
data,
|
|
367
|
+
data: payload,
|
|
309
368
|
traceparent,
|
|
310
|
-
baggage
|
|
369
|
+
baggage,
|
|
370
|
+
action
|
|
311
371
|
});
|
|
312
372
|
});
|
|
313
373
|
};
|
|
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
374
|
this.listFunctions = async () => {
|
|
327
|
-
return (await this.trigger(
|
|
375
|
+
return (await this.trigger({
|
|
376
|
+
function_id: EngineFunctions.LIST_FUNCTIONS,
|
|
377
|
+
payload: {}
|
|
378
|
+
})).functions;
|
|
328
379
|
};
|
|
329
380
|
this.listWorkers = async () => {
|
|
330
|
-
return (await this.trigger(
|
|
381
|
+
return (await this.trigger({
|
|
382
|
+
function_id: EngineFunctions.LIST_WORKERS,
|
|
383
|
+
payload: {}
|
|
384
|
+
})).workers;
|
|
385
|
+
};
|
|
386
|
+
this.listTriggers = async (includeInternal = false) => {
|
|
387
|
+
return (await this.trigger({
|
|
388
|
+
function_id: EngineFunctions.LIST_TRIGGERS,
|
|
389
|
+
payload: { include_internal: includeInternal }
|
|
390
|
+
})).triggers;
|
|
331
391
|
};
|
|
332
392
|
this.createStream = (streamName, stream) => {
|
|
333
393
|
this.registerFunction({ id: `stream::get(${streamName})` }, stream.get.bind(stream));
|
|
@@ -361,48 +421,6 @@ var Sdk = class {
|
|
|
361
421
|
}
|
|
362
422
|
};
|
|
363
423
|
};
|
|
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
424
|
this.shutdown = async () => {
|
|
407
425
|
this.isShuttingDown = true;
|
|
408
426
|
this.stopMetricsReporting();
|
|
@@ -418,7 +436,6 @@ var Sdk = class {
|
|
|
418
436
|
this.ws.close();
|
|
419
437
|
this.ws = void 0;
|
|
420
438
|
}
|
|
421
|
-
this.stateCallbacks.clear();
|
|
422
439
|
this.setConnectionState("disconnected");
|
|
423
440
|
};
|
|
424
441
|
this.workerName = options?.workerName ?? getDefaultWorkerName();
|
|
@@ -437,29 +454,26 @@ var Sdk = class {
|
|
|
437
454
|
registerWorkerMetadata() {
|
|
438
455
|
const telemetryOpts = this.options?.telemetry;
|
|
439
456
|
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
|
-
|
|
457
|
+
this.trigger({
|
|
458
|
+
function_id: EngineFunctions.REGISTER_WORKER,
|
|
459
|
+
payload: {
|
|
460
|
+
runtime: "node",
|
|
461
|
+
version: SDK_VERSION,
|
|
462
|
+
name: this.workerName,
|
|
463
|
+
os: getOsInfo(),
|
|
464
|
+
pid: process.pid,
|
|
465
|
+
telemetry: {
|
|
466
|
+
language,
|
|
467
|
+
project_name: telemetryOpts?.project_name,
|
|
468
|
+
framework: telemetryOpts?.framework,
|
|
469
|
+
amplitude_api_key: telemetryOpts?.amplitude_api_key
|
|
470
|
+
}
|
|
471
|
+
},
|
|
472
|
+
action: { type: "void" }
|
|
452
473
|
});
|
|
453
474
|
}
|
|
454
475
|
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
|
-
}
|
|
476
|
+
if (this.connectionState !== state) this.connectionState = state;
|
|
463
477
|
}
|
|
464
478
|
connect() {
|
|
465
479
|
if (this.isShuttingDown) return;
|
|
@@ -604,18 +618,6 @@ var Sdk = class {
|
|
|
604
618
|
});
|
|
605
619
|
else console.error(`[iii] ${message}`, error ?? "");
|
|
606
620
|
}
|
|
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
621
|
onInvocationResult(invocation_id, result, error) {
|
|
620
622
|
const invocation = this.invocations.get(invocation_id);
|
|
621
623
|
if (invocation) {
|
|
@@ -658,12 +660,14 @@ var Sdk = class {
|
|
|
658
660
|
baggage: getResponseBaggage()
|
|
659
661
|
});
|
|
660
662
|
} catch (error) {
|
|
663
|
+
const isError = error instanceof Error;
|
|
661
664
|
this.sendMessage(MessageType.InvocationResult, {
|
|
662
665
|
invocation_id,
|
|
663
666
|
function_id,
|
|
664
667
|
error: {
|
|
665
668
|
code: "invocation_failed",
|
|
666
|
-
message: error.message
|
|
669
|
+
message: isError ? error.message : String(error),
|
|
670
|
+
stacktrace: isError ? error.stack : void 0
|
|
667
671
|
},
|
|
668
672
|
traceparent: getResponseTraceparent(),
|
|
669
673
|
baggage: getResponseBaggage()
|
|
@@ -749,10 +753,82 @@ var Sdk = class {
|
|
|
749
753
|
}
|
|
750
754
|
}
|
|
751
755
|
};
|
|
756
|
+
/**
|
|
757
|
+
* Factory object that constructs routing actions for {@link ISdk.trigger}.
|
|
758
|
+
*
|
|
759
|
+
* @example
|
|
760
|
+
* ```typescript
|
|
761
|
+
* import { TriggerAction } from 'iii-sdk'
|
|
762
|
+
*
|
|
763
|
+
* // Enqueue to a named queue
|
|
764
|
+
* iii.trigger({
|
|
765
|
+
* function_id: 'process',
|
|
766
|
+
* payload: { data: 'hello' },
|
|
767
|
+
* action: TriggerAction.Enqueue({ queue: 'jobs' }),
|
|
768
|
+
* })
|
|
769
|
+
*
|
|
770
|
+
* // Fire-and-forget
|
|
771
|
+
* iii.trigger({
|
|
772
|
+
* function_id: 'notify',
|
|
773
|
+
* payload: {},
|
|
774
|
+
* action: TriggerAction.Void(),
|
|
775
|
+
* })
|
|
776
|
+
* ```
|
|
777
|
+
*/
|
|
778
|
+
const TriggerAction = {
|
|
779
|
+
Enqueue: (opts) => ({
|
|
780
|
+
type: "enqueue",
|
|
781
|
+
...opts
|
|
782
|
+
}),
|
|
783
|
+
Void: () => ({ type: "void" })
|
|
784
|
+
};
|
|
785
|
+
/**
|
|
786
|
+
* Creates and returns a connected SDK instance. The WebSocket connection is
|
|
787
|
+
* established automatically -- there is no separate `connect()` call.
|
|
788
|
+
*
|
|
789
|
+
* @param address - WebSocket URL of the III engine (e.g. `ws://localhost:49134`).
|
|
790
|
+
* @param options - Optional {@link InitOptions} for worker name, timeouts, reconnection, and OTel.
|
|
791
|
+
* @returns A connected {@link ISdk} instance.
|
|
792
|
+
*
|
|
793
|
+
* @example
|
|
794
|
+
* ```typescript
|
|
795
|
+
* import { registerWorker } from 'iii-sdk'
|
|
796
|
+
*
|
|
797
|
+
* const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134', {
|
|
798
|
+
* workerName: 'my-worker',
|
|
799
|
+
* })
|
|
800
|
+
* ```
|
|
801
|
+
*/
|
|
752
802
|
const registerWorker = (address, options) => new Sdk(address, options);
|
|
753
803
|
|
|
754
804
|
//#endregion
|
|
755
805
|
//#region src/logger.ts
|
|
806
|
+
/**
|
|
807
|
+
* Structured logger that emits logs as OpenTelemetry LogRecords.
|
|
808
|
+
*
|
|
809
|
+
* Every log call automatically captures the active trace and span context,
|
|
810
|
+
* correlating your logs with distributed traces without any manual wiring.
|
|
811
|
+
* When OTel is not initialized, Logger gracefully falls back to `console.*`.
|
|
812
|
+
*
|
|
813
|
+
* Pass structured data as the second argument to any log method. Using an
|
|
814
|
+
* object of key-value pairs (instead of string interpolation) lets you
|
|
815
|
+
* filter, aggregate, and build dashboards in your observability backend.
|
|
816
|
+
*
|
|
817
|
+
* @example
|
|
818
|
+
* ```typescript
|
|
819
|
+
* import { Logger } from 'iii-sdk'
|
|
820
|
+
*
|
|
821
|
+
* const logger = new Logger()
|
|
822
|
+
*
|
|
823
|
+
* // Basic logging — trace context is injected automatically
|
|
824
|
+
* logger.info('Worker connected')
|
|
825
|
+
*
|
|
826
|
+
* // Structured context for dashboards and alerting
|
|
827
|
+
* logger.info('Order processed', { orderId: 'ord_123', amount: 49.99, currency: 'USD' })
|
|
828
|
+
* logger.warn('Retry attempt', { attempt: 3, maxRetries: 5, endpoint: '/api/charge' })
|
|
829
|
+
* logger.error('Payment failed', { orderId: 'ord_123', gateway: 'stripe', errorCode: 'card_declined' })
|
|
830
|
+
* ```
|
|
831
|
+
*/
|
|
756
832
|
var Logger = class {
|
|
757
833
|
get otelLogger() {
|
|
758
834
|
if (!this._otelLogger) this._otelLogger = getLogger();
|
|
@@ -793,20 +869,72 @@ var Logger = class {
|
|
|
793
869
|
default: console.log(message, data);
|
|
794
870
|
}
|
|
795
871
|
}
|
|
872
|
+
/**
|
|
873
|
+
* Log an info-level message.
|
|
874
|
+
*
|
|
875
|
+
* @param message - Human-readable log message.
|
|
876
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
877
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
878
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
879
|
+
*
|
|
880
|
+
* @example
|
|
881
|
+
* ```typescript
|
|
882
|
+
* logger.info('Order processed', { orderId: 'ord_123', status: 'completed' })
|
|
883
|
+
* ```
|
|
884
|
+
*/
|
|
796
885
|
info(message, data) {
|
|
797
886
|
this.emit(message, SeverityNumber.INFO, data);
|
|
798
887
|
}
|
|
888
|
+
/**
|
|
889
|
+
* Log a warning-level message.
|
|
890
|
+
*
|
|
891
|
+
* @param message - Human-readable log message.
|
|
892
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
893
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
894
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* ```typescript
|
|
898
|
+
* logger.warn('Retry attempt', { attempt: 3, maxRetries: 5, endpoint: '/api/charge' })
|
|
899
|
+
* ```
|
|
900
|
+
*/
|
|
799
901
|
warn(message, data) {
|
|
800
902
|
this.emit(message, SeverityNumber.WARN, data);
|
|
801
903
|
}
|
|
904
|
+
/**
|
|
905
|
+
* Log an error-level message.
|
|
906
|
+
*
|
|
907
|
+
* @param message - Human-readable log message.
|
|
908
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
909
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
910
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
911
|
+
*
|
|
912
|
+
* @example
|
|
913
|
+
* ```typescript
|
|
914
|
+
* logger.error('Payment failed', { orderId: 'ord_123', gateway: 'stripe', errorCode: 'card_declined' })
|
|
915
|
+
* ```
|
|
916
|
+
*/
|
|
802
917
|
error(message, data) {
|
|
803
918
|
this.emit(message, SeverityNumber.ERROR, data);
|
|
804
919
|
}
|
|
920
|
+
/**
|
|
921
|
+
* Log a debug-level message.
|
|
922
|
+
*
|
|
923
|
+
* @param message - Human-readable log message.
|
|
924
|
+
* @param data - Structured context attached as OTel log attributes.
|
|
925
|
+
* Use key-value objects to enable filtering and aggregation in your
|
|
926
|
+
* observability backend (e.g. Grafana, Datadog, New Relic).
|
|
927
|
+
*
|
|
928
|
+
* @example
|
|
929
|
+
* ```typescript
|
|
930
|
+
* logger.debug('Cache lookup', { key: 'user:42', hit: false })
|
|
931
|
+
* ```
|
|
932
|
+
*/
|
|
805
933
|
debug(message, data) {
|
|
806
934
|
this.emit(message, SeverityNumber.DEBUG, data);
|
|
807
935
|
}
|
|
808
936
|
};
|
|
809
937
|
|
|
810
938
|
//#endregion
|
|
811
|
-
export { ChannelReader, ChannelWriter, Logger, http, registerWorker };
|
|
939
|
+
export { ChannelReader, ChannelWriter, Logger, TriggerAction, http, registerWorker };
|
|
812
940
|
//# sourceMappingURL=index.mjs.map
|