@lunora/workflow 1.0.0-alpha.45 → 1.0.0-alpha.46
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/do/index.mjs +1 -1
- package/dist/index.d.mts +10 -17
- package/dist/index.d.ts +10 -17
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/MAX_BRANCHES-j59ZUUSu.mjs +1 -0
- package/dist/packem_shared/NonRetryableError-BPu01mU8.mjs +1 -0
- package/dist/packem_shared/createRunStep-D9PS5mFS.mjs +1 -0
- package/dist/packem_shared/{createWaitForEvent-KkdE11qX.mjs → createWaitForEvent-D5Ohn2Pz.mjs} +1 -1
- package/dist/packem_shared/{createWorkflowRunContext-B00Q8USs.mjs → createWorkflowRunContext-CrZIe0U-.mjs} +1 -1
- package/dist/packem_shared/isDuplicateInstanceError-CecnqNAa.mjs +1 -0
- package/dist/packem_shared/{run-step-CAUCn1UO.mjs → run-step--dLU2w7O.mjs} +1 -1
- package/package.json +3 -3
- package/dist/packem_shared/MAX_BRANCHES-DQ7WZU8W.mjs +0 -1
- package/dist/packem_shared/NonRetryableError-BV6ELTK1.mjs +0 -1
- package/dist/packem_shared/createRunStep-C6_1x53O.mjs +0 -1
package/dist/do/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{WorkflowEntrypoint as p}from"cloudflare:workers";import{NonRetryableError as i}from"cloudflare:workflows";import{convertNonRetryableError as m}from"../packem_shared/NonRetryableError-
|
|
1
|
+
import{WorkflowEntrypoint as p}from"cloudflare:workers";import{NonRetryableError as i}from"cloudflare:workflows";import{convertNonRetryableError as m}from"../packem_shared/NonRetryableError-BPu01mU8.mjs";import{extractBranchMarker as f,stripBranchMarker as h,signalBranchParentSafe as l,errorOutcome as u,okOutcome as y}from"../packem_shared/MAX_BRANCHES-j59ZUUSu.mjs";import{createWorkflowRunContext as d}from"../packem_shared/createWorkflowRunContext-CrZIe0U-.mjs";class N extends p{#r;#t;constructor(t,e,o,r){super(t,e),this.#t=o,this.#r=r??"workflow"}async run(t,e){const o=e,r=f(t.payload),c=r?{...t,payload:h(t.payload)}:t,a=d({env:this.env,event:c,exportName:this.#r,nonRetryableErrorClass:i,step:o});let n;try{n=await this.#t.handler(a)}catch(s){return r&&await l({env:this.env,log:a.log,step:o},r,u(s)),m(s,i)}return r&&await l({env:this.env,log:a.log,step:o},r,y(n)),n}}export{N as default};
|
package/dist/index.d.mts
CHANGED
|
@@ -129,6 +129,16 @@ declare const workflowDefaultName: (exportName: string) => string;
|
|
|
129
129
|
declare const defineWorkflow: <Params = Record<string, unknown>, Output = unknown>(config: WorkflowConfig<Params, Output>) => WorkflowDefinition<Params, Output>;
|
|
130
130
|
/** True when a value is a `defineWorkflow` result (the runtime brand check). */
|
|
131
131
|
declare const isWorkflowDefinition: (value: unknown) => value is WorkflowDefinition;
|
|
132
|
+
/**
|
|
133
|
+
* Whether a `WorkflowBinding.create()` rejection is a duplicate-instance-id
|
|
134
|
+
* error — the idempotency signal that a *previous* attempt's create already
|
|
135
|
+
* applied — as opposed to a transient or config failure (Workflows service
|
|
136
|
+
* error, instance-creation quota, bad params).
|
|
137
|
+
*
|
|
138
|
+
* Every other failure MUST surface, so the caller retries or fails visibly
|
|
139
|
+
* rather than silently reporting success for work that never started.
|
|
140
|
+
*/
|
|
141
|
+
declare const isDuplicateInstanceError: (error: unknown) => boolean;
|
|
132
142
|
/**
|
|
133
143
|
* Throw from a workflow step (or handler) to fail the instance immediately
|
|
134
144
|
* **without** retrying — the portable mirror of `cloudflare:workflows`'
|
|
@@ -162,23 +172,6 @@ declare const toNativeNonRetryableError: (error: NonRetryableError, NativeNonRet
|
|
|
162
172
|
* Always throws — the `never` return lets callers `return convertNonRetryableError(...)`.
|
|
163
173
|
*/
|
|
164
174
|
declare const convertNonRetryableError: (error: unknown, NativeNonRetryableError: NativeNonRetryableErrorConstructor | undefined) => never;
|
|
165
|
-
/**
|
|
166
|
-
* Whether a `WorkflowBinding.create()` rejection is a duplicate-instance-id
|
|
167
|
-
* error — the idempotency signal that a *previous* attempt's create already
|
|
168
|
-
* applied — as opposed to a transient or config failure (Workflows service
|
|
169
|
-
* error, instance-creation quota, bad params).
|
|
170
|
-
*
|
|
171
|
-
* `step.do` memoizes a step's RESULT, not its side effects: a spawn body that
|
|
172
|
-
* fails after `create` landed is re-run, and only this rejection means the
|
|
173
|
-
* child is already there to attach to. Every other failure MUST surface, so the
|
|
174
|
-
* durable step retries or fails visibly rather than silently attaching to an
|
|
175
|
-
* unrelated instance.
|
|
176
|
-
*
|
|
177
|
-
* Lives here, in the package that owns the Workflows binding contract, so the
|
|
178
|
-
* fan-out spawn and `@lunora/agent`'s sub-agent/channel dispatch cannot drift
|
|
179
|
-
* apart on what counts as "already exists".
|
|
180
|
-
*/
|
|
181
|
-
declare const isDuplicateInstanceError: (error: unknown) => boolean;
|
|
182
175
|
/** Hard cap on branches per `ctx.parallel` call — auto-scale, never silently spawn unbounded DOs. */
|
|
183
176
|
declare const MAX_BRANCHES = 100;
|
|
184
177
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -129,6 +129,16 @@ declare const workflowDefaultName: (exportName: string) => string;
|
|
|
129
129
|
declare const defineWorkflow: <Params = Record<string, unknown>, Output = unknown>(config: WorkflowConfig<Params, Output>) => WorkflowDefinition<Params, Output>;
|
|
130
130
|
/** True when a value is a `defineWorkflow` result (the runtime brand check). */
|
|
131
131
|
declare const isWorkflowDefinition: (value: unknown) => value is WorkflowDefinition;
|
|
132
|
+
/**
|
|
133
|
+
* Whether a `WorkflowBinding.create()` rejection is a duplicate-instance-id
|
|
134
|
+
* error — the idempotency signal that a *previous* attempt's create already
|
|
135
|
+
* applied — as opposed to a transient or config failure (Workflows service
|
|
136
|
+
* error, instance-creation quota, bad params).
|
|
137
|
+
*
|
|
138
|
+
* Every other failure MUST surface, so the caller retries or fails visibly
|
|
139
|
+
* rather than silently reporting success for work that never started.
|
|
140
|
+
*/
|
|
141
|
+
declare const isDuplicateInstanceError: (error: unknown) => boolean;
|
|
132
142
|
/**
|
|
133
143
|
* Throw from a workflow step (or handler) to fail the instance immediately
|
|
134
144
|
* **without** retrying — the portable mirror of `cloudflare:workflows`'
|
|
@@ -162,23 +172,6 @@ declare const toNativeNonRetryableError: (error: NonRetryableError, NativeNonRet
|
|
|
162
172
|
* Always throws — the `never` return lets callers `return convertNonRetryableError(...)`.
|
|
163
173
|
*/
|
|
164
174
|
declare const convertNonRetryableError: (error: unknown, NativeNonRetryableError: NativeNonRetryableErrorConstructor | undefined) => never;
|
|
165
|
-
/**
|
|
166
|
-
* Whether a `WorkflowBinding.create()` rejection is a duplicate-instance-id
|
|
167
|
-
* error — the idempotency signal that a *previous* attempt's create already
|
|
168
|
-
* applied — as opposed to a transient or config failure (Workflows service
|
|
169
|
-
* error, instance-creation quota, bad params).
|
|
170
|
-
*
|
|
171
|
-
* `step.do` memoizes a step's RESULT, not its side effects: a spawn body that
|
|
172
|
-
* fails after `create` landed is re-run, and only this rejection means the
|
|
173
|
-
* child is already there to attach to. Every other failure MUST surface, so the
|
|
174
|
-
* durable step retries or fails visibly rather than silently attaching to an
|
|
175
|
-
* unrelated instance.
|
|
176
|
-
*
|
|
177
|
-
* Lives here, in the package that owns the Workflows binding contract, so the
|
|
178
|
-
* fan-out spawn and `@lunora/agent`'s sub-agent/channel dispatch cannot drift
|
|
179
|
-
* apart on what counts as "already exists".
|
|
180
|
-
*/
|
|
181
|
-
declare const isDuplicateInstanceError: (error: unknown) => boolean;
|
|
182
175
|
/** Hard cap on branches per `ctx.parallel` call — auto-scale, never silently spawn unbounded DOs. */
|
|
183
176
|
declare const MAX_BRANCHES = 100;
|
|
184
177
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createWorkflowContext as e}from"./packem_shared/createWorkflowContext-zQR_9N-v.mjs";import{default as f}from"./packem_shared/createWorkflows-LvAyUyKq.mjs";import{defineWorkflowEvent as a,isWorkflowEventDefinition as i}from"./packem_shared/defineWorkflowEvent-suERPEEV.mjs";import{defineStep as
|
|
1
|
+
import{createWorkflowContext as e}from"./packem_shared/createWorkflowContext-zQR_9N-v.mjs";import{default as f}from"./packem_shared/createWorkflows-LvAyUyKq.mjs";import{defineWorkflowEvent as a,isWorkflowEventDefinition as i}from"./packem_shared/defineWorkflowEvent-suERPEEV.mjs";import{defineStep as p,isStepDefinition as s}from"./packem_shared/defineStep-D1-9eOnA.mjs";import{defineWorkflow as w,isWorkflowDefinition as x,workflowBindingName as k,workflowClassName as c,workflowDefaultName as E}from"./packem_shared/defineWorkflow-tKaIifbZ.mjs";import{NonRetryableError as N,convertNonRetryableError as R,isNonRetryableError as v,toNativeNonRetryableError as d}from"./packem_shared/NonRetryableError-BPu01mU8.mjs";import{MAX_BRANCHES as u,branch as C}from"./packem_shared/MAX_BRANCHES-j59ZUUSu.mjs";import{WorkflowsRestError as S,createWorkflowsRestClient as y}from"./packem_shared/WorkflowsRestError-Ccq1FUR1.mjs";import{createWorkflowRunContext as g}from"./packem_shared/createWorkflowRunContext-CrZIe0U-.mjs";import{c as h,v as F}from"./packem_shared/run-step--dLU2w7O.mjs";import{createWaitForEvent as I}from"./packem_shared/createWaitForEvent-D5Ohn2Pz.mjs";import{isDuplicateInstanceError as X}from"./packem_shared/isDuplicateInstanceError-CecnqNAa.mjs";export{u as MAX_BRANCHES,N as NonRetryableError,S as WorkflowsRestError,C as branch,R as convertNonRetryableError,h as createRunStep,I as createWaitForEvent,e as createWorkflowContext,g as createWorkflowRunContext,f as createWorkflows,y as createWorkflowsRestClient,p as defineStep,w as defineWorkflow,a as defineWorkflowEvent,X as isDuplicateInstanceError,v as isNonRetryableError,s as isStepDefinition,x as isWorkflowDefinition,i as isWorkflowEventDefinition,d as toNativeNonRetryableError,F as validateStepArgs,k as workflowBindingName,c as workflowClassName,E as workflowDefaultName};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as R}from"@lunora/errors";import{h as x,B as A,a as g}from"./branch-marker-CCpWfS5k.mjs";import{RESERVED_EVENT_TYPE_PREFIX as P}from"./defineWorkflowEvent-suERPEEV.mjs";import{NonRetryableError as f}from"./NonRetryableError-BPu01mU8.mjs";import{isDuplicateInstanceError as N}from"./isDuplicateInstanceError-CecnqNAa.mjs";const p=t=>t.toString(16).padStart(4,"0"),O=t=>{let e=8997,n=33826,a=40164,o=52210;for(let c=0;c<t.length;c+=1){e^=t.charCodeAt(c);const l=e*435,d=n*435,h=a*435+e*256,r=o*435+n*256,s=d+(l>>>16),i=h+(s>>>16),u=r+(i>>>16);e=l&65535,n=s&65535,a=i&65535,o=u&65535}return p(o)+p(a)+p(n)+p(e)},v=100,I=100,b="lunora:spawn:",k="lunora:await:",C="lunora:signal:",W="lunora:compensate:",F=17,w=(t,e="")=>t.length+e.length<=I?`${t}${e}`:`${t.slice(0,I-e.length-F)}-${O(t)}${e}`,S=`${P}branch:`,G=(t,e,n)=>({compensateWith:n?.compensateWith,id:n?.id,params:e,timeout:n?.timeout,workflow:t}),T=t=>t instanceof Error?{message:t.message,name:t.name}:{message:String(t),name:"Error"},M=t=>({status:"ok",value:t}),U=t=>({error:T(t),status:"error"}),$=1048576,X=t=>{try{return String(t instanceof Error?t.message:t).slice(0,200)}catch{return"the failure itself could not be described"}},B=t=>{let e;try{e=new TextEncoder().encode(JSON.stringify(t)).length}catch(n){return{error:{message:`branch outcome cannot be serialised to JSON (${X(n)}) — the parent can never receive it. Return a plain JSON value (no cycles, no BigInt, no class instance that fails to serialise) or a reference the parent can dereference (an R2 key, a row id)`,name:"BranchOutputUnserializable"},status:"error"}}return e<=$?t:{error:{message:`branch outcome serialises to ${String(e)} bytes, over Cloudflare's ${String($)}-byte event payload limit — the parent can never receive it. Return a reference the parent can dereference (an R2 key, a row id) instead of the payload itself`,name:"BranchOutputTooLarge"},status:"error"}},E=async(t,e)=>{try{return{attached:!1,instance:await t.create(e)}}catch(n){if(!N(n))throw n;return{attached:!0,instance:await t.get(e.id)}}},j=async t=>{const{error:e,output:n,status:a}=await t.status();if(a==="complete")return M(n);if(a==="errored"||a==="terminated")return{error:e??{message:`branch instance ${a}`,name:"Error"},status:"error"}},D=t=>{if(typeof t!="object"||t===null)return;const{status:e}=t;return e==="error"||e==="ok"?t:void 0};class m extends Error{branchError;kind;plan;constructor(e,n,a){super(n.message),this.branchError=n,this.kind=a,this.plan=e}}const H=async(t,e,n)=>{for(let a=e.length-1;a>=0;a-=1){const o=e[a],c=o?.plan.item.compensateWith;if(!(o===void 0||c===void 0))try{const l=t.resolveBinding(c);await t.step.do(`${W}${o.plan.childId}`,async()=>{const d=w(o.plan.childId,"-compensate"),h={branch:o.plan.item.workflow,error:n,index:o.plan.index,output:o.output};return await E(l,{id:d,params:h}),d})}catch(l){t.log?.error(`ctx.parallel: group-saga compensation "${c}" for branch "${o.plan.item.workflow}" (#${String(o.plan.index)}) failed`,l)}}},q=t=>async n=>{if(n.length===0)return[];if(n.length>v)throw new f(`ctx.parallel: ${String(n.length)} branches exceeds the cap of ${String(v)} — split the fan-out or raise the work into fewer child workflows`);const a=n.map((r,s)=>{const i=w(t.nextChildId(r.id));return{childId:i,eventType:`${S}${i}`,index:s,item:r}}),o=new Set;for(const r of a){if(o.has(r.childId))throw new f(`ctx.parallel: duplicate branch id "${r.childId}" — each branch in a group must resolve to a unique child instance id (check explicit \`id\` options)`);o.add(r.childId)}const c=await Promise.all(a.map(r=>t.step.do(`${b}${r.childId}`,async()=>{const s=t.resolveBinding(r.item.workflow),i={eventType:r.eventType,index:r.index,parentBinding:t.parentBinding,parentId:t.instanceId},{attached:u,instance:_}=await E(s,{id:r.childId,params:{...r.item.params,[g]:i}});if(!u)return;const y=await j(_);return y===void 0?void 0:B(y)}))),l=Array.from({length:a.length}),d=Array.from({length:a.length}),h=async(r,s)=>{let i;if(s===void 0)try{i=(await t.step.waitForEvent(`${k}${r.childId}`,{timeout:r.item.timeout,type:r.eventType})).payload}catch(u){throw new m(r,T(u),"join failed")}else i=s;if(i.status==="error")throw new m(r,i.error,"failed");d[r.index]={output:i.value,plan:r},l[r.index]=i.value};try{await Promise.all(a.map((r,s)=>h(r,D(c[s]))))}catch(r){throw r instanceof m?(await H(t,d.filter(s=>s!==void 0),r.branchError),new f(`ctx.parallel: branch "${r.plan.item.workflow}" (#${String(r.plan.index)}) ${r.kind}: ${r.branchError.message}`)):r}return l},Q=t=>async(e,n,a)=>{if(x(n))throw new R("BAD_REQUEST",`@lunora/workflow: params ${A}`);const o=w(t.nextChildId(a?.id));return await t.step.do(`${b}${o}`,async()=>{const c=t.resolveBinding(e);return await E(c,{id:o,params:n}),o}),t.resolveBinding(e).get(o)},Z=t=>{if(typeof t!="object"||t===null)return;const e=t[g];if(typeof e!="object"||e===null)return;const n=e;if(!(typeof n.eventType!="string"||typeof n.parentBinding!="string"||typeof n.parentId!="string"||typeof n.index!="number")&&!(!n.parentBinding.startsWith("WORKFLOW_")||!n.eventType.startsWith(S)))return{eventType:n.eventType,index:n.index,parentBinding:n.parentBinding,parentId:n.parentId}},tt=t=>{if(typeof t!="object"||t===null)return t;const e={...t};return Reflect.deleteProperty(e,g),e},L=async(t,e,n)=>{const a=t.env[e.parentBinding];if(!a||typeof a.get!="function")return;const o=a.get.bind(a),c=B(n);await t.step.do(`${C}${String(e.index)}`,async()=>(await(await o(e.parentId)).sendEvent({payload:c,type:e.eventType}),e.eventType))},et=async(t,e,n)=>{try{await L(t,e,n)}catch(a){t.log?.error(`@lunora/workflow: failed to signal branch parent "${e.parentId}" (event "${e.eventType}")`,a)}};export{v as MAX_BRANCHES,G as branch,q as createParallel,Q as createSpawn,U as errorOutcome,Z as extractBranchMarker,M as okOutcome,L as signalBranchParent,et as signalBranchParentSafe,tt as stripBranchMarker};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isDuplicateInstanceError as E}from"./isDuplicateInstanceError-CecnqNAa.mjs";const s="__lunoraNonRetryable";class r extends Error{constructor(e,n="NonRetryableError"){super(e),this.name=n,this[s]=!0}}const a=t=>t instanceof Error&&t[s]===!0,c=(t,e)=>{const n=new e(t.message,t.name);return t.stack!==void 0&&(n.stack=t.stack),t.cause!==void 0&&n.cause===void 0&&(n.cause=t.cause),n},i=(t,e)=>{throw e!==void 0&&a(t)?c(t,e):t},u=(t,e,n)=>{const o=new r(t);return e!==void 0&&(o.cause=e),i(o,n)};export{r as NonRetryableError,i as convertNonRetryableError,E as isDuplicateInstanceError,a as isNonRetryableError,u as raiseNonRetryable,c as toNativeNonRetryableError};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as p,v as o}from"./run-step--dLU2w7O.mjs";import"./defineWorkflowEvent-suERPEEV.mjs";import"./NonRetryableError-BPu01mU8.mjs";export{p as createRunStep,o as validateStepArgs};
|
package/dist/packem_shared/{createWaitForEvent-KkdE11qX.mjs → createWaitForEvent-D5Ohn2Pz.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{eventDefinitionProblem as m,RESERVED_EVENT_TYPE_PREFIX as i}from"./defineWorkflowEvent-suERPEEV.mjs";import{raiseNonRetryable as a}from"./NonRetryableError-
|
|
1
|
+
import{eventDefinitionProblem as m,RESERVED_EVENT_TYPE_PREFIX as i}from"./defineWorkflowEvent-suERPEEV.mjs";import{raiseNonRetryable as a}from"./NonRetryableError-BPu01mU8.mjs";const c=e=>async(r,t)=>{const n=m(r);if(n!==void 0)return a(`@lunora/workflow: ctx.waitForEvent ${n}`,void 0,e.nonRetryableErrorClass);if(t?.name?.startsWith(i)===!0)return a(`@lunora/workflow: ctx.waitForEvent step name "${t.name}" is reserved — the "${i}" prefix is used by the framework's own steps`,void 0,e.nonRetryableErrorClass);const s=await e.step.waitForEvent(t?.name??`event:${r.type}`,{timeout:t?.timeout,type:r.type});try{return r.payload.parse(s.payload)}catch(o){const l=o instanceof Error?o.message:String(o);return a(`@lunora/workflow: event "${r.type}" payload validation failed: ${l}`,o,e.nonRetryableErrorClass)}};export{c as createWaitForEvent};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{J as w,c as v}from"./run-step
|
|
1
|
+
import{J as w,c as v}from"./run-step--dLU2w7O.mjs";import{LunoraError as i}from"@lunora/errors";import{workflowBindingName as s}from"./defineWorkflow-tKaIifbZ.mjs";import{createSpawn as u,createParallel as m}from"./MAX_BRANCHES-j59ZUUSu.mjs";import{createWaitForEvent as p}from"./createWaitForEvent-D5Ohn2Pz.mjs";const g=e=>({debug:(r,...n)=>{console.debug(e,r,...n)},error:(r,...n)=>{console.error(e,r,...n)},info:(r,...n)=>{console.info(e,r,...n)},warn:(r,...n)=>{console.warn(e,r,...n)}}),I=e=>{const r=g(`[workflow:${e.exportName}]`),n=w({env:e.env,fetchImpl:e.fetchImpl,label:"@lunora/workflow"}),f=t=>{const a=s(t),o=e.env[a];if(!o||typeof o.create!="function"||typeof o.get!="function")throw new i("INTERNAL",`@lunora/workflow: cannot spawn child workflow "${t}" — no Workflow binding "${a}" on env (is it declared in lunora/workflows.ts?)`);return o};let l=0;const d=t=>{if(t!==void 0)return t;const a=`${e.event.instanceId}-c${String(l)}`;return l+=1,a},c={env:e.env,instanceId:e.event.instanceId,log:r,nextChildId:d,parentBinding:s(e.exportName),resolveBinding:f,step:e.step};return{env:e.env,event:e.event,log:r,parallel:m(c),params:e.event.payload,run:n,runStep:v({env:e.env,log:r,nonRetryableErrorClass:e.nonRetryableErrorClass,run:n,step:e.step}),spawn:u(c),step:e.step,waitForEvent:p({nonRetryableErrorClass:e.nonRetryableErrorClass,step:e.step})}};export{I as createWorkflowRunContext};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const t=/already[\s_-]?exists/iu,e=s=>t.test(s instanceof Error?s.message:String(s));export{e as isDuplicateInstanceError};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as m,isLunoraError as $}from"@lunora/errors";import{parseValidatorMap as S}from"@lunora/values";import{RESERVED_EVENT_TYPE_PREFIX as E}from"./defineWorkflowEvent-suERPEEV.mjs";import{raiseNonRetryable as N,convertNonRetryableError as O}from"./NonRetryableError-
|
|
1
|
+
import{LunoraError as m,isLunoraError as $}from"@lunora/errors";import{parseValidatorMap as S}from"@lunora/values";import{RESERVED_EVENT_TYPE_PREFIX as E}from"./defineWorkflowEvent-suERPEEV.mjs";import{raiseNonRetryable as N,convertNonRetryableError as O}from"./NonRetryableError-BPu01mU8.mjs";const _=(t,r,e)=>{if(r===void 0)return{dispose:()=>{},signal:t};const a=new AbortController,o=setTimeout(()=>{a.abort(e())},r);return{dispose:()=>{clearTimeout(o)},signal:a.signal}},f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",A=t=>{let r="",e=0;const a=t.length-2;for(;e<a;e+=3){const n=t[e]<<16|t[e+1]<<8|t[e+2];r+=f.charAt(n>>18&63)+f.charAt(n>>12&63)+f.charAt(n>>6&63)+f.charAt(n&63)}const o=t.length-e;if(o===1){const n=t[e]<<16;r+=f.charAt(n>>18&63)+f.charAt(n>>12&63)}else if(o===2){const n=t[e]<<16|t[e+1]<<8;r+=f.charAt(n>>18&63)+f.charAt(n>>12&63)+f.charAt(n>>6&63)}return r};new TextDecoder;const v=new TextEncoder,w="=",L=t=>{for(let r=0;r<t.length;r+=1)if(t.charCodeAt(r)>255)return!1;return!0},k=t=>A(v.encode(JSON.stringify(t))),D=t=>!t.startsWith(w)&&L(t)?t:`${w}${A(v.encode(t))}`,x="/_lunora/scheduler/dispatch",C=3e4,U=t=>{let r=t.length;for(;r>0&&t[r-1]==="/";)r-=1;return t.slice(0,r)},R=Symbol("lunoraDispatchFailure"),P=Symbol("lunoraDispatchMessageId"),T=(t,r)=>(r!==void 0&&Object.defineProperty(t,P,{value:r}),t),M=(t,r)=>(Object.defineProperty(t,R,{value:!0}),T(t,r)),J=(t,r,e,a)=>{try{const o=JSON.parse(e)?.error;if(typeof o=="object"&&o!==null&&typeof o.code=="string"){const{code:n,data:i,message:l}=o;return M(new m(n,typeof l=="string"?l:void 0,{data:i,status:r}),a)}}catch{}return T(new m("INTERNAL",`${t}: function dispatch failed (${String(r)}): ${e}`,{status:r}),a)},j=new Set([400,403,404,422]),H=new Set(["DISPATCH_UNAUTHENTICATED"]),K=t=>$(t)&&t[R]===!0&&j.has(t.status)&&!H.has(t.code),W=(t,r,e)=>new m("INTERNAL",`${t}: function dispatch to "${r}" timed out after ${String(e)}ms`,{status:503}),X=t=>{const{label:r}=t,e=globalThis.fetch,a=t.fetchImpl??(typeof e=="function"?e.bind(globalThis):void 0);return async(o,n,i={})=>{if(typeof a!="function")throw new TypeError(`${r}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const l=t.env.LUNORA_ORIGIN_URL;if(typeof l!="string"||l.length===0)throw new m("INTERNAL",`${r}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const d=t.env.LUNORA_ADMIN_TOKEN;if(typeof d!="string"||d.length===0)throw new m("INTERNAL",`${r}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const b=`${U(l)}${x}`,c={authorization:`Bearer ${d}`,"content-type":"application/json"};t.identity?.userId!==void 0&&(c["x-lunora-userid"]=D(t.identity.userId)),t.identity?.claims!==void 0&&(c["x-lunora-identity"]=k(t.identity.claims));const p=i.timeoutMs??C,g=_(void 0,p,()=>new DOMException(`dispatch timed out after ${String(p)}ms`,"TimeoutError")),s=h=>{throw h instanceof Error&&h.name==="TimeoutError"?W(r,o.__lunoraRef,p):h};let u;try{try{u=await a(b,{body:JSON.stringify({args:n??{},functionPath:o.__lunoraRef,id:i.dedupId,shardKey:i.shardKey}),headers:c,method:"POST",signal:g.signal})}catch(y){return s(y)}if(!u.ok){let y;try{y=await u.text()}catch(I){return s(I)}throw J(r,u.status,y,i.messageId)}let h;try{h=await u.text()}catch(y){return s(y)}if(h.length===0)return;try{return JSON.parse(h)}catch{throw new m("INTERNAL",`${r}: function dispatch returned a non-JSON body (${String(u.status)}): ${h}`,{status:u.status})}}finally{g.dispose()}}},V=(t,r)=>S(t,r,"step args"),Y=t=>async(r,e,a)=>{const o=a?.config??r.config,n=a?.name??r.name;if(n.startsWith(E))return N(`@lunora/workflow: ctx.runStep step name "${n}" is reserved — the "${E}" prefix is used by the framework's own steps`,void 0,t.nonRetryableErrorClass);const i=V(r.args,e),l=async c=>{const p={attempt:c.attempt,config:c.config,env:t.env,log:t.log,run:t.run,step:c.step};let g;try{g=await r.handler(p,i)}catch(s){return K(s)?N(s.message,s,t.nonRetryableErrorClass):O(s,t.nonRetryableErrorClass)}if(!r.returns)return g;try{return r.returns.parse(g)}catch(s){const u=s instanceof Error?s.message:String(s);return N(`step "${r.name}" returns validation failed: ${u}`,s,t.nonRetryableErrorClass)}},d=r.rollback,b=d?{rollback:async c=>{await d({args:i,env:t.env,error:c.error,log:t.log,output:c.output,run:t.run})},rollbackConfig:r.rollbackConfig}:void 0;return o===void 0?t.step.do(n,l,b):t.step.do(n,o,l,b)};export{X as J,Y as c,V as v};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/workflow",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.46",
|
|
4
4
|
"description": "Durable workflows for Lunora: defineWorkflow over Cloudflare Workflows, generated WorkflowEntrypoint classes, and the ctx.workflows surface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
52
|
-
"@lunora/values": "1.0.0-alpha.
|
|
51
|
+
"@lunora/errors": "1.0.0-alpha.32",
|
|
52
|
+
"@lunora/values": "1.0.0-alpha.40"
|
|
53
53
|
},
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": "^22.15.0 || >=24.11.0"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as B}from"@lunora/errors";import{h as _,B as R,a as g}from"./branch-marker-CCpWfS5k.mjs";import{RESERVED_EVENT_TYPE_PREFIX as A}from"./defineWorkflowEvent-suERPEEV.mjs";import{NonRetryableError as f,isDuplicateInstanceError as P}from"./NonRetryableError-BV6ELTK1.mjs";const p=t=>t.toString(16).padStart(4,"0"),N=t=>{let e=8997,n=33826,a=40164,o=52210;for(let c=0;c<t.length;c+=1){e^=t.charCodeAt(c);const l=e*435,d=n*435,h=a*435+e*256,r=o*435+n*256,s=d+(l>>>16),i=h+(s>>>16),u=r+(i>>>16);e=l&65535,n=s&65535,a=i&65535,o=u&65535}return p(o)+p(a)+p(n)+p(e)},y=100,v=100,$="lunora:spawn:",O="lunora:await:",k="lunora:signal:",C="lunora:compensate:",F=t=>{const e=`${t}-compensate`;return e.length<=v?e:`${t.slice(0,v-28)}-${N(t)}-compensate`},b=`${A}branch:`,Y=(t,e,n)=>({compensateWith:n?.compensateWith,id:n?.id,params:e,timeout:n?.timeout,workflow:t}),S=t=>t instanceof Error?{message:t.message,name:t.name}:{message:String(t),name:"Error"},W=t=>({status:"ok",value:t}),z=t=>({error:S(t),status:"error"}),I=1048576,M=t=>{try{return String(t instanceof Error?t.message:t).slice(0,200)}catch{return"the failure itself could not be described"}},T=t=>{let e;try{e=new TextEncoder().encode(JSON.stringify(t)).length}catch(n){return{error:{message:`branch outcome cannot be serialised to JSON (${M(n)}) — the parent can never receive it. Return a plain JSON value (no cycles, no BigInt, no class instance that fails to serialise) or a reference the parent can dereference (an R2 key, a row id)`,name:"BranchOutputUnserializable"},status:"error"}}return e<=I?t:{error:{message:`branch outcome serialises to ${String(e)} bytes, over Cloudflare's ${String(I)}-byte event payload limit — the parent can never receive it. Return a reference the parent can dereference (an R2 key, a row id) instead of the payload itself`,name:"BranchOutputTooLarge"},status:"error"}},w=async(t,e)=>{try{return{attached:!1,instance:await t.create(e)}}catch(n){if(!P(n))throw n;return{attached:!0,instance:await t.get(e.id)}}},X=async t=>{const{error:e,output:n,status:a}=await t.status();if(a==="complete")return W(n);if(a==="errored"||a==="terminated")return{error:e??{message:`branch instance ${a}`,name:"Error"},status:"error"}},j=t=>{if(typeof t!="object"||t===null)return;const{status:e}=t;return e==="error"||e==="ok"?t:void 0};class m extends Error{branchError;kind;plan;constructor(e,n,a){super(n.message),this.branchError=n,this.kind=a,this.plan=e}}const H=async(t,e,n)=>{for(let a=e.length-1;a>=0;a-=1){const o=e[a],c=o?.plan.item.compensateWith;if(!(o===void 0||c===void 0))try{const l=t.resolveBinding(c);await t.step.do(`${C}${o.plan.childId}`,async()=>{const d=F(o.plan.childId),h={branch:o.plan.item.workflow,error:n,index:o.plan.index,output:o.output};return await w(l,{id:d,params:h}),d})}catch(l){t.log?.error(`ctx.parallel: group-saga compensation "${c}" for branch "${o.plan.item.workflow}" (#${String(o.plan.index)}) failed`,l)}}},G=t=>async n=>{if(n.length===0)return[];if(n.length>y)throw new f(`ctx.parallel: ${String(n.length)} branches exceeds the cap of ${String(y)} — split the fan-out or raise the work into fewer child workflows`);const a=n.map((r,s)=>{const i=t.nextChildId(r.id);return{childId:i,eventType:`${b}${i}`,index:s,item:r}}),o=new Set;for(const r of a){if(o.has(r.childId))throw new f(`ctx.parallel: duplicate branch id "${r.childId}" — each branch in a group must resolve to a unique child instance id (check explicit \`id\` options)`);o.add(r.childId)}const c=await Promise.all(a.map(r=>t.step.do(`${$}${r.childId}`,async()=>{const s=t.resolveBinding(r.item.workflow),i={eventType:r.eventType,index:r.index,parentBinding:t.parentBinding,parentId:t.instanceId},{attached:u,instance:x}=await w(s,{id:r.childId,params:{...r.item.params,[g]:i}});if(!u)return;const E=await X(x);return E===void 0?void 0:T(E)}))),l=Array.from({length:a.length}),d=Array.from({length:a.length}),h=async(r,s)=>{let i;if(s===void 0)try{i=(await t.step.waitForEvent(`${O}${r.childId}`,{timeout:r.item.timeout,type:r.eventType})).payload}catch(u){throw new m(r,S(u),"join failed")}else i=s;if(i.status==="error")throw new m(r,i.error,"failed");d[r.index]={output:i.value,plan:r},l[r.index]=i.value};try{await Promise.all(a.map((r,s)=>h(r,j(c[s]))))}catch(r){throw r instanceof m?(await H(t,d.filter(s=>s!==void 0),r.branchError),new f(`ctx.parallel: branch "${r.plan.item.workflow}" (#${String(r.plan.index)}) ${r.kind}: ${r.branchError.message}`)):r}return l},U=t=>async(e,n,a)=>{if(_(n))throw new B("BAD_REQUEST",`@lunora/workflow: params ${R}`);const o=t.nextChildId(a?.id);return await t.step.do(`${$}${o}`,async()=>{const c=t.resolveBinding(e);return await w(c,{id:o,params:n}),o}),t.resolveBinding(e).get(o)},q=t=>{if(typeof t!="object"||t===null)return;const e=t[g];if(typeof e!="object"||e===null)return;const n=e;if(!(typeof n.eventType!="string"||typeof n.parentBinding!="string"||typeof n.parentId!="string"||typeof n.index!="number")&&!(!n.parentBinding.startsWith("WORKFLOW_")||!n.eventType.startsWith(b)))return{eventType:n.eventType,index:n.index,parentBinding:n.parentBinding,parentId:n.parentId}},Q=t=>{if(typeof t!="object"||t===null)return t;const e={...t};return Reflect.deleteProperty(e,g),e},L=async(t,e,n)=>{const a=t.env[e.parentBinding];if(!a||typeof a.get!="function")return;const o=a.get.bind(a),c=T(n);await t.step.do(`${k}${String(e.index)}`,async()=>(await(await o(e.parentId)).sendEvent({payload:c,type:e.eventType}),e.eventType))},Z=async(t,e,n)=>{try{await L(t,e,n)}catch(a){t.log?.error(`@lunora/workflow: failed to signal branch parent "${e.parentId}" (event "${e.eventType}")`,a)}};export{y as MAX_BRANCHES,Y as branch,G as createParallel,U as createSpawn,z as errorOutcome,q as extractBranchMarker,W as okOutcome,L as signalBranchParent,Z as signalBranchParentSafe,Q as stripBranchMarker};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const o="__lunoraNonRetryable";class a extends Error{constructor(s,n="NonRetryableError"){super(s),this.name=n,this[o]=!0}}const c=t=>t instanceof Error&&t[o]===!0,i=(t,s)=>{const n=new s(t.message,t.name);return t.stack!==void 0&&(n.stack=t.stack),t.cause!==void 0&&n.cause===void 0&&(n.cause=t.cause),n},r=(t,s)=>{throw s!==void 0&&c(t)?i(t,s):t},E=(t,s,n)=>{const e=new a(t);return s!==void 0&&(e.cause=s),r(e,n)},u=/already[\s_-]?exists/iu,N=t=>u.test(t instanceof Error?t.message:String(t));export{a as NonRetryableError,r as convertNonRetryableError,N as isDuplicateInstanceError,c as isNonRetryableError,E as raiseNonRetryable,i as toNativeNonRetryableError};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{c as p,v as o}from"./run-step-CAUCn1UO.mjs";import"./defineWorkflowEvent-suERPEEV.mjs";import"./NonRetryableError-BV6ELTK1.mjs";export{p as createRunStep,o as validateStepArgs};
|