loadwise 0.5.1 → 0.6.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/PERFORMANCE.md +31 -6
- package/README.md +580 -1024
- package/bin/loadwise.js +4 -0
- package/lib/accessibility/frontend-detect.js +2 -1
- package/lib/accessibility/rule-ids.js +12 -9
- package/{editor/accessibility-examples.cjs → lib/editor/accessibility-examples.js} +20 -21
- package/lib/editor/actions.js +117 -0
- package/{editor/apply-fix.cjs → lib/editor/apply-fix.js} +1 -6
- package/{editor/control-flow-comparison.cjs → lib/editor/control-flow-comparison.js} +1 -3
- package/lib/editor/diagnostics.js +365 -0
- package/lib/editor/dismissals.js +111 -0
- package/lib/editor/editor-document.js +99 -0
- package/{editor/finding-comparison.cjs → lib/editor/finding-comparison.js} +4 -7
- package/lib/editor/fix-edits.js +200 -0
- package/lib/editor/language-server.js +437 -0
- package/{editor/maintainability-examples.cjs → lib/editor/maintainability-examples.js} +1 -3
- package/{editor/perf-site-comparison.cjs → lib/editor/perf-site-comparison.js} +1 -3
- package/lib/editor/perf-watch.js +78 -0
- package/{editor/performance-examples.cjs → lib/editor/performance-examples.js} +129 -128
- package/{editor/reliability-examples.cjs → lib/editor/reliability-examples.js} +100 -41
- package/lib/editor/scan-session.js +334 -0
- package/{editor/security-examples.cjs → lib/editor/security-examples.js} +28 -32
- package/lib/editor/server-commands.js +232 -0
- package/{editor/source-example.cjs → lib/editor/source-example.js} +3 -5
- package/{editor/standalone-performance-examples.cjs → lib/editor/standalone-performance-examples.js} +349 -333
- package/lib/editor/types/dismissal.js +1 -0
- package/lib/editor/types/fix-example.js +1 -0
- package/lib/editor/types/source-document.js +1 -0
- package/lib/editor/workspace-files.js +78 -0
- package/lib/editor/workspace-paths.js +22 -0
- package/lib/maintainability/analyze-source.js +2 -0
- package/lib/maintainability/apply-fix.js +1 -1
- package/lib/maintainability/cli.js +1 -1
- package/lib/maintainability/examples.js +58 -41
- package/lib/maintainability/rule-ids.js +29 -14
- package/lib/maintainability/scan-changed.js +3 -0
- package/lib/maintainability/scan.js +13 -9
- package/lib/maintainability/tsconfig-strict.js +69 -0
- package/lib/maintainability/type-safety-hygiene.js +204 -0
- package/lib/performance/analyze.js +2 -0
- package/lib/performance/cli-arguments.js +0 -2
- package/lib/performance/cli-policy.js +17 -9
- package/lib/performance/cli-scan.js +10 -8
- package/lib/performance/cli.js +4 -23
- package/lib/performance/editor-signal.js +1 -1
- package/lib/performance/finding-text.js +3 -3
- package/lib/performance/fix-cli.js +1 -1
- package/lib/performance/frontend-perf.js +7 -0
- package/lib/performance/frontend-render-perf.js +200 -0
- package/lib/performance/llm-max-tokens.js +56 -0
- package/lib/performance/native.js +46 -26
- package/lib/performance/polyglot.js +22 -1
- package/lib/performance/pooling.js +38 -23
- package/lib/performance/projects.js +42 -21
- package/lib/performance/rule-ids.js +236 -222
- package/lib/performance/site-fixes/database.js +3 -1
- package/lib/performance/site-fixes/integrity.js +15 -1
- package/lib/performance/source-inputs.js +18 -6
- package/lib/performance/unscanned-javascript.js +66 -19
- package/lib/reliability/analyze.js +12 -0
- package/lib/reliability/child-process.js +106 -0
- package/lib/reliability/error-values.js +177 -0
- package/lib/reliability/pool-timeout.js +79 -0
- package/lib/reliability/react-bugs.js +602 -0
- package/lib/reliability/resource-leaks.js +3 -6
- package/lib/reliability/rule-ids.js +169 -127
- package/lib/reliability/switch-exhaustive.js +284 -0
- package/lib/reliability/uncaught-exception.js +223 -0
- package/lib/security/auth-crypto/rule-ids.js +3 -2
- package/lib/security/cli.js +6 -5
- package/lib/security/deps/rule-ids.js +1 -0
- package/lib/security/examples/auth-crypto.cjs +5 -4
- package/lib/security/examples/deps.cjs +1 -0
- package/lib/security/examples/exposure.cjs +55 -42
- package/lib/security/examples/files.cjs +1 -0
- package/lib/security/examples/injection.cjs +1 -0
- package/lib/security/examples/moved.cjs +5 -4
- package/lib/security/examples/request-forgery.cjs +1 -0
- package/lib/security/exposure/client-env.js +113 -0
- package/lib/security/exposure/frontend-security.js +12 -10
- package/lib/security/exposure/index.js +14 -3
- package/lib/security/exposure/jsx-url.js +267 -0
- package/lib/security/exposure/next-image-config.js +34 -0
- package/lib/security/exposure/package-manifest.js +45 -0
- package/lib/security/exposure/react-version.js +45 -0
- package/lib/security/exposure/rule-ids.js +16 -12
- package/lib/security/files/index.js +1 -1
- package/lib/security/files/rule-ids.js +1 -0
- package/lib/security/injection/code.js +24 -10
- package/lib/security/injection/command.js +81 -28
- package/lib/security/injection/nosql.js +109 -22
- package/lib/security/injection/redos.js +152 -308
- package/lib/security/injection/regex-pattern.js +290 -0
- package/lib/security/injection/regex.js +30 -11
- package/lib/security/injection/rule-ids.js +2 -1
- package/lib/security/injection/shared.js +19 -1
- package/lib/security/injection/sql.js +83 -2
- package/lib/security/request-forgery/rule-ids.js +1 -0
- package/lib/security/rule-descriptions.js +36 -32
- package/lib/security/rule-ids.js +74 -67
- package/lib/security/taint/filename-guard.js +3 -1
- package/lib/security/taint/guards.js +84 -10
- package/lib/security/taint/index.js +124 -13
- package/lib/security/taint/propagate.js +10 -7
- package/lib/security/taint/sanitizers.js +7 -3
- package/lib/security/taint/sources.js +6 -3
- package/lib/security/taint/summaries.js +13 -1
- package/lib/security/taint/url-parts.js +14 -5
- package/lib/shared/agent-session.js +2 -2
- package/lib/shared/check.js +9 -6
- package/lib/shared/explain.js +5 -5
- package/lib/shared/fix-convergence.js +1 -1
- package/lib/shared/formats/finding-location.js +6 -2
- package/lib/shared/formats/sarif.js +3 -3
- package/lib/shared/heap-limit.js +6 -0
- package/lib/shared/pillars.js +7 -7
- package/lib/shared/rule-docs.js +12 -0
- package/lib/shared/scan-cache.js +0 -1
- package/lib/shared/workspaces.js +31 -9
- package/package.json +25 -17
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/schema/loadwise.config.schema.json +44 -0
- package/editor/README.md +0 -47
- package/examples/redis-keys.otlp.json +0 -22
- package/lib/maintainability/notify-editor.js +0 -1
- package/lib/performance/editor-report.js +0 -191
- package/lib/performance/editor-setup.js +0 -189
- package/lib/performance/editor-watch-supervisor.js +0 -104
- package/lib/performance/editor-watch.js +0 -193
- package/lib/performance/notify-editor.js +0 -1
- package/lib/performance/recursive-watch.js +0 -181
- /package/lib/{performance/types/editor-report.js → editor/types/comparison.js} +0 -0
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
5
|
-
import ts from 'typescript';
|
|
6
|
-
function errorCode(error) {
|
|
7
|
-
return error instanceof Error && 'code' in error && typeof error.code === 'string' ? error.code : undefined;
|
|
8
|
-
}
|
|
9
|
-
const watchTask = {
|
|
10
|
-
label: 'Loadwise: Watch',
|
|
11
|
-
type: 'process',
|
|
12
|
-
command: 'node',
|
|
13
|
-
args: ['${workspaceFolder}/node_modules/loadwise/bin/loadwise.js', 'perf', 'editor', 'watch'],
|
|
14
|
-
options: { cwd: '${workspaceFolder}' },
|
|
15
|
-
isBackground: true,
|
|
16
|
-
runOptions: { runOn: 'folderOpen', instanceLimit: 1 },
|
|
17
|
-
presentation: { reveal: 'silent', revealProblems: 'never', panel: 'dedicated', clear: false },
|
|
18
|
-
problemMatcher: {
|
|
19
|
-
owner: 'loadwise',
|
|
20
|
-
source: 'Loadwise',
|
|
21
|
-
applyTo: 'allDocuments',
|
|
22
|
-
fileLocation: ['relative', '${workspaceFolder}'],
|
|
23
|
-
pattern: {
|
|
24
|
-
regexp: '^LOADWISE \\| (.+?) \\| (\\d+) \\| (\\d+) \\| (\\d+) \\| (error|warning) \\| ([a-z0-9-]+) \\| ([\\s\\S]+)$',
|
|
25
|
-
file: 1,
|
|
26
|
-
line: 2,
|
|
27
|
-
column: 3,
|
|
28
|
-
endColumn: 4,
|
|
29
|
-
severity: 5,
|
|
30
|
-
code: 6,
|
|
31
|
-
message: 7,
|
|
32
|
-
},
|
|
33
|
-
background: {
|
|
34
|
-
activeOnStart: false,
|
|
35
|
-
beginsPattern: '^Loadwise: scan started$',
|
|
36
|
-
endsPattern: '^Loadwise: scan finished$',
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
// Insert immediately after the opening bracket. Everything the user wrote, including comments,
|
|
41
|
-
// stays in place.
|
|
42
|
-
function prependItems(text, opening, items, hasExisting) {
|
|
43
|
-
const newline = text.includes('\r\n') ? '\r\n' : '\n';
|
|
44
|
-
const lineStart = text.lastIndexOf('\n', opening) + 1;
|
|
45
|
-
const indent = /^[ \t]*/.exec(text.slice(lineStart, opening))?.[0] ?? '';
|
|
46
|
-
const childIndent = indent + (indent.includes('\t') ? '\t' : ' ');
|
|
47
|
-
const insertion = items
|
|
48
|
-
.split('\n')
|
|
49
|
-
.map((line) => childIndent + line)
|
|
50
|
-
.join(newline);
|
|
51
|
-
const tail = text.slice(opening + 1);
|
|
52
|
-
const separator = /^[ \t]*\r?\n/.test(tail) ? '' : newline + (hasExisting ? childIndent : indent);
|
|
53
|
-
return text.slice(0, opening + 1) + newline + insertion + (hasExisting ? ',' : '') + separator + tail;
|
|
54
|
-
}
|
|
55
|
-
function parseTasksFile(filename, text) {
|
|
56
|
-
const source = ts.parseJsonText(filename, text);
|
|
57
|
-
const errors = [...(source.parseDiagnostics ?? [])];
|
|
58
|
-
const config = ts.convertToObject(source, errors);
|
|
59
|
-
const object = source.statements[0]?.expression;
|
|
60
|
-
if (errors.length || !object || !ts.isObjectLiteralExpression(object)) {
|
|
61
|
-
throw new Error(filename + ' must contain a valid JSONC object. Correct the existing file and retry.');
|
|
62
|
-
}
|
|
63
|
-
if (Object.hasOwn(config, 'version') && config.version !== '2.0.0') {
|
|
64
|
-
throw new Error(filename + ' uses an incompatible task version. Migrate it to 2.0.0 first.');
|
|
65
|
-
}
|
|
66
|
-
return { source, config, object };
|
|
67
|
-
}
|
|
68
|
-
function topLevelSettings(filename, object) {
|
|
69
|
-
const settings = new Map();
|
|
70
|
-
for (const property of object.properties) {
|
|
71
|
-
if (!ts.isPropertyAssignment(property) || !(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)))
|
|
72
|
-
continue;
|
|
73
|
-
const name = property.name.text;
|
|
74
|
-
if (name !== 'tasks' && name !== 'version')
|
|
75
|
-
continue;
|
|
76
|
-
if (settings.has(name))
|
|
77
|
-
throw new Error(filename + ' contains duplicate ' + name + ' properties.');
|
|
78
|
-
settings.set(name, property.initializer);
|
|
79
|
-
}
|
|
80
|
-
return settings;
|
|
81
|
-
}
|
|
82
|
-
function existingWatchTasks(filename, config) {
|
|
83
|
-
const matches = config.tasks.filter((task) => task?.label === watchTask.label);
|
|
84
|
-
if (matches.length > 1 || (matches.length && !isDeepStrictEqual(matches[0], watchTask))) {
|
|
85
|
-
throw new Error(filename +
|
|
86
|
-
' already defines a different or duplicate "' +
|
|
87
|
-
watchTask.label +
|
|
88
|
-
'". Rename or reconcile it before setup.');
|
|
89
|
-
}
|
|
90
|
-
return matches;
|
|
91
|
-
}
|
|
92
|
-
function updatedTasks(filename, text) {
|
|
93
|
-
if (text === undefined)
|
|
94
|
-
return JSON.stringify({ version: '2.0.0', tasks: [watchTask] }, null, 2) + '\n';
|
|
95
|
-
const { source, config, object } = parseTasksFile(filename, text);
|
|
96
|
-
const settings = topLevelSettings(filename, object);
|
|
97
|
-
const members = [];
|
|
98
|
-
if (!settings.has('version'))
|
|
99
|
-
members.push('"version": "2.0.0"');
|
|
100
|
-
const tasks = settings.get('tasks');
|
|
101
|
-
if (!tasks) {
|
|
102
|
-
members.push('"tasks": ' + JSON.stringify([watchTask], null, 2));
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
if (!ts.isArrayLiteralExpression(tasks))
|
|
106
|
-
throw new Error(filename + ' must have an array for tasks.');
|
|
107
|
-
const matches = existingWatchTasks(filename, config);
|
|
108
|
-
if (!matches.length)
|
|
109
|
-
text = prependItems(text, tasks.getStart(source), JSON.stringify(watchTask, null, 2), tasks.elements.length > 0);
|
|
110
|
-
}
|
|
111
|
-
return members.length
|
|
112
|
-
? prependItems(text, object.getStart(source), members.join(',\n'), object.properties.length > 0)
|
|
113
|
-
: text;
|
|
114
|
-
}
|
|
115
|
-
function readExisting(filename) {
|
|
116
|
-
const stat = fs.lstatSync(filename, { throwIfNoEntry: false });
|
|
117
|
-
if (!stat)
|
|
118
|
-
return;
|
|
119
|
-
if (!stat.isFile())
|
|
120
|
-
throw new Error(filename + ' must be a regular file.');
|
|
121
|
-
return { text: fs.readFileSync(filename, 'utf8'), mode: stat.mode };
|
|
122
|
-
}
|
|
123
|
-
// Windows editors and indexers can briefly open tasks.json without sharing delete access.
|
|
124
|
-
const sharingErrors = new Set(['EPERM', 'EBUSY', 'EACCES']);
|
|
125
|
-
const renameBackoffMs = [25, 50, 100];
|
|
126
|
-
function replaceExisting(temporary, filename, existing) {
|
|
127
|
-
for (let attempt = 0;; attempt++) {
|
|
128
|
-
try {
|
|
129
|
-
fs.renameSync(temporary, filename);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
const code = errorCode(error);
|
|
134
|
-
if (process.platform !== 'win32' ||
|
|
135
|
-
code === undefined ||
|
|
136
|
-
!sharingErrors.has(code) ||
|
|
137
|
-
attempt >= renameBackoffMs.length)
|
|
138
|
-
throw error;
|
|
139
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, renameBackoffMs[attempt]);
|
|
140
|
-
const current = readExisting(filename);
|
|
141
|
-
if (existing.text !== current?.text || existing.mode !== current?.mode)
|
|
142
|
-
throw new Error(filename + ' changed during setup. Run setup again.');
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
function setupEditor(root) {
|
|
147
|
-
if (!fs.statSync(root, { throwIfNoEntry: false })?.isDirectory())
|
|
148
|
-
throw new Error('Expected an existing workspace directory.');
|
|
149
|
-
const folder = path.resolve(root, '.vscode');
|
|
150
|
-
fs.mkdirSync(folder, { recursive: true });
|
|
151
|
-
if (!fs.lstatSync(folder).isDirectory())
|
|
152
|
-
throw new Error(folder + ' must be a regular workspace directory.');
|
|
153
|
-
const filename = path.join(folder, 'tasks.json');
|
|
154
|
-
const existing = readExisting(filename);
|
|
155
|
-
const text = updatedTasks(filename, existing?.text);
|
|
156
|
-
if (text === existing?.text)
|
|
157
|
-
return { filename, changed: false };
|
|
158
|
-
const temporary = path.join(folder, '.loadwise-' + randomUUID() + '.tmp');
|
|
159
|
-
try {
|
|
160
|
-
fs.writeFileSync(temporary, text, { flag: 'wx', mode: existing?.mode });
|
|
161
|
-
const current = readExisting(filename);
|
|
162
|
-
if (text === current?.text)
|
|
163
|
-
return { filename, changed: false };
|
|
164
|
-
if (existing?.text !== current?.text)
|
|
165
|
-
throw new Error(filename + ' changed during setup. Run setup again.');
|
|
166
|
-
if (existing) {
|
|
167
|
-
fs.chmodSync(temporary, existing.mode);
|
|
168
|
-
replaceExisting(temporary, filename, existing);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
try {
|
|
172
|
-
fs.linkSync(temporary, filename);
|
|
173
|
-
}
|
|
174
|
-
catch (error) {
|
|
175
|
-
const code = errorCode(error);
|
|
176
|
-
if (code === 'EEXIST' && readExisting(filename)?.text === text)
|
|
177
|
-
return { filename, changed: false };
|
|
178
|
-
if (code === undefined || !['EPERM', 'ENOTSUP', 'EOPNOTSUPP', 'ENOSYS'].includes(code))
|
|
179
|
-
throw error;
|
|
180
|
-
fs.copyFileSync(temporary, filename, fs.constants.COPYFILE_EXCL);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
finally {
|
|
185
|
-
fs.rmSync(temporary, { force: true });
|
|
186
|
-
}
|
|
187
|
-
return { filename, changed: true };
|
|
188
|
-
}
|
|
189
|
-
export { setupEditor };
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { fork } from 'node:child_process';
|
|
4
|
-
const installPollMs = 1000;
|
|
5
|
-
const installSettleMs = 700;
|
|
6
|
-
const installRetryMs = 3000;
|
|
7
|
-
const maxInstallRetries = 3;
|
|
8
|
-
function superviseEditor(root, explicitConfig) {
|
|
9
|
-
// Keep the installed path, including a package-manager symlink, so a replacement resolves to the
|
|
10
|
-
// new checkout.
|
|
11
|
-
const script = path.resolve(process.argv[1]);
|
|
12
|
-
const installed = path.join(root, 'node_modules', 'loadwise', 'bin', 'loadwise.js');
|
|
13
|
-
const cli = fs.existsSync(installed) && fs.realpathSync(installed) === fs.realpathSync(script) ? installed : script;
|
|
14
|
-
const markers = new Set([cli, path.join(path.dirname(path.dirname(cli)), 'package.json')]);
|
|
15
|
-
let worker, timer, retries = maxInstallRetries, restarting = false, stopped = false;
|
|
16
|
-
function launch() {
|
|
17
|
-
if (stopped || !fs.existsSync(cli))
|
|
18
|
-
return;
|
|
19
|
-
restarting = false;
|
|
20
|
-
worker = fork(cli, ['perf', 'editor', 'watch', ...(explicitConfig ? ['-p', explicitConfig] : [])], {
|
|
21
|
-
cwd: root,
|
|
22
|
-
env: { ...process.env, LOADWISE_EDITOR_SCAN: '1', LOADWISE_EDITOR_WORKER: '1' },
|
|
23
|
-
execArgv: [],
|
|
24
|
-
windowsHide: true,
|
|
25
|
-
stdio: ['ignore', 'inherit', 'inherit', 'ipc'],
|
|
26
|
-
});
|
|
27
|
-
const child = worker;
|
|
28
|
-
child.on('error', (error) => {
|
|
29
|
-
console.error(error.message);
|
|
30
|
-
if (!child.pid)
|
|
31
|
-
finished(2);
|
|
32
|
-
});
|
|
33
|
-
// On Windows a manually disconnected IPC channel can outlive the child's exit event.
|
|
34
|
-
child.once('exit', finished);
|
|
35
|
-
function finished(code) {
|
|
36
|
-
worker = undefined;
|
|
37
|
-
if (stopped)
|
|
38
|
-
return;
|
|
39
|
-
if (restarting) {
|
|
40
|
-
if (!timer)
|
|
41
|
-
launch();
|
|
42
|
-
}
|
|
43
|
-
else if (code !== 0) {
|
|
44
|
-
// A package manager can publish its manifest and entry point before its runtime files.
|
|
45
|
-
if (retries > 0) {
|
|
46
|
-
retries--;
|
|
47
|
-
timer = setTimeout(restart, installRetryMs);
|
|
48
|
-
}
|
|
49
|
-
else
|
|
50
|
-
console.error('Editor worker exited. Waiting for a checker package update.');
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
stop();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function terminateWorker(target) {
|
|
58
|
-
if (!target)
|
|
59
|
-
return;
|
|
60
|
-
if (target.connected)
|
|
61
|
-
target.disconnect();
|
|
62
|
-
const killTimer = setTimeout(() => {
|
|
63
|
-
if (target.exitCode === null && target.signalCode === null)
|
|
64
|
-
target.kill('SIGKILL');
|
|
65
|
-
}, 3000);
|
|
66
|
-
killTimer.unref();
|
|
67
|
-
}
|
|
68
|
-
function restart() {
|
|
69
|
-
timer = undefined;
|
|
70
|
-
restarting = true;
|
|
71
|
-
if (worker) {
|
|
72
|
-
terminateWorker(worker);
|
|
73
|
-
}
|
|
74
|
-
else
|
|
75
|
-
launch();
|
|
76
|
-
}
|
|
77
|
-
function installationChanged(current, previous) {
|
|
78
|
-
// watchFile reports an initial all-zero stat for an optional package that is not installed.
|
|
79
|
-
if (!current.nlink && !previous.nlink)
|
|
80
|
-
return;
|
|
81
|
-
retries = maxInstallRetries;
|
|
82
|
-
restarting = true;
|
|
83
|
-
clearTimeout(timer);
|
|
84
|
-
timer = setTimeout(restart, installSettleMs);
|
|
85
|
-
}
|
|
86
|
-
function stop() {
|
|
87
|
-
stopped = true;
|
|
88
|
-
clearTimeout(timer);
|
|
89
|
-
for (const marker of markers)
|
|
90
|
-
fs.unwatchFile(marker, installationChanged);
|
|
91
|
-
if (worker)
|
|
92
|
-
terminateWorker(worker);
|
|
93
|
-
process.off('SIGINT', stop);
|
|
94
|
-
process.off('SIGTERM', stop);
|
|
95
|
-
}
|
|
96
|
-
// Poll only these entry points. Filesystem watches on the old inode can go silent after a
|
|
97
|
-
// dependency is replaced.
|
|
98
|
-
for (const marker of markers)
|
|
99
|
-
fs.watchFile(marker, { interval: installPollMs }, installationChanged);
|
|
100
|
-
process.once('SIGINT', stop);
|
|
101
|
-
process.once('SIGTERM', stop);
|
|
102
|
-
launch();
|
|
103
|
-
}
|
|
104
|
-
export { superviseEditor };
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { promisify } from 'node:util';
|
|
4
|
-
import { execFile as execFileNodeChildProcess } from 'node:child_process';
|
|
5
|
-
import { superviseEditor } from './editor-watch-supervisor.js';
|
|
6
|
-
import { watchTree } from './recursive-watch.js';
|
|
7
|
-
import { isSourceInput } from './source-inputs.js';
|
|
8
|
-
import { formatFinding } from './editor-report.js';
|
|
9
|
-
import { checkPolyglot } from './polyglot.js';
|
|
10
|
-
import { listenForEditorRefresh } from './editor-signal.js';
|
|
11
|
-
const execFile = promisify(execFileNodeChildProcess);
|
|
12
|
-
const ignoredDirectories = new Set([
|
|
13
|
-
'.git',
|
|
14
|
-
'.hg',
|
|
15
|
-
'.svn',
|
|
16
|
-
'node_modules',
|
|
17
|
-
'dist',
|
|
18
|
-
'build',
|
|
19
|
-
'coverage',
|
|
20
|
-
'.next',
|
|
21
|
-
'.nuxt',
|
|
22
|
-
'.venv',
|
|
23
|
-
'venv',
|
|
24
|
-
'__pycache__',
|
|
25
|
-
'.cache',
|
|
26
|
-
'.turbo',
|
|
27
|
-
'target',
|
|
28
|
-
'vendor',
|
|
29
|
-
'.worktrees',
|
|
30
|
-
]);
|
|
31
|
-
const nativePerformanceSource = /\.(?:py|go|java|cs|rs)$/i;
|
|
32
|
-
const otherMaintainabilitySource = /\.(?:rb|php|kts?|c|cc|cpp|cxx|h|hh|hpp|hxx)$/i;
|
|
33
|
-
const debounceMs = 500;
|
|
34
|
-
function exists(file) {
|
|
35
|
-
return fs.access(file).then(() => true, () => false);
|
|
36
|
-
}
|
|
37
|
-
async function firstExisting(root, names) {
|
|
38
|
-
for (const name of names)
|
|
39
|
-
if (await exists(path.join(root, name)))
|
|
40
|
-
return name;
|
|
41
|
-
}
|
|
42
|
-
// The maintainability checker ships in this package, so its findings always come from the same
|
|
43
|
-
// release.
|
|
44
|
-
const loadwiseCli = path.resolve(import.meta.dirname, '../../bin/loadwise.js');
|
|
45
|
-
async function maintainability(cli, root, signal) {
|
|
46
|
-
let stdout;
|
|
47
|
-
try {
|
|
48
|
-
({ stdout } = await execFile(process.execPath, [cli, 'maint', '.', '--json'], {
|
|
49
|
-
cwd: root,
|
|
50
|
-
signal,
|
|
51
|
-
windowsHide: true,
|
|
52
|
-
maxBuffer: 64 * 1024 * 1024,
|
|
53
|
-
}));
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
if (!(error instanceof Error))
|
|
57
|
-
throw error;
|
|
58
|
-
const stderr = 'stderr' in error && typeof error.stderr === 'string' ? error.stderr.trim() : '';
|
|
59
|
-
if (!('code' in error) || error.code !== 1)
|
|
60
|
-
throw new Error(stderr || error.message, { cause: error });
|
|
61
|
-
stdout = 'stdout' in error && typeof error.stdout === 'string' ? error.stdout : '';
|
|
62
|
-
}
|
|
63
|
-
return JSON.parse(stdout).findings;
|
|
64
|
-
}
|
|
65
|
-
function watchEditor(root, explicitConfig, checkCode, cli = loadwiseCli, hooks = {}) {
|
|
66
|
-
if (!process.send || process.env.LOADWISE_EDITOR_WORKER !== '1') {
|
|
67
|
-
return superviseEditor(root, explicitConfig);
|
|
68
|
-
}
|
|
69
|
-
const jsFiles = new Set();
|
|
70
|
-
const polyglotFiles = new Set();
|
|
71
|
-
const otherSources = new Set();
|
|
72
|
-
const controller = new AbortController();
|
|
73
|
-
let ready = false;
|
|
74
|
-
let running = false;
|
|
75
|
-
let dirty = false;
|
|
76
|
-
let timer;
|
|
77
|
-
let scanController;
|
|
78
|
-
async function scan() {
|
|
79
|
-
running = true;
|
|
80
|
-
dirty = false;
|
|
81
|
-
const scanSignal = (scanController = new AbortController()).signal;
|
|
82
|
-
const fileCache = new Map();
|
|
83
|
-
console.log('Loadwise: scan started');
|
|
84
|
-
async function report(source, scanSource) {
|
|
85
|
-
try {
|
|
86
|
-
const findings = await scanSource();
|
|
87
|
-
for (const finding of findings) {
|
|
88
|
-
const marker = formatFinding(root, source, finding, fileCache);
|
|
89
|
-
if (marker)
|
|
90
|
-
console.log(marker);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
catch (error) {
|
|
94
|
-
if (!controller.signal.aborted && !scanSignal.aborted)
|
|
95
|
-
console.error(source +
|
|
96
|
-
' scan failed: ' +
|
|
97
|
-
(error instanceof Error ? error.message : String(error)).replaceAll(/[\p{Cc}\p{Zl}\p{Zp}]/gu, ' '));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
const config = explicitConfig ?? (await firstExisting(root, ['tsconfig.json', 'jsconfig.json']));
|
|
102
|
-
if (!scanSignal.aborted && (config || jsFiles.size)) {
|
|
103
|
-
await report('loadwise perf', () => checkCode(config ? ['-p', config] : Array.from(jsFiles, (file) => path.resolve(root, file))));
|
|
104
|
-
}
|
|
105
|
-
if (!scanSignal.aborted && polyglotFiles.size) {
|
|
106
|
-
await report('loadwise perf', async () => (await checkPolyglot([root])).findings);
|
|
107
|
-
}
|
|
108
|
-
if (!scanSignal.aborted) {
|
|
109
|
-
await report('loadwise perf', async () => {
|
|
110
|
-
const migrationsCli = await import('./migrations/cli.js');
|
|
111
|
-
return migrationsCli.checkProjectMigrations({ root });
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
if (!scanSignal.aborted && (jsFiles.size || polyglotFiles.size || otherSources.size)) {
|
|
115
|
-
await report('loadwise maint', () => maintainability(cli, root, scanSignal));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
finally {
|
|
119
|
-
console.log('Loadwise: scan finished');
|
|
120
|
-
running = false;
|
|
121
|
-
scanController = undefined;
|
|
122
|
-
if (dirty && !timer && !controller.signal.aborted)
|
|
123
|
-
schedule();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function runScheduled() {
|
|
127
|
-
timer = undefined;
|
|
128
|
-
if (!running)
|
|
129
|
-
void scan();
|
|
130
|
-
}
|
|
131
|
-
function schedule() {
|
|
132
|
-
dirty = true;
|
|
133
|
-
if (!ready || controller.signal.aborted)
|
|
134
|
-
return;
|
|
135
|
-
// The in-flight scan is stale: stop its child, the follow-up starts one debounce after the last
|
|
136
|
-
// change.
|
|
137
|
-
scanController?.abort();
|
|
138
|
-
clearTimeout(timer);
|
|
139
|
-
timer = setTimeout(runScheduled, debounceMs);
|
|
140
|
-
}
|
|
141
|
-
const watcher = (hooks.watchTree ?? watchTree)(root, (relative, stat) => {
|
|
142
|
-
if (relative.split(path.sep).some((segment) => ignoredDirectories.has(segment)))
|
|
143
|
-
return true;
|
|
144
|
-
return Boolean(stat?.isFile() && /(?:\.min\.(?:[cm]?js|css)|\.map|\.tsbuildinfo)$/i.test(relative));
|
|
145
|
-
});
|
|
146
|
-
watcher.on('all', (event, file) => {
|
|
147
|
-
if (!['add', 'unlink', 'change'].includes(event))
|
|
148
|
-
return;
|
|
149
|
-
const files = isSourceInput(file)
|
|
150
|
-
? jsFiles
|
|
151
|
-
: nativePerformanceSource.test(file)
|
|
152
|
-
? polyglotFiles
|
|
153
|
-
: otherMaintainabilitySource.test(file)
|
|
154
|
-
? otherSources
|
|
155
|
-
: undefined;
|
|
156
|
-
if (files) {
|
|
157
|
-
if (event === 'unlink')
|
|
158
|
-
files.delete(file);
|
|
159
|
-
else
|
|
160
|
-
files.add(file);
|
|
161
|
-
}
|
|
162
|
-
if (files || /\.json$|\.(?:sql|prisma)$|\.d\.[cm]?ts$|(?:^|[/\\])\.gitignore$/i.test(file) || event !== 'change')
|
|
163
|
-
schedule();
|
|
164
|
-
});
|
|
165
|
-
watcher.once('ready', () => {
|
|
166
|
-
ready = true;
|
|
167
|
-
schedule();
|
|
168
|
-
});
|
|
169
|
-
const refresh = (hooks.listenForEditorRefresh ?? listenForEditorRefresh)(root, schedule);
|
|
170
|
-
function stop() {
|
|
171
|
-
clearTimeout(timer);
|
|
172
|
-
controller.abort();
|
|
173
|
-
scanController?.abort();
|
|
174
|
-
void watcher.close();
|
|
175
|
-
refresh.close();
|
|
176
|
-
process.off('disconnect', stop);
|
|
177
|
-
if (process.connected)
|
|
178
|
-
process.disconnect?.();
|
|
179
|
-
process.off('SIGINT', stop);
|
|
180
|
-
process.off('SIGTERM', stop);
|
|
181
|
-
}
|
|
182
|
-
watcher.on('error', (error) => {
|
|
183
|
-
console.error(error.message);
|
|
184
|
-
process.exitCode = 2;
|
|
185
|
-
stop();
|
|
186
|
-
});
|
|
187
|
-
process.once('SIGINT', stop);
|
|
188
|
-
process.once('SIGTERM', stop);
|
|
189
|
-
process.once('disconnect', stop);
|
|
190
|
-
if (!process.connected)
|
|
191
|
-
stop();
|
|
192
|
-
}
|
|
193
|
-
export { watchEditor };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../shared/notify-editor.js';
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { EventEmitter } from 'node:events';
|
|
4
|
-
// fs.watch fires several raw events per save (and rename pairs for atomic writes); one short settle
|
|
5
|
-
// merges them.
|
|
6
|
-
const settleMs = 50;
|
|
7
|
-
// Watches root recursively with the built-in fs.watch and emits chokidar-style events: 'all'
|
|
8
|
-
// (event: 'add' | 'change' | 'unlink', relativePath), 'ready' after the initial walk, and 'error'.
|
|
9
|
-
// ignored(relativePath, stats) is checked for every directory and file, as fs.watch cannot exclude
|
|
10
|
-
// paths itself.
|
|
11
|
-
function watchTree(root, ignored) {
|
|
12
|
-
const emitter = Object.assign(new EventEmitter(), { close: async () => { } });
|
|
13
|
-
const knownFiles = new Map();
|
|
14
|
-
const knownDirectories = new Set();
|
|
15
|
-
const pendingPaths = new Set();
|
|
16
|
-
let rescanPending = false;
|
|
17
|
-
let settleTimer;
|
|
18
|
-
let closed = false;
|
|
19
|
-
let nativeWatcher;
|
|
20
|
-
function fail(error) {
|
|
21
|
-
if (closed)
|
|
22
|
-
return;
|
|
23
|
-
emitter.emit('error', error);
|
|
24
|
-
}
|
|
25
|
-
function lstat(relative) {
|
|
26
|
-
return fs.promises.lstat(path.join(root, relative)).catch((error) => {
|
|
27
|
-
if (error.code === 'ENOENT' || error.code === 'ENOTDIR' || error.code === 'EPERM')
|
|
28
|
-
return undefined;
|
|
29
|
-
throw error;
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
function emitFile(relative, stats, changed) {
|
|
33
|
-
if (closed)
|
|
34
|
-
return;
|
|
35
|
-
const known = knownFiles.has(relative);
|
|
36
|
-
knownFiles.set(relative, stats.mtimeMs);
|
|
37
|
-
if (!known)
|
|
38
|
-
emitter.emit('all', 'add', relative);
|
|
39
|
-
else if (changed)
|
|
40
|
-
emitter.emit('all', 'change', relative);
|
|
41
|
-
}
|
|
42
|
-
function forget(relative) {
|
|
43
|
-
const prefix = relative + path.sep;
|
|
44
|
-
for (const file of knownFiles.keys()) {
|
|
45
|
-
if (file !== relative && !file.startsWith(prefix))
|
|
46
|
-
continue;
|
|
47
|
-
knownFiles.delete(file);
|
|
48
|
-
if (!closed)
|
|
49
|
-
emitter.emit('all', 'unlink', file);
|
|
50
|
-
}
|
|
51
|
-
for (const directory of knownDirectories) {
|
|
52
|
-
if (directory === relative || directory.startsWith(prefix))
|
|
53
|
-
knownDirectories.delete(directory);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
// Walks a directory, reporting new files (and, when compareMtime, files edited since they were
|
|
57
|
-
// last seen).
|
|
58
|
-
async function walk(relative, seen, compareMtime = false) {
|
|
59
|
-
let entries;
|
|
60
|
-
try {
|
|
61
|
-
entries = await fs.promises.readdir(path.join(root, relative), { withFileTypes: true });
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
if (relative === '')
|
|
65
|
-
throw error;
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
knownDirectories.add(relative);
|
|
69
|
-
for (const entry of entries) {
|
|
70
|
-
if (closed)
|
|
71
|
-
return;
|
|
72
|
-
if (!entry.isFile() && !entry.isDirectory())
|
|
73
|
-
continue;
|
|
74
|
-
const child = relative ? path.join(relative, entry.name) : entry.name;
|
|
75
|
-
const stats = await lstat(child);
|
|
76
|
-
if (!stats || ignored(child, stats))
|
|
77
|
-
continue;
|
|
78
|
-
if (stats.isDirectory())
|
|
79
|
-
await walk(child, seen, compareMtime);
|
|
80
|
-
else if (stats.isFile()) {
|
|
81
|
-
seen?.add(child);
|
|
82
|
-
emitFile(child, stats, compareMtime && knownFiles.get(child) !== stats.mtimeMs);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async function rescan() {
|
|
87
|
-
const seen = new Set();
|
|
88
|
-
await walk('', seen, true);
|
|
89
|
-
for (const file of knownFiles.keys())
|
|
90
|
-
if (!seen.has(file))
|
|
91
|
-
forget(file);
|
|
92
|
-
}
|
|
93
|
-
async function settlePath(relative) {
|
|
94
|
-
if (relative.split(path.sep).some((segment) => segment === '..'))
|
|
95
|
-
return;
|
|
96
|
-
const stats = await lstat(relative);
|
|
97
|
-
if (!stats)
|
|
98
|
-
return forget(relative);
|
|
99
|
-
if (ignored(relative, stats))
|
|
100
|
-
return;
|
|
101
|
-
if (stats.isFile()) {
|
|
102
|
-
if (knownDirectories.has(relative))
|
|
103
|
-
forget(relative);
|
|
104
|
-
return emitFile(relative, stats, true);
|
|
105
|
-
}
|
|
106
|
-
if (!stats.isDirectory())
|
|
107
|
-
return;
|
|
108
|
-
if (knownFiles.has(relative))
|
|
109
|
-
forget(relative);
|
|
110
|
-
// A new or renamed-in directory: its files may land before the watch covers it, so walk it.
|
|
111
|
-
if (!knownDirectories.has(relative))
|
|
112
|
-
await walk(relative);
|
|
113
|
-
}
|
|
114
|
-
let flushing = false;
|
|
115
|
-
async function flush() {
|
|
116
|
-
settleTimer = undefined;
|
|
117
|
-
if (flushing || closed)
|
|
118
|
-
return;
|
|
119
|
-
flushing = true;
|
|
120
|
-
const paths = [...pendingPaths];
|
|
121
|
-
pendingPaths.clear();
|
|
122
|
-
const fullRescan = rescanPending;
|
|
123
|
-
rescanPending = false;
|
|
124
|
-
try {
|
|
125
|
-
// A vanished root never reports itself reliably on every platform, so check it on each batch.
|
|
126
|
-
if (!(await fs.promises.stat(root)).isDirectory())
|
|
127
|
-
throw new Error('Watched root is not a directory: ' + root);
|
|
128
|
-
if (fullRescan)
|
|
129
|
-
await rescan();
|
|
130
|
-
else
|
|
131
|
-
for (const relative of paths)
|
|
132
|
-
await settlePath(relative);
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
fail(error);
|
|
136
|
-
}
|
|
137
|
-
finally {
|
|
138
|
-
flushing = false;
|
|
139
|
-
if (!closed && (pendingPaths.size > 0 || rescanPending)) {
|
|
140
|
-
settleTimer ??= setTimeout(() => void flush(), settleMs);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
function queue(filename) {
|
|
145
|
-
if (closed)
|
|
146
|
-
return;
|
|
147
|
-
const relative = filename ? path.normalize(filename.toString()) : '';
|
|
148
|
-
// Without stats, ignored() can only reject by path, which drops events under node_modules, .git
|
|
149
|
-
// and so on.
|
|
150
|
-
if (relative !== '.' && ignored(relative))
|
|
151
|
-
return;
|
|
152
|
-
// No filename (or the root itself) means "something changed somewhere": diff the whole tree.
|
|
153
|
-
if (relative === '' || relative === '.')
|
|
154
|
-
rescanPending = true;
|
|
155
|
-
else
|
|
156
|
-
pendingPaths.add(relative);
|
|
157
|
-
settleTimer ??= setTimeout(() => void flush(), settleMs);
|
|
158
|
-
}
|
|
159
|
-
try {
|
|
160
|
-
nativeWatcher = fs.watch(root, { recursive: true, persistent: true }, (_eventType, filename) => queue(filename));
|
|
161
|
-
nativeWatcher.on('error', fail);
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
process.nextTick(fail, error);
|
|
165
|
-
emitter.close = async () => {
|
|
166
|
-
closed = true;
|
|
167
|
-
};
|
|
168
|
-
return emitter;
|
|
169
|
-
}
|
|
170
|
-
walk('', undefined, false).then(() => {
|
|
171
|
-
if (!closed)
|
|
172
|
-
emitter.emit('ready');
|
|
173
|
-
}, fail);
|
|
174
|
-
emitter.close = async () => {
|
|
175
|
-
closed = true;
|
|
176
|
-
clearTimeout(settleTimer);
|
|
177
|
-
nativeWatcher?.close();
|
|
178
|
-
};
|
|
179
|
-
return emitter;
|
|
180
|
-
}
|
|
181
|
-
export { watchTree };
|
|
File without changes
|