@reticlehq/next 3.7.0 → 3.8.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.
- package/index.cjs +172 -12
- package/package.json +2 -2
package/index.cjs
CHANGED
|
@@ -33,6 +33,20 @@ const DAEMON_ENTRY_SUFFIX = '.json';
|
|
|
33
33
|
// ESM/TS dependency on core, so the values are duplicated here the way the constants above are — and
|
|
34
34
|
// pinned to core's by test, because a URL that drifts produces a silent no-connect rather than an
|
|
35
35
|
// error anybody sees.
|
|
36
|
+
// Mirrors core's devServerRegistryFileName: <state>/devserver-<port>.json, the file the Vite plugin
|
|
37
|
+
// writes. The daemon reads it to learn which folder serves a page. Pinned to core's by test.
|
|
38
|
+
const DEV_SERVER_PREFIX = 'devserver-';
|
|
39
|
+
const DEV_SERVER_SUFFIX = '.json';
|
|
40
|
+
/** Set by Next when its server listens: the port, and the URL it serves on. */
|
|
41
|
+
const NEXT_PORT_ENV = 'PORT';
|
|
42
|
+
const NEXT_ORIGIN_ENV = '__NEXT_PRIVATE_ORIGIN';
|
|
43
|
+
/** Set by a dev proxy (portless) to the URL the page is served on. */
|
|
44
|
+
const DEV_PROXY_URL_ENV = 'PORTLESS_URL';
|
|
45
|
+
// Mirrors core's checkoutOf: a linked worktree's `.git` is a file, `gitdir: <main>/.git/worktrees/<name>`.
|
|
46
|
+
const GIT_DIR_NAME = '.git';
|
|
47
|
+
const GITDIR_PREFIX = /^gitdir:\s*(.+)$/m;
|
|
48
|
+
const WORKTREES_SEGMENT = 'worktrees';
|
|
49
|
+
const JSON_INDENT = 2;
|
|
36
50
|
const RETICLE_CLIENT_HOST = 'localhost';
|
|
37
51
|
const RETICLE_WS_PATH = '/reticle';
|
|
38
52
|
|
|
@@ -49,6 +63,8 @@ const RETICLE_WS_PATH = '/reticle';
|
|
|
49
63
|
* @returns {string | undefined}
|
|
50
64
|
*/
|
|
51
65
|
function ensurePairingToken(dir) {
|
|
66
|
+
// Only a home folder that is not absolute gets here (see pairingTokenDir).
|
|
67
|
+
if (!path.isAbsolute(dir)) return undefined;
|
|
52
68
|
const file = path.join(dir, PAIRING_TOKEN_FILE);
|
|
53
69
|
try {
|
|
54
70
|
const existing = fs.readFileSync(file, 'utf8').trim();
|
|
@@ -74,7 +90,35 @@ function ensurePairingToken(dir) {
|
|
|
74
90
|
* @returns {string | undefined}
|
|
75
91
|
*/
|
|
76
92
|
function readPairingToken() {
|
|
77
|
-
return ensurePairingToken(
|
|
93
|
+
return ensurePairingToken(pairingTokenDir());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A Windows drive path: `C:\...`, `C:/...` or the drive-relative `C:x`. */
|
|
97
|
+
const WINDOWS_DRIVE = /^[A-Za-z]:/;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The token folder, by the same rule as core's `resolveStateDir` (this file is plain CJS and does
|
|
101
|
+
* not depend on core). A relative override is resolved against the working folder. A Windows path
|
|
102
|
+
* on POSIX is not used, because there it is a relative path. Both warn, and neither stops `next dev`.
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
function pairingTokenDir() {
|
|
106
|
+
const override = process.env[PAIRING_TOKEN_DIR_ENV];
|
|
107
|
+
const fallback = path.join(os.homedir(), RETICLE_HOME_DIR);
|
|
108
|
+
if (override === undefined || override.length === 0 || path.isAbsolute(override)) {
|
|
109
|
+
return reticleHomeDir();
|
|
110
|
+
}
|
|
111
|
+
if (path.sep === '/' && WINDOWS_DRIVE.test(override)) {
|
|
112
|
+
console.warn(
|
|
113
|
+
`[reticle] ${PAIRING_TOKEN_DIR_ENV} "${override}" is a Windows path, and this system does not use Windows paths. Reticle uses "${fallback}".`,
|
|
114
|
+
);
|
|
115
|
+
return fallback;
|
|
116
|
+
}
|
|
117
|
+
const resolved = path.resolve(override);
|
|
118
|
+
console.warn(
|
|
119
|
+
`[reticle] ${PAIRING_TOKEN_DIR_ENV} "${override}" is a relative path. Reticle uses "${resolved}", in the folder this process runs in.`,
|
|
120
|
+
);
|
|
121
|
+
return resolved;
|
|
78
122
|
}
|
|
79
123
|
|
|
80
124
|
/**
|
|
@@ -144,21 +188,124 @@ function discoverDaemonUrl(
|
|
|
144
188
|
const port = Number(requested);
|
|
145
189
|
if (Number.isInteger(port) && port > 0 && port <= MAX_TCP_PORT) return bridgeUrl(port);
|
|
146
190
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
} catch {
|
|
155
|
-
return undefined; // no .reticle.json: this project has not been through `reticle init`
|
|
156
|
-
}
|
|
191
|
+
const parsed = readReticleConfig(cwd);
|
|
192
|
+
// No .reticle.json: this project has not been through `reticle init`.
|
|
193
|
+
if (parsed === undefined) return undefined;
|
|
194
|
+
const projectId = typeof parsed?.projectId === 'string' ? parsed.projectId : undefined;
|
|
195
|
+
const declared = parsed?.port;
|
|
196
|
+
const configuredPort =
|
|
197
|
+
Number.isInteger(declared) && declared > 0 && declared <= MAX_TCP_PORT ? declared : undefined;
|
|
157
198
|
const discovered = discoverRegisteredPort(projectId, home, alive, configuredPort);
|
|
158
199
|
const port = discovered ?? configuredPort;
|
|
159
200
|
return port === undefined ? undefined : bridgeUrl(port);
|
|
160
201
|
}
|
|
161
202
|
|
|
203
|
+
/**
|
|
204
|
+
* The project's `.reticle.json`, parsed, or undefined when there is none. A git worktree reads the
|
|
205
|
+
* main checkout's copy when it has none of its own: the file is often git-ignored, so
|
|
206
|
+
* `git worktree add` does not copy it, and the worktree then dialled the default port.
|
|
207
|
+
* @param {string} cwd
|
|
208
|
+
*/
|
|
209
|
+
function readReticleConfig(cwd) {
|
|
210
|
+
for (const dir of [cwd, mainCheckoutTwin(cwd)]) {
|
|
211
|
+
if (dir === undefined) continue;
|
|
212
|
+
try {
|
|
213
|
+
return JSON.parse(fs.readFileSync(path.join(dir, RETICLE_CONFIG_FILE), 'utf8'));
|
|
214
|
+
} catch {
|
|
215
|
+
// Missing or unreadable here: try the next place.
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The same folder in the main checkout when `dir` is in a linked git worktree, else undefined.
|
|
223
|
+
* Mirrors core's mainCheckoutTwin.
|
|
224
|
+
* @param {string} dir
|
|
225
|
+
* @returns {string | undefined}
|
|
226
|
+
*/
|
|
227
|
+
function mainCheckoutTwin(dir) {
|
|
228
|
+
let current = path.resolve(dir);
|
|
229
|
+
for (;;) {
|
|
230
|
+
const dotGit = path.join(current, GIT_DIR_NAME);
|
|
231
|
+
let text;
|
|
232
|
+
try {
|
|
233
|
+
text = fs.readFileSync(dotGit, 'utf8');
|
|
234
|
+
} catch (error) {
|
|
235
|
+
// A directory is the main checkout's own `.git`: nothing to fall back to.
|
|
236
|
+
if (error?.code === 'EISDIR') return undefined;
|
|
237
|
+
}
|
|
238
|
+
if (text !== undefined) {
|
|
239
|
+
const match = GITDIR_PREFIX.exec(text);
|
|
240
|
+
if (match === null) return undefined;
|
|
241
|
+
const parts = path.resolve(current, match[1].trim()).split(path.sep);
|
|
242
|
+
const isWorktree =
|
|
243
|
+
WORKTREES_SEGMENT === parts[parts.length - 2] && GIT_DIR_NAME === parts[parts.length - 3];
|
|
244
|
+
if (!isWorktree) return undefined;
|
|
245
|
+
return path.join(
|
|
246
|
+
parts.slice(0, -3).join(path.sep),
|
|
247
|
+
path.relative(current, path.resolve(dir)),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
const parent = path.dirname(current);
|
|
251
|
+
if (parent === current) return undefined;
|
|
252
|
+
current = parent;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The files this process announced, so a config evaluated twice registers one cleanup. */
|
|
257
|
+
const announced = new Set();
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Say where this dev server serves from, as the Vite plugin does: `<state>/devserver-<port>.json`.
|
|
261
|
+
* The daemon reads it to learn which git worktree served a page, also behind a dev proxy and on a
|
|
262
|
+
* platform with no `lsof`. Nothing before Next listens: it sets `PORT` then. Returns the withdraw.
|
|
263
|
+
* @param {Record<string, string | undefined>} env
|
|
264
|
+
* @param {string} root
|
|
265
|
+
* @param {string} home
|
|
266
|
+
* @param {string | undefined} projectId
|
|
267
|
+
* @returns {() => void}
|
|
268
|
+
*/
|
|
269
|
+
function announceDevServer(
|
|
270
|
+
env = process.env,
|
|
271
|
+
root = process.cwd(),
|
|
272
|
+
home = reticleHomeDir(STATE_DIR_ENV),
|
|
273
|
+
projectId = undefined,
|
|
274
|
+
) {
|
|
275
|
+
const port = Number(env[NEXT_PORT_ENV]);
|
|
276
|
+
if (!Number.isInteger(port) || port <= 0 || port > MAX_TCP_PORT) return () => {};
|
|
277
|
+
const file = path.join(home, `${DEV_SERVER_PREFIX}${String(port)}${DEV_SERVER_SUFFIX}`);
|
|
278
|
+
const url = env[DEV_PROXY_URL_ENV] || env[NEXT_ORIGIN_ENV] || `http://localhost:${String(port)}/`;
|
|
279
|
+
const sdkVersion = sdkPackageVersion();
|
|
280
|
+
const entry = {
|
|
281
|
+
port,
|
|
282
|
+
pid: process.pid,
|
|
283
|
+
root,
|
|
284
|
+
url,
|
|
285
|
+
...(typeof sdkVersion === 'string' && sdkVersion.length > 0 ? { sdkVersion } : {}),
|
|
286
|
+
startedAt: Date.now(),
|
|
287
|
+
...(projectId === undefined ? {} : { projectId }),
|
|
288
|
+
};
|
|
289
|
+
try {
|
|
290
|
+
fs.mkdirSync(home, { recursive: true });
|
|
291
|
+
fs.writeFileSync(file, `${JSON.stringify(entry, null, JSON_INDENT)}\n`);
|
|
292
|
+
} catch {
|
|
293
|
+
// No home directory, a read-only filesystem, a sandbox. The app still serves.
|
|
294
|
+
return () => {};
|
|
295
|
+
}
|
|
296
|
+
const withdraw = () => {
|
|
297
|
+
announced.delete(file);
|
|
298
|
+
try {
|
|
299
|
+
fs.rmSync(file, { force: true });
|
|
300
|
+
} catch {
|
|
301
|
+
// Already gone. The daemon's liveness check covers what we cannot remove.
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
if (!announced.has(file)) process.once('exit', withdraw);
|
|
305
|
+
announced.add(file);
|
|
306
|
+
return withdraw;
|
|
307
|
+
}
|
|
308
|
+
|
|
162
309
|
/**
|
|
163
310
|
* The live daemon registered in `home` for `projectId`, lowest port on a tie — or the configured port,
|
|
164
311
|
* when a live daemon registered there is not another project's (core's `pickDaemonPort`, rule 1).
|
|
@@ -425,6 +572,13 @@ function withReticle(nextConfig = {}, options = {}) {
|
|
|
425
572
|
const userWebpack = nextConfig.webpack;
|
|
426
573
|
const token = readPairingToken();
|
|
427
574
|
const daemonUrl = discoverDaemonUrl();
|
|
575
|
+
const projectId = readReticleConfig(process.cwd())?.projectId;
|
|
576
|
+
announceDevServer(
|
|
577
|
+
process.env,
|
|
578
|
+
process.cwd(),
|
|
579
|
+
reticleHomeDir(STATE_DIR_ENV),
|
|
580
|
+
typeof projectId === 'string' ? projectId : undefined,
|
|
581
|
+
);
|
|
428
582
|
const allowedDevOrigins = devOrigins(nextConfig.allowedDevOrigins);
|
|
429
583
|
return {
|
|
430
584
|
...nextConfig,
|
|
@@ -473,4 +627,10 @@ function withReticle(nextConfig = {}, options = {}) {
|
|
|
473
627
|
};
|
|
474
628
|
}
|
|
475
629
|
|
|
476
|
-
module.exports = {
|
|
630
|
+
module.exports = {
|
|
631
|
+
withReticle,
|
|
632
|
+
readPairingToken,
|
|
633
|
+
discoverDaemonUrl,
|
|
634
|
+
knowsAllowedDevOrigins,
|
|
635
|
+
announceDevServer,
|
|
636
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reticlehq/next",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.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": "3.
|
|
41
|
+
"@reticlehq/babel-plugin": "3.8.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"next": ">=13"
|