iii-sdk 0.19.7 → 0.20.0-alpha.2
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/channel.cjs +5 -0
- package/dist/channel.d.cts +2 -0
- package/dist/channel.d.mts +2 -0
- package/dist/channel.mjs +3 -0
- package/dist/{utils-DXL7JI0q.mjs → channels-De8fNMyx.mjs} +3 -108
- package/dist/channels-De8fNMyx.mjs.map +1 -0
- package/dist/{utils-CuS1Knym.cjs → channels-ZbQzcwnN.cjs} +3 -166
- package/dist/channels-ZbQzcwnN.cjs.map +1 -0
- package/dist/engine.cjs +5 -0
- package/dist/engine.d.cts +3 -0
- package/dist/engine.d.mts +3 -0
- package/dist/engine.mjs +3 -0
- package/dist/errors.cjs +28 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +40 -0
- package/dist/errors.d.cts.map +1 -0
- package/dist/errors.d.mts +40 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +25 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/helpers.cjs +6 -5
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +23 -5
- package/dist/helpers.d.cts.map +1 -1
- package/dist/helpers.d.mts +23 -5
- package/dist/helpers.d.mts.map +1 -1
- package/dist/helpers.mjs +4 -3
- package/dist/helpers.mjs.map +1 -1
- package/dist/iii-constants-Baptl8nm.d.mts +47 -0
- package/dist/iii-constants-Baptl8nm.d.mts.map +1 -0
- package/dist/iii-constants-BqXp8xSN.d.cts +47 -0
- package/dist/iii-constants-BqXp8xSN.d.cts.map +1 -0
- package/dist/iii-constants-Br94RUNi.cjs +67 -0
- package/dist/iii-constants-Br94RUNi.cjs.map +1 -0
- package/dist/iii-constants-_k3SoHry.mjs +43 -0
- package/dist/iii-constants-_k3SoHry.mjs.map +1 -0
- package/dist/iii-types-6aHBgy7l.cjs +24 -0
- package/dist/iii-types-6aHBgy7l.cjs.map +1 -0
- package/dist/iii-types-CRx2qAjB.mjs +18 -0
- package/dist/iii-types-CRx2qAjB.mjs.map +1 -0
- package/dist/index.cjs +93 -172
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -81
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +14 -81
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +30 -104
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +0 -0
- package/dist/internal.d.cts +2 -0
- package/dist/internal.d.mts +2 -0
- package/dist/internal.mjs +1 -0
- package/dist/protocol.cjs +4 -0
- package/dist/protocol.d.cts +2 -0
- package/dist/protocol.d.mts +2 -0
- package/dist/protocol.mjs +3 -0
- package/dist/runtime.cjs +0 -0
- package/dist/runtime.d.cts +3 -0
- package/dist/runtime.d.mts +3 -0
- package/dist/runtime.mjs +1 -0
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +3 -7
- package/dist/state.d.cts.map +1 -1
- package/dist/state.d.mts +3 -7
- package/dist/state.d.mts.map +1 -1
- package/dist/state.mjs.map +1 -1
- package/dist/stream.d.cts +5 -246
- package/dist/stream.d.cts.map +1 -1
- package/dist/stream.d.mts +5 -246
- package/dist/stream.d.mts.map +1 -1
- package/dist/trigger.cjs +0 -0
- package/dist/trigger.d.cts +2 -0
- package/dist/trigger.d.mts +2 -0
- package/dist/trigger.mjs +1 -0
- package/dist/{utils-BnRzIUCy.d.cts → types-BZoO05vA.d.cts} +45 -220
- package/dist/types-BZoO05vA.d.cts.map +1 -0
- package/dist/{utils-e84Qph-9.d.mts → types-BlFE8MZG.d.mts} +45 -220
- package/dist/types-BlFE8MZG.d.mts.map +1 -0
- package/dist/utils-63wxBJg-.cjs +121 -0
- package/dist/utils-63wxBJg-.cjs.map +1 -0
- package/dist/utils-8G4hHqvZ.mjs +69 -0
- package/dist/utils-8G4hHqvZ.mjs.map +1 -0
- package/package.json +37 -2
- package/typedoc.json +13 -1
- package/vitest.config.ts +3 -3
- package/dist/utils-BnRzIUCy.d.cts.map +0 -1
- package/dist/utils-CuS1Knym.cjs.map +0 -1
- package/dist/utils-DXL7JI0q.mjs.map +0 -1
- package/dist/utils-e84Qph-9.d.mts.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Readable, Writable } from "node:stream";
|
|
2
|
+
import { HttpInvocationConfig } from "@iii-dev/helpers/http";
|
|
2
3
|
|
|
3
4
|
//#region src/iii-types.d.ts
|
|
4
5
|
declare enum MessageType {
|
|
@@ -18,6 +19,11 @@ type RegisterTriggerTypeMessage = {
|
|
|
18
19
|
id: string;
|
|
19
20
|
description: string;
|
|
20
21
|
};
|
|
22
|
+
type ErrorBody = {
|
|
23
|
+
code: string;
|
|
24
|
+
message: string;
|
|
25
|
+
stacktrace?: string;
|
|
26
|
+
};
|
|
21
27
|
type RegisterTriggerMessage = {
|
|
22
28
|
message_type: MessageType.RegisterTrigger;
|
|
23
29
|
id: string;
|
|
@@ -26,35 +32,6 @@ type RegisterTriggerMessage = {
|
|
|
26
32
|
config: unknown;
|
|
27
33
|
metadata?: Record<string, unknown>;
|
|
28
34
|
};
|
|
29
|
-
/**
|
|
30
|
-
* Authentication configuration for HTTP-invoked functions.
|
|
31
|
-
*
|
|
32
|
-
* - `hmac` -- HMAC signature verification using a shared secret.
|
|
33
|
-
* - `bearer` -- Bearer token authentication.
|
|
34
|
-
* - `api_key` -- API key sent via a custom header.
|
|
35
|
-
*/
|
|
36
|
-
type HttpAuthConfig = {
|
|
37
|
-
type: 'hmac';
|
|
38
|
-
secret_key: string;
|
|
39
|
-
} | {
|
|
40
|
-
type: 'bearer';
|
|
41
|
-
token_key: string;
|
|
42
|
-
} | {
|
|
43
|
-
type: 'api_key';
|
|
44
|
-
header: string;
|
|
45
|
-
value_key: string;
|
|
46
|
-
};
|
|
47
|
-
/**
|
|
48
|
-
* Configuration for registering an HTTP-invoked function (Lambda, Cloudflare
|
|
49
|
-
* Workers, etc.) instead of a local handler.
|
|
50
|
-
*/
|
|
51
|
-
type HttpInvocationConfig = {
|
|
52
|
-
/** URL to invoke. */url: string; /** HTTP method. Defaults to `POST`. */
|
|
53
|
-
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; /** Timeout in milliseconds. */
|
|
54
|
-
timeout_ms?: number; /** Custom headers to send with the request. */
|
|
55
|
-
headers?: Record<string, string>; /** Authentication configuration. */
|
|
56
|
-
auth?: HttpAuthConfig;
|
|
57
|
-
};
|
|
58
35
|
type RegisterFunctionFormat = {
|
|
59
36
|
/**
|
|
60
37
|
* The name of the parameter
|
|
@@ -119,30 +96,6 @@ type TriggerAction = {
|
|
|
119
96
|
} | {
|
|
120
97
|
type: 'void';
|
|
121
98
|
};
|
|
122
|
-
/**
|
|
123
|
-
* Input passed to the RBAC auth function during WebSocket upgrade.
|
|
124
|
-
* Contains the HTTP headers, query parameters, and client IP from the
|
|
125
|
-
* connecting worker's upgrade request.
|
|
126
|
-
*/
|
|
127
|
-
type AuthInput = {
|
|
128
|
-
/** HTTP headers from the WebSocket upgrade request. */headers: Record<string, string>; /** Query parameters from the upgrade URL. Each key maps to an array of values to support repeated keys. */
|
|
129
|
-
query_params: Record<string, string[]>; /** IP address of the connecting client. */
|
|
130
|
-
ip_address: string;
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* Return value from the RBAC auth function. Controls which functions the
|
|
134
|
-
* authenticated worker can invoke and what context is forwarded to the
|
|
135
|
-
* middleware.
|
|
136
|
-
*/
|
|
137
|
-
type AuthResult = {
|
|
138
|
-
/** Additional function IDs to allow beyond the `expose_functions` config. */allowed_functions: string[]; /** Function IDs to deny even if they match `expose_functions`. Takes precedence over allowed. */
|
|
139
|
-
forbidden_functions: string[]; /** Trigger type IDs the worker may register triggers for. When omitted, all types are allowed. */
|
|
140
|
-
allowed_trigger_types?: string[]; /** Whether the worker may register new trigger types. */
|
|
141
|
-
allow_trigger_type_registration: boolean; /** Whether the worker may register new functions. Defaults to `true` if omitted. */
|
|
142
|
-
allow_function_registration?: boolean; /** Arbitrary context forwarded to the middleware function on every invocation. */
|
|
143
|
-
context: Record<string, unknown>; /** Optional prefix applied to all function IDs registered by this worker. */
|
|
144
|
-
function_registration_prefix?: string;
|
|
145
|
-
};
|
|
146
99
|
/**
|
|
147
100
|
* Input passed to the RBAC middleware function on every function invocation
|
|
148
101
|
* through the RBAC port. The middleware can inspect, modify, or reject the
|
|
@@ -155,80 +108,7 @@ type MiddlewareFunctionInput = {
|
|
|
155
108
|
context: Record<string, unknown>;
|
|
156
109
|
};
|
|
157
110
|
/**
|
|
158
|
-
*
|
|
159
|
-
* when a worker attempts to register a new trigger type through the RBAC port.
|
|
160
|
-
* Return an {@link OnTriggerTypeRegistrationResult} with the (possibly mapped)
|
|
161
|
-
* fields, or throw to deny the registration.
|
|
162
|
-
*/
|
|
163
|
-
type OnTriggerTypeRegistrationInput = {
|
|
164
|
-
/** ID of the trigger type being registered. */trigger_type_id: string; /** Human-readable description of the trigger type. */
|
|
165
|
-
description: string; /** Auth context from `AuthResult.context` for this session. */
|
|
166
|
-
context: Record<string, unknown>;
|
|
167
|
-
};
|
|
168
|
-
/**
|
|
169
|
-
* Result returned from the `on_trigger_type_registration_function_id` hook.
|
|
170
|
-
* All fields are optional -- omitted fields keep the original value from the
|
|
171
|
-
* registration request.
|
|
172
|
-
*/
|
|
173
|
-
type OnTriggerTypeRegistrationResult = {
|
|
174
|
-
/** Mapped trigger type ID. */trigger_type_id?: string; /** Mapped description. */
|
|
175
|
-
description?: string;
|
|
176
|
-
};
|
|
177
|
-
/**
|
|
178
|
-
* Input passed to the `on_trigger_registration_function_id` hook
|
|
179
|
-
* when a worker attempts to register a trigger through the RBAC port.
|
|
180
|
-
* Return an {@link OnTriggerRegistrationResult} with the (possibly mapped)
|
|
181
|
-
* fields, or throw to deny the registration.
|
|
182
|
-
*/
|
|
183
|
-
type OnTriggerRegistrationInput = {
|
|
184
|
-
/** ID of the trigger being registered. */trigger_id: string; /** Trigger type identifier. */
|
|
185
|
-
trigger_type: string; /** ID of the function this trigger is bound to. */
|
|
186
|
-
function_id: string; /** Trigger-specific configuration. */
|
|
187
|
-
config: unknown; /** Arbitrary metadata attached to the trigger. */
|
|
188
|
-
metadata?: Record<string, unknown>; /** Auth context from `AuthResult.context` for this session. */
|
|
189
|
-
context: Record<string, unknown>;
|
|
190
|
-
};
|
|
191
|
-
/**
|
|
192
|
-
* Result returned from the `on_trigger_registration_function_id` hook.
|
|
193
|
-
* All fields are optional -- omitted fields keep the original value from the
|
|
194
|
-
* registration request.
|
|
195
|
-
*/
|
|
196
|
-
type OnTriggerRegistrationResult = {
|
|
197
|
-
/** Mapped trigger ID. */trigger_id?: string; /** Mapped trigger type. */
|
|
198
|
-
trigger_type?: string; /** Mapped function ID. */
|
|
199
|
-
function_id?: string; /** Mapped trigger configuration. */
|
|
200
|
-
config?: unknown;
|
|
201
|
-
};
|
|
202
|
-
/**
|
|
203
|
-
* Input passed to the `on_function_registration_function_id` hook
|
|
204
|
-
* when a worker attempts to register a function through the RBAC port.
|
|
205
|
-
* Return an {@link OnFunctionRegistrationResult} with the (possibly mapped)
|
|
206
|
-
* fields, or throw to deny the registration.
|
|
207
|
-
*/
|
|
208
|
-
type OnFunctionRegistrationInput = {
|
|
209
|
-
/** ID of the function being registered. */function_id: string; /** Human-readable description of the function. */
|
|
210
|
-
description?: string; /** Arbitrary metadata attached to the function. */
|
|
211
|
-
metadata?: Record<string, unknown>; /** Auth context from `AuthResult.context` for this session. */
|
|
212
|
-
context: Record<string, unknown>;
|
|
213
|
-
};
|
|
214
|
-
/**
|
|
215
|
-
* Result returned from the `on_function_registration_function_id` hook.
|
|
216
|
-
* All fields are optional -- omitted fields keep the original value from the
|
|
217
|
-
* registration request.
|
|
218
|
-
*/
|
|
219
|
-
type OnFunctionRegistrationResult = {
|
|
220
|
-
/** Mapped function ID. */function_id?: string; /** Mapped description. */
|
|
221
|
-
description?: string; /** Mapped metadata. */
|
|
222
|
-
metadata?: Record<string, unknown>;
|
|
223
|
-
};
|
|
224
|
-
/**
|
|
225
|
-
* Result returned when a function is invoked with `TriggerAction.Enqueue`.
|
|
226
|
-
*/
|
|
227
|
-
type EnqueueResult = {
|
|
228
|
-
/** Unique receipt ID for the enqueued message. */messageReceiptId: string;
|
|
229
|
-
};
|
|
230
|
-
/**
|
|
231
|
-
* Request object passed to {@link ISdk.trigger}.
|
|
111
|
+
* Request object passed to {@link IIIClient.trigger}.
|
|
232
112
|
*
|
|
233
113
|
* @typeParam TInput - Type of the payload.
|
|
234
114
|
*/
|
|
@@ -237,6 +117,12 @@ type TriggerRequest<TInput = unknown> = {
|
|
|
237
117
|
payload: TInput; /** Routing action. Omit for synchronous request/response. */
|
|
238
118
|
action?: TriggerAction; /** Override the default invocation timeout in milliseconds. */
|
|
239
119
|
timeoutMs?: number;
|
|
120
|
+
/**
|
|
121
|
+
* Optional per-invocation metadata (arbitrary JSON). Travels as a separate
|
|
122
|
+
* channel from the payload and is surfaced to the target handler as a
|
|
123
|
+
* dedicated argument. Omitted from the wire message when undefined.
|
|
124
|
+
*/
|
|
125
|
+
metadata?: unknown;
|
|
240
126
|
};
|
|
241
127
|
/**
|
|
242
128
|
* Serializable reference to one end of a streaming channel. Can be included
|
|
@@ -357,7 +243,7 @@ type TriggerConfig<TConfig> = {
|
|
|
357
243
|
};
|
|
358
244
|
/**
|
|
359
245
|
* Handler interface for custom trigger types. Passed to
|
|
360
|
-
* `
|
|
246
|
+
* `IIIClient.registerTriggerType`.
|
|
361
247
|
*
|
|
362
248
|
* @typeParam TConfig - Type of the trigger-specific configuration.
|
|
363
249
|
*
|
|
@@ -381,7 +267,13 @@ type TriggerHandler<TConfig> = {
|
|
|
381
267
|
//#region src/types.d.ts
|
|
382
268
|
/**
|
|
383
269
|
* Async function handler for a registered function. Receives the invocation
|
|
384
|
-
* payload and returns the
|
|
270
|
+
* payload and an optional per-invocation `metadata` value, and returns the
|
|
271
|
+
* result.
|
|
272
|
+
*
|
|
273
|
+
* `metadata` is arbitrary JSON travelling on a separate channel from the
|
|
274
|
+
* payload. It is `undefined` when the caller did not attach any. Existing
|
|
275
|
+
* single-argument handlers keep working -- the extra argument is simply
|
|
276
|
+
* ignored by them.
|
|
385
277
|
*
|
|
386
278
|
* @typeParam TInput - Type of the invocation payload.
|
|
387
279
|
* @typeParam TOutput - Type of the return value.
|
|
@@ -389,15 +281,15 @@ type TriggerHandler<TConfig> = {
|
|
|
389
281
|
* @example
|
|
390
282
|
* ```typescript
|
|
391
283
|
* const handler: RemoteFunctionHandler<{ name: string }, { message: string }> =
|
|
392
|
-
* async (data) => ({ message: `Hello, ${data.name}!` })
|
|
284
|
+
* async (data, metadata) => ({ message: `Hello, ${data.name}!` })
|
|
393
285
|
* ```
|
|
394
286
|
*/
|
|
395
|
-
type RemoteFunctionHandler<TInput = any, TOutput = any> = (data: TInput) => Promise<TOutput>;
|
|
287
|
+
type RemoteFunctionHandler<TInput = any, TOutput = any> = (data: TInput, metadata?: unknown) => Promise<TOutput>;
|
|
396
288
|
type RegisterTriggerInput = Omit<RegisterTriggerMessage, 'message_type' | 'id'>;
|
|
397
289
|
type RegisterFunctionInput = Omit<RegisterFunctionMessage, 'message_type'>;
|
|
398
290
|
type RegisterFunctionOptions = Omit<RegisterFunctionMessage, 'message_type' | 'id'>;
|
|
399
291
|
type RegisterTriggerTypeInput = Omit<RegisterTriggerTypeMessage, 'message_type'>;
|
|
400
|
-
interface
|
|
292
|
+
interface IIIClient {
|
|
401
293
|
/**
|
|
402
294
|
* Registers a new trigger. A trigger is a way to invoke a function when a certain event occurs.
|
|
403
295
|
* @param trigger - The trigger to register
|
|
@@ -405,7 +297,7 @@ interface ISdk {
|
|
|
405
297
|
*
|
|
406
298
|
* @example
|
|
407
299
|
* ```typescript
|
|
408
|
-
* const trigger =
|
|
300
|
+
* const trigger = worker.registerTrigger({
|
|
409
301
|
* type: 'cron',
|
|
410
302
|
* function_id: 'my-service::process-batch',
|
|
411
303
|
* config: { expression: '0 *\/5 * * * * *' },
|
|
@@ -426,14 +318,14 @@ interface ISdk {
|
|
|
426
318
|
* @example
|
|
427
319
|
* ```typescript
|
|
428
320
|
* // Local handler
|
|
429
|
-
* const ref =
|
|
321
|
+
* const ref = worker.registerFunction(
|
|
430
322
|
* 'greet',
|
|
431
323
|
* async (data: { name: string }) => ({ message: `Hello, ${data.name}!` }),
|
|
432
324
|
* { description: 'Returns a greeting' },
|
|
433
325
|
* )
|
|
434
326
|
*
|
|
435
327
|
* // HTTP invocation
|
|
436
|
-
* const lambdaRef =
|
|
328
|
+
* const lambdaRef = worker.registerFunction(
|
|
437
329
|
* 'external::my-lambda',
|
|
438
330
|
* {
|
|
439
331
|
* url: 'https://abc123.lambda-url.us-east-1.on.aws',
|
|
@@ -458,7 +350,7 @@ interface ISdk {
|
|
|
458
350
|
* @example
|
|
459
351
|
* ```typescript
|
|
460
352
|
* // Synchronous invocation
|
|
461
|
-
* const result = await
|
|
353
|
+
* const result = await worker.trigger<{ name: string }, { message: string }>({
|
|
462
354
|
* function_id: 'greet',
|
|
463
355
|
* payload: { name: 'World' },
|
|
464
356
|
* timeoutMs: 5000,
|
|
@@ -466,14 +358,14 @@ interface ISdk {
|
|
|
466
358
|
* console.log(result.message) // "Hello, World!"
|
|
467
359
|
*
|
|
468
360
|
* // Fire-and-forget
|
|
469
|
-
* await
|
|
361
|
+
* await worker.trigger({
|
|
470
362
|
* function_id: 'send-email',
|
|
471
363
|
* payload: { to: 'user@example.com' },
|
|
472
364
|
* action: TriggerAction.Void(),
|
|
473
365
|
* })
|
|
474
366
|
*
|
|
475
367
|
* // Enqueue for async processing
|
|
476
|
-
* const receipt = await
|
|
368
|
+
* const receipt = await worker.trigger({
|
|
477
369
|
* function_id: 'process-order',
|
|
478
370
|
* payload: { orderId: '123' },
|
|
479
371
|
* action: TriggerAction.Enqueue({ queue: 'orders' }),
|
|
@@ -491,12 +383,12 @@ interface ISdk {
|
|
|
491
383
|
* ```typescript
|
|
492
384
|
* type CronConfig = { expression: string }
|
|
493
385
|
*
|
|
494
|
-
*
|
|
386
|
+
* worker.registerTriggerType<CronConfig>(
|
|
495
387
|
* { id: 'cron', description: 'Fires on a cron schedule' },
|
|
496
388
|
* {
|
|
497
389
|
* async registerTrigger({ id, function_id, config }) {
|
|
498
390
|
* startCronJob(id, config.expression, () =>
|
|
499
|
-
*
|
|
391
|
+
* worker.trigger({ function_id, payload: {} }),
|
|
500
392
|
* )
|
|
501
393
|
* },
|
|
502
394
|
* async unregisterTrigger({ id }) {
|
|
@@ -513,7 +405,7 @@ interface ISdk {
|
|
|
513
405
|
*
|
|
514
406
|
* @example
|
|
515
407
|
* ```typescript
|
|
516
|
-
*
|
|
408
|
+
* worker.unregisterTriggerType({ id: 'cron', description: 'Fires on a cron schedule' })
|
|
517
409
|
* ```
|
|
518
410
|
*/
|
|
519
411
|
unregisterTriggerType(triggerType: RegisterTriggerTypeInput): void;
|
|
@@ -523,7 +415,7 @@ interface ISdk {
|
|
|
523
415
|
* @example
|
|
524
416
|
* ```typescript
|
|
525
417
|
* process.on('SIGTERM', async () => {
|
|
526
|
-
* await
|
|
418
|
+
* await worker.shutdown()
|
|
527
419
|
* process.exit(0)
|
|
528
420
|
* })
|
|
529
421
|
* ```
|
|
@@ -531,14 +423,14 @@ interface ISdk {
|
|
|
531
423
|
shutdown(): Promise<void>;
|
|
532
424
|
}
|
|
533
425
|
/**
|
|
534
|
-
* Handle returned by {@link
|
|
426
|
+
* Handle returned by {@link IIIClient.registerTrigger}. Use `unregister()` to
|
|
535
427
|
* remove the trigger from the engine.
|
|
536
428
|
*/
|
|
537
429
|
type Trigger = {
|
|
538
430
|
/** Removes this trigger from the engine. */unregister(): void;
|
|
539
431
|
};
|
|
540
432
|
/**
|
|
541
|
-
* Handle returned by {@link
|
|
433
|
+
* Handle returned by {@link IIIClient.registerFunction}. Contains the function's
|
|
542
434
|
* `id` and an `unregister()` method.
|
|
543
435
|
*/
|
|
544
436
|
type FunctionRef = {
|
|
@@ -546,7 +438,7 @@ type FunctionRef = {
|
|
|
546
438
|
unregister: () => void;
|
|
547
439
|
};
|
|
548
440
|
/**
|
|
549
|
-
* Typed handle returned by {@link
|
|
441
|
+
* Typed handle returned by {@link IIIClient.registerTriggerType}.
|
|
550
442
|
*
|
|
551
443
|
* Provides convenience methods to register triggers and functions scoped
|
|
552
444
|
* to this trigger type, so callers don't need to repeat the `type` field.
|
|
@@ -557,12 +449,12 @@ type FunctionRef = {
|
|
|
557
449
|
* ```typescript
|
|
558
450
|
* type CronConfig = { expression: string }
|
|
559
451
|
*
|
|
560
|
-
* const cron =
|
|
452
|
+
* const cron = worker.registerTriggerType<CronConfig>(
|
|
561
453
|
* { id: 'cron', description: 'Fires on a cron schedule' },
|
|
562
454
|
* cronHandler,
|
|
563
455
|
* )
|
|
564
456
|
*
|
|
565
|
-
* // Register a trigger
|
|
457
|
+
* // Register a trigger, type is inferred as CronConfig
|
|
566
458
|
* cron.registerTrigger('my::fn', { expression: '0 *\/5 * * * * *' })
|
|
567
459
|
*
|
|
568
460
|
* // Register a function and bind a trigger in one call
|
|
@@ -619,89 +511,22 @@ type InternalHttpRequest<TBody = unknown> = {
|
|
|
619
511
|
request_body: ChannelReader;
|
|
620
512
|
};
|
|
621
513
|
/**
|
|
622
|
-
* Response object passed to
|
|
514
|
+
* Response object passed to streaming function handlers. Use `status()` and
|
|
623
515
|
* `headers()` to set response metadata, write to `stream` for streaming
|
|
624
516
|
* responses, and call `close()` when done.
|
|
625
517
|
*/
|
|
626
|
-
type
|
|
518
|
+
type StreamResponse = {
|
|
627
519
|
/** Set the HTTP status code. */status: (statusCode: number) => void; /** Set response headers. */
|
|
628
520
|
headers: (headers: Record<string, string>) => void; /** Writable stream for the response body. */
|
|
629
521
|
stream: NodeJS.WritableStream; /** Close the response. */
|
|
630
522
|
close: () => void;
|
|
631
523
|
};
|
|
632
524
|
/**
|
|
633
|
-
* Incoming
|
|
525
|
+
* Incoming streaming request received by a function registered with a stream trigger.
|
|
634
526
|
*
|
|
635
527
|
* @typeParam TBody - Type of the parsed request body.
|
|
636
528
|
*/
|
|
637
|
-
type
|
|
638
|
-
/**
|
|
639
|
-
* Alias for {@link HttpRequest}. Represents an incoming API request.
|
|
640
|
-
*
|
|
641
|
-
* @typeParam TBody - Type of the parsed request body.
|
|
642
|
-
*/
|
|
643
|
-
type ApiRequest<TBody = unknown> = HttpRequest<TBody>;
|
|
644
|
-
/**
|
|
645
|
-
* Structured API response returned from HTTP function handlers.
|
|
646
|
-
*
|
|
647
|
-
* @typeParam TStatus - HTTP status code literal type.
|
|
648
|
-
* @typeParam TBody - Type of the response body.
|
|
649
|
-
*
|
|
650
|
-
* @example
|
|
651
|
-
* ```typescript
|
|
652
|
-
* const response: ApiResponse = {
|
|
653
|
-
* status_code: 200,
|
|
654
|
-
* headers: { 'content-type': 'application/json' },
|
|
655
|
-
* body: { message: 'ok' },
|
|
656
|
-
* }
|
|
657
|
-
* ```
|
|
658
|
-
*/
|
|
659
|
-
type ApiResponse<TStatus extends number = number, TBody = string | Buffer | Record<string, unknown>> = {
|
|
660
|
-
/** HTTP status code. */status_code: TStatus; /** Response headers. */
|
|
661
|
-
headers?: Record<string, string>; /** Response body. */
|
|
662
|
-
body?: TBody;
|
|
663
|
-
};
|
|
664
|
-
//#endregion
|
|
665
|
-
//#region src/utils.d.ts
|
|
666
|
-
/**
|
|
667
|
-
* Helper that wraps an HTTP-style handler (with separate `req`/`res` arguments)
|
|
668
|
-
* into the function handler format expected by the SDK.
|
|
669
|
-
*
|
|
670
|
-
* @param callback - Async handler receiving an {@link HttpRequest} and {@link HttpResponse}.
|
|
671
|
-
* @returns A function handler compatible with {@link ISdk.registerFunction}.
|
|
672
|
-
*
|
|
673
|
-
* @example
|
|
674
|
-
* ```typescript
|
|
675
|
-
* import { http } from 'iii-sdk'
|
|
676
|
-
*
|
|
677
|
-
* iii.registerFunction(
|
|
678
|
-
* 'my-api',
|
|
679
|
-
* http(async (req, res) => {
|
|
680
|
-
* res.status(200)
|
|
681
|
-
* res.headers({ 'content-type': 'application/json' })
|
|
682
|
-
* res.stream.end(JSON.stringify({ hello: 'world' }))
|
|
683
|
-
* res.close()
|
|
684
|
-
* }),
|
|
685
|
-
* )
|
|
686
|
-
* ```
|
|
687
|
-
*/
|
|
688
|
-
declare const http: (callback: (req: HttpRequest, res: HttpResponse) => Promise<void | ApiResponse>) => (req: InternalHttpRequest) => Promise<void | ApiResponse>;
|
|
689
|
-
/**
|
|
690
|
-
* Type guard that checks if a value is a {@link StreamChannelRef}.
|
|
691
|
-
*
|
|
692
|
-
* @param value - Value to check.
|
|
693
|
-
* @returns `true` if the value is a valid `StreamChannelRef`.
|
|
694
|
-
*/
|
|
695
|
-
declare const isChannelRef: (value: unknown) => value is StreamChannelRef;
|
|
696
|
-
/**
|
|
697
|
-
* Recursively extract all {@link StreamChannelRef} values from a JSON-like
|
|
698
|
-
* input, returning each match paired with its dotted/bracketed path. Mirrors
|
|
699
|
-
* the Rust SDK's `extract_channel_refs`.
|
|
700
|
-
*
|
|
701
|
-
* @param data - Arbitrary JSON-like value.
|
|
702
|
-
* @returns Array of `[path, ref]` tuples. Empty when no refs are found.
|
|
703
|
-
*/
|
|
704
|
-
declare const extractChannelRefs: (data: unknown) => Array<[string, StreamChannelRef]>;
|
|
529
|
+
type StreamRequest<TBody = unknown> = Omit<InternalHttpRequest<TBody>, 'response'>;
|
|
705
530
|
//#endregion
|
|
706
|
-
export {
|
|
707
|
-
//# sourceMappingURL=
|
|
531
|
+
export { RegisterFunctionFormat as C, StreamChannelRef as D, RegisterTriggerTypeMessage as E, TriggerRequest as O, MiddlewareFunctionInput as S, RegisterTriggerMessage as T, ChannelItem as _, RegisterFunctionInput as a, ErrorBody as b, RegisterTriggerTypeInput as c, StreamResponse as d, Trigger as f, ChannelDirection as g, TriggerHandler as h, InternalHttpRequest as i, RemoteFunctionHandler as l, TriggerConfig as m, FunctionRef as n, RegisterFunctionOptions as o, TriggerTypeRef as p, IIIClient as r, RegisterTriggerInput as s, Channel as t, StreamRequest as u, ChannelReader as v, RegisterFunctionMessage as w, MessageType as x, ChannelWriter as y };
|
|
532
|
+
//# sourceMappingURL=types-BlFE8MZG.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-BlFE8MZG.d.mts","names":[],"sources":["../src/iii-types.ts","../src/channels.ts","../src/triggers.ts","../src/types.ts"],"mappings":";;;;aAEY,WAAA;EACV,gBAAA;EACA,kBAAA;EACA,cAAA;EACA,gBAAA;EACA,mBAAA;EACA,eAAA;EACA,iBAAA;EACA,qBAAA;EACA,yBAAA;EACA,gBAAA;AAAA;AAAA,KAGU,0BAAA;EACV,YAAA,EAAc,WAAA,CAAY,mBAAA;EAC1B,EAAA;EACA,WAAA;AAAA;AAAA,KAcU,SAAA;EACV,IAAA;EACA,OAAA;EACA,UAAA;AAAA;AAAA,KAWU,sBAAA;EACV,YAAA,EAAc,WAAA,CAAY,eAAA;EAC1B,EAAA;EACA,IAAA;EACA,WAAA;EACA,MAAA;EACA,QAAA,GAAW,MAAA;AAAA;AAAA,KAGD,sBAAA;EANV;;;EAUA,IAAA;EAPW;;;EAWX,WAAA;EARgC;;;EAYhC,IAAA;EAJA;;;EAQA,UAAA,GAAa,MAAA;EAIb;;;EAAA,KAAA;EAKY;AAGd;;EAJE,QAAA;EAAA,CACC,GAAA;AAAA;AAAA,KAGS,uBAAA;EACV,YAAA,EAAc,WAAA,CAAY,gBAAA;EAqBb;;;EAjBb,EAAA;EAJc;;;EAQd,WAAA;EAIA;;;EAAA,cAAA,GAAiB,sBAAA;EAKjB;;;EADA,eAAA,GAAkB,sBAAA;EAClB,QAAA,GAAW,MAAA;EAIsB;AAUnC;;EAVE,UAAA,GAAa,oBAAA;AAAA;;;;;;AAiBf;;KAPY,aAAA;EAAkB,IAAA;EAAiB,KAAA;AAAA;EAAoB,IAAA;AAAA;;;;;;KAOvD,uBAAA;EAQD,wCANT,WAAA,UAMe;EAJf,OAAA,EAAS,MAAA,mBAYe;EAVxB,MAAA,GAAS,aAAA,EAgBa;E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nPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
let node_fs = require("node:fs");
|
|
29
|
+
node_fs = __toESM(node_fs);
|
|
30
|
+
let node_path = require("node:path");
|
|
31
|
+
node_path = __toESM(node_path);
|
|
32
|
+
|
|
33
|
+
//#region src/utils.ts
|
|
34
|
+
/**
|
|
35
|
+
* Returns a project identifier for telemetry, derived from the current working
|
|
36
|
+
* directory. Reads `package.json` `name` if present at `cwd`; otherwise falls
|
|
37
|
+
* back to the basename of `cwd`. Returns `undefined` only when both signals
|
|
38
|
+
* are unavailable (e.g. cwd is the filesystem root).
|
|
39
|
+
*
|
|
40
|
+
* No directory walking, only inspects `cwd` itself, so the SDK never reads
|
|
41
|
+
* files outside the user's explicit working directory.
|
|
42
|
+
*/
|
|
43
|
+
function detectProjectName(cwd = process.cwd()) {
|
|
44
|
+
try {
|
|
45
|
+
const manifest = node_path.join(cwd, "package.json");
|
|
46
|
+
if (node_fs.existsSync(manifest)) {
|
|
47
|
+
const parsed = JSON.parse(node_fs.readFileSync(manifest, "utf8"));
|
|
48
|
+
if (typeof parsed.name === "string") {
|
|
49
|
+
const trimmed = parsed.name.trim();
|
|
50
|
+
if (trimmed) return trimmed;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} catch {}
|
|
54
|
+
return node_path.basename(cwd).trim() || void 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Type guard that checks if a value is a {@link StreamChannelRef}.
|
|
58
|
+
*
|
|
59
|
+
* @param value - Value to check.
|
|
60
|
+
* @returns `true` if the value is a valid `StreamChannelRef`.
|
|
61
|
+
*/
|
|
62
|
+
const isChannelRef = (value) => {
|
|
63
|
+
if (typeof value !== "object" || value === null) return false;
|
|
64
|
+
const maybe = value;
|
|
65
|
+
return typeof maybe.channel_id === "string" && typeof maybe.access_key === "string" && (maybe.direction === "read" || maybe.direction === "write");
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Recursively extract all {@link StreamChannelRef} values from a JSON-like
|
|
69
|
+
* input, returning each match paired with its dotted/bracketed path. Mirrors
|
|
70
|
+
* the Rust SDK's `extract_channel_refs`.
|
|
71
|
+
*
|
|
72
|
+
* @param data - Arbitrary JSON-like value.
|
|
73
|
+
* @returns Array of `[path, ref]` tuples. Empty when no refs are found.
|
|
74
|
+
*/
|
|
75
|
+
const extractChannelRefs = (data) => {
|
|
76
|
+
const refs = [];
|
|
77
|
+
extractRefsRecursive(data, "", refs);
|
|
78
|
+
return refs;
|
|
79
|
+
};
|
|
80
|
+
const extractRefsRecursive = (data, prefix, refs) => {
|
|
81
|
+
if (isChannelRef(data)) {
|
|
82
|
+
refs.push([prefix, data]);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(data)) {
|
|
86
|
+
for (let i = 0; i < data.length; i++) {
|
|
87
|
+
const path = prefix === "" ? `[${i}]` : `${prefix}[${i}]`;
|
|
88
|
+
extractRefsRecursive(data[i], path, refs);
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (typeof data !== "object" || data === null) return;
|
|
93
|
+
for (const [key, value] of Object.entries(data)) extractRefsRecursive(value, prefix === "" ? key : `${prefix}.${key}`, refs);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
Object.defineProperty(exports, '__toESM', {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
get: function () {
|
|
100
|
+
return __toESM;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(exports, 'detectProjectName', {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
get: function () {
|
|
106
|
+
return detectProjectName;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(exports, 'extractChannelRefs', {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
get: function () {
|
|
112
|
+
return extractChannelRefs;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(exports, 'isChannelRef', {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
get: function () {
|
|
118
|
+
return isChannelRef;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
//# sourceMappingURL=utils-63wxBJg-.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-63wxBJg-.cjs","names":["path","fs"],"sources":["../src/utils.ts"],"sourcesContent":["import * as fs from 'node:fs'\nimport * as path from 'node:path'\nimport type { StreamChannelRef } from './iii-types'\n\n/**\n * Returns a project identifier for telemetry, derived from the current working\n * directory. Reads `package.json` `name` if present at `cwd`; otherwise falls\n * back to the basename of `cwd`. Returns `undefined` only when both signals\n * are unavailable (e.g. cwd is the filesystem root).\n *\n * No directory walking, only inspects `cwd` itself, so the SDK never reads\n * files outside the user's explicit working directory.\n */\nexport function detectProjectName(cwd: string = process.cwd()): string | undefined {\n try {\n const manifest = path.join(cwd, 'package.json')\n if (fs.existsSync(manifest)) {\n const parsed = JSON.parse(fs.readFileSync(manifest, 'utf8')) as { name?: unknown }\n if (typeof parsed.name === 'string') {\n const trimmed = parsed.name.trim()\n if (trimmed) return trimmed\n }\n }\n } catch {\n // fall through to directory-name fallback\n }\n\n const base = path.basename(cwd).trim()\n return base || undefined\n}\n\n/**\n * Type guard that checks if a value is a {@link StreamChannelRef}.\n *\n * @param value - Value to check.\n * @returns `true` if the value is a valid `StreamChannelRef`.\n */\nexport const isChannelRef = (value: unknown): value is StreamChannelRef => {\n if (typeof value !== 'object' || value === null) return false\n const maybe = value as Partial<StreamChannelRef>\n return (\n typeof maybe.channel_id === 'string' &&\n typeof maybe.access_key === 'string' &&\n (maybe.direction === 'read' || maybe.direction === 'write')\n )\n}\n\n/**\n * Recursively extract all {@link StreamChannelRef} values from a JSON-like\n * input, returning each match paired with its dotted/bracketed path. Mirrors\n * the Rust SDK's `extract_channel_refs`.\n *\n * @param data - Arbitrary JSON-like value.\n * @returns Array of `[path, ref]` tuples. Empty when no refs are found.\n */\nexport const extractChannelRefs = (data: unknown): Array<[string, StreamChannelRef]> => {\n const refs: Array<[string, StreamChannelRef]> = []\n extractRefsRecursive(data, '', refs)\n return refs\n}\n\nconst extractRefsRecursive = (\n data: unknown,\n prefix: string,\n refs: Array<[string, StreamChannelRef]>,\n): void => {\n if (isChannelRef(data)) {\n refs.push([prefix, data])\n return\n }\n if (Array.isArray(data)) {\n for (let i = 0; i < data.length; i++) {\n const path = prefix === '' ? `[${i}]` : `${prefix}[${i}]`\n extractRefsRecursive(data[i], path, refs)\n }\n return\n }\n if (typeof data !== 'object' || data === null) return\n\n for (const [key, value] of Object.entries(data as Record<string, unknown>)) {\n const path = prefix === '' ? key : `${prefix}.${key}`\n extractRefsRecursive(value, path, refs)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,SAA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3
|
+
|
|
4
|
+
//#region src/utils.ts
|
|
5
|
+
/**
|
|
6
|
+
* Returns a project identifier for telemetry, derived from the current working
|
|
7
|
+
* directory. Reads `package.json` `name` if present at `cwd`; otherwise falls
|
|
8
|
+
* back to the basename of `cwd`. Returns `undefined` only when both signals
|
|
9
|
+
* are unavailable (e.g. cwd is the filesystem root).
|
|
10
|
+
*
|
|
11
|
+
* No directory walking, only inspects `cwd` itself, so the SDK never reads
|
|
12
|
+
* files outside the user's explicit working directory.
|
|
13
|
+
*/
|
|
14
|
+
function detectProjectName(cwd = process.cwd()) {
|
|
15
|
+
try {
|
|
16
|
+
const manifest = path.join(cwd, "package.json");
|
|
17
|
+
if (fs.existsSync(manifest)) {
|
|
18
|
+
const parsed = JSON.parse(fs.readFileSync(manifest, "utf8"));
|
|
19
|
+
if (typeof parsed.name === "string") {
|
|
20
|
+
const trimmed = parsed.name.trim();
|
|
21
|
+
if (trimmed) return trimmed;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} catch {}
|
|
25
|
+
return path.basename(cwd).trim() || void 0;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Type guard that checks if a value is a {@link StreamChannelRef}.
|
|
29
|
+
*
|
|
30
|
+
* @param value - Value to check.
|
|
31
|
+
* @returns `true` if the value is a valid `StreamChannelRef`.
|
|
32
|
+
*/
|
|
33
|
+
const isChannelRef = (value) => {
|
|
34
|
+
if (typeof value !== "object" || value === null) return false;
|
|
35
|
+
const maybe = value;
|
|
36
|
+
return typeof maybe.channel_id === "string" && typeof maybe.access_key === "string" && (maybe.direction === "read" || maybe.direction === "write");
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Recursively extract all {@link StreamChannelRef} values from a JSON-like
|
|
40
|
+
* input, returning each match paired with its dotted/bracketed path. Mirrors
|
|
41
|
+
* the Rust SDK's `extract_channel_refs`.
|
|
42
|
+
*
|
|
43
|
+
* @param data - Arbitrary JSON-like value.
|
|
44
|
+
* @returns Array of `[path, ref]` tuples. Empty when no refs are found.
|
|
45
|
+
*/
|
|
46
|
+
const extractChannelRefs = (data) => {
|
|
47
|
+
const refs = [];
|
|
48
|
+
extractRefsRecursive(data, "", refs);
|
|
49
|
+
return refs;
|
|
50
|
+
};
|
|
51
|
+
const extractRefsRecursive = (data, prefix, refs) => {
|
|
52
|
+
if (isChannelRef(data)) {
|
|
53
|
+
refs.push([prefix, data]);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(data)) {
|
|
57
|
+
for (let i = 0; i < data.length; i++) {
|
|
58
|
+
const path = prefix === "" ? `[${i}]` : `${prefix}[${i}]`;
|
|
59
|
+
extractRefsRecursive(data[i], path, refs);
|
|
60
|
+
}
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (typeof data !== "object" || data === null) return;
|
|
64
|
+
for (const [key, value] of Object.entries(data)) extractRefsRecursive(value, prefix === "" ? key : `${prefix}.${key}`, refs);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { extractChannelRefs as n, isChannelRef as r, detectProjectName as t };
|
|
69
|
+
//# sourceMappingURL=utils-8G4hHqvZ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-8G4hHqvZ.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import * as fs from 'node:fs'\nimport * as path from 'node:path'\nimport type { StreamChannelRef } from './iii-types'\n\n/**\n * Returns a project identifier for telemetry, derived from the current working\n * directory. Reads `package.json` `name` if present at `cwd`; otherwise falls\n * back to the basename of `cwd`. Returns `undefined` only when both signals\n * are unavailable (e.g. cwd is the filesystem root).\n *\n * No directory walking, only inspects `cwd` itself, so the SDK never reads\n * files outside the user's explicit working directory.\n */\nexport function detectProjectName(cwd: string = process.cwd()): string | undefined {\n try {\n const manifest = path.join(cwd, 'package.json')\n if (fs.existsSync(manifest)) {\n const parsed = JSON.parse(fs.readFileSync(manifest, 'utf8')) as { name?: unknown }\n if (typeof parsed.name === 'string') {\n const trimmed = parsed.name.trim()\n if (trimmed) return trimmed\n }\n }\n } catch {\n // fall through to directory-name fallback\n }\n\n const base = path.basename(cwd).trim()\n return base || undefined\n}\n\n/**\n * Type guard that checks if a value is a {@link StreamChannelRef}.\n *\n * @param value - Value to check.\n * @returns `true` if the value is a valid `StreamChannelRef`.\n */\nexport const isChannelRef = (value: unknown): value is StreamChannelRef => {\n if (typeof value !== 'object' || value === null) return false\n const maybe = value as Partial<StreamChannelRef>\n return (\n typeof maybe.channel_id === 'string' &&\n typeof maybe.access_key === 'string' &&\n (maybe.direction === 'read' || maybe.direction === 'write')\n )\n}\n\n/**\n * Recursively extract all {@link StreamChannelRef} values from a JSON-like\n * input, returning each match paired with its dotted/bracketed path. Mirrors\n * the Rust SDK's `extract_channel_refs`.\n *\n * @param data - Arbitrary JSON-like value.\n * @returns Array of `[path, ref]` tuples. Empty when no refs are found.\n */\nexport const extractChannelRefs = (data: unknown): Array<[string, StreamChannelRef]> => {\n const refs: Array<[string, StreamChannelRef]> = []\n extractRefsRecursive(data, '', refs)\n return refs\n}\n\nconst extractRefsRecursive = (\n data: unknown,\n prefix: string,\n refs: Array<[string, StreamChannelRef]>,\n): void => {\n if (isChannelRef(data)) {\n refs.push([prefix, data])\n return\n }\n if (Array.isArray(data)) {\n for (let i = 0; i < data.length; i++) {\n const path = prefix === '' ? `[${i}]` : `${prefix}[${i}]`\n extractRefsRecursive(data[i], path, refs)\n }\n return\n }\n if (typeof data !== 'object' || data === null) return\n\n for (const [key, value] of Object.entries(data as Record<string, unknown>)) {\n const path = prefix === '' ? key : `${prefix}.${key}`\n extractRefsRecursive(value, path, refs)\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,SAAgB,kBAAkB,MAAc,QAAQ,KAAK,EAAsB;AACjF,KAAI;EACF,MAAM,WAAW,KAAK,KAAK,KAAK,eAAe;AAC/C,MAAI,GAAG,WAAW,SAAS,EAAE;GAC3B,M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