Skip to content

Commit 453e164

Browse files
trivikrRafaelGSS
andauthored
chore: replace undici with native https.request (#26)
* chore: remove debug dependency (#25) Part of: #19 * Use response.pipe instead of stream.pipeline * Add error handler for fetchCoreIndex and call end() * Remove variable req in getCoreIndex() --------- Co-authored-by: Rafael Gonzaga <[email protected]>
1 parent 7a6c304 commit 453e164

File tree

3 files changed

+49
-57
lines changed

3 files changed

+49
-57
lines changed

is-vulnerable.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
const { danger, allGood, bold, vulnerableWarning, separator } = require('./ascii')
2-
const { request, stream, setGlobalDispatcher, Agent } = require('undici')
3-
const EE = require('events')
2+
const { request } = require('https')
43
const fs = require('fs')
54
const path = require('path')
65
const satisfies = require('semver/functions/satisfies')
76
const nv = require('@pkgjs/nv')
87

9-
setGlobalDispatcher(new Agent({ connections: 20 }))
10-
118
const CORE_RAW_URL = 'https://raw.githubusercontent.com/nodejs/security-wg/main/vuln/core/index.json'
129

1310
let lastETagValue
@@ -38,28 +35,57 @@ function updateLastETag (etag) {
3835
}
3936

4037
async function fetchCoreIndex () {
41-
const abortRequest = new EE()
42-
await stream(CORE_RAW_URL, { signal: abortRequest }, ({ statusCode }) => {
43-
if (statusCode !== 200) {
44-
console.error('Request to Github failed. Aborting...')
45-
abortRequest.emit('abort')
38+
await new Promise((resolve) => {
39+
request(CORE_RAW_URL, (res) => {
40+
if (res.statusCode !== 200) {
41+
console.error(`Request to Github returned http status ${res.statusCode}. Aborting...`)
42+
process.nextTick(() => { process.exit(1) })
43+
}
44+
45+
const fileStream = fs.createWriteStream(coreLocalFile)
46+
res.pipe(fileStream)
47+
48+
fileStream.on('finish', () => {
49+
fileStream.close()
50+
resolve()
51+
})
52+
53+
fileStream.on('error', (err) => {
54+
console.error(`Error ${err.message} while writing to '${coreLocalFile}'. Aborting...`)
55+
process.nextTick(() => { process.exit(1) })
56+
})
57+
}).on('error', (err) => {
58+
console.error(`Request to Github returned error ${err.message}. Aborting...`)
4659
process.nextTick(() => { process.exit(1) })
47-
}
48-
return fs.createWriteStream(coreLocalFile, { flags: 'w', autoClose: true })
60+
}).end()
4961
})
5062
return readLocal(coreLocalFile)
5163
}
5264

5365
async function getCoreIndex () {
54-
const { headers } = await request(CORE_RAW_URL, { method: 'HEAD' })
55-
if (!lastETagValue || lastETagValue !== headers.etag || !fs.existsSync(coreLocalFile)) {
56-
updateLastETag(headers.etag)
57-
debug('Creating local core.json')
58-
return fetchCoreIndex()
59-
} else {
60-
debug(`No updates from upstream. Getting a cached version: ${coreLocalFile}`)
61-
return readLocal(coreLocalFile)
62-
}
66+
return new Promise((resolve) => {
67+
request(CORE_RAW_URL, { method: 'HEAD' }, (res) => {
68+
if (res.statusCode !== 200) {
69+
console.error(`Request to Github returned http status ${res.statusCode}. Aborting...`)
70+
process.nextTick(() => { process.exit(1) })
71+
}
72+
73+
res.on('data', () => {})
74+
75+
const { etag } = res.headers
76+
if (!lastETagValue || lastETagValue !== etag || !fs.existsSync(coreLocalFile)) {
77+
updateLastETag(etag)
78+
debug('Creating local core.json')
79+
resolve(fetchCoreIndex())
80+
} else {
81+
debug(`No updates from upstream. Getting a cached version: ${coreLocalFile}`)
82+
resolve(readLocal(coreLocalFile))
83+
}
84+
}).on('error', (err) => {
85+
console.error(`Request to Github returned error ${err.message}. Aborting...`)
86+
process.nextTick(() => { process.exit(1) })
87+
}).end()
88+
})
6389
}
6490

6591
const checkPlatform = platform => {

package-lock.json

Lines changed: 2 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
"dependencies": {
3131
"@actions/core": "^1.10.0",
3232
"@pkgjs/nv": "^0.2.1",
33-
"semver": "^7.3.8",
34-
"undici": "^5.15.1"
33+
"semver": "^7.3.8"
3534
},
3635
"devDependencies": {
3736
"standard": "^17.0.0",

0 commit comments

Comments
 (0)