lonnymq 0.0.4 → 0.0.5

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