Last active
August 10, 2023 19:41
-
-
Save wraithgar/043997411ee4fc79f9465296a2138197 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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