@lunora/client 1.0.0-alpha.21 → 1.0.0-alpha.23

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.
Files changed (32) hide show
  1. package/dist/auth/index.d.mts +1 -1
  2. package/dist/auth/index.d.ts +1 -1
  3. package/dist/index.d.mts +249 -4
  4. package/dist/index.d.ts +249 -4
  5. package/dist/index.mjs +10 -4
  6. package/dist/packem_shared/ClientServiceWorker-C3PAFwy0.mjs +100 -0
  7. package/dist/packem_shared/{LunoraClient-kXpHNyaE.mjs → LunoraClient-BBCQjjbl.mjs} +382 -243
  8. package/dist/packem_shared/{OfflineQueue-GGYJRmhF.mjs → OfflineQueue-B4HUF7rt.mjs} +1 -1
  9. package/dist/packem_shared/SubscriptionRegistry-CxS_Inha.mjs +31 -0
  10. package/dist/packem_shared/TabCoordinator-BwRR8H06.mjs +222 -0
  11. package/dist/packem_shared/createClientQuery-CQ51bWAE.mjs +71 -0
  12. package/dist/packem_shared/createLocalStore-BtqUmOQA.mjs +2 -0
  13. package/dist/packem_shared/createReply-lI4tVS2w.mjs +36 -0
  14. package/dist/packem_shared/{createServerClient-DF-3mLmb.mjs → createServerClient-CTTAmvMx.mjs} +1 -1
  15. package/dist/packem_shared/createSnapshotPrecondition-CxQ1T4ZP.mjs +18 -0
  16. package/dist/packem_shared/httpStream-BJU-aflc.mjs +159 -0
  17. package/dist/packem_shared/{local-store-BveBeFEo.mjs → local-store-DIq-UWfD.mjs} +1 -1
  18. package/dist/packem_shared/{lunora-client.d-BYkEjCEJ.d.mts → lunora-client.d-JvtVpf8A.d.mts} +302 -18
  19. package/dist/packem_shared/{lunora-client.d-BYkEjCEJ.d.ts → lunora-client.d-JvtVpf8A.d.ts} +302 -18
  20. package/dist/packem_shared/{offline-queue-B9vfdSqp.mjs → offline-queue-CF4_Co5k.mjs} +29 -0
  21. package/dist/packem_shared/{preload.d-B-vyHnml.d.ts → preload.d-C4_d_l5v.d.ts} +1 -1
  22. package/dist/packem_shared/{preload.d-DrfuisCE.d.mts → preload.d-DKbjGN5O.d.mts} +1 -1
  23. package/dist/packem_shared/wire-key-Djie6aaR.mjs +266 -0
  24. package/dist/query/index.d.mts +2 -2
  25. package/dist/query/index.d.ts +2 -2
  26. package/dist/ssr/index.d.mts +3 -3
  27. package/dist/ssr/index.d.ts +3 -3
  28. package/dist/ssr/index.mjs +1 -1
  29. package/package.json +2 -2
  30. package/dist/packem_shared/SubscriptionRegistry-DjGKZsqq.mjs +0 -1
  31. package/dist/packem_shared/createLocalStore-jRoqmazl.mjs +0 -2
  32. package/dist/packem_shared/subscription-BjynOXCU.mjs +0 -68
@@ -0,0 +1,266 @@
1
+ const compareKeys = (a, b) => {
2
+ if (a < b) {
3
+ return -1;
4
+ }
5
+ return a > b ? 1 : 0;
6
+ };
7
+ const stableStringify = (value) => {
8
+ if (value === void 0) {
9
+ return "null";
10
+ }
11
+ if (typeof value === "bigint") {
12
+ throw new TypeError("stableStringify: cannot use a bigint in a stable JSON cache key — pass it as a string, or use stableWireKey");
13
+ }
14
+ if (value === null || typeof value !== "object") {
15
+ return JSON.stringify(value);
16
+ }
17
+ if (Array.isArray(value)) {
18
+ return `[${value.map((item) => stableStringify(item)).join(",")}]`;
19
+ }
20
+ const proto = Object.getPrototypeOf(value);
21
+ if (proto !== null && proto !== Object.prototype) {
22
+ const name = value.constructor?.name ?? "value";
23
+ throw new TypeError(
24
+ `stableStringify: cannot use a ${name} in a stable JSON cache key — only plain objects, arrays, and JSON primitives are supported (wire-typed values key via stableWireKey)`
25
+ );
26
+ }
27
+ const record = value;
28
+ const keys = Object.keys(record).toSorted(compareKeys);
29
+ const parts = [];
30
+ for (const key of keys) {
31
+ const raw = record[key];
32
+ if (raw === void 0) {
33
+ continue;
34
+ }
35
+ parts.push(`${JSON.stringify(key)}:${stableStringify(raw)}`);
36
+ }
37
+ return `{${parts.join(",")}}`;
38
+ };
39
+
40
+ const toBase64 = (bytes) => {
41
+ let binary = "";
42
+ const chunk = 32768;
43
+ for (let index = 0; index < bytes.length; index += chunk) {
44
+ binary += String.fromCharCode(...bytes.subarray(index, index + chunk));
45
+ }
46
+ return btoa(binary);
47
+ };
48
+ const fromBase64 = (base64) => {
49
+ const binary = atob(base64);
50
+ const bytes = new Uint8Array(binary.length);
51
+ for (let index = 0; index < binary.length; index += 1) {
52
+ bytes[index] = binary.codePointAt(index) ?? 0;
53
+ }
54
+ return bytes;
55
+ };
56
+
57
+ const TAG = "$lunora.wire$";
58
+ const MAX_DEPTH = 64;
59
+ const MAX_BIGINT_DIGITS = 1024;
60
+ const UNSAFE_KEY = "__proto__";
61
+ const TYPED_ARRAY_CTORS = {
62
+ BigInt64Array,
63
+ BigUint64Array,
64
+ Float32Array,
65
+ Float64Array,
66
+ Int8Array,
67
+ Int16Array,
68
+ Int32Array,
69
+ Uint8Array,
70
+ Uint8ClampedArray,
71
+ Uint16Array,
72
+ Uint32Array
73
+ };
74
+ const ERROR_CTORS = {
75
+ Error,
76
+ EvalError,
77
+ RangeError,
78
+ ReferenceError,
79
+ SyntaxError,
80
+ TypeError,
81
+ URIError
82
+ };
83
+ const encodeWire = (value, depth = 0) => {
84
+ if (depth > MAX_DEPTH) {
85
+ throw new RangeError(`wire-codec: value nesting exceeds the ${MAX_DEPTH}-level limit`);
86
+ }
87
+ if (value === void 0) {
88
+ return [TAG, "undefined"];
89
+ }
90
+ if (value === null) {
91
+ return null;
92
+ }
93
+ const kind = typeof value;
94
+ if (kind === "bigint") {
95
+ return [TAG, "bigint", value.toString()];
96
+ }
97
+ if (kind === "number") {
98
+ const numeric = value;
99
+ if (Number.isNaN(numeric)) {
100
+ return [TAG, "nan"];
101
+ }
102
+ if (numeric === Infinity) {
103
+ return [TAG, "inf"];
104
+ }
105
+ if (numeric === -Infinity) {
106
+ return [TAG, "-inf"];
107
+ }
108
+ return numeric;
109
+ }
110
+ if (kind !== "object") {
111
+ return value;
112
+ }
113
+ if (value instanceof Date) {
114
+ return [TAG, "date", encodeWire(value.getTime(), depth + 1)];
115
+ }
116
+ if (value instanceof Error) {
117
+ const error = value;
118
+ const properties = {};
119
+ for (const key of Object.keys(error)) {
120
+ if (error[key] !== void 0) {
121
+ properties[key] = encodeWire(error[key], depth + 1);
122
+ }
123
+ }
124
+ const encodedError = [TAG, "error", error.name, error.message, properties];
125
+ if (error.cause !== void 0) {
126
+ encodedError.push(encodeWire(error.cause, depth + 1));
127
+ }
128
+ return encodedError;
129
+ }
130
+ if (value instanceof URL) {
131
+ return [TAG, "url", value.href];
132
+ }
133
+ if (value instanceof Map) {
134
+ return [TAG, "map", [...value.entries()].map(([k, v]) => [encodeWire(k, depth + 1), encodeWire(v, depth + 1)])];
135
+ }
136
+ if (value instanceof Set) {
137
+ return [TAG, "set", [...value].map((item) => encodeWire(item, depth + 1))];
138
+ }
139
+ if (value instanceof ArrayBuffer) {
140
+ return [TAG, "bytes", toBase64(new Uint8Array(value)), "ArrayBuffer"];
141
+ }
142
+ if (ArrayBuffer.isView(value)) {
143
+ const view = value;
144
+ const ctorName = view.constructor.name;
145
+ const bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
146
+ return ctorName === "Uint8Array" ? [TAG, "bytes", toBase64(bytes)] : [TAG, "bytes", toBase64(bytes), ctorName];
147
+ }
148
+ if (Array.isArray(value)) {
149
+ const encoded = value.map((item) => encodeWire(item, depth + 1));
150
+ return encoded.length > 0 && encoded[0] === TAG ? [TAG, "arr", encoded] : encoded;
151
+ }
152
+ const proto = Object.getPrototypeOf(value);
153
+ if (proto !== null && proto !== Object.prototype) {
154
+ const name = value.constructor?.name ?? "value";
155
+ throw new TypeError(
156
+ `wire-codec: cannot encode a ${name} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`
157
+ );
158
+ }
159
+ const source = value;
160
+ const result = {};
161
+ for (const key of Object.keys(source)) {
162
+ const field = source[key];
163
+ if (field !== void 0) {
164
+ result[key] = encodeWire(field, depth + 1);
165
+ }
166
+ }
167
+ return result;
168
+ };
169
+ const decodeWire = (value, depth = 0) => {
170
+ if (depth > MAX_DEPTH) {
171
+ throw new RangeError(`wire-codec: value nesting exceeds the ${MAX_DEPTH}-level limit`);
172
+ }
173
+ if (value === null || typeof value !== "object") {
174
+ return value;
175
+ }
176
+ if (Array.isArray(value)) {
177
+ if (value[0] === TAG) {
178
+ const tag = value[1];
179
+ switch (tag) {
180
+ case "-inf": {
181
+ return -Infinity;
182
+ }
183
+ case "arr": {
184
+ return value[2].map((item) => decodeWire(item, depth + 1));
185
+ }
186
+ case "bigint": {
187
+ const raw = value[2];
188
+ if (typeof raw !== "string" || raw.length > MAX_BIGINT_DIGITS || !/^-?\d+$/.test(raw)) {
189
+ throw new RangeError(`wire-codec: invalid or over-long bigint (max ${MAX_BIGINT_DIGITS} digits)`);
190
+ }
191
+ return BigInt(raw);
192
+ }
193
+ case "date": {
194
+ return new Date(decodeWire(value[2], depth + 1));
195
+ }
196
+ case "map": {
197
+ return new Map(value[2].map(([k, v]) => [decodeWire(k, depth + 1), decodeWire(v, depth + 1)]));
198
+ }
199
+ case "set": {
200
+ return new Set(value[2].map((item) => decodeWire(item, depth + 1)));
201
+ }
202
+ case "url": {
203
+ return new URL(value[2]);
204
+ }
205
+ case "error": {
206
+ const name = value[2];
207
+ const message = value[3];
208
+ const Ctor = (Object.hasOwn(ERROR_CTORS, name) ? ERROR_CTORS[name] : void 0) ?? Error;
209
+ const error = new Ctor(message);
210
+ if (error.name !== name) {
211
+ Object.defineProperty(error, "name", { configurable: true, value: name, writable: true });
212
+ }
213
+ const props = decodeWire(value[4], depth + 1);
214
+ for (const key of Object.keys(props)) {
215
+ if (key === UNSAFE_KEY) {
216
+ Object.defineProperty(error, key, { configurable: true, enumerable: true, value: props[key], writable: true });
217
+ } else {
218
+ error[key] = props[key];
219
+ }
220
+ }
221
+ if (value.length > 5) {
222
+ Object.defineProperty(error, "cause", { configurable: true, value: decodeWire(value[5], depth + 1), writable: true });
223
+ }
224
+ return error;
225
+ }
226
+ case "bytes": {
227
+ const bytes = fromBase64(value[2]);
228
+ const ctorName = value[3] ?? "Uint8Array";
229
+ if (ctorName === "ArrayBuffer") {
230
+ return bytes.buffer.byteLength === bytes.byteLength ? bytes.buffer : bytes.slice().buffer;
231
+ }
232
+ const Ctor = Object.hasOwn(TYPED_ARRAY_CTORS, ctorName) ? TYPED_ARRAY_CTORS[ctorName] : void 0;
233
+ return Ctor ? new Ctor(bytes.slice().buffer) : bytes;
234
+ }
235
+ case "inf": {
236
+ return Infinity;
237
+ }
238
+ case "nan": {
239
+ return Number.NaN;
240
+ }
241
+ case "undefined": {
242
+ return void 0;
243
+ }
244
+ default: {
245
+ return value.map((item) => decodeWire(item, depth + 1));
246
+ }
247
+ }
248
+ }
249
+ return value.map((item) => decodeWire(item, depth + 1));
250
+ }
251
+ const source = value;
252
+ const result = {};
253
+ for (const key of Object.keys(source)) {
254
+ const decoded = decodeWire(source[key], depth + 1);
255
+ if (key === UNSAFE_KEY) {
256
+ Object.defineProperty(result, key, { configurable: true, enumerable: true, value: decoded, writable: true });
257
+ } else {
258
+ result[key] = decoded;
259
+ }
260
+ }
261
+ return result;
262
+ };
263
+
264
+ const stableWireKey = (value) => stableStringify(encodeWire(value));
265
+
266
+ export { decodeWire as d, encodeWire as e, stableWireKey as s };
@@ -1,5 +1,5 @@
1
- import { S as SubscriptionError, F as FunctionReference, A as ArgsOf, R as ReturnOf, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-BYkEjCEJ.mjs";
2
- export type { b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-BYkEjCEJ.mjs";
1
+ import { S as SubscriptionError, F as FunctionReference, A as ArgsOf, R as ReturnOf, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-JvtVpf8A.mjs";
2
+ export type { b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-JvtVpf8A.mjs";
3
3
  import '@lunora/runtime';
4
4
  /**
5
5
  * The sentinel a framework adapter resolves its reactive args to when it wants
@@ -1,5 +1,5 @@
1
- import { S as SubscriptionError, F as FunctionReference, A as ArgsOf, R as ReturnOf, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-BYkEjCEJ.js";
2
- export type { b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-BYkEjCEJ.js";
1
+ import { S as SubscriptionError, F as FunctionReference, A as ArgsOf, R as ReturnOf, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-JvtVpf8A.js";
2
+ export type { b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-JvtVpf8A.js";
3
3
  import '@lunora/runtime';
4
4
  /**
5
5
  * The sentinel a framework adapter resolves its reactive args to when it wants
@@ -1,6 +1,6 @@
1
- import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-BYkEjCEJ.mjs";
2
- export type { A as ArgsOf, F as FunctionReference, R as ReturnOf } from "../packem_shared/lunora-client.d-BYkEjCEJ.mjs";
3
- export { p as preloadQuery, a as preloadedQueryResult } from "../packem_shared/preload.d-DrfuisCE.mjs";
1
+ import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-JvtVpf8A.mjs";
2
+ export type { A as ArgsOf, F as FunctionReference, R as ReturnOf } from "../packem_shared/lunora-client.d-JvtVpf8A.mjs";
3
+ export { p as preloadQuery, a as preloadedQueryResult } from "../packem_shared/preload.d-DKbjGN5O.mjs";
4
4
  import '@lunora/runtime';
5
5
  /**
6
6
  * Structural shape of a better-auth `getSession` call's resolved value.
@@ -1,6 +1,6 @@
1
- import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-BYkEjCEJ.js";
2
- export type { A as ArgsOf, F as FunctionReference, R as ReturnOf } from "../packem_shared/lunora-client.d-BYkEjCEJ.js";
3
- export { p as preloadQuery, a as preloadedQueryResult } from "../packem_shared/preload.d-B-vyHnml.js";
1
+ import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-JvtVpf8A.js";
2
+ export type { A as ArgsOf, F as FunctionReference, R as ReturnOf } from "../packem_shared/lunora-client.d-JvtVpf8A.js";
3
+ export { p as preloadQuery, a as preloadedQueryResult } from "../packem_shared/preload.d-C4_d_l5v.js";
4
4
  import '@lunora/runtime';
5
5
  /**
6
6
  * Structural shape of a better-auth `getSession` call's resolved value.
@@ -1,4 +1,4 @@
1
1
  export { getServerSession } from '../packem_shared/getServerSession-8jXewqxd.mjs';
2
2
  export { deserializePreloaded, serializePreloaded } from '../packem_shared/deserializePreloaded-C0eJTY_W.mjs';
3
- export { createServerClient } from '../packem_shared/createServerClient-DF-3mLmb.mjs';
3
+ export { createServerClient } from '../packem_shared/createServerClient-CTTAmvMx.mjs';
4
4
  export { preloadQuery, preloadedQueryResult } from '../packem_shared/preloadQuery-lobFkD2Z.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/client",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "Lunora browser SDK: WebSocket transport, optimistic updates, and an offline mutation queue",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -62,7 +62,7 @@
62
62
  "access": "public"
63
63
  },
64
64
  "dependencies": {
65
- "@lunora/errors": "1.0.0-alpha.4"
65
+ "@lunora/errors": "1.0.0-alpha.5"
66
66
  },
67
67
  "engines": {
68
68
  "node": "^22.15.0 || >=24.11.0"
@@ -1 +0,0 @@
1
- export { S as SubscriptionRegistry } from './subscription-BjynOXCU.mjs';
@@ -1,2 +0,0 @@
1
- export { c as createLocalStore } from './local-store-BveBeFEo.mjs';
2
- import './subscription-BjynOXCU.mjs';
@@ -1,68 +0,0 @@
1
- const compareKeys = (a, b) => {
2
- if (a < b) {
3
- return -1;
4
- }
5
- return a > b ? 1 : 0;
6
- };
7
- const stableStringify = (value) => {
8
- if (value === void 0) {
9
- return "null";
10
- }
11
- if (typeof value === "bigint") {
12
- throw new TypeError("stableStringify: cannot use a bigint in a cache key (query/subscription/shape args) — pass it as a string");
13
- }
14
- if (value === null || typeof value !== "object") {
15
- return JSON.stringify(value);
16
- }
17
- if (Array.isArray(value)) {
18
- return `[${value.map((item) => stableStringify(item)).join(",")}]`;
19
- }
20
- const proto = Object.getPrototypeOf(value);
21
- if (proto !== null && proto !== Object.prototype) {
22
- const name = value.constructor?.name ?? "value";
23
- throw new TypeError(
24
- `stableStringify: cannot use a ${name} in a cache key (query/subscription/shape args) — only plain objects, arrays, and JSON primitives are supported`
25
- );
26
- }
27
- const record = value;
28
- const keys = Object.keys(record).toSorted(compareKeys);
29
- const parts = [];
30
- for (const key of keys) {
31
- const raw = record[key];
32
- if (raw === void 0) {
33
- continue;
34
- }
35
- parts.push(`${JSON.stringify(key)}:${stableStringify(raw)}`);
36
- }
37
- return `{${parts.join(",")}}`;
38
- };
39
-
40
- class SubscriptionRegistry {
41
- static key(functionPath, args, shardKey) {
42
- return `${functionPath}::${stableStringify(args)}::${shardKey ?? ""}`;
43
- }
44
- byKey = /* @__PURE__ */ new Map();
45
- byId = /* @__PURE__ */ new Map();
46
- get(key) {
47
- return this.byKey.get(key);
48
- }
49
- getById(id) {
50
- return this.byId.get(id);
51
- }
52
- add(state) {
53
- this.byKey.set(SubscriptionRegistry.key(state.fn.__lunoraRef, state.args, state.shardKey), state);
54
- this.byId.set(state.id, state);
55
- }
56
- remove(state) {
57
- const key = SubscriptionRegistry.key(state.fn.__lunoraRef, state.args, state.shardKey);
58
- if (this.byKey.get(key) === state) {
59
- this.byKey.delete(key);
60
- }
61
- this.byId.delete(state.id);
62
- }
63
- all() {
64
- return [...this.byKey.values()];
65
- }
66
- }
67
-
68
- export { SubscriptionRegistry as S, stableStringify as s };