@awsless/ts-file-cache 0.0.19 → 0.0.21

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/dist/index.cjs CHANGED
@@ -3,65 +3,21 @@ let fs_promises = require("fs/promises");
3
3
  let path = require("path");
4
4
  let crypto = require("crypto");
5
5
  let node_module = require("node:module");
6
- let _swc_core = require("@swc/core");
7
- let swc_walk = require("swc-walk");
8
- let swc_walk_baseVisitor = require("swc-walk/baseVisitor");
6
+ let estree_walker = require("estree-walker");
7
+ let oxc_parser = require("oxc-parser");
9
8
  let yaml = require("yaml");
10
9
  //#region src/import.ts
11
- var PatchedBaseVisitor = class extends swc_walk_baseVisitor.BaseVisitor {
12
- FunctionBody(node, state, callback) {
13
- for (const statement of node.stmts) callback(statement, state);
14
- }
15
- };
16
- const baseVisitor = new PatchedBaseVisitor();
17
- const parseOptions = (file) => {
18
- if (file.endsWith(".tsx")) return {
19
- syntax: "typescript",
20
- tsx: true,
21
- decorators: true
22
- };
23
- if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) return {
24
- syntax: "typescript",
25
- decorators: true
26
- };
27
- return {
28
- syntax: "ecmascript",
29
- jsx: true,
30
- decorators: true
31
- };
32
- };
33
10
  const findImports = async (file, code) => {
34
- let ast;
35
- try {
36
- ast = await (0, _swc_core.parse)(code, parseOptions(file));
37
- } catch (error) {
38
- throw new Error(`Failed to parse: ${file}`, { cause: error });
39
- }
11
+ const ast = (0, oxc_parser.parseSync)(file, code);
12
+ if (ast.errors.length > 0) throw new Error(`Failed to parse: ${file}`, { cause: ast.errors[0] });
40
13
  const importing = /* @__PURE__ */ new Set();
41
- try {
42
- (0, swc_walk.simple)(ast, {
43
- ImportDeclaration(node) {
44
- importing.add(node.source.value);
45
- },
46
- ExportAllDeclaration(node) {
47
- importing.add(node.source.value);
48
- },
49
- ExportNamedDeclaration(node) {
50
- if (node.source) importing.add(node.source.value);
51
- },
52
- CallExpression(node) {
53
- if (node.callee.type === "Import") {
54
- const first = node.arguments.at(0);
55
- if (first && first.expression.type === "StringLiteral") importing.add(first.expression.value);
56
- }
57
- },
58
- TsImportEqualsDeclaration(node) {
59
- if (node.moduleRef.type === "TsExternalModuleReference") importing.add(node.moduleRef.expression.value);
60
- }
61
- }, baseVisitor);
62
- } catch (error) {
63
- throw new Error(`Failed to walk the AST of: ${file}`, { cause: error });
64
- }
14
+ (0, estree_walker.walk)(ast.program, { enter(node) {
15
+ if (node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration") importing.add(node.source.value);
16
+ if (node.type === "ExportNamedDeclaration" && node.source) importing.add(node.source.value);
17
+ if (node.type === "ImportExpression" && node.source.type === "Literal") importing.add(node.source.value);
18
+ const importEquals = node;
19
+ if (importEquals.type === "TSImportEqualsDeclaration" && importEquals.moduleReference.type === "TSExternalModuleReference") importing.add(importEquals.moduleReference.expression.value);
20
+ } });
65
21
  return [...importing].map((importee) => {
66
22
  if (importee.startsWith(".")) return (0, path.resolve)((0, path.dirname)(file), importee);
67
23
  const parts = importee.split("/");
@@ -128,10 +84,9 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
128
84
  return;
129
85
  }
130
86
  const module = getPackageName(file);
131
- if (hashes.has(module)) return;
132
87
  const dependency = findDependency(workspace, module, sourceFile);
133
88
  if (dependency) {
134
- if (dependency.type === "package") hashes.set(module, Buffer.from(`${module}:${dependency.version}`, "utf8"));
89
+ if (dependency.type === "package") hashes.set(`${module}@${dependency.version}`, Buffer.from(dependency.treeHash, "hex"));
135
90
  else {
136
91
  const localPackage = workspace.packages[dependency.link];
137
92
  if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
@@ -166,7 +121,7 @@ const findDependency = (workspace, module, source) => {
166
121
  };
167
122
  //#endregion
168
123
  //#region src/package-manager/bun.ts
169
- const bun = async (cwd, lockFile) => {
124
+ const bun = async (cwd, lockFile, lockfileHash) => {
170
125
  const data = parseJsonc(lockFile);
171
126
  const resolvedVersions = {};
172
127
  for (const [key, entry] of Object.entries(data.packages ?? {})) {
@@ -202,11 +157,13 @@ const bun = async (cwd, lockFile) => {
202
157
  const version = resolvedVersions[name] ?? resolvedVersions[`${workspace.name}/${name}`];
203
158
  if (version) dependencies[name] = {
204
159
  type: "package",
205
- version
160
+ version,
161
+ treeHash: lockfileHash
206
162
  };
207
163
  else dependencies[name] = {
208
164
  type: "package",
209
- version: specifier
165
+ version: specifier,
166
+ treeHash: lockfileHash
210
167
  };
211
168
  }
212
169
  importers[path$4] = dependencies;
@@ -269,8 +226,9 @@ const stripJsoncSyntax = (text) => {
269
226
  };
270
227
  //#endregion
271
228
  //#region src/package-manager/pnpm.ts
272
- const pnpm = async (cwd, lockFile) => {
229
+ const pnpm = async (cwd, lockFile, lockfileHash) => {
273
230
  const data = (0, yaml.parse)(lockFile);
231
+ const treeHash = createTreeHasher(data, lockfileHash);
274
232
  const importers = {};
275
233
  for (const [path$2, importee] of Object.entries(data.importers)) {
276
234
  const deps = {
@@ -285,7 +243,8 @@ const pnpm = async (cwd, lockFile) => {
285
243
  };
286
244
  else dependencies[name] = {
287
245
  type: "package",
288
- version: entry.version
246
+ version: entry.version,
247
+ treeHash: treeHash(`${name}@${entry.version}`)
289
248
  };
290
249
  importers[path$2] = dependencies;
291
250
  }
@@ -294,6 +253,41 @@ const pnpm = async (cwd, lockFile) => {
294
253
  packages: await buildPackages(cwd, importers)
295
254
  };
296
255
  };
256
+ const createTreeHasher = (data, lockfileHash) => {
257
+ if (!data.snapshots) return () => lockfileHash;
258
+ const graph = {};
259
+ for (const [key, entry] of Object.entries(data.snapshots)) {
260
+ const deps = {
261
+ ...entry.dependencies,
262
+ ...entry.optionalDependencies
263
+ };
264
+ const children = [];
265
+ for (const [name, version] of Object.entries(deps)) if (!version.startsWith("link:")) children.push(`${name}@${version}`);
266
+ graph[key] = children;
267
+ }
268
+ const hashed = /* @__PURE__ */ new Map();
269
+ return (key) => {
270
+ const cached = hashed.get(key);
271
+ if (cached) return cached;
272
+ const reachable = /* @__PURE__ */ new Set([key]);
273
+ const queue = [key];
274
+ while (queue.length > 0) {
275
+ const current = queue.pop();
276
+ for (const child of graph[current] ?? []) if (!reachable.has(child)) {
277
+ reachable.add(child);
278
+ queue.push(child);
279
+ }
280
+ }
281
+ const hash = (0, crypto.createHash)("sha1");
282
+ for (const entry of Array.from(reachable).toSorted()) {
283
+ hash.update(entry);
284
+ hash.update("\n");
285
+ }
286
+ const digest = hash.digest("hex");
287
+ hashed.set(key, digest);
288
+ return digest;
289
+ };
290
+ };
297
291
  //#endregion
298
292
  //#region src/package-manager/util.ts
299
293
  const parsers = {
@@ -306,9 +300,10 @@ const loadPackageManager = async (search, level = 5) => {
306
300
  const file = (0, path.join)(search, lockFileName);
307
301
  if (await fileExist(file)) {
308
302
  const content = await (0, fs_promises.readFile)(file, "utf8");
303
+ const lockfileHash = (0, crypto.createHash)("sha1").update(content).digest("hex");
309
304
  return {
310
- ...await parser(search, content),
311
- lockfileHash: (0, crypto.createHash)("sha1").update(content).digest("hex")
305
+ ...await parser(search, content, lockfileHash),
306
+ lockfileHash
312
307
  };
313
308
  }
314
309
  }
@@ -348,15 +343,12 @@ const defaultOptions = { extensions: [
348
343
  "mts",
349
344
  "tsx"
350
345
  ] };
351
- const seedHashes = (workspace) => {
352
- return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
353
- };
354
346
  const generateFileHash = async (workspace, file, opts = {}) => {
355
347
  const options = {
356
348
  ...defaultOptions,
357
349
  ...opts
358
350
  };
359
- const hashes = seedHashes(workspace);
351
+ const hashes = /* @__PURE__ */ new Map();
360
352
  const absoluteFile = toAbsolute(file);
361
353
  await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
362
354
  return mergeHashes(hashes);
@@ -366,7 +358,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
366
358
  ...defaultOptions,
367
359
  ...opts
368
360
  };
369
- const hashes = seedHashes(workspace);
361
+ const hashes = /* @__PURE__ */ new Map();
370
362
  const absoluteFolder = toAbsolute(folder);
371
363
  const files = await (0, fs_promises.readdir)(absoluteFolder, {
372
364
  recursive: true,
package/dist/index.d.cts CHANGED
@@ -2,6 +2,7 @@
2
2
  type PackageDependency = {
3
3
  type: 'package';
4
4
  version: string;
5
+ treeHash: string;
5
6
  };
6
7
  type WorkspaceDependency = {
7
8
  type: 'workspace';
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  type PackageDependency = {
3
3
  type: 'package';
4
4
  version: string;
5
+ treeHash: string;
5
6
  };
6
7
  type WorkspaceDependency = {
7
8
  type: 'workspace';
package/dist/index.js CHANGED
@@ -2,65 +2,21 @@ import { builtinModules } from "node:module";
2
2
  import { lstat, readFile, readdir, stat } from "fs/promises";
3
3
  import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "path";
4
4
  import { createHash } from "crypto";
5
- import { parse } from "@swc/core";
6
- import { simple } from "swc-walk";
7
- import { BaseVisitor } from "swc-walk/baseVisitor";
8
- import { parse as parse$1 } from "yaml";
5
+ import { walk } from "estree-walker";
6
+ import { parseSync } from "oxc-parser";
7
+ import { parse } from "yaml";
9
8
  //#region src/import.ts
10
- var PatchedBaseVisitor = class extends BaseVisitor {
11
- FunctionBody(node, state, callback) {
12
- for (const statement of node.stmts) callback(statement, state);
13
- }
14
- };
15
- const baseVisitor = new PatchedBaseVisitor();
16
- const parseOptions = (file) => {
17
- if (file.endsWith(".tsx")) return {
18
- syntax: "typescript",
19
- tsx: true,
20
- decorators: true
21
- };
22
- if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) return {
23
- syntax: "typescript",
24
- decorators: true
25
- };
26
- return {
27
- syntax: "ecmascript",
28
- jsx: true,
29
- decorators: true
30
- };
31
- };
32
9
  const findImports = async (file, code) => {
33
- let ast;
34
- try {
35
- ast = await parse(code, parseOptions(file));
36
- } catch (error) {
37
- throw new Error(`Failed to parse: ${file}`, { cause: error });
38
- }
10
+ const ast = parseSync(file, code);
11
+ if (ast.errors.length > 0) throw new Error(`Failed to parse: ${file}`, { cause: ast.errors[0] });
39
12
  const importing = /* @__PURE__ */ new Set();
40
- try {
41
- simple(ast, {
42
- ImportDeclaration(node) {
43
- importing.add(node.source.value);
44
- },
45
- ExportAllDeclaration(node) {
46
- importing.add(node.source.value);
47
- },
48
- ExportNamedDeclaration(node) {
49
- if (node.source) importing.add(node.source.value);
50
- },
51
- CallExpression(node) {
52
- if (node.callee.type === "Import") {
53
- const first = node.arguments.at(0);
54
- if (first && first.expression.type === "StringLiteral") importing.add(first.expression.value);
55
- }
56
- },
57
- TsImportEqualsDeclaration(node) {
58
- if (node.moduleRef.type === "TsExternalModuleReference") importing.add(node.moduleRef.expression.value);
59
- }
60
- }, baseVisitor);
61
- } catch (error) {
62
- throw new Error(`Failed to walk the AST of: ${file}`, { cause: error });
63
- }
13
+ walk(ast.program, { enter(node) {
14
+ if (node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration") importing.add(node.source.value);
15
+ if (node.type === "ExportNamedDeclaration" && node.source) importing.add(node.source.value);
16
+ if (node.type === "ImportExpression" && node.source.type === "Literal") importing.add(node.source.value);
17
+ const importEquals = node;
18
+ if (importEquals.type === "TSImportEqualsDeclaration" && importEquals.moduleReference.type === "TSExternalModuleReference") importing.add(importEquals.moduleReference.expression.value);
19
+ } });
64
20
  return [...importing].map((importee) => {
65
21
  if (importee.startsWith(".")) return resolve(dirname(file), importee);
66
22
  const parts = importee.split("/");
@@ -127,10 +83,9 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
127
83
  return;
128
84
  }
129
85
  const module = getPackageName(file);
130
- if (hashes.has(module)) return;
131
86
  const dependency = findDependency(workspace, module, sourceFile);
132
87
  if (dependency) {
133
- if (dependency.type === "package") hashes.set(module, Buffer.from(`${module}:${dependency.version}`, "utf8"));
88
+ if (dependency.type === "package") hashes.set(`${module}@${dependency.version}`, Buffer.from(dependency.treeHash, "hex"));
134
89
  else {
135
90
  const localPackage = workspace.packages[dependency.link];
136
91
  if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
@@ -165,7 +120,7 @@ const findDependency = (workspace, module, source) => {
165
120
  };
166
121
  //#endregion
167
122
  //#region src/package-manager/bun.ts
168
- const bun = async (cwd, lockFile) => {
123
+ const bun = async (cwd, lockFile, lockfileHash) => {
169
124
  const data = parseJsonc(lockFile);
170
125
  const resolvedVersions = {};
171
126
  for (const [key, entry] of Object.entries(data.packages ?? {})) {
@@ -201,11 +156,13 @@ const bun = async (cwd, lockFile) => {
201
156
  const version = resolvedVersions[name] ?? resolvedVersions[`${workspace.name}/${name}`];
202
157
  if (version) dependencies[name] = {
203
158
  type: "package",
204
- version
159
+ version,
160
+ treeHash: lockfileHash
205
161
  };
206
162
  else dependencies[name] = {
207
163
  type: "package",
208
- version: specifier
164
+ version: specifier,
165
+ treeHash: lockfileHash
209
166
  };
210
167
  }
211
168
  importers[path] = dependencies;
@@ -268,8 +225,9 @@ const stripJsoncSyntax = (text) => {
268
225
  };
269
226
  //#endregion
270
227
  //#region src/package-manager/pnpm.ts
271
- const pnpm = async (cwd, lockFile) => {
272
- const data = parse$1(lockFile);
228
+ const pnpm = async (cwd, lockFile, lockfileHash) => {
229
+ const data = parse(lockFile);
230
+ const treeHash = createTreeHasher(data, lockfileHash);
273
231
  const importers = {};
274
232
  for (const [path, importee] of Object.entries(data.importers)) {
275
233
  const deps = {
@@ -284,7 +242,8 @@ const pnpm = async (cwd, lockFile) => {
284
242
  };
285
243
  else dependencies[name] = {
286
244
  type: "package",
287
- version: entry.version
245
+ version: entry.version,
246
+ treeHash: treeHash(`${name}@${entry.version}`)
288
247
  };
289
248
  importers[path] = dependencies;
290
249
  }
@@ -293,6 +252,41 @@ const pnpm = async (cwd, lockFile) => {
293
252
  packages: await buildPackages(cwd, importers)
294
253
  };
295
254
  };
255
+ const createTreeHasher = (data, lockfileHash) => {
256
+ if (!data.snapshots) return () => lockfileHash;
257
+ const graph = {};
258
+ for (const [key, entry] of Object.entries(data.snapshots)) {
259
+ const deps = {
260
+ ...entry.dependencies,
261
+ ...entry.optionalDependencies
262
+ };
263
+ const children = [];
264
+ for (const [name, version] of Object.entries(deps)) if (!version.startsWith("link:")) children.push(`${name}@${version}`);
265
+ graph[key] = children;
266
+ }
267
+ const hashed = /* @__PURE__ */ new Map();
268
+ return (key) => {
269
+ const cached = hashed.get(key);
270
+ if (cached) return cached;
271
+ const reachable = /* @__PURE__ */ new Set([key]);
272
+ const queue = [key];
273
+ while (queue.length > 0) {
274
+ const current = queue.pop();
275
+ for (const child of graph[current] ?? []) if (!reachable.has(child)) {
276
+ reachable.add(child);
277
+ queue.push(child);
278
+ }
279
+ }
280
+ const hash = createHash("sha1");
281
+ for (const entry of Array.from(reachable).toSorted()) {
282
+ hash.update(entry);
283
+ hash.update("\n");
284
+ }
285
+ const digest = hash.digest("hex");
286
+ hashed.set(key, digest);
287
+ return digest;
288
+ };
289
+ };
296
290
  //#endregion
297
291
  //#region src/package-manager/util.ts
298
292
  const parsers = {
@@ -305,9 +299,10 @@ const loadPackageManager = async (search, level = 5) => {
305
299
  const file = join(search, lockFileName);
306
300
  if (await fileExist(file)) {
307
301
  const content = await readFile(file, "utf8");
302
+ const lockfileHash = createHash("sha1").update(content).digest("hex");
308
303
  return {
309
- ...await parser(search, content),
310
- lockfileHash: createHash("sha1").update(content).digest("hex")
304
+ ...await parser(search, content, lockfileHash),
305
+ lockfileHash
311
306
  };
312
307
  }
313
308
  }
@@ -347,15 +342,12 @@ const defaultOptions = { extensions: [
347
342
  "mts",
348
343
  "tsx"
349
344
  ] };
350
- const seedHashes = (workspace) => {
351
- return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
352
- };
353
345
  const generateFileHash = async (workspace, file, opts = {}) => {
354
346
  const options = {
355
347
  ...defaultOptions,
356
348
  ...opts
357
349
  };
358
- const hashes = seedHashes(workspace);
350
+ const hashes = /* @__PURE__ */ new Map();
359
351
  const absoluteFile = toAbsolute(file);
360
352
  await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
361
353
  return mergeHashes(hashes);
@@ -365,7 +357,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
365
357
  ...defaultOptions,
366
358
  ...opts
367
359
  };
368
- const hashes = seedHashes(workspace);
360
+ const hashes = /* @__PURE__ */ new Map();
369
361
  const absoluteFolder = toAbsolute(folder);
370
362
  const files = await readdir(absoluteFolder, {
371
363
  recursive: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/ts-file-cache",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "bugs": {
5
5
  "url": "https://github.com/awsless/awsless/issues"
6
6
  },
@@ -24,8 +24,8 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
- "@swc/core": "1.16.1",
28
- "swc-walk": "3.0.0-rc.0",
27
+ "estree-walker": "3.0.3",
28
+ "oxc-parser": "0.146.0",
29
29
  "yaml": "2.9.0"
30
30
  },
31
31
  "scripts": {