iii-sdk 0.12.0-next.1 → 0.13.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.
- package/LICENSE.spdx +13 -10
- package/README.md +17 -28
- package/dist/index.cjs +119 -123
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +29 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +95 -74
- package/dist/index.mjs.map +1 -1
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +8 -1
- package/dist/state.d.cts.map +1 -1
- package/dist/state.d.mts +8 -1
- package/dist/state.d.mts.map +1 -1
- package/dist/state.mjs.map +1 -1
- package/dist/{stream-BkrU83KD.d.mts → stream-0aOn-1J0.d.cts} +99 -11
- package/dist/stream-0aOn-1J0.d.cts.map +1 -0
- package/dist/{stream-CJdvFip3.d.cts → stream-R8MrPAf1.d.mts} +99 -11
- package/dist/stream-R8MrPAf1.d.mts.map +1 -0
- package/dist/stream.d.cts +2 -2
- package/dist/stream.d.mts +2 -2
- package/dist/{utils-zW5CJdUp.cjs → telemetry-system-BGZS9Vit.cjs} +362 -78
- package/dist/telemetry-system-BGZS9Vit.cjs.map +1 -0
- package/dist/{utils-D15-mMqf.mjs → telemetry-system-DMHiN8V9.mjs} +262 -78
- package/dist/telemetry-system-DMHiN8V9.mjs.map +1 -0
- package/dist/telemetry.cjs +35 -30
- package/dist/telemetry.d.cts +2 -2
- package/dist/telemetry.d.mts +2 -2
- package/dist/telemetry.mjs +2 -2
- package/dist/{utils-BkIiccFQ.d.cts → utils-DhgysSJS.d.cts} +62 -131
- package/dist/utils-DhgysSJS.d.cts.map +1 -0
- package/dist/{utils-B6IYL9lX.d.mts → utils-oTg3ZAbD.d.mts} +62 -131
- package/dist/utils-oTg3ZAbD.d.mts.map +1 -0
- package/package.json +1 -1
- package/dist/stream-BkrU83KD.d.mts.map +0 -1
- package/dist/stream-CJdvFip3.d.cts.map +0 -1
- package/dist/utils-B6IYL9lX.d.mts.map +0 -1
- package/dist/utils-BkIiccFQ.d.cts.map +0 -1
- package/dist/utils-D15-mMqf.mjs.map +0 -1
- package/dist/utils-zW5CJdUp.cjs.map +0 -1
package/LICENSE.spdx
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
SPDXVersion: SPDX-2.
|
|
1
|
+
SPDXVersion: SPDX-2.3
|
|
2
2
|
DataLicense: CC0-1.0
|
|
3
|
+
SPDXID: SPDXRef-DOCUMENT
|
|
4
|
+
DocumentName: iii
|
|
5
|
+
DocumentNamespace: https://iii.dev/spdx/iii
|
|
3
6
|
PackageName: iii
|
|
4
|
-
|
|
7
|
+
SPDXID: SPDXRef-Package
|
|
5
8
|
PackageSupplier: Organization: Motia LLC
|
|
6
9
|
PackageHomePage: https://iii.dev
|
|
7
|
-
PackageLicenseDeclared: Elastic-2.0
|
|
8
|
-
|
|
10
|
+
PackageLicenseDeclared: Apache-2.0 AND Elastic-2.0
|
|
11
|
+
PackageLicenseConcluded: Apache-2.0 AND Elastic-2.0
|
|
9
12
|
PackageCopyrightText: 2024-present, Motia LLC
|
|
10
|
-
PackageSummary: <text>iii
|
|
11
|
-
|
|
12
|
-
and
|
|
13
|
+
PackageSummary: <text>iii turns distributed backend complexity into a
|
|
14
|
+
simple set of real-time, interoperable primitives called Workers,
|
|
15
|
+
Triggers, and Functions. The result is coordinated execution
|
|
16
|
+
that behaves as if it were a single runtime.
|
|
13
17
|
</text>
|
|
14
|
-
PackageComment: <text>
|
|
15
|
-
|
|
16
|
-
are licensed under Apache License 2.0.
|
|
18
|
+
PackageComment: <text>Inbound contributions from external
|
|
19
|
+
contributors are accepted under Apache-2.0 only; see CONTRIBUTING.md.
|
|
17
20
|
</text>
|
|
18
21
|
Created: 2024-01-01T00:00:00Z
|
|
19
22
|
PackageDownloadLocation: git://github.com/iii-hq/iii
|
package/README.md
CHANGED
|
@@ -8,7 +8,8 @@ Node.js / TypeScript SDK for the [iii engine](https://github.com/iii-hq/iii).
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
10
10
|
```bash
|
|
11
|
-
|
|
11
|
+
pnpm add iii-sdk
|
|
12
|
+
# or: npm install iii-sdk
|
|
12
13
|
```
|
|
13
14
|
|
|
14
15
|
## Hello World
|
|
@@ -18,33 +19,34 @@ import { registerWorker } from 'iii-sdk'
|
|
|
18
19
|
|
|
19
20
|
const iii = registerWorker('ws://localhost:49134')
|
|
20
21
|
|
|
21
|
-
iii.registerFunction('greet', async (input) => {
|
|
22
|
+
iii.registerFunction('hello::greet', async (input) => {
|
|
22
23
|
return { message: `Hello, ${input.name}!` }
|
|
23
24
|
})
|
|
24
25
|
|
|
25
26
|
iii.registerTrigger({
|
|
26
27
|
type: 'http',
|
|
27
|
-
function_id: 'greet',
|
|
28
|
+
function_id: 'hello::greet',
|
|
28
29
|
config: { api_path: '/greet', http_method: 'POST' },
|
|
29
30
|
})
|
|
30
31
|
|
|
31
|
-
const result = await iii.trigger({ function_id: 'greet', payload: { name: 'world' } })
|
|
32
|
+
const result = await iii.trigger({ function_id: 'hello::greet', payload: { name: 'world' } })
|
|
32
33
|
```
|
|
33
34
|
|
|
34
35
|
## API
|
|
35
36
|
|
|
36
|
-
| Operation | Signature
|
|
37
|
-
| ------------------------ |
|
|
38
|
-
| Initialize | `registerWorker(url, options?)`
|
|
39
|
-
| Register function | `iii.registerFunction(id, handler, options?)`
|
|
40
|
-
| Register trigger | `iii.registerTrigger({ type, function_id, config })`
|
|
41
|
-
| Invoke (await) | `await iii.trigger({ function_id, payload })`
|
|
42
|
-
| Invoke (fire-and-forget) | `iii.trigger({ function_id, payload, action: TriggerAction.Void() })`
|
|
37
|
+
| Operation | Signature | Description |
|
|
38
|
+
| ------------------------ | --------------------------------------------------------------------------------- | ------------------------------------------------------------ |
|
|
39
|
+
| Initialize | `registerWorker(url, options?)` | Create and connect to the engine. Returns an `ISdk` instance |
|
|
40
|
+
| Register function | `iii.registerFunction(id, handler, options?)` | Register a function that can be invoked by name |
|
|
41
|
+
| Register trigger | `iii.registerTrigger({ type, function_id, config })` | Bind a trigger (HTTP, cron, queue, etc.) to a function |
|
|
42
|
+
| Invoke (await) | `await iii.trigger({ function_id, payload })` | Invoke a function and wait for the result |
|
|
43
|
+
| Invoke (fire-and-forget) | `iii.trigger({ function_id, payload, action: TriggerAction.Void() })` | Invoke without waiting |
|
|
44
|
+
| Invoke (enqueue) | `iii.trigger({ function_id, payload, action: TriggerAction.Enqueue({ queue }) })` | Route invocation through a named queue |
|
|
43
45
|
|
|
44
46
|
### Registering Functions
|
|
45
47
|
|
|
46
48
|
```javascript
|
|
47
|
-
iii.registerFunction('orders
|
|
49
|
+
iii.registerFunction('orders::create', async (input) => {
|
|
48
50
|
return { status_code: 201, body: { id: '123', item: input.body.item } }
|
|
49
51
|
})
|
|
50
52
|
```
|
|
@@ -54,7 +56,7 @@ iii.registerFunction('orders.create', async (input) => {
|
|
|
54
56
|
```javascript
|
|
55
57
|
iii.registerTrigger({
|
|
56
58
|
type: 'http',
|
|
57
|
-
function_id: 'orders
|
|
59
|
+
function_id: 'orders::create',
|
|
58
60
|
config: { api_path: '/orders', http_method: 'POST' },
|
|
59
61
|
})
|
|
60
62
|
```
|
|
@@ -66,24 +68,11 @@ import { registerWorker, TriggerAction } from 'iii-sdk'
|
|
|
66
68
|
|
|
67
69
|
const iii = registerWorker('ws://localhost:49134')
|
|
68
70
|
|
|
69
|
-
const result = await iii.trigger({ function_id: 'orders
|
|
71
|
+
const result = await iii.trigger({ function_id: 'orders::create', payload: { item: 'widget' } })
|
|
70
72
|
|
|
71
|
-
iii.trigger({ function_id: 'analytics
|
|
73
|
+
iii.trigger({ function_id: 'analytics::track', payload: { event: 'page_view' }, action: TriggerAction.Void() })
|
|
72
74
|
```
|
|
73
75
|
|
|
74
|
-
## Node Modules
|
|
75
|
-
|
|
76
|
-
| Import | What it provides |
|
|
77
|
-
| ------------------- | ------------------------------------- |
|
|
78
|
-
| `iii-sdk` | Core SDK (`registerWorker`, types) |
|
|
79
|
-
| `iii-sdk/stream` | Stream client for real-time state |
|
|
80
|
-
| `iii-sdk/state` | State client for key-value operations |
|
|
81
|
-
| `iii-sdk/telemetry` | OpenTelemetry integration |
|
|
82
|
-
|
|
83
|
-
## Removed methods
|
|
84
|
-
|
|
85
|
-
`call`, `callVoid`, and `triggerVoid` have been removed. Use `trigger()` for all invocations. For fire-and-forget, use `trigger({ function_id, payload, action: TriggerAction.Void() })`.
|
|
86
|
-
|
|
87
76
|
## Resources
|
|
88
77
|
|
|
89
78
|
- [Documentation](https://iii.dev/docs)
|
package/dist/index.cjs
CHANGED
|
@@ -1,38 +1,11 @@
|
|
|
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
|
-
const require_utils = require('./utils-zW5CJdUp.cjs');
|
|
2
|
+
const require_telemetry_system = require('./telemetry-system-BGZS9Vit.cjs');
|
|
30
3
|
let node_stream = require("node:stream");
|
|
31
4
|
let ws = require("ws");
|
|
32
5
|
let _opentelemetry_api = require("@opentelemetry/api");
|
|
33
6
|
let node_module = require("node:module");
|
|
34
7
|
let node_os = require("node:os");
|
|
35
|
-
node_os = __toESM(node_os);
|
|
8
|
+
node_os = require_telemetry_system.__toESM(node_os);
|
|
36
9
|
let _opentelemetry_api_logs = require("@opentelemetry/api-logs");
|
|
37
10
|
|
|
38
11
|
//#region src/channels.ts
|
|
@@ -218,12 +191,34 @@ function buildChannelUrl(engineWsBase, channelId, accessKey, direction) {
|
|
|
218
191
|
return `${engineWsBase.replace(/\/$/, "")}/ws/channels/${channelId}?key=${encodeURIComponent(accessKey)}&dir=${direction}`;
|
|
219
192
|
}
|
|
220
193
|
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/errors.ts
|
|
196
|
+
var IIIInvocationError = class extends Error {
|
|
197
|
+
constructor(init) {
|
|
198
|
+
super(`${init.code}: ${init.message}`);
|
|
199
|
+
this.name = "IIIInvocationError";
|
|
200
|
+
this.code = init.code;
|
|
201
|
+
this.function_id = init.function_id;
|
|
202
|
+
this.stacktrace = init.stacktrace;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* True when `value` looks like the wire `ErrorBody` the engine sends in
|
|
207
|
+
* `InvocationResult.error`: `{ code: string, message: string, stacktrace?: string }`.
|
|
208
|
+
* Used to distinguish an engine rejection (which we wrap in
|
|
209
|
+
* {@link IIIInvocationError}) from a JS `Error` thrown elsewhere.
|
|
210
|
+
*/
|
|
211
|
+
function isErrorBody(value) {
|
|
212
|
+
if (typeof value !== "object" || value === null) return false;
|
|
213
|
+
const v = value;
|
|
214
|
+
return typeof v.code === "string" && typeof v.message === "string" && (v.stacktrace === void 0 || typeof v.stacktrace === "string");
|
|
215
|
+
}
|
|
216
|
+
|
|
221
217
|
//#endregion
|
|
222
218
|
//#region src/iii-types.ts
|
|
223
219
|
let MessageType = /* @__PURE__ */ function(MessageType) {
|
|
224
220
|
MessageType["RegisterFunction"] = "registerfunction";
|
|
225
221
|
MessageType["UnregisterFunction"] = "unregisterfunction";
|
|
226
|
-
MessageType["RegisterService"] = "registerservice";
|
|
227
222
|
MessageType["InvokeFunction"] = "invokefunction";
|
|
228
223
|
MessageType["InvocationResult"] = "invocationresult";
|
|
229
224
|
MessageType["RegisterTriggerType"] = "registertriggertype";
|
|
@@ -249,11 +244,9 @@ var Sdk = class {
|
|
|
249
244
|
this.address = address;
|
|
250
245
|
this.options = options;
|
|
251
246
|
this.functions = /* @__PURE__ */ new Map();
|
|
252
|
-
this.services = /* @__PURE__ */ new Map();
|
|
253
247
|
this.invocations = /* @__PURE__ */ new Map();
|
|
254
248
|
this.triggers = /* @__PURE__ */ new Map();
|
|
255
249
|
this.triggerTypes = /* @__PURE__ */ new Map();
|
|
256
|
-
this.functionsAvailableCallbacks = /* @__PURE__ */ new Set();
|
|
257
250
|
this.messagesToSend = [];
|
|
258
251
|
this.reconnectAttempt = 0;
|
|
259
252
|
this.connectionState = "disconnected";
|
|
@@ -340,9 +333,42 @@ var Sdk = class {
|
|
|
340
333
|
this.functions.set(functionId, {
|
|
341
334
|
message: fullMessage,
|
|
342
335
|
handler: async (input, traceparent, baggage) => {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
336
|
+
const tracePayloads = !(process.env.III_DISABLE_TRACE_PAYLOADS === "1" || process.env.III_DISABLE_TRACE_PAYLOADS?.toLowerCase() === "true");
|
|
337
|
+
const payloadMaxBytes = require_telemetry_system.resolveMaxBytesFromEnv();
|
|
338
|
+
const runHandler = async () => {
|
|
339
|
+
if (tracePayloads) {
|
|
340
|
+
const { json, truncated } = require_telemetry_system.redactAndTruncate(input, payloadMaxBytes);
|
|
341
|
+
require_telemetry_system.recordSpanEvent("iii.invocation.input", {
|
|
342
|
+
"iii.payload.json": json,
|
|
343
|
+
"iii.payload.truncated": truncated
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
try {
|
|
347
|
+
const result = await handler(input);
|
|
348
|
+
if (tracePayloads) {
|
|
349
|
+
const { json, truncated } = require_telemetry_system.redactAndTruncate(result, payloadMaxBytes);
|
|
350
|
+
require_telemetry_system.recordSpanEvent("iii.invocation.output", {
|
|
351
|
+
"iii.payload.json": json,
|
|
352
|
+
"iii.payload.truncated": truncated,
|
|
353
|
+
"iii.payload.ok": true
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
return result;
|
|
357
|
+
} catch (err) {
|
|
358
|
+
if (tracePayloads) {
|
|
359
|
+
const { json, truncated } = require_telemetry_system.redactAndTruncate({ error: err instanceof Error ? err.message : String(err) }, payloadMaxBytes);
|
|
360
|
+
require_telemetry_system.recordSpanEvent("iii.invocation.output", {
|
|
361
|
+
"iii.payload.json": json,
|
|
362
|
+
"iii.payload.truncated": truncated,
|
|
363
|
+
"iii.payload.ok": false
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
throw err;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
if (require_telemetry_system.getTracer()) {
|
|
370
|
+
const parentContext = require_telemetry_system.extractContext(traceparent, baggage);
|
|
371
|
+
return _opentelemetry_api.context.with(parentContext, () => require_telemetry_system.withSpan(`call ${functionId}`, { kind: _opentelemetry_api.SpanKind.SERVER }, async () => await runHandler()));
|
|
346
372
|
}
|
|
347
373
|
const traceId = crypto.randomUUID().replace(/-/g, "");
|
|
348
374
|
const spanId = crypto.randomUUID().replace(/-/g, "").slice(0, 16);
|
|
@@ -351,7 +377,7 @@ var Sdk = class {
|
|
|
351
377
|
spanId,
|
|
352
378
|
traceFlags: 1
|
|
353
379
|
});
|
|
354
|
-
return _opentelemetry_api.context.with(_opentelemetry_api.trace.setSpan(_opentelemetry_api.context.active(), syntheticSpan), async () => await
|
|
380
|
+
return _opentelemetry_api.context.with(_opentelemetry_api.trace.setSpan(_opentelemetry_api.context.active(), syntheticSpan), async () => await runHandler());
|
|
355
381
|
}
|
|
356
382
|
});
|
|
357
383
|
} else this.functions.set(functionId, { message: fullMessage });
|
|
@@ -363,17 +389,6 @@ var Sdk = class {
|
|
|
363
389
|
}
|
|
364
390
|
};
|
|
365
391
|
};
|
|
366
|
-
this.registerService = (message) => {
|
|
367
|
-
const msg = {
|
|
368
|
-
...message,
|
|
369
|
-
name: message.name ?? message.id
|
|
370
|
-
};
|
|
371
|
-
this.sendMessage(MessageType.RegisterService, msg, true);
|
|
372
|
-
this.services.set(message.id, {
|
|
373
|
-
...msg,
|
|
374
|
-
message_type: MessageType.RegisterService
|
|
375
|
-
});
|
|
376
|
-
};
|
|
377
392
|
this.createChannel = async (bufferSize) => {
|
|
378
393
|
const result = await this.trigger({
|
|
379
394
|
function_id: "engine::channels::create",
|
|
@@ -390,8 +405,8 @@ var Sdk = class {
|
|
|
390
405
|
const { function_id, payload, action, timeoutMs } = request;
|
|
391
406
|
const effectiveTimeout = timeoutMs ?? this.invocationTimeoutMs;
|
|
392
407
|
if (action?.type === "void") {
|
|
393
|
-
const traceparent =
|
|
394
|
-
const baggage =
|
|
408
|
+
const traceparent = require_telemetry_system.injectTraceparent();
|
|
409
|
+
const baggage = require_telemetry_system.injectBaggage();
|
|
395
410
|
this.sendMessage(MessageType.InvokeFunction, {
|
|
396
411
|
function_id,
|
|
397
412
|
data: payload,
|
|
@@ -402,13 +417,17 @@ var Sdk = class {
|
|
|
402
417
|
return;
|
|
403
418
|
}
|
|
404
419
|
const invocation_id = crypto.randomUUID();
|
|
405
|
-
const traceparent =
|
|
406
|
-
const baggage =
|
|
420
|
+
const traceparent = require_telemetry_system.injectTraceparent();
|
|
421
|
+
const baggage = require_telemetry_system.injectBaggage();
|
|
407
422
|
return new Promise((resolve, reject) => {
|
|
408
423
|
const timeout = setTimeout(() => {
|
|
409
424
|
if (this.invocations.get(invocation_id)) {
|
|
410
425
|
this.invocations.delete(invocation_id);
|
|
411
|
-
reject(
|
|
426
|
+
reject(new IIIInvocationError({
|
|
427
|
+
code: "TIMEOUT",
|
|
428
|
+
message: `invocation timed out after ${effectiveTimeout}ms`,
|
|
429
|
+
function_id
|
|
430
|
+
}));
|
|
412
431
|
}
|
|
413
432
|
}, effectiveTimeout);
|
|
414
433
|
this.invocations.set(invocation_id, {
|
|
@@ -420,6 +439,7 @@ var Sdk = class {
|
|
|
420
439
|
clearTimeout(timeout);
|
|
421
440
|
reject(error);
|
|
422
441
|
},
|
|
442
|
+
function_id,
|
|
423
443
|
timeout
|
|
424
444
|
});
|
|
425
445
|
this.sendMessage(MessageType.InvokeFunction, {
|
|
@@ -432,30 +452,6 @@ var Sdk = class {
|
|
|
432
452
|
});
|
|
433
453
|
});
|
|
434
454
|
};
|
|
435
|
-
this.listFunctions = async () => {
|
|
436
|
-
return (await this.trigger({
|
|
437
|
-
function_id: require_utils.EngineFunctions.LIST_FUNCTIONS,
|
|
438
|
-
payload: {}
|
|
439
|
-
})).functions;
|
|
440
|
-
};
|
|
441
|
-
this.listWorkers = async () => {
|
|
442
|
-
return (await this.trigger({
|
|
443
|
-
function_id: require_utils.EngineFunctions.LIST_WORKERS,
|
|
444
|
-
payload: {}
|
|
445
|
-
})).workers;
|
|
446
|
-
};
|
|
447
|
-
this.listTriggers = async (includeInternal = false) => {
|
|
448
|
-
return (await this.trigger({
|
|
449
|
-
function_id: require_utils.EngineFunctions.LIST_TRIGGERS,
|
|
450
|
-
payload: { include_internal: includeInternal }
|
|
451
|
-
})).triggers;
|
|
452
|
-
};
|
|
453
|
-
this.listTriggerTypes = async (includeInternal = false) => {
|
|
454
|
-
return (await this.trigger({
|
|
455
|
-
function_id: require_utils.EngineFunctions.LIST_TRIGGER_TYPES,
|
|
456
|
-
payload: { include_internal: includeInternal }
|
|
457
|
-
})).trigger_types;
|
|
458
|
-
};
|
|
459
455
|
this.createStream = (streamName, stream) => {
|
|
460
456
|
this.registerFunction(`stream::get(${streamName})`, stream.get.bind(stream));
|
|
461
457
|
this.registerFunction(`stream::set(${streamName})`, stream.set.bind(stream));
|
|
@@ -463,35 +459,10 @@ var Sdk = class {
|
|
|
463
459
|
this.registerFunction(`stream::list(${streamName})`, stream.list.bind(stream));
|
|
464
460
|
this.registerFunction(`stream::list_groups(${streamName})`, stream.listGroups.bind(stream));
|
|
465
461
|
};
|
|
466
|
-
this.onFunctionsAvailable = (callback) => {
|
|
467
|
-
this.functionsAvailableCallbacks.add(callback);
|
|
468
|
-
if (!this.functionsAvailableTrigger) {
|
|
469
|
-
if (!this.functionsAvailableFunctionPath) this.functionsAvailableFunctionPath = `engine.on_functions_available.${crypto.randomUUID()}`;
|
|
470
|
-
const function_id = this.functionsAvailableFunctionPath;
|
|
471
|
-
if (!this.functions.has(function_id)) this.registerFunction(function_id, async ({ functions }) => {
|
|
472
|
-
this.functionsAvailableCallbacks.forEach((handler) => {
|
|
473
|
-
handler(functions);
|
|
474
|
-
});
|
|
475
|
-
return null;
|
|
476
|
-
});
|
|
477
|
-
this.functionsAvailableTrigger = this.registerTrigger({
|
|
478
|
-
type: require_utils.EngineTriggers.FUNCTIONS_AVAILABLE,
|
|
479
|
-
function_id,
|
|
480
|
-
config: {}
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
return () => {
|
|
484
|
-
this.functionsAvailableCallbacks.delete(callback);
|
|
485
|
-
if (this.functionsAvailableCallbacks.size === 0 && this.functionsAvailableTrigger) {
|
|
486
|
-
this.functionsAvailableTrigger.unregister();
|
|
487
|
-
this.functionsAvailableTrigger = void 0;
|
|
488
|
-
}
|
|
489
|
-
};
|
|
490
|
-
};
|
|
491
462
|
this.shutdown = async () => {
|
|
492
463
|
this.isShuttingDown = true;
|
|
493
464
|
this.stopMetricsReporting();
|
|
494
|
-
await
|
|
465
|
+
await require_telemetry_system.shutdownOtel();
|
|
495
466
|
this.clearReconnectTimeout();
|
|
496
467
|
for (const [_id, invocation] of this.invocations) {
|
|
497
468
|
if (invocation.timeout) clearTimeout(invocation.timeout);
|
|
@@ -500,7 +471,10 @@ var Sdk = class {
|
|
|
500
471
|
this.invocations.clear();
|
|
501
472
|
if (this.ws) {
|
|
502
473
|
this.ws.removeAllListeners();
|
|
503
|
-
this.ws.
|
|
474
|
+
this.ws.on("error", () => {});
|
|
475
|
+
try {
|
|
476
|
+
this.ws.close();
|
|
477
|
+
} catch {}
|
|
504
478
|
this.ws = void 0;
|
|
505
479
|
}
|
|
506
480
|
this.setConnectionState("disconnected");
|
|
@@ -509,10 +483,10 @@ var Sdk = class {
|
|
|
509
483
|
this.metricsReportingEnabled = options?.enableMetricsReporting ?? true;
|
|
510
484
|
this.invocationTimeoutMs = options?.invocationTimeoutMs ?? 3e4;
|
|
511
485
|
this.reconnectionConfig = {
|
|
512
|
-
...
|
|
486
|
+
...require_telemetry_system.DEFAULT_BRIDGE_RECONNECTION_CONFIG,
|
|
513
487
|
...options?.reconnectionConfig
|
|
514
488
|
};
|
|
515
|
-
|
|
489
|
+
require_telemetry_system.initOtel({
|
|
516
490
|
...options?.otel,
|
|
517
491
|
engineWsUrl: this.address
|
|
518
492
|
});
|
|
@@ -522,16 +496,17 @@ var Sdk = class {
|
|
|
522
496
|
const telemetryOpts = this.options?.telemetry;
|
|
523
497
|
const language = telemetryOpts?.language ?? Intl.DateTimeFormat().resolvedOptions().locale ?? process.env.LANG?.split(".")[0];
|
|
524
498
|
this.trigger({
|
|
525
|
-
function_id:
|
|
499
|
+
function_id: require_telemetry_system.EngineFunctions.REGISTER_WORKER,
|
|
526
500
|
payload: {
|
|
527
501
|
runtime: "node",
|
|
528
502
|
version: SDK_VERSION,
|
|
529
503
|
name: this.workerName,
|
|
530
504
|
os: getOsInfo(),
|
|
531
505
|
pid: process.pid,
|
|
506
|
+
isolation: process.env.III_ISOLATION || null,
|
|
532
507
|
telemetry: {
|
|
533
508
|
language,
|
|
534
|
-
project_name: telemetryOpts?.project_name,
|
|
509
|
+
project_name: telemetryOpts?.project_name ?? require_telemetry_system.detectProjectName(),
|
|
535
510
|
framework: telemetryOpts?.framework?.trim() || "iii-node",
|
|
536
511
|
amplitude_api_key: telemetryOpts?.amplitude_api_key
|
|
537
512
|
}
|
|
@@ -582,18 +557,18 @@ var Sdk = class {
|
|
|
582
557
|
}
|
|
583
558
|
startMetricsReporting() {
|
|
584
559
|
if (!this.metricsReportingEnabled || !this.workerId) return;
|
|
585
|
-
const meter =
|
|
560
|
+
const meter = require_telemetry_system.getMeter();
|
|
586
561
|
if (!meter) {
|
|
587
562
|
console.warn("[iii] Worker metrics disabled: OpenTelemetry not initialized. Call initOtel() with metricsEnabled: true before creating the iii.");
|
|
588
563
|
return;
|
|
589
564
|
}
|
|
590
|
-
|
|
565
|
+
require_telemetry_system.registerWorkerGauges(meter, {
|
|
591
566
|
workerId: this.workerId,
|
|
592
567
|
workerName: this.workerName
|
|
593
568
|
});
|
|
594
569
|
}
|
|
595
570
|
stopMetricsReporting() {
|
|
596
|
-
|
|
571
|
+
require_telemetry_system.stopWorkerGauges();
|
|
597
572
|
}
|
|
598
573
|
onSocketClose() {
|
|
599
574
|
this.ws?.removeAllListeners();
|
|
@@ -611,9 +586,6 @@ var Sdk = class {
|
|
|
611
586
|
this.triggerTypes.forEach(({ message }) => {
|
|
612
587
|
this.sendMessage(MessageType.RegisterTriggerType, message, true);
|
|
613
588
|
});
|
|
614
|
-
this.services.forEach((service) => {
|
|
615
|
-
this.sendMessage(MessageType.RegisterService, service, true);
|
|
616
|
-
});
|
|
617
589
|
this.functions.forEach(({ message }) => {
|
|
618
590
|
this.sendMessage(MessageType.RegisterFunction, message, true);
|
|
619
591
|
});
|
|
@@ -677,7 +649,7 @@ var Sdk = class {
|
|
|
677
649
|
else if (!skipIfClosed) this.messagesToSend.push(wireMessage);
|
|
678
650
|
}
|
|
679
651
|
logError(message, error) {
|
|
680
|
-
const otelLogger =
|
|
652
|
+
const otelLogger = require_telemetry_system.getLogger();
|
|
681
653
|
const errorMessage = error instanceof Error ? error.message : String(error ?? "");
|
|
682
654
|
if (otelLogger) otelLogger.emit({
|
|
683
655
|
severityNumber: _opentelemetry_api_logs.SeverityNumber.ERROR,
|
|
@@ -689,12 +661,34 @@ var Sdk = class {
|
|
|
689
661
|
const invocation = this.invocations.get(invocation_id);
|
|
690
662
|
if (invocation) {
|
|
691
663
|
if (invocation.timeout) clearTimeout(invocation.timeout);
|
|
692
|
-
error
|
|
664
|
+
if (error) invocation.reject(this.toInvocationError(error, invocation.function_id));
|
|
665
|
+
else invocation.resolve(result);
|
|
693
666
|
}
|
|
694
667
|
this.invocations.delete(invocation_id);
|
|
695
668
|
}
|
|
669
|
+
/**
|
|
670
|
+
* Wrap a wire-format `ErrorBody` in {@link IIIInvocationError} so callers get
|
|
671
|
+
* a real `Error` with a readable `.message` and a typed `.code`. Pass-through
|
|
672
|
+
* for values that are already `Error` subclasses. Everything else is wrapped
|
|
673
|
+
* under an `UNKNOWN` code so `String(err) !== '[object Object]'` holds for
|
|
674
|
+
* every rejection path.
|
|
675
|
+
*/
|
|
676
|
+
toInvocationError(error, function_id) {
|
|
677
|
+
if (error instanceof Error) return error;
|
|
678
|
+
if (isErrorBody(error)) return new IIIInvocationError({
|
|
679
|
+
code: error.code,
|
|
680
|
+
message: error.message,
|
|
681
|
+
function_id,
|
|
682
|
+
stacktrace: error.stacktrace
|
|
683
|
+
});
|
|
684
|
+
return new IIIInvocationError({
|
|
685
|
+
code: "UNKNOWN",
|
|
686
|
+
message: typeof error === "string" ? error : JSON.stringify(error) ?? String(error),
|
|
687
|
+
function_id
|
|
688
|
+
});
|
|
689
|
+
}
|
|
696
690
|
resolveChannelValue(value) {
|
|
697
|
-
if (
|
|
691
|
+
if (require_telemetry_system.isChannelRef(value)) return value.direction === "read" ? new ChannelReader(this.address, value) : new ChannelWriter(this.address, value);
|
|
698
692
|
if (Array.isArray(value)) return value.map((item) => this.resolveChannelValue(item));
|
|
699
693
|
if (value !== null && typeof value === "object") {
|
|
700
694
|
const out = {};
|
|
@@ -705,8 +699,8 @@ var Sdk = class {
|
|
|
705
699
|
}
|
|
706
700
|
async onInvokeFunction(invocation_id, function_id, input, traceparent, baggage) {
|
|
707
701
|
const fn = this.functions.get(function_id);
|
|
708
|
-
const getResponseTraceparent = () =>
|
|
709
|
-
const getResponseBaggage = () =>
|
|
702
|
+
const getResponseTraceparent = () => require_telemetry_system.injectTraceparent() ?? traceparent;
|
|
703
|
+
const getResponseBaggage = () => require_telemetry_system.injectBaggage() ?? baggage;
|
|
710
704
|
const resolvedInput = this.resolveChannelValue(input);
|
|
711
705
|
if (fn?.handler) {
|
|
712
706
|
if (!invocation_id) {
|
|
@@ -899,7 +893,7 @@ const registerWorker = (address, options) => new Sdk(address, options);
|
|
|
899
893
|
*/
|
|
900
894
|
var Logger = class {
|
|
901
895
|
get otelLogger() {
|
|
902
|
-
if (!this._otelLogger) this._otelLogger =
|
|
896
|
+
if (!this._otelLogger) this._otelLogger = require_telemetry_system.getLogger();
|
|
903
897
|
return this._otelLogger;
|
|
904
898
|
}
|
|
905
899
|
constructor(traceId, serviceName, spanId) {
|
|
@@ -910,8 +904,8 @@ var Logger = class {
|
|
|
910
904
|
}
|
|
911
905
|
emit(message, severity, data) {
|
|
912
906
|
const attributes = {};
|
|
913
|
-
const traceId = this.traceId ??
|
|
914
|
-
const spanId = this.spanId ??
|
|
907
|
+
const traceId = this.traceId ?? require_telemetry_system.currentTraceId();
|
|
908
|
+
const spanId = this.spanId ?? require_telemetry_system.currentSpanId();
|
|
915
909
|
if (traceId) attributes.trace_id = traceId;
|
|
916
910
|
if (spanId) attributes.span_id = spanId;
|
|
917
911
|
if (this.serviceName) attributes["service.name"] = this.serviceName;
|
|
@@ -1006,9 +1000,11 @@ var Logger = class {
|
|
|
1006
1000
|
//#endregion
|
|
1007
1001
|
exports.ChannelReader = ChannelReader;
|
|
1008
1002
|
exports.ChannelWriter = ChannelWriter;
|
|
1003
|
+
exports.EngineFunctions = require_telemetry_system.EngineFunctions;
|
|
1004
|
+
exports.EngineTriggers = require_telemetry_system.EngineTriggers;
|
|
1005
|
+
exports.IIIInvocationError = IIIInvocationError;
|
|
1009
1006
|
exports.Logger = Logger;
|
|
1010
1007
|
exports.TriggerAction = TriggerAction;
|
|
1011
|
-
exports.
|
|
1012
|
-
exports.http = require_utils.http;
|
|
1008
|
+
exports.http = require_telemetry_system.http;
|
|
1013
1009
|
exports.registerWorker = registerWorker;
|
|
1014
1010
|
//# sourceMappingURL=index.cjs.map
|