Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace undici with native https.request #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const { request, stream, setGlobalDispatcher, Agent } = require('undici')
const EE = require('events')
const { request } = require('https')
const { pipeline } = require('stream')
const fs = require('fs')
const path = require('path')
const os = require('os')
Expand All @@ -10,8 +10,6 @@ const satisfies = require('semver/functions/satisfies')
const { danger, vulnerableWarning, bold, separator, allGood } = require('./ascii')
const nv = require('@pkgjs/nv')

setGlobalDispatcher(new Agent({ connections: 20 }))

const CORE_RAW_URL = 'https://raw.githubusercontent.com/nodejs/security-wg/main/vuln/core/index.json'

let lastETagValue
Expand All @@ -36,28 +34,55 @@ function updateLastETag (etag) {
}

async function fetchCoreIndex () {
const abortRequest = new EE()
await stream(CORE_RAW_URL, { signal: abortRequest }, ({ statusCode }) => {
if (statusCode !== 200) {
console.error('Request to Github failed. Aborting...')
abortRequest.emit('abort')
process.nextTick(() => { process.exit(1) })
}
return fs.createWriteStream(coreLocalFile, { flags: 'w', autoClose: true })
await new Promise((resolve) => {
request(CORE_RAW_URL, (res) => {
if (res.statusCode !== 200) {
console.error('Request to Github failed. Aborting...')
process.nextTick(() => { process.exit(1) })
}

const file = fs.createWriteStream(coreLocalFile)
pipeline(res, file, (err) => {
if (err) {
console.error(`Problem with request: ${err.message}`)
process.nextTick(() => { process.exit(1) })
} else {
resolve()
}
})
})
})
return readLocal(coreLocalFile)
}

async function getCoreIndex () {
const { headers } = await request(CORE_RAW_URL, { method: 'HEAD' })
if (!lastETagValue || lastETagValue !== headers.etag || !fs.existsSync(coreLocalFile)) {
updateLastETag(headers.etag)
debug('Creating local core.json')
return fetchCoreIndex()
} else {
debug(`No updates from upstream. Getting a cached version: ${coreLocalFile}`)
return readLocal(coreLocalFile)
}
return new Promise((resolve) => {
const req = request(CORE_RAW_URL, { method: 'HEAD' }, (res) => {
if (res.statusCode !== 200) {
console.error('Request to Github failed. Aborting...')
process.nextTick(() => { process.exit(1) })
}

res.on('data', () => {})

const { etag } = res.headers
if (!lastETagValue || lastETagValue !== etag || !fs.existsSync(coreLocalFile)) {
updateLastETag(etag)
debug('Creating local core.json')
resolve(fetchCoreIndex())
} else {
debug(`No updates from upstream. Getting a cached version: ${coreLocalFile}`)
resolve(readLocal(coreLocalFile))
}
})

req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`)
process.nextTick(() => { process.exit(1) })
})

req.end()
})
}

const checkPlatform = platform => {
Expand Down
33 changes: 1 addition & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"@pkgjs/nv": "^0.2.1",
"cli-color": "^2.0.3",
"debug": "^4.3.4",
"semver": "^7.3.8",
"undici": "^5.15.1"
"semver": "^7.3.8"
},
"devDependencies": {
"standard": "^17.0.0",
Expand Down