lonnymq 0.0.7 → 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 x=new WeakMap,J=(e)=>{var n=x.get(e),a;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:!(a=X(e,_))||a.enumerable}));return x.set(e,n),n};var j=(e,n)=>{for(var a in n)d(e,a,{get:n[a],enumerable:!0,configurable:!0,set:(_)=>n[a]=()=>_})};var re={};j(re,{Queue:()=>y,MessageDequeueCommand:()=>o,MessageDeleteCommand:()=>l,MessageDeferCommand:()=>h,MessageCreateCommand:()=>i,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,'""')}"`},ae=(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 s=(e,...n)=>{let a=[];for(let _=0;_<e.length;_+=1)if(a.push(e[_]),_<n.length)a.push(ae(n[_]));return Z(a.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(s`
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 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`
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},se=(e)=>{return R(e*60)},f=(e)=>{return se(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"),$=!1,N=R(0),q=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[s`
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,17 +160,20 @@ 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
- "message_next_dequeue_after"
166
+ "message_next_dequeue_after",
167
+ "message_next_seq_no"
69
168
  INTO v_channel_state;
70
169
 
71
170
  IF v_channel_state."current_size" >= v_channel_policy."max_size" THEN
72
171
  RETURN QUERY SELECT
73
- ${E(1)};
172
+ ${r(1)};
74
173
  RETURN;
75
174
  END IF;
76
175
 
77
- INSERT INTO ${t(e.schema)}."message" (
176
+ INSERT INTO ${s(e.schema)}."message" (
78
177
  "id",
79
178
  "channel_name",
80
179
  "name",
@@ -95,48 +194,58 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
95
194
  "name" = EXCLUDED."name"
96
195
  RETURNING
97
196
  "id",
197
+ "seq_no",
98
198
  "dequeue_after"
99
199
  INTO v_message;
100
200
 
101
201
  IF v_message."id" != p_id THEN
102
202
  RETURN QUERY SELECT
103
- ${E(2)};
203
+ ${r(2)};
104
204
  RETURN;
105
205
  END IF;
106
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
+
107
214
  IF
108
215
  v_channel_state."message_next_id" IS NULL OR
109
- v_channel_state."message_next_dequeue_after" > v_message."dequeue_after"
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")
110
218
  THEN
111
- UPDATE ${t(e.schema)}."channel_state" SET
219
+ UPDATE ${s(e.schema)}."channel_state" SET
112
220
  "current_size" = v_channel_state."current_size" + 1,
113
221
  "message_next_id" = v_message."id",
114
- "message_next_dequeue_after" = GREATEST(v_now, v_message."dequeue_after")
222
+ "message_next_dequeue_after" = v_dequeue_after,
223
+ "message_next_seq_no" = v_message."seq_no"
115
224
  WHERE "id" = v_channel_state."id";
116
225
  ELSE
117
- UPDATE ${t(e.schema)}."channel_state" SET
226
+ UPDATE ${s(e.schema)}."channel_state" SET
118
227
  "current_size" = v_channel_state."current_size" + 1
119
228
  WHERE "id" = v_channel_state."id";
120
229
  END IF;
121
230
 
122
- 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;
123
241
 
124
242
  RETURN QUERY SELECT
125
- ${E(0)};
243
+ ${r(0)};
126
244
  RETURN;
127
245
  END;
128
246
  $$ LANGUAGE plpgsql;
129
- `]}};var M=require("node:crypto");class i{schema;channelName;name;content;lockMs;id;delayMs;createdAt;constructor(e){let n=e.name??null,a=e.lockMs===void 0?q: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=a,this.delayMs=_,this.createdAt=new Date}async execute(e){let n=await e.query(s`
130
- SELECT * FROM ${t(this.schema)}."message_create"(
131
- $1,
132
- $2,
133
- $3,
134
- $4,
135
- $5::INTEGER,
136
- $6::INTEGER
137
- )
138
- `.value,[this.id,this.channelName,this.name,this.content,this.lockMs,this.delayMs]).then((a)=>a.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[s`
139
- 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" ()
140
249
  RETURNS TABLE (
141
250
  result_code INTEGER,
142
251
  content BYTEA,
@@ -163,7 +272,7 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
163
272
  "message"."lock_ms",
164
273
  "message"."dequeue_after",
165
274
  "message"."num_attempts"
166
- FROM ${t(e.schema)}."message"
275
+ FROM ${s(e.schema)}."message"
167
276
  WHERE "is_locked"
168
277
  ORDER BY "dequeue_after" ASC
169
278
  FOR UPDATE
@@ -172,15 +281,13 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
172
281
  INTO v_message_locked;
173
282
 
174
283
  IF v_message_locked."dequeue_after" <= v_now THEN
175
- UPDATE ${t(e.schema)}."message" SET
284
+ UPDATE ${s(e.schema)}."message" SET
176
285
  "num_attempts" = v_message_locked."num_attempts" + 1,
177
286
  "dequeue_after" = v_now + (v_message_locked."lock_ms" * INTERVAL '1 millisecond')
178
287
  WHERE "id" = v_message_locked."id";
179
288
 
180
- PERFORM ${t(e.schema)}."wake"(v_message_locked."lock_ms");
181
-
182
289
  RETURN QUERY SELECT
183
- ${E(1)},
290
+ ${r(1)},
184
291
  v_message_locked.content,
185
292
  v_message_locked.state,
186
293
  JSON_BUILD_OBJECT(
@@ -199,7 +306,7 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
199
306
  "channel_state"."message_next_id",
200
307
  "channel_state"."message_next_dequeue_after",
201
308
  "channel_state"."current_concurrency"
202
- FROM ${t(e.schema)}."channel_state"
309
+ FROM ${s(e.schema)}."channel_state"
203
310
  WHERE "message_next_id" IS NOT NULL
204
311
  AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency")
205
312
  ORDER BY "message_next_dequeue_after" ASC
@@ -215,7 +322,7 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
215
322
  );
216
323
 
217
324
  RETURN QUERY SELECT
218
- ${E(0)},
325
+ ${r(0)},
219
326
  NULL::BYTEA,
220
327
  NULL::BYTEA,
221
328
  JSON_BUILD_OBJECT(
@@ -232,25 +339,24 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
232
339
  "message"."num_attempts",
233
340
  "message"."state",
234
341
  "message"."lock_ms"
235
- FROM ${t(e.schema)}."message"
342
+ FROM ${s(e.schema)}."message"
236
343
  WHERE "id" = v_channel_state."message_next_id"
237
344
  INTO v_message_dequeue;
238
345
 
239
- UPDATE ${t(e.schema)}."message" SET
346
+ UPDATE ${s(e.schema)}."message" SET
240
347
  "is_locked" = TRUE,
241
348
  "num_attempts" = v_message_dequeue."num_attempts" + 1,
242
349
  "dequeue_after" = v_now + (v_message_dequeue."lock_ms" * INTERVAL '1 millisecond')
243
350
  WHERE "id" = v_message_dequeue."id";
244
351
 
245
- PERFORM ${t(e.schema)}."wake"(v_message_dequeue."lock_ms");
246
-
247
352
  SELECT
248
353
  "message"."id",
249
- "message"."dequeue_after"
250
- FROM ${t(e.schema)}."message"
354
+ "message"."dequeue_after",
355
+ "message"."seq_no"
356
+ FROM ${s(e.schema)}."message"
251
357
  WHERE NOT "is_locked"
252
358
  AND "channel_name" = v_message_dequeue."channel_name"
253
- ORDER BY "dequeue_after" ASC, "id" ASC
359
+ ORDER BY "dequeue_after" ASC, "seq_no" ASC
254
360
  LIMIT 1
255
361
  INTO v_message_next_dequeue;
256
362
 
@@ -260,21 +366,24 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
260
366
  v_now + (COALESCE(v_channel_state."release_interval_ms", 0) * INTERVAL '1 millisecond')
261
367
  );
262
368
 
263
- UPDATE ${t(e.schema)}."channel_state" SET
369
+ UPDATE ${s(e.schema)}."channel_state" SET
264
370
  "current_concurrency" = v_channel_state."current_concurrency" + 1,
265
371
  "message_next_id" = v_message_next_dequeue."id",
266
- "message_next_dequeue_after" = v_message_next_dequeue_after
372
+ "message_next_dequeue_after" = v_message_next_dequeue_after,
373
+ "message_next_seq_no" = v_message_next_dequeue."seq_no",
374
+ "message_last_dequeued_at" = v_now
267
375
  WHERE "id" = v_channel_state."id";
268
376
  ELSE
269
- UPDATE ${t(e.schema)}."channel_state" SET
377
+ UPDATE ${s(e.schema)}."channel_state" SET
270
378
  "current_concurrency" = v_channel_state."current_concurrency" + 1,
379
+ "message_last_dequeued_at" = v_now,
271
380
  "message_next_id" = NULL
272
381
  WHERE "id" = v_channel_state."id";
273
382
  END IF;
274
383
 
275
384
 
276
385
  RETURN QUERY SELECT
277
- ${E(1)},
386
+ ${r(1)},
278
387
  v_message_dequeue.content,
279
388
  v_message_dequeue.state,
280
389
  JSON_BUILD_OBJECT(
@@ -286,15 +395,8 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
286
395
  RETURN;
287
396
  END;
288
397
  $$ LANGUAGE plpgsql;
289
- `]}};class o{schema;constructor(e){this.schema=e.schema}async execute(e){let n=await e.query(s`
290
- SELECT
291
- result_code,
292
- metadata,
293
- content,
294
- state
295
- FROM ${t(this.schema)}."message_dequeue"()
296
- `.value,[]).then((a)=>a.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 P={name:c(__filename),sql:(e)=>{return[s`
297
- 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" (
298
400
  p_id UUID
299
401
  )
300
402
  RETURNS TABLE (
@@ -309,24 +411,24 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
309
411
  "message"."id",
310
412
  "message"."channel_name",
311
413
  "message"."is_locked"
312
- FROM ${t(e.schema)}."message"
414
+ FROM ${s(e.schema)}."message"
313
415
  WHERE "id" = p_id
314
416
  FOR UPDATE
315
417
  INTO v_message;
316
418
 
317
419
  IF v_message."id" IS NULL THEN
318
420
  RETURN QUERY SELECT
319
- ${E(0)};
421
+ ${r(0)};
320
422
  RETURN;
321
423
  ELSIF NOT v_message."is_locked" THEN
322
424
  RETURN QUERY SELECT
323
- ${E(1)};
425
+ ${r(1)};
324
426
  RETURN;
325
427
  END IF;
326
428
 
327
429
  SELECT
328
430
  "channel_policy"."id"
329
- FROM ${t(e.schema)}."channel_policy"
431
+ FROM ${s(e.schema)}."channel_policy"
330
432
  WHERE "name" = v_message."channel_name"
331
433
  FOR SHARE
332
434
  INTO v_channel_policy;
@@ -335,35 +437,41 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
335
437
  "channel_state"."id",
336
438
  "channel_state"."current_size",
337
439
  "channel_state"."current_concurrency"
338
- FROM ${t(e.schema)}."channel_state"
440
+ FROM ${s(e.schema)}."channel_state"
339
441
  WHERE "name" = v_message."channel_name"
340
442
  FOR UPDATE
341
443
  INTO v_channel_state;
342
444
 
343
445
  IF v_channel_policy."id" IS NULL AND v_channel_state."current_size" = 1 THEN
344
- DELETE FROM ${t(e.schema)}."channel_state"
446
+ DELETE FROM ${s(e.schema)}."channel_state"
345
447
  WHERE "id" = v_channel_state."id";
346
448
  ELSE
347
- UPDATE ${t(e.schema)}."channel_state" SET
449
+ UPDATE ${s(e.schema)}."channel_state" SET
348
450
  "current_concurrency" = v_channel_state."current_concurrency" - 1,
349
451
  "current_size" = v_channel_state."current_size" - 1
350
452
  WHERE "id" = v_channel_state."id";
351
453
  END IF;
352
454
 
353
- 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"
354
466
  WHERE "id" = p_id;
355
467
 
356
- RETURN QUERY SELECT
357
- ${E(2)};
358
- RETURN;
468
+ RETURN QUERY SELECT
469
+ ${r(2)};
470
+ RETURN;
359
471
  END;
360
472
  $$ LANGUAGE plpgsql;
361
- `]}};class l{schema;id;constructor(e){this.schema=e.schema,this.id=e.id}async execute(e){let n=await e.query(s`
362
- SELECT * FROM ${t(this.schema)}."message_delete"(
363
- $1
364
- )
365
- `.value,[this.id]).then((a)=>a.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 B={name:c(__filename),sql:(e)=>{return[s`
366
- 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" (
367
475
  p_id UUID,
368
476
  p_delay_ms INTEGER,
369
477
  p_state BYTEA
@@ -372,160 +480,101 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
372
480
  result_code INTEGER
373
481
  ) AS $$
374
482
  DECLARE
483
+ v_now TIMESTAMP;
375
484
  v_channel_state RECORD;
376
485
  v_dequeue_after TIMESTAMP;
377
486
  v_message RECORD;
378
487
  BEGIN
488
+ v_now := NOW();
489
+
379
490
  SELECT
380
491
  "message"."id",
381
492
  "message"."channel_name",
382
- "message"."is_locked"
383
- FROM ${t(e.schema)}."message"
493
+ "message"."is_locked",
494
+ "message"."seq_no"
495
+ FROM ${s(e.schema)}."message"
384
496
  WHERE "id" = p_id
385
497
  FOR UPDATE
386
498
  INTO v_message;
387
499
 
388
500
  IF v_message."id" IS NULL THEN
389
501
  RETURN QUERY SELECT
390
- ${E(0)};
502
+ ${r(0)};
391
503
  RETURN;
392
504
  ELSIF NOT v_message."is_locked" THEN
393
505
  RETURN QUERY SELECT
394
- ${E(1)};
506
+ ${r(1)};
395
507
  RETURN;
396
508
  END IF;
397
509
 
398
510
  SELECT
399
511
  "channel_state"."current_concurrency",
512
+ "channel_state"."release_interval_ms",
400
513
  "channel_state"."message_next_id",
401
- "channel_state"."message_next_dequeue_after"
402
- FROM ${t(e.schema)}."channel_state"
514
+ "channel_state"."message_next_dequeue_after",
515
+ "channel_state"."message_last_dequeued_at",
516
+ "channel_state"."message_next_seq_no"
517
+ FROM ${s(e.schema)}."channel_state"
403
518
  WHERE "name" = v_message."channel_name"
404
519
  FOR UPDATE
405
520
  INTO v_channel_state;
406
521
 
407
522
  v_dequeue_after := NOW() + INTERVAL '1 MILLISECOND' * p_delay_ms;
408
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
+
409
530
  IF
410
531
  v_channel_state."message_next_id" IS NULL OR
411
- v_channel_state."message_next_dequeue_after" > v_dequeue_after
532
+ v_channel_state."message_next_dequeue_after" > v_dequeue_after OR
533
+ (v_channel_state."message_next_dequeue_after" = v_dequeue_after AND v_channel_state."message_next_seq_no" > v_message."seq_no")
412
534
  THEN
413
- UPDATE ${t(e.schema)}."channel_state" SET
535
+ UPDATE ${s(e.schema)}."channel_state" SET
414
536
  "current_concurrency" = v_channel_state."current_concurrency" - 1,
415
537
  "message_next_id" = v_message."id",
416
- "message_next_dequeue_after" = v_dequeue_after
538
+ "message_next_dequeue_after" = v_dequeue_after,
539
+ "message_next_seq_no" = v_message."seq_no"
417
540
  WHERE "name" = v_message."channel_name";
418
541
  ELSE
419
- UPDATE ${t(e.schema)}."channel_state" SET
542
+ UPDATE ${s(e.schema)}."channel_state" SET
420
543
  "current_concurrency" = v_channel_state."current_concurrency" - 1
421
544
  WHERE "name" = v_message."channel_name";
422
545
  END IF;
423
546
 
424
- UPDATE ${t(e.schema)}."message" SET
547
+ UPDATE ${s(e.schema)}."message" SET
425
548
  "state" = p_state,
426
549
  "is_locked" = FALSE,
427
550
  "dequeue_after" = v_dequeue_after
428
551
  WHERE "id" = p_id;
429
552
 
430
- PERFORM ${t(e.schema)}."wake"(GREATEST(0, p_delay_ms));
431
-
432
- RETURN QUERY SELECT
433
- ${E(2)};
434
- RETURN;
435
- END;
436
- $$ LANGUAGE plpgsql;
437
- `]}};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(s`
438
- SELECT * FROM ${t(this.schema)}."message_defer"(
439
- $1,
440
- $2,
441
- $3
442
- )
443
- `.value,[this.id,this.delayMs,this.state]).then((a)=>a.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(`
444
- `),a=Number.MAX_SAFE_INTEGER;for(let _ of n){if(_.trim().length===0)continue;let T=_.search(/\S/);a=Math.min(a,T)}return n.map((_)=>_.slice(a)).join(`
445
- `).trim()};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/00-table-channel-policy.ts",k={name:c(__filename),sql:(e)=>{return[s`
446
- CREATE TABLE ${t(e.schema)}."channel_policy" (
447
- "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
448
- "name" TEXT NOT NULL,
449
- "max_size" INTEGER,
450
- "max_concurrency" INTEGER,
451
- "release_interval_ms" INTEGER,
452
- PRIMARY KEY ("id")
453
- );
454
- `,s`
455
- CREATE UNIQUE INDEX "channel_policy_name_ux"
456
- ON ${t(e.schema)}."channel_policy" ("name");
457
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/01-table-channel-state.ts",b={name:c(__filename),sql:(e)=>{return[s`
458
- CREATE TABLE ${t(e.schema)}."channel_state" (
459
- "id" UUID NOT NULL DEFAULT GEN_RANDOM_UUID(),
460
- "name" TEXT NOT NULL,
461
- "max_size" INTEGER,
462
- "max_concurrency" INTEGER,
463
- "release_interval_ms" INTEGER,
464
- "current_size" INTEGER NOT NULL,
465
- "current_concurrency" INTEGER NOT NULL,
466
- "message_next_id" UUID,
467
- "message_next_dequeue_after" TIMESTAMP,
468
- PRIMARY KEY ("id")
469
- );
470
- `,s`
471
- CREATE UNIQUE INDEX "channel_state_name_ux"
472
- ON ${t(e.schema)}."channel_state" ("name");
473
- `,s`
474
- CREATE INDEX "channel_state_dequeue_ix"
475
- ON ${t(e.schema)}."channel_state" (
476
- "message_next_dequeue_after" ASC
477
- ) WHERE "message_next_id" IS NOT NULL
478
- AND ("max_concurrency" IS NULL OR "current_concurrency" < "max_concurrency");
479
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/02-table-message.ts",z={name:c(__filename),sql:(e)=>{return[s`
480
- CREATE TABLE ${t(e.schema)}."message" (
481
- "id" UUID NOT NULL,
482
- "channel_name" TEXT NOT NULL,
483
- "name" TEXT,
484
- "content" BYTEA NOT NULL,
485
- "state" BYTEA,
486
- "lock_ms" INTEGER NOT NULL,
487
- "is_locked" BOOLEAN NOT NULL DEFAULT FALSE,
488
- "num_attempts" INTEGER NOT NULL DEFAULT 0,
489
- "dequeue_after" TIMESTAMP NOT NULL,
490
- PRIMARY KEY ("id")
491
- );
492
- `,s`
493
- CREATE UNIQUE INDEX "message_name_ux"
494
- ON ${t(e.schema)}."message" (
495
- "channel_name",
496
- "name"
497
- ) WHERE "num_attempts" = 0
498
- `,s`
499
- CREATE INDEX "message_dequeue_ix"
500
- ON ${t(e.schema)}."message" (
501
- "channel_name",
502
- "dequeue_after" ASC,
503
- "id" ASC
504
- ) WHERE NOT "is_locked";
505
- `,s`
506
- CREATE INDEX "message_locked_dequeue_ix"
507
- ON ${t(e.schema)}."message" (
508
- "dequeue_after" ASC
509
- ) WHERE "is_locked";
510
- `]}};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[s`
511
- CREATE FUNCTION ${t(e.schema)}."wake" (
512
- p_delay_ms INTEGER
513
- ) RETURNS VOID AS $$
514
- BEGIN
515
- IF ${E(e.useWake)} THEN
516
- 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
+ );
517
562
  END IF;
563
+
564
+ RETURN QUERY SELECT
565
+ ${r(2)};
566
+ RETURN;
518
567
  END;
519
568
  $$ LANGUAGE plpgsql;
520
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/08-function-channel-policy-set.ts",W={name:c(__filename),sql:(e)=>{return[s`
521
- 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" (
522
571
  p_name TEXT,
523
572
  p_max_size INTEGER,
524
573
  p_max_concurrency INTEGER,
525
574
  p_release_interval_ms INTEGER
526
575
  ) RETURNS VOID AS $$
527
576
  BEGIN
528
- INSERT INTO ${t(e.schema)}."channel_policy" (
577
+ INSERT INTO ${s(e.schema)}."channel_policy" (
529
578
  "name",
530
579
  "max_size",
531
580
  "max_concurrency",
@@ -540,45 +589,41 @@ var{defineProperty:d,getOwnPropertyNames:V,getOwnPropertyDescriptor:X}=Object,K=
540
589
  "max_concurrency" = EXCLUDED."max_concurrency",
541
590
  "release_interval_ms" = EXCLUDED."release_interval_ms";
542
591
 
543
- UPDATE ${t(e.schema)}."channel_state" SET
592
+ UPDATE ${s(e.schema)}."channel_state" SET
544
593
  "max_size" = p_max_size,
545
594
  "max_concurrency" = p_max_concurrency,
546
595
  "release_interval_ms" = p_release_interval_ms
547
596
  WHERE "name" = p_name;
548
-
549
- PERFORM ${t(e.schema)}."wake"(0);
550
597
  END;
551
598
  $$ LANGUAGE plpgsql;
552
- `]}};var __filename="/home/runner/work/lonnymq/lonnymq/src/migration/09-function-channel-policy-clear.ts",Y={name:c(__filename),sql:(e)=>{return[s`
553
- 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" (
554
601
  p_name TEXT
555
602
  ) RETURNS VOID AS $$
556
603
  DECLARE
557
604
  v_channel_state RECORD;
558
605
  BEGIN
559
- DELETE FROM ${t(e.schema)}."channel_policy"
606
+ DELETE FROM ${s(e.schema)}."channel_policy"
560
607
  WHERE "name" = p_name;
561
608
 
562
609
  SELECT
563
610
  "channel_state"."id",
564
611
  "channel_state"."current_size"
565
- FROM ${t(e.schema)}."channel_state"
612
+ FROM ${s(e.schema)}."channel_state"
566
613
  WHERE "name" = p_name
567
614
  FOR UPDATE
568
615
  INTO v_channel_state;
569
616
 
570
617
  IF v_channel_state."current_size" = 0 THEN
571
- DELETE FROM ${t(e.schema)}."channel_state"
618
+ DELETE FROM ${s(e.schema)}."channel_state"
572
619
  WHERE "id" = v_channel_state."id";
573
620
  ELSE
574
- UPDATE ${t(e.schema)}."channel_state" SET
621
+ UPDATE ${s(e.schema)}."channel_state" SET
575
622
  "max_size" = NULL,
576
623
  "max_concurrency" = NULL,
577
624
  "release_interval_ms" = NULL
578
625
  WHERE "name" = p_name;
579
626
  END IF;
580
-
581
- PERFORM ${t(e.schema)}."wake"(0);
582
627
  END;
583
628
  $$ LANGUAGE plpgsql;
584
- `]}};class S{schema;channelName;registerFn;constructor(e){this.schema=e.schema,this.channelName=e.channelName,this.registerFn=e.registerFn}create(e){let n=new i({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([n.channelName,n.name,n.createdAt.toISOString()]),execute:(T)=>n.execute(T).then((Q)=>_(Q))})});return{messageId:n.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 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(a)=>{await n.execute(a)}})}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 U{schema;channelName;constructor(e){this.schema=e.schema,this.channelName=e.channelName}async create(e){return new i({schema:this.schema,channelName:this.channelName,name:e.name,content:e.content,lockMs:e.lockMs,delayMs:e.delayMs}).execute(e.databaseClient)}}class D{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 U({schema:e.schema,channelName:e.channelName}),this.policy=new D({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 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 y{schema;constructor(e){this.schema=e}async dequeue(e){let a=await new o({schema:this.schema}).execute(e.databaseClient);if(a.resultType==="MESSAGE_DEQUEUED")return{resultType:"MESSAGE_DEQUEUED",message:new v({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 O({schema:this.schema,channelName:e})}batch(){return new L({schema:this.schema})}migrations(e){return[k,b,z,w,F,G,P,B,W,Y].map((n)=>({name:n.name,sql:n.sql({schema:this.schema,useWake:e.useWake??$}).map((a)=>H(a.value))})).sort((n,a)=>n.name.localeCompare(a.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))}}