This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
publish.js
151 lines (127 loc) · 4.21 KB
/
publish.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module.exports = publish
var path = require("path")
, url = require("url")
, semver = require("semver")
, crypto = require("crypto")
, fs = require("fs")
function publish (data, tarball, cb) {
var email = this.conf.get('email')
var auth = this.conf.get('_auth')
var username = this.conf.get('username')
if (!email || !auth || !username) {
var er = new Error("auth and email required for publishing")
er.code = 'ENEEDAUTH'
return cb(er)
}
if (data.name !== encodeURIComponent(data.name).toLowerCase())
return cb(new Error('invalid name: must be lowercase and url-safe'))
var ver = semver.clean(data.version)
if (!ver)
return cb(new Error('invalid semver: ' + data.version))
data.version = ver
var self = this
fs.stat(tarball, function(er, s) {
if (er) return cb(er)
fs.readFile(tarball, 'base64', function(er, tardata) {
if (er) return cb(er)
putFirst.call(self, data, tardata, s, username, email, cb)
})
})
}
function putFirst (data, tardata, stat, username, email, cb) {
// optimistically try to PUT all in one single atomic thing.
// If 409, then GET and merge, try again.
// If other error, then fail.
var root =
{ _id : data.name
, name : data.name
, description : data.description
, "dist-tags" : {}
, versions : {}
, readme: data.readme || ""
, maintainers :
[ { name : username
, email : email
}
]
}
root.versions[ data.version ] = data
data.maintainers = JSON.parse(JSON.stringify(root.maintainers))
var tag = data.tag || this.conf.get('tag') || "latest"
root["dist-tags"][tag] = data.version
var registry = this.conf.get('registry')
var tbName = data.name + "-" + data.version + ".tgz"
, tbURI = data.name + "/-/" + tbName
data._id = data.name+"@"+data.version
data.dist = data.dist || {}
data.dist.shasum = crypto.createHash("sha1").update(tardata, 'base64').digest("hex")
data.dist.tarball = url.resolve(registry, tbURI)
.replace(/^https:\/\//, "http://")
root._attachments = {}
root._attachments[ tbName ] = {
content_type: 'application/octet-stream',
data: tardata,
length: stat.size
};
this.request("PUT", data.name, root, function (er, parsed, json, res) {
var r409 = "must supply latest _rev to update existing package"
var r409b = "Document update conflict."
var conflict = res && res.statusCode === 409
if (parsed && (parsed.reason === r409 || parsed.reason === r409b))
conflict = true
// a 409 is typical here. GET the data and merge in.
if (er && !conflict) {
this.log.error("publish", "Failed PUT "
+(res && res.statusCode))
return cb(er)
}
if (!er && !conflict)
return cb(er, parsed, json, res)
// let's see what versions are already published.
this.request("GET", data.name, function (er, current) {
if (er)
return cb(er)
putNext.call(this, data.version, root, current, cb)
}.bind(this))
}.bind(this))
}
function putNext(newVersion, root, current, cb) {
// already have the tardata on the root object
// just merge in existing stuff
// if the version already exists, and not a --force, then raise error
var force = this.conf.get('force')
var curVers = Object.keys(current.versions || {}).map(function (v) {
return semver.clean(v, true)
})
if (!force && curVers.indexOf(newVersion) !== -1) {
return cb(conflictError(root.name))
}
current.versions[newVersion] = root.versions[newVersion]
current._attachments = current._attachments || {}
for (var i in root) {
switch (i) {
// objects that copy over the new stuffs
case 'dist-tags':
case 'versions':
case '_attachments':
for (var j in root[i])
current[i][j] = root[i][j]
break
// ignore these
case 'maintainers':
break;
// copy
default:
current[i] = root[i]
}
}
var maint = JSON.parse(JSON.stringify(root.maintainers))
root.versions[newVersion].maintainers = maint
this.request("PUT", root.name, current, cb)
}
function conflictError (pkgid) {
var e = new Error("cannot modify existing version")
e.code = "EPUBLISHCONFLICT"
e.pkgid = pkgid
return e
}