@umituz/web-hono 1.17.0 → 1.18.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@umituz/web-hono",
3
- "version": "1.17.0",
4
- "description": "API backend platform for Cloudflare Workers + Neon Postgres: createApi skeleton + error envelope layer, full-CRUD resource factory (opt-in create/remove, list params), schema→client type bridge (ClientRow), apiFetch/apiWrite client, reactive fetch-backed store (+React hooks), better-auth server integration (tables/getter/mount/session helpers), weak-compare If-None-Match matcher (matchesEtag), cookie channels + request scope/ETag/R2-stream server helpers, TTL resource cache, and db lifecycle scripts (reset / snapshot / seed)",
3
+ "version": "1.18.0",
4
+ "description": "API backend platform for Cloudflare Workers + Neon Postgres: createApi skeleton + error envelope layer, full-CRUD resource factory (opt-in create/remove, list params), schema→client type bridge (ClientRow), apiFetch/apiWrite client, reactive fetch-backed store (+React hooks), doc-sync diff/snapshot/push machine (snapshotDocs/diffOps/pushOps), better-auth server integration (tables/getter/mount/session helpers), weak-compare If-None-Match matcher (matchesEtag), cookie channels + request scope/ETag/R2-stream server helpers, TTL resource cache, and db lifecycle scripts (reset / snapshot / seed)",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "types": "./src/index.ts",
@@ -14,6 +14,7 @@
14
14
  "./dates": "./src/dates.ts",
15
15
  "./store": "./src/store.ts",
16
16
  "./store/react": "./src/store-react.ts",
17
+ "./sync": "./src/sync.ts",
17
18
  "./lifecycle": "./src/lifecycle.ts",
18
19
  "./auth": "./src/auth.ts",
19
20
  "./mail": "./src/mail.ts",
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@
18
18
  * `/client` apiFetch / apiWrite — the same-origin API client
19
19
  * `/store` createStore — the fetch-backed reactive store
20
20
  * `/store/react` makeStoreHooks — useQuery/useDbValue/useCollection/useHydration
21
+ * `/sync` snapshotDocs / diffOps / pushOps — the doc-sync diff machine
21
22
  * `/lifecycle` reset / snapshot / seed — the shared-Neon db lifecycle
22
23
  * `/cookies` createCookieChannel — one named browser cookie, read/write/clear
23
24
  * `/http` requestScopeOf / respondWithEtag / internalError / CACHE_CONTROLS
package/src/sync.ts ADDED
@@ -0,0 +1,64 @@
1
+ import { apiFetch } from './client';
2
+
3
+ export type SyncDoc = Record<string, unknown> & { id: string };
4
+
5
+ export type SyncCollections = Record<string, readonly SyncDoc[]>;
6
+
7
+ export type SyncPutOp = { op: 'put'; collection: string; doc: SyncDoc };
8
+
9
+ export type SyncDeleteOp = { op: 'del'; collection: string; id: string };
10
+
11
+ export type SyncOp = SyncPutOp | SyncDeleteOp;
12
+
13
+ export type StripFields = Record<string, readonly string[]>;
14
+
15
+ export function snapshotDocs(collections: SyncCollections, stripFields?: StripFields): Map<string, string> {
16
+ const snap = new Map<string, string>();
17
+ for (const [collection, docs] of Object.entries(collections)) {
18
+ for (const doc of docs) {
19
+ const fields = stripFields?.[collection];
20
+ const wire = fields
21
+ ? Object.fromEntries(Object.entries(doc).filter(([field]) => !fields.includes(field)))
22
+ : doc;
23
+ snap.set(`${collection}:${doc.id}`, JSON.stringify(wire));
24
+ }
25
+ }
26
+ return snap;
27
+ }
28
+
29
+ export function diffOps(last: Map<string, string>, next: SyncCollections, stripFields?: StripFields): SyncOp[] {
30
+ const current = snapshotDocs(next, stripFields);
31
+ const ops: SyncOp[] = [];
32
+ for (const [key, json] of current) {
33
+ if (last.get(key) === json) continue;
34
+ const separator = key.indexOf(':');
35
+ const collection = key.slice(0, separator);
36
+ const doc = next[collection]?.find((entry) => entry.id === key.slice(separator + 1));
37
+ if (doc) ops.push({ op: 'put', collection, doc });
38
+ }
39
+ for (const key of last.keys()) {
40
+ if (current.has(key)) continue;
41
+ const separator = key.indexOf(':');
42
+ ops.push({ op: 'del', collection: key.slice(0, separator), id: key.slice(separator + 1) });
43
+ }
44
+ return ops;
45
+ }
46
+
47
+ export async function pushOps(endpoint: string, ops: readonly SyncOp[], chunk: number): Promise<void> {
48
+ for (let index = 0; index < ops.length; index += chunk) {
49
+ await apiFetch(endpoint, { method: 'POST', json: { ops: ops.slice(index, index + chunk) } });
50
+ }
51
+ }
52
+
53
+ export function pushOpsKeepalive(endpoint: string, ops: readonly SyncOp[], chunk: number, bodyLimit: number): void {
54
+ for (let index = 0; index < ops.length; index += chunk) {
55
+ const body = JSON.stringify({ ops: ops.slice(index, index + chunk) });
56
+ if (body.length > bodyLimit) continue;
57
+ void fetch(endpoint, {
58
+ method: 'POST',
59
+ headers: { 'content-type': 'application/json' },
60
+ body,
61
+ keepalive: true,
62
+ }).catch(() => {});
63
+ }
64
+ }