@stagepass/cli 2.2.7 → 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 +37 -18
- package/TESTING-macos.md +28 -10
- 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 +64 -23
- package/src/commands/new.js +397 -41
- package/src/commands/npm-run.js +8 -0
- package/src/commands/start.js +196 -127
- package/src/commands/stop.js +10 -19
- package/src/commands/unlink.js +6 -4
- package/src/commands/upgrade.js +51 -12
- package/src/lib/apply-site-block.js +48 -0
- 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/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
package/src/commands/upgrade.js
CHANGED
|
@@ -13,10 +13,10 @@ import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
|
|
|
13
13
|
import {
|
|
14
14
|
PACKAGES_MAJOR_RANGE,
|
|
15
15
|
fetchPublished,
|
|
16
|
-
resolveSnippetLoaderVersion,
|
|
17
16
|
installedHasPerModuleTypes,
|
|
18
17
|
maxSatisfying,
|
|
19
18
|
rangeHasPerModuleTypes,
|
|
19
|
+
resolveSnippetLoaderVersion,
|
|
20
20
|
satisfiesSimple,
|
|
21
21
|
} from '../lib/registry.js';
|
|
22
22
|
import { detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
|
|
@@ -32,6 +32,7 @@ const STAGEPASS_SCOPE = '@stagepass/';
|
|
|
32
32
|
const SAFE_SPEC_RE = /^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
+
* True when the version looks like an npm dist-tag (bare word, e.g. latest).
|
|
35
36
|
* @param {string} version
|
|
36
37
|
*/
|
|
37
38
|
function isDistTag(version) {
|
|
@@ -43,7 +44,15 @@ function isDistTag(version) {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
*
|
|
47
|
+
* Resolve the range to write for one @stagepass/* package.
|
|
48
|
+
*
|
|
49
|
+
* Never writes a version or range that no published release satisfies
|
|
50
|
+
* (releases can be staged but not yet approved, which gives ETARGET):
|
|
51
|
+
* - default: `^<latest>` when latest is in the current major, else
|
|
52
|
+
* `^<highest published 2.x>`; `^2` when npm is unreachable
|
|
53
|
+
* - `--version <semver|range>`: must match a published version
|
|
54
|
+
* - `--version <dist-tag>`: the published version behind that tag
|
|
55
|
+
*
|
|
47
56
|
* @param {string} name
|
|
48
57
|
* @param {string|undefined} requested
|
|
49
58
|
* @param {{ distTags: Record<string, string>, versions: string[] } | null} published
|
|
@@ -88,6 +97,8 @@ export function resolvePackageTarget(name, requested, published) {
|
|
|
88
97
|
}
|
|
89
98
|
|
|
90
99
|
/**
|
|
100
|
+
* Resolve target ranges for every @stagepass/* dependency in package.json.
|
|
101
|
+
*
|
|
91
102
|
* @param {string|undefined} requested `--version`
|
|
92
103
|
* @param {string[]} names package names
|
|
93
104
|
* @param {object} [deps]
|
|
@@ -110,6 +121,7 @@ export async function resolveUpgradeTargets(requested, names, deps = {}) {
|
|
|
110
121
|
}
|
|
111
122
|
|
|
112
123
|
/**
|
|
124
|
+
* Collect @stagepass/* entries from dependencies and devDependencies.
|
|
113
125
|
* @param {object} pkg
|
|
114
126
|
* @returns {{ section: 'dependencies'|'devDependencies', name: string, from: string }[]}
|
|
115
127
|
*/
|
|
@@ -129,6 +141,7 @@ export function findStagepassDeps(pkg) {
|
|
|
129
141
|
}
|
|
130
142
|
|
|
131
143
|
/**
|
|
144
|
+
* Rewrite @stagepass/* versions in a package.json object (mutates a copy).
|
|
132
145
|
* @param {object} pkg
|
|
133
146
|
* @param {string | Record<string, string>} target one spec for all, or per package name
|
|
134
147
|
* @returns {{ next: object, changes: { name: string, from: string, to: string, section: string }[] }}
|
|
@@ -151,6 +164,10 @@ export function applyStagepassVersions(pkg, target) {
|
|
|
151
164
|
}
|
|
152
165
|
|
|
153
166
|
/**
|
|
167
|
+
* Upgrade @stagepass/* deps in the current project's package.json and sync
|
|
168
|
+
* the declared modules into stagepass.json, AGENTS.md, the Cursor rule and
|
|
169
|
+
* tsconfig/jsconfig types.
|
|
170
|
+
*
|
|
154
171
|
* @param {object} [options]
|
|
155
172
|
* @param {boolean} [options.dryRun]
|
|
156
173
|
* @param {string} [options.version]
|
|
@@ -186,10 +203,11 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
186
203
|
return 1;
|
|
187
204
|
}
|
|
188
205
|
|
|
206
|
+
// Validate --site before any registry / network access (no partial side effects).
|
|
189
207
|
if (options.site) {
|
|
190
|
-
const
|
|
191
|
-
if (!
|
|
192
|
-
log(chalk.red(
|
|
208
|
+
const siteCheck = normalizeSiteUrl(options.site);
|
|
209
|
+
if (!siteCheck.ok) {
|
|
210
|
+
log(chalk.red(siteCheck.error));
|
|
193
211
|
return 1;
|
|
194
212
|
}
|
|
195
213
|
}
|
|
@@ -332,17 +350,38 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
332
350
|
}
|
|
333
351
|
if (t.resolved && satisfiesSimple(t.resolved, PACKAGES_MAJOR_RANGE)) snippetVersion = t.resolved;
|
|
334
352
|
}
|
|
353
|
+
|
|
335
354
|
if (options.dryRun) {
|
|
336
355
|
log(chalk.dim('\nDry run: no files were modified.'));
|
|
337
356
|
} else {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
357
|
+
try {
|
|
358
|
+
if (changes.length > 0) {
|
|
359
|
+
try {
|
|
360
|
+
if ((await fse.lstat(pkgPath)).isSymbolicLink()) {
|
|
361
|
+
log(chalk.red('package.json is a symlink; refusing to write.'));
|
|
362
|
+
return 1;
|
|
363
|
+
}
|
|
364
|
+
} catch {
|
|
365
|
+
/* missing / unreadable: writeFile will surface the error */
|
|
366
|
+
}
|
|
367
|
+
const trailingNewline = raw.endsWith('\n');
|
|
368
|
+
const pkgBody = `${JSON.stringify(next, null, 2)}${trailingNewline ? '\n' : ''}`;
|
|
369
|
+
await fse.writeFile(pkgPath, pkgBody, 'utf8');
|
|
370
|
+
log(chalk.green('\nUpdated package.json.'));
|
|
371
|
+
}
|
|
372
|
+
await applyProjectSync(cwd, plan.writes, fse);
|
|
373
|
+
if (plan.writes.length > 0) log(chalk.green(`Updated ${plan.writes.length} project file(s).`));
|
|
374
|
+
} catch (err) {
|
|
375
|
+
// Restore package.json if project-sync failed after the pkg write.
|
|
376
|
+
try {
|
|
377
|
+
if (changes.length > 0) await fse.writeFile(pkgPath, raw, 'utf8');
|
|
378
|
+
} catch {
|
|
379
|
+
/* best effort */
|
|
380
|
+
}
|
|
381
|
+
log(chalk.red(err instanceof Error ? err.message : String(err)));
|
|
382
|
+
if (options.verbose === true && err instanceof Error && err.stack) log(err.stack);
|
|
383
|
+
return 1;
|
|
343
384
|
}
|
|
344
|
-
await applyProjectSync(cwd, plan.writes, fse);
|
|
345
|
-
if (plan.writes.length > 0) log(chalk.green(`Updated ${plan.writes.length} project file(s).`));
|
|
346
385
|
}
|
|
347
386
|
|
|
348
387
|
if (!snippetVersion) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rewrite one Stagepass site block in the Caddyfile from a projects.json entry.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from 'fs-extra';
|
|
6
|
+
import { renderSiteBlock, upsertBlock, isPhpEnabled } from './caddyfile.js';
|
|
7
|
+
import { validateRoot, getCaddyfilePath } from './paths.js';
|
|
8
|
+
import { normalizeDevPort } from './dev-port.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} domain
|
|
12
|
+
* @param {{ root?: string, origins?: string[], allowAllOrigins?: boolean, php?: boolean, devPort?: number }} project
|
|
13
|
+
* @param {object} [opts]
|
|
14
|
+
* @param {string} [opts.caddyfilePath]
|
|
15
|
+
* @param {typeof fs} [opts.fs]
|
|
16
|
+
* @returns {Promise<{ ok: true, path: string } | { ok: false, error: string }>}
|
|
17
|
+
*/
|
|
18
|
+
export async function applySiteBlock(domain, project, opts = {}) {
|
|
19
|
+
const fse = opts.fs || fs;
|
|
20
|
+
const caddyfilePath = opts.caddyfilePath || getCaddyfilePath();
|
|
21
|
+
if (!project || typeof project.root !== 'string') {
|
|
22
|
+
return { ok: false, error: 'Project root is missing.' };
|
|
23
|
+
}
|
|
24
|
+
const rootCheck = validateRoot(project.root);
|
|
25
|
+
if (!rootCheck.ok) {
|
|
26
|
+
return { ok: false, error: rootCheck.error };
|
|
27
|
+
}
|
|
28
|
+
const devPort = normalizeDevPort(project.devPort);
|
|
29
|
+
let block;
|
|
30
|
+
try {
|
|
31
|
+
block = renderSiteBlock(domain, rootCheck.path, {
|
|
32
|
+
allowAllOrigins: project.allowAllOrigins === true,
|
|
33
|
+
origins: project.origins || [],
|
|
34
|
+
php: isPhpEnabled(project.php),
|
|
35
|
+
devPort: devPort || undefined,
|
|
36
|
+
});
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let content = '';
|
|
42
|
+
if (await fse.pathExists(caddyfilePath)) {
|
|
43
|
+
content = await fse.readFile(caddyfilePath, 'utf-8');
|
|
44
|
+
}
|
|
45
|
+
content = upsertBlock(content, domain, block);
|
|
46
|
+
await fse.writeFile(caddyfilePath, `${content.trim()}\n`, 'utf-8');
|
|
47
|
+
return { ok: true, path: caddyfilePath };
|
|
48
|
+
}
|
package/src/lib/caddyfile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { escapeRegExp } from './domain.js';
|
|
2
2
|
import { escapeCaddyQuotedString, validateRoot } from './paths.js';
|
|
3
3
|
import { buildOriginAllowlistRegex, validateOriginList } from './origins.js';
|
|
4
|
+
import { isValidDevPort, normalizeDevPort } from './dev-port.js';
|
|
4
5
|
|
|
5
6
|
/** Marker written inside every Stagepass-managed site block. */
|
|
6
7
|
export const BLOCK_VERSION = 'v2';
|
|
@@ -23,8 +24,20 @@ export const SENSITIVE_BLOCK_DOCS = Object.freeze({
|
|
|
23
24
|
* @typedef {object} SiteBlockOptions
|
|
24
25
|
* @property {boolean} [allowAllOrigins]
|
|
25
26
|
* @property {string[]} [origins]
|
|
27
|
+
* @property {boolean} [php] When false, omit php_fastcgi. Missing/undefined keeps PHP on (2.x backcompat).
|
|
28
|
+
* @property {number|null|undefined} [devPort] Live-reload SSE reverse_proxy port (1024..65535), only while `stagepass dev` runs.
|
|
26
29
|
*/
|
|
27
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Whether a site block should enable PHP FastCGI.
|
|
33
|
+
* Explicit false disables; everything else (including missing) keeps the legacy on behaviour.
|
|
34
|
+
* @param {boolean|undefined} php
|
|
35
|
+
* @returns {boolean}
|
|
36
|
+
*/
|
|
37
|
+
export function isPhpEnabled(php) {
|
|
38
|
+
return php !== false;
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
/**
|
|
29
42
|
* @param {string} domain - normalized foo.sp
|
|
30
43
|
* @param {string} root - absolute project path
|
|
@@ -43,9 +56,16 @@ export function renderSiteBlock(domain, root, options = {}) {
|
|
|
43
56
|
throw new Error(originsResult.error);
|
|
44
57
|
}
|
|
45
58
|
const origins = originsResult.origins;
|
|
59
|
+
const php = isPhpEnabled(options.php);
|
|
60
|
+
const devPort = normalizeDevPort(options.devPort);
|
|
61
|
+
if (options.devPort != null && options.devPort !== '' && !devPort) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Invalid devPort ${String(options.devPort)} (must be an integer 1024..65535).`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
46
66
|
|
|
47
67
|
const quotedRoot = `"${escapeCaddyQuotedString(rootResult.path)}"`;
|
|
48
|
-
const optionsComment = formatOptionsComment({ allowAllOrigins, origins });
|
|
68
|
+
const optionsComment = formatOptionsComment({ allowAllOrigins, origins, php, devPort });
|
|
49
69
|
|
|
50
70
|
const lines = [
|
|
51
71
|
`# START: ${domain}`,
|
|
@@ -68,13 +88,16 @@ export function renderSiteBlock(domain, root, options = {}) {
|
|
|
68
88
|
lines.push(...allowlistCorsLines(origins));
|
|
69
89
|
}
|
|
70
90
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
'
|
|
77
|
-
)
|
|
91
|
+
// Live-reload SSE must be before file_server so the path is not served from disk.
|
|
92
|
+
if (devPort && isValidDevPort(devPort)) {
|
|
93
|
+
lines.push(...liveReloadHandleLines(devPort, allowAllOrigins));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
lines.push('', ' handle {', ` root * ${quotedRoot}`);
|
|
97
|
+
if (php) {
|
|
98
|
+
lines.push(' php_fastcgi 127.0.0.1:9000');
|
|
99
|
+
}
|
|
100
|
+
lines.push(' file_server');
|
|
78
101
|
|
|
79
102
|
if (allowAllOrigins) {
|
|
80
103
|
lines.push(' header Access-Control-Allow-Origin *');
|
|
@@ -90,14 +113,45 @@ export function renderSiteBlock(domain, root, options = {}) {
|
|
|
90
113
|
return lines.join('\n');
|
|
91
114
|
}
|
|
92
115
|
|
|
93
|
-
function formatOptionsComment({ allowAllOrigins, origins }) {
|
|
94
|
-
const parts = [
|
|
116
|
+
function formatOptionsComment({ allowAllOrigins, origins, php, devPort }) {
|
|
117
|
+
const parts = [
|
|
118
|
+
`allowAllOrigins=${allowAllOrigins ? 'true' : 'false'}`,
|
|
119
|
+
`php=${php ? 'true' : 'false'}`,
|
|
120
|
+
];
|
|
95
121
|
if (origins.length > 0) {
|
|
96
122
|
parts.push(`origins=${origins.join(',')}`);
|
|
97
123
|
}
|
|
124
|
+
if (devPort && isValidDevPort(devPort)) {
|
|
125
|
+
parts.push(`devPort=${devPort}`);
|
|
126
|
+
}
|
|
98
127
|
return `# stagepass-options: ${parts.join(' ')}`;
|
|
99
128
|
}
|
|
100
129
|
|
|
130
|
+
/**
|
|
131
|
+
* reverse_proxy for /__stagepass/live with the same CORS / LNA policy as assets.
|
|
132
|
+
* Port is validated before call; still interpolated only as a decimal integer.
|
|
133
|
+
* @param {number} port
|
|
134
|
+
* @param {boolean} allowAllOrigins
|
|
135
|
+
*/
|
|
136
|
+
function liveReloadHandleLines(port, allowAllOrigins) {
|
|
137
|
+
const lines = [
|
|
138
|
+
' handle /__stagepass/live {',
|
|
139
|
+
' reverse_proxy 127.0.0.1:' + String(port) + ' {',
|
|
140
|
+
' flush_interval -1',
|
|
141
|
+
' }',
|
|
142
|
+
];
|
|
143
|
+
if (allowAllOrigins) {
|
|
144
|
+
lines.push(' header Access-Control-Allow-Origin *');
|
|
145
|
+
} else {
|
|
146
|
+
lines.push(
|
|
147
|
+
' header @cors_origin Access-Control-Allow-Origin "{http.request.header.Origin}"',
|
|
148
|
+
' header @cors_origin Vary Origin'
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
lines.push(' }', '');
|
|
152
|
+
return lines;
|
|
153
|
+
}
|
|
154
|
+
|
|
101
155
|
function allowAllCorsLines() {
|
|
102
156
|
return [
|
|
103
157
|
' @options method OPTIONS',
|
|
@@ -169,11 +223,12 @@ export function parseBlocks(content) {
|
|
|
169
223
|
* @param {string} blockText
|
|
170
224
|
*/
|
|
171
225
|
export function parseOptionsFromBlock(blockText) {
|
|
172
|
-
const defaults = { allowAllOrigins: false, origins: [] };
|
|
226
|
+
const defaults = { allowAllOrigins: false, origins: [], php: undefined, devPort: undefined };
|
|
173
227
|
const line = blockText.match(/^# stagepass-options:\s*(.+)$/m);
|
|
174
228
|
if (!line) {
|
|
175
229
|
if (!/# stagepass-block:/m.test(blockText)) {
|
|
176
|
-
|
|
230
|
+
// Legacy pre-marker blocks always had PHP.
|
|
231
|
+
return { allowAllOrigins: true, origins: [], php: true, devPort: undefined };
|
|
177
232
|
}
|
|
178
233
|
return defaults;
|
|
179
234
|
}
|
|
@@ -181,13 +236,17 @@ export function parseOptionsFromBlock(blockText) {
|
|
|
181
236
|
const text = line[1];
|
|
182
237
|
const allowMatch = text.match(/allowAllOrigins=(true|false)/);
|
|
183
238
|
const allowAllOrigins = allowMatch ? allowMatch[1] === 'true' : false;
|
|
239
|
+
const phpMatch = text.match(/(?:^|\s)php=(true|false)(?:\s|$)/);
|
|
240
|
+
const php = phpMatch ? phpMatch[1] === 'true' : undefined;
|
|
184
241
|
const originsMatch = text.match(/origins=([^\s]+)/);
|
|
185
242
|
let origins = [];
|
|
186
243
|
if (originsMatch) {
|
|
187
244
|
const list = validateOriginList(originsMatch[1].split(',').filter(Boolean));
|
|
188
245
|
origins = list.ok ? list.origins : [];
|
|
189
246
|
}
|
|
190
|
-
|
|
247
|
+
const devPortMatch = text.match(/(?:^|\s)devPort=(\d+)(?:\s|$)/);
|
|
248
|
+
const devPort = devPortMatch ? normalizeDevPort(Number(devPortMatch[1])) : undefined;
|
|
249
|
+
return { allowAllOrigins, origins, php, devPort: devPort || undefined };
|
|
191
250
|
}
|
|
192
251
|
|
|
193
252
|
/**
|
package/src/lib/cdn-snippets.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Canonical CDN embed snippets recommended by Stagepass docs.
|
|
3
6
|
* Doctor checks these so generated / documented snippets never drift to @latest.
|
|
4
|
-
* Do not scan user sites.
|
|
5
7
|
*/
|
|
6
8
|
export const RECOMMENDED_CDN_SNIPPETS = Object.freeze([
|
|
7
9
|
'https://cdn.jsdelivr.net/npm/@stagepass/loader@2/dist/loader.min.js',
|
|
@@ -11,14 +13,154 @@ export const RECOMMENDED_CDN_SNIPPETS = Object.freeze([
|
|
|
11
13
|
'https://cdn.jsdelivr.net/npm/@stagepass/modules@2/dist/all.min.js',
|
|
12
14
|
]);
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Unpinned / legacy CDN pins for @stagepass/loader and @stagepass/modules:
|
|
18
|
+
* - `@latest`
|
|
19
|
+
* - bare major `@1` (not `@1.2.0` exact pins)
|
|
20
|
+
* - package URL with no `@version` at all
|
|
21
|
+
*
|
|
22
|
+
* Matches jsDelivr and unpkg-style paths.
|
|
23
|
+
*/
|
|
24
|
+
export const UNPINNED_CDN_RE =
|
|
25
|
+
/(?:cdn\.jsdelivr\.net\/npm|unpkg\.com)\/@stagepass\/(?:loader|modules)(?:@latest|@1(?![0-9.])|(?!@))(?=[/"'?\s]|$)/gi;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Test whether a single URL/snippet string is an unpinned Stagepass CDN reference.
|
|
29
|
+
* @param {string} snippet
|
|
30
|
+
* @returns {boolean}
|
|
31
|
+
*/
|
|
32
|
+
export function isUnpinnedCdnSnippet(snippet) {
|
|
33
|
+
const text = String(snippet || '');
|
|
34
|
+
UNPINNED_CDN_RE.lastIndex = 0;
|
|
35
|
+
return UNPINNED_CDN_RE.test(text);
|
|
36
|
+
}
|
|
15
37
|
|
|
16
38
|
/**
|
|
17
39
|
* @param {string[]} [snippets]
|
|
18
40
|
* @returns {{ ok: boolean, offenders: string[] }}
|
|
19
41
|
*/
|
|
20
42
|
export function findUnpinnedCdnSnippets(snippets = RECOMMENDED_CDN_SNIPPETS) {
|
|
21
|
-
const offenders = snippets.filter((s) =>
|
|
43
|
+
const offenders = snippets.filter((s) => isUnpinnedCdnSnippet(s));
|
|
44
|
+
return { ok: offenders.length === 0, offenders };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Extract offending substring matches from free-form file content.
|
|
49
|
+
* @param {string} content
|
|
50
|
+
* @returns {string[]}
|
|
51
|
+
*/
|
|
52
|
+
export function findUnpinnedCdnMatchesInText(content) {
|
|
53
|
+
const text = String(content || '');
|
|
54
|
+
const candidateRe =
|
|
55
|
+
/(?:https?:\/\/)?(?:cdn\.jsdelivr\.net\/npm|unpkg\.com)\/@stagepass\/(?:loader|modules)[^"'<\s]*/gi;
|
|
56
|
+
const matches = text.match(candidateRe);
|
|
57
|
+
if (!matches) return [];
|
|
58
|
+
const seen = new Set();
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const m of matches) {
|
|
61
|
+
if (!isUnpinnedCdnSnippet(m)) continue;
|
|
62
|
+
if (seen.has(m)) continue;
|
|
63
|
+
seen.add(m);
|
|
64
|
+
out.push(m);
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Default project files that may contain Webflow / CDN snippets. */
|
|
70
|
+
export const PROJECT_CDN_SCAN_FILES = Object.freeze([
|
|
71
|
+
'README.md',
|
|
72
|
+
'stagepass.json',
|
|
73
|
+
'webflow-head.html',
|
|
74
|
+
'webflow-footer.html',
|
|
75
|
+
'snippets.html',
|
|
76
|
+
'stagepass-snippets.html',
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
/** Skip HTML / allowlisted files larger than this when scanning for unpinned CDN URLs. */
|
|
80
|
+
export const PROJECT_CDN_SCAN_MAX_BYTES = 1024 * 1024;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Non-recursive `*.html` in project root and `snippets/` (regular files only).
|
|
84
|
+
* @param {string} cwd
|
|
85
|
+
* @param {typeof fs} [fse]
|
|
86
|
+
* @returns {Promise<string[]>}
|
|
87
|
+
*/
|
|
88
|
+
export async function listProjectHtmlCdnScanFiles(cwd, fse = fs) {
|
|
89
|
+
/** @type {string[]} */
|
|
90
|
+
const out = [];
|
|
91
|
+
for (const dirRel of ['', 'snippets']) {
|
|
92
|
+
const dir = dirRel ? path.join(cwd, dirRel) : cwd;
|
|
93
|
+
let names;
|
|
94
|
+
try {
|
|
95
|
+
names = await fse.readdir(dir);
|
|
96
|
+
} catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
for (const name of names) {
|
|
100
|
+
if (!name.endsWith('.html')) continue;
|
|
101
|
+
const rel = dirRel ? path.join(dirRel, name) : name;
|
|
102
|
+
const full = path.join(cwd, rel);
|
|
103
|
+
try {
|
|
104
|
+
const st = await fse.lstat(full);
|
|
105
|
+
if (st.isSymbolicLink() || !st.isFile() || st.size > PROJECT_CDN_SCAN_MAX_BYTES) continue;
|
|
106
|
+
out.push(rel);
|
|
107
|
+
} catch {
|
|
108
|
+
/* skip */
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Read-only scan of the current project's snippet-bearing files for unpinned CDN URLs.
|
|
117
|
+
* Scans allowlisted files plus `*.html` in the project root and `snippets/`
|
|
118
|
+
* (non-recursive; regular files ≤ 1 MB; symlinks skipped).
|
|
119
|
+
*
|
|
120
|
+
* @param {object} [opts]
|
|
121
|
+
* @param {string} [opts.cwd]
|
|
122
|
+
* @param {string[]} [opts.files]
|
|
123
|
+
* @param {boolean} [opts.skipHtmlDiscover]
|
|
124
|
+
* @param {typeof fs} [opts.fs]
|
|
125
|
+
* @param {(p: string) => Promise<boolean>} [opts.pathExists]
|
|
126
|
+
* @param {(p: string, enc: string) => Promise<string>} [opts.readFile]
|
|
127
|
+
* @returns {Promise<{ ok: boolean, offenders: { file: string, match: string }[] }>}
|
|
128
|
+
*/
|
|
129
|
+
export async function scanProjectUnpinnedCdn(opts = {}) {
|
|
130
|
+
const cwd = opts.cwd || process.cwd();
|
|
131
|
+
const fse = opts.fs || fs;
|
|
132
|
+
const fixed = opts.files || PROJECT_CDN_SCAN_FILES;
|
|
133
|
+
const html =
|
|
134
|
+
opts.skipHtmlDiscover === true ? [] : await listProjectHtmlCdnScanFiles(cwd, fse);
|
|
135
|
+
const files = [...new Set([...fixed, ...html])];
|
|
136
|
+
const pathExists = opts.pathExists || ((p) => fse.pathExists(p));
|
|
137
|
+
const readFile = opts.readFile || ((p, enc) => fse.readFile(p, enc));
|
|
138
|
+
/** @type {{ file: string, match: string }[]} */
|
|
139
|
+
const offenders = [];
|
|
140
|
+
|
|
141
|
+
for (const rel of files) {
|
|
142
|
+
if (typeof rel !== 'string' || rel.includes('..') || path.isAbsolute(rel)) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const full = path.join(cwd, rel);
|
|
146
|
+
if (!(await pathExists(full))) continue;
|
|
147
|
+
try {
|
|
148
|
+
const st = await fse.lstat(full);
|
|
149
|
+
if (st.isSymbolicLink() || !st.isFile() || st.size > PROJECT_CDN_SCAN_MAX_BYTES) continue;
|
|
150
|
+
} catch {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
let content = '';
|
|
154
|
+
try {
|
|
155
|
+
content = await readFile(full, 'utf8');
|
|
156
|
+
} catch {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
for (const match of findUnpinnedCdnMatchesInText(content)) {
|
|
160
|
+
offenders.push({ file: rel, match });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
22
164
|
return { ok: offenders.length === 0, offenders };
|
|
23
165
|
}
|
|
24
166
|
|