-
Notifications
You must be signed in to change notification settings - Fork 2
/
registry.js
244 lines (223 loc) · 8.4 KB
/
registry.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
var log = require("ringo/logging").getLogger(module.id);
var cache = exports.cache = require("./cache");
var config = require("./config");
var httpclient = require("./httpclient");
var strings = require("ringo/utils/strings");
var crypto = require("./crypto");
var semver = require("./semver");
var files = require("./files");
var {Path} = require("fs");
var {SimpleDateFormat} = java.text;
var {Locale, TimeZone} = java.util;
const DATEFORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",
Locale.US);
DATEFORMATTER.setTimeZone(TimeZone.getTimeZone("UTC"));
exports.getPackageDescriptor = function(name, version) {
var catalog = cache.get();
if (!catalog.hasOwnProperty(name)) {
throw new Error("Package '" + name + "' does not exist in registry");
}
var descriptor = catalog[name];
if (version == undefined || version === "latest") {
version = descriptor.latest;
}
for each (let candidate in descriptor.versions) {
if (semver.satisfies(candidate.version, version)) {
return candidate;
}
}
throw new Error("Unable to find version " + version + " for package " + name);
};
exports.getLatestCompatible = function(name, version) {
var catalog = cache.get();
if (!catalog.hasOwnProperty(name)) {
throw new Error("Package '" + name + "' does not exist in registry");
}
var descriptor = catalog[name];
for each (let candidate in descriptor.versions) {
if (semver.isCompatible(candidate.version, version)) {
return candidate;
}
}
throw new Error("Unable to find latest compatible version for package " +
name + " " + version);
};
exports.isLatest = function(name, version) {
var descriptor = this.getPackageDescriptor(name);
return semver.isGreater(descriptor.latest, version) === false;
};
exports.getPackage = function(descriptor) {
var url = httpclient.composeUrl(config.registryUrl, "download", descriptor.filename);
log.debug("Retrieving package", url);
var file = httpclient.getBinary(url);
files.verifyChecksums(file, descriptor.checksums);
return file;
};
exports.getPackageOwners = function(name) {
var catalog = cache.get();
if (!catalog.hasOwnProperty(name)) {
throw new Error("Package '" + name + "' does not exist in registry");
}
return catalog[name].owners;
};
exports.exists = function(name, version) {
var catalog = cache.get();
var pkgInfo = catalog[name];
return pkgInfo != null && (!version || pkgInfo.versions.some(function(v) {
return v.version == version;
}));
};
exports.createUser = function(username, pwd, email) {
log.debug("Creating user account", username, "...");
var salt = crypto.createSalt();
var digest = crypto.createDigest(pwd, salt);
var url = httpclient.composeUrl(config.registryUrl, "api/users/");
var response = httpclient.post(url, {
"username": username,
"salt": strings.b64encode(salt),
"password": strings.b64encode(digest),
"email": email
});
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.userExists = function(username) {
var url = httpclient.composeUrl(config.registryUrl, "api/users", username);
var response = httpclient.get(url);
return response.statusCode === 200;
};
exports.getSalt = function(username) {
var url = httpclient.composeUrl(config.registryUrl, "api/users", username, "salt");
var response = httpclient.get(url);
if (response.statusCode === 200) {
return response.body;
}
return null;
};
exports.changePassword = function(username, password, newPassword) {
log.debug("Changing password of", username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/users/password");
var response = httpclient.post(url, {
"password": newPassword
}, username, password);
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.initPasswordReset = function(username, email) {
log.debug("Initializing password reset for", username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/users", username, "reset");
var response = httpclient.post(url, {
"email": email
});
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.resetPassword = function(username, token, password) {
log.debug("Resetting password of", username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/users", username, "password");
var response = httpclient.post(url, {
"token": token,
"password": password
});
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.publish = function(username, password, descriptor, archivePath, force) {
log.debug(descriptor.name, "is published by", username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/packages",
descriptor.name, descriptor.version);
var response = httpclient.post(url, {
"descriptor": JSON.stringify(descriptor),
"pkg": new Path(archivePath),
"force": force === true
}, username, password);
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.unpublish = function(username, password, name, version) {
var composeArgs = [config.registryUrl, "api/packages", name];
if (version != undefined) {
composeArgs.push(version);
}
log.debug(name, version, "is unpublished by", username, "...");
var response = httpclient.del(httpclient.composeUrl.apply(null, composeArgs),
username, password);
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.search = function(query) {
var url = httpclient.composeUrl(config.registryUrl, "api/search");
var response = httpclient.get(url, {
"q": query
});
return response.body;
};
exports.addOwner = function(username, password, pkgName, ownerName) {
log.debug(ownerName, "is added to list of owners of", pkgName, "by",
username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/owners", pkgName, ownerName);
var response = httpclient.put(url, username, password);
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.removeOwner = function(username, password, pkgName, ownerName) {
log.debug(ownerName, "is removed from list of owners of", pkgName, "by",
username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/owners", pkgName, ownerName);
var response = httpclient.del(url, username, password);
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};
exports.getPackages = function() {
var url = httpclient.composeUrl(config.registryUrl, "api/packages/");
log.debug("Retrieving registry catalog from", url);
var response = httpclient.get(url);
if (response.statusCode !== 200) {
throw new Error("Unable to retrieve package catalog");
}
return response.body;
};
exports.getUpdates = function(lastUpdate) {
var utcDateStr = DATEFORMATTER.format(lastUpdate);
var url = httpclient.composeUrl(config.registryUrl, "api/updates");
log.debug("Retrieving catalog updates from", url, "(if-modified-since:",
utcDateStr, ")");
var response = httpclient.get(url, null, {
"if-modified-since": utcDateStr
});
if (response.statusCode === 200) {
return response.body;
} else if (response.statusCode !== 304) {
log.warn("Unable to retrieve catalog updates, status code",
response.statusCode);
throw new Error("Unable to retrieve catalog updates");
}
return null;
};
exports.changeEmail = function(username, password, email) {
log.debug("Changing email of", username, "...");
var url = httpclient.composeUrl(config.registryUrl, "api/users/email");
var response = httpclient.post(url, {
"email": email
}, username, password);
if (response.statusCode !== 200) {
throw new Error(response.body.message);
}
return response.body;
};