-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrawl.js
254 lines (214 loc) · 6.24 KB
/
crawl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* @import {PackumentResult, Packument} from 'pacote'
*/
/**
* @typedef {'cjs' | 'dual' | 'esm' | 'faux'} Style
* Style.
*/
import fs from 'node:fs/promises'
import process from 'node:process'
import dotenv from 'dotenv'
import {npmHighImpact} from 'npm-high-impact'
import pacote from 'pacote'
dotenv.config()
const token = process.env.NPM_TOKEN
if (!token) {
throw new Error(
'Expected `NPM_TOKEN` in env, please add a `.env` file with it'
)
}
let slice = 0
const size = 20
const now = new Date()
const destination = new URL(
'../data/' +
String(now.getUTCFullYear()).padStart(4, '0') +
'-' +
String(now.getUTCMonth() + 1).padStart(2, '0') +
'-' +
String(now.getUTCDate()).padStart(2, '0') +
'.json',
import.meta.url
)
/** @type {Record<string, Style | undefined>} */
const allResults = {}
console.error('fetching %s packages', npmHighImpact.length)
// eslint-disable-next-line no-constant-condition
while (true) {
const names = npmHighImpact.slice(slice * size, (slice + 1) * size)
if (names.length === 0) {
break
}
console.error(
'fetching page: %s, collected total: %s out of %s',
slice,
slice * size,
npmHighImpact.length
)
const promises = names.map(async function (name) {
/** @type {Packument & PackumentResult} */
let result
try {
result = await pacote.packument(name, {
fullMetadata: true,
preferOffline: true,
token
})
} catch (error) {
console.error('package w/ error: %s, likely spam: %s', name, error)
/** @type {[string, Style | undefined]} */
const info = [name, undefined]
return info
}
/** @type {[string, Style | undefined]} */
const info = [name, analyzePackument(result)]
return info
})
/** @type {Array<[string, Style | undefined]>} */
let results
try {
results = await Promise.all(promises)
} catch (error) {
console.error(error)
console.error('sleeping for 10s…')
await sleep(10 * 1000)
continue
}
for (const [name, style] of results) {
allResults[name] = style
console.error(' add: %s (%s)', name, style)
}
// Intermediate writes to help debugging and seeing some results early.
setTimeout(async function () {
await fs.writeFile(
destination,
JSON.stringify(allResults, undefined, 2) + '\n'
)
})
slice++
}
await fs.writeFile(destination, JSON.stringify(allResults, undefined, 2) + '\n')
console.error('done!')
/**
* @param {number} ms
* Miliseconds to sleep.
* @returns {Promise<undefined>}
* Nothing.
*/
function sleep(ms) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(undefined)
}, ms)
})
}
/**
* @param {Packument & PackumentResult} result
* Result.
* @returns {Style | undefined}
* Style.
*/
function analyzePackument(result) {
const latest = (result['dist-tags'] || {}).latest
// Some spam packages were removed. They might still be in the list tho.
if (!latest) {
console.error('package w/o `latest`: %s, likely spam', result.name)
return
}
const packument = (result.versions || {})[latest]
const {exports, main, type} = packument
/** @type {boolean | undefined} */
let cjs
/** @type {boolean | undefined} */
let esm
/** @type {boolean | undefined} */
let fauxEsm
if (packument.module) {
fauxEsm = true
}
// Check exports map.
if (exports && typeof exports === 'object') {
for (const exportId in exports) {
if (Object.hasOwn(exports, exportId) && typeof exportId === 'string') {
// @ts-expect-error: indexing on object is fine.
const value = /** @type {unknown} */ (exports[exportId])
analyzeThing(value, packument.name + '#exports')
}
}
}
// Explicit `commonjs` set, with a explicit `import` or `.mjs` too.
if (esm && type === 'commonjs') {
cjs = true
}
// Explicit `module` set, with explicit `require` or `.cjs` too.
if (cjs && type === 'module') {
esm = true
}
// If there are no explicit exports:
if (cjs === undefined && esm === undefined) {
if (type === 'module' || (main && /\.mjs$/.test(main))) {
esm = true
} else {
cjs = true
}
}
/** @type {Style} */
const style = esm && cjs ? 'dual' : esm ? 'esm' : fauxEsm ? 'faux' : 'cjs'
return style
/**
* @param {unknown} value
* Thing.
* @param {string} path
* Path in `package.json`.
* @returns {undefined}
* Nothing.
*/
function analyzeThing(value, path) {
if (value && typeof value === 'object') {
if (Array.isArray(value)) {
const values = /** @type {Array<unknown>} */ (value)
let index = -1
while (++index < values.length) {
analyzeThing(values[index], path + '[' + index + ']')
}
} else {
// Cast as indexing on object is fine.
const record = /** @type {Record<string, unknown>} */ (value)
let dots = false
for (const [key, subvalue] of Object.entries(record)) {
if (key.charAt(0) !== '.') break
analyzeThing(subvalue, path + '["' + key + '"]')
dots = true
}
if (dots) return
let explicit = false
const conditionImport = Boolean('import' in record && record.import)
const conditionRequire = Boolean('require' in record && record.require)
const conditionDefault = Boolean('default' in record && record.default)
if (conditionImport || conditionRequire) {
explicit = true
}
if (conditionImport || (conditionRequire && conditionDefault)) {
esm = true
}
if (conditionRequire || (conditionImport && conditionDefault)) {
cjs = true
}
const defaults = record.node || record.default
if (typeof defaults === 'string' && !explicit) {
if (/\.mjs$/.test(defaults)) esm = true
if (/\.cjs$/.test(defaults)) cjs = true
}
}
} else if (typeof value === 'string') {
if (/\.mjs$/.test(value)) esm = true
if (/\.cjs$/.test(value)) cjs = true
} else if (value === null) {
// Something explicitly not available,
// for a particular condition,
// or before a glob which would allow it.
} else {
console.error('unknown:', [value], path)
}
}
}