@stagepass/cli 2.2.6 → 2.3.0-beta.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/README.md +61 -13
- package/TESTING-macos.md +35 -8
- package/bin/index.js +4 -1
- package/package.json +9 -8
- package/src/cli.js +62 -7
- package/src/commands/certs.js +375 -19
- package/src/commands/daemon.js +75 -7
- package/src/commands/dev.js +419 -0
- package/src/commands/link.js +66 -25
- package/src/commands/new.js +397 -41
- package/src/commands/npm-run.js +8 -0
- package/src/commands/reload.js +2 -2
- package/src/commands/setup.js +5 -5
- package/src/commands/start.js +386 -83
- package/src/commands/stop.js +50 -31
- package/src/commands/unlink.js +8 -6
- package/src/commands/upgrade.js +51 -12
- package/src/lib/apply-site-block.js +48 -0
- package/src/lib/autostart.js +300 -3
- package/src/lib/caddyfile.js +72 -13
- package/src/lib/cdn-snippets.js +145 -3
- package/src/lib/certs.js +334 -47
- package/src/lib/dev-port.js +45 -0
- package/src/lib/doctor-site.js +61 -5
- package/src/lib/doctor.js +48 -4
- package/src/lib/ensure-running.js +89 -0
- package/src/lib/exec.js +72 -24
- package/src/lib/live-server.js +484 -0
- package/src/lib/node-engine.js +55 -0
- package/src/lib/notify.js +62 -0
- package/src/lib/origins.js +27 -5
- package/src/lib/package-versions.js +9 -0
- package/src/lib/paths.js +18 -0
- package/src/lib/php-fpm.js +33 -0
- package/src/lib/project-sync.js +165 -20
- package/src/lib/projects.js +137 -7
- package/src/lib/prompts.js +19 -0
- package/src/lib/registry.js +27 -6
- package/src/lib/scaffold.js +288 -11
- package/src/lib/site-detect.js +78 -25
- package/src/lib/snippets.js +14 -11
- package/src/lib/spinner.js +60 -0
- package/src/lib/versions.js +20 -8
- package/templates/project/AGENTS.md.tmpl +4 -3
- package/templates/project/README.md.tmpl +35 -6
- package/templates/project/api/README.md.tmpl +7 -0
- package/templates/project/examples/php/README.md.tmpl +18 -0
- package/templates/project/examples/php/form.php +105 -0
- package/templates/project/examples/php/proxy.php +171 -0
- package/templates/project/scripts/build.mjs +200 -37
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local live-reload event server for `stagepass dev`.
|
|
3
|
+
* Binds 127.0.0.1 only. Serves Server-Sent Events at /__stagepass/live.
|
|
4
|
+
* No request data is reflected into responses.
|
|
5
|
+
*
|
|
6
|
+
* Change classification is content-based: identical rewrites (same bytes) are
|
|
7
|
+
* ignored. Only real CSS changes batch as `{type:'css'}`; any other real change
|
|
8
|
+
* is `{type:'reload'}`. `manifest.json`, dotfiles, `~` backups and `.map` files
|
|
9
|
+
* are ignored.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import http from 'node:http';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import { watch as fsWatch } from 'node:fs';
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
|
|
18
|
+
export const LIVE_PATH = '/__stagepass/live';
|
|
19
|
+
export const MAX_CONNECTIONS = 32;
|
|
20
|
+
export const HEARTBEAT_MS = 25_000;
|
|
21
|
+
export const DEFAULT_DEBOUNCE_MS = 100;
|
|
22
|
+
/** Skip hashing (treat as always-changed) above this size. */
|
|
23
|
+
export const MAX_HASH_BYTES = 4 * 1024 * 1024;
|
|
24
|
+
/** Bound memory for the content-hash map. */
|
|
25
|
+
export const MAX_TRACKED_FILES = 4096;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} projectRoot absolute project root
|
|
29
|
+
* @param {string} outDirRel relative output dir (e.g. "dist")
|
|
30
|
+
* @returns {{ ok: true, abs: string, rel: string } | { ok: false, error: string }}
|
|
31
|
+
*/
|
|
32
|
+
export function resolveOutputDir(projectRoot, outDirRel = 'dist') {
|
|
33
|
+
const root = path.resolve(projectRoot);
|
|
34
|
+
const raw = typeof outDirRel === 'string' && outDirRel.trim() ? outDirRel.trim() : 'dist';
|
|
35
|
+
if (path.isAbsolute(raw) || raw.includes('\0')) {
|
|
36
|
+
return { ok: false, error: 'Output directory must be a relative path inside the project.' };
|
|
37
|
+
}
|
|
38
|
+
const normalized = path.normalize(raw).replace(/\\/g, '/');
|
|
39
|
+
if (
|
|
40
|
+
normalized === '..' ||
|
|
41
|
+
normalized.startsWith('../') ||
|
|
42
|
+
normalized.split('/').includes('..')
|
|
43
|
+
) {
|
|
44
|
+
return { ok: false, error: 'Output directory must stay inside the project root.' };
|
|
45
|
+
}
|
|
46
|
+
const abs = path.resolve(root, normalized);
|
|
47
|
+
const relToRoot = path.relative(root, abs);
|
|
48
|
+
if (relToRoot.startsWith('..') || path.isAbsolute(relToRoot)) {
|
|
49
|
+
return { ok: false, error: 'Output directory must stay inside the project root.' };
|
|
50
|
+
}
|
|
51
|
+
return { ok: true, abs, rel: relToRoot.split(path.sep).join('/') || '.' };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Classify changed relative URL paths into css vs reload.
|
|
56
|
+
* @param {string[]} urlPaths
|
|
57
|
+
* @returns {'css'|'reload'}
|
|
58
|
+
*/
|
|
59
|
+
export function classifyChange(urlPaths) {
|
|
60
|
+
if (urlPaths.length === 0) return 'reload';
|
|
61
|
+
const onlyCss = urlPaths.every((p) => /\.css$/i.test(p.split('?')[0]));
|
|
62
|
+
return onlyCss ? 'css' : 'reload';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} base basename
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
*/
|
|
69
|
+
export function isIgnoredWatchBase(base) {
|
|
70
|
+
return (
|
|
71
|
+
base.startsWith('.') ||
|
|
72
|
+
base.endsWith('~') ||
|
|
73
|
+
base.endsWith('.map') ||
|
|
74
|
+
base === 'manifest.json'
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Turn an absolute file path under outAbs into a site-relative URL path
|
|
80
|
+
* (project-root relative, with leading slash). Returns null when outside or ignored.
|
|
81
|
+
* @param {string} fileAbs
|
|
82
|
+
* @param {string} projectRoot
|
|
83
|
+
* @param {string} outAbs
|
|
84
|
+
*/
|
|
85
|
+
export function toUrlPath(fileAbs, projectRoot, outAbs) {
|
|
86
|
+
const root = path.resolve(projectRoot);
|
|
87
|
+
const out = path.resolve(outAbs);
|
|
88
|
+
const file = path.resolve(fileAbs);
|
|
89
|
+
const relOut = path.relative(out, file);
|
|
90
|
+
if (relOut.startsWith('..') || path.isAbsolute(relOut)) return null;
|
|
91
|
+
const relProject = path.relative(root, file);
|
|
92
|
+
if (relProject.startsWith('..') || path.isAbsolute(relProject)) return null;
|
|
93
|
+
const base = path.basename(file);
|
|
94
|
+
if (isIgnoredWatchBase(base)) return null;
|
|
95
|
+
return '/' + relProject.split(path.sep).join('/');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolve a path that must stay under outAbs (no escape). Returns null if outside.
|
|
100
|
+
* Does not follow the final path if missing (deletion); still rejects lexical escapes.
|
|
101
|
+
* @param {string} fileAbs
|
|
102
|
+
* @param {string} outAbs
|
|
103
|
+
* @returns {string|null}
|
|
104
|
+
*/
|
|
105
|
+
export function resolveUnderOutDir(fileAbs, outAbs) {
|
|
106
|
+
const out = path.resolve(outAbs);
|
|
107
|
+
const file = path.resolve(fileAbs);
|
|
108
|
+
const rel = path.relative(out, file);
|
|
109
|
+
if (rel.startsWith('..') || path.isAbsolute(rel) || rel.includes('\0')) return null;
|
|
110
|
+
return file;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {string} fileAbs
|
|
115
|
+
* @param {number} [maxBytes]
|
|
116
|
+
* @returns {{ kind: 'missing' } | { kind: 'hash', hash: string } | { kind: 'large', token: string } | { kind: 'error' }}
|
|
117
|
+
*/
|
|
118
|
+
export function contentFingerprint(fileAbs, maxBytes = MAX_HASH_BYTES) {
|
|
119
|
+
try {
|
|
120
|
+
const st = fs.statSync(fileAbs);
|
|
121
|
+
if (!st.isFile()) return { kind: 'error' };
|
|
122
|
+
if (st.size > maxBytes) {
|
|
123
|
+
return { kind: 'large', token: `large:${st.size}:${Math.trunc(st.mtimeMs)}` };
|
|
124
|
+
}
|
|
125
|
+
const buf = fs.readFileSync(fileAbs);
|
|
126
|
+
return {
|
|
127
|
+
kind: 'hash',
|
|
128
|
+
hash: createHash('sha256').update(buf).digest('hex'),
|
|
129
|
+
};
|
|
130
|
+
} catch (err) {
|
|
131
|
+
if (err && typeof err === 'object' && 'code' in err && err.code === 'ENOENT') {
|
|
132
|
+
return { kind: 'missing' };
|
|
133
|
+
}
|
|
134
|
+
return { kind: 'error' };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Format one SSE data event.
|
|
140
|
+
* @param {{ type: string, paths: string[] }} payload
|
|
141
|
+
*/
|
|
142
|
+
export function formatSseEvent(payload) {
|
|
143
|
+
return `data: ${JSON.stringify(payload)}\n\n`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Walk files under dir (non-following for dirs outside via resolve). Soft-fail.
|
|
148
|
+
* @param {string} dirAbs
|
|
149
|
+
* @param {(fileAbs: string) => void} visit
|
|
150
|
+
*/
|
|
151
|
+
function walkFiles(dirAbs, visit) {
|
|
152
|
+
/** @type {string[]} */
|
|
153
|
+
const stack = [dirAbs];
|
|
154
|
+
while (stack.length) {
|
|
155
|
+
const dir = stack.pop();
|
|
156
|
+
let entries;
|
|
157
|
+
try {
|
|
158
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
159
|
+
} catch {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
for (const ent of entries) {
|
|
163
|
+
const full = path.join(dir, ent.name);
|
|
164
|
+
if (ent.isDirectory()) {
|
|
165
|
+
stack.push(full);
|
|
166
|
+
} else if (ent.isFile()) {
|
|
167
|
+
visit(full);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @typedef {object} LiveServerOptions
|
|
175
|
+
* @property {string} projectRoot
|
|
176
|
+
* @property {string} [outDir]
|
|
177
|
+
* @property {number} [debounceMs]
|
|
178
|
+
* @property {number} [maxConnections]
|
|
179
|
+
* @property {number} [heartbeatMs]
|
|
180
|
+
* @property {(msg: string) => void} [log]
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Create the live-reload HTTP server (not yet listening).
|
|
185
|
+
* @param {LiveServerOptions} options
|
|
186
|
+
*/
|
|
187
|
+
export function createLiveServer(options) {
|
|
188
|
+
const projectRoot = path.resolve(options.projectRoot);
|
|
189
|
+
const outResolved = resolveOutputDir(projectRoot, options.outDir || 'dist');
|
|
190
|
+
if (!outResolved.ok) {
|
|
191
|
+
throw new Error(outResolved.error);
|
|
192
|
+
}
|
|
193
|
+
const outAbs = outResolved.abs;
|
|
194
|
+
const debounceMs =
|
|
195
|
+
typeof options.debounceMs === 'number' && options.debounceMs >= 0
|
|
196
|
+
? options.debounceMs
|
|
197
|
+
: DEFAULT_DEBOUNCE_MS;
|
|
198
|
+
const maxConnections =
|
|
199
|
+
typeof options.maxConnections === 'number' && options.maxConnections > 0
|
|
200
|
+
? options.maxConnections
|
|
201
|
+
: MAX_CONNECTIONS;
|
|
202
|
+
const heartbeatMs =
|
|
203
|
+
typeof options.heartbeatMs === 'number' && options.heartbeatMs > 0
|
|
204
|
+
? options.heartbeatMs
|
|
205
|
+
: HEARTBEAT_MS;
|
|
206
|
+
const log = options.log || (() => {});
|
|
207
|
+
|
|
208
|
+
/** @type {Set<import('node:http').ServerResponse>} */
|
|
209
|
+
const clients = new Set();
|
|
210
|
+
/** @type {Set<string>} */
|
|
211
|
+
const pending = new Set();
|
|
212
|
+
/** @type {ReturnType<typeof setTimeout>|null} */
|
|
213
|
+
let debounceTimer = null;
|
|
214
|
+
/** @type {import('node:fs').FSWatcher|null} */
|
|
215
|
+
let watcher = null;
|
|
216
|
+
/** @type {ReturnType<typeof setInterval>|null} */
|
|
217
|
+
let heartbeat = null;
|
|
218
|
+
/** @type {Map<string, string>} abs path → fingerprint token */
|
|
219
|
+
const fileHashes = new Map();
|
|
220
|
+
|
|
221
|
+
function rememberHash(fileAbs, token) {
|
|
222
|
+
if (fileHashes.has(fileAbs)) {
|
|
223
|
+
fileHashes.set(fileAbs, token);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (fileHashes.size >= MAX_TRACKED_FILES) {
|
|
227
|
+
const first = fileHashes.keys().next().value;
|
|
228
|
+
if (first !== undefined) fileHashes.delete(first);
|
|
229
|
+
}
|
|
230
|
+
fileHashes.set(fileAbs, token);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function seedHashes() {
|
|
234
|
+
fileHashes.clear();
|
|
235
|
+
walkFiles(outAbs, (fileAbs) => {
|
|
236
|
+
const safe = resolveUnderOutDir(fileAbs, outAbs);
|
|
237
|
+
if (!safe) return;
|
|
238
|
+
if (isIgnoredWatchBase(path.basename(safe))) return;
|
|
239
|
+
const fp = contentFingerprint(safe);
|
|
240
|
+
if (fp.kind === 'hash') rememberHash(safe, fp.hash);
|
|
241
|
+
else if (fp.kind === 'large') rememberHash(safe, fp.token);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Compare current disk content to the last known fingerprint.
|
|
247
|
+
* Updates the hash map when the file exists. Intended for flush time so
|
|
248
|
+
* mid-write fs.watch races do not create false positives.
|
|
249
|
+
* @param {string} fileAbs
|
|
250
|
+
* @returns {boolean} true when content actually changed (or deleted)
|
|
251
|
+
*/
|
|
252
|
+
function hasContentChanged(fileAbs) {
|
|
253
|
+
const safe = resolveUnderOutDir(fileAbs, outAbs);
|
|
254
|
+
if (!safe) return false;
|
|
255
|
+
|
|
256
|
+
const fp = contentFingerprint(safe);
|
|
257
|
+
if (fp.kind === 'missing') {
|
|
258
|
+
if (!fileHashes.has(safe)) return false;
|
|
259
|
+
fileHashes.delete(safe);
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
if (fp.kind === 'error') {
|
|
263
|
+
// Unreadable: treat as changed so we do not miss a real update.
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
const token = fp.kind === 'hash' ? fp.hash : fp.token;
|
|
267
|
+
const prev = fileHashes.get(safe);
|
|
268
|
+
if (prev === token) return false;
|
|
269
|
+
rememberHash(safe, token);
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function broadcast(payload) {
|
|
274
|
+
const body = formatSseEvent(payload);
|
|
275
|
+
for (const res of clients) {
|
|
276
|
+
try {
|
|
277
|
+
res.write(body);
|
|
278
|
+
} catch {
|
|
279
|
+
clients.delete(res);
|
|
280
|
+
try {
|
|
281
|
+
res.end();
|
|
282
|
+
} catch {
|
|
283
|
+
/* ignore */
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function flushPending() {
|
|
290
|
+
debounceTimer = null;
|
|
291
|
+
if (pending.size === 0) return;
|
|
292
|
+
/** @type {string[]} */
|
|
293
|
+
const changed = [];
|
|
294
|
+
for (const fileAbs of pending) {
|
|
295
|
+
if (!hasContentChanged(fileAbs)) continue;
|
|
296
|
+
const urlPath = toUrlPath(fileAbs, projectRoot, outAbs);
|
|
297
|
+
if (urlPath) changed.push(urlPath);
|
|
298
|
+
}
|
|
299
|
+
pending.clear();
|
|
300
|
+
if (changed.length === 0) return;
|
|
301
|
+
const paths = [...new Set(changed)].sort();
|
|
302
|
+
const type = classifyChange(paths);
|
|
303
|
+
broadcast({ type, paths });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function noteChange(fileAbs) {
|
|
307
|
+
const safe = resolveUnderOutDir(fileAbs, outAbs);
|
|
308
|
+
if (!safe) return;
|
|
309
|
+
// Ignore junk / manifest early; defer content compare until debounce flush
|
|
310
|
+
// so identical rewrites (and mid-write watch races) do not false-trigger.
|
|
311
|
+
if (!toUrlPath(safe, projectRoot, outAbs)) return;
|
|
312
|
+
pending.add(safe);
|
|
313
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
314
|
+
debounceTimer = setTimeout(flushPending, debounceMs);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function startWatch() {
|
|
318
|
+
try {
|
|
319
|
+
fs.mkdirSync(outAbs, { recursive: true });
|
|
320
|
+
} catch {
|
|
321
|
+
/* ignore */
|
|
322
|
+
}
|
|
323
|
+
seedHashes();
|
|
324
|
+
try {
|
|
325
|
+
watcher = fsWatch(outAbs, { recursive: true }, (_event, filename) => {
|
|
326
|
+
if (!filename) return;
|
|
327
|
+
const name = typeof filename === 'string' ? filename : String(filename);
|
|
328
|
+
// Join only; resolveUnderOutDir rejects escapes (including .. segments).
|
|
329
|
+
noteChange(path.join(outAbs, name));
|
|
330
|
+
});
|
|
331
|
+
watcher.on('error', () => {
|
|
332
|
+
/* keep server up; next change may recover */
|
|
333
|
+
});
|
|
334
|
+
} catch (err) {
|
|
335
|
+
log(
|
|
336
|
+
`Live reload: could not watch ${outAbs} (${err instanceof Error ? err.message : String(err)})`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function stopWatch() {
|
|
342
|
+
if (debounceTimer) {
|
|
343
|
+
clearTimeout(debounceTimer);
|
|
344
|
+
debounceTimer = null;
|
|
345
|
+
}
|
|
346
|
+
pending.clear();
|
|
347
|
+
fileHashes.clear();
|
|
348
|
+
if (watcher) {
|
|
349
|
+
try {
|
|
350
|
+
watcher.close();
|
|
351
|
+
} catch {
|
|
352
|
+
/* ignore */
|
|
353
|
+
}
|
|
354
|
+
watcher = null;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const server = http.createServer((req, res) => {
|
|
359
|
+
const method = req.method || 'GET';
|
|
360
|
+
let pathname = '/';
|
|
361
|
+
try {
|
|
362
|
+
pathname = new URL(req.url || '/', 'http://127.0.0.1').pathname;
|
|
363
|
+
} catch {
|
|
364
|
+
pathname = '/';
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (pathname !== LIVE_PATH) {
|
|
368
|
+
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
369
|
+
res.end('Not found');
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (method === 'OPTIONS') {
|
|
374
|
+
res.writeHead(204);
|
|
375
|
+
res.end();
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
380
|
+
res.writeHead(405, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
381
|
+
res.end('Method not allowed');
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (clients.size >= maxConnections) {
|
|
386
|
+
res.writeHead(503, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
387
|
+
res.end('Too many connections');
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
res.writeHead(200, {
|
|
392
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
393
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
394
|
+
Connection: 'keep-alive',
|
|
395
|
+
'X-Accel-Buffering': 'no',
|
|
396
|
+
});
|
|
397
|
+
if (method === 'HEAD') {
|
|
398
|
+
res.end();
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
res.write(': connected\n\n');
|
|
403
|
+
clients.add(res);
|
|
404
|
+
|
|
405
|
+
const onClose = () => {
|
|
406
|
+
clients.delete(res);
|
|
407
|
+
req.off('close', onClose);
|
|
408
|
+
};
|
|
409
|
+
req.on('close', onClose);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
function startHeartbeat() {
|
|
413
|
+
heartbeat = setInterval(() => {
|
|
414
|
+
for (const res of clients) {
|
|
415
|
+
try {
|
|
416
|
+
res.write(': ping\n\n');
|
|
417
|
+
} catch {
|
|
418
|
+
clients.delete(res);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}, heartbeatMs);
|
|
422
|
+
if (typeof heartbeat.unref === 'function') heartbeat.unref();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* @param {number} [port] 0 = ephemeral
|
|
427
|
+
* @returns {Promise<{ port: number, close: () => Promise<void> }>}
|
|
428
|
+
*/
|
|
429
|
+
function listen(port = 0) {
|
|
430
|
+
return new Promise((resolve, reject) => {
|
|
431
|
+
const onError = (err) => {
|
|
432
|
+
server.off('listening', onListening);
|
|
433
|
+
reject(err);
|
|
434
|
+
};
|
|
435
|
+
const onListening = () => {
|
|
436
|
+
server.off('error', onError);
|
|
437
|
+
const addr = server.address();
|
|
438
|
+
const bound =
|
|
439
|
+
addr && typeof addr === 'object' && typeof addr.port === 'number' ? addr.port : 0;
|
|
440
|
+
startWatch();
|
|
441
|
+
startHeartbeat();
|
|
442
|
+
resolve({
|
|
443
|
+
port: bound,
|
|
444
|
+
close: async () => {
|
|
445
|
+
if (heartbeat) {
|
|
446
|
+
clearInterval(heartbeat);
|
|
447
|
+
heartbeat = null;
|
|
448
|
+
}
|
|
449
|
+
stopWatch();
|
|
450
|
+
for (const res of clients) {
|
|
451
|
+
try {
|
|
452
|
+
res.end();
|
|
453
|
+
} catch {
|
|
454
|
+
/* ignore */
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
clients.clear();
|
|
458
|
+
await new Promise((resClose, rejClose) => {
|
|
459
|
+
server.close((err) => (err ? rejClose(err) : resClose()));
|
|
460
|
+
});
|
|
461
|
+
},
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
server.once('error', onError);
|
|
465
|
+
server.once('listening', onListening);
|
|
466
|
+
server.listen(port, '127.0.0.1');
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return {
|
|
471
|
+
server,
|
|
472
|
+
listen,
|
|
473
|
+
/** @internal test helpers */
|
|
474
|
+
_clients: clients,
|
|
475
|
+
_noteChange: noteChange,
|
|
476
|
+
_flushPending: flushPending,
|
|
477
|
+
_broadcast: broadcast,
|
|
478
|
+
_fileHashes: fileHashes,
|
|
479
|
+
_seedHashes: seedHashes,
|
|
480
|
+
_hasContentChanged: hasContentChanged,
|
|
481
|
+
outAbs,
|
|
482
|
+
outRel: outResolved.rel,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Early Node engine gate for the CLI bin.
|
|
3
|
+
* Kept free of heavy deps so it can run before importing commander/inquirer.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const MIN_NODE_MAJOR = 20;
|
|
7
|
+
export const MIN_NODE_MINOR = 17;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} [version] process.version-style (e.g. v20.10.0)
|
|
11
|
+
* @returns {{ major: number, minor: number, patch: number } | null}
|
|
12
|
+
*/
|
|
13
|
+
export function parseNodeVersion(version = process.version) {
|
|
14
|
+
const m = /^v?(\d+)\.(\d+)\.(\d+)/.exec(String(version || ''));
|
|
15
|
+
if (!m) return null;
|
|
16
|
+
return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]) };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} [version]
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
export function meetsNodeEngine(version = process.version) {
|
|
24
|
+
const parsed = parseNodeVersion(version);
|
|
25
|
+
if (!parsed) return false;
|
|
26
|
+
if (parsed.major > MIN_NODE_MAJOR) return true;
|
|
27
|
+
if (parsed.major < MIN_NODE_MAJOR) return false;
|
|
28
|
+
return parsed.minor >= MIN_NODE_MINOR;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} [version]
|
|
33
|
+
* @returns {string}
|
|
34
|
+
*/
|
|
35
|
+
export function unsupportedNodeMessage(version = process.version) {
|
|
36
|
+
return (
|
|
37
|
+
`@stagepass/cli requires Node.js >=${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0 ` +
|
|
38
|
+
`(current: ${version}). Upgrade Node, or pin @stagepass/cli@2.2.7 on Node 18.`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {object} [opts]
|
|
44
|
+
* @param {string} [opts.version]
|
|
45
|
+
* @param {(msg: string) => void} [opts.write]
|
|
46
|
+
* @param {(code: number) => void} [opts.exit]
|
|
47
|
+
*/
|
|
48
|
+
export function assertNodeEngine(opts = {}) {
|
|
49
|
+
const version = opts.version || process.version;
|
|
50
|
+
if (meetsNodeEngine(version)) return;
|
|
51
|
+
const write = opts.write || ((msg) => console.error(msg));
|
|
52
|
+
const exit = opts.exit || ((code) => process.exit(code));
|
|
53
|
+
write(unsupportedNodeMessage(version));
|
|
54
|
+
exit(1);
|
|
55
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { defaultExec } from './exec.js';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_TITLE = 'Stagepass';
|
|
4
|
+
const MAX_TITLE = 80;
|
|
5
|
+
const MAX_BODY = 200;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Truncate notification text for display.
|
|
9
|
+
* @param {unknown} value
|
|
10
|
+
* @param {number} max
|
|
11
|
+
*/
|
|
12
|
+
export function truncateNotifyText(value, max) {
|
|
13
|
+
const text = String(value == null ? '' : value).replace(/\s+/g, ' ').trim();
|
|
14
|
+
if (text.length <= max) return text;
|
|
15
|
+
return `${text.slice(0, Math.max(0, max - 1))}…`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Build `osascript` argv so title/body are passed via AppleScript `argv`
|
|
20
|
+
* (never interpolated into the script source).
|
|
21
|
+
*
|
|
22
|
+
* @param {string} title
|
|
23
|
+
* @param {string} body
|
|
24
|
+
* @returns {string[]}
|
|
25
|
+
*/
|
|
26
|
+
export function buildOsascriptNotificationArgs(title, body) {
|
|
27
|
+
const safeTitle = truncateNotifyText(title || DEFAULT_TITLE, MAX_TITLE) || DEFAULT_TITLE;
|
|
28
|
+
const safeBody = truncateNotifyText(body || '', MAX_BODY) || 'Stagepass reported a problem.';
|
|
29
|
+
// argv items after -- are positional; script never embeds caller text.
|
|
30
|
+
const script = [
|
|
31
|
+
'on run argv',
|
|
32
|
+
' set theTitle to item 1 of argv',
|
|
33
|
+
' set theMessage to item 2 of argv',
|
|
34
|
+
' display notification theMessage with title theTitle',
|
|
35
|
+
'end run',
|
|
36
|
+
].join('\n');
|
|
37
|
+
return ['-e', script, '--', safeTitle, safeBody];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Fire-and-forget macOS notification. Never throws; never blocks callers long.
|
|
42
|
+
*
|
|
43
|
+
* @param {object} opts
|
|
44
|
+
* @param {string} [opts.title]
|
|
45
|
+
* @param {string} opts.body
|
|
46
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
47
|
+
* @param {(ms: number) => Promise<void>} [opts.sleep]
|
|
48
|
+
* @param {number} [opts.timeoutMs]
|
|
49
|
+
* @returns {Promise<boolean>} true if the process exited 0
|
|
50
|
+
*/
|
|
51
|
+
export async function notifyMac(opts) {
|
|
52
|
+
const exec = opts.exec || defaultExec;
|
|
53
|
+
const timeoutMs = opts.timeoutMs ?? 3000;
|
|
54
|
+
const args = buildOsascriptNotificationArgs(opts.title || DEFAULT_TITLE, opts.body);
|
|
55
|
+
try {
|
|
56
|
+
const run = exec('osascript', args, { timeout: timeoutMs });
|
|
57
|
+
await run;
|
|
58
|
+
return true;
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/lib/origins.js
CHANGED
|
@@ -11,6 +11,29 @@
|
|
|
11
11
|
|
|
12
12
|
const UNSAFE_ORIGIN_RE = /[\s#"'{}$\\;()[\],<>|/]/;
|
|
13
13
|
const HOSTNAME_LABEL_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
14
|
+
/** Whitespace (incl. Unicode) or C0/DEL controls; reject on the raw CLI string before trim/URL parse. */
|
|
15
|
+
const HOST_WHITESPACE_OR_CONTROL_RE = /[\s\x00-\x1f\x7f]/;
|
|
16
|
+
export const HOST_WHITESPACE_ERROR =
|
|
17
|
+
'Host must not contain whitespace or control characters.';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Reject hosts / site inputs that contain whitespace or control characters.
|
|
21
|
+
* Call on the raw string before any trim or `new URL()`.
|
|
22
|
+
*
|
|
23
|
+
* @param {unknown} input
|
|
24
|
+
* @param {string} [label]
|
|
25
|
+
* @returns {{ ok: true } | { ok: false, error: string }}
|
|
26
|
+
*/
|
|
27
|
+
export function rejectWhitespaceOrControls(input, label = 'Host') {
|
|
28
|
+
const raw = String(input == null ? '' : input);
|
|
29
|
+
if (HOST_WHITESPACE_OR_CONTROL_RE.test(raw)) {
|
|
30
|
+
return {
|
|
31
|
+
ok: false,
|
|
32
|
+
error: `${label} must not contain whitespace or control characters.`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return { ok: true };
|
|
36
|
+
}
|
|
14
37
|
|
|
15
38
|
/** Built-in Origin regex alternatives (without leading ^ / trailing $ / (?i)). */
|
|
16
39
|
export const DEFAULT_ORIGIN_ALTERNATIVES = Object.freeze([
|
|
@@ -40,15 +63,14 @@ export function isBroadWebflowWildcard(entry) {
|
|
|
40
63
|
* @returns {{ ok: true, value: string } | { ok: false, error: string }}
|
|
41
64
|
*/
|
|
42
65
|
export function validateOriginEntry(input) {
|
|
43
|
-
if (typeof input !== 'string' || input
|
|
66
|
+
if (typeof input !== 'string' || input === '') {
|
|
44
67
|
return { ok: false, error: 'Origin entry is empty.' };
|
|
45
68
|
}
|
|
46
69
|
|
|
47
|
-
|
|
70
|
+
const ws = rejectWhitespaceOrControls(input, 'Origin');
|
|
71
|
+
if (!ws.ok) return ws;
|
|
48
72
|
|
|
49
|
-
|
|
50
|
-
return { ok: false, error: 'Origin contains control characters.' };
|
|
51
|
-
}
|
|
73
|
+
let raw = input.toLowerCase();
|
|
52
74
|
|
|
53
75
|
// Optional https:// only (http reserved for localhost defaults)
|
|
54
76
|
if (raw.startsWith('http://')) {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GENERATED FILE. Do not edit by hand.
|
|
3
|
+
* Run: node scripts/sync-cli-versions.mjs
|
|
4
|
+
*
|
|
5
|
+
* Sourced from packages/loader and packages/modules package.json at generate time.
|
|
6
|
+
* The published CLI ships this snapshot; it cannot read the monorepo at runtime.
|
|
7
|
+
*/
|
|
8
|
+
export const LOADER_VERSION = "2.4.0";
|
|
9
|
+
export const MODULES_VERSION = "2.2.2";
|
package/src/lib/paths.js
CHANGED
|
@@ -65,6 +65,24 @@ export function getCaddyRootCaPath(opts = {}) {
|
|
|
65
65
|
return path.join(dataDir, 'pki', 'authorities', 'local', 'root.crt');
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Caddy local CA authority directory (contains root.crt / intermediate).
|
|
70
|
+
* @param {{ home?: string, env?: NodeJS.ProcessEnv, dataDir?: string }} [opts]
|
|
71
|
+
*/
|
|
72
|
+
export function getCaddyPkiAuthoritiesLocalDir(opts = {}) {
|
|
73
|
+
const dataDir = opts.dataDir || getCaddyDataDir(opts);
|
|
74
|
+
return path.join(dataDir, 'pki', 'authorities', 'local');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Caddy on-disk site certificate store for the internal ("local") issuer.
|
|
79
|
+
* @param {{ home?: string, env?: NodeJS.ProcessEnv, dataDir?: string }} [opts]
|
|
80
|
+
*/
|
|
81
|
+
export function getCaddyCertificatesLocalDir(opts = {}) {
|
|
82
|
+
const dataDir = opts.dataDir || getCaddyDataDir(opts);
|
|
83
|
+
return path.join(dataDir, 'certificates', 'local');
|
|
84
|
+
}
|
|
85
|
+
|
|
68
86
|
/**
|
|
69
87
|
* Common secondary data dir when Homebrew's Caddy service sets XDG_DATA_HOME.
|
|
70
88
|
* Report-only helper for doctor/certs (never delete).
|