@lunora/scheduler 1.0.0-alpha.152 → 1.0.0-alpha.154
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.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{default as o}from"./packem_shared/createScheduler-D7F48b45.mjs";import{default as a}from"./packem_shared/createWorkpool-CRgaqKBp.mjs";import{createCronTrigger as f}from"./packem_shared/createCronTrigger-cg32QNgj.mjs";import{CRON_SCHEDULE_KINDS as p,compileCronSchedule as c,cronJobs as d}from"./packem_shared/CRON_SCHEDULE_KINDS-v0tEYoxJ.mjs";import{createQueueConsumer as m,createQueueWorkpool as n,httpDispatcher as x}from"./packem_shared/createQueueConsumer-
|
|
1
|
+
import{default as o}from"./packem_shared/createScheduler-D7F48b45.mjs";import{default as a}from"./packem_shared/createWorkpool-CRgaqKBp.mjs";import{createCronTrigger as f}from"./packem_shared/createCronTrigger-cg32QNgj.mjs";import{CRON_SCHEDULE_KINDS as p,compileCronSchedule as c,cronJobs as d}from"./packem_shared/CRON_SCHEDULE_KINDS-v0tEYoxJ.mjs";import{createQueueConsumer as m,createQueueWorkpool as n,httpDispatcher as x}from"./packem_shared/createQueueConsumer-DbqTkqqq.mjs";import{r as i}from"./packem_shared/resolve-schedule-id-B2ghxgFm.mjs";import{MAX_RETRY_ATTEMPTS as E,RETRY_BASE_DELAY_MS as C,SchedulerDO as _}from"./packem_shared/MAX_RETRY_ATTEMPTS-UyUIuyeb.mjs";import{createSchedulerHost as R}from"./packem_shared/createSchedulerHost-DvY88elW.mjs";import{isWorkflowReference as A}from"./packem_shared/isWorkflowReference-CT3tdefh.mjs";import{assertValidCronExpression as g,isValidCronExpression as k,warnIfSecondsLeading as L}from"./packem_shared/assertValidCronExpression-fiNfNiWf.mjs";import{default as W}from"./packem_shared/assertScheduleDelay-XVk4jfO1.mjs";import{default as w}from"./packem_shared/assertScheduleInstant-DewOrnLr.mjs";export{p as CRON_SCHEDULE_KINDS,E as MAX_RETRY_ATTEMPTS,C as RETRY_BASE_DELAY_MS,_ as SchedulerDO,W as assertScheduleDelay,w as assertScheduleInstant,g as assertValidCronExpression,c as compileCronSchedule,f as createCronTrigger,m as createQueueConsumer,n as createQueueWorkpool,o as createScheduler,R as createSchedulerHost,a as createWorkpool,d as cronJobs,x as httpDispatcher,k as isValidCronExpression,A as isWorkflowReference,i as resolveScheduleId,L as warnIfSecondsLeading};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{LunoraError as i,isLunoraError as P}from"@lunora/errors";import{decodeWire as K,encodeWire as D}from"@lunora/wire";const J=9e5,C="DISPATCH_IN_PROGRESS",k="x-lunora-dispatch-declined",B=(e,t,n)=>{if(t===void 0)return{dispose:()=>{},signal:e};const a=new AbortController,o=setTimeout(()=>{a.abort(n())},t);return{dispose:()=>{clearTimeout(o)},signal:a.signal}},l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",I=e=>{let t="",n=0;const a=e.length-2;for(;n<a;n+=3){const r=e[n]<<16|e[n+1]<<8|e[n+2];t+=l.charAt(r>>18&63)+l.charAt(r>>12&63)+l.charAt(r>>6&63)+l.charAt(r&63)}const o=e.length-n;if(o===1){const r=e[n]<<16;t+=l.charAt(r>>18&63)+l.charAt(r>>12&63)}else if(o===2){const r=e[n]<<16|e[n+1]<<8;t+=l.charAt(r>>18&63)+l.charAt(r>>12&63)+l.charAt(r>>6&63)}return t};new TextDecoder;const _=new TextEncoder,$="=",Q=e=>{for(let t=0;t<e.length;t+=1)if(e.charCodeAt(t)>255)return!1;return!0},H=e=>I(_.encode(e)),W=e=>H(JSON.stringify(e)),j=e=>!e.startsWith($)&&Q(e)?e:`${$}${I(_.encode(e))}`,F="/_lunora/scheduler/dispatch",G=3e4,X=e=>{let t=e.length;for(;t>0&&e[t-1]==="/";)t-=1;return e.slice(0,t)},E=Symbol("lunoraDispatchFailure"),q=Symbol("lunoraDispatchDecline"),z=Symbol("lunoraDispatchMessageId"),v=(e,t)=>(t!==void 0&&Object.defineProperty(e,z,{value:t}),e),V=(e,t)=>(Object.defineProperty(e,E,{value:!0}),v(e,t)),Y=(e,t,n,a)=>{const{status:o}=t;try{const r=JSON.parse(n)?.error;if(typeof r=="object"&&r!==null&&typeof r.code=="string"){const{code:s,data:c,message:h}=r,f=V(new i(s,typeof h=="string"?h:void 0,{data:c,status:o}),a);return t.headers.get(k)==="1"&&Object.defineProperty(f,q,{value:!0}),f}}catch{}return v(new i("INTERNAL",`${e}: function dispatch failed (${String(o)}): ${n}`,{status:o}),a)},Z=e=>P(e)&&e[E]===!0,ee=e=>Z(e)&&e[q]===!0,R=J/1e3,te=3,ne=async(e,t)=>{const n=t.maxRetries??te,a=typeof e.attempts=="number"&&e.attempts>n;let o="";if(a&&t.requeue!==void 0)try{return await t.requeue(R),e.ack(),"requeued"}catch(r){o=` Re-enqueueing a delayed copy failed (${r instanceof Error?r.message:String(r)}).`}return e.retry({delaySeconds:R}),a&&console.error(`${t.where}: message ${e.id} was declined (${C}) on its last delivery (attempt ${String(e.attempts)} of ${String(n+1)}) — its earlier delivery is still running the call.${o} It is retried anyway, but its retries are exhausted, so it will be dead-lettered if the queue has a deadLetterQueue and dropped if not. Raise max_retries so a slow call cannot exhaust it.`),"retried"},re=(e,t,n)=>new i("INTERNAL",`${e}: function dispatch to "${t}" timed out after ${String(n)}ms`,{status:503}),ae=e=>{const{label:t}=e,n=globalThis.fetch,a=e.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(o,r,s={})=>{if(typeof a!="function")throw new TypeError(`${t}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const c=()=>r??{},h=e.env.LUNORA_ORIGIN_URL;if(typeof h!="string"||h.length===0)throw new i("INTERNAL",`${t}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const f=e.env.LUNORA_ADMIN_TOKEN;if(typeof f!="string"||f.length===0)throw new i("INTERNAL",`${t}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const m=`${X(h)}${F}`,g={authorization:`Bearer ${f}`,"content-type":"application/json"};e.traceparent!==void 0&&e.traceparent.length>0&&(g.traceparent=e.traceparent),e.identity?.userId!==void 0&&(g["x-lunora-userid"]=j(e.identity.userId)),e.identity?.claims!==void 0&&(g["x-lunora-identity"]=W(e.identity.claims));const w=s.timeoutMs??G,N=B(void 0,w,()=>new DOMException(`dispatch timed out after ${String(w)}ms`,"TimeoutError")),A=d=>{throw d instanceof Error&&d.name==="TimeoutError"?re(t,o.__lunoraRef,w):d};let u;try{try{u=await a(m,{body:JSON.stringify({args:c(),functionPath:o.__lunoraRef,id:s.dedupId,shardKey:s.shardKey}),headers:g,method:"POST",signal:N.signal})}catch(p){return A(p)}if(!u.ok){let p;try{p=await u.text()}catch(M){return A(M)}throw Y(t,u,p,s.messageId)}let d;try{d=await u.text()}catch(p){return A(p)}if(d.length===0)return;let y;try{y=JSON.parse(d)}catch{throw new i("INTERNAL",`${t}: function dispatch returned a non-JSON body (${String(u.status)}): ${d}`,{status:u.status})}if(y===null||typeof y!="object"||Array.isArray(y)||!("result"in y))throw new i("INTERNAL",`${t}: function dispatch returned a JSON body that is not a { result } envelope (${String(u.status)}): ${d}`,{status:u.status});return K(y.result)}finally{N.dispose()}}},oe="lunora.requeue.v1",O=new TextEncoder,T=async(e,t)=>crypto.subtle.importKey("raw",O.encode(e),{hash:"SHA-256",name:"HMAC"},!1,[t]),L=(e,t,n)=>O.encode(`${oe}
|
|
2
|
+
${e}
|
|
3
|
+
${t}
|
|
4
|
+
${n}`),se=e=>[...new Uint8Array(e)].map(t=>t.toString(16).padStart(2,"0")).join(""),ce=/^[\da-f]{64}$/u,ie=async(e,t,n,a)=>se(await crypto.subtle.sign("HMAC",await T(e,"sign"),L(t,n,a))),ue=async(e,t,n,a,o)=>{if(typeof o!="string"||!ce.test(o))return!1;const r=Uint8Array.from({length:o.length/2},(s,c)=>Number.parseInt(o.slice(c*2,c*2+2),16));return crypto.subtle.verify("HMAC",await T(e,"verify"),r,L(t,n,a))},S=100,de=3e5,b=e=>e===void 0?void 0:D(e),pe=e=>{if(!e.queue)throw new i("INTERNAL","@lunora/scheduler: `queue` (a Cloudflare Queue binding) is required");return{enqueue:async(a,o,r={})=>{const s={args:b(o),functionPath:a.__lunoraRef,shardKey:r.shardKey},c=r.delaySeconds===void 0?void 0:{delaySeconds:r.delaySeconds};await e.queue.send(s,c)},enqueueBatch:async(a,o)=>{if(a.length>S)throw new i("VALIDATION_ERROR",`@lunora/scheduler: enqueueBatch exceeds ${String(S)} (got ${String(a.length)}) — split across calls`);const r=a.map(s=>({body:{args:b(s.args),functionPath:s.ref.__lunoraRef,shardKey:s.shardKey}}));await e.queue.sendBatch(r,o)}}},le=e=>typeof e=="object"&&e!==null&&typeof e.functionPath=="string",U="scheduler",x=e=>JSON.stringify({args:e.args,functionPath:e.functionPath,shardKey:e.shardKey}),he=async(e,t,n)=>{const{requeuedFrom:a,requeueMac:o,...r}=e;return typeof a=="string"&&n!==void 0&&await ue(n,U,a,x(r),o).catch(()=>!1)?{id:a,job:r,requeued:!0}:{id:t,job:r,requeued:!1}},ge=e=>async t=>{await Promise.all(t.messages.map(async n=>{let a;try{if(!le(n.body))throw new i("INTERNAL","@lunora/scheduler: queue message body is not a QueueJob (missing functionPath)");a=await he(n.body,n.id,e.requeue?.secret),await e.dispatch(a.job,a.id),n.ack()}catch(o){if(ee(o)&&a!==void 0){const{requeue:r}=e,{id:s,job:c,requeued:h}=a;await ne(n,{maxRetries:e.maxRetries,requeue:r===void 0||h?void 0:async f=>{const m=await ie(r.secret,U,s,x(c));await r.queue.send({...c,requeuedFrom:s,requeueMac:m},{delaySeconds:f})},where:`@lunora/scheduler: queue "${t.queue}"`});return}n.retry()}}))},me=e=>{const t=ae({env:{LUNORA_ADMIN_TOKEN:e.adminToken,LUNORA_ORIGIN_URL:e.originUrl},fetchImpl:e.fetchImpl,label:"@lunora/scheduler"}),n=e.timeoutMs??de;return async(a,o)=>{await t({__lunoraRef:a.functionPath},a.args,{dedupId:o,messageId:o,shardKey:a.shardKey,timeoutMs:n})}};export{ge as createQueueConsumer,pe as createQueueWorkpool,me as httpDispatcher};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/scheduler",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.154",
|
|
4
4
|
"description": "Scheduling for Lunora: runAfter / runAt and Cron Triggers via SchedulerDO",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@lunora/errors": ">=1.0.0-alpha.51 <2.0.0-0",
|
|
54
54
|
"@lunora/platform": "1.0.0-alpha.75",
|
|
55
|
-
"@lunora/wire": "1.0.0-alpha.
|
|
55
|
+
"@lunora/wire": "1.0.0-alpha.3",
|
|
56
56
|
"@visulima/cron-parser": "2.0.0"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import{LunoraError as c,isLunoraError as P}from"@lunora/errors";import{decodeWire as D,encodeWire as K}from"@lunora/wire";const J=9e5,C="DISPATCH_IN_PROGRESS",k="x-lunora-dispatch-declined",Q=(e,t,n)=>{if(t===void 0)return{dispose:()=>{},signal:e};const a=new AbortController,o=setTimeout(()=>{a.abort(n())},t);return{dispose:()=>{clearTimeout(o)},signal:a.signal}},l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",I=e=>{let t="",n=0;const a=e.length-2;for(;n<a;n+=3){const r=e[n]<<16|e[n+1]<<8|e[n+2];t+=l.charAt(r>>18&63)+l.charAt(r>>12&63)+l.charAt(r>>6&63)+l.charAt(r&63)}const o=e.length-n;if(o===1){const r=e[n]<<16;t+=l.charAt(r>>18&63)+l.charAt(r>>12&63)}else if(o===2){const r=e[n]<<16|e[n+1]<<8;t+=l.charAt(r>>18&63)+l.charAt(r>>12&63)+l.charAt(r>>6&63)}return t};new TextDecoder;const _=new TextEncoder,$="=",B=e=>{for(let t=0;t<e.length;t+=1)if(e.charCodeAt(t)>255)return!1;return!0},H=e=>I(_.encode(JSON.stringify(e))),W=e=>!e.startsWith($)&&B(e)?e:`${$}${I(_.encode(e))}`,j="/_lunora/scheduler/dispatch",F=3e4,G=e=>{let t=e.length;for(;t>0&&e[t-1]==="/";)t-=1;return e.slice(0,t)},E=Symbol("lunoraDispatchFailure"),q=Symbol("lunoraDispatchDecline"),X=Symbol("lunoraDispatchMessageId"),v=(e,t)=>(t!==void 0&&Object.defineProperty(e,X,{value:t}),e),z=(e,t)=>(Object.defineProperty(e,E,{value:!0}),v(e,t)),V=(e,t,n,a)=>{const{status:o}=t;try{const r=JSON.parse(n)?.error;if(typeof r=="object"&&r!==null&&typeof r.code=="string"){const{code:s,data:i,message:h}=r,f=z(new c(s,typeof h=="string"?h:void 0,{data:i,status:o}),a);return t.headers.get(k)==="1"&&Object.defineProperty(f,q,{value:!0}),f}}catch{}return v(new c("INTERNAL",`${e}: function dispatch failed (${String(o)}): ${n}`,{status:o}),a)},Y=e=>P(e)&&e[E]===!0,Z=e=>Y(e)&&e[q]===!0,R=J/1e3,ee=3,te=async(e,t)=>{const n=t.maxRetries??ee,a=typeof e.attempts=="number"&&e.attempts>n;let o="";if(a&&t.requeue!==void 0)try{return await t.requeue(R),e.ack(),"requeued"}catch(r){o=` Re-enqueueing a delayed copy failed (${r instanceof Error?r.message:String(r)}).`}return e.retry({delaySeconds:R}),a&&console.error(`${t.where}: message ${e.id} was declined (${C}) on its last delivery (attempt ${String(e.attempts)} of ${String(n+1)}) — its earlier delivery is still running the call.${o} It is retried anyway, but its retries are exhausted, so it will be dead-lettered if the queue has a deadLetterQueue and dropped if not. Raise max_retries so a slow call cannot exhaust it.`),"retried"},ne=(e,t,n)=>new c("INTERNAL",`${e}: function dispatch to "${t}" timed out after ${String(n)}ms`,{status:503}),re=e=>{const{label:t}=e,n=globalThis.fetch,a=e.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(o,r,s={})=>{if(typeof a!="function")throw new TypeError(`${t}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const i=()=>r??{},h=e.env.LUNORA_ORIGIN_URL;if(typeof h!="string"||h.length===0)throw new c("INTERNAL",`${t}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const f=e.env.LUNORA_ADMIN_TOKEN;if(typeof f!="string"||f.length===0)throw new c("INTERNAL",`${t}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const m=`${G(h)}${j}`,g={authorization:`Bearer ${f}`,"content-type":"application/json"};e.traceparent!==void 0&&e.traceparent.length>0&&(g.traceparent=e.traceparent),e.identity?.userId!==void 0&&(g["x-lunora-userid"]=W(e.identity.userId)),e.identity?.claims!==void 0&&(g["x-lunora-identity"]=H(e.identity.claims));const w=s.timeoutMs??F,N=Q(void 0,w,()=>new DOMException(`dispatch timed out after ${String(w)}ms`,"TimeoutError")),A=d=>{throw d instanceof Error&&d.name==="TimeoutError"?ne(t,o.__lunoraRef,w):d};let u;try{try{u=await a(m,{body:JSON.stringify({args:i(),functionPath:o.__lunoraRef,id:s.dedupId,shardKey:s.shardKey}),headers:g,method:"POST",signal:N.signal})}catch(p){return A(p)}if(!u.ok){let p;try{p=await u.text()}catch(M){return A(M)}throw V(t,u,p,s.messageId)}let d;try{d=await u.text()}catch(p){return A(p)}if(d.length===0)return;let y;try{y=JSON.parse(d)}catch{throw new c("INTERNAL",`${t}: function dispatch returned a non-JSON body (${String(u.status)}): ${d}`,{status:u.status})}if(y===null||typeof y!="object"||Array.isArray(y)||!("result"in y))throw new c("INTERNAL",`${t}: function dispatch returned a JSON body that is not a { result } envelope (${String(u.status)}): ${d}`,{status:u.status});return D(y.result)}finally{N.dispose()}}},ae="lunora.requeue.v1",O=new TextEncoder,T=async(e,t)=>crypto.subtle.importKey("raw",O.encode(e),{hash:"SHA-256",name:"HMAC"},!1,[t]),L=(e,t,n)=>O.encode(`${ae}
|
|
2
|
-
${e}
|
|
3
|
-
${t}
|
|
4
|
-
${n}`),oe=e=>[...new Uint8Array(e)].map(t=>t.toString(16).padStart(2,"0")).join(""),se=/^[\da-f]{64}$/u,ie=async(e,t,n,a)=>oe(await crypto.subtle.sign("HMAC",await T(e,"sign"),L(t,n,a))),ce=async(e,t,n,a,o)=>{if(typeof o!="string"||!se.test(o))return!1;const r=Uint8Array.from({length:o.length/2},(s,i)=>Number.parseInt(o.slice(i*2,i*2+2),16));return crypto.subtle.verify("HMAC",await T(e,"verify"),r,L(t,n,a))},S=100,ue=3e5,b=e=>e===void 0?void 0:K(e),ye=e=>{if(!e.queue)throw new c("INTERNAL","@lunora/scheduler: `queue` (a Cloudflare Queue binding) is required");return{enqueue:async(a,o,r={})=>{const s={args:b(o),functionPath:a.__lunoraRef,shardKey:r.shardKey},i=r.delaySeconds===void 0?void 0:{delaySeconds:r.delaySeconds};await e.queue.send(s,i)},enqueueBatch:async(a,o)=>{if(a.length>S)throw new c("VALIDATION_ERROR",`@lunora/scheduler: enqueueBatch exceeds ${String(S)} (got ${String(a.length)}) — split across calls`);const r=a.map(s=>({body:{args:b(s.args),functionPath:s.ref.__lunoraRef,shardKey:s.shardKey}}));await e.queue.sendBatch(r,o)}}},de=e=>typeof e=="object"&&e!==null&&typeof e.functionPath=="string",U="scheduler",x=e=>JSON.stringify({args:e.args,functionPath:e.functionPath,shardKey:e.shardKey}),le=async(e,t,n)=>{const{requeuedFrom:a,requeueMac:o,...r}=e;return typeof a=="string"&&n!==void 0&&await ce(n,U,a,x(r),o).catch(()=>!1)?{id:a,job:r,requeued:!0}:{id:t,job:r,requeued:!1}},pe=e=>async t=>{await Promise.all(t.messages.map(async n=>{let a;try{if(!de(n.body))throw new c("INTERNAL","@lunora/scheduler: queue message body is not a QueueJob (missing functionPath)");a=await le(n.body,n.id,e.requeue?.secret),await e.dispatch(a.job,a.id),n.ack()}catch(o){if(Z(o)&&a!==void 0){const{requeue:r}=e,{id:s,job:i,requeued:h}=a;await te(n,{maxRetries:e.maxRetries,requeue:r===void 0||h?void 0:async f=>{const m=await ie(r.secret,U,s,x(i));await r.queue.send({...i,requeuedFrom:s,requeueMac:m},{delaySeconds:f})},where:`@lunora/scheduler: queue "${t.queue}"`});return}n.retry()}}))},ge=e=>{const t=re({env:{LUNORA_ADMIN_TOKEN:e.adminToken,LUNORA_ORIGIN_URL:e.originUrl},fetchImpl:e.fetchImpl,label:"@lunora/scheduler"}),n=e.timeoutMs??ue;return async(a,o)=>{await t({__lunoraRef:a.functionPath},a.args,{dedupId:o,messageId:o,shardKey:a.shardKey,timeoutMs:n})}};export{pe as createQueueConsumer,ye as createQueueWorkpool,ge as httpDispatcher};
|