1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
const { join, basename } = require('path')
const { existsSync, readFileSync } = require('fs')
const http = require('http')
const PORT = 12345 + (+process.env.TAP_CHILD_ID || 0)
let server = null
const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
const start = () => new Promise((resolve) => {
server = http.createServer((req, res) => {
res.setHeader('connection', 'close')
const f = join(__dirname, join('/', req.url.replace(/@/, '').replace(/%2f/i, '/')))
// a magic package that causes us to return an error that will be logged
if (basename(f) === 'fail_reflect_user_agent') {
res.statusCode = 404
res.setHeader('npm-notice', req.headers['user-agent'])
return res.end()
}
const isCorgi = req.headers.accept.includes('application/vnd.npm.install-v1+json')
const file = f + (
isCorgi && existsSync(`${f}.min.json`) ? '.min.json'
: existsSync(`${f}.json`) ? '.json'
: existsSync(`${f}/index.json`) ? 'index.json'
: ''
)
try {
const body = readFileSync(file)
res.setHeader('content-length', body.length)
res.setHeader('content-type', /\.min\.json$/.test(file) ? corgiDoc
: /\.json$/.test(file) ? 'application/json'
: 'application/octet-stream')
res.end(body)
} catch {
res.statusCode = 500
res.setHeader('content-type', 'text/plain')
res.end('bad')
}
}).listen(PORT, resolve)
})
module.exports = {
start,
stop: () => server.close(),
registry: `http://localhost:${PORT}/`,
}
|