@socketsecurity/lib 5.7.0 → 5.8.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.
@@ -46,20 +46,50 @@ function getPath() {
46
46
  return _path;
47
47
  }
48
48
  const NODE_JS_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs"]);
49
+ const packageJsonPathCache = /* @__PURE__ */ new Map();
50
+ const packageJsonContentCache = /* @__PURE__ */ new Map();
49
51
  function findPackageJson(filePath) {
50
52
  const fs = /* @__PURE__ */ getFs();
51
53
  const path = /* @__PURE__ */ getPath();
52
- let currentDir = path.dirname(path.resolve(filePath));
54
+ const startDir = path.dirname(path.resolve(filePath));
55
+ const cached = packageJsonPathCache.get(startDir);
56
+ if (cached !== void 0) {
57
+ if (cached === null) {
58
+ return void 0;
59
+ }
60
+ if (fs.existsSync(cached)) {
61
+ return cached;
62
+ }
63
+ packageJsonPathCache.delete(startDir);
64
+ }
65
+ let currentDir = startDir;
53
66
  const root = path.parse(currentDir).root;
54
67
  while (currentDir !== root) {
55
68
  const packageJsonPath = path.join(currentDir, "package.json");
56
69
  if (fs.existsSync(packageJsonPath)) {
70
+ packageJsonPathCache.set(startDir, packageJsonPath);
57
71
  return packageJsonPath;
58
72
  }
59
73
  currentDir = path.dirname(currentDir);
60
74
  }
75
+ packageJsonPathCache.set(startDir, null);
61
76
  return void 0;
62
77
  }
78
+ function readPackageJson(packageJsonPath) {
79
+ const fs = /* @__PURE__ */ getFs();
80
+ const cached = packageJsonContentCache.get(packageJsonPath);
81
+ if (cached !== void 0) {
82
+ return cached;
83
+ }
84
+ try {
85
+ const content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
86
+ packageJsonContentCache.set(packageJsonPath, content);
87
+ return content;
88
+ } catch {
89
+ packageJsonContentCache.set(packageJsonPath, null);
90
+ return null;
91
+ }
92
+ }
63
93
  function detectDlxExecutableType(filePath) {
64
94
  const fs = /* @__PURE__ */ getFs();
65
95
  const path = /* @__PURE__ */ getPath();
@@ -88,20 +118,16 @@ function detectExecutableType(filePath) {
88
118
  return detectLocalExecutableType(filePath);
89
119
  }
90
120
  function detectLocalExecutableType(filePath) {
91
- const fs = /* @__PURE__ */ getFs();
92
121
  const packageJsonPath = findPackageJson(filePath);
93
122
  if (packageJsonPath !== void 0) {
94
- try {
95
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
96
- if (packageJson.bin) {
97
- return {
98
- type: "package",
99
- method: "package-json",
100
- packageJsonPath,
101
- inDlxCache: false
102
- };
103
- }
104
- } catch {
123
+ const packageJson = readPackageJson(packageJsonPath);
124
+ if (packageJson?.bin) {
125
+ return {
126
+ type: "package",
127
+ method: "package-json",
128
+ packageJsonPath,
129
+ inDlxCache: false
130
+ };
105
131
  }
106
132
  }
107
133
  if (isJsFilePath(filePath)) {
@@ -234,7 +234,7 @@ function findBinaryPath(packageDir, packageName, binaryName) {
234
234
  });
235
235
  binPath = binObj[binName];
236
236
  } catch {
237
- const lastSegment = packageName.split("/").pop();
237
+ const lastSegment = packageName.split("/").pop() ?? packageName;
238
238
  const candidates = [
239
239
  binaryName,
240
240
  lastSegment,
@@ -314,15 +314,24 @@ function parsePackageSpec(spec) {
314
314
  };
315
315
  }
316
316
  }
317
+ const binaryPathCache = /* @__PURE__ */ new Map();
317
318
  function resolveBinaryPath(basePath) {
318
319
  if (!import_platform.WIN32) {
319
320
  return basePath;
320
321
  }
321
322
  const fs = /* @__PURE__ */ getFs();
323
+ const cached = binaryPathCache.get(basePath);
324
+ if (cached) {
325
+ if (fs.existsSync(cached)) {
326
+ return cached;
327
+ }
328
+ binaryPathCache.delete(basePath);
329
+ }
322
330
  const extensions = [".cmd", ".bat", ".ps1", ".exe", ""];
323
331
  for (const ext of extensions) {
324
332
  const testPath = basePath + ext;
325
333
  if (fs.existsSync(testPath)) {
334
+ binaryPathCache.set(basePath, testPath);
326
335
  return testPath;
327
336
  }
328
337
  }