@lunora/queue 1.0.0-alpha.12 → 1.0.0-alpha.14

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{createQueueCaptureSink as t,shouldCaptureQueue as r}from"./packem_shared/createQueueCaptureSink-saac8Ywl.mjs";import{createQueueContext as a}from"./packem_shared/createQueueContext-BuXG9VQI.mjs";import{default as i}from"./packem_shared/createQueues-BZBGfWI4.mjs";import{defineQueue as p,isQueueDefinition as m,queueBindingName as x,queueDefaultName as Q}from"./packem_shared/defineQueue-DJYVNfQY.mjs";import{dispatchQueueBatch as d}from"./packem_shared/dispatchQueueBatch-GZMtLZNW.mjs";import{createQueueRunContext as C}from"./packem_shared/createQueueRunContext-B-TpEaZa.mjs";export{t as createQueueCaptureSink,a as createQueueContext,C as createQueueRunContext,i as createQueues,p as defineQueue,d as dispatchQueueBatch,m as isQueueDefinition,x as queueBindingName,Q as queueDefaultName,r as shouldCaptureQueue};
1
+ import{createQueueCaptureSink as t,shouldCaptureQueue as r}from"./packem_shared/createQueueCaptureSink-saac8Ywl.mjs";import{createQueueContext as a}from"./packem_shared/createQueueContext-BuXG9VQI.mjs";import{default as i}from"./packem_shared/createQueues-BZBGfWI4.mjs";import{defineQueue as p,isQueueDefinition as m,queueBindingName as x,queueDefaultName as Q}from"./packem_shared/defineQueue-DJYVNfQY.mjs";import{dispatchQueueBatch as d}from"./packem_shared/dispatchQueueBatch-BYb3OFCX.mjs";import{createQueueRunContext as C}from"./packem_shared/createQueueRunContext-Dg_fCCaC.mjs";export{t as createQueueCaptureSink,a as createQueueContext,C as createQueueRunContext,i as createQueues,p as defineQueue,d as dispatchQueueBatch,m as isQueueDefinition,x as queueBindingName,Q as queueDefaultName,r as shouldCaptureQueue};
@@ -0,0 +1 @@
1
+ import{LunoraError as a}from"@lunora/errors";const g=e=>({debug:(t,...n)=>{console.debug(e,t,...n)},error:(t,...n)=>{console.error(e,t,...n)},info:(t,...n)=>{console.info(e,t,...n)},warn:(t,...n)=>{console.warn(e,t,...n)}}),w=e=>{let t="";for(let n=0;n<e.length;n+=32768)t+=String.fromCharCode(...e.subarray(n,n+32768));return btoa(t)},p=e=>w(e).replaceAll("+","-").replaceAll("/","_").replace(/=+$/,"");new TextDecoder;const y=new TextEncoder,d="=",b=e=>{for(let t=0;t<e.length;t+=1)if(e.charCodeAt(t)>255)return!1;return!0},m=e=>p(y.encode(JSON.stringify(e))),I=e=>!e.startsWith(d)&&b(e)?e:`${d}${p(y.encode(e))}`,R="/_lunora/scheduler/dispatch",A=e=>{let t=e.length;for(;t>0&&e[t-1]==="/";)t-=1;return e.slice(0,t)},O=(e,t,n)=>{try{const o=JSON.parse(n)?.error;if(typeof o=="object"&&o!==null&&typeof o.code=="string"){const{code:i,data:c,message:s}=o;return new a(i,typeof s=="string"?s:void 0,{data:c,status:t})}}catch{}return new a("INTERNAL",`${e}: function dispatch failed (${String(t)}): ${n}`,{status:t})},$=e=>{const{label:t}=e,n=globalThis.fetch,o=e.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(i,c,s={})=>{if(typeof o!="function")throw new TypeError(`${t}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const l=e.env.LUNORA_ORIGIN_URL;if(typeof l!="string"||l.length===0)throw new a("INTERNAL",`${t}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const u=e.env.LUNORA_ADMIN_TOKEN;if(typeof u!="string"||u.length===0)throw new a("INTERNAL",`${t}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const N=`${A(l)}${R}`,f={authorization:`Bearer ${u}`,"content-type":"application/json"};e.identity?.userId!==void 0&&(f["x-lunora-userid"]=I(e.identity.userId)),e.identity?.claims!==void 0&&(f["x-lunora-identity"]=m(e.identity.claims));const r=await o(N,{body:JSON.stringify({args:c??{},functionPath:i.__lunoraRef,shardKey:s.shardKey}),headers:f,method:"POST"});if(!r.ok)throw O(t,r.status,await r.text());const h=await r.text();if(h.length!==0)try{return JSON.parse(h)}catch{throw new a("INTERNAL",`${t}: function dispatch returned a non-JSON body (${String(r.status)}): ${h}`,{status:r.status})}}},L=e=>({env:e.env,log:g(`[queue:${e.exportName}]`),run:$({env:e.env,fetchImpl:e.fetchImpl,label:"@lunora/queue"})});export{L as createQueueRunContext};
@@ -0,0 +1 @@
1
+ import{LunoraError as l}from"@lunora/errors";import{createQueueRunContext as f}from"./createQueueRunContext-Dg_fCCaC.mjs";const m=3,d=e=>{if(e instanceof Date)return e.getTime();const t=typeof e=="number"?e:Number(e);return Number.isFinite(t)?t:0},h=(e,t)=>new Proxy(e,{get:(r,a)=>a==="ack"?()=>{t.set(r,"ack"),r.ack()}:a==="retry"?o=>{t.set(r,"retry"),r.retry(o)}:Reflect.get(r,a,r)}),y=(e,t,r)=>new Proxy(e,{get:(a,o)=>o==="ackAll"?()=>{r("ack"),a.ackAll()}:o==="retryAll"?i=>{r("retry"),a.retryAll(i)}:o==="messages"?t:Reflect.get(a,o,a)}),g=e=>{const t=new Map,r=e.messages,a=r.map(i=>h(i,t)),o=y(e,a,i=>{for(const s of r)t.has(s)||t.set(s,i)});return{dispositions:t,originals:r,wrappedBatch:o}},w=e=>{if(e instanceof Error)return e.message;if(typeof e=="string")return e;if(e!==null&&typeof e=="object")try{return JSON.stringify(e)}catch{return"[unserializable thrown value]"}return String(e)},b=(e,t,r,a,o)=>{const i=a?w(o):void 0,s=typeof t.definition.maxRetries=="number"?t.definition.maxRetries:m;return e.originals.map(u=>{const c=e.dispositions.get(u)??(a?"error":"ack"),n=typeof u.attempts=="number"?u.attempts:1;return{attempts:n,body:u.body,deadLettered:c!=="ack"&&n>s,error:c==="error"?i:void 0,exportName:t.exportName,messageId:u.id,outcome:c,queue:r,timestamp:d(u.timestamp)}})},x=async(e,t,r)=>{const a=Object.hasOwn(t,e.queue)?t[e.queue]:void 0;if(a===void 0){const n=Object.keys(t),p=n.length===0?"no push queues are declared":`known push queues: ${n.join(", ")}`;throw new l("INTERNAL",`@lunora/queue: received a batch for queue "${e.queue}" but no push handler is registered (${p})`)}const{handler:o}=a.definition;if(typeof o!="function")throw new TypeError(`@lunora/queue: queue "${e.queue}" (${a.exportName}) has no push handler — it is declared as a pull consumer`);const i=f({env:r.env,exportName:a.exportName,fetchImpl:r.fetchImpl});if(r.capture===void 0){await o(i,e);return}const s=g(e);let u=!1,c;try{await o(i,s.wrappedBatch)}catch(n){u=!0,c=n}try{const n=b(s,a,e.queue,u,c);await r.capture(n)}catch(n){console.warn("@lunora/queue: capture sink failed (delivery unaffected):",n)}if(u)throw c};export{x as dispatchQueueBatch};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/queue",
3
- "version": "1.0.0-alpha.12",
3
+ "version": "1.0.0-alpha.14",
4
4
  "description": "Cloudflare Queues for Lunora: defineQueue producers + consumers, the ctx.queues surface, and the generated queue() worker handler",
5
5
  "keywords": [
6
6
  "background-jobs",
@@ -44,8 +44,8 @@
44
44
  "access": "public"
45
45
  },
46
46
  "dependencies": {
47
- "@lunora/errors": "1.0.0-alpha.9",
48
- "@lunora/platform": "1.0.0-alpha.1"
47
+ "@lunora/errors": "1.0.0-alpha.11",
48
+ "@lunora/platform": "1.0.0-alpha.2"
49
49
  },
50
50
  "engines": {
51
51
  "node": "^22.15.0 || >=24.11.0"
@@ -1 +0,0 @@
1
- import{LunoraError as a}from"@lunora/errors";const p=t=>({debug:(e,...n)=>{console.debug(t,e,...n)},error:(e,...n)=>{console.error(t,e,...n)},info:(e,...n)=>{console.info(t,e,...n)},warn:(e,...n)=>{console.warn(t,e,...n)}}),N="/_lunora/scheduler/dispatch",y=t=>{let e=t.length;for(;e>0&&t[e-1]==="/";)e-=1;return t.slice(0,e)},g=(t,e,n)=>{try{const o=JSON.parse(n)?.error;if(typeof o=="object"&&o!==null&&typeof o.code=="string"){const{code:s,data:c,message:i}=o;return new a(s,typeof i=="string"?i:void 0,{data:c,status:e})}}catch{}return new a("INTERNAL",`${t}: function dispatch failed (${String(e)}): ${n}`,{status:e})},w=t=>{const{label:e}=t,n=globalThis.fetch,o=t.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(s,c,i={})=>{if(typeof o!="function")throw new TypeError(`${e}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const u=t.env.LUNORA_ORIGIN_URL;if(typeof u!="string"||u.length===0)throw new a("INTERNAL",`${e}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const l=t.env.LUNORA_ADMIN_TOKEN;if(typeof l!="string"||l.length===0)throw new a("INTERNAL",`${e}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const d=`${y(u)}${N}`,f={authorization:`Bearer ${l}`,"content-type":"application/json"};t.identity?.userId!==void 0&&(f["x-lunora-userid"]=t.identity.userId),t.identity?.claims!==void 0&&(f["x-lunora-identity"]=JSON.stringify(t.identity.claims));const r=await o(d,{body:JSON.stringify({args:c??{},functionPath:s.__lunoraRef,shardKey:i.shardKey}),headers:f,method:"POST"});if(!r.ok)throw g(e,r.status,await r.text());const h=await r.text();if(h.length!==0)try{return JSON.parse(h)}catch{throw new a("INTERNAL",`${e}: function dispatch returned a non-JSON body (${String(r.status)}): ${h}`,{status:r.status})}}},I=t=>({env:t.env,log:p(`[queue:${t.exportName}]`),run:w({env:t.env,fetchImpl:t.fetchImpl,label:"@lunora/queue"})});export{I as createQueueRunContext};
@@ -1 +0,0 @@
1
- import{LunoraError as m}from"@lunora/errors";import{createQueueRunContext as d}from"./createQueueRunContext-B-TpEaZa.mjs";const l=3,f=e=>{if(e instanceof Date)return e.getTime();const r=typeof e=="number"?e:Number(e);return Number.isFinite(r)?r:0},h=e=>{const r=new Map,o=e.messages,n=o.map(t=>({ack:()=>{r.set(t,"ack"),t.ack()},get attempts(){return t.attempts},get body(){return t.body},get id(){return t.id},retry:a=>{r.set(t,"retry"),t.retry(a)},get timestamp(){return t.timestamp}})),i=t=>{for(const a of o)r.has(a)||r.set(a,t)},c={ackAll:()=>{i("ack"),e.ackAll()},messages:n,queue:e.queue,retryAll:t=>{i("retry"),e.retryAll(t)}};return{dispositions:r,originals:o,wrappedBatch:c}},y=e=>{if(e instanceof Error)return e.message;if(typeof e=="string")return e;if(e!==null&&typeof e=="object")try{return JSON.stringify(e)}catch{return"[unserializable thrown value]"}return String(e)},g=(e,r,o,n,i)=>{const c=n?y(i):void 0,t=typeof r.definition.maxRetries=="number"?r.definition.maxRetries:l;return e.originals.map(a=>{const s=e.dispositions.get(a)??(n?"error":"ack"),u=typeof a.attempts=="number"?a.attempts:1;return{attempts:u,body:a.body,deadLettered:s!=="ack"&&u>t,error:s==="error"?c:void 0,exportName:r.exportName,messageId:a.id,outcome:s,queue:o,timestamp:f(a.timestamp)}})},w=async(e,r,o)=>{const n=Object.hasOwn(r,e.queue)?r[e.queue]:void 0;if(n===void 0){const u=Object.keys(r),p=u.length===0?"no push queues are declared":`known push queues: ${u.join(", ")}`;throw new m("INTERNAL",`@lunora/queue: received a batch for queue "${e.queue}" but no push handler is registered (${p})`)}const{handler:i}=n.definition;if(typeof i!="function")throw new TypeError(`@lunora/queue: queue "${e.queue}" (${n.exportName}) has no push handler — it is declared as a pull consumer`);const c=d({env:o.env,exportName:n.exportName,fetchImpl:o.fetchImpl});if(o.capture===void 0){await i(c,e);return}const t=h(e);let a=!1,s;try{await i(c,t.wrappedBatch)}catch(u){a=!0,s=u}try{const u=g(t,n,e.queue,a,s);await o.capture(u)}catch(u){console.warn("@lunora/queue: capture sink failed (delivery unaffected):",u)}if(a)throw s};export{w as dispatchQueueBatch};