@jay-framework/stack-client-runtime 0.26.5 → 0.26.7
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.cjs +186 -109
- package/dist/index.js +186 -109
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -340,6 +340,39 @@ class ActionError extends Error {
|
|
|
340
340
|
}
|
|
341
341
|
}
|
|
342
342
|
let globalOptions = {};
|
|
343
|
+
const ACTION_RESULT_BASE = "/_jay/action-results";
|
|
344
|
+
const TICKET_HEADER = "Jay-Action-Ticket";
|
|
345
|
+
let ticketCounter = 0;
|
|
346
|
+
function getActionSeed() {
|
|
347
|
+
const jay = globalThis.__jay;
|
|
348
|
+
return typeof jay?.actionSeed === "string" ? jay.actionSeed : void 0;
|
|
349
|
+
}
|
|
350
|
+
function deriveTicket() {
|
|
351
|
+
const seed = getActionSeed();
|
|
352
|
+
return seed ? `${seed}.${ticketCounter++}` : void 0;
|
|
353
|
+
}
|
|
354
|
+
function isGatewayStatus(status) {
|
|
355
|
+
return status === 502 || status === 503 || status === 504;
|
|
356
|
+
}
|
|
357
|
+
function isNetworkError(error) {
|
|
358
|
+
return error instanceof TypeError;
|
|
359
|
+
}
|
|
360
|
+
async function fetchWithDeadline(url, init, deadline) {
|
|
361
|
+
const controller = new AbortController();
|
|
362
|
+
const remaining = Math.max(0, deadline - Date.now());
|
|
363
|
+
const timeoutId = setTimeout(() => controller.abort(), remaining);
|
|
364
|
+
try {
|
|
365
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
366
|
+
} finally {
|
|
367
|
+
clearTimeout(timeoutId);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
async function parseActionResponse(response, actionName) {
|
|
371
|
+
const data = await response.json();
|
|
372
|
+
if (data.success) return data.data;
|
|
373
|
+
if (data.error) throw new ActionError(data.error.code, data.error.message);
|
|
374
|
+
throw new ActionError("UNKNOWN_ERROR", `Action '${actionName}' failed`);
|
|
375
|
+
}
|
|
343
376
|
function setActionCallerOptions(options) {
|
|
344
377
|
globalOptions = { ...globalOptions, ...options };
|
|
345
378
|
}
|
|
@@ -413,36 +446,30 @@ function createActionCaller(actionName, method = "POST", options) {
|
|
|
413
446
|
const baseUrl = globalOptions.baseUrl ?? "";
|
|
414
447
|
const useFormData = options?.acceptsFiles && hasFiles(input);
|
|
415
448
|
const url = useFormData ? `${baseUrl}${ACTION_ENDPOINT_BASE}/${actionName}` : buildActionUrl(baseUrl, actionName, method, input);
|
|
449
|
+
const ticket = deriveTicket();
|
|
416
450
|
const fetchOptions = {
|
|
417
451
|
method,
|
|
418
452
|
headers: {
|
|
419
453
|
...useFormData ? {} : { "Content-Type": "application/json" },
|
|
420
|
-
...globalOptions.headers
|
|
454
|
+
...globalOptions.headers,
|
|
455
|
+
...ticket ? { [TICKET_HEADER]: ticket } : {}
|
|
421
456
|
}
|
|
422
457
|
};
|
|
423
458
|
if (method !== "GET") {
|
|
424
459
|
fetchOptions.body = useFormData ? buildFormData(input) : JSON.stringify(input);
|
|
425
460
|
}
|
|
426
461
|
const timeout = globalOptions.timeout ?? 3e4;
|
|
427
|
-
const
|
|
428
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
429
|
-
fetchOptions.signal = controller.signal;
|
|
462
|
+
const deadline = Date.now() + timeout;
|
|
430
463
|
try {
|
|
431
|
-
const response = await
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (data.success) {
|
|
435
|
-
return data.data;
|
|
436
|
-
} else {
|
|
437
|
-
if (data.error) {
|
|
438
|
-
throw new ActionError(data.error.code, data.error.message);
|
|
439
|
-
}
|
|
440
|
-
throw new ActionError("UNKNOWN_ERROR", "Unknown error occurred");
|
|
464
|
+
const response = await fetchWithDeadline(url, fetchOptions, deadline);
|
|
465
|
+
if (ticket && (response.status === 202 || isGatewayStatus(response.status))) {
|
|
466
|
+
return pollForResult(baseUrl, ticket, actionName, deadline);
|
|
441
467
|
}
|
|
468
|
+
return await parseActionResponse(response, actionName);
|
|
442
469
|
} catch (error) {
|
|
443
|
-
|
|
444
|
-
if (
|
|
445
|
-
|
|
470
|
+
if (error instanceof ActionError) throw error;
|
|
471
|
+
if (ticket && isNetworkError(error)) {
|
|
472
|
+
return pollForResult(baseUrl, ticket, actionName, deadline);
|
|
446
473
|
}
|
|
447
474
|
if (error instanceof Error && error.name === "AbortError") {
|
|
448
475
|
throw new ActionError(
|
|
@@ -460,6 +487,53 @@ function createActionCaller(actionName, method = "POST", options) {
|
|
|
460
487
|
}
|
|
461
488
|
};
|
|
462
489
|
}
|
|
490
|
+
async function pollForResult(baseUrl, ticket, actionName, deadline) {
|
|
491
|
+
const url = `${baseUrl}${ACTION_RESULT_BASE}/${ticket}`;
|
|
492
|
+
let noProgressRetries = 0;
|
|
493
|
+
for (; ; ) {
|
|
494
|
+
if (Date.now() >= deadline) {
|
|
495
|
+
throw new ActionError(
|
|
496
|
+
"TIMEOUT",
|
|
497
|
+
`Action '${actionName}' timed out while awaiting result`
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
let response;
|
|
501
|
+
try {
|
|
502
|
+
response = await fetchWithDeadline(
|
|
503
|
+
url,
|
|
504
|
+
{ method: "GET", headers: { ...globalOptions.headers } },
|
|
505
|
+
deadline
|
|
506
|
+
);
|
|
507
|
+
} catch (error) {
|
|
508
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
509
|
+
throw new ActionError(
|
|
510
|
+
"TIMEOUT",
|
|
511
|
+
`Action '${actionName}' timed out while awaiting result`
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
if (isNetworkError(error) && noProgressRetries++ < 2) continue;
|
|
515
|
+
throw new ActionError("NETWORK_ERROR", `Network error awaiting '${actionName}'`);
|
|
516
|
+
}
|
|
517
|
+
if (response.status === 202) {
|
|
518
|
+
noProgressRetries = 0;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (response.status === 404) {
|
|
522
|
+
throw new ActionError(
|
|
523
|
+
"RESULT_NOT_FOUND",
|
|
524
|
+
`Result for '${actionName}' is no longer available (server restarted or result expired)`
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
if (isGatewayStatus(response.status)) {
|
|
528
|
+
if (noProgressRetries++ < 2) continue;
|
|
529
|
+
throw new ActionError(
|
|
530
|
+
"NETWORK_ERROR",
|
|
531
|
+
`Repeated gateway errors awaiting '${actionName}'`
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
return parseActionResponse(response, actionName);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
463
537
|
function buildActionUrl(baseUrl, actionName, method, input) {
|
|
464
538
|
const path = `${ACTION_ENDPOINT_BASE}/${actionName}`;
|
|
465
539
|
const fullUrl = `${baseUrl}${path}`;
|
|
@@ -480,104 +554,107 @@ function buildActionUrl(baseUrl, actionName, method, input) {
|
|
|
480
554
|
return fullUrl;
|
|
481
555
|
}
|
|
482
556
|
function createStreamCaller(actionName, options) {
|
|
483
|
-
return (input) =>
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
error = err instanceof ActionError ? err : new ActionError(
|
|
513
|
-
"NETWORK_ERROR",
|
|
514
|
-
`Network error streaming '${actionName}': ${err.message}`
|
|
515
|
-
);
|
|
516
|
-
if (resolveNext) resolveNext();
|
|
517
|
-
});
|
|
557
|
+
return (input) => streamChunks(actionName, input, options);
|
|
558
|
+
}
|
|
559
|
+
async function* streamChunks(actionName, input, options) {
|
|
560
|
+
const baseUrl = globalOptions.baseUrl ?? "";
|
|
561
|
+
const useFormData = options?.acceptsFiles && hasFiles(input);
|
|
562
|
+
const ticket = deriveTicket();
|
|
563
|
+
const controller = new AbortController();
|
|
564
|
+
let received = 0;
|
|
565
|
+
let noProgressRetries = 0;
|
|
566
|
+
try {
|
|
567
|
+
let response = await openStreamSegment(
|
|
568
|
+
`${baseUrl}${ACTION_ENDPOINT_BASE}/${actionName}`,
|
|
569
|
+
{
|
|
570
|
+
method: "POST",
|
|
571
|
+
headers: {
|
|
572
|
+
...useFormData ? {} : { "Content-Type": "application/json" },
|
|
573
|
+
...globalOptions.headers,
|
|
574
|
+
...ticket ? { [TICKET_HEADER]: ticket } : {}
|
|
575
|
+
},
|
|
576
|
+
body: useFormData ? buildFormData(input) : JSON.stringify(input),
|
|
577
|
+
signal: controller.signal
|
|
578
|
+
},
|
|
579
|
+
actionName
|
|
580
|
+
);
|
|
581
|
+
for (; ; ) {
|
|
582
|
+
let end = "cut";
|
|
583
|
+
const before = received;
|
|
584
|
+
try {
|
|
585
|
+
const reader = response.body.getReader();
|
|
518
586
|
const decoder = new TextDecoder();
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
if (parsed.done) {
|
|
534
|
-
done = true;
|
|
535
|
-
if (resolveNext) resolveNext();
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
if ("chunk" in parsed) {
|
|
539
|
-
chunks.push(parsed.chunk);
|
|
540
|
-
if (resolveNext) resolveNext();
|
|
541
|
-
}
|
|
542
|
-
}
|
|
587
|
+
let buffer = "";
|
|
588
|
+
let finished = false;
|
|
589
|
+
while (!finished) {
|
|
590
|
+
const { done: readerDone, value } = await reader.read();
|
|
591
|
+
if (value) buffer += decoder.decode(value, { stream: true });
|
|
592
|
+
const lines = buffer.split("\n");
|
|
593
|
+
buffer = readerDone ? "" : lines.pop();
|
|
594
|
+
for (const line of lines) {
|
|
595
|
+
if (!line.trim()) continue;
|
|
596
|
+
const parsed = JSON.parse(line);
|
|
597
|
+
if (parsed.error !== void 0 && parsed.error !== null) {
|
|
598
|
+
throw new ActionError("STREAM_ERROR", String(parsed.error));
|
|
543
599
|
}
|
|
544
|
-
if (
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
600
|
+
if (parsed.done) {
|
|
601
|
+
end = "done";
|
|
602
|
+
finished = true;
|
|
603
|
+
break;
|
|
548
604
|
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
await fetchPromise;
|
|
558
|
-
while (true) {
|
|
559
|
-
if (chunks.length > 0) {
|
|
560
|
-
return { value: chunks.shift(), done: false };
|
|
561
|
-
}
|
|
562
|
-
if (error) {
|
|
563
|
-
throw error;
|
|
564
|
-
}
|
|
565
|
-
if (done) {
|
|
566
|
-
return { value: void 0, done: true };
|
|
567
|
-
}
|
|
568
|
-
await new Promise((resolve) => {
|
|
569
|
-
resolveNext = resolve;
|
|
570
|
-
});
|
|
571
|
-
resolveNext = null;
|
|
605
|
+
if (parsed.resume) {
|
|
606
|
+
end = "resume";
|
|
607
|
+
finished = true;
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
if ("chunk" in parsed) {
|
|
611
|
+
received++;
|
|
612
|
+
yield parsed.chunk;
|
|
572
613
|
}
|
|
573
|
-
},
|
|
574
|
-
[Symbol.asyncIterator]() {
|
|
575
|
-
return this;
|
|
576
614
|
}
|
|
577
|
-
|
|
615
|
+
if (readerDone) break;
|
|
616
|
+
}
|
|
617
|
+
} catch (err) {
|
|
618
|
+
if (err instanceof ActionError) throw err;
|
|
619
|
+
end = "cut";
|
|
578
620
|
}
|
|
579
|
-
|
|
580
|
-
|
|
621
|
+
if (end === "done") return;
|
|
622
|
+
if (!ticket) {
|
|
623
|
+
throw new ActionError("STREAM_ERROR", `Stream '${actionName}' ended unexpectedly`);
|
|
624
|
+
}
|
|
625
|
+
const progressed = received > before;
|
|
626
|
+
noProgressRetries = progressed ? 0 : noProgressRetries + 1;
|
|
627
|
+
if (!progressed && noProgressRetries > 2) {
|
|
628
|
+
throw new ActionError("STREAM_ERROR", `Stream '${actionName}' ended unexpectedly`);
|
|
629
|
+
}
|
|
630
|
+
response = await openStreamSegment(
|
|
631
|
+
`${baseUrl}${ACTION_RESULT_BASE}/${ticket}?from=${received}`,
|
|
632
|
+
{ method: "GET", headers: { ...globalOptions.headers }, signal: controller.signal },
|
|
633
|
+
actionName
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
} finally {
|
|
637
|
+
controller.abort();
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
async function openStreamSegment(url, init, actionName) {
|
|
641
|
+
let response;
|
|
642
|
+
try {
|
|
643
|
+
response = await fetch(url, init);
|
|
644
|
+
} catch (err) {
|
|
645
|
+
return emptyCutResponse();
|
|
646
|
+
}
|
|
647
|
+
if (response.status === 404) {
|
|
648
|
+
throw new ActionError(
|
|
649
|
+
"RESULT_NOT_FOUND",
|
|
650
|
+
`Result for '${actionName}' is no longer available (server restarted or result expired)`
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
if (!response.ok || !response.body) return emptyCutResponse();
|
|
654
|
+
return response;
|
|
655
|
+
}
|
|
656
|
+
function emptyCutResponse() {
|
|
657
|
+
return new Response(new Blob([]), { status: 200 });
|
|
581
658
|
}
|
|
582
659
|
function isSimpleObject(obj) {
|
|
583
660
|
if (typeof obj !== "object" || obj === null) {
|
package/dist/index.js
CHANGED
|
@@ -338,6 +338,39 @@ class ActionError extends Error {
|
|
|
338
338
|
}
|
|
339
339
|
}
|
|
340
340
|
let globalOptions = {};
|
|
341
|
+
const ACTION_RESULT_BASE = "/_jay/action-results";
|
|
342
|
+
const TICKET_HEADER = "Jay-Action-Ticket";
|
|
343
|
+
let ticketCounter = 0;
|
|
344
|
+
function getActionSeed() {
|
|
345
|
+
const jay = globalThis.__jay;
|
|
346
|
+
return typeof jay?.actionSeed === "string" ? jay.actionSeed : void 0;
|
|
347
|
+
}
|
|
348
|
+
function deriveTicket() {
|
|
349
|
+
const seed = getActionSeed();
|
|
350
|
+
return seed ? `${seed}.${ticketCounter++}` : void 0;
|
|
351
|
+
}
|
|
352
|
+
function isGatewayStatus(status) {
|
|
353
|
+
return status === 502 || status === 503 || status === 504;
|
|
354
|
+
}
|
|
355
|
+
function isNetworkError(error) {
|
|
356
|
+
return error instanceof TypeError;
|
|
357
|
+
}
|
|
358
|
+
async function fetchWithDeadline(url, init, deadline) {
|
|
359
|
+
const controller = new AbortController();
|
|
360
|
+
const remaining = Math.max(0, deadline - Date.now());
|
|
361
|
+
const timeoutId = setTimeout(() => controller.abort(), remaining);
|
|
362
|
+
try {
|
|
363
|
+
return await fetch(url, { ...init, signal: controller.signal });
|
|
364
|
+
} finally {
|
|
365
|
+
clearTimeout(timeoutId);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async function parseActionResponse(response, actionName) {
|
|
369
|
+
const data = await response.json();
|
|
370
|
+
if (data.success) return data.data;
|
|
371
|
+
if (data.error) throw new ActionError(data.error.code, data.error.message);
|
|
372
|
+
throw new ActionError("UNKNOWN_ERROR", `Action '${actionName}' failed`);
|
|
373
|
+
}
|
|
341
374
|
function setActionCallerOptions(options) {
|
|
342
375
|
globalOptions = { ...globalOptions, ...options };
|
|
343
376
|
}
|
|
@@ -411,36 +444,30 @@ function createActionCaller(actionName, method = "POST", options) {
|
|
|
411
444
|
const baseUrl = globalOptions.baseUrl ?? "";
|
|
412
445
|
const useFormData = options?.acceptsFiles && hasFiles(input);
|
|
413
446
|
const url = useFormData ? `${baseUrl}${ACTION_ENDPOINT_BASE}/${actionName}` : buildActionUrl(baseUrl, actionName, method, input);
|
|
447
|
+
const ticket = deriveTicket();
|
|
414
448
|
const fetchOptions = {
|
|
415
449
|
method,
|
|
416
450
|
headers: {
|
|
417
451
|
...useFormData ? {} : { "Content-Type": "application/json" },
|
|
418
|
-
...globalOptions.headers
|
|
452
|
+
...globalOptions.headers,
|
|
453
|
+
...ticket ? { [TICKET_HEADER]: ticket } : {}
|
|
419
454
|
}
|
|
420
455
|
};
|
|
421
456
|
if (method !== "GET") {
|
|
422
457
|
fetchOptions.body = useFormData ? buildFormData(input) : JSON.stringify(input);
|
|
423
458
|
}
|
|
424
459
|
const timeout = globalOptions.timeout ?? 3e4;
|
|
425
|
-
const
|
|
426
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
427
|
-
fetchOptions.signal = controller.signal;
|
|
460
|
+
const deadline = Date.now() + timeout;
|
|
428
461
|
try {
|
|
429
|
-
const response = await
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if (data.success) {
|
|
433
|
-
return data.data;
|
|
434
|
-
} else {
|
|
435
|
-
if (data.error) {
|
|
436
|
-
throw new ActionError(data.error.code, data.error.message);
|
|
437
|
-
}
|
|
438
|
-
throw new ActionError("UNKNOWN_ERROR", "Unknown error occurred");
|
|
462
|
+
const response = await fetchWithDeadline(url, fetchOptions, deadline);
|
|
463
|
+
if (ticket && (response.status === 202 || isGatewayStatus(response.status))) {
|
|
464
|
+
return pollForResult(baseUrl, ticket, actionName, deadline);
|
|
439
465
|
}
|
|
466
|
+
return await parseActionResponse(response, actionName);
|
|
440
467
|
} catch (error) {
|
|
441
|
-
|
|
442
|
-
if (
|
|
443
|
-
|
|
468
|
+
if (error instanceof ActionError) throw error;
|
|
469
|
+
if (ticket && isNetworkError(error)) {
|
|
470
|
+
return pollForResult(baseUrl, ticket, actionName, deadline);
|
|
444
471
|
}
|
|
445
472
|
if (error instanceof Error && error.name === "AbortError") {
|
|
446
473
|
throw new ActionError(
|
|
@@ -458,6 +485,53 @@ function createActionCaller(actionName, method = "POST", options) {
|
|
|
458
485
|
}
|
|
459
486
|
};
|
|
460
487
|
}
|
|
488
|
+
async function pollForResult(baseUrl, ticket, actionName, deadline) {
|
|
489
|
+
const url = `${baseUrl}${ACTION_RESULT_BASE}/${ticket}`;
|
|
490
|
+
let noProgressRetries = 0;
|
|
491
|
+
for (; ; ) {
|
|
492
|
+
if (Date.now() >= deadline) {
|
|
493
|
+
throw new ActionError(
|
|
494
|
+
"TIMEOUT",
|
|
495
|
+
`Action '${actionName}' timed out while awaiting result`
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
let response;
|
|
499
|
+
try {
|
|
500
|
+
response = await fetchWithDeadline(
|
|
501
|
+
url,
|
|
502
|
+
{ method: "GET", headers: { ...globalOptions.headers } },
|
|
503
|
+
deadline
|
|
504
|
+
);
|
|
505
|
+
} catch (error) {
|
|
506
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
507
|
+
throw new ActionError(
|
|
508
|
+
"TIMEOUT",
|
|
509
|
+
`Action '${actionName}' timed out while awaiting result`
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
if (isNetworkError(error) && noProgressRetries++ < 2) continue;
|
|
513
|
+
throw new ActionError("NETWORK_ERROR", `Network error awaiting '${actionName}'`);
|
|
514
|
+
}
|
|
515
|
+
if (response.status === 202) {
|
|
516
|
+
noProgressRetries = 0;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (response.status === 404) {
|
|
520
|
+
throw new ActionError(
|
|
521
|
+
"RESULT_NOT_FOUND",
|
|
522
|
+
`Result for '${actionName}' is no longer available (server restarted or result expired)`
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
if (isGatewayStatus(response.status)) {
|
|
526
|
+
if (noProgressRetries++ < 2) continue;
|
|
527
|
+
throw new ActionError(
|
|
528
|
+
"NETWORK_ERROR",
|
|
529
|
+
`Repeated gateway errors awaiting '${actionName}'`
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
return parseActionResponse(response, actionName);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
461
535
|
function buildActionUrl(baseUrl, actionName, method, input) {
|
|
462
536
|
const path = `${ACTION_ENDPOINT_BASE}/${actionName}`;
|
|
463
537
|
const fullUrl = `${baseUrl}${path}`;
|
|
@@ -478,104 +552,107 @@ function buildActionUrl(baseUrl, actionName, method, input) {
|
|
|
478
552
|
return fullUrl;
|
|
479
553
|
}
|
|
480
554
|
function createStreamCaller(actionName, options) {
|
|
481
|
-
return (input) =>
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
error = err instanceof ActionError ? err : new ActionError(
|
|
511
|
-
"NETWORK_ERROR",
|
|
512
|
-
`Network error streaming '${actionName}': ${err.message}`
|
|
513
|
-
);
|
|
514
|
-
if (resolveNext) resolveNext();
|
|
515
|
-
});
|
|
555
|
+
return (input) => streamChunks(actionName, input, options);
|
|
556
|
+
}
|
|
557
|
+
async function* streamChunks(actionName, input, options) {
|
|
558
|
+
const baseUrl = globalOptions.baseUrl ?? "";
|
|
559
|
+
const useFormData = options?.acceptsFiles && hasFiles(input);
|
|
560
|
+
const ticket = deriveTicket();
|
|
561
|
+
const controller = new AbortController();
|
|
562
|
+
let received = 0;
|
|
563
|
+
let noProgressRetries = 0;
|
|
564
|
+
try {
|
|
565
|
+
let response = await openStreamSegment(
|
|
566
|
+
`${baseUrl}${ACTION_ENDPOINT_BASE}/${actionName}`,
|
|
567
|
+
{
|
|
568
|
+
method: "POST",
|
|
569
|
+
headers: {
|
|
570
|
+
...useFormData ? {} : { "Content-Type": "application/json" },
|
|
571
|
+
...globalOptions.headers,
|
|
572
|
+
...ticket ? { [TICKET_HEADER]: ticket } : {}
|
|
573
|
+
},
|
|
574
|
+
body: useFormData ? buildFormData(input) : JSON.stringify(input),
|
|
575
|
+
signal: controller.signal
|
|
576
|
+
},
|
|
577
|
+
actionName
|
|
578
|
+
);
|
|
579
|
+
for (; ; ) {
|
|
580
|
+
let end = "cut";
|
|
581
|
+
const before = received;
|
|
582
|
+
try {
|
|
583
|
+
const reader = response.body.getReader();
|
|
516
584
|
const decoder = new TextDecoder();
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
return;
|
|
530
|
-
}
|
|
531
|
-
if (parsed.done) {
|
|
532
|
-
done = true;
|
|
533
|
-
if (resolveNext) resolveNext();
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
if ("chunk" in parsed) {
|
|
537
|
-
chunks.push(parsed.chunk);
|
|
538
|
-
if (resolveNext) resolveNext();
|
|
539
|
-
}
|
|
540
|
-
}
|
|
585
|
+
let buffer = "";
|
|
586
|
+
let finished = false;
|
|
587
|
+
while (!finished) {
|
|
588
|
+
const { done: readerDone, value } = await reader.read();
|
|
589
|
+
if (value) buffer += decoder.decode(value, { stream: true });
|
|
590
|
+
const lines = buffer.split("\n");
|
|
591
|
+
buffer = readerDone ? "" : lines.pop();
|
|
592
|
+
for (const line of lines) {
|
|
593
|
+
if (!line.trim()) continue;
|
|
594
|
+
const parsed = JSON.parse(line);
|
|
595
|
+
if (parsed.error !== void 0 && parsed.error !== null) {
|
|
596
|
+
throw new ActionError("STREAM_ERROR", String(parsed.error));
|
|
541
597
|
}
|
|
542
|
-
if (
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
598
|
+
if (parsed.done) {
|
|
599
|
+
end = "done";
|
|
600
|
+
finished = true;
|
|
601
|
+
break;
|
|
546
602
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
await fetchPromise;
|
|
556
|
-
while (true) {
|
|
557
|
-
if (chunks.length > 0) {
|
|
558
|
-
return { value: chunks.shift(), done: false };
|
|
559
|
-
}
|
|
560
|
-
if (error) {
|
|
561
|
-
throw error;
|
|
562
|
-
}
|
|
563
|
-
if (done) {
|
|
564
|
-
return { value: void 0, done: true };
|
|
565
|
-
}
|
|
566
|
-
await new Promise((resolve) => {
|
|
567
|
-
resolveNext = resolve;
|
|
568
|
-
});
|
|
569
|
-
resolveNext = null;
|
|
603
|
+
if (parsed.resume) {
|
|
604
|
+
end = "resume";
|
|
605
|
+
finished = true;
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
if ("chunk" in parsed) {
|
|
609
|
+
received++;
|
|
610
|
+
yield parsed.chunk;
|
|
570
611
|
}
|
|
571
|
-
},
|
|
572
|
-
[Symbol.asyncIterator]() {
|
|
573
|
-
return this;
|
|
574
612
|
}
|
|
575
|
-
|
|
613
|
+
if (readerDone) break;
|
|
614
|
+
}
|
|
615
|
+
} catch (err) {
|
|
616
|
+
if (err instanceof ActionError) throw err;
|
|
617
|
+
end = "cut";
|
|
576
618
|
}
|
|
577
|
-
|
|
578
|
-
|
|
619
|
+
if (end === "done") return;
|
|
620
|
+
if (!ticket) {
|
|
621
|
+
throw new ActionError("STREAM_ERROR", `Stream '${actionName}' ended unexpectedly`);
|
|
622
|
+
}
|
|
623
|
+
const progressed = received > before;
|
|
624
|
+
noProgressRetries = progressed ? 0 : noProgressRetries + 1;
|
|
625
|
+
if (!progressed && noProgressRetries > 2) {
|
|
626
|
+
throw new ActionError("STREAM_ERROR", `Stream '${actionName}' ended unexpectedly`);
|
|
627
|
+
}
|
|
628
|
+
response = await openStreamSegment(
|
|
629
|
+
`${baseUrl}${ACTION_RESULT_BASE}/${ticket}?from=${received}`,
|
|
630
|
+
{ method: "GET", headers: { ...globalOptions.headers }, signal: controller.signal },
|
|
631
|
+
actionName
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
} finally {
|
|
635
|
+
controller.abort();
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
async function openStreamSegment(url, init, actionName) {
|
|
639
|
+
let response;
|
|
640
|
+
try {
|
|
641
|
+
response = await fetch(url, init);
|
|
642
|
+
} catch (err) {
|
|
643
|
+
return emptyCutResponse();
|
|
644
|
+
}
|
|
645
|
+
if (response.status === 404) {
|
|
646
|
+
throw new ActionError(
|
|
647
|
+
"RESULT_NOT_FOUND",
|
|
648
|
+
`Result for '${actionName}' is no longer available (server restarted or result expired)`
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
if (!response.ok || !response.body) return emptyCutResponse();
|
|
652
|
+
return response;
|
|
653
|
+
}
|
|
654
|
+
function emptyCutResponse() {
|
|
655
|
+
return new Response(new Blob([]), { status: 200 });
|
|
579
656
|
}
|
|
580
657
|
function isSimpleObject(obj) {
|
|
581
658
|
if (typeof obj !== "object" || obj === null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jay-framework/stack-client-runtime",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"test:watch": "vitest"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@jay-framework/component": "^0.26.
|
|
31
|
-
"@jay-framework/fullstack-component": "^0.26.
|
|
32
|
-
"@jay-framework/runtime": "^0.26.
|
|
33
|
-
"@jay-framework/runtime-automation": "^0.26.
|
|
34
|
-
"@jay-framework/view-state-merge": "^0.26.
|
|
30
|
+
"@jay-framework/component": "^0.26.7",
|
|
31
|
+
"@jay-framework/fullstack-component": "^0.26.7",
|
|
32
|
+
"@jay-framework/runtime": "^0.26.7",
|
|
33
|
+
"@jay-framework/runtime-automation": "^0.26.7",
|
|
34
|
+
"@jay-framework/view-state-merge": "^0.26.7"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@jay-framework/dev-environment": "^0.26.
|
|
38
|
-
"@jay-framework/jay-cli": "^0.26.
|
|
37
|
+
"@jay-framework/dev-environment": "^0.26.7",
|
|
38
|
+
"@jay-framework/jay-cli": "^0.26.7",
|
|
39
39
|
"@types/express": "^5.0.2",
|
|
40
40
|
"@types/node": "^22.15.21",
|
|
41
41
|
"nodemon": "^3.0.3",
|