lonnymq 0.0.17 → 0.0.18

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:V,getOwnPropertyDescriptor:z}=Object,W=Object.prototype.hasOwnProperty;var L=new WeakMap,Y=(e)=>{var t=L.get(e),a;if(t)return t;if(t=h({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")V(e).map((r)=>!W.call(t,r)&&h(t,r,{get:()=>e[r],enumerable:!(a=z(e,r))||a.enumerable}));return L.set(e,t),t};var X=(e,t)=>{for(var a in t)h(e,a,{get:t[a],enumerable:!0,configurable:!0,set:(r)=>t[a]=()=>r})};var ie={};X(ie,{queueEventDecode:()=>O,Queue:()=>v,MessageHeartbeatCommand:()=>R,MessageDequeueCommand:()=>i,MessageDeleteCommand:()=>u,MessageDeferCommand:()=>c,MessageCreateCommand:()=>o,ChannelPolicySetCommand:()=>m,ChannelPolicyClearCommand:()=>l});module.exports=Y(ie);var E=(e)=>({nodeType:"VALUE",value:e}),s=(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 n=(e,...t)=>{let a=[];for(let r=0;r<e.length;r+=1)if(a.push(e[r]),r<t.length)a.push(ee(t[r]));return J(a.join(""))};class l{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}async execute(e){await e.query(n`
1
+ var{defineProperty:N,getOwnPropertyNames:V,getOwnPropertyDescriptor:z}=Object,W=Object.prototype.hasOwnProperty;var L=new WeakMap,Y=(e)=>{var t=L.get(e),a;if(t)return t;if(t=N({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")V(e).map((r)=>!W.call(t,r)&&N(t,r,{get:()=>e[r],enumerable:!(a=z(e,r))||a.enumerable}));return L.set(e,t),t};var X=(e,t)=>{for(var a in t)N(e,a,{get:t[a],enumerable:!0,configurable:!0,set:(r)=>t[a]=()=>r})};var ie={};X(ie,{queueEventDecode:()=>O,Queue:()=>v,MessageHeartbeatCommand:()=>d,MessageDequeueCommand:()=>c,MessageDeleteCommand:()=>i,MessageDeferCommand:()=>u,MessageCreateCommand:()=>o,ChannelPolicySetCommand:()=>m,ChannelPolicyClearCommand:()=>l});module.exports=Y(ie);var E=(e)=>({nodeType:"VALUE",value:e}),s=(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 n=(e,...t)=>{let a=[];for(let r=0;r<e.length;r+=1)if(a.push(e[r]),r<t.length)a.push(ee(t[r]));return J(a.join(""))};class l{schema;channelName;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.createdAt=new Date}async execute(e){await e.query(n`
2
2
  SELECT 1 FROM ${s(this.schema)}."channel_policy_clear"(
3
3
  $1
4
4
  )
@@ -9,7 +9,7 @@ var{defineProperty:h,getOwnPropertyNames:V,getOwnPropertyDescriptor:z}=Object,W=
9
9
  $3::INTEGER,
10
10
  $4::INTEGER
11
11
  )
12
- `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var U=(e)=>{return e*1000};var d=U(0);var O=(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 f=require("node:crypto");class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,a=Math.max(0,e.lockMs),r=e.delayMs===void 0?d:e.delayMs;this.id=f.randomUUID(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=a,this.delayMs=r,this.createdAt=new Date}async execute(e){let t=await e.query(n`
12
+ `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var U=(e)=>{return e*1000};var T=U(0);var O=(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 f=require("node:crypto");class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,a=Math.max(0,e.lockMs),r=e.delayMs===void 0?T:e.delayMs;this.id=f.randomUUID(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=a,this.delayMs=r,this.createdAt=new Date}async execute(e){let t=await e.query(n`
13
13
  SELECT * FROM ${s(this.schema)}."message_create"(
14
14
  $1,
15
15
  $2,
@@ -18,33 +18,33 @@ var{defineProperty:h,getOwnPropertyNames:V,getOwnPropertyDescriptor:z}=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((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(n`
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 c{schema;constructor(e){this.schema=e.schema}async execute(e){let t=await e.query(n`
22
22
  SELECT
23
23
  result_code,
24
24
  metadata,
25
25
  content,
26
26
  state
27
27
  FROM ${s(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;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.numAttempts=e.numAttempts}async execute(e){let t=await e.query(n`
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 i{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.numAttempts=e.numAttempts}async execute(e){let t=await e.query(n`
29
29
  SELECT * FROM ${s(this.schema)}."message_delete"(
30
30
  $1,
31
31
  $2::BIGINT
32
32
  )
33
- `.value,[this.id,this.numAttempts]).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 c{schema;id;numAttempts;delayMs;state;constructor(e){let t=e.delayMs===void 0?d:e.delayMs;this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id,this.delayMs=t,this.state=e.state??null}async execute(e){let t=await e.query(n`
33
+ `.value,[this.id,this.numAttempts]).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 u{schema;id;numAttempts;delayMs;state;constructor(e){let t=e.delayMs===void 0?T:e.delayMs;this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id,this.delayMs=t,this.state=e.state??null}async execute(e){let t=await e.query(n`
34
34
  SELECT * FROM ${s(this.schema)}."message_defer"(
35
35
  $1,
36
36
  $2::BIGINT,
37
37
  $3::BIGINT,
38
38
  $4
39
39
  )
40
- `.value,[this.id,this.numAttempts,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")}}class R{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id}async execute(e){let t=await e.query(n`
40
+ `.value,[this.id,this.numAttempts,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")}}class d{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id}async execute(e){let t=await e.query(n`
41
41
  SELECT * FROM ${s(this.schema)}."message_heartbeat"(
42
42
  $1,
43
43
  $2::BIGINT
44
44
  )
45
45
  `.value,[this.id,this.numAttempts]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"MESSAGE_STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_HEARTBEATED"};else throw new Error("Unexpected result")}}var x=(e)=>{let t=e.split(`
46
- `),a=Number.MAX_SAFE_INTEGER;for(let r of t){if(r.trim().length===0)continue;let T=r.search(/\S/);a=Math.min(a,T)}return t.map((r)=>r.slice(a)).join(`
47
- `).trim()};var S=require("path"),__filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",Ee=S.dirname(S.dirname(__filename)),_e=new RegExp(`^${Ee}/`),_=(e)=>{return e.replace(_e,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",G={name:_(__filename),sql:(e)=>{return[n`
46
+ `),a=Number.MAX_SAFE_INTEGER;for(let r of t){if(r.trim().length===0)continue;let h=r.search(/\S/);a=Math.min(a,h)}return t.map((r)=>r.slice(a)).join(`
47
+ `).trim()};var g=require("path"),__filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",Ee=g.dirname(g.dirname(__filename)),_e=new RegExp(`^${Ee}/`),_=(e)=>{return e.replace(_e,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",G={name:_(__filename),sql:(e)=>{return[n`
48
48
  CREATE TABLE ${s(e.schema)}."channel_policy" (
49
49
  "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
50
50
  "name" TEXT NOT NULL,
@@ -701,4 +701,4 @@ var{defineProperty:h,getOwnPropertyNames:V,getOwnPropertyDescriptor:z}=Object,W=
701
701
  WHERE "name" = p_name;
702
702
  END;
703
703
  $$ LANGUAGE plpgsql;
704
- `]}};class g{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}),a=new Promise((r)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((k)=>r(k))})});return{messageId:t.id,promise:a}}}class A{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new m({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 p{policy;message;constructor(e){this.message=new g(e),this.policy=new A(e)}}var ce=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class D{commands;schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.commands=[]}channel(e){return new p({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){let t=this.adaptor(e.databaseClient);for(let a of this.commands.sort(ce))await a.execute(t)}}class y{schema;channelName;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}async create(e){let t=this.adaptor(e.databaseClient);return new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(t)}}class M{schema;adaptor;channelName;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}set(e){let t=this.adaptor(e.databaseClient);return new m({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(t)}clear(e){let t=this.adaptor(e.databaseClient);return new l({schema:this.schema,channelName:this.channelName}).execute(t)}}class C{policy;message;constructor(e){this.message=new y({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName}),this.policy=new M({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName})}}class I{schema;adaptor;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,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){let t=this.adaptor(e.databaseClient);return new c({schema:this.schema,id:this.id,numAttempts:this.numAttempts,delayMs:e.delayMs,state:e.state}).execute(t)}async delete(e){let t=this.adaptor(e.databaseClient);return new u({schema:this.schema,numAttempts:this.numAttempts,id:this.id}).execute(t)}async heartbeat(e){let t=this.adaptor(e.databaseClient);return new c({schema:this.schema,id:this.id,numAttempts:this.numAttempts}).execute(t)}}class v{schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor?e.adaptor:(t)=>t}async dequeue(e){let t=new i({schema:this.schema}),a=this.adaptor(e.databaseClient),r=await t.execute(a);if(r.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new I({schema:this.schema,adaptor:this.adaptor,id:r.message.id,channelName:r.message.channelName,name:r.message.name,isUnlocked:r.message.isUnlocked,content:r.message.content,state:r.message.state,numAttempts:r.message.numAttempts})};else return r}channel(e){return new C({adaptor:this.adaptor,schema:this.schema,channelName:e})}batch(){return new D({schema:this.schema,adaptor:this.adaptor})}migrations(e={}){return[G,F,q,$,b,B,P,Q,H,w].sort((t,a)=>t.name.localeCompare(a.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>x(t.value))}}
704
+ `]}};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}),a=new Promise((r)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(h)=>t.execute(h).then((k)=>r(k))})});return{messageId:t.id,promise:a}}}class A{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new m({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 p{policy;message;constructor(e){this.message=new S(e),this.policy=new A(e)}}var ce=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class D{commands;schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.commands=[]}channel(e){return new p({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){let t=this.adaptor(e.databaseClient);for(let a of this.commands.sort(ce))await a.execute(t)}}class y{schema;channelName;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}async create(e){let t=this.adaptor(e.databaseClient);return new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(t)}}class M{schema;adaptor;channelName;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}set(e){let t=this.adaptor(e.databaseClient);return new m({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(t)}clear(e){let t=this.adaptor(e.databaseClient);return new l({schema:this.schema,channelName:this.channelName}).execute(t)}}class C{policy;message;constructor(e){this.message=new y({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName}),this.policy=new M({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName})}}class I{schema;adaptor;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,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){let t=this.adaptor(e.databaseClient);return new u({schema:this.schema,id:this.id,numAttempts:this.numAttempts,delayMs:e.delayMs,state:e.state}).execute(t)}async delete(e){let t=this.adaptor(e.databaseClient);return new i({schema:this.schema,numAttempts:this.numAttempts,id:this.id}).execute(t)}async heartbeat(e){let t=this.adaptor(e.databaseClient);return new d({schema:this.schema,id:this.id,numAttempts:this.numAttempts}).execute(t)}}class v{schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor?e.adaptor:(t)=>t}async dequeue(e){let t=new c({schema:this.schema}),a=this.adaptor(e.databaseClient),r=await t.execute(a);if(r.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new I({schema:this.schema,adaptor:this.adaptor,id:r.message.id,channelName:r.message.channelName,name:r.message.name,isUnlocked:r.message.isUnlocked,content:r.message.content,state:r.message.state,numAttempts:r.message.numAttempts})};else return r}channel(e){return new C({adaptor:this.adaptor,schema:this.schema,channelName:e})}batch(){return new D({schema:this.schema,adaptor:this.adaptor})}migrations(e={}){return[G,F,q,$,b,B,P,Q,H,w].sort((t,a)=>t.name.localeCompare(a.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>x(t.value))}}
package/dist/index.d.ts CHANGED
@@ -313,7 +313,7 @@ export declare class QueueMessage<T> {
313
313
  }): Promise<MessageDeleteCommandResult>;
314
314
  heartbeat(params: {
315
315
  databaseClient: T;
316
- }): Promise<MessageDeferCommandResult>;
316
+ }): Promise<MessageHeartbeatCommandResult>;
317
317
  }
318
318
  export type MessageDequeueResult<T> = {
319
319
  resultType: "MESSAGE_NOT_AVAILABLE";
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),Q=(e)=
9
9
  $3::INTEGER,
10
10
  $4::INTEGER
11
11
  )
12
- `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var M=(e)=>{return e*1000};var i=M(0);var z=(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 Y}from"node:crypto";class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,a=Math.max(0,e.lockMs),E=e.delayMs===void 0?i:e.delayMs;this.id=Y(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=a,this.delayMs=E,this.createdAt=new Date}async execute(e){let t=await e.query(n`
12
+ `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var C=(e)=>{return e*1000};var c=C(0);var z=(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 Y}from"node:crypto";class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,a=Math.max(0,e.lockMs),E=e.delayMs===void 0?c:e.delayMs;this.id=Y(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=t,this.lockMs=a,this.delayMs=E,this.createdAt=new Date}async execute(e){let t=await e.query(n`
13
13
  SELECT * FROM ${s(this.schema)}."message_create"(
14
14
  $1,
15
15
  $2,
@@ -18,32 +18,32 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(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 u{schema;constructor(e){this.schema=e.schema}async execute(e){let t=await e.query(n`
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(n`
22
22
  SELECT
23
23
  result_code,
24
24
  metadata,
25
25
  content,
26
26
  state
27
27
  FROM ${s(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 d{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.numAttempts=e.numAttempts}async execute(e){let t=await e.query(n`
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;numAttempts;constructor(e){this.schema=e.schema,this.id=e.id,this.numAttempts=e.numAttempts}async execute(e){let t=await e.query(n`
29
29
  SELECT * FROM ${s(this.schema)}."message_delete"(
30
30
  $1,
31
31
  $2::BIGINT
32
32
  )
33
- `.value,[this.id,this.numAttempts]).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 c{schema;id;numAttempts;delayMs;state;constructor(e){let t=e.delayMs===void 0?i:e.delayMs;this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id,this.delayMs=t,this.state=e.state??null}async execute(e){let t=await e.query(n`
33
+ `.value,[this.id,this.numAttempts]).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;numAttempts;delayMs;state;constructor(e){let t=e.delayMs===void 0?c:e.delayMs;this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id,this.delayMs=t,this.state=e.state??null}async execute(e){let t=await e.query(n`
34
34
  SELECT * FROM ${s(this.schema)}."message_defer"(
35
35
  $1,
36
36
  $2::BIGINT,
37
37
  $3::BIGINT,
38
38
  $4
39
39
  )
40
- `.value,[this.id,this.numAttempts,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")}}class C{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id}async execute(e){let t=await e.query(n`
40
+ `.value,[this.id,this.numAttempts,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")}}class T{schema;id;numAttempts;constructor(e){this.schema=e.schema,this.numAttempts=e.numAttempts,this.id=e.id}async execute(e){let t=await e.query(n`
41
41
  SELECT * FROM ${s(this.schema)}."message_heartbeat"(
42
42
  $1,
43
43
  $2::BIGINT
44
44
  )
45
45
  `.value,[this.id,this.numAttempts]).then((a)=>a.rows[0]);if(t.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(t.result_code===1)return{resultType:"MESSAGE_STATE_INVALID"};else if(t.result_code===2)return{resultType:"MESSAGE_HEARTBEATED"};else throw new Error("Unexpected result")}}var I=(e)=>{let t=e.split(`
46
- `),a=Number.MAX_SAFE_INTEGER;for(let E of t){if(E.trim().length===0)continue;let T=E.search(/\S/);a=Math.min(a,T)}return t.map((E)=>E.slice(a)).join(`
46
+ `),a=Number.MAX_SAFE_INTEGER;for(let E of t){if(E.trim().length===0)continue;let h=E.search(/\S/);a=Math.min(a,h)}return t.map((E)=>E.slice(a)).join(`
47
47
  `).trim()};import{dirname as v}from"path";var __filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",Z=v(v(__filename)),ee=new RegExp(`^${Z}/`),_=(e)=>{return e.replace(ee,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",L={name:_(__filename),sql:(e)=>{return[n`
48
48
  CREATE TABLE ${s(e.schema)}."channel_policy" (
49
49
  "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
@@ -701,4 +701,4 @@ var r=(e)=>({nodeType:"VALUE",value:e}),s=(e)=>({nodeType:"REF",value:e}),Q=(e)=
701
701
  WHERE "name" = p_name;
702
702
  END;
703
703
  $$ LANGUAGE plpgsql;
704
- `]}};class N{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}),a=new Promise((E)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((P)=>E(P))})});return{messageId:t.id,promise:a}}}class R{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let t=new m({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 N(e),this.policy=new R(e)}}var ae=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class g{commands;schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.commands=[]}channel(e){return new S({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){let t=this.adaptor(e.databaseClient);for(let a of this.commands.sort(ae))await a.execute(t)}}class A{schema;channelName;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}async create(e){let t=this.adaptor(e.databaseClient);return new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(t)}}class p{schema;adaptor;channelName;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}set(e){let t=this.adaptor(e.databaseClient);return new m({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(t)}clear(e){let t=this.adaptor(e.databaseClient);return new l({schema:this.schema,channelName:this.channelName}).execute(t)}}class D{policy;message;constructor(e){this.message=new A({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName}),this.policy=new p({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName})}}class y{schema;adaptor;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,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){let t=this.adaptor(e.databaseClient);return new c({schema:this.schema,id:this.id,numAttempts:this.numAttempts,delayMs:e.delayMs,state:e.state}).execute(t)}async delete(e){let t=this.adaptor(e.databaseClient);return new d({schema:this.schema,numAttempts:this.numAttempts,id:this.id}).execute(t)}async heartbeat(e){let t=this.adaptor(e.databaseClient);return new c({schema:this.schema,id:this.id,numAttempts:this.numAttempts}).execute(t)}}class B{schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor?e.adaptor:(t)=>t}async dequeue(e){let t=new u({schema:this.schema}),a=this.adaptor(e.databaseClient),E=await t.execute(a);if(E.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new y({schema:this.schema,adaptor:this.adaptor,id:E.message.id,channelName:E.message.channelName,name:E.message.name,isUnlocked:E.message.isUnlocked,content:E.message.content,state:E.message.state,numAttempts:E.message.numAttempts})};else return E}channel(e){return new D({adaptor:this.adaptor,schema:this.schema,channelName:e})}batch(){return new g({schema:this.schema,adaptor:this.adaptor})}migrations(e={}){return[L,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)=>I(t.value))}}export{z as queueEventDecode,B as Queue,C as MessageHeartbeatCommand,u as MessageDequeueCommand,d as MessageDeleteCommand,c as MessageDeferCommand,o as MessageCreateCommand,m as ChannelPolicySetCommand,l as ChannelPolicyClearCommand};
704
+ `]}};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}),a=new Promise((E)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(h)=>t.execute(h).then((P)=>E(P))})});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 m({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 ae=(e,t)=>{return e.sortKey.localeCompare(t.sortKey)};class A{commands;schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.commands=[]}channel(e){return new S({schema:this.schema,channelName:e,registerFn:(t)=>{this.commands.push(t)}})}async execute(e){let t=this.adaptor(e.databaseClient);for(let a of this.commands.sort(ae))await a.execute(t)}}class p{schema;channelName;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}async create(e){let t=this.adaptor(e.databaseClient);return new o({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(t)}}class D{schema;adaptor;channelName;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,this.channelName=e.channelName}set(e){let t=this.adaptor(e.databaseClient);return new m({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(t)}clear(e){let t=this.adaptor(e.databaseClient);return new l({schema:this.schema,channelName:this.channelName}).execute(t)}}class y{policy;message;constructor(e){this.message=new p({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName}),this.policy=new D({schema:e.schema,adaptor:e.adaptor,channelName:e.channelName})}}class M{schema;adaptor;id;isUnlocked;channelName;name;content;state;numAttempts;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor,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){let t=this.adaptor(e.databaseClient);return new d({schema:this.schema,id:this.id,numAttempts:this.numAttempts,delayMs:e.delayMs,state:e.state}).execute(t)}async delete(e){let t=this.adaptor(e.databaseClient);return new u({schema:this.schema,numAttempts:this.numAttempts,id:this.id}).execute(t)}async heartbeat(e){let t=this.adaptor(e.databaseClient);return new T({schema:this.schema,id:this.id,numAttempts:this.numAttempts}).execute(t)}}class B{schema;adaptor;constructor(e){this.schema=e.schema,this.adaptor=e.adaptor?e.adaptor:(t)=>t}async dequeue(e){let t=new i({schema:this.schema}),a=this.adaptor(e.databaseClient),E=await t.execute(a);if(E.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new M({schema:this.schema,adaptor:this.adaptor,id:E.message.id,channelName:E.message.channelName,name:E.message.name,isUnlocked:E.message.isUnlocked,content:E.message.content,state:E.message.state,numAttempts:E.message.numAttempts})};else return E}channel(e){return new y({adaptor:this.adaptor,schema:this.schema,channelName:e})}batch(){return new A({schema:this.schema,adaptor:this.adaptor})}migrations(e={}){return[L,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)=>I(t.value))}}export{z as queueEventDecode,B as Queue,T as MessageHeartbeatCommand,i as MessageDequeueCommand,u as MessageDeleteCommand,d as MessageDeferCommand,o as MessageCreateCommand,m as ChannelPolicySetCommand,l as ChannelPolicyClearCommand};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lonnymq",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {