node-modules-tools 1.3.2 → 1.4.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/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  <p align="center">
12
12
  <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
13
- <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
13
+ <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg' alt='Sponsors' />
14
14
  </a>
15
15
  </p>
16
16
 
@@ -1,3 +1,3 @@
1
- export { C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, i as PackageModuleTypes } from './shared/node-modules-tools.DH68osY8.mjs';
1
+ export { C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, i as PackageModuleTypes } from './shared/node-modules-tools.BWZx79LE.mjs';
2
2
  import 'pkg-types';
3
3
  import 'publint';
@@ -1,3 +1,3 @@
1
- export { C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, i as PackageModuleTypes } from './shared/node-modules-tools.DH68osY8.js';
1
+ export { C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, i as PackageModuleTypes } from './shared/node-modules-tools.BWZx79LE.js';
2
2
  import 'pkg-types';
3
3
  import 'publint';
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AgentName } from 'package-manager-detector';
2
- import { P as PackageNodeRaw, a as PackageNodeBase, b as PackageNode, c as PackageModuleType } from './shared/node-modules-tools.DH68osY8.mjs';
3
- export { C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, F as FileCategory, N as NpmMeta, f as NpmMetaLatest, g as PackageInstallSizeInfo, h as PackageModuleTypeSimple, i as PackageModuleTypes } from './shared/node-modules-tools.DH68osY8.mjs';
2
+ import { P as PackageNodeRaw, a as PackageNodeBase, b as PackageNode, c as PackageModuleType } from './shared/node-modules-tools.BWZx79LE.mjs';
3
+ export { A as AuditLevelString, C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, F as FileCategory, N as NpmMeta, f as NpmMetaLatest, g as PackageInstallSizeInfo, h as PackageModuleTypeSimple, i as PackageModuleTypes } from './shared/node-modules-tools.BWZx79LE.mjs';
4
4
  import { PackageJson } from 'pkg-types';
5
5
  export { PackageJson } from 'pkg-types';
6
6
  export { NormalizedFunding, PackageNodeLike, ParsedFunding, ParsedLicense, constructPackageFilter, constructPackageFilters, normalizePkgAuthors, normalizePkgFundings, normalizePkgLicense, normalizePkgRepository, parseAuthor, parseFunding } from './utils.mjs';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AgentName } from 'package-manager-detector';
2
- import { P as PackageNodeRaw, a as PackageNodeBase, b as PackageNode, c as PackageModuleType } from './shared/node-modules-tools.DH68osY8.js';
3
- export { C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, F as FileCategory, N as NpmMeta, f as NpmMetaLatest, g as PackageInstallSizeInfo, h as PackageModuleTypeSimple, i as PackageModuleTypes } from './shared/node-modules-tools.DH68osY8.js';
2
+ import { P as PackageNodeRaw, a as PackageNodeBase, b as PackageNode, c as PackageModuleType } from './shared/node-modules-tools.BWZx79LE.js';
3
+ export { A as AuditLevelString, C as CLUSTER_DEP_DEV, d as CLUSTER_DEP_OPTIONAL, e as CLUSTER_DEP_PROD, F as FileCategory, N as NpmMeta, f as NpmMetaLatest, g as PackageInstallSizeInfo, h as PackageModuleTypeSimple, i as PackageModuleTypes } from './shared/node-modules-tools.BWZx79LE.js';
4
4
  import { PackageJson } from 'pkg-types';
5
5
  export { PackageJson } from 'pkg-types';
6
6
  export { NormalizedFunding, PackageNodeLike, ParsedFunding, ParsedLicense, constructPackageFilter, constructPackageFilters, normalizePkgAuthors, normalizePkgFundings, normalizePkgLicense, normalizePkgRepository, parseAuthor, parseFunding } from './utils.js';
package/dist/index.mjs CHANGED
@@ -106,96 +106,81 @@ function populateRawResult(input) {
106
106
  return result;
107
107
  }
108
108
 
109
- function analyzePackageModuleType(pkgJson) {
110
- if (pkgJson.name?.startsWith("@types/"))
111
- return "dts";
112
- const { exports: exports$1, main, type } = pkgJson;
113
- let cjs;
114
- let esm;
115
- let fauxEsm;
116
- if (pkgJson.module) {
117
- fauxEsm = true;
118
- }
119
- if (exports$1) {
120
- analyzeThing(exports$1, `${pkgJson.name}#exports`);
109
+ function analyzeExports(exports$1, depth = 0) {
110
+ const result = {
111
+ hasImport: false,
112
+ hasRequire: false,
113
+ hasModule: false
114
+ };
115
+ if (depth > 10)
116
+ return result;
117
+ if (exports$1 == null)
118
+ return result;
119
+ if (typeof exports$1 === "string") {
120
+ if (exports$1.endsWith(".mjs") || exports$1.endsWith(".mts"))
121
+ result.hasImport = true;
122
+ else if (exports$1.endsWith(".cjs") || exports$1.endsWith(".cts"))
123
+ result.hasRequire = true;
124
+ return result;
121
125
  }
122
- if (esm && type === "commonjs") {
123
- cjs = true;
126
+ if (Array.isArray(exports$1)) {
127
+ for (const item of exports$1)
128
+ mergeAnalysis(result, analyzeExports(item, depth + 1));
129
+ return result;
124
130
  }
125
- if (cjs && type === "module") {
126
- esm = true;
131
+ if (typeof exports$1 === "object") {
132
+ for (const [key, value] of Object.entries(exports$1)) {
133
+ if (key === "import")
134
+ result.hasImport = true;
135
+ else if (key === "require")
136
+ result.hasRequire = true;
137
+ else if (key === "module")
138
+ result.hasModule = true;
139
+ mergeAnalysis(result, analyzeExports(value, depth + 1));
140
+ }
127
141
  }
128
- if (cjs === void 0 && esm === void 0) {
129
- if (type === "module" || main && /\.mjs$/.test(main)) {
130
- esm = true;
131
- } else if (main) {
132
- cjs = true;
142
+ return result;
143
+ }
144
+ function mergeAnalysis(target, source) {
145
+ target.hasImport = target.hasImport || source.hasImport;
146
+ target.hasRequire = target.hasRequire || source.hasRequire;
147
+ target.hasModule = target.hasModule || source.hasModule;
148
+ }
149
+ function analyzePackageModuleType(pkgJson) {
150
+ if (pkgJson.name?.startsWith("@types/"))
151
+ return "dts";
152
+ const hasExports = pkgJson.exports != null;
153
+ const hasModule = !!pkgJson.module;
154
+ const hasMain = !!pkgJson.main;
155
+ const isTypeModule = pkgJson.type === "module";
156
+ if (hasExports && pkgJson.exports) {
157
+ const exportInfo = analyzeExports(pkgJson.exports);
158
+ if (exportInfo.hasImport && exportInfo.hasRequire)
159
+ return "dual";
160
+ if (exportInfo.hasImport || exportInfo.hasModule) {
161
+ if (hasMain && !isTypeModule)
162
+ return "dual";
163
+ return "esm";
164
+ }
165
+ if (exportInfo.hasRequire) {
166
+ if (hasModule)
167
+ return "dual";
168
+ return "cjs";
133
169
  }
134
170
  }
135
- if (esm && cjs)
136
- return "dual";
137
- if (esm)
138
- return "esm";
139
- if (fauxEsm)
171
+ if (hasModule && hasMain) {
172
+ const mainIsCJS = pkgJson.main?.endsWith(".cjs") || pkgJson.main?.endsWith(".js") && !isTypeModule;
173
+ return mainIsCJS ? "faux" : "esm";
174
+ }
175
+ if (hasModule)
140
176
  return "faux";
141
- if (!esm && !cjs && !pkgJson.main && !pkgJson.exports && pkgJson.types)
177
+ if (isTypeModule || hasMain && pkgJson.main?.endsWith(".mjs"))
178
+ return "esm";
179
+ if (hasMain)
180
+ return "cjs";
181
+ if (pkgJson.types || pkgJson.typings)
142
182
  return "dts";
143
183
  return "cjs";
144
- function analyzeThing(value, path) {
145
- if (value && typeof value === "object") {
146
- if (Array.isArray(value)) {
147
- const values = (
148
- /** @type {Array<unknown>} */
149
- value
150
- );
151
- let index = -1;
152
- while (++index < values.length) {
153
- analyzeThing(values[index], `${path}[${index}]`);
154
- }
155
- } else {
156
- const record = (
157
- /** @type {Record<string, unknown>} */
158
- value
159
- );
160
- let dots = false;
161
- for (const [key, subvalue] of Object.entries(record)) {
162
- if (key.charAt(0) !== ".")
163
- break;
164
- analyzeThing(subvalue, `${path}["${key}"]`);
165
- dots = true;
166
- }
167
- if (dots)
168
- return;
169
- let explicit = false;
170
- const conditionImport = Boolean("import" in record && record.import);
171
- const conditionRequire = Boolean("require" in record && record.require);
172
- const conditionDefault = Boolean("default" in record && record.default);
173
- if (conditionImport || conditionRequire) {
174
- explicit = true;
175
- }
176
- if (conditionImport || conditionRequire && conditionDefault) {
177
- esm = true;
178
- }
179
- if (conditionRequire || conditionImport && conditionDefault) {
180
- cjs = true;
181
- }
182
- const defaults = record.node || record.default;
183
- if (typeof defaults === "string" && !explicit) {
184
- if (/\.mjs$/.test(defaults))
185
- esm = true;
186
- if (/\.cjs$/.test(defaults))
187
- cjs = true;
188
- }
189
- }
190
- } else if (typeof value === "string") {
191
- if (/\.mjs$/.test(value))
192
- esm = true;
193
- if (/\.cjs$/.test(value))
194
- cjs = true;
195
- } else if (value === null) ; else {
196
- console.error("unknown:", [value], path);
197
- }
198
- }
199
184
  }
200
185
 
201
186
  async function getPackageInstallSize(pkg) {
@@ -63,10 +63,16 @@ interface PackageNode extends PackageNodeBase {
63
63
  publint?: Message[] | null;
64
64
  };
65
65
  }
66
+ type AuditLevelString = 'low' | 'moderate' | 'high' | 'critical';
66
67
  interface NpmMeta {
67
68
  publishedAt: number;
68
69
  deprecated?: string;
69
70
  provenance?: 'trustedPublisher' | boolean;
71
+ vulnerability?: {
72
+ level: AuditLevelString;
73
+ title: string;
74
+ url?: string;
75
+ };
70
76
  }
71
77
  /**
72
78
  * Npm meta of the latest version of a certain package
@@ -92,4 +98,4 @@ declare const CLUSTER_DEP_DEV = "dep:dev";
92
98
  declare const CLUSTER_DEP_OPTIONAL = "dep:optional";
93
99
 
94
100
  export { CLUSTER_DEP_DEV as C, CLUSTER_DEP_OPTIONAL as d, CLUSTER_DEP_PROD as e, PackageModuleTypes as i };
95
- export type { FileCategory as F, NpmMeta as N, PackageNodeRaw as P, PackageNodeBase as a, PackageNode as b, PackageModuleType as c, NpmMetaLatest as f, PackageInstallSizeInfo as g, PackageModuleTypeSimple as h };
101
+ export type { AuditLevelString as A, FileCategory as F, NpmMeta as N, PackageNodeRaw as P, PackageNodeBase as a, PackageNode as b, PackageModuleType as c, NpmMetaLatest as f, PackageInstallSizeInfo as g, PackageModuleTypeSimple as h };
@@ -63,10 +63,16 @@ interface PackageNode extends PackageNodeBase {
63
63
  publint?: Message[] | null;
64
64
  };
65
65
  }
66
+ type AuditLevelString = 'low' | 'moderate' | 'high' | 'critical';
66
67
  interface NpmMeta {
67
68
  publishedAt: number;
68
69
  deprecated?: string;
69
70
  provenance?: 'trustedPublisher' | boolean;
71
+ vulnerability?: {
72
+ level: AuditLevelString;
73
+ title: string;
74
+ url?: string;
75
+ };
70
76
  }
71
77
  /**
72
78
  * Npm meta of the latest version of a certain package
@@ -92,4 +98,4 @@ declare const CLUSTER_DEP_DEV = "dep:dev";
92
98
  declare const CLUSTER_DEP_OPTIONAL = "dep:optional";
93
99
 
94
100
  export { CLUSTER_DEP_DEV as C, CLUSTER_DEP_OPTIONAL as d, CLUSTER_DEP_PROD as e, PackageModuleTypes as i };
95
- export type { FileCategory as F, NpmMeta as N, PackageNodeRaw as P, PackageNodeBase as a, PackageNode as b, PackageModuleType as c, NpmMetaLatest as f, PackageInstallSizeInfo as g, PackageModuleTypeSimple as h };
101
+ export type { AuditLevelString as A, FileCategory as F, NpmMeta as N, PackageNodeRaw as P, PackageNodeBase as a, PackageNode as b, PackageModuleType as c, NpmMetaLatest as f, PackageInstallSizeInfo as g, PackageModuleTypeSimple as h };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-modules-tools",
3
3
  "type": "module",
4
- "version": "1.3.2",
4
+ "version": "1.4.1",
5
5
  "description": "Tools for inspecting node_modules",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -32,12 +32,12 @@
32
32
  "package-manager-detector": "^1.6.0",
33
33
  "pathe": "^2.0.3",
34
34
  "pkg-types": "^2.3.0",
35
- "publint": "^0.3.17",
35
+ "publint": "^0.3.18",
36
36
  "semver": "^7.7.4",
37
37
  "tinyexec": "^1.0.2"
38
38
  },
39
39
  "devDependencies": {
40
- "@pnpm/list": "^1000.2.5",
40
+ "@pnpm/list": "^1000.3.2",
41
41
  "@pnpm/types": "^1001.3.0",
42
42
  "@types/js-yaml": "^4.0.9",
43
43
  "@types/stream-json": "^1.7.8",