@supacloud/js 0.41.0 → 0.42.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/COMPATIBILITY.md +1 -1
- package/README.md +1 -1
- package/dist/index.cjs +299 -2
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +299 -2
- package/dist/task-events.cjs +0 -1
- package/dist/workflow-dispatcher.d.cts +94 -0
- package/dist/workflow-dispatcher.d.mts +94 -0
- package/package.json +10 -10
package/COMPATIBILITY.md
CHANGED
|
@@ -28,7 +28,7 @@ const { createCommandScope } = require('@supacloud/js/contracts');
|
|
|
28
28
|
|
|
29
29
|
The supported SDK Node floor remains **22.12.0**; native CJS output does not
|
|
30
30
|
lower dependency runtime requirements. CI also covers Node 24, Node 26 and
|
|
31
|
-
Bun 1.4.
|
|
31
|
+
Bun 1.4.3. These checks do not certify older Node, Electron, React Native/Hermes
|
|
32
32
|
or every browser/bundler combination.
|
|
33
33
|
|
|
34
34
|
Client creation remains synchronous. Network methods retain their Promise
|
package/README.md
CHANGED
|
@@ -679,7 +679,7 @@ if (claim?.status === "claimed") {
|
|
|
679
679
|
}
|
|
680
680
|
```
|
|
681
681
|
|
|
682
|
-
Available operations are `start`, `claim`, `advance`, `complete`, `retry`, `fail`, `cancel`, `get`, and `events`. No workflow RPC is automatically retried. Identity-bound mutations support explicit replay of the same request after a lost response; changed replay payloads can fail with a conflict. `claim` is not idempotent: repeating it after a lost response can acquire another message or attempt. An unconfirmed mutation, including a response rejected by the guard, may already have committed. Install the current workflow SQL before using the retry SDK, which requires the persisted `retryReceipt` response field. See the repository's `docs/durable-workflows.md` for execution, security, and DBOS design boundaries.
|
|
682
|
+
Available operations are `start`, `claim`, `advance`, `complete`, `retry`, `fail`, `cancel`, `get`, and `events`. No workflow RPC is automatically retried. Identity-bound mutations support explicit replay of the same request after a lost response; changed replay payloads can fail with a conflict. `claim` is not idempotent: repeating it after a lost response can acquire another message or attempt. An unconfirmed mutation, including a response rejected by the guard, may already have committed. Install the current workflow SQL before using the retry SDK, which requires the persisted `retryReceipt` response field. Trusted process hosts can use `createSupaCloudWorkflowRegistry` and `createSupaCloudWorkflowDispatcher` for exact version/step routing and explicit settlement; existing Job runtimes can use `createSupaCloudWorkflowTransport` instead. Unknown claims remain unsettled. See the repository's `docs/durable-workflows.md` for execution, security, and DBOS design boundaries.
|
|
683
683
|
|
|
684
684
|
Message IDs, queue message IDs, event cursors, and run row versions are decimal strings, preserving the full PostgreSQL `bigint` range. Retry a `claim` that returns SQLSTATE `40001`; the contended queue lease is rolled back before that error is returned.
|
|
685
685
|
|
package/dist/index.cjs
CHANGED
|
@@ -63,6 +63,7 @@ __export(exports_src, {
|
|
|
63
63
|
SupaCloudWorkflowClaimError: () => SupaCloudWorkflowClaimError,
|
|
64
64
|
SupaCloudWorkflowClaimInputError: () => SupaCloudWorkflowClaimInputError,
|
|
65
65
|
SupaCloudWorkflowCompleteError: () => SupaCloudWorkflowCompleteError,
|
|
66
|
+
SupaCloudWorkflowDispatchError: () => SupaCloudWorkflowDispatchError,
|
|
66
67
|
SupaCloudWorkflowEventsError: () => SupaCloudWorkflowEventsError,
|
|
67
68
|
SupaCloudWorkflowFailError: () => SupaCloudWorkflowFailError,
|
|
68
69
|
SupaCloudWorkflowReadError: () => SupaCloudWorkflowReadError,
|
|
@@ -79,7 +80,10 @@ __export(exports_src, {
|
|
|
79
80
|
createSupaCloudOAuthFetch: () => createSupaCloudOAuthFetch,
|
|
80
81
|
createSupaCloudProcedureClient: () => createSupaCloudProcedureClient,
|
|
81
82
|
createSupaCloudTaskFetch: () => createSupaCloudTaskFetch,
|
|
83
|
+
createSupaCloudWorkflowDispatcher: () => createSupaCloudWorkflowDispatcher,
|
|
82
84
|
createSupaCloudWorkflowFetch: () => createSupaCloudWorkflowFetch,
|
|
85
|
+
createSupaCloudWorkflowRegistry: () => createSupaCloudWorkflowRegistry,
|
|
86
|
+
createSupaCloudWorkflowTransport: () => createSupaCloudWorkflowTransport,
|
|
83
87
|
invalidateByTags: () => invalidateByTags,
|
|
84
88
|
isSupaCloudProcedureError: () => isSupaCloudProcedureError,
|
|
85
89
|
isSupaCloudProcedureErrorCode: () => isSupaCloudProcedureErrorCode,
|
|
@@ -2619,6 +2623,299 @@ function createSupaCloudOAuthFetch(options = {}) {
|
|
|
2619
2623
|
return fetchImpl(replacementRequest(request, endpoint, body));
|
|
2620
2624
|
};
|
|
2621
2625
|
}
|
|
2626
|
+
// src/workflow-dispatcher.ts
|
|
2627
|
+
var identifier = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,119}$/;
|
|
2628
|
+
var workflowError = /^[^\u0000-\u001f\u007f]{1,4000}$/;
|
|
2629
|
+
function definitionKey(workflowName, workflowVersion, stepKey) {
|
|
2630
|
+
return `${workflowName}\x00${workflowVersion}\x00${stepKey}`;
|
|
2631
|
+
}
|
|
2632
|
+
function validWorkflowVersion(value) {
|
|
2633
|
+
if (typeof value !== "string" || value.length < 1 || value.length > 80)
|
|
2634
|
+
return false;
|
|
2635
|
+
let count = 0;
|
|
2636
|
+
for (const character of value) {
|
|
2637
|
+
const code = character.codePointAt(0);
|
|
2638
|
+
if (++count > 80 || code === 0 || code !== undefined && code >= 55296 && code <= 57343) {
|
|
2639
|
+
return false;
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
return true;
|
|
2643
|
+
}
|
|
2644
|
+
function jsonObject(value) {
|
|
2645
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2646
|
+
}
|
|
2647
|
+
function record7(value) {
|
|
2648
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
2649
|
+
}
|
|
2650
|
+
function safeErrorMessage(value) {
|
|
2651
|
+
return typeof value === "string" && value.trim() === value && workflowError.test(value);
|
|
2652
|
+
}
|
|
2653
|
+
function errorMessage(error) {
|
|
2654
|
+
const message = error instanceof Error ? error.message : typeof error === "string" ? error : "WORKFLOW_STEP_FAILED";
|
|
2655
|
+
return message.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 4000) || "WORKFLOW_STEP_FAILED";
|
|
2656
|
+
}
|
|
2657
|
+
function validStepResult(value) {
|
|
2658
|
+
const result = record7(value);
|
|
2659
|
+
if (!result || typeof result.kind !== "string")
|
|
2660
|
+
return false;
|
|
2661
|
+
if (result.kind === "advance") {
|
|
2662
|
+
return typeof result.nextStepKey === "string" && identifier.test(result.nextStepKey) && (result.output === undefined || jsonObject(result.output)) && (result.nextInput === undefined || jsonObject(result.nextInput)) && (result.nextMaxAttempts === undefined || typeof result.nextMaxAttempts === "number" && Number.isSafeInteger(result.nextMaxAttempts) && result.nextMaxAttempts >= 1 && result.nextMaxAttempts <= 100);
|
|
2663
|
+
}
|
|
2664
|
+
if (result.kind === "complete") {
|
|
2665
|
+
return (result.stepOutput === undefined || jsonObject(result.stepOutput)) && (result.runOutput === undefined || jsonObject(result.runOutput));
|
|
2666
|
+
}
|
|
2667
|
+
if (result.kind === "retry") {
|
|
2668
|
+
return safeErrorMessage(result.errorMessage) && (result.delaySeconds === undefined || typeof result.delaySeconds === "number" && Number.isSafeInteger(result.delaySeconds) && result.delaySeconds >= 0 && result.delaySeconds <= 86400);
|
|
2669
|
+
}
|
|
2670
|
+
return result.kind === "fail" && safeErrorMessage(result.errorMessage);
|
|
2671
|
+
}
|
|
2672
|
+
function validateDefinition(value) {
|
|
2673
|
+
if (!identifier.test(value.workflowName) || !validWorkflowVersion(value.workflowVersion) || !identifier.test(value.stepKey) || !value.handler || typeof value.handler.decode !== "function" || typeof value.handler.authorize !== "function" || typeof value.handler.execute !== "function") {
|
|
2674
|
+
throw new TypeError("WORKFLOW_DEFINITION_INVALID");
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
function createSupaCloudWorkflowRegistry(definitions) {
|
|
2678
|
+
const entries = new Map;
|
|
2679
|
+
for (const definition of definitions) {
|
|
2680
|
+
validateDefinition(definition);
|
|
2681
|
+
const key = definitionKey(definition.workflowName, definition.workflowVersion, definition.stepKey);
|
|
2682
|
+
if (entries.has(key))
|
|
2683
|
+
throw new TypeError("WORKFLOW_DEFINITION_DUPLICATE");
|
|
2684
|
+
const decode = definition.handler.decode;
|
|
2685
|
+
const authorize = definition.handler.authorize;
|
|
2686
|
+
const execute = definition.handler.execute;
|
|
2687
|
+
const handler = Object.freeze({
|
|
2688
|
+
decode(input) {
|
|
2689
|
+
return decode.call(definition.handler, input);
|
|
2690
|
+
},
|
|
2691
|
+
authorize(input, context) {
|
|
2692
|
+
return authorize.call(definition.handler, input, context);
|
|
2693
|
+
},
|
|
2694
|
+
execute(input, context) {
|
|
2695
|
+
return execute.call(definition.handler, input, context);
|
|
2696
|
+
}
|
|
2697
|
+
});
|
|
2698
|
+
entries.set(key, Object.freeze({
|
|
2699
|
+
workflowName: definition.workflowName,
|
|
2700
|
+
workflowVersion: definition.workflowVersion,
|
|
2701
|
+
stepKey: definition.stepKey,
|
|
2702
|
+
handler
|
|
2703
|
+
}));
|
|
2704
|
+
}
|
|
2705
|
+
return Object.freeze({
|
|
2706
|
+
resolve(workflowName, workflowVersion, stepKey) {
|
|
2707
|
+
return entries.get(definitionKey(workflowName, workflowVersion, stepKey));
|
|
2708
|
+
}
|
|
2709
|
+
});
|
|
2710
|
+
}
|
|
2711
|
+
|
|
2712
|
+
class SupaCloudWorkflowDispatchError extends Error {
|
|
2713
|
+
code;
|
|
2714
|
+
claim;
|
|
2715
|
+
constructor(code, claim, options) {
|
|
2716
|
+
super(code);
|
|
2717
|
+
this.code = code;
|
|
2718
|
+
this.claim = claim;
|
|
2719
|
+
if (options?.cause !== undefined)
|
|
2720
|
+
this.cause = options.cause;
|
|
2721
|
+
this.name = "SupaCloudWorkflowDispatchError";
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
function attempt(claim) {
|
|
2725
|
+
return {
|
|
2726
|
+
stepId: claim.stepId,
|
|
2727
|
+
messageId: claim.messageId,
|
|
2728
|
+
attempt: claim.attempt,
|
|
2729
|
+
workerId: claim.workerId
|
|
2730
|
+
};
|
|
2731
|
+
}
|
|
2732
|
+
function createSupaCloudWorkflowTransport(options) {
|
|
2733
|
+
const visibilityTimeoutSeconds = options.visibilityTimeoutSeconds ?? 300;
|
|
2734
|
+
const retryDelaySeconds = options.retryDelaySeconds ?? 5;
|
|
2735
|
+
if (typeof options.workerId !== "string" || options.workerId.length < 1 || options.workerId.length > 200 || /[\u0000-\u001f\u007f]/.test(options.workerId)) {
|
|
2736
|
+
throw new TypeError("WORKFLOW_WORKER_ID_INVALID");
|
|
2737
|
+
}
|
|
2738
|
+
if (!Number.isSafeInteger(visibilityTimeoutSeconds) || visibilityTimeoutSeconds < 15 || visibilityTimeoutSeconds > 3600) {
|
|
2739
|
+
throw new TypeError("WORKFLOW_VISIBILITY_TIMEOUT_INVALID");
|
|
2740
|
+
}
|
|
2741
|
+
if (!Number.isSafeInteger(retryDelaySeconds) || retryDelaySeconds < 0 || retryDelaySeconds > 86400) {
|
|
2742
|
+
throw new TypeError("WORKFLOW_RETRY_DELAY_INVALID");
|
|
2743
|
+
}
|
|
2744
|
+
return {
|
|
2745
|
+
async claim(signal) {
|
|
2746
|
+
signal.throwIfAborted();
|
|
2747
|
+
const result = await options.workflows.claim({
|
|
2748
|
+
workerId: options.workerId,
|
|
2749
|
+
visibilityTimeoutSeconds
|
|
2750
|
+
});
|
|
2751
|
+
signal.throwIfAborted();
|
|
2752
|
+
if (!result || result.status !== "claimed")
|
|
2753
|
+
return null;
|
|
2754
|
+
await options.acceptClaim?.(result);
|
|
2755
|
+
signal.throwIfAborted();
|
|
2756
|
+
return result;
|
|
2757
|
+
},
|
|
2758
|
+
async ack(claim, output) {
|
|
2759
|
+
if (!jsonObject(output))
|
|
2760
|
+
throw new TypeError("WORKFLOW_OUTPUT_INVALID");
|
|
2761
|
+
return options.workflows.complete({
|
|
2762
|
+
...attempt(claim),
|
|
2763
|
+
runOutput: output
|
|
2764
|
+
});
|
|
2765
|
+
},
|
|
2766
|
+
async fail(claim, error) {
|
|
2767
|
+
const request = { ...attempt(claim), errorMessage: errorMessage(error) };
|
|
2768
|
+
return claim.attempt < claim.maxAttempts ? options.workflows.retry({ ...request, delaySeconds: retryDelaySeconds }) : options.workflows.fail(request);
|
|
2769
|
+
}
|
|
2770
|
+
};
|
|
2771
|
+
}
|
|
2772
|
+
function claimResult(claim) {
|
|
2773
|
+
if (claim.status === "dead_lettered")
|
|
2774
|
+
return { status: "dead_lettered", claim };
|
|
2775
|
+
return { status: "discarded", claim };
|
|
2776
|
+
}
|
|
2777
|
+
function abortableDelay(signal, milliseconds) {
|
|
2778
|
+
if (milliseconds <= 0)
|
|
2779
|
+
return Promise.resolve();
|
|
2780
|
+
return new Promise((resolve, reject) => {
|
|
2781
|
+
const timer = setTimeout(() => {
|
|
2782
|
+
signal.removeEventListener("abort", abort);
|
|
2783
|
+
resolve();
|
|
2784
|
+
}, milliseconds);
|
|
2785
|
+
const abort = () => {
|
|
2786
|
+
clearTimeout(timer);
|
|
2787
|
+
signal.removeEventListener("abort", abort);
|
|
2788
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
2789
|
+
};
|
|
2790
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
2791
|
+
if (signal.aborted)
|
|
2792
|
+
abort();
|
|
2793
|
+
});
|
|
2794
|
+
}
|
|
2795
|
+
function createSupaCloudWorkflowDispatcher(options) {
|
|
2796
|
+
const {
|
|
2797
|
+
workflows,
|
|
2798
|
+
registry,
|
|
2799
|
+
workerId,
|
|
2800
|
+
visibilityTimeoutSeconds = 300,
|
|
2801
|
+
pollIntervalMs = 250
|
|
2802
|
+
} = options;
|
|
2803
|
+
if (typeof workerId !== "string" || workerId.length < 1 || workerId.length > 200 || /[\u0000-\u001f\u007f]/.test(workerId)) {
|
|
2804
|
+
throw new TypeError("WORKFLOW_WORKER_ID_INVALID");
|
|
2805
|
+
}
|
|
2806
|
+
if (!Number.isSafeInteger(visibilityTimeoutSeconds) || visibilityTimeoutSeconds < 15 || visibilityTimeoutSeconds > 3600) {
|
|
2807
|
+
throw new TypeError("WORKFLOW_VISIBILITY_TIMEOUT_INVALID");
|
|
2808
|
+
}
|
|
2809
|
+
if (!Number.isSafeInteger(pollIntervalMs) || pollIntervalMs < 0 || pollIntervalMs > 60000) {
|
|
2810
|
+
throw new TypeError("WORKFLOW_POLL_INTERVAL_INVALID");
|
|
2811
|
+
}
|
|
2812
|
+
const dispatchOnce = async (signal = new AbortController().signal) => {
|
|
2813
|
+
signal.throwIfAborted();
|
|
2814
|
+
const claimed = await workflows.claim({ workerId, visibilityTimeoutSeconds });
|
|
2815
|
+
if (claimed === null)
|
|
2816
|
+
return { status: "idle" };
|
|
2817
|
+
if (claimed.status !== "claimed")
|
|
2818
|
+
return claimResult(claimed);
|
|
2819
|
+
if (claimed.workerId !== workerId) {
|
|
2820
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_WORKER_MISMATCH", claimed);
|
|
2821
|
+
}
|
|
2822
|
+
const definition = registry.resolve(claimed.workflowName, claimed.workflowVersion, claimed.stepKey);
|
|
2823
|
+
if (!definition) {
|
|
2824
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_NOT_FOUND", claimed);
|
|
2825
|
+
}
|
|
2826
|
+
const context = Object.freeze({
|
|
2827
|
+
runId: claimed.runId,
|
|
2828
|
+
workflowName: claimed.workflowName,
|
|
2829
|
+
workflowVersion: claimed.workflowVersion,
|
|
2830
|
+
stepId: claimed.stepId,
|
|
2831
|
+
stepKey: claimed.stepKey,
|
|
2832
|
+
messageId: claimed.messageId,
|
|
2833
|
+
attempt: claimed.attempt,
|
|
2834
|
+
maxAttempts: claimed.maxAttempts,
|
|
2835
|
+
workerId,
|
|
2836
|
+
signal
|
|
2837
|
+
});
|
|
2838
|
+
let input;
|
|
2839
|
+
try {
|
|
2840
|
+
input = definition.handler.decode(claimed.input);
|
|
2841
|
+
} catch (error) {
|
|
2842
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_DECODE_FAILED", claimed, { cause: error });
|
|
2843
|
+
}
|
|
2844
|
+
signal.throwIfAborted();
|
|
2845
|
+
let authorized;
|
|
2846
|
+
try {
|
|
2847
|
+
authorized = await definition.handler.authorize(input, context);
|
|
2848
|
+
} catch (error) {
|
|
2849
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_AUTHORIZATION_FAILED", claimed, { cause: error });
|
|
2850
|
+
}
|
|
2851
|
+
if (authorized !== true) {
|
|
2852
|
+
signal.throwIfAborted();
|
|
2853
|
+
const request = {
|
|
2854
|
+
...attempt(claimed),
|
|
2855
|
+
errorMessage: "WORKFLOW_STEP_FORBIDDEN"
|
|
2856
|
+
};
|
|
2857
|
+
return { status: "failed", run: await workflows.fail(request) };
|
|
2858
|
+
}
|
|
2859
|
+
signal.throwIfAborted();
|
|
2860
|
+
let result;
|
|
2861
|
+
try {
|
|
2862
|
+
result = await definition.handler.execute(input, context);
|
|
2863
|
+
} catch (error) {
|
|
2864
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_EXECUTION_FAILED", claimed, { cause: error });
|
|
2865
|
+
}
|
|
2866
|
+
signal.throwIfAborted();
|
|
2867
|
+
if (!validStepResult(result)) {
|
|
2868
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_RESULT_INVALID", claimed);
|
|
2869
|
+
}
|
|
2870
|
+
if (result.kind === "advance") {
|
|
2871
|
+
const request = {
|
|
2872
|
+
...attempt(claimed),
|
|
2873
|
+
output: result.output,
|
|
2874
|
+
nextStepKey: result.nextStepKey,
|
|
2875
|
+
nextInput: result.nextInput,
|
|
2876
|
+
nextMaxAttempts: result.nextMaxAttempts
|
|
2877
|
+
};
|
|
2878
|
+
return { status: "advanced", run: await workflows.advance(request) };
|
|
2879
|
+
}
|
|
2880
|
+
if (result.kind === "complete") {
|
|
2881
|
+
const request = {
|
|
2882
|
+
...attempt(claimed),
|
|
2883
|
+
stepOutput: result.stepOutput,
|
|
2884
|
+
runOutput: result.runOutput
|
|
2885
|
+
};
|
|
2886
|
+
return { status: "completed", run: await workflows.complete(request) };
|
|
2887
|
+
}
|
|
2888
|
+
if (result.kind === "retry") {
|
|
2889
|
+
const request = {
|
|
2890
|
+
...attempt(claimed),
|
|
2891
|
+
errorMessage: result.errorMessage,
|
|
2892
|
+
delaySeconds: result.delaySeconds
|
|
2893
|
+
};
|
|
2894
|
+
return { status: "retried", run: await workflows.retry(request) };
|
|
2895
|
+
}
|
|
2896
|
+
const request = {
|
|
2897
|
+
...attempt(claimed),
|
|
2898
|
+
errorMessage: result.errorMessage
|
|
2899
|
+
};
|
|
2900
|
+
return { status: "failed", run: await workflows.fail(request) };
|
|
2901
|
+
};
|
|
2902
|
+
return {
|
|
2903
|
+
dispatchOnce,
|
|
2904
|
+
async run(signal = new AbortController().signal) {
|
|
2905
|
+
try {
|
|
2906
|
+
while (!signal.aborted) {
|
|
2907
|
+
const result = await dispatchOnce(signal);
|
|
2908
|
+
if (result.status === "idle")
|
|
2909
|
+
await abortableDelay(signal, pollIntervalMs);
|
|
2910
|
+
}
|
|
2911
|
+
} catch (error) {
|
|
2912
|
+
if (signal.aborted && error === signal.reason)
|
|
2913
|
+
return;
|
|
2914
|
+
throw error;
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
};
|
|
2918
|
+
}
|
|
2622
2919
|
// src/workflow-fetch.ts
|
|
2623
2920
|
var workflowPath = /\/rpc\/supacloud_workflow_(?:start|claim|advance|complete|retry|fail|cancel|get|events)$/;
|
|
2624
2921
|
function createSupaCloudWorkflowFetch(options = {}) {
|
|
@@ -2642,7 +2939,7 @@ function getSupaCloudApiFetchError(response) {
|
|
|
2642
2939
|
function isRecord4(value) {
|
|
2643
2940
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2644
2941
|
}
|
|
2645
|
-
function
|
|
2942
|
+
function errorMessage2(error) {
|
|
2646
2943
|
return isRecord4(error) && typeof error.message === "string" ? error.message : "Supabase Function invocation failed";
|
|
2647
2944
|
}
|
|
2648
2945
|
async function readRequestBody(request) {
|
|
@@ -2733,7 +3030,7 @@ function createSupaCloudApiFetch(options) {
|
|
|
2733
3030
|
}
|
|
2734
3031
|
if (invocation.error instanceof Error)
|
|
2735
3032
|
throw invocation.error;
|
|
2736
|
-
throw new Error(
|
|
3033
|
+
throw new Error(errorMessage2(invocation.error));
|
|
2737
3034
|
}
|
|
2738
3035
|
return responseFromData(invocation.data, invocation.response instanceof Response ? invocation.response : undefined);
|
|
2739
3036
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -15,6 +15,7 @@ export * from "./oauth-clients.cjs";
|
|
|
15
15
|
export * from "./oauth-server.cjs";
|
|
16
16
|
export { createSupaCloudOAuthFetch, type SupaCloudOAuthFetchOptions, } from "./auth-fetch.cjs";
|
|
17
17
|
export * from "./workflows.cjs";
|
|
18
|
+
export * from "./workflow-dispatcher.cjs";
|
|
18
19
|
export { createSupaCloudWorkflowFetch, type SupaCloudWorkflowFetchOptions } from "./workflow-fetch.cjs";
|
|
19
20
|
export { createSupaCloudCommandFetch, type SupaCloudCommandFetchOptions } from "./command-fetch.cjs";
|
|
20
21
|
export { createSupaCloudArtifactFetch, type SupaCloudArtifactFetchOptions } from "./artifact-fetch.cjs";
|
package/dist/index.d.mts
CHANGED
|
@@ -15,6 +15,7 @@ export * from "./oauth-clients.mjs";
|
|
|
15
15
|
export * from "./oauth-server.mjs";
|
|
16
16
|
export { createSupaCloudOAuthFetch, type SupaCloudOAuthFetchOptions, } from "./auth-fetch.mjs";
|
|
17
17
|
export * from "./workflows.mjs";
|
|
18
|
+
export * from "./workflow-dispatcher.mjs";
|
|
18
19
|
export { createSupaCloudWorkflowFetch, type SupaCloudWorkflowFetchOptions } from "./workflow-fetch.mjs";
|
|
19
20
|
export { createSupaCloudCommandFetch, type SupaCloudCommandFetchOptions } from "./command-fetch.mjs";
|
|
20
21
|
export { createSupaCloudArtifactFetch, type SupaCloudArtifactFetchOptions } from "./artifact-fetch.mjs";
|
package/dist/index.mjs
CHANGED
|
@@ -2531,6 +2531,299 @@ function createSupaCloudOAuthFetch(options = {}) {
|
|
|
2531
2531
|
return fetchImpl(replacementRequest(request, endpoint, body));
|
|
2532
2532
|
};
|
|
2533
2533
|
}
|
|
2534
|
+
// src/workflow-dispatcher.ts
|
|
2535
|
+
var identifier = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,119}$/;
|
|
2536
|
+
var workflowError = /^[^\u0000-\u001f\u007f]{1,4000}$/;
|
|
2537
|
+
function definitionKey(workflowName, workflowVersion, stepKey) {
|
|
2538
|
+
return `${workflowName}\x00${workflowVersion}\x00${stepKey}`;
|
|
2539
|
+
}
|
|
2540
|
+
function validWorkflowVersion(value) {
|
|
2541
|
+
if (typeof value !== "string" || value.length < 1 || value.length > 80)
|
|
2542
|
+
return false;
|
|
2543
|
+
let count = 0;
|
|
2544
|
+
for (const character of value) {
|
|
2545
|
+
const code = character.codePointAt(0);
|
|
2546
|
+
if (++count > 80 || code === 0 || code !== undefined && code >= 55296 && code <= 57343) {
|
|
2547
|
+
return false;
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
return true;
|
|
2551
|
+
}
|
|
2552
|
+
function jsonObject(value) {
|
|
2553
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2554
|
+
}
|
|
2555
|
+
function record7(value) {
|
|
2556
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
2557
|
+
}
|
|
2558
|
+
function safeErrorMessage(value) {
|
|
2559
|
+
return typeof value === "string" && value.trim() === value && workflowError.test(value);
|
|
2560
|
+
}
|
|
2561
|
+
function errorMessage(error) {
|
|
2562
|
+
const message = error instanceof Error ? error.message : typeof error === "string" ? error : "WORKFLOW_STEP_FAILED";
|
|
2563
|
+
return message.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 4000) || "WORKFLOW_STEP_FAILED";
|
|
2564
|
+
}
|
|
2565
|
+
function validStepResult(value) {
|
|
2566
|
+
const result = record7(value);
|
|
2567
|
+
if (!result || typeof result.kind !== "string")
|
|
2568
|
+
return false;
|
|
2569
|
+
if (result.kind === "advance") {
|
|
2570
|
+
return typeof result.nextStepKey === "string" && identifier.test(result.nextStepKey) && (result.output === undefined || jsonObject(result.output)) && (result.nextInput === undefined || jsonObject(result.nextInput)) && (result.nextMaxAttempts === undefined || typeof result.nextMaxAttempts === "number" && Number.isSafeInteger(result.nextMaxAttempts) && result.nextMaxAttempts >= 1 && result.nextMaxAttempts <= 100);
|
|
2571
|
+
}
|
|
2572
|
+
if (result.kind === "complete") {
|
|
2573
|
+
return (result.stepOutput === undefined || jsonObject(result.stepOutput)) && (result.runOutput === undefined || jsonObject(result.runOutput));
|
|
2574
|
+
}
|
|
2575
|
+
if (result.kind === "retry") {
|
|
2576
|
+
return safeErrorMessage(result.errorMessage) && (result.delaySeconds === undefined || typeof result.delaySeconds === "number" && Number.isSafeInteger(result.delaySeconds) && result.delaySeconds >= 0 && result.delaySeconds <= 86400);
|
|
2577
|
+
}
|
|
2578
|
+
return result.kind === "fail" && safeErrorMessage(result.errorMessage);
|
|
2579
|
+
}
|
|
2580
|
+
function validateDefinition(value) {
|
|
2581
|
+
if (!identifier.test(value.workflowName) || !validWorkflowVersion(value.workflowVersion) || !identifier.test(value.stepKey) || !value.handler || typeof value.handler.decode !== "function" || typeof value.handler.authorize !== "function" || typeof value.handler.execute !== "function") {
|
|
2582
|
+
throw new TypeError("WORKFLOW_DEFINITION_INVALID");
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
function createSupaCloudWorkflowRegistry(definitions) {
|
|
2586
|
+
const entries = new Map;
|
|
2587
|
+
for (const definition of definitions) {
|
|
2588
|
+
validateDefinition(definition);
|
|
2589
|
+
const key = definitionKey(definition.workflowName, definition.workflowVersion, definition.stepKey);
|
|
2590
|
+
if (entries.has(key))
|
|
2591
|
+
throw new TypeError("WORKFLOW_DEFINITION_DUPLICATE");
|
|
2592
|
+
const decode = definition.handler.decode;
|
|
2593
|
+
const authorize = definition.handler.authorize;
|
|
2594
|
+
const execute = definition.handler.execute;
|
|
2595
|
+
const handler = Object.freeze({
|
|
2596
|
+
decode(input) {
|
|
2597
|
+
return decode.call(definition.handler, input);
|
|
2598
|
+
},
|
|
2599
|
+
authorize(input, context) {
|
|
2600
|
+
return authorize.call(definition.handler, input, context);
|
|
2601
|
+
},
|
|
2602
|
+
execute(input, context) {
|
|
2603
|
+
return execute.call(definition.handler, input, context);
|
|
2604
|
+
}
|
|
2605
|
+
});
|
|
2606
|
+
entries.set(key, Object.freeze({
|
|
2607
|
+
workflowName: definition.workflowName,
|
|
2608
|
+
workflowVersion: definition.workflowVersion,
|
|
2609
|
+
stepKey: definition.stepKey,
|
|
2610
|
+
handler
|
|
2611
|
+
}));
|
|
2612
|
+
}
|
|
2613
|
+
return Object.freeze({
|
|
2614
|
+
resolve(workflowName, workflowVersion, stepKey) {
|
|
2615
|
+
return entries.get(definitionKey(workflowName, workflowVersion, stepKey));
|
|
2616
|
+
}
|
|
2617
|
+
});
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
class SupaCloudWorkflowDispatchError extends Error {
|
|
2621
|
+
code;
|
|
2622
|
+
claim;
|
|
2623
|
+
constructor(code, claim, options) {
|
|
2624
|
+
super(code);
|
|
2625
|
+
this.code = code;
|
|
2626
|
+
this.claim = claim;
|
|
2627
|
+
if (options?.cause !== undefined)
|
|
2628
|
+
this.cause = options.cause;
|
|
2629
|
+
this.name = "SupaCloudWorkflowDispatchError";
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
function attempt(claim) {
|
|
2633
|
+
return {
|
|
2634
|
+
stepId: claim.stepId,
|
|
2635
|
+
messageId: claim.messageId,
|
|
2636
|
+
attempt: claim.attempt,
|
|
2637
|
+
workerId: claim.workerId
|
|
2638
|
+
};
|
|
2639
|
+
}
|
|
2640
|
+
function createSupaCloudWorkflowTransport(options) {
|
|
2641
|
+
const visibilityTimeoutSeconds = options.visibilityTimeoutSeconds ?? 300;
|
|
2642
|
+
const retryDelaySeconds = options.retryDelaySeconds ?? 5;
|
|
2643
|
+
if (typeof options.workerId !== "string" || options.workerId.length < 1 || options.workerId.length > 200 || /[\u0000-\u001f\u007f]/.test(options.workerId)) {
|
|
2644
|
+
throw new TypeError("WORKFLOW_WORKER_ID_INVALID");
|
|
2645
|
+
}
|
|
2646
|
+
if (!Number.isSafeInteger(visibilityTimeoutSeconds) || visibilityTimeoutSeconds < 15 || visibilityTimeoutSeconds > 3600) {
|
|
2647
|
+
throw new TypeError("WORKFLOW_VISIBILITY_TIMEOUT_INVALID");
|
|
2648
|
+
}
|
|
2649
|
+
if (!Number.isSafeInteger(retryDelaySeconds) || retryDelaySeconds < 0 || retryDelaySeconds > 86400) {
|
|
2650
|
+
throw new TypeError("WORKFLOW_RETRY_DELAY_INVALID");
|
|
2651
|
+
}
|
|
2652
|
+
return {
|
|
2653
|
+
async claim(signal) {
|
|
2654
|
+
signal.throwIfAborted();
|
|
2655
|
+
const result = await options.workflows.claim({
|
|
2656
|
+
workerId: options.workerId,
|
|
2657
|
+
visibilityTimeoutSeconds
|
|
2658
|
+
});
|
|
2659
|
+
signal.throwIfAborted();
|
|
2660
|
+
if (!result || result.status !== "claimed")
|
|
2661
|
+
return null;
|
|
2662
|
+
await options.acceptClaim?.(result);
|
|
2663
|
+
signal.throwIfAborted();
|
|
2664
|
+
return result;
|
|
2665
|
+
},
|
|
2666
|
+
async ack(claim, output) {
|
|
2667
|
+
if (!jsonObject(output))
|
|
2668
|
+
throw new TypeError("WORKFLOW_OUTPUT_INVALID");
|
|
2669
|
+
return options.workflows.complete({
|
|
2670
|
+
...attempt(claim),
|
|
2671
|
+
runOutput: output
|
|
2672
|
+
});
|
|
2673
|
+
},
|
|
2674
|
+
async fail(claim, error) {
|
|
2675
|
+
const request = { ...attempt(claim), errorMessage: errorMessage(error) };
|
|
2676
|
+
return claim.attempt < claim.maxAttempts ? options.workflows.retry({ ...request, delaySeconds: retryDelaySeconds }) : options.workflows.fail(request);
|
|
2677
|
+
}
|
|
2678
|
+
};
|
|
2679
|
+
}
|
|
2680
|
+
function claimResult(claim) {
|
|
2681
|
+
if (claim.status === "dead_lettered")
|
|
2682
|
+
return { status: "dead_lettered", claim };
|
|
2683
|
+
return { status: "discarded", claim };
|
|
2684
|
+
}
|
|
2685
|
+
function abortableDelay(signal, milliseconds) {
|
|
2686
|
+
if (milliseconds <= 0)
|
|
2687
|
+
return Promise.resolve();
|
|
2688
|
+
return new Promise((resolve, reject) => {
|
|
2689
|
+
const timer = setTimeout(() => {
|
|
2690
|
+
signal.removeEventListener("abort", abort);
|
|
2691
|
+
resolve();
|
|
2692
|
+
}, milliseconds);
|
|
2693
|
+
const abort = () => {
|
|
2694
|
+
clearTimeout(timer);
|
|
2695
|
+
signal.removeEventListener("abort", abort);
|
|
2696
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
2697
|
+
};
|
|
2698
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
2699
|
+
if (signal.aborted)
|
|
2700
|
+
abort();
|
|
2701
|
+
});
|
|
2702
|
+
}
|
|
2703
|
+
function createSupaCloudWorkflowDispatcher(options) {
|
|
2704
|
+
const {
|
|
2705
|
+
workflows,
|
|
2706
|
+
registry,
|
|
2707
|
+
workerId,
|
|
2708
|
+
visibilityTimeoutSeconds = 300,
|
|
2709
|
+
pollIntervalMs = 250
|
|
2710
|
+
} = options;
|
|
2711
|
+
if (typeof workerId !== "string" || workerId.length < 1 || workerId.length > 200 || /[\u0000-\u001f\u007f]/.test(workerId)) {
|
|
2712
|
+
throw new TypeError("WORKFLOW_WORKER_ID_INVALID");
|
|
2713
|
+
}
|
|
2714
|
+
if (!Number.isSafeInteger(visibilityTimeoutSeconds) || visibilityTimeoutSeconds < 15 || visibilityTimeoutSeconds > 3600) {
|
|
2715
|
+
throw new TypeError("WORKFLOW_VISIBILITY_TIMEOUT_INVALID");
|
|
2716
|
+
}
|
|
2717
|
+
if (!Number.isSafeInteger(pollIntervalMs) || pollIntervalMs < 0 || pollIntervalMs > 60000) {
|
|
2718
|
+
throw new TypeError("WORKFLOW_POLL_INTERVAL_INVALID");
|
|
2719
|
+
}
|
|
2720
|
+
const dispatchOnce = async (signal = new AbortController().signal) => {
|
|
2721
|
+
signal.throwIfAborted();
|
|
2722
|
+
const claimed = await workflows.claim({ workerId, visibilityTimeoutSeconds });
|
|
2723
|
+
if (claimed === null)
|
|
2724
|
+
return { status: "idle" };
|
|
2725
|
+
if (claimed.status !== "claimed")
|
|
2726
|
+
return claimResult(claimed);
|
|
2727
|
+
if (claimed.workerId !== workerId) {
|
|
2728
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_WORKER_MISMATCH", claimed);
|
|
2729
|
+
}
|
|
2730
|
+
const definition = registry.resolve(claimed.workflowName, claimed.workflowVersion, claimed.stepKey);
|
|
2731
|
+
if (!definition) {
|
|
2732
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_NOT_FOUND", claimed);
|
|
2733
|
+
}
|
|
2734
|
+
const context = Object.freeze({
|
|
2735
|
+
runId: claimed.runId,
|
|
2736
|
+
workflowName: claimed.workflowName,
|
|
2737
|
+
workflowVersion: claimed.workflowVersion,
|
|
2738
|
+
stepId: claimed.stepId,
|
|
2739
|
+
stepKey: claimed.stepKey,
|
|
2740
|
+
messageId: claimed.messageId,
|
|
2741
|
+
attempt: claimed.attempt,
|
|
2742
|
+
maxAttempts: claimed.maxAttempts,
|
|
2743
|
+
workerId,
|
|
2744
|
+
signal
|
|
2745
|
+
});
|
|
2746
|
+
let input;
|
|
2747
|
+
try {
|
|
2748
|
+
input = definition.handler.decode(claimed.input);
|
|
2749
|
+
} catch (error) {
|
|
2750
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_DECODE_FAILED", claimed, { cause: error });
|
|
2751
|
+
}
|
|
2752
|
+
signal.throwIfAborted();
|
|
2753
|
+
let authorized;
|
|
2754
|
+
try {
|
|
2755
|
+
authorized = await definition.handler.authorize(input, context);
|
|
2756
|
+
} catch (error) {
|
|
2757
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_AUTHORIZATION_FAILED", claimed, { cause: error });
|
|
2758
|
+
}
|
|
2759
|
+
if (authorized !== true) {
|
|
2760
|
+
signal.throwIfAborted();
|
|
2761
|
+
const request = {
|
|
2762
|
+
...attempt(claimed),
|
|
2763
|
+
errorMessage: "WORKFLOW_STEP_FORBIDDEN"
|
|
2764
|
+
};
|
|
2765
|
+
return { status: "failed", run: await workflows.fail(request) };
|
|
2766
|
+
}
|
|
2767
|
+
signal.throwIfAborted();
|
|
2768
|
+
let result;
|
|
2769
|
+
try {
|
|
2770
|
+
result = await definition.handler.execute(input, context);
|
|
2771
|
+
} catch (error) {
|
|
2772
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_EXECUTION_FAILED", claimed, { cause: error });
|
|
2773
|
+
}
|
|
2774
|
+
signal.throwIfAborted();
|
|
2775
|
+
if (!validStepResult(result)) {
|
|
2776
|
+
throw new SupaCloudWorkflowDispatchError("WORKFLOW_HANDLER_RESULT_INVALID", claimed);
|
|
2777
|
+
}
|
|
2778
|
+
if (result.kind === "advance") {
|
|
2779
|
+
const request = {
|
|
2780
|
+
...attempt(claimed),
|
|
2781
|
+
output: result.output,
|
|
2782
|
+
nextStepKey: result.nextStepKey,
|
|
2783
|
+
nextInput: result.nextInput,
|
|
2784
|
+
nextMaxAttempts: result.nextMaxAttempts
|
|
2785
|
+
};
|
|
2786
|
+
return { status: "advanced", run: await workflows.advance(request) };
|
|
2787
|
+
}
|
|
2788
|
+
if (result.kind === "complete") {
|
|
2789
|
+
const request = {
|
|
2790
|
+
...attempt(claimed),
|
|
2791
|
+
stepOutput: result.stepOutput,
|
|
2792
|
+
runOutput: result.runOutput
|
|
2793
|
+
};
|
|
2794
|
+
return { status: "completed", run: await workflows.complete(request) };
|
|
2795
|
+
}
|
|
2796
|
+
if (result.kind === "retry") {
|
|
2797
|
+
const request = {
|
|
2798
|
+
...attempt(claimed),
|
|
2799
|
+
errorMessage: result.errorMessage,
|
|
2800
|
+
delaySeconds: result.delaySeconds
|
|
2801
|
+
};
|
|
2802
|
+
return { status: "retried", run: await workflows.retry(request) };
|
|
2803
|
+
}
|
|
2804
|
+
const request = {
|
|
2805
|
+
...attempt(claimed),
|
|
2806
|
+
errorMessage: result.errorMessage
|
|
2807
|
+
};
|
|
2808
|
+
return { status: "failed", run: await workflows.fail(request) };
|
|
2809
|
+
};
|
|
2810
|
+
return {
|
|
2811
|
+
dispatchOnce,
|
|
2812
|
+
async run(signal = new AbortController().signal) {
|
|
2813
|
+
try {
|
|
2814
|
+
while (!signal.aborted) {
|
|
2815
|
+
const result = await dispatchOnce(signal);
|
|
2816
|
+
if (result.status === "idle")
|
|
2817
|
+
await abortableDelay(signal, pollIntervalMs);
|
|
2818
|
+
}
|
|
2819
|
+
} catch (error) {
|
|
2820
|
+
if (signal.aborted && error === signal.reason)
|
|
2821
|
+
return;
|
|
2822
|
+
throw error;
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
};
|
|
2826
|
+
}
|
|
2534
2827
|
// src/workflow-fetch.ts
|
|
2535
2828
|
var workflowPath = /\/rpc\/supacloud_workflow_(?:start|claim|advance|complete|retry|fail|cancel|get|events)$/;
|
|
2536
2829
|
function createSupaCloudWorkflowFetch(options = {}) {
|
|
@@ -2554,7 +2847,7 @@ function getSupaCloudApiFetchError(response) {
|
|
|
2554
2847
|
function isRecord4(value) {
|
|
2555
2848
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2556
2849
|
}
|
|
2557
|
-
function
|
|
2850
|
+
function errorMessage2(error) {
|
|
2558
2851
|
return isRecord4(error) && typeof error.message === "string" ? error.message : "Supabase Function invocation failed";
|
|
2559
2852
|
}
|
|
2560
2853
|
async function readRequestBody(request) {
|
|
@@ -2645,7 +2938,7 @@ function createSupaCloudApiFetch(options) {
|
|
|
2645
2938
|
}
|
|
2646
2939
|
if (invocation.error instanceof Error)
|
|
2647
2940
|
throw invocation.error;
|
|
2648
|
-
throw new Error(
|
|
2941
|
+
throw new Error(errorMessage2(invocation.error));
|
|
2649
2942
|
}
|
|
2650
2943
|
return responseFromData(invocation.data, invocation.response instanceof Response ? invocation.response : undefined);
|
|
2651
2944
|
};
|
|
@@ -4171,6 +4464,7 @@ export {
|
|
|
4171
4464
|
SupaCloudWorkflowClaimError,
|
|
4172
4465
|
SupaCloudWorkflowClaimInputError,
|
|
4173
4466
|
SupaCloudWorkflowCompleteError,
|
|
4467
|
+
SupaCloudWorkflowDispatchError,
|
|
4174
4468
|
SupaCloudWorkflowEventsError,
|
|
4175
4469
|
SupaCloudWorkflowFailError,
|
|
4176
4470
|
SupaCloudWorkflowReadError,
|
|
@@ -4187,7 +4481,10 @@ export {
|
|
|
4187
4481
|
createSupaCloudOAuthFetch,
|
|
4188
4482
|
createSupaCloudProcedureClient,
|
|
4189
4483
|
createSupaCloudTaskFetch,
|
|
4484
|
+
createSupaCloudWorkflowDispatcher,
|
|
4190
4485
|
createSupaCloudWorkflowFetch,
|
|
4486
|
+
createSupaCloudWorkflowRegistry,
|
|
4487
|
+
createSupaCloudWorkflowTransport,
|
|
4191
4488
|
invalidateByTags,
|
|
4192
4489
|
isSupaCloudProcedureError,
|
|
4193
4490
|
isSupaCloudProcedureErrorCode,
|
package/dist/task-events.cjs
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { SupaCloudWorkflowClaim, SupaCloudWorkflowClaimResult, SupaCloudWorkflowJson, SupaCloudWorkflowRun, SupaCloudWorkflowsClient } from "./workflows.cjs";
|
|
2
|
+
export type SupaCloudWorkflowStepResult = {
|
|
3
|
+
kind: "advance";
|
|
4
|
+
nextStepKey: string;
|
|
5
|
+
output?: SupaCloudWorkflowJson;
|
|
6
|
+
nextInput?: SupaCloudWorkflowJson;
|
|
7
|
+
nextMaxAttempts?: number;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "complete";
|
|
10
|
+
stepOutput?: SupaCloudWorkflowJson;
|
|
11
|
+
runOutput?: SupaCloudWorkflowJson;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "retry";
|
|
14
|
+
errorMessage: string;
|
|
15
|
+
delaySeconds?: number;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "fail";
|
|
18
|
+
errorMessage: string;
|
|
19
|
+
};
|
|
20
|
+
export interface SupaCloudWorkflowStepContext {
|
|
21
|
+
readonly runId: string;
|
|
22
|
+
readonly workflowName: string;
|
|
23
|
+
readonly workflowVersion: string;
|
|
24
|
+
readonly stepId: string;
|
|
25
|
+
readonly stepKey: string;
|
|
26
|
+
readonly messageId: string;
|
|
27
|
+
readonly attempt: number;
|
|
28
|
+
readonly maxAttempts: number;
|
|
29
|
+
readonly workerId: string;
|
|
30
|
+
readonly signal: AbortSignal;
|
|
31
|
+
}
|
|
32
|
+
export interface SupaCloudWorkflowStepHandler {
|
|
33
|
+
decode(input: SupaCloudWorkflowJson): unknown;
|
|
34
|
+
authorize(input: unknown, context: SupaCloudWorkflowStepContext): boolean | Promise<boolean>;
|
|
35
|
+
execute(input: unknown, context: SupaCloudWorkflowStepContext): SupaCloudWorkflowStepResult | Promise<SupaCloudWorkflowStepResult>;
|
|
36
|
+
}
|
|
37
|
+
export interface SupaCloudWorkflowDefinition {
|
|
38
|
+
readonly workflowName: string;
|
|
39
|
+
readonly workflowVersion: string;
|
|
40
|
+
readonly stepKey: string;
|
|
41
|
+
readonly handler: SupaCloudWorkflowStepHandler;
|
|
42
|
+
}
|
|
43
|
+
export interface SupaCloudWorkflowRegistry {
|
|
44
|
+
resolve(workflowName: string, workflowVersion: string, stepKey: string): SupaCloudWorkflowDefinition | undefined;
|
|
45
|
+
}
|
|
46
|
+
export interface SupaCloudWorkflowTransport {
|
|
47
|
+
claim(signal: AbortSignal): Promise<SupaCloudWorkflowClaim | null>;
|
|
48
|
+
ack(claim: SupaCloudWorkflowClaim, output: unknown): Promise<SupaCloudWorkflowRun>;
|
|
49
|
+
fail(claim: SupaCloudWorkflowClaim, error: unknown): Promise<SupaCloudWorkflowRun>;
|
|
50
|
+
}
|
|
51
|
+
export declare function createSupaCloudWorkflowRegistry(definitions: readonly SupaCloudWorkflowDefinition[]): SupaCloudWorkflowRegistry;
|
|
52
|
+
export type SupaCloudWorkflowDispatchResult = {
|
|
53
|
+
readonly status: "idle";
|
|
54
|
+
} | {
|
|
55
|
+
readonly status: "dead_lettered";
|
|
56
|
+
readonly claim: Exclude<SupaCloudWorkflowClaimResult, null>;
|
|
57
|
+
} | {
|
|
58
|
+
readonly status: "discarded";
|
|
59
|
+
readonly claim: Exclude<SupaCloudWorkflowClaimResult, null>;
|
|
60
|
+
} | {
|
|
61
|
+
readonly status: "advanced" | "completed" | "retried" | "failed";
|
|
62
|
+
readonly run: SupaCloudWorkflowRun;
|
|
63
|
+
};
|
|
64
|
+
export declare class SupaCloudWorkflowDispatchError extends Error {
|
|
65
|
+
readonly code: "WORKFLOW_HANDLER_NOT_FOUND" | "WORKFLOW_WORKER_MISMATCH" | "WORKFLOW_HANDLER_DECODE_FAILED" | "WORKFLOW_HANDLER_AUTHORIZATION_FAILED" | "WORKFLOW_HANDLER_EXECUTION_FAILED" | "WORKFLOW_HANDLER_RESULT_INVALID";
|
|
66
|
+
readonly claim: SupaCloudWorkflowClaim;
|
|
67
|
+
constructor(code: "WORKFLOW_HANDLER_NOT_FOUND" | "WORKFLOW_WORKER_MISMATCH" | "WORKFLOW_HANDLER_DECODE_FAILED" | "WORKFLOW_HANDLER_AUTHORIZATION_FAILED" | "WORKFLOW_HANDLER_EXECUTION_FAILED" | "WORKFLOW_HANDLER_RESULT_INVALID", claim: SupaCloudWorkflowClaim, options?: {
|
|
68
|
+
cause?: unknown;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
type WorkflowMutationPort = Pick<SupaCloudWorkflowsClient, "claim" | "advance" | "complete" | "retry" | "fail">;
|
|
72
|
+
type WorkflowTransportPort = Pick<SupaCloudWorkflowsClient, "claim" | "complete" | "retry" | "fail">;
|
|
73
|
+
export declare function createSupaCloudWorkflowTransport(options: {
|
|
74
|
+
readonly workflows: WorkflowTransportPort;
|
|
75
|
+
readonly workerId: string;
|
|
76
|
+
readonly visibilityTimeoutSeconds?: number;
|
|
77
|
+
readonly retryDelaySeconds?: number;
|
|
78
|
+
readonly acceptClaim?: (claim: SupaCloudWorkflowClaim) => void | Promise<void>;
|
|
79
|
+
}): SupaCloudWorkflowTransport;
|
|
80
|
+
/**
|
|
81
|
+
* Executes exactly one claimed step. Queue polling, leases and retry budgets
|
|
82
|
+
* remain owned by the Workflow service; an unknown claim is left unsettled.
|
|
83
|
+
*/
|
|
84
|
+
export declare function createSupaCloudWorkflowDispatcher(options: {
|
|
85
|
+
readonly workflows: WorkflowMutationPort;
|
|
86
|
+
readonly registry: SupaCloudWorkflowRegistry;
|
|
87
|
+
readonly workerId: string;
|
|
88
|
+
readonly visibilityTimeoutSeconds?: number;
|
|
89
|
+
readonly pollIntervalMs?: number;
|
|
90
|
+
}): {
|
|
91
|
+
dispatchOnce: (signal?: AbortSignal) => Promise<SupaCloudWorkflowDispatchResult>;
|
|
92
|
+
run(signal?: AbortSignal): Promise<void>;
|
|
93
|
+
};
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { SupaCloudWorkflowClaim, SupaCloudWorkflowClaimResult, SupaCloudWorkflowJson, SupaCloudWorkflowRun, SupaCloudWorkflowsClient } from "./workflows.mjs";
|
|
2
|
+
export type SupaCloudWorkflowStepResult = {
|
|
3
|
+
kind: "advance";
|
|
4
|
+
nextStepKey: string;
|
|
5
|
+
output?: SupaCloudWorkflowJson;
|
|
6
|
+
nextInput?: SupaCloudWorkflowJson;
|
|
7
|
+
nextMaxAttempts?: number;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "complete";
|
|
10
|
+
stepOutput?: SupaCloudWorkflowJson;
|
|
11
|
+
runOutput?: SupaCloudWorkflowJson;
|
|
12
|
+
} | {
|
|
13
|
+
kind: "retry";
|
|
14
|
+
errorMessage: string;
|
|
15
|
+
delaySeconds?: number;
|
|
16
|
+
} | {
|
|
17
|
+
kind: "fail";
|
|
18
|
+
errorMessage: string;
|
|
19
|
+
};
|
|
20
|
+
export interface SupaCloudWorkflowStepContext {
|
|
21
|
+
readonly runId: string;
|
|
22
|
+
readonly workflowName: string;
|
|
23
|
+
readonly workflowVersion: string;
|
|
24
|
+
readonly stepId: string;
|
|
25
|
+
readonly stepKey: string;
|
|
26
|
+
readonly messageId: string;
|
|
27
|
+
readonly attempt: number;
|
|
28
|
+
readonly maxAttempts: number;
|
|
29
|
+
readonly workerId: string;
|
|
30
|
+
readonly signal: AbortSignal;
|
|
31
|
+
}
|
|
32
|
+
export interface SupaCloudWorkflowStepHandler {
|
|
33
|
+
decode(input: SupaCloudWorkflowJson): unknown;
|
|
34
|
+
authorize(input: unknown, context: SupaCloudWorkflowStepContext): boolean | Promise<boolean>;
|
|
35
|
+
execute(input: unknown, context: SupaCloudWorkflowStepContext): SupaCloudWorkflowStepResult | Promise<SupaCloudWorkflowStepResult>;
|
|
36
|
+
}
|
|
37
|
+
export interface SupaCloudWorkflowDefinition {
|
|
38
|
+
readonly workflowName: string;
|
|
39
|
+
readonly workflowVersion: string;
|
|
40
|
+
readonly stepKey: string;
|
|
41
|
+
readonly handler: SupaCloudWorkflowStepHandler;
|
|
42
|
+
}
|
|
43
|
+
export interface SupaCloudWorkflowRegistry {
|
|
44
|
+
resolve(workflowName: string, workflowVersion: string, stepKey: string): SupaCloudWorkflowDefinition | undefined;
|
|
45
|
+
}
|
|
46
|
+
export interface SupaCloudWorkflowTransport {
|
|
47
|
+
claim(signal: AbortSignal): Promise<SupaCloudWorkflowClaim | null>;
|
|
48
|
+
ack(claim: SupaCloudWorkflowClaim, output: unknown): Promise<SupaCloudWorkflowRun>;
|
|
49
|
+
fail(claim: SupaCloudWorkflowClaim, error: unknown): Promise<SupaCloudWorkflowRun>;
|
|
50
|
+
}
|
|
51
|
+
export declare function createSupaCloudWorkflowRegistry(definitions: readonly SupaCloudWorkflowDefinition[]): SupaCloudWorkflowRegistry;
|
|
52
|
+
export type SupaCloudWorkflowDispatchResult = {
|
|
53
|
+
readonly status: "idle";
|
|
54
|
+
} | {
|
|
55
|
+
readonly status: "dead_lettered";
|
|
56
|
+
readonly claim: Exclude<SupaCloudWorkflowClaimResult, null>;
|
|
57
|
+
} | {
|
|
58
|
+
readonly status: "discarded";
|
|
59
|
+
readonly claim: Exclude<SupaCloudWorkflowClaimResult, null>;
|
|
60
|
+
} | {
|
|
61
|
+
readonly status: "advanced" | "completed" | "retried" | "failed";
|
|
62
|
+
readonly run: SupaCloudWorkflowRun;
|
|
63
|
+
};
|
|
64
|
+
export declare class SupaCloudWorkflowDispatchError extends Error {
|
|
65
|
+
readonly code: "WORKFLOW_HANDLER_NOT_FOUND" | "WORKFLOW_WORKER_MISMATCH" | "WORKFLOW_HANDLER_DECODE_FAILED" | "WORKFLOW_HANDLER_AUTHORIZATION_FAILED" | "WORKFLOW_HANDLER_EXECUTION_FAILED" | "WORKFLOW_HANDLER_RESULT_INVALID";
|
|
66
|
+
readonly claim: SupaCloudWorkflowClaim;
|
|
67
|
+
constructor(code: "WORKFLOW_HANDLER_NOT_FOUND" | "WORKFLOW_WORKER_MISMATCH" | "WORKFLOW_HANDLER_DECODE_FAILED" | "WORKFLOW_HANDLER_AUTHORIZATION_FAILED" | "WORKFLOW_HANDLER_EXECUTION_FAILED" | "WORKFLOW_HANDLER_RESULT_INVALID", claim: SupaCloudWorkflowClaim, options?: {
|
|
68
|
+
cause?: unknown;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
type WorkflowMutationPort = Pick<SupaCloudWorkflowsClient, "claim" | "advance" | "complete" | "retry" | "fail">;
|
|
72
|
+
type WorkflowTransportPort = Pick<SupaCloudWorkflowsClient, "claim" | "complete" | "retry" | "fail">;
|
|
73
|
+
export declare function createSupaCloudWorkflowTransport(options: {
|
|
74
|
+
readonly workflows: WorkflowTransportPort;
|
|
75
|
+
readonly workerId: string;
|
|
76
|
+
readonly visibilityTimeoutSeconds?: number;
|
|
77
|
+
readonly retryDelaySeconds?: number;
|
|
78
|
+
readonly acceptClaim?: (claim: SupaCloudWorkflowClaim) => void | Promise<void>;
|
|
79
|
+
}): SupaCloudWorkflowTransport;
|
|
80
|
+
/**
|
|
81
|
+
* Executes exactly one claimed step. Queue polling, leases and retry budgets
|
|
82
|
+
* remain owned by the Workflow service; an unknown claim is left unsettled.
|
|
83
|
+
*/
|
|
84
|
+
export declare function createSupaCloudWorkflowDispatcher(options: {
|
|
85
|
+
readonly workflows: WorkflowMutationPort;
|
|
86
|
+
readonly registry: SupaCloudWorkflowRegistry;
|
|
87
|
+
readonly workerId: string;
|
|
88
|
+
readonly visibilityTimeoutSeconds?: number;
|
|
89
|
+
readonly pollIntervalMs?: number;
|
|
90
|
+
}): {
|
|
91
|
+
dispatchOnce: (signal?: AbortSignal) => Promise<SupaCloudWorkflowDispatchResult>;
|
|
92
|
+
run(signal?: AbortSignal): Promise<void>;
|
|
93
|
+
};
|
|
94
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.42.1",
|
|
4
4
|
"description": "Platform SDK for SupaCloud, built on top of supabase-js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"directory": "packages/supacloud-js"
|
|
97
97
|
},
|
|
98
98
|
"peerDependencies": {
|
|
99
|
-
"@supabase/supabase-js": "^2.
|
|
99
|
+
"@supabase/supabase-js": "^2.117.3",
|
|
100
100
|
"@tanstack/query-core": "^5.104.1"
|
|
101
101
|
},
|
|
102
102
|
"peerDependenciesMeta": {
|
|
@@ -105,18 +105,18 @@
|
|
|
105
105
|
}
|
|
106
106
|
},
|
|
107
107
|
"dependencies": {
|
|
108
|
-
"@supacloud/contracts": "0.
|
|
109
|
-
"@supacloud/query": "0.
|
|
108
|
+
"@supacloud/contracts": "0.9.0",
|
|
109
|
+
"@supacloud/query": "0.3.1",
|
|
110
110
|
"rxjs": "7.8.2"
|
|
111
111
|
},
|
|
112
112
|
"devDependencies": {
|
|
113
|
-
"@types/bun": "^1.4.
|
|
114
|
-
"@supabase/supabase-js": "^2.
|
|
113
|
+
"@types/bun": "^1.4.3",
|
|
114
|
+
"@supabase/supabase-js": "^2.117.3",
|
|
115
115
|
"typescript": "^7.0.2",
|
|
116
|
-
"@tanstack/query-core": "5.104.
|
|
117
|
-
"@tanstack/svelte-query": "6.3.
|
|
118
|
-
"typebox": "1.3.
|
|
119
|
-
"svelte": "5.57.
|
|
116
|
+
"@tanstack/query-core": "5.104.2",
|
|
117
|
+
"@tanstack/svelte-query": "6.3.2",
|
|
118
|
+
"typebox": "1.3.37",
|
|
119
|
+
"svelte": "5.57.2"
|
|
120
120
|
},
|
|
121
121
|
"engines": {
|
|
122
122
|
"node": ">=22.12.0"
|