@lunora/client 0.0.0 → 1.0.0-alpha.10
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/LICENSE.md +105 -0
- package/README.md +113 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/auth/index.d.mts +20 -0
- package/dist/auth/index.d.ts +20 -0
- package/dist/auth/index.mjs +60 -0
- package/dist/index.d.mts +385 -0
- package/dist/index.d.ts +385 -0
- package/dist/index.mjs +15 -0
- package/dist/packem_shared/CONFLICT_ERROR_CODE-aUdVbEDw.mjs +4 -0
- package/dist/packem_shared/DEFAULT_MAX_BUFFER-BDkqO5PW.mjs +107 -0
- package/dist/packem_shared/LunoraClient-CgZ6FhKP.mjs +2721 -0
- package/dist/packem_shared/OfflineQueue-BI0FNNvc.mjs +1 -0
- package/dist/packem_shared/SKIP-vItZChkw.mjs +50 -0
- package/dist/packem_shared/SubscriptionRegistry-Dn-7k7eo.mjs +1 -0
- package/dist/packem_shared/applyDelta-4jFGTPA3.mjs +61 -0
- package/dist/packem_shared/createAsyncStoragePersistence-1Z5BZ8RC.mjs +45 -0
- package/dist/packem_shared/createInMemoryBookmarkStorage-BoN7a7TH.mjs +11 -0
- package/dist/packem_shared/createInMemoryPersistence-CW82inU5.mjs +105 -0
- package/dist/packem_shared/createInMemoryQueryCache-B1PQ9Twl.mjs +138 -0
- package/dist/packem_shared/createLocalStore-IOur0jHF.mjs +1 -0
- package/dist/packem_shared/createMutationRunner-BqsavzvG.mjs +21 -0
- package/dist/packem_shared/createMutatorRunner-BETvCd0p.mjs +31 -0
- package/dist/packem_shared/createReconnect-Di_-oHH7.mjs +22 -0
- package/dist/packem_shared/createServerClient-BxkNcRlR.mjs +11 -0
- package/dist/packem_shared/deserializePreloaded-C0eJTY_W.mjs +4 -0
- package/dist/packem_shared/getServerSession-8jXewqxd.mjs +13 -0
- package/dist/packem_shared/local-store-BNgN3Dw3.mjs +111 -0
- package/dist/packem_shared/lunora-client.d-B5vWSgvD.d.mts +2196 -0
- package/dist/packem_shared/lunora-client.d-B5vWSgvD.d.ts +2196 -0
- package/dist/packem_shared/offline-queue-7Wc4onA0.mjs +164 -0
- package/dist/packem_shared/preload.d-3XJD-2hM.d.mts +20 -0
- package/dist/packem_shared/preload.d-CKZR675M.d.ts +20 -0
- package/dist/packem_shared/preloadQuery-lobFkD2Z.mjs +13 -0
- package/dist/packem_shared/subscription-C1Jy7HiF.mjs +55 -0
- package/dist/pagination/index.d.mts +82 -0
- package/dist/pagination/index.d.ts +82 -0
- package/dist/pagination/index.mjs +61 -0
- package/dist/query/index.d.mts +62 -0
- package/dist/query/index.d.ts +62 -0
- package/dist/query/index.mjs +1 -0
- package/dist/ssr/index.d.mts +115 -0
- package/dist/ssr/index.d.ts +115 -0
- package/dist/ssr/index.mjs +4 -0
- package/package.json +53 -17
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { O as OfflineQueue, n as nextId, r as reportPersistenceError } from './offline-queue-7Wc4onA0.mjs';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const SKIP = "skip";
|
|
2
|
+
const toSubscriptionError = (error) => {
|
|
3
|
+
if (error instanceof Error) {
|
|
4
|
+
return { message: error.message };
|
|
5
|
+
}
|
|
6
|
+
return { message: String(error) };
|
|
7
|
+
};
|
|
8
|
+
const createQuerySubscription = (client, function_, args, sinks, options = {}) => {
|
|
9
|
+
if (args === SKIP) {
|
|
10
|
+
sinks.onReset?.();
|
|
11
|
+
return () => {
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
let cancelled = false;
|
|
15
|
+
const handleError = (error) => {
|
|
16
|
+
if (cancelled) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
sinks.onError?.(error);
|
|
20
|
+
};
|
|
21
|
+
const onError = sinks.onError ? handleError : void 0;
|
|
22
|
+
let unsubscribe;
|
|
23
|
+
try {
|
|
24
|
+
unsubscribe = client.subscribe(
|
|
25
|
+
function_,
|
|
26
|
+
args,
|
|
27
|
+
(value) => {
|
|
28
|
+
if (cancelled) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
sinks.onData(value);
|
|
32
|
+
},
|
|
33
|
+
{ onError, shardKey: options.shardKey }
|
|
34
|
+
);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (!sinks.onError) {
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
handleError(toSubscriptionError(error));
|
|
40
|
+
return () => {
|
|
41
|
+
cancelled = true;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return () => {
|
|
45
|
+
cancelled = true;
|
|
46
|
+
unsubscribe();
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { SKIP, createQuerySubscription, toSubscriptionError };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { S as SubscriptionRegistry } from './subscription-C1Jy7HiF.mjs';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const ID_FIELD = "_id";
|
|
2
|
+
const CREATION_FIELD = "_creationTime";
|
|
3
|
+
const rowId = (row) => {
|
|
4
|
+
if (typeof row !== "object" || row === null) {
|
|
5
|
+
return void 0;
|
|
6
|
+
}
|
|
7
|
+
const id = row[ID_FIELD];
|
|
8
|
+
return typeof id === "string" ? id : void 0;
|
|
9
|
+
};
|
|
10
|
+
const insertionIndex = (list, row) => {
|
|
11
|
+
const creation = row[CREATION_FIELD];
|
|
12
|
+
if (typeof creation !== "number") {
|
|
13
|
+
return list.length;
|
|
14
|
+
}
|
|
15
|
+
for (const [index, existingRow] of list.entries()) {
|
|
16
|
+
const existing = existingRow[CREATION_FIELD];
|
|
17
|
+
if (typeof existing === "number" && existing > creation) {
|
|
18
|
+
return index;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return list.length;
|
|
22
|
+
};
|
|
23
|
+
const isMutationDelta = (value) => {
|
|
24
|
+
if (typeof value !== "object" || value === null) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const candidate = value;
|
|
28
|
+
return typeof candidate["key"] === "string" && typeof candidate["table"] === "string" && (candidate["op"] === "insert" || candidate["op"] === "update" || candidate["op"] === "delete");
|
|
29
|
+
};
|
|
30
|
+
const applyDelta = (current, delta) => {
|
|
31
|
+
if (!Array.isArray(current)) {
|
|
32
|
+
return void 0;
|
|
33
|
+
}
|
|
34
|
+
const rows = [];
|
|
35
|
+
for (const element of current) {
|
|
36
|
+
const id = rowId(element);
|
|
37
|
+
if (id === void 0) {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
rows.push(element);
|
|
41
|
+
}
|
|
42
|
+
const { key, op, row } = delta;
|
|
43
|
+
if (op === "delete") {
|
|
44
|
+
const next2 = rows.filter((existing) => existing[ID_FIELD] !== key);
|
|
45
|
+
return next2.length === rows.length ? [...rows] : next2;
|
|
46
|
+
}
|
|
47
|
+
if (row === void 0) {
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
const existingIndex = rows.findIndex((existing) => existing[ID_FIELD] === key);
|
|
51
|
+
if (existingIndex === -1) {
|
|
52
|
+
const next2 = [...rows];
|
|
53
|
+
next2.splice(insertionIndex(rows, row), 0, row);
|
|
54
|
+
return next2;
|
|
55
|
+
}
|
|
56
|
+
const next = [...rows];
|
|
57
|
+
next[existingIndex] = row;
|
|
58
|
+
return next;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { applyDelta, isMutationDelta };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const DEFAULT_KEY = "lunora:offline-mutations";
|
|
2
|
+
const createAsyncStoragePersistence = (options) => {
|
|
3
|
+
const { storage } = options;
|
|
4
|
+
const key = options.key ?? DEFAULT_KEY;
|
|
5
|
+
let chain = Promise.resolve();
|
|
6
|
+
const serialize = (run) => {
|
|
7
|
+
const next = chain.then(run, run);
|
|
8
|
+
chain = next.then(
|
|
9
|
+
() => void 0,
|
|
10
|
+
() => void 0
|
|
11
|
+
);
|
|
12
|
+
return next;
|
|
13
|
+
};
|
|
14
|
+
const readAll = async () => {
|
|
15
|
+
const raw = await storage.getItem(key);
|
|
16
|
+
if (raw === null) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
const parsed = JSON.parse(raw);
|
|
21
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
22
|
+
} catch {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const writeAll = (mutations) => storage.setItem(key, JSON.stringify(mutations));
|
|
27
|
+
return {
|
|
28
|
+
append: (mutation) => serialize(async () => {
|
|
29
|
+
const mutations = await readAll();
|
|
30
|
+
mutations.push(mutation);
|
|
31
|
+
await writeAll(mutations);
|
|
32
|
+
}),
|
|
33
|
+
clear: () => serialize(() => storage.removeItem(key)),
|
|
34
|
+
load: () => serialize(readAll),
|
|
35
|
+
remove: (id) => serialize(async () => {
|
|
36
|
+
const mutations = await readAll();
|
|
37
|
+
const remaining = mutations.filter((mutation) => mutation.id !== id);
|
|
38
|
+
if (remaining.length !== mutations.length) {
|
|
39
|
+
await writeAll(remaining);
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export { createAsyncStoragePersistence };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const createInMemoryPersistence = () => {
|
|
2
|
+
const entries = /* @__PURE__ */ new Map();
|
|
3
|
+
const clone = (mutation) => {
|
|
4
|
+
return {
|
|
5
|
+
args: { ...mutation.args },
|
|
6
|
+
functionPath: mutation.functionPath,
|
|
7
|
+
id: mutation.id,
|
|
8
|
+
identity: mutation.identity,
|
|
9
|
+
shardKey: mutation.shardKey
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
append: (mutation) => {
|
|
14
|
+
entries.set(mutation.id, clone(mutation));
|
|
15
|
+
return Promise.resolve();
|
|
16
|
+
},
|
|
17
|
+
clear: () => {
|
|
18
|
+
entries.clear();
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
},
|
|
21
|
+
load: () => Promise.resolve([...entries.values()].map((mutation) => clone(mutation))),
|
|
22
|
+
remove: (id) => {
|
|
23
|
+
entries.delete(id);
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
const DEFAULT_DATABASE = "lunora";
|
|
29
|
+
const DEFAULT_STORE = "offline-mutations";
|
|
30
|
+
const ID_INDEX = "by_id";
|
|
31
|
+
const promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
32
|
+
request.addEventListener("success", () => {
|
|
33
|
+
resolve(request.result);
|
|
34
|
+
});
|
|
35
|
+
request.addEventListener("error", () => {
|
|
36
|
+
reject(request.error ?? new Error("IndexedDB request failed"));
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
const createIndexedDbPersistence = (options = {}) => {
|
|
40
|
+
const factory = options.indexedDB ?? (typeof indexedDB === "undefined" ? void 0 : indexedDB);
|
|
41
|
+
if (!factory) {
|
|
42
|
+
throw new Error("createIndexedDbPersistence: no IndexedDB available — pass `indexedDB` or use createInMemoryPersistence()");
|
|
43
|
+
}
|
|
44
|
+
const databaseName = options.databaseName ?? DEFAULT_DATABASE;
|
|
45
|
+
const storeName = options.storeName ?? DEFAULT_STORE;
|
|
46
|
+
let databasePromise;
|
|
47
|
+
const openDatabase = () => {
|
|
48
|
+
if (databasePromise) {
|
|
49
|
+
return databasePromise;
|
|
50
|
+
}
|
|
51
|
+
databasePromise = new Promise((resolve, reject) => {
|
|
52
|
+
const request = factory.open(databaseName, 1);
|
|
53
|
+
request.addEventListener("upgradeneeded", () => {
|
|
54
|
+
const database = request.result;
|
|
55
|
+
if (!database.objectStoreNames.contains(storeName)) {
|
|
56
|
+
const store = database.createObjectStore(storeName, { autoIncrement: true });
|
|
57
|
+
store.createIndex(ID_INDEX, "id", { unique: true });
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
request.addEventListener("success", () => {
|
|
61
|
+
resolve(request.result);
|
|
62
|
+
});
|
|
63
|
+
request.addEventListener("error", () => {
|
|
64
|
+
reject(request.error ?? new Error("IndexedDB open failed"));
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return databasePromise;
|
|
68
|
+
};
|
|
69
|
+
const withStore = async (mode, run) => {
|
|
70
|
+
const database = await openDatabase();
|
|
71
|
+
const transaction = database.transaction(storeName, mode);
|
|
72
|
+
const result = await run(transaction.objectStore(storeName));
|
|
73
|
+
await new Promise((resolve, reject) => {
|
|
74
|
+
transaction.addEventListener("complete", () => {
|
|
75
|
+
resolve();
|
|
76
|
+
});
|
|
77
|
+
transaction.addEventListener("error", () => {
|
|
78
|
+
reject(transaction.error ?? new Error("IndexedDB transaction failed"));
|
|
79
|
+
});
|
|
80
|
+
transaction.addEventListener("abort", () => {
|
|
81
|
+
reject(transaction.error ?? new Error("IndexedDB transaction aborted"));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
append: async (mutation) => {
|
|
88
|
+
await withStore("readwrite", (store) => promisifyRequest(store.add(mutation)));
|
|
89
|
+
},
|
|
90
|
+
clear: async () => {
|
|
91
|
+
await withStore("readwrite", (store) => promisifyRequest(store.clear()));
|
|
92
|
+
},
|
|
93
|
+
load: async () => withStore("readonly", (store) => promisifyRequest(store.getAll())),
|
|
94
|
+
remove: async (id) => {
|
|
95
|
+
await withStore("readwrite", async (store) => {
|
|
96
|
+
const key = await promisifyRequest(store.index(ID_INDEX).getKey(id));
|
|
97
|
+
if (key !== void 0) {
|
|
98
|
+
await promisifyRequest(store.delete(key));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export { createInMemoryPersistence, createIndexedDbPersistence };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
const queryCacheKey = (functionPath, argsKey, shardKey) => `${functionPath}::${argsKey}::${shardKey ?? ""}`;
|
|
2
|
+
const DEFAULT_MAX_ENTRIES = 500;
|
|
3
|
+
const createInMemoryQueryCache = (options = {}) => {
|
|
4
|
+
const maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;
|
|
5
|
+
const entries = /* @__PURE__ */ new Map();
|
|
6
|
+
const clone = (entry) => {
|
|
7
|
+
return { ...entry, value: structuredClone(entry.value) };
|
|
8
|
+
};
|
|
9
|
+
const evict = () => {
|
|
10
|
+
if (entries.size <= maxEntries) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const ordered = [...entries.values()].toSorted((a, b) => a.ts - b.ts);
|
|
14
|
+
for (const entry of ordered) {
|
|
15
|
+
if (entries.size <= maxEntries) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
entries.delete(entry.key);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
clear: () => {
|
|
23
|
+
entries.clear();
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
},
|
|
26
|
+
load: () => Promise.resolve([...entries.values()].map((entry) => clone(entry))),
|
|
27
|
+
put: (key, entry) => {
|
|
28
|
+
entries.set(key, clone({ ...entry, key }));
|
|
29
|
+
evict();
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
},
|
|
32
|
+
remove: (key) => {
|
|
33
|
+
entries.delete(key);
|
|
34
|
+
return Promise.resolve();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const DEFAULT_DATABASE = "lunora";
|
|
39
|
+
const DEFAULT_STORE = "query-cache";
|
|
40
|
+
const TS_INDEX = "by_ts";
|
|
41
|
+
const DATABASE_VERSION = 2;
|
|
42
|
+
const promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
43
|
+
request.addEventListener("success", () => {
|
|
44
|
+
resolve(request.result);
|
|
45
|
+
});
|
|
46
|
+
request.addEventListener("error", () => {
|
|
47
|
+
reject(request.error ?? new Error("IndexedDB request failed"));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
const createIndexedDbQueryCache = (options = {}) => {
|
|
51
|
+
const factory = options.indexedDB ?? (typeof indexedDB === "undefined" ? void 0 : indexedDB);
|
|
52
|
+
if (!factory) {
|
|
53
|
+
throw new Error("createIndexedDbQueryCache: no IndexedDB available — pass `indexedDB` or use createInMemoryQueryCache()");
|
|
54
|
+
}
|
|
55
|
+
const databaseName = options.databaseName ?? DEFAULT_DATABASE;
|
|
56
|
+
const storeName = options.storeName ?? DEFAULT_STORE;
|
|
57
|
+
const maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;
|
|
58
|
+
let databasePromise;
|
|
59
|
+
const openDatabase = () => {
|
|
60
|
+
if (databasePromise) {
|
|
61
|
+
return databasePromise;
|
|
62
|
+
}
|
|
63
|
+
databasePromise = new Promise((resolve, reject) => {
|
|
64
|
+
const request = factory.open(databaseName, DATABASE_VERSION);
|
|
65
|
+
request.addEventListener("upgradeneeded", () => {
|
|
66
|
+
const database = request.result;
|
|
67
|
+
if (!database.objectStoreNames.contains(storeName)) {
|
|
68
|
+
const store = database.createObjectStore(storeName, { keyPath: "key" });
|
|
69
|
+
store.createIndex(TS_INDEX, "ts", { unique: false });
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
request.addEventListener("success", () => {
|
|
73
|
+
resolve(request.result);
|
|
74
|
+
});
|
|
75
|
+
request.addEventListener("error", () => {
|
|
76
|
+
reject(request.error ?? new Error("IndexedDB open failed"));
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
return databasePromise;
|
|
80
|
+
};
|
|
81
|
+
const withStore = async (mode, run) => {
|
|
82
|
+
const database = await openDatabase();
|
|
83
|
+
const transaction = database.transaction(storeName, mode);
|
|
84
|
+
const result = await run(transaction.objectStore(storeName));
|
|
85
|
+
await new Promise((resolve, reject) => {
|
|
86
|
+
transaction.addEventListener("complete", () => {
|
|
87
|
+
resolve();
|
|
88
|
+
});
|
|
89
|
+
transaction.addEventListener("error", () => {
|
|
90
|
+
reject(transaction.error ?? new Error("IndexedDB transaction failed"));
|
|
91
|
+
});
|
|
92
|
+
transaction.addEventListener("abort", () => {
|
|
93
|
+
reject(transaction.error ?? new Error("IndexedDB transaction aborted"));
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
return result;
|
|
97
|
+
};
|
|
98
|
+
const evict = async (store) => {
|
|
99
|
+
const count = await promisifyRequest(store.count());
|
|
100
|
+
let overflow = count - maxEntries;
|
|
101
|
+
if (overflow <= 0) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
await new Promise((resolve, reject) => {
|
|
105
|
+
const cursorRequest = store.index(TS_INDEX).openCursor();
|
|
106
|
+
cursorRequest.addEventListener("success", () => {
|
|
107
|
+
const cursor = cursorRequest.result;
|
|
108
|
+
if (!cursor || overflow <= 0) {
|
|
109
|
+
resolve();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
cursor.delete();
|
|
113
|
+
overflow -= 1;
|
|
114
|
+
cursor.continue();
|
|
115
|
+
});
|
|
116
|
+
cursorRequest.addEventListener("error", () => {
|
|
117
|
+
reject(cursorRequest.error ?? new Error("IndexedDB eviction failed"));
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
clear: async () => {
|
|
123
|
+
await withStore("readwrite", (store) => promisifyRequest(store.clear()));
|
|
124
|
+
},
|
|
125
|
+
load: async () => withStore("readonly", (store) => promisifyRequest(store.getAll())),
|
|
126
|
+
put: async (key, entry) => {
|
|
127
|
+
await withStore("readwrite", async (store) => {
|
|
128
|
+
await promisifyRequest(store.put({ ...entry, key }));
|
|
129
|
+
await evict(store);
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
remove: async (key) => {
|
|
133
|
+
await withStore("readwrite", (store) => promisifyRequest(store.delete(key)));
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export { createInMemoryQueryCache, createIndexedDbQueryCache, queryCacheKey };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { c as createLocalStore } from './local-store-BNgN3Dw3.mjs';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const createMutationRunner = (client, function_, sinks) => {
|
|
2
|
+
let inFlight = 0;
|
|
3
|
+
return async (args, options) => {
|
|
4
|
+
inFlight += 1;
|
|
5
|
+
sinks.setPending(true);
|
|
6
|
+
try {
|
|
7
|
+
const result = await client.mutation(function_, args, options);
|
|
8
|
+
sinks.setResult(result);
|
|
9
|
+
return result;
|
|
10
|
+
} catch (error) {
|
|
11
|
+
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
12
|
+
sinks.setError(normalized);
|
|
13
|
+
throw normalized;
|
|
14
|
+
} finally {
|
|
15
|
+
inFlight -= 1;
|
|
16
|
+
sinks.setPending(inFlight > 0);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { createMutationRunner };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const createMutatorRunner = (handle, sinks) => {
|
|
2
|
+
let inFlight = 0;
|
|
3
|
+
let latestInvocation = 0;
|
|
4
|
+
const mutate = async (args) => {
|
|
5
|
+
latestInvocation += 1;
|
|
6
|
+
const invocation = latestInvocation;
|
|
7
|
+
inFlight += 1;
|
|
8
|
+
sinks.setPending(true);
|
|
9
|
+
try {
|
|
10
|
+
await handle(args).isPersisted.promise;
|
|
11
|
+
if (invocation === latestInvocation) {
|
|
12
|
+
sinks.setError(void 0);
|
|
13
|
+
}
|
|
14
|
+
} catch (error) {
|
|
15
|
+
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
16
|
+
if (invocation === latestInvocation) {
|
|
17
|
+
sinks.setError(normalized);
|
|
18
|
+
}
|
|
19
|
+
throw normalized;
|
|
20
|
+
} finally {
|
|
21
|
+
inFlight -= 1;
|
|
22
|
+
sinks.setPending(inFlight > 0);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const reset = () => {
|
|
26
|
+
sinks.setError(void 0);
|
|
27
|
+
};
|
|
28
|
+
return { mutate, reset };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { createMutatorRunner };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const createReconnect = (options = {}, random = Math.random) => {
|
|
2
|
+
const initialDelayMs = options.initialDelayMs ?? 250;
|
|
3
|
+
const maxDelayMs = options.maxDelayMs ?? 3e4;
|
|
4
|
+
const jitter = options.jitter ?? true;
|
|
5
|
+
let attempt = 0;
|
|
6
|
+
return {
|
|
7
|
+
next() {
|
|
8
|
+
const exponential = Math.min(maxDelayMs, initialDelayMs * 2 ** attempt);
|
|
9
|
+
attempt += 1;
|
|
10
|
+
if (!jitter) {
|
|
11
|
+
return exponential;
|
|
12
|
+
}
|
|
13
|
+
const min = exponential / 2;
|
|
14
|
+
return Math.floor(min + random() * (exponential - min));
|
|
15
|
+
},
|
|
16
|
+
reset() {
|
|
17
|
+
attempt = 0;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { createReconnect };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { LunoraClient } from './LunoraClient-CgZ6FhKP.mjs';
|
|
2
|
+
|
|
3
|
+
const createServerClient = (options) => {
|
|
4
|
+
const client = new LunoraClient({ fetch: options.fetch, url: options.url });
|
|
5
|
+
if (options.token !== void 0) {
|
|
6
|
+
client.setAuthToken(options.token);
|
|
7
|
+
}
|
|
8
|
+
return client;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { createServerClient };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const extractHeaders = (source) => {
|
|
2
|
+
if (source instanceof Headers) {
|
|
3
|
+
return source;
|
|
4
|
+
}
|
|
5
|
+
return source.headers;
|
|
6
|
+
};
|
|
7
|
+
const getServerSession = async (request, auth) => {
|
|
8
|
+
const headers = extractHeaders(request);
|
|
9
|
+
const result = await auth.api.getSession({ headers });
|
|
10
|
+
return result ?? null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { getServerSession };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const foldOptimistic = (base, layers) => {
|
|
2
|
+
let value = base;
|
|
3
|
+
for (const layer of layers) {
|
|
4
|
+
try {
|
|
5
|
+
value = layer.transform(value);
|
|
6
|
+
} catch {
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
const notifySubscription = (state, value) => {
|
|
12
|
+
if (value === state.lastValue) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
state.lastValue = value;
|
|
16
|
+
for (const callback of state.callbacks) {
|
|
17
|
+
try {
|
|
18
|
+
callback(value);
|
|
19
|
+
} catch {
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const applyOptimisticLayer = (state, optimistic) => {
|
|
24
|
+
let next;
|
|
25
|
+
try {
|
|
26
|
+
next = optimistic(state.lastValue);
|
|
27
|
+
} catch {
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
const layer = { id: /* @__PURE__ */ Symbol("optimistic"), transform: optimistic };
|
|
31
|
+
state.optimisticLayers.push(layer);
|
|
32
|
+
notifySubscription(state, next);
|
|
33
|
+
const remove = () => {
|
|
34
|
+
const index = state.optimisticLayers.findIndex((entry) => entry.id === layer.id);
|
|
35
|
+
if (index === -1) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
state.optimisticLayers.splice(index, 1);
|
|
39
|
+
return true;
|
|
40
|
+
};
|
|
41
|
+
const refold = () => {
|
|
42
|
+
notifySubscription(state, foldOptimistic(state.serverBase, state.optimisticLayers));
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
confirm: (commitCursor) => {
|
|
46
|
+
if (commitCursor === void 0) {
|
|
47
|
+
remove();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
layer.commitCursor = commitCursor;
|
|
51
|
+
if (state.serverCursor !== void 0 && state.serverCursor >= commitCursor && remove()) {
|
|
52
|
+
refold();
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
rollback: () => {
|
|
56
|
+
if (remove()) {
|
|
57
|
+
refold();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const dropConfirmedLayers = (state, cursor) => {
|
|
63
|
+
if (cursor === void 0 || state.optimisticLayers.length === 0) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const before = state.optimisticLayers.length;
|
|
67
|
+
state.optimisticLayers = state.optimisticLayers.filter((layer) => layer.commitCursor === void 0 || layer.commitCursor > cursor);
|
|
68
|
+
return state.optimisticLayers.length !== before;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const createLocalStore = (subscriptions, shardKey, stableStringify) => {
|
|
72
|
+
const confirms = [];
|
|
73
|
+
const rollbacks = [];
|
|
74
|
+
const findState = (functionRef, argsKey) => {
|
|
75
|
+
for (const state of subscriptions.all()) {
|
|
76
|
+
if (state.fn.__lunoraRef === functionRef && state.shardKey === shardKey && state.argsKey === argsKey) {
|
|
77
|
+
return state;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return void 0;
|
|
81
|
+
};
|
|
82
|
+
const store = {
|
|
83
|
+
getAllQueries: (function_) => {
|
|
84
|
+
const matches = [];
|
|
85
|
+
for (const state of subscriptions.all()) {
|
|
86
|
+
if (state.fn.__lunoraRef === function_.__lunoraRef && state.shardKey === shardKey) {
|
|
87
|
+
matches.push({ args: state.args, value: state.lastValue });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return matches;
|
|
91
|
+
},
|
|
92
|
+
getQuery: (function_, args) => {
|
|
93
|
+
const state = findState(function_.__lunoraRef, stableStringify(args ?? {}));
|
|
94
|
+
return state?.lastValue;
|
|
95
|
+
},
|
|
96
|
+
setQuery: (function_, args, value) => {
|
|
97
|
+
const state = findState(function_.__lunoraRef, stableStringify(args ?? {}));
|
|
98
|
+
if (!state) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const handle = applyOptimisticLayer(state, () => value);
|
|
102
|
+
if (handle) {
|
|
103
|
+
confirms.push(handle.confirm);
|
|
104
|
+
rollbacks.push(handle.rollback);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
return { confirms, rollbacks, store };
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export { applyOptimisticLayer as a, createLocalStore as c, dropConfirmedLayers as d, foldOptimistic as f, notifySubscription as n };
|