Skip to content

Commit

Permalink
new api, new name
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Dec 23, 2019
1 parent 3b99dcc commit 97f71e0
Show file tree
Hide file tree
Showing 9 changed files with 3,787 additions and 3,061 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.nyc_output
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.nyc_output
33 changes: 12 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
# dependants-stream
# npm-dependants

Get a stream of node modules depending on a given module.
Get dependants of a module on npm.

[![build status](https://secure.travis-ci.org/juliangruber/dependants-stream.png)](http://travis-ci.org/juliangruber/dependants-stream)

## Example
## Usage

```js
var dependants = require('dependants-stream')

dependants('intersect')
.on('data', function (name) {
console.log(name)
})
.on('end', function () {
// ...
})
const dependants = require('npm-dependants')

for await (const dependant of dependants('express')) {
console.log(dependant)
// webpack-dev-server
// webpack-bundle-analyzer
// ...
}
```

## API

### dependants(name[, opts])

Create a readable stream emitting names of modules depending on module `name`. Overwrite the default registry url with `opts.registry`. The stream can be `destroy`ed.

## Installation

```bash
$ npm install dependants-stream
$ npm install npm-dependants
```

## License
Expand Down
9 changes: 9 additions & 0 deletions example.js
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()
65 changes: 27 additions & 38 deletions index.js
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
Loading

0 comments on commit 97f71e0

Please sign in to comment.