@hostwebhook/node 1.4.0 → 1.4.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/dist/index.d.mts +60 -4
- package/dist/index.d.ts +60 -4
- package/dist/index.js +152 -1
- package/dist/index.mjs +151 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -219,15 +219,29 @@ interface SendOptions {
|
|
|
219
219
|
* Wait for the full pipeline result via SSE streaming (blocking).
|
|
220
220
|
* The SDK will POST the payload, then open an SSE stream to receive
|
|
221
221
|
* pipeline steps and the final result. Returns when the pipeline completes.
|
|
222
|
-
*
|
|
222
|
+
*
|
|
223
|
+
* **Timeout:** ~125s (slightly above the server's 120s SSE timeout).
|
|
224
|
+
*
|
|
225
|
+
* **When to use:** Short pipelines where you need the result before
|
|
226
|
+
* responding to the user (e.g., validation, quick transforms).
|
|
227
|
+
*
|
|
228
|
+
* **When NOT to use:**
|
|
229
|
+
* - Long-running pipelines (file processing, scraping) — use fire-and-forget instead
|
|
230
|
+
* - Approval nodes — the stream returns `{ awaitingApproval: true }` immediately,
|
|
231
|
+
* it does NOT wait for the approval to be resolved (that can take hours/days)
|
|
232
|
+
* - Delay nodes — if the delay exceeds the 125s timeout, the stream times out
|
|
233
|
+
*
|
|
234
|
+
* For approval workflows, use `responseMode: 'sync'` on the endpoint instead —
|
|
235
|
+
* it returns the approval status inline without blocking the SSE stream.
|
|
223
236
|
*/
|
|
224
237
|
waitForResult?: boolean;
|
|
225
238
|
/**
|
|
226
239
|
* Non-blocking pipeline result callback. `send()` returns immediately with
|
|
227
240
|
* the eventId, and the callback fires in the background when the pipeline
|
|
228
|
-
* completes (via SSE stream).
|
|
241
|
+
* completes (via SSE stream).
|
|
229
242
|
*
|
|
230
|
-
*
|
|
243
|
+
* Same timeout limitations as `waitForResult` (125s max).
|
|
244
|
+
* For long pipelines, use WebSocket subscription instead.
|
|
231
245
|
*
|
|
232
246
|
* @example
|
|
233
247
|
* ```ts
|
|
@@ -340,6 +354,48 @@ declare class HostWebhook {
|
|
|
340
354
|
fileName: string;
|
|
341
355
|
mimeType: string;
|
|
342
356
|
}> | SendOptions, maybeOptions?: SendOptions): Promise<SendResult>;
|
|
357
|
+
/**
|
|
358
|
+
* Send a webhook and stream pipeline steps as an async iterable.
|
|
359
|
+
*
|
|
360
|
+
* Much simpler than `waitForResult` or `onResult` callbacks.
|
|
361
|
+
* Iteration ends when the pipeline completes. Access `stream.result`
|
|
362
|
+
* after the loop for the final pipeline result.
|
|
363
|
+
*
|
|
364
|
+
* @overload stream(payload) — uses default token
|
|
365
|
+
* @overload stream(token, payload) — specify token per call
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* const hw = new HostWebhook({ apiKey: 'hwk_...', token: 'ingress_token' });
|
|
370
|
+
* const stream = await hw.stream({ event: 'order.created' });
|
|
371
|
+
*
|
|
372
|
+
* for await (const step of stream) {
|
|
373
|
+
* console.log(`${step.nodeType}: ${step.status}`);
|
|
374
|
+
* }
|
|
375
|
+
* console.log('Done:', stream.result);
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
stream(tokenOrPayload: string | Record<string, unknown>, maybePayload?: Record<string, unknown>): Promise<PipelineStream>;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Async iterable pipeline stream. Yields PipelineStep objects as
|
|
382
|
+
* nodes execute, then completes. Access `.result` after iteration.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* const stream = await hw.stream(payload);
|
|
387
|
+
* for await (const step of stream) {
|
|
388
|
+
* console.log(step.nodeType, step.status);
|
|
389
|
+
* }
|
|
390
|
+
* console.log(stream.result); // { syncStatus: 200, ... }
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
declare class PipelineStream {
|
|
394
|
+
#private;
|
|
395
|
+
result: SendResult;
|
|
396
|
+
constructor(steps: PipelineStep[], result: SendResult);
|
|
397
|
+
static connect(streamUrl: string, ingressResult: SendResult): Promise<PipelineStream>;
|
|
398
|
+
[Symbol.asyncIterator](): AsyncGenerator<PipelineStep, void, undefined>;
|
|
343
399
|
}
|
|
344
400
|
/**
|
|
345
401
|
* Standalone send function (no class instantiation needed).
|
|
@@ -352,4 +408,4 @@ declare class HostWebhook {
|
|
|
352
408
|
*/
|
|
353
409
|
declare function send(config: HostWebhookConfig, payload: Record<string, unknown>, options?: SendOptions): Promise<SendResult>;
|
|
354
410
|
|
|
355
|
-
export { HostWebhook, type HostWebhookConfig, type PipelineStep, SendError, type SendOptions, type SendResult, type StaticSignature, VerificationError, type VerifyOptions, type WebhookHeaders, createNestGuard, expressMiddleware, fastifyHook, parseHeaders, send, verify };
|
|
411
|
+
export { HostWebhook, type HostWebhookConfig, type PipelineStep, PipelineStream, SendError, type SendOptions, type SendResult, type StaticSignature, VerificationError, type VerifyOptions, type WebhookHeaders, createNestGuard, expressMiddleware, fastifyHook, parseHeaders, send, verify };
|
package/dist/index.d.ts
CHANGED
|
@@ -219,15 +219,29 @@ interface SendOptions {
|
|
|
219
219
|
* Wait for the full pipeline result via SSE streaming (blocking).
|
|
220
220
|
* The SDK will POST the payload, then open an SSE stream to receive
|
|
221
221
|
* pipeline steps and the final result. Returns when the pipeline completes.
|
|
222
|
-
*
|
|
222
|
+
*
|
|
223
|
+
* **Timeout:** ~125s (slightly above the server's 120s SSE timeout).
|
|
224
|
+
*
|
|
225
|
+
* **When to use:** Short pipelines where you need the result before
|
|
226
|
+
* responding to the user (e.g., validation, quick transforms).
|
|
227
|
+
*
|
|
228
|
+
* **When NOT to use:**
|
|
229
|
+
* - Long-running pipelines (file processing, scraping) — use fire-and-forget instead
|
|
230
|
+
* - Approval nodes — the stream returns `{ awaitingApproval: true }` immediately,
|
|
231
|
+
* it does NOT wait for the approval to be resolved (that can take hours/days)
|
|
232
|
+
* - Delay nodes — if the delay exceeds the 125s timeout, the stream times out
|
|
233
|
+
*
|
|
234
|
+
* For approval workflows, use `responseMode: 'sync'` on the endpoint instead —
|
|
235
|
+
* it returns the approval status inline without blocking the SSE stream.
|
|
223
236
|
*/
|
|
224
237
|
waitForResult?: boolean;
|
|
225
238
|
/**
|
|
226
239
|
* Non-blocking pipeline result callback. `send()` returns immediately with
|
|
227
240
|
* the eventId, and the callback fires in the background when the pipeline
|
|
228
|
-
* completes (via SSE stream).
|
|
241
|
+
* completes (via SSE stream).
|
|
229
242
|
*
|
|
230
|
-
*
|
|
243
|
+
* Same timeout limitations as `waitForResult` (125s max).
|
|
244
|
+
* For long pipelines, use WebSocket subscription instead.
|
|
231
245
|
*
|
|
232
246
|
* @example
|
|
233
247
|
* ```ts
|
|
@@ -340,6 +354,48 @@ declare class HostWebhook {
|
|
|
340
354
|
fileName: string;
|
|
341
355
|
mimeType: string;
|
|
342
356
|
}> | SendOptions, maybeOptions?: SendOptions): Promise<SendResult>;
|
|
357
|
+
/**
|
|
358
|
+
* Send a webhook and stream pipeline steps as an async iterable.
|
|
359
|
+
*
|
|
360
|
+
* Much simpler than `waitForResult` or `onResult` callbacks.
|
|
361
|
+
* Iteration ends when the pipeline completes. Access `stream.result`
|
|
362
|
+
* after the loop for the final pipeline result.
|
|
363
|
+
*
|
|
364
|
+
* @overload stream(payload) — uses default token
|
|
365
|
+
* @overload stream(token, payload) — specify token per call
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* const hw = new HostWebhook({ apiKey: 'hwk_...', token: 'ingress_token' });
|
|
370
|
+
* const stream = await hw.stream({ event: 'order.created' });
|
|
371
|
+
*
|
|
372
|
+
* for await (const step of stream) {
|
|
373
|
+
* console.log(`${step.nodeType}: ${step.status}`);
|
|
374
|
+
* }
|
|
375
|
+
* console.log('Done:', stream.result);
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
stream(tokenOrPayload: string | Record<string, unknown>, maybePayload?: Record<string, unknown>): Promise<PipelineStream>;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Async iterable pipeline stream. Yields PipelineStep objects as
|
|
382
|
+
* nodes execute, then completes. Access `.result` after iteration.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* const stream = await hw.stream(payload);
|
|
387
|
+
* for await (const step of stream) {
|
|
388
|
+
* console.log(step.nodeType, step.status);
|
|
389
|
+
* }
|
|
390
|
+
* console.log(stream.result); // { syncStatus: 200, ... }
|
|
391
|
+
* ```
|
|
392
|
+
*/
|
|
393
|
+
declare class PipelineStream {
|
|
394
|
+
#private;
|
|
395
|
+
result: SendResult;
|
|
396
|
+
constructor(steps: PipelineStep[], result: SendResult);
|
|
397
|
+
static connect(streamUrl: string, ingressResult: SendResult): Promise<PipelineStream>;
|
|
398
|
+
[Symbol.asyncIterator](): AsyncGenerator<PipelineStep, void, undefined>;
|
|
343
399
|
}
|
|
344
400
|
/**
|
|
345
401
|
* Standalone send function (no class instantiation needed).
|
|
@@ -352,4 +408,4 @@ declare class HostWebhook {
|
|
|
352
408
|
*/
|
|
353
409
|
declare function send(config: HostWebhookConfig, payload: Record<string, unknown>, options?: SendOptions): Promise<SendResult>;
|
|
354
410
|
|
|
355
|
-
export { HostWebhook, type HostWebhookConfig, type PipelineStep, SendError, type SendOptions, type SendResult, type StaticSignature, VerificationError, type VerifyOptions, type WebhookHeaders, createNestGuard, expressMiddleware, fastifyHook, parseHeaders, send, verify };
|
|
411
|
+
export { HostWebhook, type HostWebhookConfig, type PipelineStep, PipelineStream, SendError, type SendOptions, type SendResult, type StaticSignature, VerificationError, type VerifyOptions, type WebhookHeaders, createNestGuard, expressMiddleware, fastifyHook, parseHeaders, send, verify };
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,7 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
|
|
|
29
29
|
var index_exports = {};
|
|
30
30
|
__export(index_exports, {
|
|
31
31
|
HostWebhook: () => HostWebhook,
|
|
32
|
+
PipelineStream: () => PipelineStream,
|
|
32
33
|
SendError: () => SendError,
|
|
33
34
|
VerificationError: () => VerificationError,
|
|
34
35
|
createNestGuard: () => createNestGuard,
|
|
@@ -337,6 +338,47 @@ var HostWebhook = class {
|
|
|
337
338
|
}
|
|
338
339
|
return __privateMethod(this, _HostWebhook_instances, doSend_fn).call(this, token, enrichedPayload, options);
|
|
339
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Send a webhook and stream pipeline steps as an async iterable.
|
|
343
|
+
*
|
|
344
|
+
* Much simpler than `waitForResult` or `onResult` callbacks.
|
|
345
|
+
* Iteration ends when the pipeline completes. Access `stream.result`
|
|
346
|
+
* after the loop for the final pipeline result.
|
|
347
|
+
*
|
|
348
|
+
* @overload stream(payload) — uses default token
|
|
349
|
+
* @overload stream(token, payload) — specify token per call
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```ts
|
|
353
|
+
* const hw = new HostWebhook({ apiKey: 'hwk_...', token: 'ingress_token' });
|
|
354
|
+
* const stream = await hw.stream({ event: 'order.created' });
|
|
355
|
+
*
|
|
356
|
+
* for await (const step of stream) {
|
|
357
|
+
* console.log(`${step.nodeType}: ${step.status}`);
|
|
358
|
+
* }
|
|
359
|
+
* console.log('Done:', stream.result);
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
async stream(tokenOrPayload, maybePayload) {
|
|
363
|
+
let token;
|
|
364
|
+
let payload;
|
|
365
|
+
if (typeof tokenOrPayload === "string") {
|
|
366
|
+
token = tokenOrPayload;
|
|
367
|
+
payload = maybePayload ?? {};
|
|
368
|
+
} else {
|
|
369
|
+
if (!__privateGet(this, _token)) {
|
|
370
|
+
throw new Error("HostWebhook: no default token set \u2014 pass token as first argument: stream(token, payload)");
|
|
371
|
+
}
|
|
372
|
+
token = __privateGet(this, _token);
|
|
373
|
+
payload = tokenOrPayload;
|
|
374
|
+
}
|
|
375
|
+
const ingressResult = await __privateMethod(this, _HostWebhook_instances, doSend_fn).call(this, token, payload);
|
|
376
|
+
if (!ingressResult.streamToken) {
|
|
377
|
+
return new PipelineStream([], ingressResult);
|
|
378
|
+
}
|
|
379
|
+
const streamUrl = `${__privateGet(this, _baseUrl)}/api/events/${ingressResult.eventId}/stream?token=${ingressResult.streamToken}`;
|
|
380
|
+
return PipelineStream.connect(streamUrl, ingressResult);
|
|
381
|
+
}
|
|
340
382
|
};
|
|
341
383
|
_apiKey = new WeakMap();
|
|
342
384
|
_token = new WeakMap();
|
|
@@ -355,7 +397,7 @@ doSend_fn = async function(token, payload, options) {
|
|
|
355
397
|
}
|
|
356
398
|
const headers = {
|
|
357
399
|
"Content-Type": options?.contentType ?? "application/json",
|
|
358
|
-
"User-Agent": `HostWebhook-SDK/${"1.4.
|
|
400
|
+
"User-Agent": `HostWebhook-SDK/${"1.4.1"}`,
|
|
359
401
|
"X-HostWebhook-Sent-At": (/* @__PURE__ */ new Date()).toISOString(),
|
|
360
402
|
...tz ? { "X-HostWebhook-Timezone": tz } : {},
|
|
361
403
|
...__privateGet(this, _defaultHeaders),
|
|
@@ -492,6 +534,114 @@ waitForStream_fn = async function(ingressResult, onStep) {
|
|
|
492
534
|
clearTimeout(timer);
|
|
493
535
|
}
|
|
494
536
|
};
|
|
537
|
+
var _steps, _pendingSteps, _done, _doneResult, _resolve, _reader, _abortController;
|
|
538
|
+
var _PipelineStream = class _PipelineStream {
|
|
539
|
+
constructor(steps, result) {
|
|
540
|
+
__privateAdd(this, _steps);
|
|
541
|
+
__privateAdd(this, _pendingSteps, []);
|
|
542
|
+
__privateAdd(this, _done, false);
|
|
543
|
+
__privateAdd(this, _doneResult, null);
|
|
544
|
+
__privateAdd(this, _resolve, null);
|
|
545
|
+
__privateAdd(this, _reader, null);
|
|
546
|
+
__privateAdd(this, _abortController, null);
|
|
547
|
+
__privateSet(this, _steps, steps);
|
|
548
|
+
this.result = result;
|
|
549
|
+
__privateSet(this, _done, true);
|
|
550
|
+
}
|
|
551
|
+
static async connect(streamUrl, ingressResult) {
|
|
552
|
+
const stream = new _PipelineStream([], ingressResult);
|
|
553
|
+
__privateSet(stream, _done, false);
|
|
554
|
+
const controller = new AbortController();
|
|
555
|
+
__privateSet(stream, _abortController, controller);
|
|
556
|
+
const timer = setTimeout(() => controller.abort(), 125e3);
|
|
557
|
+
try {
|
|
558
|
+
const res = await fetch(streamUrl, { signal: controller.signal });
|
|
559
|
+
if (!res.ok) {
|
|
560
|
+
clearTimeout(timer);
|
|
561
|
+
__privateSet(stream, _done, true);
|
|
562
|
+
return stream;
|
|
563
|
+
}
|
|
564
|
+
__privateSet(stream, _reader, res.body.getReader());
|
|
565
|
+
const decoder = new TextDecoder();
|
|
566
|
+
let buffer = "";
|
|
567
|
+
let currentEvent = "";
|
|
568
|
+
(async () => {
|
|
569
|
+
var _a, _b, _c, _d;
|
|
570
|
+
try {
|
|
571
|
+
while (true) {
|
|
572
|
+
const { done, value } = await __privateGet(stream, _reader).read();
|
|
573
|
+
if (done) break;
|
|
574
|
+
buffer += decoder.decode(value, { stream: true });
|
|
575
|
+
const lines = buffer.split("\n");
|
|
576
|
+
buffer = lines.pop();
|
|
577
|
+
for (const line of lines) {
|
|
578
|
+
if (line.startsWith("event: ")) {
|
|
579
|
+
currentEvent = line.slice(7).trim();
|
|
580
|
+
} else if (line.startsWith("data: ")) {
|
|
581
|
+
try {
|
|
582
|
+
const parsed = JSON.parse(line.slice(6));
|
|
583
|
+
if (currentEvent === "step") {
|
|
584
|
+
__privateGet(stream, _steps).push(parsed);
|
|
585
|
+
__privateGet(stream, _pendingSteps).push(parsed);
|
|
586
|
+
(_a = __privateGet(stream, _resolve)) == null ? void 0 : _a.call(stream);
|
|
587
|
+
} else if (currentEvent === "done") {
|
|
588
|
+
__privateSet(stream, _doneResult, parsed);
|
|
589
|
+
stream.result = {
|
|
590
|
+
...ingressResult,
|
|
591
|
+
steps: __privateGet(stream, _steps),
|
|
592
|
+
syncStatus: parsed.status ?? 200,
|
|
593
|
+
...parsed.body ?? parsed
|
|
594
|
+
};
|
|
595
|
+
__privateSet(stream, _done, true);
|
|
596
|
+
(_b = __privateGet(stream, _resolve)) == null ? void 0 : _b.call(stream);
|
|
597
|
+
return;
|
|
598
|
+
} else if (currentEvent === "timeout") {
|
|
599
|
+
stream.result = { ...ingressResult, steps: __privateGet(stream, _steps), syncStatus: 408, error: "Pipeline timeout" };
|
|
600
|
+
__privateSet(stream, _done, true);
|
|
601
|
+
(_c = __privateGet(stream, _resolve)) == null ? void 0 : _c.call(stream);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
} catch {
|
|
605
|
+
}
|
|
606
|
+
currentEvent = "";
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
} catch {
|
|
611
|
+
} finally {
|
|
612
|
+
clearTimeout(timer);
|
|
613
|
+
__privateSet(stream, _done, true);
|
|
614
|
+
(_d = __privateGet(stream, _resolve)) == null ? void 0 : _d.call(stream);
|
|
615
|
+
}
|
|
616
|
+
})();
|
|
617
|
+
} catch {
|
|
618
|
+
clearTimeout(timer);
|
|
619
|
+
__privateSet(stream, _done, true);
|
|
620
|
+
}
|
|
621
|
+
return stream;
|
|
622
|
+
}
|
|
623
|
+
async *[Symbol.asyncIterator]() {
|
|
624
|
+
while (true) {
|
|
625
|
+
if (__privateGet(this, _pendingSteps).length > 0) {
|
|
626
|
+
yield __privateGet(this, _pendingSteps).shift();
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
if (__privateGet(this, _done)) return;
|
|
630
|
+
await new Promise((resolve) => {
|
|
631
|
+
__privateSet(this, _resolve, resolve);
|
|
632
|
+
});
|
|
633
|
+
__privateSet(this, _resolve, null);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
_steps = new WeakMap();
|
|
638
|
+
_pendingSteps = new WeakMap();
|
|
639
|
+
_done = new WeakMap();
|
|
640
|
+
_doneResult = new WeakMap();
|
|
641
|
+
_resolve = new WeakMap();
|
|
642
|
+
_reader = new WeakMap();
|
|
643
|
+
_abortController = new WeakMap();
|
|
644
|
+
var PipelineStream = _PipelineStream;
|
|
495
645
|
async function send(config, payload, options) {
|
|
496
646
|
const client = new HostWebhook(config);
|
|
497
647
|
return client.send(payload, options);
|
|
@@ -499,6 +649,7 @@ async function send(config, payload, options) {
|
|
|
499
649
|
// Annotate the CommonJS export names for ESM import in node:
|
|
500
650
|
0 && (module.exports = {
|
|
501
651
|
HostWebhook,
|
|
652
|
+
PipelineStream,
|
|
502
653
|
SendError,
|
|
503
654
|
VerificationError,
|
|
504
655
|
createNestGuard,
|
package/dist/index.mjs
CHANGED
|
@@ -304,6 +304,47 @@ var HostWebhook = class {
|
|
|
304
304
|
}
|
|
305
305
|
return __privateMethod(this, _HostWebhook_instances, doSend_fn).call(this, token, enrichedPayload, options);
|
|
306
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Send a webhook and stream pipeline steps as an async iterable.
|
|
309
|
+
*
|
|
310
|
+
* Much simpler than `waitForResult` or `onResult` callbacks.
|
|
311
|
+
* Iteration ends when the pipeline completes. Access `stream.result`
|
|
312
|
+
* after the loop for the final pipeline result.
|
|
313
|
+
*
|
|
314
|
+
* @overload stream(payload) — uses default token
|
|
315
|
+
* @overload stream(token, payload) — specify token per call
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* const hw = new HostWebhook({ apiKey: 'hwk_...', token: 'ingress_token' });
|
|
320
|
+
* const stream = await hw.stream({ event: 'order.created' });
|
|
321
|
+
*
|
|
322
|
+
* for await (const step of stream) {
|
|
323
|
+
* console.log(`${step.nodeType}: ${step.status}`);
|
|
324
|
+
* }
|
|
325
|
+
* console.log('Done:', stream.result);
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
async stream(tokenOrPayload, maybePayload) {
|
|
329
|
+
let token;
|
|
330
|
+
let payload;
|
|
331
|
+
if (typeof tokenOrPayload === "string") {
|
|
332
|
+
token = tokenOrPayload;
|
|
333
|
+
payload = maybePayload ?? {};
|
|
334
|
+
} else {
|
|
335
|
+
if (!__privateGet(this, _token)) {
|
|
336
|
+
throw new Error("HostWebhook: no default token set \u2014 pass token as first argument: stream(token, payload)");
|
|
337
|
+
}
|
|
338
|
+
token = __privateGet(this, _token);
|
|
339
|
+
payload = tokenOrPayload;
|
|
340
|
+
}
|
|
341
|
+
const ingressResult = await __privateMethod(this, _HostWebhook_instances, doSend_fn).call(this, token, payload);
|
|
342
|
+
if (!ingressResult.streamToken) {
|
|
343
|
+
return new PipelineStream([], ingressResult);
|
|
344
|
+
}
|
|
345
|
+
const streamUrl = `${__privateGet(this, _baseUrl)}/api/events/${ingressResult.eventId}/stream?token=${ingressResult.streamToken}`;
|
|
346
|
+
return PipelineStream.connect(streamUrl, ingressResult);
|
|
347
|
+
}
|
|
307
348
|
};
|
|
308
349
|
_apiKey = new WeakMap();
|
|
309
350
|
_token = new WeakMap();
|
|
@@ -322,7 +363,7 @@ doSend_fn = async function(token, payload, options) {
|
|
|
322
363
|
}
|
|
323
364
|
const headers = {
|
|
324
365
|
"Content-Type": options?.contentType ?? "application/json",
|
|
325
|
-
"User-Agent": `HostWebhook-SDK/${"1.4.
|
|
366
|
+
"User-Agent": `HostWebhook-SDK/${"1.4.1"}`,
|
|
326
367
|
"X-HostWebhook-Sent-At": (/* @__PURE__ */ new Date()).toISOString(),
|
|
327
368
|
...tz ? { "X-HostWebhook-Timezone": tz } : {},
|
|
328
369
|
...__privateGet(this, _defaultHeaders),
|
|
@@ -459,12 +500,121 @@ waitForStream_fn = async function(ingressResult, onStep) {
|
|
|
459
500
|
clearTimeout(timer);
|
|
460
501
|
}
|
|
461
502
|
};
|
|
503
|
+
var _steps, _pendingSteps, _done, _doneResult, _resolve, _reader, _abortController;
|
|
504
|
+
var _PipelineStream = class _PipelineStream {
|
|
505
|
+
constructor(steps, result) {
|
|
506
|
+
__privateAdd(this, _steps);
|
|
507
|
+
__privateAdd(this, _pendingSteps, []);
|
|
508
|
+
__privateAdd(this, _done, false);
|
|
509
|
+
__privateAdd(this, _doneResult, null);
|
|
510
|
+
__privateAdd(this, _resolve, null);
|
|
511
|
+
__privateAdd(this, _reader, null);
|
|
512
|
+
__privateAdd(this, _abortController, null);
|
|
513
|
+
__privateSet(this, _steps, steps);
|
|
514
|
+
this.result = result;
|
|
515
|
+
__privateSet(this, _done, true);
|
|
516
|
+
}
|
|
517
|
+
static async connect(streamUrl, ingressResult) {
|
|
518
|
+
const stream = new _PipelineStream([], ingressResult);
|
|
519
|
+
__privateSet(stream, _done, false);
|
|
520
|
+
const controller = new AbortController();
|
|
521
|
+
__privateSet(stream, _abortController, controller);
|
|
522
|
+
const timer = setTimeout(() => controller.abort(), 125e3);
|
|
523
|
+
try {
|
|
524
|
+
const res = await fetch(streamUrl, { signal: controller.signal });
|
|
525
|
+
if (!res.ok) {
|
|
526
|
+
clearTimeout(timer);
|
|
527
|
+
__privateSet(stream, _done, true);
|
|
528
|
+
return stream;
|
|
529
|
+
}
|
|
530
|
+
__privateSet(stream, _reader, res.body.getReader());
|
|
531
|
+
const decoder = new TextDecoder();
|
|
532
|
+
let buffer = "";
|
|
533
|
+
let currentEvent = "";
|
|
534
|
+
(async () => {
|
|
535
|
+
var _a, _b, _c, _d;
|
|
536
|
+
try {
|
|
537
|
+
while (true) {
|
|
538
|
+
const { done, value } = await __privateGet(stream, _reader).read();
|
|
539
|
+
if (done) break;
|
|
540
|
+
buffer += decoder.decode(value, { stream: true });
|
|
541
|
+
const lines = buffer.split("\n");
|
|
542
|
+
buffer = lines.pop();
|
|
543
|
+
for (const line of lines) {
|
|
544
|
+
if (line.startsWith("event: ")) {
|
|
545
|
+
currentEvent = line.slice(7).trim();
|
|
546
|
+
} else if (line.startsWith("data: ")) {
|
|
547
|
+
try {
|
|
548
|
+
const parsed = JSON.parse(line.slice(6));
|
|
549
|
+
if (currentEvent === "step") {
|
|
550
|
+
__privateGet(stream, _steps).push(parsed);
|
|
551
|
+
__privateGet(stream, _pendingSteps).push(parsed);
|
|
552
|
+
(_a = __privateGet(stream, _resolve)) == null ? void 0 : _a.call(stream);
|
|
553
|
+
} else if (currentEvent === "done") {
|
|
554
|
+
__privateSet(stream, _doneResult, parsed);
|
|
555
|
+
stream.result = {
|
|
556
|
+
...ingressResult,
|
|
557
|
+
steps: __privateGet(stream, _steps),
|
|
558
|
+
syncStatus: parsed.status ?? 200,
|
|
559
|
+
...parsed.body ?? parsed
|
|
560
|
+
};
|
|
561
|
+
__privateSet(stream, _done, true);
|
|
562
|
+
(_b = __privateGet(stream, _resolve)) == null ? void 0 : _b.call(stream);
|
|
563
|
+
return;
|
|
564
|
+
} else if (currentEvent === "timeout") {
|
|
565
|
+
stream.result = { ...ingressResult, steps: __privateGet(stream, _steps), syncStatus: 408, error: "Pipeline timeout" };
|
|
566
|
+
__privateSet(stream, _done, true);
|
|
567
|
+
(_c = __privateGet(stream, _resolve)) == null ? void 0 : _c.call(stream);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
} catch {
|
|
571
|
+
}
|
|
572
|
+
currentEvent = "";
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
} catch {
|
|
577
|
+
} finally {
|
|
578
|
+
clearTimeout(timer);
|
|
579
|
+
__privateSet(stream, _done, true);
|
|
580
|
+
(_d = __privateGet(stream, _resolve)) == null ? void 0 : _d.call(stream);
|
|
581
|
+
}
|
|
582
|
+
})();
|
|
583
|
+
} catch {
|
|
584
|
+
clearTimeout(timer);
|
|
585
|
+
__privateSet(stream, _done, true);
|
|
586
|
+
}
|
|
587
|
+
return stream;
|
|
588
|
+
}
|
|
589
|
+
async *[Symbol.asyncIterator]() {
|
|
590
|
+
while (true) {
|
|
591
|
+
if (__privateGet(this, _pendingSteps).length > 0) {
|
|
592
|
+
yield __privateGet(this, _pendingSteps).shift();
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
if (__privateGet(this, _done)) return;
|
|
596
|
+
await new Promise((resolve) => {
|
|
597
|
+
__privateSet(this, _resolve, resolve);
|
|
598
|
+
});
|
|
599
|
+
__privateSet(this, _resolve, null);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
_steps = new WeakMap();
|
|
604
|
+
_pendingSteps = new WeakMap();
|
|
605
|
+
_done = new WeakMap();
|
|
606
|
+
_doneResult = new WeakMap();
|
|
607
|
+
_resolve = new WeakMap();
|
|
608
|
+
_reader = new WeakMap();
|
|
609
|
+
_abortController = new WeakMap();
|
|
610
|
+
var PipelineStream = _PipelineStream;
|
|
462
611
|
async function send(config, payload, options) {
|
|
463
612
|
const client = new HostWebhook(config);
|
|
464
613
|
return client.send(payload, options);
|
|
465
614
|
}
|
|
466
615
|
export {
|
|
467
616
|
HostWebhook,
|
|
617
|
+
PipelineStream,
|
|
468
618
|
SendError,
|
|
469
619
|
VerificationError,
|
|
470
620
|
createNestGuard,
|