wp-shakedown 0.0.0-stage → 0.2.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.
- package/LICENSE +340 -0
- package/README.md +74 -2
- package/dist/baseline.d.ts +6 -0
- package/dist/baseline.js +41 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +296 -0
- package/dist/diff.d.ts +8 -0
- package/dist/diff.js +26 -0
- package/dist/engines/config.d.ts +3 -0
- package/dist/engines/config.js +134 -0
- package/dist/engines/custom.d.ts +10 -0
- package/dist/engines/custom.js +286 -0
- package/dist/engines/eslint.d.ts +8 -0
- package/dist/engines/eslint.js +96 -0
- package/dist/engines/index.d.ts +2 -0
- package/dist/engines/index.js +14 -0
- package/dist/engines/integrity.d.ts +2 -0
- package/dist/engines/integrity.js +108 -0
- package/dist/engines/php-compat.d.ts +2 -0
- package/dist/engines/php-compat.js +26 -0
- package/dist/engines/php-lint.d.ts +6 -0
- package/dist/engines/php-lint.js +42 -0
- package/dist/engines/phpstan.d.ts +11 -0
- package/dist/engines/phpstan.js +103 -0
- package/dist/engines/stylelint.d.ts +4 -0
- package/dist/engines/stylelint.js +63 -0
- package/dist/engines/theme-check.d.ts +7 -0
- package/dist/engines/theme-check.js +109 -0
- package/dist/engines/theme-review.d.ts +24 -0
- package/dist/engines/theme-review.js +378 -0
- package/dist/engines/updates.d.ts +6 -0
- package/dist/engines/updates.js +78 -0
- package/dist/engines/vulnerabilities.d.ts +22 -0
- package/dist/engines/vulnerabilities.js +128 -0
- package/dist/engines/wp-json.d.ts +18 -0
- package/dist/engines/wp-json.js +134 -0
- package/dist/engines/wpcs.d.ts +7 -0
- package/dist/engines/wpcs.js +60 -0
- package/dist/exec.d.ts +12 -0
- package/dist/exec.js +19 -0
- package/dist/fingerprint.d.ts +10 -0
- package/dist/fingerprint.js +29 -0
- package/dist/health/install.d.ts +16 -0
- package/dist/health/install.js +68 -0
- package/dist/home.d.ts +2 -0
- package/dist/home.js +6 -0
- package/dist/hub.d.ts +32 -0
- package/dist/hub.js +99 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +36 -0
- package/dist/js/files.d.ts +4 -0
- package/dist/js/files.js +47 -0
- package/dist/js/tools.d.ts +3 -0
- package/dist/js/tools.js +67 -0
- package/dist/mcp/server.d.ts +9 -0
- package/dist/mcp/server.js +91 -0
- package/dist/mcp/tools.d.ts +26 -0
- package/dist/mcp/tools.js +163 -0
- package/dist/net.d.ts +18 -0
- package/dist/net.js +68 -0
- package/dist/php/files.d.ts +3 -0
- package/dist/php/files.js +53 -0
- package/dist/php/php.d.ts +4 -0
- package/dist/php/php.js +25 -0
- package/dist/php/phpcs.d.ts +25 -0
- package/dist/php/phpcs.js +55 -0
- package/dist/php/tools.d.ts +3 -0
- package/dist/php/tools.js +66 -0
- package/dist/profiles.d.ts +4 -0
- package/dist/profiles.js +41 -0
- package/dist/progress.d.ts +3 -0
- package/dist/progress.js +13 -0
- package/dist/projects.d.ts +36 -0
- package/dist/projects.js +91 -0
- package/dist/report/escape.d.ts +1 -0
- package/dist/report/escape.js +4 -0
- package/dist/report/html.d.ts +2 -0
- package/dist/report/html.js +100 -0
- package/dist/report/json.d.ts +2 -0
- package/dist/report/json.js +3 -0
- package/dist/report/markdown.d.ts +5 -0
- package/dist/report/markdown.js +92 -0
- package/dist/report/sarif.d.ts +2 -0
- package/dist/report/sarif.js +44 -0
- package/dist/report/write.d.ts +9 -0
- package/dist/report/write.js +48 -0
- package/dist/runner.d.ts +14 -0
- package/dist/runner.js +166 -0
- package/dist/session.d.ts +22 -0
- package/dist/session.js +39 -0
- package/dist/target.d.ts +5 -0
- package/dist/target.js +114 -0
- package/dist/types.d.ts +165 -0
- package/dist/types.js +6 -0
- package/dist/ui/server.d.ts +13 -0
- package/dist/ui/server.js +458 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/versions.d.ts +9 -0
- package/dist/versions.js +35 -0
- package/js-tools/eslint.config.mjs +19 -0
- package/js-tools/package-lock.json +7004 -0
- package/js-tools/package.json +13 -0
- package/js-tools/stylelint.config.mjs +4 -0
- package/package.json +58 -4
- package/php-tools/composer.json +68 -0
- package/php-tools/composer.lock +889 -0
- package/php-tools/theme-check.php +239 -0
- package/ui/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/ui/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/ui/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/ui/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/ui/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/ui/dist/assets/index-DXwdz_Wu.css +2 -0
- package/ui/dist/assets/index-V8IOqxHa.js +54 -0
- package/ui/dist/index.html +14 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
3
|
+
import http from 'node:http';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { customEngines } from '../engines/custom.js';
|
|
8
|
+
import { profiles } from '../profiles.js';
|
|
9
|
+
import { addProject, listProjects, removeProject } from '../projects.js';
|
|
10
|
+
import { readPreviousRun } from '../report/write.js';
|
|
11
|
+
import { runSession } from '../session.js';
|
|
12
|
+
import { detectTarget } from '../target.js';
|
|
13
|
+
import { DEPTHS } from '../types.js';
|
|
14
|
+
import { VERSION } from '../version.js';
|
|
15
|
+
// The page is a Vite build. Its files are index.html and the hashed files in assets/.
|
|
16
|
+
const UI_DIR = fileURLToPath(new URL('../../ui/dist/', import.meta.url));
|
|
17
|
+
const TYPES = {
|
|
18
|
+
'.html': 'text/html; charset=utf-8',
|
|
19
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
20
|
+
'.css': 'text/css; charset=utf-8',
|
|
21
|
+
'.woff2': 'font/woff2',
|
|
22
|
+
'.svg': 'image/svg+xml',
|
|
23
|
+
};
|
|
24
|
+
/** The file for a route, or undefined. Only index.html and a plain file name under /assets/ are served, so a URL cannot walk out of the folder. */
|
|
25
|
+
function staticFile(route) {
|
|
26
|
+
if (route === '/' || route === '/index.html')
|
|
27
|
+
return 'index.html';
|
|
28
|
+
const m = /^\/assets\/([A-Za-z0-9._-]+)$/.exec(route);
|
|
29
|
+
return m && !m[1].startsWith('.') && TYPES[path.extname(m[1])] ? path.join('assets', m[1]) : undefined;
|
|
30
|
+
}
|
|
31
|
+
const HEADERS = {
|
|
32
|
+
'Cache-Control': 'no-store',
|
|
33
|
+
'X-Content-Type-Options': 'nosniff',
|
|
34
|
+
'Referrer-Policy': 'no-referrer',
|
|
35
|
+
'Content-Security-Policy': "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; font-src 'self'; connect-src 'self'; img-src 'self' data:; base-uri 'none'; form-action 'none'",
|
|
36
|
+
};
|
|
37
|
+
const MAX_BODY = 65536;
|
|
38
|
+
const MAX_RUNNING = 3;
|
|
39
|
+
const REPORT_CSP = "default-src 'none'; style-src 'unsafe-inline'; img-src data:; base-uri 'none'; form-action 'none'";
|
|
40
|
+
const REPORT_FILES = ['report.md', 'report.json', 'report.sarif', 'report.html'];
|
|
41
|
+
function toWire(e) {
|
|
42
|
+
if (e.type === 'run:start') {
|
|
43
|
+
const { kind, name, path: p, units } = e.target;
|
|
44
|
+
return { type: e.type, target: { kind, name, path: p, units: units.length }, jobs: e.jobs };
|
|
45
|
+
}
|
|
46
|
+
if (e.type === 'run:end')
|
|
47
|
+
return { type: e.type, summary: e.result.summary, finishedAt: e.result.finishedAt };
|
|
48
|
+
return e;
|
|
49
|
+
}
|
|
50
|
+
function same(a, b) {
|
|
51
|
+
const x = Buffer.from(a);
|
|
52
|
+
const y = Buffer.from(b);
|
|
53
|
+
return x.length === y.length && timingSafeEqual(x, y);
|
|
54
|
+
}
|
|
55
|
+
export function startUi(opts) {
|
|
56
|
+
const token = opts.token ?? randomBytes(24).toString('base64url');
|
|
57
|
+
const startDir = path.resolve(opts.startDir ?? process.cwd());
|
|
58
|
+
let port = 0;
|
|
59
|
+
const clients = new Set();
|
|
60
|
+
const active = new Map();
|
|
61
|
+
const queue = [];
|
|
62
|
+
// What a page that connects later can still show: the last run of each project in this server session.
|
|
63
|
+
const finished = new Map();
|
|
64
|
+
const send = (res, status, body) => {
|
|
65
|
+
res.writeHead(status, { ...HEADERS, 'Content-Type': 'application/json; charset=utf-8' });
|
|
66
|
+
res.end(JSON.stringify(body));
|
|
67
|
+
};
|
|
68
|
+
const sse = (res, name, data) => {
|
|
69
|
+
res.write(`event: ${name}\ndata: ${JSON.stringify(data)}\n\n`);
|
|
70
|
+
};
|
|
71
|
+
const broadcast = (name, data) => {
|
|
72
|
+
for (const c of clients)
|
|
73
|
+
sse(c, name, data);
|
|
74
|
+
};
|
|
75
|
+
const push = (id, a, event) => {
|
|
76
|
+
a.events.push(event);
|
|
77
|
+
broadcast('run', { id, event });
|
|
78
|
+
};
|
|
79
|
+
function readBody(req) {
|
|
80
|
+
return new Promise((resolve) => {
|
|
81
|
+
const chunks = [];
|
|
82
|
+
let size = 0;
|
|
83
|
+
req.on('data', (c) => {
|
|
84
|
+
size += c.length;
|
|
85
|
+
if (size > MAX_BODY) {
|
|
86
|
+
resolve(undefined);
|
|
87
|
+
req.removeAllListeners('data');
|
|
88
|
+
req.resume();
|
|
89
|
+
}
|
|
90
|
+
else
|
|
91
|
+
chunks.push(c);
|
|
92
|
+
});
|
|
93
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
94
|
+
req.on('error', () => resolve(undefined));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function begin(id, a) {
|
|
98
|
+
const ctl = a.controller;
|
|
99
|
+
a.state = 'running';
|
|
100
|
+
finished.delete(id);
|
|
101
|
+
let ended = false;
|
|
102
|
+
let session;
|
|
103
|
+
broadcast('projects', {});
|
|
104
|
+
runSession({
|
|
105
|
+
input: a.path,
|
|
106
|
+
...(a.profile ? { profile: a.profile } : {}),
|
|
107
|
+
...(a.depth ? { depth: a.depth } : {}),
|
|
108
|
+
...(a.experimental ? { experimental: true } : {}),
|
|
109
|
+
engines: opts.engines,
|
|
110
|
+
signal: ctl.signal,
|
|
111
|
+
onEvent: (e) => {
|
|
112
|
+
// A stopped run reports one message, not a partial result.
|
|
113
|
+
if (e.type === 'run:end' && ctl.signal.aborted)
|
|
114
|
+
return;
|
|
115
|
+
if (e.type === 'run:end')
|
|
116
|
+
ended = true;
|
|
117
|
+
push(id, a, toWire(e));
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
.then((s) => {
|
|
121
|
+
if (!ctl.signal.aborted)
|
|
122
|
+
session = s;
|
|
123
|
+
})
|
|
124
|
+
.catch((err) => {
|
|
125
|
+
if (!ctl.signal.aborted)
|
|
126
|
+
push(id, a, { type: 'run:error', message: err instanceof Error ? err.message : String(err) });
|
|
127
|
+
})
|
|
128
|
+
.finally(() => {
|
|
129
|
+
if (ctl.signal.aborted && !ended)
|
|
130
|
+
push(id, a, { type: 'run:error', message: 'The run was stopped.' });
|
|
131
|
+
active.delete(id);
|
|
132
|
+
finished.set(id, { events: a.events, ...(session ? { session } : {}) });
|
|
133
|
+
pump();
|
|
134
|
+
broadcast('projects', {});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function pump() {
|
|
138
|
+
const running = () => [...active.values()].filter((a) => a.state === 'running').length;
|
|
139
|
+
while (running() < MAX_RUNNING && queue.length > 0) {
|
|
140
|
+
const id = queue.shift();
|
|
141
|
+
const a = active.get(id);
|
|
142
|
+
if (a)
|
|
143
|
+
begin(id, a);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function cancel(id) {
|
|
147
|
+
const a = active.get(id);
|
|
148
|
+
if (!a)
|
|
149
|
+
return;
|
|
150
|
+
const at = queue.indexOf(id);
|
|
151
|
+
if (at >= 0) {
|
|
152
|
+
queue.splice(at, 1);
|
|
153
|
+
active.delete(id);
|
|
154
|
+
broadcast('projects', {});
|
|
155
|
+
}
|
|
156
|
+
else
|
|
157
|
+
a.controller.abort();
|
|
158
|
+
}
|
|
159
|
+
async function jsonBody(req, res) {
|
|
160
|
+
if (!(req.headers['content-type'] ?? '').startsWith('application/json')) {
|
|
161
|
+
send(res, 415, { error: 'Send JSON.' });
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const text = await readBody(req);
|
|
165
|
+
if (text === undefined) {
|
|
166
|
+
res.setHeader('Connection', 'close');
|
|
167
|
+
send(res, 413, { error: 'The request is too large.' });
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
const data = JSON.parse(text);
|
|
172
|
+
return data !== null && typeof data === 'object' && !Array.isArray(data) ? data : {};
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return {};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** The report folder and finished session of a project, or undefined when it has no report. */
|
|
179
|
+
function reportOf(id) {
|
|
180
|
+
const session = finished.get(id)?.session;
|
|
181
|
+
if (session)
|
|
182
|
+
return { dir: session.dir, session };
|
|
183
|
+
const dir = listProjects().find((p) => p.id === id)?.last?.dir;
|
|
184
|
+
return dir ? { dir } : undefined;
|
|
185
|
+
}
|
|
186
|
+
const handle = async (req, res) => {
|
|
187
|
+
const url = new URL(req.url ?? '/', `http://127.0.0.1:${port}`);
|
|
188
|
+
const route = url.pathname;
|
|
189
|
+
const method = req.method ?? 'GET';
|
|
190
|
+
if (!route.startsWith('/api/')) {
|
|
191
|
+
const file = staticFile(route);
|
|
192
|
+
if (!file || method !== 'GET')
|
|
193
|
+
return send(res, file ? 405 : 404, { error: file ? 'Method not allowed.' : 'Not found.' });
|
|
194
|
+
try {
|
|
195
|
+
const body = readFileSync(path.join(UI_DIR, file));
|
|
196
|
+
res.writeHead(200, { ...HEADERS, 'Content-Type': TYPES[path.extname(file)] });
|
|
197
|
+
res.end(body);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
send(res, 404, { error: 'Not found.' });
|
|
201
|
+
}
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (req.headers.host !== `127.0.0.1:${port}` && req.headers.host !== `localhost:${port}`) {
|
|
205
|
+
return send(res, 403, { error: 'Bad host.' });
|
|
206
|
+
}
|
|
207
|
+
const origin = req.headers.origin;
|
|
208
|
+
if (origin !== undefined && origin !== `http://127.0.0.1:${port}` && origin !== `http://localhost:${port}`) {
|
|
209
|
+
return send(res, 403, { error: 'Bad origin.' });
|
|
210
|
+
}
|
|
211
|
+
const given = req.headers['x-shakedown-token'] ?? url.searchParams.get('t') ?? '';
|
|
212
|
+
if (!same(Array.isArray(given) ? (given[0] ?? '') : given, token)) {
|
|
213
|
+
return send(res, 401, { error: 'Missing or wrong token.' });
|
|
214
|
+
}
|
|
215
|
+
const methods = {
|
|
216
|
+
'/api/state': 'GET',
|
|
217
|
+
'/api/dirs': 'GET',
|
|
218
|
+
'/api/detect': 'GET',
|
|
219
|
+
'/api/projects': 'GET, POST',
|
|
220
|
+
'/api/projects/remove': 'POST',
|
|
221
|
+
'/api/run': 'POST',
|
|
222
|
+
'/api/cancel': 'POST',
|
|
223
|
+
'/api/report': 'GET',
|
|
224
|
+
'/api/report.html': 'GET',
|
|
225
|
+
'/api/events': 'GET',
|
|
226
|
+
};
|
|
227
|
+
const want = methods[route];
|
|
228
|
+
if (!want)
|
|
229
|
+
return send(res, 404, { error: 'Not found.' });
|
|
230
|
+
if (!want.split(', ').includes(method))
|
|
231
|
+
return send(res, 405, { error: 'Method not allowed.' });
|
|
232
|
+
switch (route) {
|
|
233
|
+
case '/api/state':
|
|
234
|
+
return send(res, 200, {
|
|
235
|
+
version: VERSION,
|
|
236
|
+
home: os.homedir(),
|
|
237
|
+
startDir,
|
|
238
|
+
engines: [...opts.engines, ...customEngines()].map(({ id, title, kinds, depth, experimental }) => ({ id, title, kinds, depth: depth ?? 'standard', ...(experimental ? { experimental } : {}) })),
|
|
239
|
+
profiles: profiles.map(({ id, title, summary, kinds }) => ({ id, title, summary, kinds })),
|
|
240
|
+
});
|
|
241
|
+
case '/api/dirs': {
|
|
242
|
+
const given = url.searchParams.get('path');
|
|
243
|
+
const p = given ?? startDir;
|
|
244
|
+
if (!path.isAbsolute(p))
|
|
245
|
+
return send(res, 400, { error: 'Give an absolute path.' });
|
|
246
|
+
if (!statSync(p, { throwIfNoEntry: false })?.isDirectory())
|
|
247
|
+
return send(res, 400, { error: `"${p}" is not a folder.` });
|
|
248
|
+
let names;
|
|
249
|
+
try {
|
|
250
|
+
names = readdirSync(p);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return send(res, 400, { error: `Cannot read "${p}".` });
|
|
254
|
+
}
|
|
255
|
+
const dirs = names
|
|
256
|
+
.filter((n) => !n.startsWith('.'))
|
|
257
|
+
.filter((n) => statSync(path.join(p, n), { throwIfNoEntry: false })?.isDirectory())
|
|
258
|
+
.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' }))
|
|
259
|
+
.map((name) => ({ name, path: path.join(p, name) }));
|
|
260
|
+
const parent = path.dirname(p);
|
|
261
|
+
return send(res, 200, { path: p, parent: parent === p ? null : parent, dirs });
|
|
262
|
+
}
|
|
263
|
+
case '/api/detect': {
|
|
264
|
+
try {
|
|
265
|
+
const t = detectTarget(url.searchParams.get('path') ?? '');
|
|
266
|
+
return send(res, 200, {
|
|
267
|
+
kind: t.kind,
|
|
268
|
+
name: t.name,
|
|
269
|
+
path: t.path,
|
|
270
|
+
units: t.units.map((u) => ({ kind: u.kind, name: u.name, path: path.relative(t.path, u.path).split(path.sep).join('/') })),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
catch (err) {
|
|
274
|
+
return send(res, 400, { error: err instanceof Error ? err.message : String(err) });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
case '/api/projects': {
|
|
278
|
+
if (method === 'GET') {
|
|
279
|
+
return send(res, 200, { projects: listProjects().map((p) => ({ ...p, ...(active.has(p.id) ? { run: active.get(p.id).state } : {}) })) });
|
|
280
|
+
}
|
|
281
|
+
const body = await jsonBody(req, res);
|
|
282
|
+
if (!body)
|
|
283
|
+
return;
|
|
284
|
+
if (typeof body.path !== 'string' || body.path === '')
|
|
285
|
+
return send(res, 400, { error: 'Send a path.' });
|
|
286
|
+
try {
|
|
287
|
+
const added = addProject(body.path);
|
|
288
|
+
if (added.added)
|
|
289
|
+
broadcast('projects', {});
|
|
290
|
+
return send(res, 200, added);
|
|
291
|
+
}
|
|
292
|
+
catch (err) {
|
|
293
|
+
return send(res, 400, { error: err instanceof Error ? err.message : String(err) });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
case '/api/projects/remove': {
|
|
297
|
+
const body = await jsonBody(req, res);
|
|
298
|
+
if (!body)
|
|
299
|
+
return;
|
|
300
|
+
const id = body.id;
|
|
301
|
+
if (typeof id !== 'string' || !listProjects().some((p) => p.id === id))
|
|
302
|
+
return send(res, 404, { error: 'No such project.' });
|
|
303
|
+
if (active.has(id))
|
|
304
|
+
return send(res, 409, { error: 'Stop the run first.' });
|
|
305
|
+
removeProject(id);
|
|
306
|
+
finished.delete(id);
|
|
307
|
+
broadcast('projects', {});
|
|
308
|
+
return send(res, 200, { ok: true });
|
|
309
|
+
}
|
|
310
|
+
case '/api/run': {
|
|
311
|
+
const body = await jsonBody(req, res);
|
|
312
|
+
if (!body)
|
|
313
|
+
return;
|
|
314
|
+
const ids = body.ids;
|
|
315
|
+
if (!Array.isArray(ids) || ids.length === 0 || !ids.every((x) => typeof x === 'string'))
|
|
316
|
+
return send(res, 400, { error: 'Send the projects to run.' });
|
|
317
|
+
const all = listProjects();
|
|
318
|
+
const chosen = ids.map((id) => all.find((p) => p.id === id));
|
|
319
|
+
if (chosen.some((p) => !p?.kind))
|
|
320
|
+
return send(res, 400, { error: 'No such project.' });
|
|
321
|
+
const depth = body.depth;
|
|
322
|
+
if (depth !== undefined && !DEPTHS.some((d) => d === depth))
|
|
323
|
+
return send(res, 400, { error: `Unknown depth "${String(depth)}".` });
|
|
324
|
+
const profile = body.profile;
|
|
325
|
+
if (profile !== undefined) {
|
|
326
|
+
const found = typeof profile === 'string' ? profiles.find((p) => p.id === profile) : undefined;
|
|
327
|
+
if (!found)
|
|
328
|
+
return send(res, 400, { error: `Unknown profile "${String(profile)}".` });
|
|
329
|
+
if (chosen.length > 1)
|
|
330
|
+
return send(res, 400, { error: 'A profile works with one project.' });
|
|
331
|
+
if (!found.kinds.includes(chosen[0].kind))
|
|
332
|
+
return send(res, 400, { error: `The ${found.id} profile does not fit "${chosen[0].name}".` });
|
|
333
|
+
}
|
|
334
|
+
const started = [];
|
|
335
|
+
for (const p of chosen) {
|
|
336
|
+
if (active.has(p.id))
|
|
337
|
+
continue;
|
|
338
|
+
active.set(p.id, {
|
|
339
|
+
path: p.path,
|
|
340
|
+
state: 'queued',
|
|
341
|
+
controller: new AbortController(),
|
|
342
|
+
events: [],
|
|
343
|
+
...(depth ? { depth: depth } : {}),
|
|
344
|
+
...(profile ? { profile: profile } : {}),
|
|
345
|
+
...(body.experimental === true ? { experimental: true } : {}),
|
|
346
|
+
});
|
|
347
|
+
queue.push(p.id);
|
|
348
|
+
started.push(p.id);
|
|
349
|
+
}
|
|
350
|
+
pump();
|
|
351
|
+
if (started.length > 0)
|
|
352
|
+
broadcast('projects', {});
|
|
353
|
+
return send(res, 202, { started });
|
|
354
|
+
}
|
|
355
|
+
case '/api/cancel': {
|
|
356
|
+
const text = await readBody(req);
|
|
357
|
+
if (text === undefined) {
|
|
358
|
+
res.setHeader('Connection', 'close');
|
|
359
|
+
return send(res, 413, { error: 'The request is too large.' });
|
|
360
|
+
}
|
|
361
|
+
let id;
|
|
362
|
+
try {
|
|
363
|
+
id = JSON.parse(text)?.id;
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
// An empty or unreadable body means every run.
|
|
367
|
+
}
|
|
368
|
+
if (typeof id === 'string')
|
|
369
|
+
cancel(id);
|
|
370
|
+
else
|
|
371
|
+
for (const key of [...active.keys()])
|
|
372
|
+
cancel(key);
|
|
373
|
+
return send(res, 200, { ok: true });
|
|
374
|
+
}
|
|
375
|
+
case '/api/report': {
|
|
376
|
+
const id = url.searchParams.get('id') ?? '';
|
|
377
|
+
const found = reportOf(id);
|
|
378
|
+
if (!found)
|
|
379
|
+
return send(res, 404, { error: 'No report yet.' });
|
|
380
|
+
const result = found.session?.result ?? readPreviousRun(found.dir);
|
|
381
|
+
if (!result)
|
|
382
|
+
return send(res, 404, { error: 'No report yet.' });
|
|
383
|
+
const diff = found.session?.diff;
|
|
384
|
+
return send(res, 200, {
|
|
385
|
+
summary: result.summary,
|
|
386
|
+
engines: result.engines,
|
|
387
|
+
finishedAt: result.finishedAt,
|
|
388
|
+
...(result.profile ? { profile: result.profile } : {}),
|
|
389
|
+
...(result.depth ? { depth: result.depth } : {}),
|
|
390
|
+
...(result.experimental ? { experimental: true } : {}),
|
|
391
|
+
...(diff ? { diff: { fixed: diff.fixed.length, added: diff.added.length, open: diff.open.length } } : {}),
|
|
392
|
+
files: found.session?.files ?? REPORT_FILES.map((f) => path.join(found.dir, f)),
|
|
393
|
+
dir: found.dir,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
case '/api/report.html': {
|
|
397
|
+
const found = reportOf(url.searchParams.get('id') ?? '');
|
|
398
|
+
try {
|
|
399
|
+
const body = readFileSync(path.join(found?.dir ?? '', 'report.html'));
|
|
400
|
+
if (!found)
|
|
401
|
+
throw new Error('none');
|
|
402
|
+
res.writeHead(200, { ...HEADERS, 'Content-Security-Policy': REPORT_CSP, 'Content-Type': TYPES['.html'] });
|
|
403
|
+
return void res.end(body);
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
return send(res, 404, { error: 'No report yet.' });
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
default: {
|
|
410
|
+
res.writeHead(200, {
|
|
411
|
+
...HEADERS,
|
|
412
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
413
|
+
Connection: 'keep-alive',
|
|
414
|
+
});
|
|
415
|
+
const runs = [
|
|
416
|
+
...[...finished].map(([id, f]) => ({ id, state: 'ended', events: f.events })),
|
|
417
|
+
...[...active].map(([id, a]) => ({ id, state: a.state, events: a.events })),
|
|
418
|
+
];
|
|
419
|
+
sse(res, 'snapshot', { runs });
|
|
420
|
+
clients.add(res);
|
|
421
|
+
const ping = setInterval(() => res.write(': ping\n\n'), 25000);
|
|
422
|
+
res.on('close', () => {
|
|
423
|
+
clearInterval(ping);
|
|
424
|
+
clients.delete(res);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
const server = http.createServer((req, res) => {
|
|
430
|
+
handle(req, res).catch(() => {
|
|
431
|
+
if (!res.headersSent)
|
|
432
|
+
send(res, 500, { error: 'Something went wrong.' });
|
|
433
|
+
else
|
|
434
|
+
res.end();
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
return new Promise((resolve, reject) => {
|
|
438
|
+
server.once('error', reject);
|
|
439
|
+
server.listen(opts.port ?? 0, '127.0.0.1', () => {
|
|
440
|
+
port = server.address().port;
|
|
441
|
+
resolve({
|
|
442
|
+
url: `http://127.0.0.1:${port}/?t=${token}`,
|
|
443
|
+
port,
|
|
444
|
+
token,
|
|
445
|
+
close: () => new Promise((done) => {
|
|
446
|
+
queue.length = 0;
|
|
447
|
+
for (const a of active.values())
|
|
448
|
+
a.controller.abort();
|
|
449
|
+
for (const c of clients)
|
|
450
|
+
c.end();
|
|
451
|
+
clients.clear();
|
|
452
|
+
server.close(() => done());
|
|
453
|
+
server.closeAllConnections();
|
|
454
|
+
}),
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION: string;
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Negative, zero or positive. A numeric part beats a text part, so 1.0-beta is older than 1.0. */
|
|
2
|
+
export declare function compareVersions(a: string, b: string): number;
|
|
3
|
+
export interface VersionRange {
|
|
4
|
+
min?: string | null;
|
|
5
|
+
minInclusive?: boolean;
|
|
6
|
+
max?: string | null;
|
|
7
|
+
maxInclusive?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function inRange(version: string, r: VersionRange): boolean;
|
package/dist/versions.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const isNum = (s) => /^\d+$/.test(s);
|
|
2
|
+
/** Negative, zero or positive. A numeric part beats a text part, so 1.0-beta is older than 1.0. */
|
|
3
|
+
export function compareVersions(a, b) {
|
|
4
|
+
const x = a.trim().toLowerCase().split(/[.\-+_]/);
|
|
5
|
+
const y = b.trim().toLowerCase().split(/[.\-+_]/);
|
|
6
|
+
for (let i = 0; i < Math.max(x.length, y.length); i++) {
|
|
7
|
+
const p = x[i] ?? '0';
|
|
8
|
+
const q = y[i] ?? '0';
|
|
9
|
+
if (isNum(p) && isNum(q)) {
|
|
10
|
+
if (Number(p) !== Number(q))
|
|
11
|
+
return Number(p) - Number(q);
|
|
12
|
+
}
|
|
13
|
+
else if (isNum(p) !== isNum(q)) {
|
|
14
|
+
return isNum(p) ? 1 : -1;
|
|
15
|
+
}
|
|
16
|
+
else if (p !== q) {
|
|
17
|
+
return p < q ? -1 : 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
const bound = (v) => !!v && v !== '*';
|
|
23
|
+
export function inRange(version, r) {
|
|
24
|
+
if (bound(r.min)) {
|
|
25
|
+
const c = compareVersions(version, r.min);
|
|
26
|
+
if (c < 0 || (c === 0 && !r.minInclusive))
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
if (bound(r.max)) {
|
|
30
|
+
const c = compareVersions(version, r.max);
|
|
31
|
+
if (c > 0 || (c === 0 && !r.maxInclusive))
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import wordpress from '@wordpress/eslint-plugin';
|
|
2
|
+
import globals from 'globals';
|
|
3
|
+
|
|
4
|
+
export default [
|
|
5
|
+
{ ignores: [ '**/node_modules/**', '**/vendor/**', '**/bower_components/**', '**/build/**', '**/dist/**', '**/*.min.js' ] },
|
|
6
|
+
...wordpress.configs.recommended,
|
|
7
|
+
// ESLint only lints the extensions a config names. Without this entry a .jsx file is skipped without a word.
|
|
8
|
+
{ files: [ '**/*.jsx' ] },
|
|
9
|
+
{
|
|
10
|
+
languageOptions: {
|
|
11
|
+
globals: { ...globals.browser, ...globals.jquery, wp: 'readonly', ajaxurl: 'readonly', _: 'readonly', Backbone: 'readonly' },
|
|
12
|
+
},
|
|
13
|
+
rules: {
|
|
14
|
+
'prettier/prettier': 'off',
|
|
15
|
+
'import/no-extraneous-dependencies': 'off',
|
|
16
|
+
'import/no-unresolved': 'off',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
];
|