Skip to content

Commit e5b5533

Browse files
committed
Add a function to test whether an URL points to a missing file
This function will come in real handy when we want to figure out whether a given package has been deployed yet. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 1dce392 commit e5b5533

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

GitForWindowsHelper/https-request.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ const httpsRequest = async (context, hostname, method, requestPath, body, header
6161
})
6262
}
6363

64+
const doesURLReturn404 = async url => {
65+
const match = url.match(/^https:\/\/([^/]+?)(:\d+)?(\/.*)?$/)
66+
if (!match) throw new Error(`Could not parse URL ${url}`)
67+
68+
const https = require('https')
69+
const options = {
70+
method: 'HEAD',
71+
host: match[1],
72+
port: Number.parseInt(match[2] || '443'),
73+
path: match[3] || '/'
74+
}
75+
return new Promise((resolve, reject) => {
76+
https.request(options, res => {
77+
if (res.error) reject(res.error)
78+
else if (res.statusCode === 404) resolve(true)
79+
else if (res.statusCode === 200) resolve(false)
80+
else reject(`Unexpected statusCode: ${res.statusCode}`)
81+
}).end()
82+
})
83+
}
84+
6485
module.exports = {
65-
httpsRequest
86+
httpsRequest,
87+
doesURLReturn404
6688
}

0 commit comments

Comments
 (0)