@reticlehq/next 2.12.0 → 2.13.1

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 +37 -12
  2. package/package.json +2 -2
package/index.cjs CHANGED
@@ -7,6 +7,7 @@
7
7
  const fs = require('node:fs');
8
8
  const os = require('node:os');
9
9
  const path = require('node:path');
10
+ const crypto = require('node:crypto');
10
11
 
11
12
  // Kept in sync with @reticlehq/core (ReticleDir / ReticleEnv). This package is plain CJS tooling and
12
13
  // deliberately has no ESM/TS dependency on core, so the two constants are mirrored here.
@@ -25,24 +26,47 @@ const RETICLE_CLIENT_HOST = 'localhost';
25
26
  const RETICLE_WS_PATH = '/reticle';
26
27
 
27
28
  /**
28
- * Read the daemon's auto-provisioned pairing token (~/.reticle/pairing-token, or the
29
- * RETICLE_PAIRING_TOKEN_DIR override). Node-side only. Returns undefined if the daemon hasn't started
30
- * yet (start it before `next dev`); the client then connects without a token and the page reloads once
31
- * the daemon is up and the config is re-read.
29
+ * Read the pairing token, or create it — whichever process gets there first.
30
+ *
31
+ * The token used to be read ONCE, when next.config was evaluated. Start `next dev` before the
32
+ * daemon and that value was empty, every page was refused, and a reload could not help: there was
33
+ * no token page-side to pick up. The comment that claimed "the client then connects without a token
34
+ * and the page reloads once it has" was false for Next.
35
+ *
36
+ * Same mint as the daemon (24 random bytes, 0600 file, never overwrite). An existing token is
37
+ * reused so a plugin-injected page keeps working after the daemon bounces.
38
+ * @param {string} dir
32
39
  * @returns {string | undefined}
33
40
  */
34
- function readPairingToken() {
35
- const override = process.env[PAIRING_TOKEN_DIR_ENV];
36
- const dir =
37
- override !== undefined && override.length > 0 ? override : path.join(os.homedir(), '.reticle');
41
+ function ensurePairingToken(dir) {
42
+ const file = path.join(dir, PAIRING_TOKEN_FILE);
43
+ try {
44
+ const existing = fs.readFileSync(file, 'utf8').trim();
45
+ if (existing.length > 0) return existing;
46
+ } catch {
47
+ /* missing or unreadable — fall through and create one */
48
+ }
38
49
  try {
39
- const token = fs.readFileSync(path.join(dir, PAIRING_TOKEN_FILE), 'utf8').trim();
40
- return token.length > 0 ? token : undefined;
50
+ const token = crypto.randomBytes(24).toString('hex');
51
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
52
+ fs.writeFileSync(file, token, { encoding: 'utf8', mode: 0o600 });
53
+ fs.chmodSync(file, 0o600);
54
+ return token;
41
55
  } catch {
42
56
  return undefined;
43
57
  }
44
58
  }
45
59
 
60
+ /**
61
+ * Read or mint the daemon's auto-provisioned pairing token (~/.reticle/pairing-token, or the
62
+ * RETICLE_PAIRING_TOKEN_DIR override). Node-side only. Minting here is what makes `next dev` before
63
+ * the daemon still authenticate.
64
+ * @returns {string | undefined}
65
+ */
66
+ function readPairingToken() {
67
+ return ensurePairingToken(reticleHomeDir());
68
+ }
69
+
46
70
  /**
47
71
  * Find the live daemon serving THIS project and return its websocket URL.
48
72
  *
@@ -249,8 +273,9 @@ function withReticle(nextConfig = {}) {
249
273
  ...nextConfig,
250
274
  ...(supportsTurbopackKey() ? { turbopack: turbopackConfig(nextConfig.turbopack) } : {}),
251
275
  // Expose the token to the client bundle as process.env.NEXT_PUBLIC_RETICLE_TOKEN (Next's convention
252
- // for client-readable env), so a dev-only client connect can present it. Omitted when the daemon
253
- // hasn't provisioned one yet — the client then connects without it and the page reloads once it has.
276
+ // for client-readable env), so a dev-only client connect can present it. Minted here if the file
277
+ // is missing: Next evaluates this once, so an empty value is frozen and a reload cannot pick a
278
+ // later token up. Omitted only when the directory is unwritable.
254
279
  env: {
255
280
  ...nextConfig.env,
256
281
  ...(token !== undefined ? { NEXT_PUBLIC_RETICLE_TOKEN: token } : {}),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/next",
3
- "version": "2.12.0",
3
+ "version": "2.13.1",
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.12.0"
41
+ "@reticlehq/babel-plugin": "2.13.1"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "next": ">=13"