-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b99dcc
commit 97f71e0
Showing
9 changed files
with
3,787 additions
and
3,061 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
.nyc_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.nyc_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const dependants = require('.') | ||
|
||
const main = async () => { | ||
for await (const dependant of dependants('express')) { | ||
console.log(dependant) | ||
} | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,34 @@ | ||
const JSONStream = require('JSONStream') | ||
const http = require('http') | ||
const qs = require('querystring') | ||
const { Transform } = require('stream') | ||
const fetch = require('node-fetch') | ||
const cheerio = require('cheerio') | ||
|
||
const dependants = (name, opts = {}) => { | ||
var registry = opts.registry || 'http://registry.npmjs.org' | ||
var query = qs.stringify({ | ||
group_level: 2, | ||
startkey: '["' + name + '"]', | ||
endkey: '["' + name + '",{}]' | ||
}) | ||
var url = registry + '/-/_view/dependedUpon?' + query | ||
module.exports = name => { | ||
const dependants = [] | ||
let offset = 0 | ||
|
||
var out = Transform({ objectMode: true }) | ||
out._transform = function (row, _, done) { | ||
done(null, row.key[1]) | ||
} | ||
out.destroy = function () { | ||
req.abort() | ||
parse.destroy() | ||
process.nextTick(function () { | ||
out.emit('close') | ||
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) dependants.push(dependant) | ||
}) | ||
offset += 36 | ||
} | ||
|
||
var req = http | ||
.get(url, function (res) { | ||
if (res.statusCode !== 200) { | ||
return parse.emit('error', new Error('bad status: ' + res.status)) | ||
return { | ||
[Symbol.asyncIterator]: async function * () { | ||
while (true) { | ||
if (dependants.length) { | ||
yield await dependants.shift() | ||
} else { | ||
await next() | ||
if (!dependants.length) return | ||
} | ||
} | ||
|
||
res.pipe(parse) | ||
}) | ||
.on('error', out.emit.bind(out, 'error')) | ||
|
||
var parse = JSONStream.parse(['rows', true]).on( | ||
'error', | ||
out.emit.bind(out, 'error') | ||
) | ||
|
||
return parse.pipe(out) | ||
} | ||
} | ||
} | ||
|
||
module.exports = dependants |
Oops, something went wrong.