@nudojs/harvester 0.1.0 → 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/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # @nudojs/harvester
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5786fa5: Auto-inject `@types` during analysis-path harvest and guard oversized dts work.
8
+
9
+ - Analysis can resolve package roots upward and feed harvested Abs into the B module graph.
10
+ - `harvestDts` accepts optional `maxFileBytes` / `maxMs` and skips unreadable or oversized files instead of failing the run.
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [5786fa5]
15
+ - Updated dependencies [5786fa5]
16
+ - @nudojs/core@0.3.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nudo Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -13,7 +13,10 @@ type HarvestedEnv = {
13
13
  skipped: number;
14
14
  };
15
15
  };
16
- declare function harvestDts(files: string[]): HarvestedEnv;
16
+ declare function harvestDts(files: string[], opts?: {
17
+ maxFileBytes?: number;
18
+ maxMs?: number;
19
+ }): HarvestedEnv;
17
20
  declare function emitEnvModule(env: HarvestedEnv, pkgName: string): string;
18
21
 
19
22
  export { type HarvestedEnv, emitEnvModule, harvestDts };
package/dist/index.js CHANGED
@@ -1,12 +1,14 @@
1
1
  // src/index.ts
2
- import { readFileSync } from "fs";
2
+ import { readFileSync, statSync } from "fs";
3
3
  import ts from "typescript";
4
4
  import { typeValueEquals, getFnSig, T } from "@nudojs/core";
5
5
  var MAX_ALIAS_DEPTH = 8;
6
6
  function scopeKey(scope) {
7
7
  return scope.moduleName ?? "\0global";
8
8
  }
9
- function harvestDts(files) {
9
+ function harvestDts(files, opts) {
10
+ const maxBytes = opts?.maxFileBytes ?? 15e5;
11
+ const deadline = opts?.maxMs !== void 0 ? Date.now() + opts.maxMs : void 0;
10
12
  const ctx = {
11
13
  globals: {},
12
14
  modules: {},
@@ -24,16 +26,33 @@ function harvestDts(files) {
24
26
  symbols: 0,
25
27
  skipped: 0
26
28
  };
29
+ let parsed = 0;
27
30
  for (const file of files) {
28
- const content = readFileSync(file, "utf8");
31
+ if (deadline !== void 0 && Date.now() > deadline) {
32
+ ctx.skipped += files.length - parsed;
33
+ break;
34
+ }
35
+ let content;
36
+ try {
37
+ const st = statSync(file);
38
+ if (st.size > maxBytes) {
39
+ ctx.skipped++;
40
+ continue;
41
+ }
42
+ content = readFileSync(file, "utf8");
43
+ } catch {
44
+ ctx.skipped++;
45
+ continue;
46
+ }
29
47
  const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
30
48
  collectStatements(ctx, sourceFile.statements, { moduleName: void 0, ns: [] });
49
+ parsed++;
31
50
  }
32
51
  materialize(ctx);
33
52
  return {
34
53
  globals: ctx.globals,
35
54
  modules: ctx.modules,
36
- stats: { files: files.length, symbols: ctx.symbols, skipped: ctx.skipped }
55
+ stats: { files: parsed, symbols: ctx.symbols, skipped: ctx.skipped }
37
56
  };
38
57
  }
39
58
  function emitEnvModule(env, pkgName) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nudojs/harvester",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Harvests TypeScript .d.ts declarations into Nudo env definitions",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -18,11 +18,11 @@
18
18
  "provenance": true,
19
19
  "registry": "https://registry.npmjs.org"
20
20
  },
21
+ "dependencies": {
22
+ "typescript": "^5.8.0",
23
+ "@nudojs/core": "0.3.0"
24
+ },
21
25
  "scripts": {
22
26
  "build": "tsup src/index.ts --format esm --dts"
23
- },
24
- "dependencies": {
25
- "@nudojs/core": "workspace:*",
26
- "typescript": "^5.8.0"
27
27
  }
28
- }
28
+ }
@@ -0,0 +1,8 @@
1
+ // harvest.test.ts 在运行时生成 generated-env.tmp.ts 并动态 import。
2
+ // 类型检查期该文件不存在,用通配环境声明提供其形状(脚本文件,非模块增强)。
3
+ declare module "*.tmp.ts" {
4
+ export function defineEnv(): {
5
+ globals: Record<string, unknown>;
6
+ modules: Record<string, Record<string, unknown>>;
7
+ };
8
+ }
@@ -98,9 +98,7 @@ describe("emitEnvModule", () => {
98
98
  });
99
99
 
100
100
  it("emitted module evaluates and defines the env", { timeout: 120_000 }, async () => {
101
- const mod = (await import("./generated-env.tmp.ts")) as {
102
- defineEnv: () => { globals: Record<string, unknown>; modules: Record<string, Record<string, unknown>> };
103
- };
101
+ const mod = await import("./generated-env.tmp.ts");
104
102
  const evaluated = mod.defineEnv();
105
103
  expect(evaluated.modules.fs).toBeDefined();
106
104
  expect(evaluated.modules["node:fs"].readFileSync).toBeDefined();
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { readFileSync } from "node:fs";
1
+ import { readFileSync, statSync } from "node:fs";
2
2
  import ts from "typescript";
3
3
  import { type TypeValue, typeValueEquals, getFnSig, T } from "@nudojs/core";
4
4
 
@@ -68,7 +68,12 @@ function scopeKey(scope: Scope): string {
68
68
  // Public API
69
69
  // ---------------------------------------------------------------------------
70
70
 
71
- export function harvestDts(files: string[]): HarvestedEnv {
71
+ export function harvestDts(
72
+ files: string[],
73
+ opts?: { maxFileBytes?: number; maxMs?: number },
74
+ ): HarvestedEnv {
75
+ const maxBytes = opts?.maxFileBytes ?? 1_500_000;
76
+ const deadline = opts?.maxMs !== undefined ? Date.now() + opts.maxMs : undefined;
72
77
  const ctx: HarvestContext = {
73
78
  globals: {},
74
79
  modules: {},
@@ -88,10 +93,27 @@ export function harvestDts(files: string[]): HarvestedEnv {
88
93
  };
89
94
 
90
95
  // Phase 1: collect declarations into the shared symbol table.
96
+ let parsed = 0;
91
97
  for (const file of files) {
92
- const content = readFileSync(file, "utf8");
98
+ if (deadline !== undefined && Date.now() > deadline) {
99
+ ctx.skipped += files.length - parsed;
100
+ break;
101
+ }
102
+ let content: string;
103
+ try {
104
+ const st = statSync(file);
105
+ if (st.size > maxBytes) {
106
+ ctx.skipped++;
107
+ continue;
108
+ }
109
+ content = readFileSync(file, "utf8");
110
+ } catch {
111
+ ctx.skipped++;
112
+ continue;
113
+ }
93
114
  const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
94
115
  collectStatements(ctx, sourceFile.statements, { moduleName: undefined, ns: [] });
116
+ parsed++;
95
117
  }
96
118
 
97
119
  // Phase 2: materialize TypeValues with the complete symbol table available.
@@ -100,7 +122,7 @@ export function harvestDts(files: string[]): HarvestedEnv {
100
122
  return {
101
123
  globals: ctx.globals,
102
124
  modules: ctx.modules,
103
- stats: { files: files.length, symbols: ctx.symbols, skipped: ctx.skipped },
125
+ stats: { files: parsed, symbols: ctx.symbols, skipped: ctx.skipped },
104
126
  };
105
127
  }
106
128