@lunora/platform-node 1.0.0-alpha.42 → 1.0.0-alpha.44
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/conformance/index.mjs +1 -1
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/{createNodePlatform-DZ710ybu.mjs → createNodePlatform-BGoxuZCi.mjs} +1 -1
- package/dist/packem_shared/createNodeShardHost-B76kaXz-.mjs +1 -0
- package/dist/packem_shared/{createNodeShardRegistry-ZWGlgv0E.mjs → createNodeShardRegistry-Dj1mXStS.mjs} +1 -1
- package/dist/packem_shared/createNodeShardState-D6XMWbFs.mjs +1 -0
- package/dist/packem_shared/createNodeSocketHost-DQs85G7W.mjs +2 -0
- package/package.json +7 -7
- package/dist/packem_shared/createNodeShardHost-CNoG3Fzy.mjs +0 -1
- package/dist/packem_shared/createNodeShardState-CpUzvWwd.mjs +0 -1
- package/dist/packem_shared/createNodeSocketHost-CoE4VZbB.mjs +0 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createNodeShardKvStore as N}from"../packem_shared/createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeSchedulerHost as k}from"../packem_shared/createNodeSchedulerHost-BEAX6I4m.mjs";import{createNodeShardHost as D}from"../packem_shared/createNodeShardHost-
|
|
1
|
+
import{createNodeShardKvStore as N}from"../packem_shared/createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeSchedulerHost as k}from"../packem_shared/createNodeSchedulerHost-BEAX6I4m.mjs";import{createNodeShardHost as D}from"../packem_shared/createNodeShardHost-B76kaXz-.mjs";import{createNodeShardRegistry as H}from"../packem_shared/createNodeShardRegistry-Dj1mXStS.mjs";import{createNodeSocketHost as T}from"../packem_shared/createNodeSocketHost-DQs85G7W.mjs";const P=()=>{const{database:o,dispose:m,host:p}=D(),h=N(o),c=H(),{directory:l}=c,{readFrames:w,restoreSocket:u,simulateRecycle:S,socket:y}=T(o),d=new Set,s=new Map,{dispose:f,scheduler:i,simulateDeadLetter:g}=k(o,{onDispatch:(e,a,t)=>{d.add(t.id),s.set(e,(s.get(e)??0)+1)}}),n=()=>{f(),m(),c.close()};return{awaitAlarmFired:async e=>{await new Promise(a=>{setTimeout(a,Math.max(0,e-Date.now())+30)})},awaitJobDispatched:async e=>{const t=(await i.list?.())?.find(r=>r.id===e);return t!==void 0&&await new Promise(r=>{setTimeout(r,Math.max(0,t.scheduledFor-Date.now())+30)}),d.has(e)},cleanup:n,cronTicks:e=>s.get(e)??0,directory:l,disposeTerminally:n,kv:h,readFrames:w,restoreSocket:u,scheduler:i,shard:p,simulateDeadLetter:g,simulateRecycle:S,socket:y}};export{P as createNodeConformanceHost};
|
package/dist/index.d.mts
CHANGED
|
@@ -197,9 +197,12 @@ interface NodeShardHostOptions {
|
|
|
197
197
|
* `_lunora_alarm` when the host is constructed over an existing database,
|
|
198
198
|
* including an alarm whose time elapsed while nothing was running.
|
|
199
199
|
*
|
|
200
|
-
* A handler that throws is isolated to its own delivery
|
|
200
|
+
* A handler that throws is isolated to its own delivery — it never reaches
|
|
201
201
|
* the caller that set the alarm, because by then that call has long
|
|
202
|
-
* returned
|
|
202
|
+
* returned — and the wakeup is then re-delivered with backoff up to
|
|
203
|
+
* {@link ALARM_RETRY_LIMIT} times, the way workerd retries a throwing
|
|
204
|
+
* `alarm()`. Delivery is at-least-once, so the handler must tolerate
|
|
205
|
+
* running twice for one scheduled timestamp.
|
|
203
206
|
*/
|
|
204
207
|
onAlarm?: () => Promise<void> | void;
|
|
205
208
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -197,9 +197,12 @@ interface NodeShardHostOptions {
|
|
|
197
197
|
* `_lunora_alarm` when the host is constructed over an existing database,
|
|
198
198
|
* including an alarm whose time elapsed while nothing was running.
|
|
199
199
|
*
|
|
200
|
-
* A handler that throws is isolated to its own delivery
|
|
200
|
+
* A handler that throws is isolated to its own delivery — it never reaches
|
|
201
201
|
* the caller that set the alarm, because by then that call has long
|
|
202
|
-
* returned
|
|
202
|
+
* returned — and the wakeup is then re-delivered with backoff up to
|
|
203
|
+
* {@link ALARM_RETRY_LIMIT} times, the way workerd retries a throwing
|
|
204
|
+
* `alarm()`. Delivery is at-least-once, so the handler must tolerate
|
|
205
|
+
* running twice for one scheduled timestamp.
|
|
203
206
|
*/
|
|
204
207
|
onAlarm?: () => Promise<void> | void;
|
|
205
208
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createNodeGlobalStore as r,createNodeSqlExec as t}from"./packem_shared/createNodeGlobalStore-Cwi33XOk.mjs";import{createNodeShardKvStore as d}from"./packem_shared/createNodeShardKvStore-DVi1OsvH.mjs";import{createNodePlatform as f}from"./packem_shared/createNodePlatform-
|
|
1
|
+
import{createNodeGlobalStore as r,createNodeSqlExec as t}from"./packem_shared/createNodeGlobalStore-Cwi33XOk.mjs";import{createNodeShardKvStore as d}from"./packem_shared/createNodeShardKvStore-DVi1OsvH.mjs";import{createNodePlatform as f}from"./packem_shared/createNodePlatform-BGoxuZCi.mjs";import{createNodeQueueHost as x}from"./packem_shared/createNodeQueueHost--mBLGSxF.mjs";import{createNodeR2Bucket as p}from"./packem_shared/createNodeR2Bucket-BzNH1lBp.mjs";import{createNodeSchedulerHost as l}from"./packem_shared/createNodeSchedulerHost-BEAX6I4m.mjs";import{createNodeShardHost as h}from"./packem_shared/createNodeShardHost-B76kaXz-.mjs";import{createNodeShardRegistry as k}from"./packem_shared/createNodeShardRegistry-Dj1mXStS.mjs";import{createNodeShardState as w}from"./packem_shared/createNodeShardState-D6XMWbFs.mjs";import{createNodeSocketHost as W}from"./packem_shared/createNodeSocketHost-DQs85G7W.mjs";import{createNodeWorkflowHost as g}from"./packem_shared/createNodeWorkflowHost-CTVBYiuj.mjs";import{createNodeWorkflowStore as q}from"./packem_shared/createNodeWorkflowStore-CL7RYP6H.mjs";export{r as createNodeGlobalStore,f as createNodePlatform,x as createNodeQueueHost,p as createNodeR2Bucket,l as createNodeSchedulerHost,h as createNodeShardHost,d as createNodeShardKvStore,k as createNodeShardRegistry,w as createNodeShardState,W as createNodeSocketHost,t as createNodeSqlExec,g as createNodeWorkflowHost,q as createNodeWorkflowStore};
|
package/dist/packem_shared/{createNodePlatform-DZ710ybu.mjs → createNodePlatform-BGoxuZCi.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as n}from"@lunora/errors";import{NODE_CAPABILITIES as S}from"@lunora/platform";import{createNodeShardKvStore as N}from"./createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeQueueHost as p}from"./createNodeQueueHost--mBLGSxF.mjs";import{createNodeR2Bucket as k}from"./createNodeR2Bucket-BzNH1lBp.mjs";import{createNodeSchedulerHost as v}from"./createNodeSchedulerHost-BEAX6I4m.mjs";import{createNodeShardHost as b}from"./createNodeShardHost-
|
|
1
|
+
import{LunoraError as n}from"@lunora/errors";import{NODE_CAPABILITIES as S}from"@lunora/platform";import{createNodeShardKvStore as N}from"./createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeQueueHost as p}from"./createNodeQueueHost--mBLGSxF.mjs";import{createNodeR2Bucket as k}from"./createNodeR2Bucket-BzNH1lBp.mjs";import{createNodeSchedulerHost as v}from"./createNodeSchedulerHost-BEAX6I4m.mjs";import{createNodeShardHost as b}from"./createNodeShardHost-B76kaXz-.mjs";import{createNodeShardRegistry as g}from"./createNodeShardRegistry-Dj1mXStS.mjs";import{createNodeSocketHost as y}from"./createNodeSocketHost-DQs85G7W.mjs";import{createNodeWorkflowHost as q}from"./createNodeWorkflowHost-CTVBYiuj.mjs";import{createNodeWorkflowStore as B}from"./createNodeWorkflowStore-CL7RYP6H.mjs";const T=(e={})=>{const{database:o,dispose:c,drain:s,host:a}=b(e),d=N(o),r=g(e),{directory:u}=r,{socket:i}=y(o),{dispose:m,scheduler:f}=v(o,e),h=e.queues===void 0?void 0:p(o,{onBatch:e.onQueueBatch??(()=>{throw new n("VALIDATION_ERROR","@lunora/platform-node: createNodePlatform was given `queues` without `onQueueBatch`, so a delivered batch has nowhere to go — pass dispatchQueueBatch from @lunora/queue")}),queues:e.queues}),l=e.objectStorageDirectory===void 0?void 0:k({directory:e.objectStorageDirectory}),w=e.workflows===void 0?void 0:q({store:B(o),workflows:e.workflows}),t=()=>{m(),c(),r.close()};return{capabilities:S,close:t,directory:u,drain:s,kv:d,objectStorage:l,queues:h,scheduler:f,shard:a,sockets:i,workflows:w,[Symbol.dispose]:t}};export{T as createNodePlatform};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AsyncLocalStorage as S}from"node:async_hooks";import{LunoraError as _}from"@lunora/errors";import v from"better-sqlite3";const w=2147483647,g=6,y=100,L=e=>e===void 0?null:e,R=(e,t)=>({get databaseSize(){const c=e.pragma("page_count",{simple:!0}),r=e.pragma("page_size",{simple:!0});return c*r},exec:(c,...r)=>{t("run SQL");const a=e.prepare(c),s=r.map(u=>L(u)),o=a.reader?a.all(...s):(a.run(...s),[]);return{[Symbol.iterator]:()=>o[Symbol.iterator](),one:()=>{if(o.length!==1)throw new Error(`expected exactly one row, got ${String(o.length)}`);return o[0]},toArray:()=>[...o]}}}),O=(e,t,c)=>{let r,a,s=0,o;e.exec("CREATE TABLE IF NOT EXISTS _lunora_alarm (id INTEGER PRIMARY KEY CHECK (id = 0), scheduled_for INTEGER NOT NULL)");const u=n=>{n===void 0?e.prepare("DELETE FROM _lunora_alarm WHERE id = 0").run():e.prepare("INSERT INTO _lunora_alarm (id, scheduled_for) VALUES (0, ?) ON CONFLICT (id) DO UPDATE SET scheduled_for = excluded.scheduled_for").run(n)},p=()=>{a!==void 0&&(clearTimeout(a),a=void 0)},m=n=>{p();const d=Math.max(0,n-Date.now());if(d>w){a=setTimeout(()=>{m(n)},w);return}a=setTimeout(()=>{r=void 0,a=void 0,e.open&&(u(void 0),(async()=>{await c?.(),s=0})().catch(()=>{if(!e.open||r!==void 0)return;if(s+=1,s>g){s=0;return}const f=Date.now()+y*2**(s-1);r=f,u(f),m(f)}))},d)},h={delete:()=>{if(!e.open)throw new Error("platform closed: cannot delete an alarm");if(t.assertOwnTurn("delete an alarm"),s=0,u(void 0),t.inside()){o={at:void 0};return}r=void 0,p()},get:()=>(o===void 0?r:o.at)??null,set:n=>{if(!e.open)throw new Error("platform closed: cannot set an alarm");t.assertOwnTurn("set an alarm");const d=typeof n=="number"?n:n.getTime();if(s=0,u(d),t.inside()){o={at:d};return}r=d,m(d)}},T=()=>{if(o===void 0)return;const{at:n}=o;if(o=void 0,n===void 0){r=void 0,p();return}r=n,m(n)},A=()=>{o=void 0},E=e.prepare("SELECT scheduled_for FROM _lunora_alarm WHERE id = 0").get();return E!==void 0&&(r=E.scheduled_for,m(E.scheduled_for)),{alarms:h,commit:T,dispose:p,rollback:A}},z=(e={})=>{const t=new v(e.path??":memory:");t.pragma("journal_mode = WAL");const c=new S;let r=!1;const a=l=>{if(r&&c.getStore()!==!0)throw new _("SHARD_UNAVAILABLE",`shard busy: cannot ${l} while another task holds this shard's transaction`)},s=R(t,a),{alarms:o,commit:u,dispose:p,rollback:m}=O(t,{assertOwnTurn:a,inside:()=>c.getStore()===!0},e.onAlarm),h=new Set;let T=Promise.resolve();const A=l=>{const i=T.then(l,l);return T=i.then(()=>{},()=>{}),i};let E=Promise.resolve();const n=async l=>{t.exec("BEGIN"),r=!0;try{const i=await c.run(!0,l);return t.exec("COMMIT"),r=!1,u(),i}catch(i){r=!1,m();try{t.exec("ROLLBACK")}catch{}throw i}},d=l=>{const i=E.then(()=>n(l),()=>n(l));return E=i.then(()=>{},()=>{}),i},f={alarms:o,runSerialized:A,shardKey:e.shardKey,sql:s,transaction:d,waitUntil:l=>{const i=l.catch(()=>{}).finally(()=>{h.delete(i)});h.add(i)}};return{database:t,dispose:()=>{p(),t.open&&t.close()},drain:async()=>{for(;h.size>0;)await Promise.all(h)},host:f}};export{z as createNodeShardHost};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mkdirSync as y,readdirSync as S}from"node:fs";import{join as h}from"node:path";import{LunoraError as l}from"@lunora/errors";import{createNodeShardKvStore as v}from"./createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeShardHost as w}from"./createNodeShardHost-
|
|
1
|
+
import{mkdirSync as y,readdirSync as S}from"node:fs";import{join as h}from"node:path";import{LunoraError as l}from"@lunora/errors";import{createNodeShardKvStore as v}from"./createNodeShardKvStore-DVi1OsvH.mjs";import{createNodeShardHost as w}from"./createNodeShardHost-B76kaXz-.mjs";import{createNodeSocketHost as A}from"./createNodeSocketHost-DQs85G7W.mjs";const c=".sqlite3",f=r=>r.replaceAll(/[^\da-z._-]/gu,t=>[...Buffer.from(t,"utf8")].map(o=>`%${o.toString(16).padStart(2,"0").toUpperCase()}`).join("")),g=r=>decodeURIComponent(r),$=(r,t)=>{const o=t.slice(0,-c.length),d=h(r,t);let a;try{a=g(o)}catch{throw new l("SHARD_UNAVAILABLE",`@lunora/platform-node: shard database "${d}" has a basename that is not valid percent-encoding, so the shard key it holds cannot be recovered. Rename or remove the file.`)}const e=`${f(a)}${c}`;if(e!==t)throw new l("SHARD_UNAVAILABLE",`@lunora/platform-node: shard database "${d}" was written under an older shard-key encoding. It decodes to shard key "${a}", which this build stores as "${e}" — so the key would be listed for fan-out while every read opened a new, empty database. Rename it: mv "${d}" "${h(r,e)}"`);return a},I=(r={})=>{const t=new Map,o=new Set;if(r.directory!==void 0){y(r.directory,{recursive:!0});for(const e of S(r.directory))e.endsWith(c)&&o.add($(r.directory,e))}const d=e=>{const s=t.get(e);if(s!==void 0)return s.shard;const i=r.directory===void 0?void 0:h(r.directory,`${f(e)}${c}`);let n;const{database:m,dispose:u,host:p}=w({onAlarm:()=>r.onAlarm?.(n),path:i,shardKey:e});return n={kv:v(m),shard:p,shardKey:e,sockets:A(m).socket},t.set(e,{dispose:u,shard:n}),o.add(e),n},a=e=>({fetch:async s=>{const i=d(e);return r.onFetch===void 0?new Response(e):r.onFetch(s,i)}});return{close:()=>{for(const e of t.values())e.dispose();t.clear()},directory:{get:e=>a(String(e)),getByName:e=>a(e),idForName:e=>e},listShardKeys:()=>[...o],shardFor:d}};export{I as createNodeShardRegistry};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const c=t=>({acceptWebSocket:(e,a)=>{t.sockets.accept(e,void 0,a)},blockConcurrencyWhile:e=>t.shard.runSerialized(e),getWebSockets:e=>t.sockets.getSockets(e),id:{name:t.shardKey},storage:{delete:e=>t.kv.delete(e),deleteAlarm:async()=>{await t.shard.alarms.delete()},get:e=>t.kv.get(e),getAlarm:async()=>t.shard.alarms.get(),list:e=>t.kv.list(e),put:(e,a)=>t.kv.put(e,a),setAlarm:async e=>{await t.shard.alarms.set(e)},sql:{get databaseSize(){return t.shard.sql.databaseSize},exec:(e,...a)=>t.shard.sql.exec(e,...a)},transaction:e=>t.shard.transaction(e)},waitUntil:e=>{t.shard.waitUntil?.(e)}});export{c as createNodeShardState};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{deserialize as A,serialize as l}from"node:v8";import{t as T}from"./to-array-buffer-DVyXU4Ep.mjs";const p=s=>typeof s=="object"&&s!==null||typeof s=="function",F=s=>{s.exec("CREATE TABLE IF NOT EXISTS _lunora_sockets (id TEXT PRIMARY KEY, attachment BLOB, tags TEXT NOT NULL DEFAULT '[]')");const f=s.prepare(`INSERT INTO _lunora_sockets (id, attachment, tags) VALUES (?, ?, ?)
|
|
2
|
+
ON CONFLICT (id) DO UPDATE SET attachment = excluded.attachment, tags = excluded.tags`),v=s.prepare("UPDATE _lunora_sockets SET attachment = ? WHERE id = ?"),S=s.prepare("UPDATE _lunora_sockets SET tags = ? WHERE id = ?"),k=s.prepare("SELECT attachment, tags FROM _lunora_sockets WHERE id = ?"),w=s.prepare("DELETE FROM _lunora_sockets WHERE id = ?"),i=new Map,a=new WeakMap;let d=new WeakMap;const u=(t,e)=>{s.open&&v.run(e===void 0?null:l(e),t)},g=(t,e)=>{s.open&&S.run(JSON.stringify([...e]),t)},h=t=>{const e=t;e.closed=!0,i.delete(e.id),p(e.raw)&&d.delete(e.raw),s.open&&w.run(e.id)},m=(t,e)=>{const o=t;return o.handle=e,a.set(e,o.id),e},R=(t,e)=>{const o=t,n=e,c=typeof n.close=="function"?n.close.bind(e):void 0;return typeof n.send!="function"&&(n.send=r=>{o.received.push(typeof r=="string"?r:T(r))}),n.close=(r,y)=>{h(o),c?.(r,y)},n.deserializeAttachment=()=>o.attachment,n.serializeAttachment=r=>{o.attachment=r,u(o.id,r)},m(o,e)},E=t=>{const e=t;return m(e,{close:(n,c)=>{h(e)},deserializeAttachment:()=>e.attachment,send:n=>{e.received.push(typeof n=="string"?n:T(n))},serializeAttachment:n=>{e.attachment=n,u(e.id,n)}})};return{readFrames:t=>(i.get(a.get(t)??"")?.received??[]).filter(e=>typeof e=="string"),restoreSocket:(t,e)=>{const o=s.open?k.get(t):void 0,n=o?.attachment===null?void 0:o?.attachment,c={attachment:n===void 0?e:A(n),closed:!1,handle:void 0,id:t,raw:void 0,received:[],tags:new Set(o===void 0?[]:JSON.parse(o.tags))};return i.set(t,c),o===void 0&&s.open&&f.run(t,c.attachment===void 0?null:l(c.attachment),JSON.stringify([...c.tags])),E(c)},simulateRecycle:()=>{i.clear(),d=new WeakMap},socket:{accept:(t,e,o)=>{if(!s.open)throw new Error("platform closed: cannot accept a socket");const n=crypto.randomUUID(),c=new Set(o);f.run(n,e===void 0?null:l(e),JSON.stringify([...c]));const r={attachment:e,closed:!1,handle:void 0,id:n,raw:t,received:[],tags:c};return i.set(n,r),p(t)?(d.set(t,r),R(r,t)):E(r)},getSockets:t=>{const e=[...i.values()];return(t===void 0?e:e.filter(n=>n.tags.has(t))).map(n=>n.handle)},handleFor:t=>p(t)?d.get(t)?.handle:void 0,idFor:t=>{const e=a.get(t);if(e===void 0)throw new Error("@lunora/platform-node: idFor called with a handle this host never issued");return e},removeTag:(t,e)=>{if(!s.open)throw new Error("platform closed: cannot remove a tag");const o=i.get(a.get(t)??"");o!==void 0&&(e===void 0?o.tags.clear():o.tags.delete(e),g(o.id,o.tags))},setTag:(t,e)=>{if(!s.open)throw new Error("platform closed: cannot set a tag");const o=i.get(a.get(t)??"");o!==void 0&&(o.tags.add(e),g(o.id,o.tags))}}}};export{F as createNodeSocketHost};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/platform-node",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.44",
|
|
4
4
|
"description": "Node implementation of the Lunora platform contracts: ShardHost, SocketHost, ShardDirectory, ShardKvStore, SchedulerHost over better-sqlite3 and an in-process socket registry (dev/test target)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"better-sqlite3",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@lunora/d1": "1.0.0-alpha.
|
|
50
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
51
|
-
"@lunora/platform": "1.0.0-alpha.
|
|
52
|
-
"@lunora/queue": "1.0.0-alpha.
|
|
53
|
-
"@lunora/sql-store": "1.0.0-alpha.
|
|
54
|
-
"@lunora/workflow": "1.0.0-alpha.
|
|
49
|
+
"@lunora/d1": "1.0.0-alpha.104",
|
|
50
|
+
"@lunora/errors": "1.0.0-alpha.30",
|
|
51
|
+
"@lunora/platform": "1.0.0-alpha.25",
|
|
52
|
+
"@lunora/queue": "1.0.0-alpha.43",
|
|
53
|
+
"@lunora/sql-store": "1.0.0-alpha.106",
|
|
54
|
+
"@lunora/workflow": "1.0.0-alpha.43",
|
|
55
55
|
"@visulima/workflow": "3.0.0",
|
|
56
56
|
"better-sqlite3": "^13.0.3",
|
|
57
57
|
"cron-parser": "5.8.1"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import E from"better-sqlite3";const p=2147483647,T=e=>e===void 0?null:e,f=e=>({get databaseSize(){const r=e.pragma("page_count",{simple:!0}),s=e.pragma("page_size",{simple:!0});return r*s},exec:(r,...s)=>{const o=e.prepare(r),l=s.map(i=>T(i)),t=o.reader?o.all(...l):(o.run(...l),[]);return{[Symbol.iterator]:()=>t[Symbol.iterator](),one:()=>{if(t.length!==1)throw new Error(`expected exactly one row, got ${String(t.length)}`);return t[0]},toArray:()=>[...t]}}}),_=(e,r)=>{let s,o;e.exec("CREATE TABLE IF NOT EXISTS _lunora_alarm (id INTEGER PRIMARY KEY CHECK (id = 0), scheduled_for INTEGER NOT NULL)");const l=n=>{n===void 0?e.prepare("DELETE FROM _lunora_alarm WHERE id = 0").run():e.prepare("INSERT INTO _lunora_alarm (id, scheduled_for) VALUES (0, ?) ON CONFLICT (id) DO UPDATE SET scheduled_for = excluded.scheduled_for").run(n)},t=()=>{o!==void 0&&(clearTimeout(o),o=void 0)},i=n=>{t();const d=Math.max(0,n-Date.now());if(d>p){o=setTimeout(()=>{i(n)},p);return}o=setTimeout(()=>{s=void 0,o=void 0,e.open&&(l(void 0),(async()=>{await r?.()})().catch(()=>{}))},d)},m={delete:()=>{if(!e.open)throw new Error("platform closed: cannot delete an alarm");s=void 0,t(),l(void 0)},get:()=>s??null,set:n=>{if(!e.open)throw new Error("platform closed: cannot set an alarm");const d=typeof n=="number"?n:n.getTime();s=d,l(d),i(d)}},u=e.prepare("SELECT scheduled_for FROM _lunora_alarm WHERE id = 0").get();return u!==void 0&&(s=u.scheduled_for,i(u.scheduled_for)),{alarms:m,dispose:t}},v=(e={})=>{const r=new E(e.path??":memory:");r.pragma("journal_mode = WAL");const s=f(r),{alarms:o,dispose:l}=_(r,e.onAlarm),t=new Set;let i=Promise.resolve();const m=c=>{const a=i.then(c,c);return i=a.then(()=>{},()=>{}),a};let u=Promise.resolve();const n=async c=>{r.exec("BEGIN");try{const a=await c();return r.exec("COMMIT"),a}catch(a){try{r.exec("ROLLBACK")}catch{}throw a}},d=c=>{const a=u.then(()=>n(c),()=>n(c));return u=a.then(()=>{},()=>{}),a},h={alarms:o,runSerialized:m,shardKey:e.shardKey,sql:s,transaction:d,waitUntil:c=>{const a=c.catch(()=>{}).finally(()=>{t.delete(a)});t.add(a)}};return{database:r,dispose:()=>{l(),r.open&&r.close()},drain:async()=>{for(;t.size>0;)await Promise.all(t)},host:h}};export{v as createNodeShardHost};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const s=t=>({acceptWebSocket:(e,a)=>{const c=t.sockets.accept(e,void 0,a),i=e;i.serializeAttachment=l=>{c.serializeAttachment(l)},i.deserializeAttachment=()=>c.deserializeAttachment()},blockConcurrencyWhile:e=>t.shard.runSerialized(e),getWebSockets:e=>t.sockets.getSockets(e),id:{name:t.shardKey},storage:{delete:e=>t.kv.delete(e),deleteAlarm:async()=>{await t.shard.alarms.delete()},get:e=>t.kv.get(e),getAlarm:async()=>t.shard.alarms.get(),list:e=>t.kv.list(e),put:(e,a)=>t.kv.put(e,a),setAlarm:async e=>{await t.shard.alarms.set(e)},sql:{get databaseSize(){return t.shard.sql.databaseSize},exec:(e,...a)=>t.shard.sql.exec(e,...a)},transaction:e=>t.shard.transaction(e)},waitUntil:e=>{t.shard.waitUntil?.(e)}});export{s as createNodeShardState};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{deserialize as w,serialize as l}from"node:v8";import{t as S}from"./to-array-buffer-DVyXU4Ep.mjs";const p=n=>typeof n=="object"&&n!==null||typeof n=="function",A=n=>{n.exec("CREATE TABLE IF NOT EXISTS _lunora_sockets (id TEXT PRIMARY KEY, attachment BLOB, tags TEXT NOT NULL DEFAULT '[]')");const g=n.prepare(`INSERT INTO _lunora_sockets (id, attachment, tags) VALUES (?, ?, ?)
|
|
2
|
-
ON CONFLICT (id) DO UPDATE SET attachment = excluded.attachment, tags = excluded.tags`),h=n.prepare("UPDATE _lunora_sockets SET attachment = ? WHERE id = ?"),E=n.prepare("UPDATE _lunora_sockets SET tags = ? WHERE id = ?"),m=n.prepare("SELECT attachment, tags FROM _lunora_sockets WHERE id = ?"),T=n.prepare("DELETE FROM _lunora_sockets WHERE id = ?"),c=new Map,i=new WeakMap;let a=new WeakMap;const v=(t,e)=>{n.open&&h.run(e===void 0?null:l(e),t)},f=(t,e)=>{n.open&&E.run(JSON.stringify([...e]),t)},u=t=>{const e=t,o={close:(r,s)=>{e.closed=!0,c.delete(e.id),p(e.raw)&&a.delete(e.raw),n.open&&T.run(e.id)},deserializeAttachment:()=>e.attachment,send:r=>{e.received.push(typeof r=="string"?r:S(r))},serializeAttachment:r=>{e.attachment=r,v(e.id,r)}};return e.handle=o,i.set(o,e.id),o};return{readFrames:t=>(c.get(i.get(t)??"")?.received??[]).filter(e=>typeof e=="string"),restoreSocket:(t,e)=>{const o=n.open?m.get(t):void 0,r=o?.attachment===null?void 0:o?.attachment,s={attachment:r===void 0?e:w(r),closed:!1,handle:void 0,id:t,raw:void 0,received:[],tags:new Set(o===void 0?[]:JSON.parse(o.tags))};return c.set(t,s),o===void 0&&n.open&&g.run(t,s.attachment===void 0?null:l(s.attachment),JSON.stringify([...s.tags])),u(s)},simulateRecycle:()=>{c.clear(),a=new WeakMap},socket:{accept:(t,e,o)=>{if(!n.open)throw new Error("platform closed: cannot accept a socket");const r=crypto.randomUUID(),s=new Set(o);g.run(r,e===void 0?null:l(e),JSON.stringify([...s]));const d={attachment:e,closed:!1,handle:void 0,id:r,raw:t,received:[],tags:s};return c.set(r,d),p(t)&&a.set(t,d),u(d)},getSockets:t=>{const e=[...c.values()];return(t===void 0?e:e.filter(r=>r.tags.has(t))).map(r=>r.handle)},handleFor:t=>p(t)?a.get(t)?.handle:void 0,idFor:t=>{const e=i.get(t);if(e===void 0)throw new Error("@lunora/platform-node: idFor called with a handle this host never issued");return e},removeTag:(t,e)=>{if(!n.open)throw new Error("platform closed: cannot remove a tag");const o=c.get(i.get(t)??"");o!==void 0&&(e===void 0?o.tags.clear():o.tags.delete(e),f(o.id,o.tags))},setTag:(t,e)=>{if(!n.open)throw new Error("platform closed: cannot set a tag");const o=c.get(i.get(t)??"");o!==void 0&&(o.tags.add(e),f(o.id,o.tags))}}}};export{A as createNodeSocketHost};
|