@nexus_js/runtime 0.6.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/dist/sync.js ADDED
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Nexus $sync Rune — "The Teleportation Rune"
3
+ *
4
+ * Automatically synchronizes client state with:
5
+ * - Browser cookies
6
+ * - SessionStorage / LocalStorage
7
+ * - Server-side DB (via Server Action endpoint)
8
+ *
9
+ * Usage:
10
+ * // Sync with a cookie (persists across sessions)
11
+ * const theme = $sync('theme', { default: 'dark', persist: 'cookie' });
12
+ * theme.value = 'light'; // auto-saves to cookie + syncs to server
13
+ *
14
+ * // Sync with DB (calls /_nexus/sync/:key automatically)
15
+ * const cart = $sync('cart', { default: [], persist: 'db' });
16
+ * cart.value = [...cart.value, newItem]; // writes to DB in background
17
+ *
18
+ * // Optimistic DB sync
19
+ * const likes = $sync('post:42:likes', { default: 0, persist: 'db', optimistic: true });
20
+ * likes.value++; // UI updates immediately, DB writes in background
21
+ */
22
+ import { $state, $effect } from './runes.js';
23
+ const SYNC_ENDPOINT = '/_nexus/sync/';
24
+ /**
25
+ * Creates a synced reactive signal.
26
+ * Reads initial value from storage/server, writes back on every change.
27
+ */
28
+ export function $sync(key, opts = {}) {
29
+ const persist = opts.persist ?? 'local';
30
+ const debounceMs = opts.debounce ?? 300;
31
+ // Read initial value from storage
32
+ const initial = readFromStorage(key, persist) ?? opts.default;
33
+ const signal = $state(initial);
34
+ const pending = $state(false);
35
+ const error = $state(null);
36
+ let debounceTimer = null;
37
+ // Watch for changes and persist
38
+ $effect(() => {
39
+ const val = signal.value;
40
+ if (debounceTimer)
41
+ clearTimeout(debounceTimer);
42
+ debounceTimer = setTimeout(async () => {
43
+ pending.value = true;
44
+ error.value = null;
45
+ try {
46
+ await writeToStorage(key, val, persist, opts.cookie);
47
+ }
48
+ catch (err) {
49
+ error.value = err instanceof Error ? err.message : String(err);
50
+ console.error(`[Nexus $sync] Failed to persist "${key}":`, err);
51
+ }
52
+ finally {
53
+ pending.value = false;
54
+ }
55
+ }, debounceMs);
56
+ });
57
+ const refresh = async () => {
58
+ pending.value = true;
59
+ try {
60
+ const fresh = await readFromServer(key);
61
+ if (fresh !== null)
62
+ signal.value = fresh;
63
+ }
64
+ catch (err) {
65
+ error.value = err instanceof Error ? err.message : String(err);
66
+ }
67
+ finally {
68
+ pending.value = false;
69
+ }
70
+ };
71
+ // Initial server sync for 'db' persistence
72
+ if (persist === 'db') {
73
+ refresh().catch(() => { });
74
+ }
75
+ return {
76
+ get value() { return signal.value; },
77
+ set value(v) { signal.value = v; },
78
+ get pending() { return pending.value; },
79
+ get error() { return error.value; },
80
+ refresh,
81
+ };
82
+ }
83
+ // ─────────────────────────────────────────────────────────────────────────────
84
+ // Storage adapters
85
+ // ─────────────────────────────────────────────────────────────────────────────
86
+ function readFromStorage(key, persist) {
87
+ if (typeof window === 'undefined')
88
+ return null;
89
+ try {
90
+ switch (persist) {
91
+ case 'cookie': {
92
+ const match = document.cookie.match(new RegExp(`(?:^|; )nx_${escKey(key)}=([^;]*)`));
93
+ return match ? JSON.parse(decodeURIComponent(match[1] ?? '')) : null;
94
+ }
95
+ case 'session': {
96
+ const raw = sessionStorage.getItem(`nx:${key}`);
97
+ return raw ? JSON.parse(raw) : null;
98
+ }
99
+ case 'local': {
100
+ const raw = localStorage.getItem(`nx:${key}`);
101
+ return raw ? JSON.parse(raw) : null;
102
+ }
103
+ case 'db':
104
+ return null; // DB reads happen async via refresh()
105
+ }
106
+ }
107
+ catch {
108
+ return null;
109
+ }
110
+ }
111
+ async function writeToStorage(key, value, persist, cookieOpts = {}) {
112
+ const serialized = JSON.stringify(value);
113
+ switch (persist) {
114
+ case 'cookie': {
115
+ const maxAge = cookieOpts?.maxAge ?? 60 * 60 * 24 * 30; // 30 days
116
+ const path = cookieOpts?.path ?? '/';
117
+ const secure = cookieOpts?.secure ? '; Secure' : '';
118
+ const sameSite = cookieOpts?.sameSite ?? 'Lax';
119
+ document.cookie =
120
+ `nx_${escKey(key)}=${encodeURIComponent(serialized)}; Max-Age=${maxAge}; Path=${path}; SameSite=${sameSite}${secure}`;
121
+ break;
122
+ }
123
+ case 'session':
124
+ sessionStorage.setItem(`nx:${key}`, serialized);
125
+ break;
126
+ case 'local':
127
+ localStorage.setItem(`nx:${key}`, serialized);
128
+ break;
129
+ case 'db':
130
+ await writeToServer(key, value);
131
+ break;
132
+ }
133
+ }
134
+ async function readFromServer(key) {
135
+ const res = await fetch(`${SYNC_ENDPOINT}${encodeURIComponent(key)}`, {
136
+ headers: { 'x-nexus-sync': '1' },
137
+ });
138
+ if (!res.ok)
139
+ return null;
140
+ const data = await res.json();
141
+ return data.value ?? null;
142
+ }
143
+ async function writeToServer(key, value) {
144
+ await fetch(`${SYNC_ENDPOINT}${encodeURIComponent(key)}`, {
145
+ method: 'POST',
146
+ headers: {
147
+ 'content-type': 'application/json',
148
+ 'x-nexus-sync': '1',
149
+ },
150
+ body: JSON.stringify({ value }),
151
+ });
152
+ }
153
+ function escKey(key) {
154
+ return key.replace(/[^a-zA-Z0-9_-]/g, '_');
155
+ }
156
+ //# sourceMappingURL=sync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.js","sourceRoot":"","sources":["../src/sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AA2B7C,MAAM,aAAa,GAAG,eAAe,CAAC;AAEtC;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAI,GAAW,EAAE,OAAuB,EAAE;IAC7D,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC;IACxC,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,IAAI,GAAG,CAAC;IAExC,kCAAkC;IAClC,MAAM,OAAO,GAAG,eAAe,CAAI,GAAG,EAAE,OAAO,CAAC,IAAK,IAAI,CAAC,OAAa,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AC;AAC7C,CAAC"}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@nexus_js/runtime",
3
+ "version": "0.6.0",
4
+ "description": "Nexus Islands runtime — selective hydration with fine-grained reactivity",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./island": {
14
+ "import": "./dist/island.js",
15
+ "types": "./dist/island.d.ts"
16
+ },
17
+ "./runes": {
18
+ "import": "./dist/runes.js",
19
+ "types": "./dist/runes.d.ts"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@nexus_js/serialize": "0.6.0"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.5.0",
27
+ "vitest": "^2.0.0"
28
+ },
29
+ "license": "MIT",
30
+ "homepage": "https://nexusjs.dev",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/bierfor/nexus.git",
34
+ "directory": "packages/runtime"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/bierfor/nexus/issues"
38
+ },
39
+ "keywords": [
40
+ "nexus",
41
+ "framework",
42
+ "full-stack",
43
+ "svelte",
44
+ "islands",
45
+ "ssr",
46
+ "vite",
47
+ "server-actions"
48
+ ],
49
+ "files": [
50
+ "dist",
51
+ "README.md"
52
+ ],
53
+ "publishConfig": {
54
+ "access": "public",
55
+ "registry": "https://registry.npmjs.org/"
56
+ },
57
+ "scripts": {
58
+ "build": "tsc -p tsconfig.json",
59
+ "dev": "tsc -p tsconfig.json --watch",
60
+ "test": "vitest run --passWithNoTests",
61
+ "clean": "rm -rf dist"
62
+ }
63
+ }