@reticlehq/next 2.10.0 → 2.12.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.
Files changed (2) hide show
  1. package/index.cjs +98 -1
  2. package/package.json +6 -3
package/index.cjs CHANGED
@@ -12,6 +12,17 @@ const path = require('node:path');
12
12
  // deliberately has no ESM/TS dependency on core, so the two constants are mirrored here.
13
13
  const PAIRING_TOKEN_DIR_ENV = 'RETICLE_PAIRING_TOKEN_DIR';
14
14
  const PAIRING_TOKEN_FILE = 'pairing-token';
15
+ const RETICLE_CONFIG_FILE = '.reticle.json';
16
+ const RETICLE_HOME_DIR = '.reticle';
17
+ // Mirrors core's daemonRegistryFileName: ~/.reticle/daemon-<port>.json.
18
+ const DAEMON_ENTRY_PREFIX = 'daemon-';
19
+ const DAEMON_ENTRY_SUFFIX = '.json';
20
+ // Mirrors core's RETICLE_CLIENT_HOST / RETICLE_WS_PATH. This package is plain CJS tooling with no
21
+ // ESM/TS dependency on core, so the values are duplicated here the way the constants above are — and
22
+ // pinned to core's by test, because a URL that drifts produces a silent no-connect rather than an
23
+ // error anybody sees.
24
+ const RETICLE_CLIENT_HOST = 'localhost';
25
+ const RETICLE_WS_PATH = '/reticle';
15
26
 
16
27
  /**
17
28
  * Read the daemon's auto-provisioned pairing token (~/.reticle/pairing-token, or the
@@ -32,6 +43,87 @@ function readPairingToken() {
32
43
  }
33
44
  }
34
45
 
46
+ /**
47
+ * Find the live daemon serving THIS project and return its websocket URL.
48
+ *
49
+ * The Vite plugin has always done this (`discoverDaemonPort`), and core's `pickDaemonPort` documents
50
+ * the rule as shared by "both the vite and next plugins" — but this package never implemented the
51
+ * next half. The consequence was a frozen port: `reticle init` baked `url: 'ws://localhost:<port>'`
52
+ * into the generated ReticleDev component at install time, and the app dialled that forever. Move the
53
+ * daemon and a Next app silently dials a port nothing is listening on, with no error anywhere except
54
+ * a console warning in a browser nobody is watching.
55
+ *
56
+ * The rule is core's, mirrored rather than imported for the same reason the constants above are: this
57
+ * package is plain CJS tooling with no ESM/TS dependency on core. Kept deliberately identical:
58
+ * 1. drop dead daemons (crashed, or a stale entry left by a kill -9);
59
+ * 2. among the living, prefer a projectId match, lowest port on a tie;
60
+ * 3. return undefined when nothing matches, so the app falls back to the default port rather than
61
+ * auto-connecting to a daemon serving a DIFFERENT project — a wrong connect is worse than an
62
+ * honest default, because it reports another app's state as this one's.
63
+ *
64
+ * The projectId is READ from `.reticle.json` rather than re-derived. Re-deriving would duplicate
65
+ * core's slug + hash rule in a third place, and a drift there does not fail loudly: it silently
66
+ * matches no daemon, which is the exact bug this function exists to remove.
67
+ * @returns {string | undefined}
68
+ */
69
+ /**
70
+ * Where Reticle keeps its per-user state: the pairing token AND the daemon registry.
71
+ *
72
+ * Honours the same RETICLE_PAIRING_TOKEN_DIR override the token reader uses, because it is the same
73
+ * directory. One override rather than two keeps a test (or a sandbox) from pointing the two halves at
74
+ * different places and getting a token from one daemon with the port of another.
75
+ */
76
+ function reticleHomeDir() {
77
+ const override = process.env[PAIRING_TOKEN_DIR_ENV];
78
+ return override !== undefined && override.length > 0
79
+ ? override
80
+ : path.join(os.homedir(), RETICLE_HOME_DIR);
81
+ }
82
+
83
+ /** process.kill(pid, 0) throws iff the process is gone: the same liveness probe the daemon uses. */
84
+ function defaultIsAlive(pid) {
85
+ try {
86
+ process.kill(pid, 0);
87
+ return true;
88
+ } catch {
89
+ return false;
90
+ }
91
+ }
92
+
93
+ function discoverDaemonUrl(cwd = process.cwd(), home = reticleHomeDir(), alive = defaultIsAlive) {
94
+ let projectId;
95
+ try {
96
+ const parsed = JSON.parse(fs.readFileSync(path.join(cwd, RETICLE_CONFIG_FILE), 'utf8'));
97
+ projectId = typeof parsed?.projectId === 'string' ? parsed.projectId : undefined;
98
+ } catch {
99
+ return undefined; // no .reticle.json: this project has not been through `reticle init`
100
+ }
101
+ if (projectId === undefined || projectId.length === 0) return undefined;
102
+ const dir = home;
103
+ let files;
104
+ try {
105
+ files = fs.readdirSync(dir);
106
+ } catch {
107
+ return undefined; // no ~/.reticle yet: no daemon has ever run here
108
+ }
109
+ const ports = [];
110
+ for (const file of files) {
111
+ if (!file.startsWith(DAEMON_ENTRY_PREFIX) || !file.endsWith(DAEMON_ENTRY_SUFFIX)) continue;
112
+ let entry;
113
+ try {
114
+ entry = JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
115
+ } catch {
116
+ continue; // a half-written or corrupt entry is not a daemon
117
+ }
118
+ if (entry?.projectId !== projectId) continue;
119
+ if (typeof entry.port !== 'number' || typeof entry.pid !== 'number') continue;
120
+ if (!alive(entry.pid)) continue;
121
+ ports.push(entry.port);
122
+ }
123
+ if (ports.length === 0) return undefined;
124
+ return `ws://${RETICLE_CLIENT_HOST}:${String(Math.min(...ports))}${RETICLE_WS_PATH}`;
125
+ }
126
+
35
127
  /**
36
128
  * The stamping loader, addressed by package export so both bundlers can resolve it. Turbopack takes
37
129
  * loaders by module id, not by absolute path.
@@ -152,6 +244,7 @@ function withReticle(nextConfig = {}) {
152
244
 
153
245
  const userWebpack = nextConfig.webpack;
154
246
  const token = readPairingToken();
247
+ const daemonUrl = discoverDaemonUrl();
155
248
  return {
156
249
  ...nextConfig,
157
250
  ...(supportsTurbopackKey() ? { turbopack: turbopackConfig(nextConfig.turbopack) } : {}),
@@ -166,6 +259,10 @@ function withReticle(nextConfig = {}) {
166
259
  // Next 16 default — never runs the webpack branch, and a source pointer that only works on one
167
260
  // of the two bundlers is worse than one that works on neither.
168
261
  NEXT_PUBLIC_RETICLE_ROOT: process.cwd(),
262
+ // The daemon serving THIS project, discovered on every dev-server start rather than baked in
263
+ // at install time. Without it the generated connect keeps whatever port `init` happened to see,
264
+ // and a daemon that later moves is unreachable with no error the user ever sees.
265
+ ...(daemonUrl !== undefined ? { NEXT_PUBLIC_RETICLE_URL: daemonUrl } : {}),
169
266
  // So a version-skewed pair can name itself instead of surfacing as a bare -32000.
170
267
  NEXT_PUBLIC_RETICLE_SDK_VERSION: sdkPackageVersion(),
171
268
  },
@@ -183,4 +280,4 @@ function withReticle(nextConfig = {}) {
183
280
  };
184
281
  }
185
282
 
186
- module.exports = { withReticle, readPairingToken };
283
+ module.exports = { withReticle, readPairingToken, discoverDaemonUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/next",
3
- "version": "2.10.0",
3
+ "version": "2.12.0",
4
4
  "description": "Next.js helper for Reticle: keeps SWC, adds source-file mapping via a dev-only webpack pre-loader (data-reticle-source).",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Reticle contributors",
@@ -38,7 +38,7 @@
38
38
  ],
39
39
  "dependencies": {
40
40
  "@babel/core": "^7.26.0",
41
- "@reticlehq/babel-plugin": "2.10.0"
41
+ "@reticlehq/babel-plugin": "2.12.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "next": ">=13"
@@ -50,5 +50,8 @@
50
50
  "engines": {
51
51
  "node": ">=20.0.0"
52
52
  },
53
- "bugs": "https://github.com/reticlehq/reticle/issues"
53
+ "bugs": "https://github.com/reticlehq/reticle/issues",
54
+ "scripts": {
55
+ "test:unit": "vitest run loader.test.mjs index.test.mjs"
56
+ }
54
57
  }