lonnymq 0.0.8 → 0.0.9

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,16 +1,112 @@
1
- var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=Object.prototype.hasOwnProperty;var y=new WeakMap,J=(e)=>{var n=y.get(e),s;if(n)return n;if(n=d({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")V(e).map((_)=>!K.call(n,_)&&d(n,_,{get:()=>e[_],enumerable:!(s=X(e,_))||s.enumerable}));return y.set(e,n),n};var j=(e,n)=>{for(var s in n)d(e,s,{get:n[s],enumerable:!0,configurable:!0,set:(_)=>n[s]=()=>_})};var re={};j(re,{Queue:()=>x,MessageDequeueCommand:()=>i,MessageDeleteCommand:()=>l,MessageDeferCommand:()=>h,MessageCreateCommand:()=>o,ChannelPolicySetCommand:()=>m,ChannelPolicyClearCommand:()=>r});module.exports=J(re);var E=(e)=>({nodeType:"VALUE",value:e}),t=(e)=>({nodeType:"REF",value:e}),Z=(e)=>({nodeType:"RAW",value:e}),ee=(e)=>{return`'${e.replace(/'/g,"''")}'`},ne=(e)=>{if(e===null)return"NULL";else if(typeof e==="string")return ee(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}`)},te=(e)=>{return`"${e.replace(/"/g,'""')}"`},se=(e)=>{if(e.nodeType==="VALUE")return ne(e.value);else if(e.nodeType==="REF")return te(e.value);else if(e.nodeType==="RAW")return e.value;else throw new Error("Unsupported SQL node type")};var a=(e,...n)=>{let s=[];for(let _=0;_<e.length;_+=1)if(s.push(e[_]),_<n.length)s.push(se(n[_]));return Z(s.join(""))};class r{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
- SELECT 1 FROM ${t(this.schema)}."channel_policy_clear"(
1
+ var{defineProperty:h,getOwnPropertyNames:z,getOwnPropertyDescriptor:V}=Object,W=Object.prototype.hasOwnProperty;var v=new WeakMap,Y=(e)=>{var t=v.get(e),n;if(t)return t;if(t=h({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")z(e).map((_)=>!W.call(t,_)&&h(t,_,{get:()=>e[_],enumerable:!(n=V(e,_))||n.enumerable}));return v.set(e,t),t};var X=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0,configurable:!0,set:(_)=>t[n]=()=>_})};var me={};X(me,{queueEventDecode:()=>O,Queue:()=>L,MessageDequeueCommand:()=>o,MessageDeleteCommand:()=>u,MessageDeferCommand:()=>i,MessageCreateCommand:()=>c,ChannelPolicySetCommand:()=>m,ChannelPolicyClearCommand:()=>l});module.exports=Y(me);var r=(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 a=(e,...t)=>{let n=[];for(let _=0;_<e.length;_+=1)if(n.push(e[_]),_<t.length)n.push(ee(t[_]));return J(n.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
+ SELECT 1 FROM ${s(this.schema)}."channel_policy_clear"(
3
3
  $1
4
4
  )
5
- `.value,[this.channelName])}}class m{schema;channelName;maxSize;maxConcurrency;releaseIntervalMs;createdAt;constructor(e){this.schema=e.schema,this.channelName=e.channelName;let n=e.maxConcurrency??null;this.maxConcurrency=n!==null?Math.max(0,n):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
- SELECT 1 FROM ${t(this.schema)}."channel_policy_set"(
5
+ `.value,[this.channelName])}}class m{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 n=e.maxSize??null;this.maxSize=n!==null?Math.max(0,n):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
+ SELECT 1 FROM ${s(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 R=(e)=>{return e*1000},ae=(e)=>{return R(e*60)},f=(e)=>{return ae(e*60)};var C=require("node:crypto");class p{value;constructor(e){this.value=e}toString(e){return C.createHash("sha256").update(e).update(this.value).digest("base64").replace(/=/g,"")}}var u=new p("WAKE"),q=!1,N=R(0),$=f(1);var g=require("path"),__filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",_e=g.dirname(g.dirname(__filename)),ce=new RegExp(`^${_e}/`),c=(e)=>{return e.replace(ce,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-create.ts";var F={name:c(__filename),sql:(e)=>{return[a`
13
- CREATE FUNCTION ${t(e.schema)}."message_create" (
12
+ `.value,[this.channelName,this.maxSize,this.maxConcurrency,this.releaseIntervalMs])}}var g=(e)=>{return e*1000},te=(e)=>{return g(e*60)},I=(e)=>{return te(e*60)};var d=g(0),U=I(1);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 x=require("node:crypto");class c{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let t=e.name??null,n=e.lockMs===void 0?U:Math.max(0,e.lockMs),_=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=n,this.delayMs=_,this.createdAt=new Date}async execute(e){let t=await e.query(a`
13
+ SELECT * FROM ${s(this.schema)}."message_create"(
14
+ $1,
15
+ $2,
16
+ $3,
17
+ $4,
18
+ $5::INTEGER,
19
+ $6::INTEGER
20
+ )
21
+ `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((n)=>n.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`
22
+ SELECT
23
+ result_code,
24
+ metadata,
25
+ content,
26
+ state
27
+ FROM ${s(this.schema)}."message_dequeue"()
28
+ `.value,[]).then((n)=>n.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,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
+ SELECT * FROM ${s(this.schema)}."message_delete"(
30
+ $1
31
+ )
32
+ `.value,[this.id]).then((n)=>n.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 i{schema;id;delayMs;state;constructor(e){let t=e.delayMs===void 0?d: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
+ SELECT * FROM ${s(this.schema)}."message_defer"(
34
+ $1,
35
+ $2,
36
+ $3
37
+ )
38
+ `.value,[this.id,this.delayMs,this.state]).then((n)=>n.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 q=(e)=>{let t=e.split(`
39
+ `),n=Number.MAX_SAFE_INTEGER;for(let _ of t){if(_.trim().length===0)continue;let T=_.search(/\S/);n=Math.min(n,T)}return t.map((_)=>_.slice(n)).join(`
40
+ `).trim()};var N=require("path"),__filename="/home/runner/work/lonnymq/lonnymq/src/core/path.ts",_e=N.dirname(N.dirname(__filename)),Ee=new RegExp(`^${_e}/`),E=(e)=>{return e.replace(Ee,"")};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",G={name:E(__filename),sql:(e)=>{return[a`
41
+ CREATE TABLE ${s(e.schema)}."channel_policy" (
42
+ "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
43
+ "name" TEXT NOT NULL,
44
+ "max_size" INTEGER,
45
+ "max_concurrency" INTEGER,
46
+ "release_interval_ms" INTEGER,
47
+ PRIMARY KEY ("id")
48
+ );
49
+ `,a`
50
+ CREATE UNIQUE INDEX "channel_policy_name_ux"
51
+ ON ${s(e.schema)}."channel_policy" ("name");
52
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/01-table-channel-state.ts",F={name:E(__filename),sql:(e)=>{return[a`
53
+ CREATE TABLE ${s(e.schema)}."channel_state" (
54
+ "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
55
+ "name" TEXT NOT NULL,
56
+ "max_size" INTEGER,
57
+ "max_concurrency" INTEGER,
58
+ "release_interval_ms" INTEGER,
59
+ "current_size" INTEGER NOT NULL,
60
+ "current_concurrency" INTEGER NOT NULL,
61
+ "message_next_id" UUID,
62
+ "message_next_seq_no" BIGINT,
63
+ "message_next_dequeue_after" TIMESTAMP,
64
+ "message_last_dequeued_at" TIMESTAMP,
65
+ PRIMARY KEY ("id")
66
+ );
67
+ `,a`
68
+ CREATE UNIQUE INDEX "channel_state_name_ux"
69
+ ON ${s(e.schema)}."channel_state" ("name");
70
+ `,a`
71
+ CREATE INDEX "channel_state_dequeue_ix"
72
+ ON ${s(e.schema)}."channel_state" (
73
+ "message_next_dequeue_after" ASC
74
+ ) WHERE "message_next_id" IS NOT NULL
75
+ AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency");
76
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/02-table-message.ts",$={name:E(__filename),sql:(e)=>{return[a`
77
+ CREATE TABLE ${s(e.schema)}."message" (
78
+ "id" UUID NOT NULL,
79
+ "channel_name" TEXT NOT NULL,
80
+ "seq_no" BIGSERIAL NOT NULL,
81
+ "name" TEXT,
82
+ "content" BYTEA NOT NULL,
83
+ "state" BYTEA,
84
+ "lock_ms" INTEGER NOT NULL,
85
+ "is_locked" BOOLEAN NOT NULL DEFAULT FALSE,
86
+ "num_attempts" INTEGER NOT NULL DEFAULT 0,
87
+ "dequeue_after" TIMESTAMP NOT NULL,
88
+ PRIMARY KEY ("id")
89
+ );
90
+ `,a`
91
+ CREATE UNIQUE INDEX "message_name_ux"
92
+ ON ${s(e.schema)}."message" (
93
+ "channel_name",
94
+ "name"
95
+ ) WHERE "num_attempts" = 0
96
+ `,a`
97
+ CREATE INDEX "message_dequeue_ix"
98
+ ON ${s(e.schema)}."message" (
99
+ "channel_name",
100
+ "dequeue_after" ASC,
101
+ "seq_no" ASC
102
+ ) WHERE NOT "is_locked";
103
+ `,a`
104
+ CREATE INDEX "message_locked_dequeue_ix"
105
+ ON ${s(e.schema)}."message" (
106
+ "dequeue_after" ASC
107
+ ) WHERE "is_locked";
108
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/03-function-message-create.ts",P={name:E(__filename),sql:(e)=>{return[a`
109
+ CREATE FUNCTION ${s(e.schema)}."message_create" (
14
110
  p_id UUID,
15
111
  p_channel_name TEXT,
16
112
  p_name TEXT,
@@ -22,10 +118,10 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
22
118
  ) AS $$
23
119
  DECLARE
24
120
  v_now TIMESTAMP;
121
+ v_dequeue_after TIMESTAMP;
25
122
  v_channel_policy RECORD;
26
123
  v_channel_state RECORD;
27
124
  v_message RECORD;
28
- v_message_next_dequeue_after TIMESTAMP;
29
125
  BEGIN
30
126
  v_now := NOW();
31
127
 
@@ -33,12 +129,12 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
33
129
  "channel_policy"."max_size",
34
130
  "channel_policy"."max_concurrency",
35
131
  "channel_policy"."release_interval_ms"
36
- FROM ${t(e.schema)}."channel_policy"
132
+ FROM ${s(e.schema)}."channel_policy"
37
133
  WHERE "name" = p_channel_name
38
134
  FOR SHARE
39
135
  INTO v_channel_policy;
40
136
 
41
- INSERT INTO ${t(e.schema)}."channel_state" (
137
+ INSERT INTO ${s(e.schema)}."channel_state" (
42
138
  "name",
43
139
  "current_size",
44
140
  "current_concurrency",
@@ -64,6 +160,8 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
64
160
  "current_concurrency",
65
161
  "max_size",
66
162
  "max_concurrency",
163
+ "release_interval_ms",
164
+ "message_last_dequeued_at",
67
165
  "message_next_id",
68
166
  "message_next_dequeue_after",
69
167
  "message_next_seq_no"
@@ -71,11 +169,11 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
71
169
 
72
170
  IF v_channel_state."current_size" >= v_channel_policy."max_size" THEN
73
171
  RETURN QUERY SELECT
74
- ${E(1)};
172
+ ${r(1)};
75
173
  RETURN;
76
174
  END IF;
77
175
 
78
- INSERT INTO ${t(e.schema)}."message" (
176
+ INSERT INTO ${s(e.schema)}."message" (
79
177
  "id",
80
178
  "channel_name",
81
179
  "name",
@@ -102,45 +200,52 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
102
200
 
103
201
  IF v_message."id" != p_id THEN
104
202
  RETURN QUERY SELECT
105
- ${E(2)};
203
+ ${r(2)};
106
204
  RETURN;
107
205
  END IF;
108
206
 
207
+ v_dequeue_after := GREATEST(
208
+ v_now,
209
+ v_channel_state."message_last_dequeued_at"
210
+ + INTERVAL '1 MILLISECOND' * COALESCE(v_channel_state."release_interval_ms", 0),
211
+ v_message."dequeue_after"
212
+ );
213
+
109
214
  IF
110
215
  v_channel_state."message_next_id" IS NULL OR
111
- v_channel_state."message_next_dequeue_after" > v_message."dequeue_after" OR
112
- (v_channel_state."message_next_dequeue_after" = v_message."dequeue_after" AND v_channel_state."message_next_seq_no" > v_message."seq_no")
216
+ v_channel_state."message_next_dequeue_after" > v_dequeue_after OR
217
+ (v_channel_state."message_next_dequeue_after" = v_dequeue_after AND v_channel_state."message_next_seq_no" > v_message."seq_no")
113
218
  THEN
114
- UPDATE ${t(e.schema)}."channel_state" SET
219
+ UPDATE ${s(e.schema)}."channel_state" SET
115
220
  "current_size" = v_channel_state."current_size" + 1,
116
221
  "message_next_id" = v_message."id",
117
- "message_next_dequeue_after" = GREATEST(v_now, v_message."dequeue_after"),
222
+ "message_next_dequeue_after" = v_dequeue_after,
118
223
  "message_next_seq_no" = v_message."seq_no"
119
224
  WHERE "id" = v_channel_state."id";
120
225
  ELSE
121
- UPDATE ${t(e.schema)}."channel_state" SET
226
+ UPDATE ${s(e.schema)}."channel_state" SET
122
227
  "current_size" = v_channel_state."current_size" + 1
123
228
  WHERE "id" = v_channel_state."id";
124
229
  END IF;
125
230
 
126
- PERFORM ${t(e.schema)}."wake"(GREATEST(0, p_delay_ms));
231
+ IF ${r(e.eventChannel!==null)} THEN
232
+ PERFORM PG_NOTIFY(
233
+ ${r(e.eventChannel)},
234
+ JSON_BUILD_OBJECT(
235
+ 'type', ${r(0)},
236
+ 'id', p_id,
237
+ 'delay_ms', p_delay_ms
238
+ )::TEXT
239
+ );
240
+ END IF;
127
241
 
128
242
  RETURN QUERY SELECT
129
- ${E(0)};
243
+ ${r(0)};
130
244
  RETURN;
131
245
  END;
132
246
  $$ LANGUAGE plpgsql;
133
- `]}};var M=require("node:crypto");class o{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let n=e.name??null,s=e.lockMs===void 0?$:Math.max(0,e.lockMs),_=e.delayMs===void 0?N:e.delayMs;this.id=M.randomUUID(),this.schema=e.schema,this.channelName=e.channelName,this.content=e.content,this.name=n,this.lockMs=s,this.delayMs=_,this.createdAt=new Date}async execute(e){let n=await e.query(a`
134
- SELECT * FROM ${t(this.schema)}."message_create"(
135
- $1,
136
- $2,
137
- $3,
138
- $4,
139
- $5::INTEGER,
140
- $6::INTEGER
141
- )
142
- `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((s)=>s.rows[0]);if(n.result_code===1)return{resultType:"MESSAGE_DROPPED"};else if(n.result_code===2)return{resultType:"MESSAGE_DEDUPLICATED"};else if(n.result_code===0)return{resultType:"MESSAGE_CREATED"};else throw new Error("Unexpected result")}}var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/05-function-message-dequeue.ts";var G={name:c(__filename),sql:(e)=>{return[a`
143
- CREATE FUNCTION ${t(e.schema)}."message_dequeue" ()
247
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/04-function-message-dequeue.ts",b={name:E(__filename),sql:(e)=>{return[a`
248
+ CREATE FUNCTION ${s(e.schema)}."message_dequeue" ()
144
249
  RETURNS TABLE (
145
250
  result_code INTEGER,
146
251
  content BYTEA,
@@ -167,7 +272,7 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
167
272
  "message"."lock_ms",
168
273
  "message"."dequeue_after",
169
274
  "message"."num_attempts"
170
- FROM ${t(e.schema)}."message"
275
+ FROM ${s(e.schema)}."message"
171
276
  WHERE "is_locked"
172
277
  ORDER BY "dequeue_after" ASC
173
278
  FOR UPDATE
@@ -176,15 +281,13 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
176
281
  INTO v_message_locked;
177
282
 
178
283
  IF v_message_locked."dequeue_after" <= v_now THEN
179
- UPDATE ${t(e.schema)}."message" SET
284
+ UPDATE ${s(e.schema)}."message" SET
180
285
  "num_attempts" = v_message_locked."num_attempts" + 1,
181
286
  "dequeue_after" = v_now + (v_message_locked."lock_ms" * INTERVAL '1 millisecond')
182
287
  WHERE "id" = v_message_locked."id";
183
288
 
184
- PERFORM ${t(e.schema)}."wake"(v_message_locked."lock_ms");
185
-
186
289
  RETURN QUERY SELECT
187
- ${E(1)},
290
+ ${r(1)},
188
291
  v_message_locked.content,
189
292
  v_message_locked.state,
190
293
  JSON_BUILD_OBJECT(
@@ -203,7 +306,7 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
203
306
  "channel_state"."message_next_id",
204
307
  "channel_state"."message_next_dequeue_after",
205
308
  "channel_state"."current_concurrency"
206
- FROM ${t(e.schema)}."channel_state"
309
+ FROM ${s(e.schema)}."channel_state"
207
310
  WHERE "message_next_id" IS NOT NULL
208
311
  AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency")
209
312
  ORDER BY "message_next_dequeue_after" ASC
@@ -219,7 +322,7 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
219
322
  );
220
323
 
221
324
  RETURN QUERY SELECT
222
- ${E(0)},
325
+ ${r(0)},
223
326
  NULL::BYTEA,
224
327
  NULL::BYTEA,
225
328
  JSON_BUILD_OBJECT(
@@ -236,23 +339,21 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
236
339
  "message"."num_attempts",
237
340
  "message"."state",
238
341
  "message"."lock_ms"
239
- FROM ${t(e.schema)}."message"
342
+ FROM ${s(e.schema)}."message"
240
343
  WHERE "id" = v_channel_state."message_next_id"
241
344
  INTO v_message_dequeue;
242
345
 
243
- UPDATE ${t(e.schema)}."message" SET
346
+ UPDATE ${s(e.schema)}."message" SET
244
347
  "is_locked" = TRUE,
245
348
  "num_attempts" = v_message_dequeue."num_attempts" + 1,
246
349
  "dequeue_after" = v_now + (v_message_dequeue."lock_ms" * INTERVAL '1 millisecond')
247
350
  WHERE "id" = v_message_dequeue."id";
248
351
 
249
- PERFORM ${t(e.schema)}."wake"(v_message_dequeue."lock_ms");
250
-
251
352
  SELECT
252
353
  "message"."id",
253
354
  "message"."dequeue_after",
254
355
  "message"."seq_no"
255
- FROM ${t(e.schema)}."message"
356
+ FROM ${s(e.schema)}."message"
256
357
  WHERE NOT "is_locked"
257
358
  AND "channel_name" = v_message_dequeue."channel_name"
258
359
  ORDER BY "dequeue_after" ASC, "seq_no" ASC
@@ -265,22 +366,24 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
265
366
  v_now + (COALESCE(v_channel_state."release_interval_ms", 0) * INTERVAL '1 millisecond')
266
367
  );
267
368
 
268
- UPDATE ${t(e.schema)}."channel_state" SET
369
+ UPDATE ${s(e.schema)}."channel_state" SET
269
370
  "current_concurrency" = v_channel_state."current_concurrency" + 1,
270
371
  "message_next_id" = v_message_next_dequeue."id",
271
372
  "message_next_dequeue_after" = v_message_next_dequeue_after,
272
- "message_next_seq_no" = v_message_next_dequeue."seq_no"
373
+ "message_next_seq_no" = v_message_next_dequeue."seq_no",
374
+ "message_last_dequeued_at" = v_now
273
375
  WHERE "id" = v_channel_state."id";
274
376
  ELSE
275
- UPDATE ${t(e.schema)}."channel_state" SET
377
+ UPDATE ${s(e.schema)}."channel_state" SET
276
378
  "current_concurrency" = v_channel_state."current_concurrency" + 1,
379
+ "message_last_dequeued_at" = v_now,
277
380
  "message_next_id" = NULL
278
381
  WHERE "id" = v_channel_state."id";
279
382
  END IF;
280
383
 
281
384
 
282
385
  RETURN QUERY SELECT
283
- ${E(1)},
386
+ ${r(1)},
284
387
  v_message_dequeue.content,
285
388
  v_message_dequeue.state,
286
389
  JSON_BUILD_OBJECT(
@@ -292,15 +395,8 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
292
395
  RETURN;
293
396
  END;
294
397
  $$ LANGUAGE plpgsql;
295
- `]}};class i{schema;constructor(e){this.schema=e.schema}async execute(e){let n=await e.query(a`
296
- SELECT
297
- result_code,
298
- metadata,
299
- content,
300
- state
301
- FROM ${t(this.schema)}."message_dequeue"()
302
- `.value,[]).then((s)=>s.rows[0]);if(n.result_code===0)return{resultType:"MESSAGE_NOT_AVAILABLE",retryMs:n.metadata.retry_ms};else if(n.result_code===1)return{resultType:"MESSAGE_DEQUEUED",message:{id:n.metadata.id,channelName:n.metadata.channel_name,name:n.metadata.name,content:n.content,state:n.state,numAttempts:n.metadata.num_attempts}};else throw new Error("Unexpected dequeue result")}}var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/06-function-message-delete.ts";var B={name:c(__filename),sql:(e)=>{return[a`
303
- CREATE FUNCTION ${t(e.schema)}."message_delete" (
398
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/05-function-message-delete.ts",B={name:E(__filename),sql:(e)=>{return[a`
399
+ CREATE FUNCTION ${s(e.schema)}."message_delete" (
304
400
  p_id UUID
305
401
  )
306
402
  RETURNS TABLE (
@@ -315,24 +411,24 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
315
411
  "message"."id",
316
412
  "message"."channel_name",
317
413
  "message"."is_locked"
318
- FROM ${t(e.schema)}."message"
414
+ FROM ${s(e.schema)}."message"
319
415
  WHERE "id" = p_id
320
416
  FOR UPDATE
321
417
  INTO v_message;
322
418
 
323
419
  IF v_message."id" IS NULL THEN
324
420
  RETURN QUERY SELECT
325
- ${E(0)};
421
+ ${r(0)};
326
422
  RETURN;
327
423
  ELSIF NOT v_message."is_locked" THEN
328
424
  RETURN QUERY SELECT
329
- ${E(1)};
425
+ ${r(1)};
330
426
  RETURN;
331
427
  END IF;
332
428
 
333
429
  SELECT
334
430
  "channel_policy"."id"
335
- FROM ${t(e.schema)}."channel_policy"
431
+ FROM ${s(e.schema)}."channel_policy"
336
432
  WHERE "name" = v_message."channel_name"
337
433
  FOR SHARE
338
434
  INTO v_channel_policy;
@@ -341,35 +437,41 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
341
437
  "channel_state"."id",
342
438
  "channel_state"."current_size",
343
439
  "channel_state"."current_concurrency"
344
- FROM ${t(e.schema)}."channel_state"
440
+ FROM ${s(e.schema)}."channel_state"
345
441
  WHERE "name" = v_message."channel_name"
346
442
  FOR UPDATE
347
443
  INTO v_channel_state;
348
444
 
349
445
  IF v_channel_policy."id" IS NULL AND v_channel_state."current_size" = 1 THEN
350
- DELETE FROM ${t(e.schema)}."channel_state"
446
+ DELETE FROM ${s(e.schema)}."channel_state"
351
447
  WHERE "id" = v_channel_state."id";
352
448
  ELSE
353
- UPDATE ${t(e.schema)}."channel_state" SET
449
+ UPDATE ${s(e.schema)}."channel_state" SET
354
450
  "current_concurrency" = v_channel_state."current_concurrency" - 1,
355
451
  "current_size" = v_channel_state."current_size" - 1
356
452
  WHERE "id" = v_channel_state."id";
357
453
  END IF;
358
454
 
359
- DELETE FROM ${t(e.schema)}."message"
455
+ IF ${r(e.eventChannel!==null)} THEN
456
+ PERFORM PG_NOTIFY(
457
+ ${r(e.eventChannel)},
458
+ JSON_BUILD_OBJECT(
459
+ 'type', ${r(1)},
460
+ 'id', p_id
461
+ )::TEXT
462
+ );
463
+ END IF;
464
+
465
+ DELETE FROM ${s(e.schema)}."message"
360
466
  WHERE "id" = p_id;
361
467
 
362
- RETURN QUERY SELECT
363
- ${E(2)};
364
- RETURN;
468
+ RETURN QUERY SELECT
469
+ ${r(2)};
470
+ RETURN;
365
471
  END;
366
472
  $$ LANGUAGE plpgsql;
367
- `]}};class l{schema;id;constructor(e){this.schema=e.schema,this.id=e.id}async execute(e){let n=await e.query(a`
368
- SELECT * FROM ${t(this.schema)}."message_delete"(
369
- $1
370
- )
371
- `.value,[this.id]).then((s)=>s.rows[0]);if(n.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(n.result_code===1)return{resultType:"STATE_INVALID"};else if(n.result_code===2)return{resultType:"MESSAGE_DELETED"};else throw new Error("Unexpected result")}}var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-message-defer.ts";var P={name:c(__filename),sql:(e)=>{return[a`
372
- CREATE FUNCTION ${t(e.schema)}."message_defer" (
473
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/06-function-message-defer.ts",Q={name:E(__filename),sql:(e)=>{return[a`
474
+ CREATE FUNCTION ${s(e.schema)}."message_defer" (
373
475
  p_id UUID,
374
476
  p_delay_ms INTEGER,
375
477
  p_state BYTEA
@@ -378,166 +480,101 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
378
480
  result_code INTEGER
379
481
  ) AS $$
380
482
  DECLARE
483
+ v_now TIMESTAMP;
381
484
  v_channel_state RECORD;
382
485
  v_dequeue_after TIMESTAMP;
383
486
  v_message RECORD;
384
487
  BEGIN
488
+ v_now := NOW();
489
+
385
490
  SELECT
386
491
  "message"."id",
387
492
  "message"."channel_name",
388
493
  "message"."is_locked",
389
494
  "message"."seq_no"
390
- FROM ${t(e.schema)}."message"
495
+ FROM ${s(e.schema)}."message"
391
496
  WHERE "id" = p_id
392
497
  FOR UPDATE
393
498
  INTO v_message;
394
499
 
395
500
  IF v_message."id" IS NULL THEN
396
501
  RETURN QUERY SELECT
397
- ${E(0)};
502
+ ${r(0)};
398
503
  RETURN;
399
504
  ELSIF NOT v_message."is_locked" THEN
400
505
  RETURN QUERY SELECT
401
- ${E(1)};
506
+ ${r(1)};
402
507
  RETURN;
403
508
  END IF;
404
509
 
405
510
  SELECT
406
511
  "channel_state"."current_concurrency",
512
+ "channel_state"."release_interval_ms",
407
513
  "channel_state"."message_next_id",
408
514
  "channel_state"."message_next_dequeue_after",
515
+ "channel_state"."message_last_dequeued_at",
409
516
  "channel_state"."message_next_seq_no"
410
- FROM ${t(e.schema)}."channel_state"
517
+ FROM ${s(e.schema)}."channel_state"
411
518
  WHERE "name" = v_message."channel_name"
412
519
  FOR UPDATE
413
520
  INTO v_channel_state;
414
521
 
415
522
  v_dequeue_after := NOW() + INTERVAL '1 MILLISECOND' * p_delay_ms;
416
523
 
524
+ v_dequeue_after := GREATEST(
525
+ v_now,
526
+ v_now + INTERVAL '1 MILLISECOND' * p_delay_ms,
527
+ v_channel_state."message_last_dequeued_at"
528
+ );
529
+
417
530
  IF
418
531
  v_channel_state."message_next_id" IS NULL OR
419
532
  v_channel_state."message_next_dequeue_after" > v_dequeue_after OR
420
533
  (v_channel_state."message_next_dequeue_after" = v_dequeue_after AND v_channel_state."message_next_seq_no" > v_message."seq_no")
421
534
  THEN
422
- UPDATE ${t(e.schema)}."channel_state" SET
535
+ UPDATE ${s(e.schema)}."channel_state" SET
423
536
  "current_concurrency" = v_channel_state."current_concurrency" - 1,
424
537
  "message_next_id" = v_message."id",
425
538
  "message_next_dequeue_after" = v_dequeue_after,
426
539
  "message_next_seq_no" = v_message."seq_no"
427
540
  WHERE "name" = v_message."channel_name";
428
541
  ELSE
429
- UPDATE ${t(e.schema)}."channel_state" SET
542
+ UPDATE ${s(e.schema)}."channel_state" SET
430
543
  "current_concurrency" = v_channel_state."current_concurrency" - 1
431
544
  WHERE "name" = v_message."channel_name";
432
545
  END IF;
433
546
 
434
- UPDATE ${t(e.schema)}."message" SET
547
+ UPDATE ${s(e.schema)}."message" SET
435
548
  "state" = p_state,
436
549
  "is_locked" = FALSE,
437
550
  "dequeue_after" = v_dequeue_after
438
551
  WHERE "id" = p_id;
439
552
 
440
- PERFORM ${t(e.schema)}."wake"(GREATEST(0, p_delay_ms));
441
-
442
- RETURN QUERY SELECT
443
- ${E(2)};
444
- RETURN;
445
- END;
446
- $$ LANGUAGE plpgsql;
447
- `]}};class h{schema;id;delayMs;state;constructor(e){let n=e.delayMs===void 0?N:e.delayMs;this.schema=e.schema,this.id=e.id,this.delayMs=n,this.state=e.state??null}async execute(e){let n=await e.query(a`
448
- SELECT * FROM ${t(this.schema)}."message_defer"(
449
- $1,
450
- $2,
451
- $3
452
- )
453
- `.value,[this.id,this.delayMs,this.state]).then((s)=>s.rows[0]);if(n.result_code===0)return{resultType:"MESSAGE_NOT_FOUND"};else if(n.result_code===1)return{resultType:"STATE_INVALID"};else if(n.result_code===2)return{resultType:"MESSAGE_DEFERRED"};else throw new Error("Unexpected result")}}var H=(e)=>{let n=e.split(`
454
- `),s=Number.MAX_SAFE_INTEGER;for(let _ of n){if(_.trim().length===0)continue;let T=_.search(/\S/);s=Math.min(s,T)}return n.map((_)=>_.slice(s)).join(`
455
- `).trim()};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",k={name:c(__filename),sql:(e)=>{return[a`
456
- CREATE TABLE ${t(e.schema)}."channel_policy" (
457
- "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
458
- "name" TEXT NOT NULL,
459
- "max_size" INTEGER,
460
- "max_concurrency" INTEGER,
461
- "release_interval_ms" INTEGER,
462
- PRIMARY KEY ("id")
463
- );
464
- `,a`
465
- CREATE UNIQUE INDEX "channel_policy_name_ux"
466
- ON ${t(e.schema)}."channel_policy" ("name");
467
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/01-table-channel-state.ts",b={name:c(__filename),sql:(e)=>{return[a`
468
- CREATE TABLE ${t(e.schema)}."channel_state" (
469
- "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
470
- "name" TEXT NOT NULL,
471
- "max_size" INTEGER,
472
- "max_concurrency" INTEGER,
473
- "release_interval_ms" INTEGER,
474
- "current_size" INTEGER NOT NULL,
475
- "current_concurrency" INTEGER NOT NULL,
476
- "message_next_id" UUID,
477
- "message_next_dequeue_after" TIMESTAMP,
478
- "message_next_seq_no" BIGINT,
479
- PRIMARY KEY ("id")
480
- );
481
- `,a`
482
- CREATE UNIQUE INDEX "channel_state_name_ux"
483
- ON ${t(e.schema)}."channel_state" ("name");
484
- `,a`
485
- CREATE INDEX "channel_state_dequeue_ix"
486
- ON ${t(e.schema)}."channel_state" (
487
- "message_next_dequeue_after" ASC
488
- ) WHERE "message_next_id" IS NOT NULL
489
- AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency");
490
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/02-table-message.ts",z={name:c(__filename),sql:(e)=>{return[a`
491
- CREATE TABLE ${t(e.schema)}."message" (
492
- "id" UUID NOT NULL,
493
- "channel_name" TEXT NOT NULL,
494
- "seq_no" BIGSERIAL NOT NULL,
495
- "name" TEXT,
496
- "content" BYTEA NOT NULL,
497
- "state" BYTEA,
498
- "lock_ms" INTEGER NOT NULL,
499
- "is_locked" BOOLEAN NOT NULL DEFAULT FALSE,
500
- "num_attempts" INTEGER NOT NULL DEFAULT 0,
501
- "dequeue_after" TIMESTAMP NOT NULL,
502
- PRIMARY KEY ("id")
503
- );
504
- `,a`
505
- CREATE UNIQUE INDEX "message_name_ux"
506
- ON ${t(e.schema)}."message" (
507
- "channel_name",
508
- "name"
509
- ) WHERE "num_attempts" = 0
510
- `,a`
511
- CREATE INDEX "message_dequeue_ix"
512
- ON ${t(e.schema)}."message" (
513
- "channel_name",
514
- "dequeue_after" ASC,
515
- "seq_no" ASC
516
- ) WHERE NOT "is_locked";
517
- `,a`
518
- CREATE INDEX "message_locked_dequeue_ix"
519
- ON ${t(e.schema)}."message" (
520
- "dequeue_after" ASC
521
- ) WHERE "is_locked";
522
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/03-function-wake.ts",w={name:c(__filename),sql:(e)=>{let n=u.toString(e.schema);return[a`
523
- CREATE FUNCTION ${t(e.schema)}."wake" (
524
- p_delay_ms INTEGER
525
- ) RETURNS VOID AS $$
526
- BEGIN
527
- IF ${E(e.useWake)} THEN
528
- PERFORM PG_NOTIFY(${E(n)}, p_delay_ms::TEXT);
553
+ IF ${r(e.eventChannel!==null)} THEN
554
+ PERFORM PG_NOTIFY(
555
+ ${r(e.eventChannel)},
556
+ JSON_BUILD_OBJECT(
557
+ 'type', ${r(2)},
558
+ 'delay_ms', p_delay_ms,
559
+ 'id', p_id
560
+ )::TEXT
561
+ );
529
562
  END IF;
563
+
564
+ RETURN QUERY SELECT
565
+ ${r(2)};
566
+ RETURN;
530
567
  END;
531
568
  $$ LANGUAGE plpgsql;
532
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-set.ts",W={name:c(__filename),sql:(e)=>{return[a`
533
- CREATE FUNCTION ${t(e.schema)}."channel_policy_set" (
569
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/07-function-channel-policy-set.ts",w={name:E(__filename),sql:(e)=>{return[a`
570
+ CREATE FUNCTION ${s(e.schema)}."channel_policy_set" (
534
571
  p_name TEXT,
535
572
  p_max_size INTEGER,
536
573
  p_max_concurrency INTEGER,
537
574
  p_release_interval_ms INTEGER
538
575
  ) RETURNS VOID AS $$
539
576
  BEGIN
540
- INSERT INTO ${t(e.schema)}."channel_policy" (
577
+ INSERT INTO ${s(e.schema)}."channel_policy" (
541
578
  "name",
542
579
  "max_size",
543
580
  "max_concurrency",
@@ -552,45 +589,41 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
552
589
  "max_concurrency" = EXCLUDED."max_concurrency",
553
590
  "release_interval_ms" = EXCLUDED."release_interval_ms";
554
591
 
555
- UPDATE ${t(e.schema)}."channel_state" SET
592
+ UPDATE ${s(e.schema)}."channel_state" SET
556
593
  "max_size" = p_max_size,
557
594
  "max_concurrency" = p_max_concurrency,
558
595
  "release_interval_ms" = p_release_interval_ms
559
596
  WHERE "name" = p_name;
560
-
561
- PERFORM ${t(e.schema)}."wake"(0);
562
597
  END;
563
598
  $$ LANGUAGE plpgsql;
564
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/09-function-channel-policy-clear.ts",Y={name:c(__filename),sql:(e)=>{return[a`
565
- CREATE FUNCTION ${t(e.schema)}."channel_policy_clear" (
599
+ `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-clear.ts",H={name:E(__filename),sql:(e)=>{return[a`
600
+ CREATE FUNCTION ${s(e.schema)}."channel_policy_clear" (
566
601
  p_name TEXT
567
602
  ) RETURNS VOID AS $$
568
603
  DECLARE
569
604
  v_channel_state RECORD;
570
605
  BEGIN
571
- DELETE FROM ${t(e.schema)}."channel_policy"
606
+ DELETE FROM ${s(e.schema)}."channel_policy"
572
607
  WHERE "name" = p_name;
573
608
 
574
609
  SELECT
575
610
  "channel_state"."id",
576
611
  "channel_state"."current_size"
577
- FROM ${t(e.schema)}."channel_state"
612
+ FROM ${s(e.schema)}."channel_state"
578
613
  WHERE "name" = p_name
579
614
  FOR UPDATE
580
615
  INTO v_channel_state;
581
616
 
582
617
  IF v_channel_state."current_size" = 0 THEN
583
- DELETE FROM ${t(e.schema)}."channel_state"
618
+ DELETE FROM ${s(e.schema)}."channel_state"
584
619
  WHERE "id" = v_channel_state."id";
585
620
  ELSE
586
- UPDATE ${t(e.schema)}."channel_state" SET
621
+ UPDATE ${s(e.schema)}."channel_state" SET
587
622
  "max_size" = NULL,
588
623
  "max_concurrency" = NULL,
589
624
  "release_interval_ms" = NULL
590
625
  WHERE "name" = p_name;
591
626
  END IF;
592
-
593
- PERFORM ${t(e.schema)}."wake"(0);
594
627
  END;
595
628
  $$ LANGUAGE plpgsql;
596
- `]}};class S{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let n=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([n.channelName,n.name,n.createdAt.toISOString()]),execute:(T)=>n.execute(T).then((Q)=>_(Q))})});return{messageId:n.id,promise:s}}}class A{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}set(e){let n=new m({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs});this.registerFn({sortKey:JSON.stringify([n.channelName,null,n.createdAt.toISOString()]),execute:async(s)=>{await n.execute(s)}})}clear(){let e=new r({schema:this.schema,channelName:this.channelName});this.registerFn({sortKey:JSON.stringify([e.channelName,null,e.createdAt.toISOString()]),execute:async(n)=>{await e.execute(n)}})}}class I{policy;message;constructor(e){this.message=new S(e),this.policy=new A(e)}}var Ee=(e,n)=>{return e.sortKey.localeCompare(n.sortKey)};class L{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new I({schema:this.schema,channelName:e,registerFn:(n)=>{this.commands.push(n)}})}async execute(e){for(let n of this.commands.sort(Ee))await n.execute(e.databaseClient)}}class v{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 U{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}set(e){return new m({schema:this.schema,channelName:this.channelName,maxConcurrency:e.maxConcurrency,maxSize:e.maxSize,releaseIntervalMs:e.releaseIntervalMs}).execute(e.databaseClient)}clear(e){return new r({schema:this.schema,channelName:this.channelName}).execute(e.databaseClient)}}class O{policy;message;constructor(e){this.message=new v({schema:e.schema,channelName:e.channelName}),this.policy=new U({schema:e.schema,channelName:e.channelName})}}class D{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 h({schema:this.schema,id:this.id,delayMs:e.delayMs,state:e.state}).execute(e.databaseClient)}async delete(e){return new l({schema:this.schema,id:this.id}).execute(e.databaseClient)}}class x{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 D({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 O({schema:this.schema,channelName:e})}batch(){return new L({schema:this.schema})}migrations(e){return[k,b,z,w,F,G,B,P,W,Y].map((n)=>({name:n.name,sql:n.sql({schema:this.schema,useWake:e.useWake??q}).map((s)=>H(s.value))})).sort((n,s)=>n.name.localeCompare(s.name))}wakeChannel(){return u.toString(this.schema)}}
629
+ `]}};class S{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let t=new c({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}),n=new Promise((_)=>{this.registerFn({sortKey:JSON.stringify([t.channelName,t.name,t.createdAt.toISOString()]),execute:(T)=>t.execute(T).then((k)=>_(k))})});return{messageId:t.id,promise:n}}}class D{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(n)=>{await t.execute(n)}})}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 y{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 A{commands;schema;constructor(e){this.commands=[],this.schema=e.schema}channel(e){return new y({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 c({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 m({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 f{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 i({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 L{schema;constructor(e){this.schema=e.schema}async dequeue(e){let n=await new o({schema:this.schema}).execute(e.databaseClient);if(n.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new f({schema:this.schema,id:n.message.id,channelName:n.message.channelName,name:n.message.name,content:n.message.content,state:n.message.state,numAttempts:n.message.numAttempts})};else return n}channel(e){return new C({schema:this.schema,channelName:e})}batch(){return new A({schema:this.schema})}migrations(e){return[G,F,$,P,b,B,Q,w,H].sort((t,n)=>t.name.localeCompare(n.name)).flatMap((t)=>t.sql({schema:this.schema,eventChannel:e.eventChannel??null})).map((t)=>q(t.value))}}