-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
38 lines (35 loc) · 939 Bytes
/
index.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
const fetch = require('node-fetch')
const cheerio = require('cheerio')
module.exports = name => {
const dependants = []
const visited = new Set()
let offset = 0
const next = async () => {
const url = `https://npmjs.com/browse/depended/${name}?offset=${offset}`
const res = await fetch(url)
const html = await res.text()
const $ = cheerio.load(html)
$('a[href^="/package/"]').each((_, el) => {
const dependant = $(el)
.attr('href')
.slice('/package/'.length)
if (dependant !== name && !visited.has(dependant)) {
dependants.push(dependant)
visited.add(dependant)
}
})
offset += 36
}
return {
[Symbol.asyncIterator]: async function * () {
while (true) {
if (dependants.length) {
yield await dependants.shift()
} else {
await next()
if (!dependants.length) return
}
}
}
}
}