@reticlehq/next 3.3.0 → 3.4.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 (3) hide show
  1. package/README.md +1 -1
  2. package/index.cjs +110 -15
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Next.js helper for [Reticle](https://github.com/reticlehq/reticle). Gives you **source-file mapping on Next.js without disabling SWC**.
4
4
 
5
- React 19 removed `_debugSource`, and Next compiles with SWC (not Babel), so the `@reticlehq/babel-plugin` route doesn't apply. `@reticlehq/next` adds a **dev-only webpack pre-loader** that stamps `data-reticle-source="file:line:col"` on your JSX _before_ SWC compiles it — so `reticle_inspect` returns the component's source file, and SWC stays on (next/font, fast refresh, etc. all keep working).
5
+ React 19 removed `_debugSource`, and Next compiles with SWC (not Babel), so the `@reticlehq/babel-plugin` route doesn't apply. `@reticlehq/next` adds a **dev-only webpack pre-loader** that stamps `data-reticle-source="file:line:col"` on your JSX _before_ SWC compiles it — so `reticle_look { action: "element" }` returns the component's source file, and SWC stays on (next/font, fast refresh, etc. all keep working).
6
6
 
7
7
  ```bash
8
8
  npm i -D @reticlehq/next
package/index.cjs CHANGED
@@ -19,6 +19,8 @@ const PAIRING_TOKEN_DIR_ENV = 'RETICLE_PAIRING_TOKEN_DIR';
19
19
  * nothing, which is why `RETICLE_PAIRING_TOKEN_DIR` above is a local constant too.
20
20
  */
21
21
  const DEV_OVERRIDE_ENV = 'RETICLE_DEV';
22
+ /** Next's own marker on the server process `next dev` forks. See the production gate below. */
23
+ const NEXT_DEV_WORKER_ENV = 'NEXT_PRIVATE_WORKER';
22
24
  const PAIRING_TOKEN_FILE = 'pairing-token';
23
25
  const RETICLE_CONFIG_FILE = '.reticle.json';
24
26
  const RETICLE_HOME_DIR = '.reticle';
@@ -85,9 +87,12 @@ function readPairingToken() {
85
87
  *
86
88
  * The rule is core's, mirrored rather than imported for the same reason the constants above are: this
87
89
  * package is plain CJS tooling with no ESM/TS dependency on core. Kept deliberately identical:
88
- * 1. drop dead daemons (crashed, or a stale entry left by a kill -9);
89
- * 2. among the living, prefer a projectId match, lowest port on a tie;
90
- * 3. return undefined when nothing matches, so the app falls back to the default port rather than
90
+ * 1. the port in `.reticle.json` wins whenever a live daemon not serving ANOTHER project is
91
+ * registered on it — `init --port`
92
+ * used to leave the old daemon alive, and discovery took the lower port and dialled it;
93
+ * 2. drop dead daemons (crashed, or a stale entry left by a kill -9);
94
+ * 3. among the living, prefer a projectId match, lowest port on a tie;
95
+ * 4. return undefined when nothing matches, so the app falls back to the default port rather than
91
96
  * auto-connecting to a daemon serving a DIFFERENT project — a wrong connect is worse than an
92
97
  * honest default, because it reports another app's state as this one's.
93
98
  *
@@ -120,16 +125,44 @@ function defaultIsAlive(pid) {
120
125
  }
121
126
  }
122
127
 
128
+ const MAX_TCP_PORT = 65535;
129
+
130
+ /** @param {number} port */
131
+ const bridgeUrl = (port) => `ws://${RETICLE_CLIENT_HOST}:${String(port)}${RETICLE_WS_PATH}`;
132
+
133
+ /**
134
+ * The same order the Vite plugin's `chooseDaemonPort` states: a live daemon registered for this
135
+ * project, then the `port` in `.reticle.json`, and only then the `url` literal `reticle init` wrote
136
+ * into the generated ReticleDev component (which this value overrides page-side). The file comes
137
+ * before the literal because it is what the daemon and the CLI read: a user who edited it moved the
138
+ * daemon, and the page kept dialling the old literal with nothing saying why.
139
+ */
123
140
  function discoverDaemonUrl(cwd = process.cwd(), home = reticleHomeDir(), alive = defaultIsAlive) {
124
141
  let projectId;
142
+ let configuredPort;
125
143
  try {
126
144
  const parsed = JSON.parse(fs.readFileSync(path.join(cwd, RETICLE_CONFIG_FILE), 'utf8'));
127
145
  projectId = typeof parsed?.projectId === 'string' ? parsed.projectId : undefined;
146
+ const port = parsed?.port;
147
+ configuredPort = Number.isInteger(port) && port > 0 && port <= MAX_TCP_PORT ? port : undefined;
128
148
  } catch {
129
149
  return undefined; // no .reticle.json: this project has not been through `reticle init`
130
150
  }
131
- if (projectId === undefined || projectId.length === 0) return undefined;
132
- const dir = home;
151
+ const discovered = discoverRegisteredPort(projectId, home, alive, configuredPort);
152
+ const port = discovered ?? configuredPort;
153
+ return port === undefined ? undefined : bridgeUrl(port);
154
+ }
155
+
156
+ /**
157
+ * The live daemon registered in `home` for `projectId`, lowest port on a tie — or the configured port,
158
+ * when a live daemon registered there is not another project's (core's `pickDaemonPort`, rule 1).
159
+ * @param {string | undefined} projectId
160
+ * @param {string} dir
161
+ * @param {(pid: number) => boolean} alive
162
+ * @param {number | undefined} configuredPort
163
+ * @returns {number | undefined}
164
+ */
165
+ function discoverRegisteredPort(projectId, dir, alive, configuredPort) {
133
166
  let files;
134
167
  try {
135
168
  files = fs.readdirSync(dir);
@@ -145,13 +178,15 @@ function discoverDaemonUrl(cwd = process.cwd(), home = reticleHomeDir(), alive =
145
178
  } catch {
146
179
  continue; // a half-written or corrupt entry is not a daemon
147
180
  }
148
- if (entry?.projectId !== projectId) continue;
149
- if (typeof entry.port !== 'number' || typeof entry.pid !== 'number') continue;
181
+ if (typeof entry?.port !== 'number' || typeof entry.pid !== 'number') continue;
150
182
  if (!alive(entry.pid)) continue;
183
+ const mine = entry.projectId === projectId;
184
+ const unnamed = entry.projectId === undefined || projectId === undefined;
185
+ if (entry.port === configuredPort && (mine || unnamed)) return configuredPort;
186
+ if (projectId === undefined || projectId.length === 0 || !mine) continue;
151
187
  ports.push(entry.port);
152
188
  }
153
- if (ports.length === 0) return undefined;
154
- return `ws://${RETICLE_CLIENT_HOST}:${String(Math.min(...ports))}${RETICLE_WS_PATH}`;
189
+ return ports.length === 0 ? undefined : Math.min(...ports);
155
190
  }
156
191
 
157
192
  /**
@@ -216,6 +251,58 @@ function turbopackConfig(existing) {
216
251
  };
217
252
  }
218
253
 
254
+ /**
255
+ * The loopback hosts a Reticle-opened tab may arrive on, in the form Next compares: the Origin's
256
+ * `hostname`, which keeps the brackets on IPv6. `localhost` is not listed because Next always
257
+ * allows it.
258
+ */
259
+ const LOOPBACK_DEV_ORIGINS = ['127.0.0.1', '[::1]'];
260
+
261
+ /**
262
+ * Whether this Next release accepts `allowedDevOrigins`. It arrived in 15.2.2 and was backported to
263
+ * 14.2.30; on anything older it is an unknown key, which Next reports as "Invalid next.config.js
264
+ * options detected" on every boot. An unreadable version is assumed modern, as elsewhere here.
265
+ * @param {string | undefined} version
266
+ * @returns {boolean}
267
+ */
268
+ function knowsAllowedDevOrigins(version) {
269
+ const [major, minor, patch] = String(version)
270
+ .split('.')
271
+ .map((n) => parseInt(n, 10));
272
+ if (!Number.isFinite(major)) return true;
273
+ const at = (m, p) => minor > m || (minor === m && patch >= p);
274
+ if (major > 15) return true;
275
+ if (major === 15) return at(2, 2);
276
+ if (major === 14) return at(2, 30);
277
+ return false;
278
+ }
279
+
280
+ /** The installed Next's version, read from the APP (see resolveFromApp), or undefined. */
281
+ function installedNextVersion() {
282
+ try {
283
+ return String(require(resolveFromApp('next/package.json')).version);
284
+ } catch {
285
+ return undefined;
286
+ }
287
+ }
288
+
289
+ /**
290
+ * The app's `allowedDevOrigins` plus the loopback hosts, or undefined when this Next does not know
291
+ * the key.
292
+ *
293
+ * Next 16 BLOCKS its dev resources (`/_next/*`, HMR) for an origin outside this list, and only
294
+ * `localhost` is allowed by default. A tab opened on 127.0.0.1 or [::1] — which is where a first
295
+ * run lands when `localhost` answers slowly on a cold compile — got the HTML and nothing else: no
296
+ * hydration, so the connect effect never ran and the SDK "never dialled the bridge".
297
+ * @param {string[] | undefined} existing
298
+ * @returns {string[] | undefined}
299
+ */
300
+ function devOrigins(existing) {
301
+ if (!knowsAllowedDevOrigins(installedNextVersion())) return undefined;
302
+ const mine = Array.isArray(existing) ? existing : [];
303
+ return [...mine, ...LOOPBACK_DEV_ORIGINS.filter((host) => !mine.includes(host))];
304
+ }
305
+
219
306
  /** Whether this Next's Turbopack accepts a rule `condition` (added with Next 16). */
220
307
  function supportsRuleConditions() {
221
308
  try {
@@ -315,19 +402,27 @@ function withReticle(nextConfig = {}, options = {}) {
315
402
  * responsible and the way out (#1069).
316
403
  */
317
404
  if (process.env.NODE_ENV === 'production' && process.env[DEV_OVERRIDE_ENV] !== '1') {
318
- console.log(
319
- `[reticle] instrumentation is OFF because NODE_ENV=production. That is correct for a build. ` +
320
- `If this is a dev server you want instrumented, set ${DEV_OVERRIDE_ENV}=1 (or do not export ` +
321
- `NODE_ENV=production for it) — nothing else about your config needs to change.`,
322
- );
405
+ // Only on a dev server, where being off is the surprise. `next build` and `next typegen`
406
+ // evaluate this config under NODE_ENV=production too, and there off is simply correct: the
407
+ // line read as a fault in every build log. NEXT_PRIVATE_WORKER is set by `next dev` on the
408
+ // server process it forks (Next 13 through 16) and by no other command.
409
+ if (process.env[NEXT_DEV_WORKER_ENV] === '1') {
410
+ console.log(
411
+ `[reticle] instrumentation is OFF because NODE_ENV=production. ` +
412
+ `If this is a dev server you want instrumented, set ${DEV_OVERRIDE_ENV}=1 (or do not export ` +
413
+ `NODE_ENV=production for it) — nothing else about your config needs to change.`,
414
+ );
415
+ }
323
416
  return nextConfig;
324
417
  }
325
418
 
326
419
  const userWebpack = nextConfig.webpack;
327
420
  const token = readPairingToken();
328
421
  const daemonUrl = discoverDaemonUrl();
422
+ const allowedDevOrigins = devOrigins(nextConfig.allowedDevOrigins);
329
423
  return {
330
424
  ...nextConfig,
425
+ ...(allowedDevOrigins !== undefined ? { allowedDevOrigins } : {}),
331
426
  ...(supportsTurbopackKey() ? { turbopack: turbopackConfig(nextConfig.turbopack) } : {}),
332
427
  // Expose the token to the client bundle as process.env.NEXT_PUBLIC_RETICLE_TOKEN (Next's convention
333
428
  // for client-readable env), so a dev-only client connect can present it. Minted here if the file
@@ -365,4 +460,4 @@ function withReticle(nextConfig = {}, options = {}) {
365
460
  };
366
461
  }
367
462
 
368
- module.exports = { withReticle, readPairingToken, discoverDaemonUrl };
463
+ module.exports = { withReticle, readPairingToken, discoverDaemonUrl, knowsAllowedDevOrigins };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/next",
3
- "version": "3.3.0",
3
+ "version": "3.4.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": "3.3.0"
41
+ "@reticlehq/babel-plugin": "3.4.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "next": ">=13"