@abelxiaoxing/cadence 2.0.38 → 2.0.41

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.
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schema": "cadence-web-assets/1",
3
- "sourceHash": "7064b712d465c98bf51cc61fe6ec273ad51e169a60b2c7926e3a55a1dc46d205",
4
- "contractHash": "0e4bbeb78c11fa9948adefea308d4d00c15af7f4879265fc079aad4b021f2324",
5
- "buildId": "06a8c1e51525d871876933a4f5b8590940aa40c32d5c032f75de426b6f215a1a",
3
+ "sourceHash": "0b8654a89e99eb82073a244e6ee7313a438573796fb84e37defec19c6c03ae3c",
4
+ "contractHash": "002b8e5b103844b2237ba13aa27a72344e5bd61b94f96bb41181f53b9dd5a225",
5
+ "buildId": "e52874a3740b61d03e6fc01f25e96df1637aaeba7997a5b6162247f331be08ee",
6
6
  "assets": [
7
7
  {
8
8
  "name": "assets/cadence.css",
@@ -73,8 +73,8 @@
73
73
  },
74
74
  {
75
75
  "name": "assets/cadence.js",
76
- "bytes": 377072,
77
- "sha256": "2368a3c3fbdaaecfea4bdb82c25e3c5a4886480a5d195f4b6f86a020e8ec7802",
76
+ "bytes": 385379,
77
+ "sha256": "8930434abe3cb2e9fa9127854434dcbf8a9260aa7d348a3514677ab8e2a0cb71",
78
78
  "references": [
79
79
  "assets/markdown.js",
80
80
  "assets/react.js",
@@ -1,393 +0,0 @@
1
- /** One live filesystem observation. The suspended iterator owns real positions,
2
- * not a page number or a replay of an earlier scan. No shell or spill files. */
3
- import { constants } from "node:fs";
4
- import { lstat, open, opendir, realpath } from "node:fs/promises";
5
- import { basename, join, matchesGlob, relative, resolve, sep } from "node:path";
6
- import { withinRoot } from "./worker-workspace.mjs";
7
-
8
- export const RESEARCH_LIMITS = Object.freeze({
9
- toolMs: 30_000,
10
- entries: 2000,
11
- depth: 8,
12
- results: 100,
13
- fileBytes: 1024 * 1024, // Per file per page, NOT a permanently invisible suffix.
14
- scanBytes: 8 * 1024 * 1024,
15
- outputBytes: 32 * 1024, // Complete JSON-encoded tool result, including metadata.
16
- chunkBytes: 64 * 1024,
17
- lineBytes: 8 * 1024,
18
- scans: 4, // Includes opening, active, suspended and closing originals.
19
- idleMs: 120_000,
20
- files: 8,
21
- ranges: 32,
22
- patterns: 16,
23
- });
24
- export class SourceChanged extends Error {
25
- constructor() {
26
- super("Investigation source changed; start a fresh query explicitly.");
27
- }
28
- }
29
- const identity = (s) =>
30
- `${s.dev}:${s.ino}:${s.mode}:${s.size}:${s.mtimeNs}:${s.ctimeNs}`;
31
- const unavailable = (e) =>
32
- ["ENOENT", "ENOTDIR", "EACCES", "EPERM", "ELOOP"].includes(e?.code);
33
- const counters = () => ({
34
- readBytes: 0,
35
- inspectedEntries: 0,
36
- inspectedLines: 0,
37
- });
38
-
39
- export class ResearchScan {
40
- constructor(workspace, query) {
41
- this.workspace = workspace;
42
- this.query = query;
43
- this.omissions = new Map();
44
- this.total = counters();
45
- this.page = counters();
46
- this.fileBytes = new Map();
47
- this.sources = new Set();
48
- this.stopping = false;
49
- this.iterator = this.records();
50
- }
51
- begin(signal) {
52
- this.signal = signal;
53
- this.page = counters();
54
- this.fileBytes.clear();
55
- }
56
- check() {
57
- if (this.stopping || this.signal?.aborted)
58
- throw new Error("Investigation canceled");
59
- }
60
- count(key, n = 1) {
61
- this.page[key] += n;
62
- this.total[key] += n;
63
- }
64
- omit(reason) {
65
- this.omissions.set(reason, (this.omissions.get(reason) ?? 0) + 1);
66
- }
67
- async source(path) {
68
- this.check();
69
- if ((await realpath(path)) !== path) throw new SourceChanged();
70
- this.check();
71
- const info = await lstat(path, { bigint: true });
72
- this.check();
73
- return { path, identity: identity(info), info };
74
- }
75
- async validateSource(source) {
76
- let current;
77
- try {
78
- current = await this.source(source.path);
79
- } catch (e) {
80
- if (unavailable(e)) throw new SourceChanged();
81
- throw e;
82
- }
83
- if (current.identity !== source.identity) throw new SourceChanged();
84
- if (
85
- source.file &&
86
- identity(await source.file.stat({ bigint: true })) !== source.identity
87
- )
88
- throw new SourceChanged();
89
- this.check();
90
- }
91
- async validate() {
92
- for (const source of this.sources) await this.validateSource(source);
93
- }
94
- async closeHandle(handle) {
95
- try {
96
- await handle.close();
97
- } catch {
98
- this.closeFailed = true;
99
- // Never classify a failed close as an ordinary unreadable-file omission.
100
- throw new Error("Investigation handle closure failed");
101
- }
102
- }
103
- async target(path) {
104
- this.check();
105
- const selected = await realpath(resolve(this.roots[0], path ?? "."));
106
- this.check();
107
- const root = this.roots.findIndex((p) => withinRoot(p, selected));
108
- if (root < 0)
109
- throw new Error(
110
- "Path outside investigation roots; select the intended root explicitly.",
111
- );
112
- return { path: selected, root };
113
- }
114
- location(target) {
115
- return {
116
- root: target.root,
117
- path:
118
- relative(this.roots[target.root], target.path).split(sep).join("/") ||
119
- basename(target.path),
120
- };
121
- }
122
- async *records() {
123
- this.roots = [];
124
- // No parallel root resolution: an invalid root never abandons sibling I/O.
125
- for (const root of this.workspace.roots) {
126
- this.check();
127
- const path = await realpath(root);
128
- const source = await this.source(path);
129
- this.roots.push(path);
130
- this.sources.add(source);
131
- }
132
- if (this.query.kind === "read") {
133
- const files = new Map();
134
- for (const request of this.query.files) {
135
- const target = await this.target(request.path);
136
- const existing = files.get(target.path);
137
- if (existing) existing.ranges.push(...request.ranges);
138
- else files.set(target.path, { ...target, ranges: [...request.ranges] });
139
- }
140
- for (const target of files.values()) {
141
- target.ranges.sort((a, b) => a.start - b.start);
142
- const ranges = [];
143
- for (const range of target.ranges) {
144
- const last = ranges.at(-1);
145
- if (last && range.start <= last.end + 1)
146
- last.end = Math.max(last.end, range.end);
147
- else ranges.push({ ...range });
148
- }
149
- yield* this.lines(target, ranges);
150
- }
151
- return;
152
- }
153
- const target = await this.target(this.query.path);
154
- const source = await this.source(target.path);
155
- if (this.query.kind === "ls" && !source.info.isDirectory())
156
- throw new Error("Select a directory");
157
- if (!source.info.isDirectory() && !source.info.isFile()) {
158
- this.omit("special file omitted");
159
- return;
160
- }
161
- const files = source.info.isDirectory()
162
- ? this.walk(target, this.query.depth)
163
- : (async function* () {
164
- yield target;
165
- })();
166
- for await (const file of files) {
167
- if (file === null || file.pause) {
168
- yield file;
169
- continue;
170
- }
171
- if (this.query.kind === "ls") {
172
- yield { item: { ...this.location(file), kind: file.kind } };
173
- continue;
174
- }
175
- const glob = this.query.glob;
176
- const name =
177
- relative(target.path, file.path).split(sep).join("/") ||
178
- basename(file.path);
179
- if (
180
- glob &&
181
- !matchesGlob(glob.includes("/") ? name : basename(file.path), glob)
182
- ) {
183
- yield null;
184
- continue;
185
- }
186
- if (this.query.kind === "find")
187
- yield { item: { ...this.location(file), kind: "file" } };
188
- else {
189
- try {
190
- yield* this.lines(file);
191
- } catch (e) {
192
- if (!unavailable(e)) throw e;
193
- this.omit("unreadable file omitted");
194
- yield null;
195
- }
196
- }
197
- }
198
- }
199
- async *walk(target, remaining) {
200
- const source = await this.source(target.path);
201
- this.check();
202
- const dir = await opendir(target.path);
203
- this.sources.add(source);
204
- try {
205
- await this.validateSource(source);
206
- for (;;) {
207
- this.check();
208
- if (this.page.inspectedEntries >= RESEARCH_LIMITS.entries) {
209
- yield { pause: "entry quantum" };
210
- continue;
211
- }
212
- const entry = await dir.read();
213
- this.check();
214
- if (!entry) return;
215
- this.count("inspectedEntries");
216
- const file = { path: join(target.path, entry.name), root: target.root };
217
- if (this.query.kind === "ls") {
218
- yield {
219
- ...file,
220
- kind: entry.isSymbolicLink()
221
- ? "symlink"
222
- : entry.isDirectory()
223
- ? "directory"
224
- : entry.isFile()
225
- ? "file"
226
- : "special",
227
- };
228
- continue;
229
- }
230
- if (entry.isSymbolicLink()) this.omit("symlink omitted");
231
- else if (entry.isDirectory()) {
232
- if ([".git", "node_modules"].includes(entry.name))
233
- this.omit(".git/node_modules omitted");
234
- else if (!remaining) this.omit("depth limit");
235
- else {
236
- // Yield before descent too, so unmatched directories consume a quantum.
237
- yield null;
238
- try {
239
- yield* this.walk(file, remaining - 1);
240
- } catch (e) {
241
- if (!unavailable(e)) throw e;
242
- this.omit("unreadable directory omitted");
243
- }
244
- }
245
- } else if (entry.isFile()) {
246
- yield file;
247
- continue;
248
- } else this.omit("special file omitted");
249
- yield null;
250
- }
251
- } finally {
252
- try {
253
- if (!this.stopping && !this.signal?.aborted)
254
- await this.validateSource(source);
255
- } finally {
256
- await this.closeHandle(dir);
257
- this.sources.delete(source);
258
- }
259
- }
260
- }
261
- async *lines(target, ranges) {
262
- const source = await this.source(target.path);
263
- if (!source.info.isFile())
264
- throw new Error("Only regular text files can be read");
265
- this.check();
266
- const file = await open(
267
- target.path,
268
- constants.O_RDONLY | constants.O_NONBLOCK | (constants.O_NOFOLLOW ?? 0),
269
- );
270
- source.file = file;
271
- this.sources.add(source);
272
- try {
273
- this.check();
274
- if (!(await file.stat()).isFile())
275
- throw new Error("Only regular text files can be read");
276
- await this.validateSource(source);
277
- const buffer = Buffer.alloc(RESEARCH_LIMITS.chunkBytes);
278
- const line = Buffer.alloc(RESEARCH_LIMITS.lineBytes);
279
- // decode() starts a fresh stream for each line. Preserve U+FEFF rather
280
- // than treating every line's leading bytes as a new file BOM.
281
- const decoder = new TextDecoder("utf-8", {
282
- fatal: true,
283
- ignoreBOM: true,
284
- });
285
- let position = 0,
286
- used = 0,
287
- number = 1,
288
- long = false,
289
- rangeIndex = 0;
290
- const emit = () => {
291
- this.count("inspectedLines");
292
- while (
293
- ranges &&
294
- rangeIndex < ranges.length &&
295
- number > ranges[rangeIndex].end
296
- )
297
- rangeIndex++;
298
- const wanted =
299
- !ranges ||
300
- (rangeIndex < ranges.length && number >= ranges[rangeIndex].start);
301
- if (!wanted) return null;
302
- if (long) {
303
- this.omit("long line omitted");
304
- return null;
305
- }
306
- let text;
307
- try {
308
- text = decoder.decode(
309
- line.subarray(0, used && line[used - 1] === 13 ? used - 1 : used),
310
- );
311
- } catch {
312
- this.omit("non-UTF-8 line omitted");
313
- return null;
314
- }
315
- const item = { ...this.location(target), line: number, text };
316
- if (!ranges) {
317
- const haystack = this.query.ignoreCase ? text.toLowerCase() : text;
318
- const matched = this.query.patterns.flatMap((pattern, index) =>
319
- haystack.includes(pattern) ? [index] : [],
320
- );
321
- if (!matched.length) return null;
322
- item.matchedPatterns = matched;
323
- }
324
- return { item };
325
- };
326
- for (;;) {
327
- this.check();
328
- const allowance = Math.min(
329
- RESEARCH_LIMITS.chunkBytes,
330
- RESEARCH_LIMITS.fileBytes - (this.fileBytes.get(target.path) ?? 0),
331
- RESEARCH_LIMITS.scanBytes - this.page.readBytes,
332
- );
333
- if (!allowance) {
334
- yield { pause: "byte quantum" };
335
- continue;
336
- }
337
- const { bytesRead } = await file.read(buffer, 0, allowance, position);
338
- this.check();
339
- this.count("readBytes", bytesRead);
340
- this.fileBytes.set(
341
- target.path,
342
- (this.fileBytes.get(target.path) ?? 0) + bytesRead,
343
- );
344
- position += bytesRead;
345
- if (!bytesRead) {
346
- if (used || long) yield emit();
347
- return;
348
- }
349
- if (buffer.subarray(0, bytesRead).includes(0)) {
350
- this.omit("binary file omitted");
351
- return;
352
- }
353
- for (let at = 0; at < bytesRead; ) {
354
- this.check();
355
- const newline = buffer.indexOf(10, at);
356
- const end = newline < 0 || newline >= bytesRead ? bytesRead : newline;
357
- if (!long) {
358
- if (used + end - at > line.length) long = true;
359
- else {
360
- buffer.copy(line, used, at, end);
361
- used += end - at;
362
- }
363
- }
364
- at = end;
365
- if (end < bytesRead) {
366
- yield emit();
367
- if (ranges && number >= ranges.at(-1).end) return;
368
- number++;
369
- used = 0;
370
- long = false;
371
- at++;
372
- }
373
- }
374
- // Even a single huge line makes bounded, observable byte progress.
375
- yield null;
376
- }
377
- } finally {
378
- try {
379
- if (!this.stopping && !this.signal?.aborted)
380
- await this.validateSource(source);
381
- } finally {
382
- await this.closeHandle(file);
383
- this.sources.delete(source);
384
- }
385
- }
386
- }
387
- async close() {
388
- this.stopping = true;
389
- await this.iterator.return();
390
- if (this.closeFailed)
391
- throw new Error("Investigation handle closure failed");
392
- }
393
- }