-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (47 loc) · 1.68 KB
/
index.js
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
50
51
52
53
54
55
56
57
58
59
'use strict'
const get = require('simple-get')
const registryUrl = require('registry-url')
const registryAuth = require('registry-auth-token')
const url = require('url')
const pkg = require('./package.json')
const UA = `packument/${pkg.version} (${pkg.homepage})`
const FULL = 'application/json'
const ABBREVIATED = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
module.exports = factory()
module.exports.factory = factory
function factory (defaults) {
return function (name, opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = null
}
opts = Object.assign({}, defaults, opts)
packument(name, opts, callback)
}
}
function packument (name, opts, callback) {
const scope = name.split('/')[0]
const endpoint = registryUrl(scope)
const auth = registryAuth(endpoint, { recursive: true })
const path = encodeURIComponent(name).replace(/^%40/, '@')
const headers = {
'user-agent': UA,
accept: opts.full ? FULL : ABBREVIATED
}
if (auth) headers.authorization = `${auth.type} ${auth.token}`
if (opts.keepAlive) headers.connection = 'keep-alive'
if (opts.headers) Object.assign(headers, opts.headers)
// eslint-disable-next-line node/no-deprecated-api
get.concat({ url: url.resolve(endpoint, path), headers }, function (err, res, buf) {
if (err) return callback(err)
if (res.statusCode === 404) return callback(new Error('package does not exist'))
if (res.statusCode !== 200) return callback(new Error(`http ${res.statusCode}`))
let packument
try {
packument = JSON.parse(buf)
} catch (err) {
return callback(err)
}
callback(null, packument, res.headers)
})
}