lonnymq 0.0.12 → 0.0.13

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.cjs CHANGED
@@ -1,4 +1,4 @@
1
- var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=Object.prototype.hasOwnProperty;var I=new WeakMap,Y=(e)=>{var t=I.get(e),s;if(t)return t;if(t=h({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")z(e).map((E)=>!W.call(t,E)&&h(t,E,{get:()=>e[E],enumerable:!(s=V(e,E))||s.enumerable}));return I.set(e,t),t};var X=(e,t)=>{for(var s in t)h(e,s,{get:t[s],enumerable:!0,configurable:!0,set:(E)=>t[s]=()=>E})};var ce={};X(ce,{queueEventDecode:()=>f,Queue:()=>L,MessageDequeueCommand:()=>o,MessageDeleteCommand:()=>i,MessageDeferCommand:()=>u,MessageCreateCommand:()=>m,ChannelPolicySetCommand:()=>c,ChannelPolicyClearCommand:()=>l});module.exports=Y(ce);var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),J=(e)=>({nodeType:"RAW",value:e}),K=(e)=>{return`'${e.replace(/'/g,"''")}'`},j=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return K(e);else if(typeof e==="number")return e.toString();else if(typeof e==="boolean")return e?"TRUE":"FALSE";else if(e instanceof Date)return`'${e.toISOString()}'`;else if(typeof e==="bigint")return e.toString();else throw new Error(`Unsupported value type: ${typeof e}`)},Z=(e)=>{return`"${e.replace(/"/g,'""')}"`},ee=(e)=>{if(e.nodeType==="VALUE")return j(e.value);else if(e.nodeType==="REF")return Z(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var a=(e,...t)=>{let s=[];for(let E=0;E<e.length;E+=1)if(s.push(e[E]),E<t.length)s.push(ee(t[E]));return J(s.join(""))};class l{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(a`
1
+ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=Object.prototype.hasOwnProperty;var I=new WeakMap,Y=(e)=>{var t=I.get(e),s;if(t)return t;if(t=h({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")z(e).map((E)=>!W.call(t,E)&&h(t,E,{get:()=>e[E],enumerable:!(s=V(e,E))||s.enumerable}));return I.set(e,t),t};var X=(e,t)=>{for(var s in t)h(e,s,{get:t[s],enumerable:!0,configurable:!0,set:(E)=>t[s]=()=>E})};var ce={};X(ce,{queueEventDecode:()=>f,Queue:()=>L,MessageDequeueCommand:()=>m,MessageDeleteCommand:()=>i,MessageDeferCommand:()=>u,MessageCreateCommand:()=>o,ChannelPolicySetCommand:()=>c,ChannelPolicyClearCommand:()=>l});module.exports=Y(ce);var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),J=(e)=>({nodeType:"RAW",value:e}),K=(e)=>{return`'${e.replace(/'/g,"''")}'`},j=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return K(e);else if(typeof e==="number")return e.toString();else if(typeof e==="boolean")return e?"TRUE":"FALSE";else if(e instanceof Date)return`'${e.toISOString()}'`;else if(typeof e==="bigint")return e.toString();else throw new Error(`Unsupported value type: ${typeof e}`)},Z=(e)=>{return`"${e.replace(/"/g,'""')}"`},ee=(e)=>{if(e.nodeType==="VALUE")return j(e.value);else if(e.nodeType==="REF")return Z(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var a=(e,...t)=>{let s=[];for(let E=0;E<e.length;E+=1)if(s.push(e[E]),E<t.length)s.push(ee(t[E]));return J(s.join(""))};class l{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(a`
2
2
  SELECT 1 FROM ${n(this.schema)}."channel_policy_clear"(
3
3
  $1
4
4
  )
@@ -9,7 +9,7 @@ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=
9
9
  $3::INTEGER,
10
10
  $4::INTEGER
11
11
  )
12
- `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var N=(e)=>{return e*1000},te=(e)=>{return N(e*60)},U=(e)=>{return te(e*60)};var d=N(0),O=U(1);var f=(e)=>{let t=JSON.parse(e);if(t.type===0)return{eventType:"MESSAGE_CREATED",id:t.id,delayMs:t.delay_ms};else if(t.type===1)return{eventType:"MESSAGE_DELETED",id:t.id};else if(t.type===2)return{eventType:"MESSAGE_DEFERRED",id:t.id,delayMs:t.delay_ms};else throw new Error("Unknown event type")};var x=require("node:crypto");class m{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,s=e.lockMs===void 0?O:Math.max(0,e.lockMs),E=e.delayMs===void 0?d:e.delayMs;this.id=x.randomUUID(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=s,this.delayMs=E,this.createdAt=new Date}async execute(e){let t=await e.query(a`
12
+ `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var N=(e)=>{return e*1000},te=(e)=>{return N(e*60)},U=(e)=>{return te(e*60)};var d=N(0),O=U(1);var f=(e)=>{let t=JSON.parse(e);if(t.type===0)return{eventType:"MESSAGE_CREATED",id:t.id,delayMs:t.delay_ms};else if(t.type===1)return{eventType:"MESSAGE_DELETED",id:t.id};else if(t.type===2)return{eventType:"MESSAGE_DEFERRED",id:t.id,delayMs:t.delay_ms};else throw new Error("Unknown event type")};var x=require("node:crypto");class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,s=e.lockMs===void 0?O:Math.max(0,e.lockMs),E=e.delayMs===void 0?d:e.delayMs;this.id=x.randomUUID(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=s,this.delayMs=E,this.createdAt=new Date}async execute(e){let t=await e.query(a`
13
13
  SELECT * FROM ${n(this.schema)}."message_create"(
14
14
  $1,
15
15
  $2,
@@ -18,7 +18,7 @@ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=
18
18
  $5::BIGINT,
19
19
  $6::BIGINT
20
20
  )
21
- `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((s)=>s.rows[0]);if(t.result_code===1)return{resultType:"MESSAGE_DROPPED"};else if(t.result_code===2)return{resultType:"MESSAGE_DEDUPLICATED"};else if(t.result_code===0)return{resultType:"MESSAGE_CREATED"};else throw new Error("Unexpected result")}}class o{schema;constructor(e){this.schema=e.schema}async execute(e){let t=await e.query(a`
21
+ `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((s)=>s.rows[0]);if(t.result_code===1)return{resultType:"MESSAGE_DROPPED"};else if(t.result_code===2)return{resultType:"MESSAGE_DEDUPLICATED"};else if(t.result_code===0)return{resultType:"MESSAGE_CREATED"};else throw new Error("Unexpected result")}}class m{schema;constructor(e){this.schema=e.schema}async execute(e){let t=await e.query(a`
22
22
  SELECT
23
23
  result_code,
24
24
  metadata,
@@ -576,7 +576,7 @@ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=
576
576
  RETURN;
577
577
  END;
578
578
  $$ LANGUAGE plpgsql;
579
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-channel-policy-set.ts",w={name:_(__filename),sql:(e)=>{return[a`
579
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-channel-policy-set.ts",k={name:_(__filename),sql:(e)=>{return[a`
580
580
  CREATE FUNCTION ${n(e.schema)}."channel_policy_set" (
581
581
  p_name TEXT,
582
582
  p_max_size INTEGER,
@@ -612,7 +612,7 @@ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=
612
612
  WHERE "name" = p_name;
613
613
  END;
614
614
  $$ LANGUAGE plpgsql;
615
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-clear.ts",k={name:_(__filename),sql:(e)=>{return[a`
615
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-clear.ts",w={name:_(__filename),sql:(e)=>{return[a`
616
616
  CREATE FUNCTION ${n(e.schema)}."channel_policy_clear" (
617
617
  p_name TEXT
618
618
  ) RETURNS VOID AS $$
@@ -642,4 +642,4 @@ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=
642
642
  END IF;
643
643
  END;
644
644
  $$ LANGUAGE plpgsql;
645
- `]}};class S{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new m({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),s=new Promise((E)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((H)=>E(H))})});return{messageId:t.id,promise:s}}}class D{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([t.channelName,null,t.createdAt.toISOString()]),execute:async(s)=>{await t.execute(s)}})}clear(){let e=new l({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(t)=>{await e.execute(t)}})}}class A{policy;message;constructor(e){this.message=new S(e),this.policy=new D(e)}}var le=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class y{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new A({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){for(let t of this.commands.sort(le))await t.execute(e.databaseClient)}}class M{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new m({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class p{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new l({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class C{policy;message;constructor(e){this.message=new M({schema:e.schema,channelName:e.channelName}),this.policy=new p({schema:e.schema,channelName:e.channelName})}}class v{schema;id;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.channelName=e.channelName,this.name=e.name,this.content=e.content,this.state=e.state,this.numAttempts=e.numAttempts}async defer(e){return new u({schema:this.schema,id:this.id,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new i({schema:this.schema,id:this.id}).execute(e.databaseClient)}}class L{schema;constructor(e){this.schema=e.schema}async dequeue(e){let s=await new o({schema:this.schema}).execute(e.databaseClient);if(s.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new v({schema:this.schema,id:s.message.id,channelName:s.message.channelName,name:s.message.name,content:s.message.content,state:s.message.state,numAttempts:s.message.numAttempts})};else return s}channel(e){return new C({schema:this.schema,channelName:e})}batch(){return new y({schema:this.schema})}migrations(e={}){return[F,q,$,B,P,b,Q,w,k].sort((t,s)=>t.name.localeCompare(s.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>G(t.value))}}
645
+ `]}};class S{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),s=new Promise((E)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((H)=>E(H))})});return{messageId:t.id,promise:s}}}class D{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([t.channelName,null,t.createdAt.toISOString()]),execute:async(s)=>{await t.execute(s)}})}clear(){let e=new l({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(t)=>{await e.execute(t)}})}}class A{policy;message;constructor(e){this.message=new S(e),this.policy=new D(e)}}var le=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class y{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new A({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){for(let t of this.commands.sort(le))await t.execute(e.databaseClient)}}class M{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class p{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new l({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class C{policy;message;constructor(e){this.message=new M({schema:e.schema,channelName:e.channelName}),this.policy=new p({schema:e.schema,channelName:e.channelName})}}class v{schema;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.channelName=e.channelName,this.isUnlocked=e.isUnlocked,this.name=e.name,this.content=e.content,this.state=e.state,this.numAttempts=e.numAttempts}async defer(e){return new u({schema:this.schema,id:this.id,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new i({schema:this.schema,id:this.id}).execute(e.databaseClient)}}class L{schema;constructor(e){this.schema=e.schema}async dequeue(e){let s=await new m({schema:this.schema}).execute(e.databaseClient);if(s.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new v({schema:this.schema,id:s.message.id,channelName:s.message.channelName,name:s.message.name,isUnlocked:s.message.isUnlocked,content:s.message.content,state:s.message.state,numAttempts:s.message.numAttempts})};else return s}channel(e){return new C({schema:this.schema,channelName:e})}batch(){return new y({schema:this.schema})}migrations(e={}){return[F,q,$,B,P,b,Q,k,w].sort((t,s)=>t.name.localeCompare(s.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>G(t.value))}}
package/dist/index.d.ts CHANGED
@@ -254,6 +254,7 @@ export declare class QueueChannel {
254
254
  export declare class QueueMessage {
255
255
  private readonly schema;
256
256
  readonly id: string;
257
+ readonly isUnlocked: boolean;
257
258
  readonly channelName: string;
258
259
  readonly name: string | null;
259
260
  readonly content: Buffer;
@@ -263,6 +264,7 @@ export declare class QueueMessage {
263
264
  schema: string;
264
265
  id: string;
265
266
  channelName: string;
267
+ isUnlocked: boolean;
266
268
  name: string | null;
267
269
  content: Buffer;
268
270
  state: Buffer | null;
package/dist/index.js CHANGED
@@ -1,15 +1,15 @@
1
- var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=>({nodeType:"RAW",value:e}),w=(e)=>{return`'${e.replace(/'/g,"''")}'`},k=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return w(e);else if(typeof e==="number")return e.toString();else if(typeof e==="boolean")return e?"TRUE":"FALSE";else if(e instanceof Date)return`'${e.toISOString()}'`;else if(typeof e==="bigint")return e.toString();else throw new Error(`Unsupported value type: ${typeof e}`)},H=(e)=>{return`"${e.replace(/"/g,'""')}"`},z=(e)=>{if(e.nodeType==="VALUE")return k(e.value);else if(e.nodeType==="REF")return H(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var s=(e,...t)=>{let a=[];for(let _=0;_<e.length;_+=1)if(a.push(e[_]),_<t.length)a.push(z(t[_]));return Q(a.join(""))};class l{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(s`
1
+ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=>({nodeType:"RAW",value:e}),k=(e)=>{return`'${e.replace(/'/g,"''")}'`},w=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return k(e);else if(typeof e==="number")return e.toString();else if(typeof e==="boolean")return e?"TRUE":"FALSE";else if(e instanceof Date)return`'${e.toISOString()}'`;else if(typeof e==="bigint")return e.toString();else throw new Error(`Unsupported value type: ${typeof e}`)},H=(e)=>{return`"${e.replace(/"/g,'""')}"`},z=(e)=>{if(e.nodeType==="VALUE")return w(e.value);else if(e.nodeType==="REF")return H(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var a=(e,...t)=>{let s=[];for(let _=0;_<e.length;_+=1)if(s.push(e[_]),_<t.length)s.push(z(t[_]));return Q(s.join(""))};class l{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(a`
2
2
  SELECT 1 FROM ${n(this.schema)}."channel_policy_clear"(
3
3
  $1
4
4
  )
5
- `.value,[this.channelName])}}class c{schema;channelName;maxSize;maxConcurrency;releaseIntervalMs;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName;let t=e.maxConcurrency??null;this.maxConcurrency=t!==null?Math.max(0,t):null;let a=e.maxSize??null;this.maxSize=a!==null?Math.max(0,a):null;let _=e.releaseIntervalMs??null;this.releaseIntervalMs=_!==null?Math.max(0,_):null,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(s`
5
+ `.value,[this.channelName])}}class c{schema;channelName;maxSize;maxConcurrency;releaseIntervalMs;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName;let t=e.maxConcurrency??null;this.maxConcurrency=t!==null?Math.max(0,t):null;let s=e.maxSize??null;this.maxSize=s!==null?Math.max(0,s):null;let _=e.releaseIntervalMs??null;this.releaseIntervalMs=_!==null?Math.max(0,_):null,this.createdAt=new Date}sortKeyGet(){return JSON.stringify([this.channelName,null,this.createdAt.toISOString()])}async execute(e){await e.query(a`
6
6
  SELECT 1 FROM ${n(this.schema)}."channel_policy_set"(
7
7
  $1,
8
8
  $2::INTEGER,
9
9
  $3::INTEGER,
10
10
  $4::INTEGER
11
11
  )
12
- `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var h=(e)=>{return e*1000},V=(e)=>{return h(e*60)},C=(e)=>{return V(e*60)};var o=h(0),v=C(1);var W=(e)=>{let t=JSON.parse(e);if(t.type===0)return{eventType:"MESSAGE_CREATED",id:t.id,delayMs:t.delay_ms};else if(t.type===1)return{eventType:"MESSAGE_DELETED",id:t.id};else if(t.type===2)return{eventType:"MESSAGE_DEFERRED",id:t.id,delayMs:t.delay_ms};else throw new Error("Unknown event type")};import{randomUUID as X}from"node:crypto";class m{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,a=e.lockMs===void 0?v:Math.max(0,e.lockMs),_=e.delayMs===void 0?o:e.delayMs;this.id=X(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=a,this.delayMs=_,this.createdAt=new Date}async execute(e){let t=await e.query(s`
12
+ `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var h=(e)=>{return e*1000},V=(e)=>{return h(e*60)},C=(e)=>{return V(e*60)};var m=h(0),v=C(1);var W=(e)=>{let t=JSON.parse(e);if(t.type===0)return{eventType:"MESSAGE_CREATED",id:t.id,delayMs:t.delay_ms};else if(t.type===1)return{eventType:"MESSAGE_DELETED",id:t.id};else if(t.type===2)return{eventType:"MESSAGE_DEFERRED",id:t.id,delayMs:t.delay_ms};else throw new Error("Unknown event type")};import{randomUUID as X}from"node:crypto";class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,s=e.lockMs===void 0?v:Math.max(0,e.lockMs),_=e.delayMs===void 0?m:e.delayMs;this.id=X(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=s,this.delayMs=_,this.createdAt=new Date}async execute(e){let t=await e.query(a`
13
13
  SELECT * FROM ${n(this.schema)}."message_create"(
14
14
  $1,
15
15
  $2,
@@ -18,26 +18,26 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
18
18
  $5::BIGINT,
19
19
  $6::BIGINT
20
20
  )
21
- `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((a)=>a.rows[0]);if(t.result_code===1)return{resultType:"MESSAGE_DROPPED"};else if(t.result_code===2)return{resultType:"MESSAGE_DEDUPLICATED"};else if(t.result_code===0)return{resultType:"MESSAGE_CREATED"};else throw new Error("Unexpected result")}}class i{schema;constructor(e){this.schema=e.schema}async execute(e){let t=await e.query(s`
21
+ `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((s)=>s.rows[0]);if(t.result_code===1)return{resultType:"MESSAGE_DROPPED"};else if(t.result_code===2)return{resultType:"MESSAGE_DEDUPLICATED"};else if(t.result_code===0)return{resultType:"MESSAGE_CREATED"};else throw new Error("Unexpected result")}}class i{schema;constructor(e){this.schema=e.schema}async execute(e){let t=await e.query(a`
22
22
  SELECT
23
23
  result_code,
24
24
  metadata,
25
25
  content,
26
26
  state
27
27
  FROM ${n(this.schema)}."message_dequeue"()
28
- `.value,[]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_AVAILABLE",retryMs:t.metadata.retry_ms};else if(t.result_code===1)return{resultType:"MESSAGE_DEQUEUED",message:{id:t.metadata.id,channelName:t.metadata.channel_name,isUnlocked:t.metadata.is_unlocked,name:t.metadata.name,content:t.content,state:t.state,numAttempts:t.metadata.num_attempts}};else throw new Error("Unexpected dequeue result")}}class u{schema;id;constructor(e){this.schema=e.schema,this.id=e.id}async execute(e){let t=await e.query(s`
28
+ `.value,[]).then((s)=>s.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_AVAILABLE",retryMs:t.metadata.retry_ms};else if(t.result_code===1)return{resultType:"MESSAGE_DEQUEUED",message:{id:t.metadata.id,channelName:t.metadata.channel_name,isUnlocked:t.metadata.is_unlocked,name:t.metadata.name,content:t.content,state:t.state,numAttempts:t.metadata.num_attempts}};else throw new Error("Unexpected dequeue result")}}class u{schema;id;constructor(e){this.schema=e.schema,this.id=e.id}async execute(e){let t=await e.query(a`
29
29
  SELECT * FROM ${n(this.schema)}."message_delete"(
30
30
  $1
31
31
  )
32
- `.value,[this.id]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_DELETED"};else throw new Error("Unexpected result")}}class d{schema;id;delayMs;state;constructor(e){let t=e.delayMs===void 0?o:e.delayMs;this.schema=e.schema,this.id=e.id,this.delayMs=t,this.state=e.state??null}async execute(e){let t=await e.query(s`
32
+ `.value,[this.id]).then((s)=>s.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_DELETED"};else throw new Error("Unexpected result")}}class d{schema;id;delayMs;state;constructor(e){let t=e.delayMs===void 0?m:e.delayMs;this.schema=e.schema,this.id=e.id,this.delayMs=t,this.state=e.state??null}async execute(e){let t=await e.query(a`
33
33
  SELECT * FROM ${n(this.schema)}."message_defer"(
34
34
  $1,
35
35
  $2::BIGINT,
36
36
  $3
37
37
  )
38
- `.value,[this.id,this.delayMs,this.state]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_DEFERRED"};else throw new Error("Unexpected result")}}var L=(e)=>{let t=e.split(`
39
- `),a=Number.MAX_SAFE_INTEGER;for(let _ of t){if(_.trim().length===0)continue;let T=_.search(/\S/);a=Math.min(a,T)}return t.map((_)=>_.slice(a)).join(`
40
- `).trim()};import{dirname as I}from"path";var __filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",Z=I(I(__filename)),ee=new RegExp(`^${Z}/`),E=(e)=>{return e.replace(ee,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",U={name:E(__filename),sql:(e)=>{return[s`
38
+ `.value,[this.id,this.delayMs,this.state]).then((s)=>s.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_DEFERRED"};else throw new Error("Unexpected result")}}var L=(e)=>{let t=e.split(`
39
+ `),s=Number.MAX_SAFE_INTEGER;for(let _ of t){if(_.trim().length===0)continue;let T=_.search(/\S/);s=Math.min(s,T)}return t.map((_)=>_.slice(s)).join(`
40
+ `).trim()};import{dirname as I}from"path";var __filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",Z=I(I(__filename)),ee=new RegExp(`^${Z}/`),E=(e)=>{return e.replace(ee,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",U={name:E(__filename),sql:(e)=>{return[a`
41
41
  CREATE TABLE ${n(e.schema)}."channel_policy" (
42
42
  "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
43
43
  "name" TEXT NOT NULL,
@@ -47,10 +47,10 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
47
47
  "created_at" TIMESTAMP NOT NULL,
48
48
  PRIMARY KEY ("id")
49
49
  );
50
- `,s`
50
+ `,a`
51
51
  CREATE UNIQUE INDEX "channel_policy_name_ux"
52
52
  ON ${n(e.schema)}."channel_policy" ("name");
53
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/01-table-channel-state.ts",O={name:E(__filename),sql:(e)=>{return[s`
53
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/01-table-channel-state.ts",O={name:E(__filename),sql:(e)=>{return[a`
54
54
  CREATE TABLE ${n(e.schema)}."channel_state" (
55
55
  "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
56
56
  "name" TEXT NOT NULL,
@@ -67,16 +67,16 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
67
67
  "created_at" TIMESTAMP NOT NULL,
68
68
  PRIMARY KEY ("id")
69
69
  );
70
- `,s`
70
+ `,a`
71
71
  CREATE UNIQUE INDEX "channel_state_name_ux"
72
72
  ON ${n(e.schema)}."channel_state" ("name");
73
- `,s`
73
+ `,a`
74
74
  CREATE INDEX "channel_state_dequeue_ix"
75
75
  ON ${n(e.schema)}."channel_state" (
76
76
  "active_next_at" ASC
77
77
  ) WHERE "message_id" IS NOT NULL
78
78
  AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency");
79
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/02-table-message.ts",f={name:E(__filename),sql:(e)=>{return[s`
79
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/02-table-message.ts",f={name:E(__filename),sql:(e)=>{return[a`
80
80
  CREATE TABLE ${n(e.schema)}."message" (
81
81
  "id" UUID NOT NULL,
82
82
  "channel_name" TEXT NOT NULL,
@@ -92,25 +92,25 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
92
92
  "created_at" TIMESTAMP NOT NULL,
93
93
  PRIMARY KEY ("id")
94
94
  );
95
- `,s`
95
+ `,a`
96
96
  CREATE UNIQUE INDEX "message_name_ux"
97
97
  ON ${n(e.schema)}."message" (
98
98
  "channel_name",
99
99
  "name"
100
100
  ) WHERE "num_attempts" = 0
101
- `,s`
101
+ `,a`
102
102
  CREATE INDEX "message_dequeue_ix"
103
103
  ON ${n(e.schema)}."message" (
104
104
  "channel_name",
105
105
  "dequeue_at" ASC,
106
106
  "seq_no" ASC
107
107
  ) WHERE NOT "is_locked";
108
- `,s`
108
+ `,a`
109
109
  CREATE INDEX "message_locked_dequeue_ix"
110
110
  ON ${n(e.schema)}."message" (
111
111
  "unlock_at" ASC
112
112
  ) WHERE "is_locked";
113
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/03-function-message-create.ts",x={name:E(__filename),sql:(e)=>{return[s`
113
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/03-function-message-create.ts",x={name:E(__filename),sql:(e)=>{return[a`
114
114
  CREATE FUNCTION ${n(e.schema)}."message_create" (
115
115
  p_id UUID,
116
116
  p_channel_name TEXT,
@@ -252,7 +252,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
252
252
  RETURN;
253
253
  END;
254
254
  $$ LANGUAGE plpgsql;
255
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",G={name:E(__filename),sql:(e)=>{return[s`
255
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",G={name:E(__filename),sql:(e)=>{return[a`
256
256
  CREATE FUNCTION ${n(e.schema)}."message_dequeue" ()
257
257
  RETURNS TABLE (
258
258
  result_code INTEGER,
@@ -407,7 +407,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
407
407
  RETURN;
408
408
  END;
409
409
  $$ LANGUAGE plpgsql;
410
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/05-function-message-delete.ts",F={name:E(__filename),sql:(e)=>{return[s`
410
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/05-function-message-delete.ts",F={name:E(__filename),sql:(e)=>{return[a`
411
411
  CREATE FUNCTION ${n(e.schema)}."message_delete" (
412
412
  p_id UUID
413
413
  )
@@ -482,7 +482,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
482
482
  RETURN;
483
483
  END;
484
484
  $$ LANGUAGE plpgsql;
485
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/06-function-message-defer.ts",q={name:E(__filename),sql:(e)=>{return[s`
485
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/06-function-message-defer.ts",q={name:E(__filename),sql:(e)=>{return[a`
486
486
  CREATE FUNCTION ${n(e.schema)}."message_defer" (
487
487
  p_id UUID,
488
488
  p_delay_ms BIGINT,
@@ -576,7 +576,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
576
576
  RETURN;
577
577
  END;
578
578
  $$ LANGUAGE plpgsql;
579
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-channel-policy-set.ts",$={name:E(__filename),sql:(e)=>{return[s`
579
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-channel-policy-set.ts",$={name:E(__filename),sql:(e)=>{return[a`
580
580
  CREATE FUNCTION ${n(e.schema)}."channel_policy_set" (
581
581
  p_name TEXT,
582
582
  p_max_size INTEGER,
@@ -612,7 +612,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
612
612
  WHERE "name" = p_name;
613
613
  END;
614
614
  $$ LANGUAGE plpgsql;
615
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-clear.ts",B={name:E(__filename),sql:(e)=>{return[s`
615
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-clear.ts",B={name:E(__filename),sql:(e)=>{return[a`
616
616
  CREATE FUNCTION ${n(e.schema)}."channel_policy_clear" (
617
617
  p_name TEXT
618
618
  ) RETURNS VOID AS $$
@@ -642,4 +642,4 @@ var r=(e)=>({nodeType:"VALUE",value:e}),n=(e)=>({nodeType:"REF",value:e}),Q=(e)=
642
642
  END IF;
643
643
  END;
644
644
  $$ LANGUAGE plpgsql;
645
- `]}};class R{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new m({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),a=new Promise((_)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((b)=>_(b))})});return{messageId:t.id,promise:a}}}class g{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([t.channelName,null,t.createdAt.toISOString()]),execute:async(a)=>{await t.execute(a)}})}clear(){let e=new l({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(t)=>{await e.execute(t)}})}}class S{policy;message;constructor(e){this.message=new R(e),this.policy=new g(e)}}var te=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class D{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new S({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){for(let t of this.commands.sort(te))await t.execute(e.databaseClient)}}class A{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new m({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class y{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new l({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class M{policy;message;constructor(e){this.message=new A({schema:e.schema,channelName:e.channelName}),this.policy=new y({schema:e.schema,channelName:e.channelName})}}class p{schema;id;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.channelName=e.channelName,this.name=e.name,this.content=e.content,this.state=e.state,this.numAttempts=e.numAttempts}async defer(e){return new d({schema:this.schema,id:this.id,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new u({schema:this.schema,id:this.id}).execute(e.databaseClient)}}class P{schema;constructor(e){this.schema=e.schema}async dequeue(e){let a=await new i({schema:this.schema}).execute(e.databaseClient);if(a.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new p({schema:this.schema,id:a.message.id,channelName:a.message.channelName,name:a.message.name,content:a.message.content,state:a.message.state,numAttempts:a.message.numAttempts})};else return a}channel(e){return new M({schema:this.schema,channelName:e})}batch(){return new D({schema:this.schema})}migrations(e={}){return[U,O,f,x,G,F,q,$,B].sort((t,a)=>t.name.localeCompare(a.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>L(t.value))}}export{W as queueEventDecode,P as Queue,i as MessageDequeueCommand,u as MessageDeleteCommand,d as MessageDeferCommand,m as MessageCreateCommand,c as ChannelPolicySetCommand,l as ChannelPolicyClearCommand};
645
+ `]}};class R{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),s=new Promise((_)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((b)=>_(b))})});return{messageId:t.id,promise:s}}}class g{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([t.channelName,null,t.createdAt.toISOString()]),execute:async(s)=>{await t.execute(s)}})}clear(){let e=new l({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(t)=>{await e.execute(t)}})}}class S{policy;message;constructor(e){this.message=new R(e),this.policy=new g(e)}}var te=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class D{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new S({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){for(let t of this.commands.sort(te))await t.execute(e.databaseClient)}}class A{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class y{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new c({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new l({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class M{policy;message;constructor(e){this.message=new A({schema:e.schema,channelName:e.channelName}),this.policy=new y({schema:e.schema,channelName:e.channelName})}}class p{schema;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.channelName=e.channelName,this.isUnlocked=e.isUnlocked,this.name=e.name,this.content=e.content,this.state=e.state,this.numAttempts=e.numAttempts}async defer(e){return new d({schema:this.schema,id:this.id,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new u({schema:this.schema,id:this.id}).execute(e.databaseClient)}}class P{schema;constructor(e){this.schema=e.schema}async dequeue(e){let s=await new i({schema:this.schema}).execute(e.databaseClient);if(s.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new p({schema:this.schema,id:s.message.id,channelName:s.message.channelName,name:s.message.name,isUnlocked:s.message.isUnlocked,content:s.message.content,state:s.message.state,numAttempts:s.message.numAttempts})};else return s}channel(e){return new M({schema:this.schema,channelName:e})}batch(){return new D({schema:this.schema})}migrations(e={}){return[U,O,f,x,G,F,q,$,B].sort((t,s)=>t.name.localeCompare(s.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>L(t.value))}}export{W as queueEventDecode,P as Queue,i as MessageDequeueCommand,u as MessageDeleteCommand,d as MessageDeferCommand,o as MessageCreateCommand,c as ChannelPolicySetCommand,l as ChannelPolicyClearCommand};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lonnymq",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {