@lunora/queue 1.0.0-alpha.136 → 1.0.0-alpha.138

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.d.mts CHANGED
@@ -356,6 +356,11 @@ interface DispatchOptions {
356
356
  env: Record<string, unknown>;
357
357
  /** Injectable fetch for the `ctx.run` dispatcher (tests). */
358
358
  fetchImpl?: typeof fetch;
359
+ /**
360
+ * Set while the deployment is paused: no handler runs, and every message is
361
+ * held for this many seconds instead. See {@link holdBatch}.
362
+ */
363
+ holdSeconds?: number;
359
364
  /** The runtime's shard-forwarding live publisher, handed to the handler as `ctx.live`. */
360
365
  live?: QueueLivePublisher;
361
366
  /**
package/dist/index.d.ts CHANGED
@@ -356,6 +356,11 @@ interface DispatchOptions {
356
356
  env: Record<string, unknown>;
357
357
  /** Injectable fetch for the `ctx.run` dispatcher (tests). */
358
358
  fetchImpl?: typeof fetch;
359
+ /**
360
+ * Set while the deployment is paused: no handler runs, and every message is
361
+ * held for this many seconds instead. See {@link holdBatch}.
362
+ */
363
+ holdSeconds?: number;
359
364
  /** The runtime's shard-forwarding live publisher, handed to the handler as `ctx.live`. */
360
365
  live?: QueueLivePublisher;
361
366
  /**
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{createQueueCaptureSink as t,shouldCaptureQueue as o}from"./packem_shared/createQueueCaptureSink-CSOPftvj.mjs";import{createQueueContext as i}from"./packem_shared/createQueueContext-D92uMeXw.mjs";import{createQueues as n}from"./packem_shared/createQueues-BJDpU8fF.mjs";import{defineQueue as f,isQueueDefinition as c,queueBindingName as x,queueDefaultName as m}from"./packem_shared/defineQueue-TY4-i3nG.mjs";import{dispatchQueueBatch as d}from"./packem_shared/dispatchQueueBatch-C_3eHm9I.mjs";import{c as C}from"./packem_shared/run-context-C2uF0PXy.mjs";import{createTopicContext as l,defineSubscription as q,defineTopic as B}from"./packem_shared/createTopicContext-BPiAT8uI.mjs";export{t as createQueueCaptureSink,i as createQueueContext,C as createQueueRunContext,n as createQueues,l as createTopicContext,f as defineQueue,q as defineSubscription,B as defineTopic,d as dispatchQueueBatch,c as isQueueDefinition,x as queueBindingName,m as queueDefaultName,o as shouldCaptureQueue};
1
+ import{createQueueCaptureSink as t,shouldCaptureQueue as o}from"./packem_shared/createQueueCaptureSink-CSOPftvj.mjs";import{createQueueContext as i}from"./packem_shared/createQueueContext-D92uMeXw.mjs";import{createQueues as n}from"./packem_shared/createQueues-BJDpU8fF.mjs";import{defineQueue as f,isQueueDefinition as c,queueBindingName as x,queueDefaultName as m}from"./packem_shared/defineQueue-TY4-i3nG.mjs";import{dispatchQueueBatch as d}from"./packem_shared/dispatchQueueBatch-DlMvIQmd.mjs";import{c as C}from"./packem_shared/run-context-C2uF0PXy.mjs";import{createTopicContext as l,defineSubscription as q,defineTopic as B}from"./packem_shared/createTopicContext-BPiAT8uI.mjs";export{t as createQueueCaptureSink,i as createQueueContext,C as createQueueRunContext,n as createQueues,l as createTopicContext,f as defineQueue,q as defineSubscription,B as defineTopic,d as dispatchQueueBatch,c as isQueueDefinition,x as queueBindingName,m as queueDefaultName,o as shouldCaptureQueue};
@@ -0,0 +1 @@
1
+ import{c as $,r as k,e as N,t as h,n as R,X as M}from"./run-context-C2uF0PXy.mjs";import{toErrorBody as A,LunoraError as S}from"@lunora/errors";import{s as b,o as T}from"./requeue-envelope-DqPmW3Ko.mjs";const g=e=>typeof e.definition.maxRetries=="number"?e.definition.maxRetries:M,B=e=>{if(e instanceof Date)return e.getTime();const n=typeof e=="number"?e:Number(e);return Number.isFinite(n)?n:0},x=async(e,n)=>{const t=await T(n,e.body).catch(()=>{});return t===void 0?{body:e.body,id:e.id,requeued:!1}:{...t,requeued:!0}},y=e=>{const n=e.LUNORA_ADMIN_TOKEN;return typeof n=="string"&&n.length>0?n:void 0},I=(e,n)=>{let t=0;return(o,i,r)=>(t+=1,e(o,i,{...r,dedupId:`${n}#${String(t)}`,messageId:n}))},O=(e,n,t,o)=>{const i=I(o,n.id);return new Proxy(e,{get:(r,s)=>s==="id"?n.id:s==="body"?n.body:s==="ack"?()=>{t.set(r,"ack"),r.ack()}:s==="retry"?a=>{t.set(r,"retry"),r.retry(a)}:s==="run"?i:Reflect.get(r,s,r)})},L=(e,n,t)=>new Proxy(e,{get:(o,i)=>i==="ackAll"?()=>{t("ack"),o.ackAll()}:i==="retryAll"?r=>{t("retry"),o.retryAll(r)}:i==="messages"?n:Reflect.get(o,i,o)}),j=async(e,n,t)=>{const o=new Map,i=e.messages,r=new Map,s=[];for(const c of i){const d=await x(c,t);r.set(c,d),s.push(O(c,d,o,n))}const f=L(e,s,c=>{for(const d of i)o.has(d)||o.set(d,c)});return{dispositions:o,originals:i,requeued:new Set,views:r,wrappedBatch:f}},m=(e,n)=>e.views.get(n)??{body:n.body,id:n.id,requeued:!1},v=(e,n)=>m(e,n).id,Q=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)},C=(e,n,t,o,i,r)=>{const s=o?Q(i):void 0,a=g(n),f=o?"error":"ack",c=typeof n.definition.deadLetterQueue=="string"&&n.definition.deadLetterQueue.length>0;return e.originals.map(d=>{const u=d===r,l=e.dispositions.get(d),p=u?"error":l??f,w=typeof d.attempts=="number"?d.attempts:1,q=m(e,d);return{attempts:w,body:q.body,deadLettered:c&&!u&&!e.requeued.has(d)&&p!=="ack"&&w>a,error:p==="error"?s:void 0,exportName:n.exportName,messageId:q.id,outcome:p,queue:t,timestamp:B(d.timestamp)}})},D=(e,n,t)=>{if(!n||!N(t))return;const o=h(t);if(o===void 0)return;const i=e.originals.find(r=>v(e,r)===o);if(!(i===void 0||e.dispositions.has(i)))return i},P=(e,n)=>{n.ack();for(const t of e.originals)t!==n&&!e.dispositions.has(t)&&(e.dispositions.set(t,"retry"),t.retry())},E=async(e,n,t,o,i)=>{const r=y(i),s=h(n),a=e.originals.find(u=>v(e,u)===s&&!e.dispositions.has(u)),f=`@lunora/queue: queue "${o}" (${t.exportName})`,c=t.binding===void 0?void 0:i[t.binding],d=[];for(const u of e.originals)if(!e.dispositions.has(u)){if(e.dispositions.set(u,"retry"),a!==void 0&&u!==a){u.retry();continue}d.push(u)}await Promise.all(d.map(async u=>{const l=m(e,u),p=typeof c?.send!="function"||l.requeued||r===void 0?void 0:async w=>{await c.send(await b(r,l.id,l.body),{contentType:"json",delaySeconds:w})};await R(u,{maxRetries:g(t),requeue:p,where:f})==="requeued"&&e.requeued.add(u)}))},F=async(e,n,t,o)=>{if(typeof n?.send!="function"||t===void 0)return"it has no producer binding or no admin token to seal a copy with";try{const i=await x(e,t);await n.send(await b(t,i.id,i.body),{contentType:"json",delaySeconds:o});return}catch(i){return i instanceof Error?i.message:String(i)}},U=async(e,n,t,o)=>{const i=y(t),r=n.binding===void 0?void 0:t[n.binding];await Promise.all(e.messages.map(async s=>{const a=await F(s,r,i,o);if(a===void 0){s.ack();return}console.error(`@lunora/queue: queue "${e.queue}" (${n.exportName}): message ${s.id} is held with a delayed retry because a fresh copy could not be sent (${a}); it spends a delivery attempt.`),s.retry({delaySeconds:o})}))},_=(e,n)=>{const t=Object.hasOwn(n,e.queue)?n[e.queue]:void 0;if(t===void 0){const o=Object.keys(n),i=o.length===0?"no push queues are declared":`known push queues: ${o.join(", ")}`;throw new S("INTERNAL",`@lunora/queue: received a batch for queue "${e.queue}" but no push handler is registered (${i}). Likely causes: the queue is declared \`pull\`, a renamed per-environment queue has no wrangler.jsonc producer with the declared binding, or codegen was not rerun.`)}return t},K=async(e,n,t)=>{const o=_(e,n);if(t.holdSeconds!==void 0){await U(e,o,t.env,t.holdSeconds);return}const{handler:i}=o.definition;if(typeof i!="function")throw new TypeError(`@lunora/queue: queue "${e.queue}" (${o.exportName}) has no push handler — it is declared as a pull consumer`);const r=$({env:t.env,exportName:o.exportName,fetchImpl:t.fetchImpl,...t.traceparent===void 0?{}:{traceparent:t.traceparent},live:t.live}),s=await j(e,r.run,y(t.env));let a=!1,f;try{await i(r,s.wrappedBatch)}catch(l){a=!0,f=l}const c=D(s,a,f);if(c!==void 0){P(s,c);const{body:l,status:p}=A(f,{redactedMessage:"internal error"});console.error(`@lunora/queue: dropped message ${v(s,c)} on queue "${e.queue}" (${o.exportName}) — a dispatch it made failed with a deterministic ${String(p)} (${l.code}: ${l.message}), so it was acked, not retried. Its retries are NOT exhausted and it is not dead-lettered — it will never be redelivered.`)}const d=a&&k(f);d&&await E(s,f,o,e.queue,t.env);const u=a&&c===void 0&&!d;if(t.capture!==void 0)try{await t.capture(C(s,o,e.queue,a,f,c))}catch(l){console.warn("@lunora/queue: capture sink failed (delivery unaffected):",l)}if(u)throw f};export{K as dispatchQueueBatch};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/queue",
3
- "version": "1.0.0-alpha.136",
3
+ "version": "1.0.0-alpha.138",
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",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@lunora/errors": ">=1.0.0-alpha.51 <2.0.0-0",
48
- "@lunora/platform": "1.0.0-alpha.76",
48
+ "@lunora/platform": "1.0.0-alpha.77",
49
49
  "@lunora/wire": "1.0.0-alpha.6"
50
50
  },
51
51
  "engines": {
@@ -1 +0,0 @@
1
- import{c as h,r as x,e as N,t as q,n as k,X as R}from"./run-context-C2uF0PXy.mjs";import{LunoraError as $,toErrorBody as M}from"@lunora/errors";import{s as A,o as I}from"./requeue-envelope-DqPmW3Ko.mjs";const b=e=>typeof e.definition.maxRetries=="number"?e.definition.maxRetries:R,O=e=>{if(e instanceof Date)return e.getTime();const t=typeof e=="number"?e:Number(e);return Number.isFinite(t)?t:0},T=async(e,t)=>{const n=await I(t,e.body).catch(()=>{});return n===void 0?{body:e.body,id:e.id,requeued:!1}:{...n,requeued:!0}},g=e=>{const t=e.LUNORA_ADMIN_TOKEN;return typeof t=="string"&&t.length>0?t:void 0},B=(e,t)=>{let n=0;return(i,o,r)=>(n+=1,e(i,o,{...r,dedupId:`${t}#${String(n)}`,messageId:t}))},L=(e,t,n,i)=>{const o=B(i,t.id);return new Proxy(e,{get:(r,s)=>s==="id"?t.id:s==="body"?t.body:s==="ack"?()=>{n.set(r,"ack"),r.ack()}:s==="retry"?f=>{n.set(r,"retry"),r.retry(f)}:s==="run"?o:Reflect.get(r,s,r)})},j=(e,t,n)=>new Proxy(e,{get:(i,o)=>o==="ackAll"?()=>{n("ack"),i.ackAll()}:o==="retryAll"?r=>{n("retry"),i.retryAll(r)}:o==="messages"?t:Reflect.get(i,o,i)}),S=async(e,t,n)=>{const i=new Map,o=e.messages,r=new Map,s=[];for(const c of o){const u=await T(c,n);r.set(c,u),s.push(L(c,u,i,t))}const l=j(e,s,c=>{for(const u of o)i.has(u)||i.set(u,c)});return{dispositions:i,originals:o,requeued:new Set,views:r,wrappedBatch:l}},m=(e,t)=>e.views.get(t)??{body:t.body,id:t.id,requeued:!1},v=(e,t)=>m(e,t).id,Q=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)},D=(e,t,n,i,o,r)=>{const s=i?Q(o):void 0,f=b(t),l=i?"error":"ack",c=typeof t.definition.deadLetterQueue=="string"&&t.definition.deadLetterQueue.length>0;return e.originals.map(u=>{const d=u===r,a=e.dispositions.get(u),p=d?"error":a??l,w=typeof u.attempts=="number"?u.attempts:1,y=m(e,u);return{attempts:w,body:y.body,deadLettered:c&&!d&&!e.requeued.has(u)&&p!=="ack"&&w>f,error:p==="error"?s:void 0,exportName:t.exportName,messageId:y.id,outcome:p,queue:n,timestamp:O(u.timestamp)}})},C=(e,t,n)=>{if(!t||!N(n))return;const i=q(n);if(i===void 0)return;const o=e.originals.find(r=>v(e,r)===i);if(!(o===void 0||e.dispositions.has(o)))return o},P=(e,t)=>{t.ack();for(const n of e.originals)n!==t&&!e.dispositions.has(n)&&(e.dispositions.set(n,"retry"),n.retry())},F=async(e,t,n,i,o)=>{const r=g(o),s=q(t),f=e.originals.find(d=>v(e,d)===s&&!e.dispositions.has(d)),l=`@lunora/queue: queue "${i}" (${n.exportName})`,c=n.binding===void 0?void 0:o[n.binding],u=[];for(const d of e.originals)if(!e.dispositions.has(d)){if(e.dispositions.set(d,"retry"),f!==void 0&&d!==f){d.retry();continue}u.push(d)}await Promise.all(u.map(async d=>{const a=m(e,d),p=typeof c?.send!="function"||a.requeued||r===void 0?void 0:async w=>{await c.send(await A(r,a.id,a.body),{contentType:"json",delaySeconds:w})};await k(d,{maxRetries:b(n),requeue:p,where:l})==="requeued"&&e.requeued.add(d)}))},E=async(e,t,n)=>{const i=Object.hasOwn(t,e.queue)?t[e.queue]:void 0;if(i===void 0){const a=Object.keys(t),p=a.length===0?"no push queues are declared":`known push queues: ${a.join(", ")}`;throw new $("INTERNAL",`@lunora/queue: received a batch for queue "${e.queue}" but no push handler is registered (${p}). Likely causes: the queue is declared \`pull\`, a renamed per-environment queue has no wrangler.jsonc producer with the declared binding, or codegen was not rerun.`)}const{handler:o}=i.definition;if(typeof o!="function")throw new TypeError(`@lunora/queue: queue "${e.queue}" (${i.exportName}) has no push handler — it is declared as a pull consumer`);const r=h({env:n.env,exportName:i.exportName,fetchImpl:n.fetchImpl,...n.traceparent===void 0?{}:{traceparent:n.traceparent},live:n.live}),s=await S(e,r.run,g(n.env));let f=!1,l;try{await o(r,s.wrappedBatch)}catch(a){f=!0,l=a}const c=C(s,f,l);if(c!==void 0){P(s,c);const{body:a,status:p}=M(l,{redactedMessage:"internal error"});console.error(`@lunora/queue: dropped message ${v(s,c)} on queue "${e.queue}" (${i.exportName}) — a dispatch it made failed with a deterministic ${String(p)} (${a.code}: ${a.message}), so it was acked, not retried. Its retries are NOT exhausted and it is not dead-lettered — it will never be redelivered.`)}const u=f&&x(l);u&&await F(s,l,i,e.queue,n.env);const d=f&&c===void 0&&!u;if(n.capture!==void 0)try{await n.capture(D(s,i,e.queue,f,l,c))}catch(a){console.warn("@lunora/queue: capture sink failed (delivery unaffected):",a)}if(d)throw l};export{E as dispatchQueueBatch};