@engini/sdk 0.21.0 → 0.23.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 +16 -0
- package/dist/resources/triggers.d.ts +8 -3
- package/dist/resources/triggers.js +68 -8
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -153,6 +153,22 @@ for await (const event of client.triggers.subscribe({ triggerId: trigger.id }))
|
|
|
153
153
|
}
|
|
154
154
|
```
|
|
155
155
|
|
|
156
|
+
Pass a `signal` to stop the subscription on your own terms, including mid-read on an idle
|
|
157
|
+
stream - the abort reaches the pending connection immediately, it is not just abandoned:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
const controller = new AbortController();
|
|
161
|
+
process.once("SIGINT", () => controller.abort());
|
|
162
|
+
|
|
163
|
+
for await (const event of client.triggers.subscribe({
|
|
164
|
+
triggerId: trigger.id,
|
|
165
|
+
signal: controller.signal,
|
|
166
|
+
})) {
|
|
167
|
+
console.log(event.id, event.payload);
|
|
168
|
+
}
|
|
169
|
+
// The generator returns normally once aborted - no try/catch needed for this path.
|
|
170
|
+
```
|
|
171
|
+
|
|
156
172
|
`client.toolset(...)` builds a local toolset (no I/O until used) or loads a server one via
|
|
157
173
|
`client.toolset({ toolsetId })`.
|
|
158
174
|
|
|
@@ -233,9 +233,9 @@ export declare class TriggersResource {
|
|
|
233
233
|
/**
|
|
234
234
|
* Create a trigger instance from a trigger type.
|
|
235
235
|
*
|
|
236
|
-
* Pass `connectionId` or `connectionName`;
|
|
237
|
-
*
|
|
238
|
-
*
|
|
236
|
+
* Pass `connectionId` or `connectionName`; omitting both is legal only for a trigger
|
|
237
|
+
* type whose application does not require a connection, where the server substitutes
|
|
238
|
+
* its own default connection — otherwise it answers `400 EMPTY_QUERY`.
|
|
239
239
|
*
|
|
240
240
|
* Which of `schedule` / `pollIntervalMinutes` / `listenColumns` apply is per trigger
|
|
241
241
|
* type — read `types.get(slug).parameters` first.
|
|
@@ -341,6 +341,10 @@ export declare class TriggersResource {
|
|
|
341
341
|
* exhausts it.
|
|
342
342
|
* @param params.heartbeatTimeoutMs Milliseconds of total silence before the socket is
|
|
343
343
|
* treated as dead.
|
|
344
|
+
* @param params.signal Abort the subscription immediately, including mid-read on an
|
|
345
|
+
* idle connection. The generator returns normally rather than throwing; the
|
|
346
|
+
* in-flight connection is aborted (not merely abandoned), so no lingering socket
|
|
347
|
+
* keeps the process alive after the caller stops iterating.
|
|
344
348
|
*/
|
|
345
349
|
subscribe(params?: {
|
|
346
350
|
triggerId?: string;
|
|
@@ -350,6 +354,7 @@ export declare class TriggersResource {
|
|
|
350
354
|
autoResume?: boolean;
|
|
351
355
|
maxReconnects?: number;
|
|
352
356
|
heartbeatTimeoutMs?: number;
|
|
357
|
+
signal?: AbortSignal;
|
|
353
358
|
}): AsyncGenerator<TriggerEventView>;
|
|
354
359
|
}
|
|
355
360
|
export {};
|
|
@@ -82,6 +82,37 @@ export const backoffTimer = {
|
|
|
82
82
|
setTimeout(resolve, ms);
|
|
83
83
|
}),
|
|
84
84
|
};
|
|
85
|
+
/**
|
|
86
|
+
* `backoffTimer.sleep`, but resolved early by an abort signal.
|
|
87
|
+
*
|
|
88
|
+
* The `signal?.aborted` guards at the top of `subscribe`'s loop and the head of its
|
|
89
|
+
* `catch` only run *after* whichever `await` they follow settles — and the backoff they
|
|
90
|
+
* guard can be `MAX_RETRY_AFTER_MS` (300s) on the 429 path. Without this, a Ctrl-C
|
|
91
|
+
* landing inside that wait would sit until the sleep finishes on its own.
|
|
92
|
+
*/
|
|
93
|
+
function interruptibleSleep(ms, signal) {
|
|
94
|
+
if (signal === undefined)
|
|
95
|
+
return backoffTimer.sleep(ms);
|
|
96
|
+
if (signal.aborted)
|
|
97
|
+
return Promise.resolve();
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
let settled = false;
|
|
100
|
+
const onAbort = () => {
|
|
101
|
+
if (settled)
|
|
102
|
+
return;
|
|
103
|
+
settled = true;
|
|
104
|
+
resolve();
|
|
105
|
+
};
|
|
106
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
107
|
+
void backoffTimer.sleep(ms).then(() => {
|
|
108
|
+
if (settled)
|
|
109
|
+
return;
|
|
110
|
+
settled = true;
|
|
111
|
+
signal.removeEventListener("abort", onAbort);
|
|
112
|
+
resolve();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
85
116
|
/**
|
|
86
117
|
* Map the trigger wire codes onto their typed errors.
|
|
87
118
|
*
|
|
@@ -457,9 +488,9 @@ export class TriggersResource {
|
|
|
457
488
|
/**
|
|
458
489
|
* Create a trigger instance from a trigger type.
|
|
459
490
|
*
|
|
460
|
-
* Pass `connectionId` or `connectionName`;
|
|
461
|
-
*
|
|
462
|
-
*
|
|
491
|
+
* Pass `connectionId` or `connectionName`; omitting both is legal only for a trigger
|
|
492
|
+
* type whose application does not require a connection, where the server substitutes
|
|
493
|
+
* its own default connection — otherwise it answers `400 EMPTY_QUERY`.
|
|
463
494
|
*
|
|
464
495
|
* Which of `schedule` / `pollIntervalMinutes` / `listenColumns` apply is per trigger
|
|
465
496
|
* type — read `types.get(slug).parameters` first.
|
|
@@ -602,8 +633,19 @@ export class TriggersResource {
|
|
|
602
633
|
* written at the deadline — the body is simply truncated. It is indistinguishable from
|
|
603
634
|
* a network blip by design, and handled the same way.
|
|
604
635
|
*/
|
|
605
|
-
async *streamOnce(query, heartbeatTimeoutMs, progress) {
|
|
636
|
+
async *streamOnce(query, heartbeatTimeoutMs, progress, signal) {
|
|
606
637
|
const controller = new AbortController();
|
|
638
|
+
// Bridges the caller's signal onto this attempt's own controller, which also carries
|
|
639
|
+
// the watchdog's aborts. `{ once: true }` on the listener pairs with the explicit
|
|
640
|
+
// removal below — this generator is re-created on every reconnect, so a listener left
|
|
641
|
+
// on a long-lived external `signal` would accumulate across an hour of resumes.
|
|
642
|
+
const onExternalAbort = () => controller.abort();
|
|
643
|
+
if (signal !== undefined) {
|
|
644
|
+
if (signal.aborted)
|
|
645
|
+
controller.abort();
|
|
646
|
+
else
|
|
647
|
+
signal.addEventListener("abort", onExternalAbort, { once: true });
|
|
648
|
+
}
|
|
607
649
|
// The heartbeat watchdog. The server promises a frame every 20s, so silence past the
|
|
608
650
|
// timeout means the socket is dead even though nothing closed it; aborting turns that
|
|
609
651
|
// into the same resumable ending as a dropped connection.
|
|
@@ -716,6 +758,8 @@ export class TriggersResource {
|
|
|
716
758
|
}
|
|
717
759
|
finally {
|
|
718
760
|
disarmWatchdog();
|
|
761
|
+
if (signal !== undefined)
|
|
762
|
+
signal.removeEventListener("abort", onExternalAbort);
|
|
719
763
|
// `abort()` first: it is synchronous and guaranteed, so the socket is released
|
|
720
764
|
// even though the cancel below may never settle.
|
|
721
765
|
controller.abort();
|
|
@@ -765,14 +809,24 @@ export class TriggersResource {
|
|
|
765
809
|
* exhausts it.
|
|
766
810
|
* @param params.heartbeatTimeoutMs Milliseconds of total silence before the socket is
|
|
767
811
|
* treated as dead.
|
|
812
|
+
* @param params.signal Abort the subscription immediately, including mid-read on an
|
|
813
|
+
* idle connection. The generator returns normally rather than throwing; the
|
|
814
|
+
* in-flight connection is aborted (not merely abandoned), so no lingering socket
|
|
815
|
+
* keeps the process alive after the caller stops iterating.
|
|
768
816
|
*/
|
|
769
817
|
async *subscribe(params = {}) {
|
|
770
818
|
const autoResume = params.autoResume ?? true;
|
|
771
819
|
const maxReconnects = params.maxReconnects ?? MAX_RECONNECTS;
|
|
772
820
|
const heartbeatTimeoutMs = params.heartbeatTimeoutMs ?? HEARTBEAT_TIMEOUT_MS;
|
|
821
|
+
const signal = params.signal;
|
|
773
822
|
let cursor = params.since;
|
|
774
823
|
let reconnects = 0;
|
|
775
824
|
for (;;) {
|
|
825
|
+
// Every path that lands back at the top of this loop passes through here first,
|
|
826
|
+
// including after a backoff sleep — so this and the mirror guard at the head of
|
|
827
|
+
// `catch` are what stop an abort from being reinterpreted as "reconnect".
|
|
828
|
+
if (signal?.aborted)
|
|
829
|
+
return;
|
|
776
830
|
const started = Date.now();
|
|
777
831
|
const progress = { sawFrame: false };
|
|
778
832
|
const query = new URLSearchParams();
|
|
@@ -785,7 +839,7 @@ export class TriggersResource {
|
|
|
785
839
|
if (cursor !== undefined)
|
|
786
840
|
query.set("since", cursor);
|
|
787
841
|
try {
|
|
788
|
-
for await (const [kind, frame] of this.streamOnce(query, heartbeatTimeoutMs, progress)) {
|
|
842
|
+
for await (const [kind, frame] of this.streamOnce(query, heartbeatTimeoutMs, progress, signal)) {
|
|
789
843
|
if (kind === "control") {
|
|
790
844
|
if (frame.reconnect !== true) {
|
|
791
845
|
// `!== ""` as well as the type check: Python coerces an empty reason to
|
|
@@ -806,6 +860,12 @@ export class TriggersResource {
|
|
|
806
860
|
}
|
|
807
861
|
}
|
|
808
862
|
catch (err) {
|
|
863
|
+
// An abort surfaces here as `EnginiNetworkError` — the aborted `reader.read()`
|
|
864
|
+
// is wrapped by `streamOnce`'s own catch, and that is also the one error this
|
|
865
|
+
// loop is built to retry. Must precede both branches below, or an interrupt
|
|
866
|
+
// during a read gets reconnected instead of ending the generator.
|
|
867
|
+
if (signal?.aborted)
|
|
868
|
+
return;
|
|
809
869
|
if (err instanceof EnginiRateLimitError) {
|
|
810
870
|
// The base class, not `EnginiTriggerStreamLimitError`: a 429 whose body is not
|
|
811
871
|
// the JSON error envelope — an ingress or CDN limiter, or a future server code
|
|
@@ -821,7 +881,7 @@ export class TriggersResource {
|
|
|
821
881
|
// honouring that literally would retry back-to-back against a server that just
|
|
822
882
|
// said "slow down".
|
|
823
883
|
const delay = Math.max((err.retryAfter ?? 0) * 1000, backoffMs(reconnects));
|
|
824
|
-
await
|
|
884
|
+
await interruptibleSleep(Math.min(delay, MAX_RETRY_AFTER_MS), signal);
|
|
825
885
|
continue;
|
|
826
886
|
}
|
|
827
887
|
if (err instanceof EnginiNetworkError) {
|
|
@@ -834,7 +894,7 @@ export class TriggersResource {
|
|
|
834
894
|
reconnects = nextAttempt(reconnects, started, progress);
|
|
835
895
|
if (reconnects > maxReconnects)
|
|
836
896
|
throw err;
|
|
837
|
-
await
|
|
897
|
+
await interruptibleSleep(backoffMs(reconnects), signal);
|
|
838
898
|
continue;
|
|
839
899
|
}
|
|
840
900
|
throw err;
|
|
@@ -845,7 +905,7 @@ export class TriggersResource {
|
|
|
845
905
|
if (reconnects > maxReconnects) {
|
|
846
906
|
throw new EnginiNetworkError(`The trigger event stream could not be resumed after ${maxReconnects} consecutive attempts.`);
|
|
847
907
|
}
|
|
848
|
-
await
|
|
908
|
+
await interruptibleSleep(backoffMs(reconnects), signal);
|
|
849
909
|
}
|
|
850
910
|
}
|
|
851
911
|
}
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@engini/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "Engini SDK — agent-first ergonomic layer over the Engini Public API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"test": "vitest run"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@engini/client": "0.
|
|
30
|
+
"@engini/client": "0.10.0",
|
|
31
31
|
"undici": "^6.0.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|