Skip to content

Instantly share code, notes, and snippets.

@wraithgar
Last active August 10, 2023 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wraithgar/043997411ee4fc79f9465296a2138197 to your computer and use it in GitHub Desktop.
Save wraithgar/043997411ee4fc79f9465296a2138197 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const https = require('node:https')
const es = require('event-stream')
const JSONStream = require('JSONStream')
const main = async () => {
const r = []
for (let i = 0; i < 10; i++) {
r.push(new Promise((resolve, reject) => {
if (process.argv[2] === 'stream') {
const req = https.request(new URL('https://registry.npmjs.org/npm'), function (res) {
res
.pipe(JSONStream.parse('dist-tags'))
.pipe(es.mapSync(function (data) {
console.log(data.latest)
resolve(data)
res.destroy()
}))
})
req.on('error', (err) => {
reject(err)
})
req.end()
} else {
let raw = ''
const req = https.request(new URL('https://registry.npmjs.org/npm'), function (res) {
res.on('data', d => {
raw = raw + d.toString('utf8')
})
res.on('end', () => {
const p = JSON.parse(raw)
console.log(p['dist-tags'].latest)
resolve(p['dist-tags'])
})
})
req.on('error', (err) => {
reject(err)
})
req.end()
}
}))
}
await Promise.all(r)
}
main()
.catch(err => {
console.error(err)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment