-
Notifications
You must be signed in to change notification settings - Fork 17
/
files.js
136 lines (123 loc) · 4.12 KB
/
files.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
/*
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License.
See LICENSE file.
*/
var async = require('async');
var path = require('path');
var options = require('./args.js');
var log = require('./logger');
var verify = require('./verify.js');
var hooks = require('./hooks');
var miss = require('mississippi');
var once = require('once');
process.registryStaticStats = {};
function writeJSONFile(name, data, callback) {
setImmediate(function(){
options.blobstore.createWriteStream(name, callback)
.end(JSON.stringify(data, null, 4) + '\n');
});
}
function readJSONFile(name, callback) {
callback = once(callback);
miss.pipe(options.blobstore.createReadStream(name), miss.concat(function(data){
try {
callback(null, JSON.parse(data.toString()));
} catch (error) {
callback(error);
}
}), function(err){
if (err) { callback(err); }
});
}
// Merge the new doc with the old doc, making sure not to delete older versions.
function newDoc(doc, callback) {
// NOTE: in here we're swallowing all the errors.
// If anything fails, we want to just use the new Doc from the update as is.
var file = path.join(doc.name, 'index.json');
if (!doc.versions) {
// bad data. still don't want to lose old versions, so set this.
doc.versions = {};
}
options.blobstore.exists(file, function(err, exists) {
if (err || !exists) { return callback(null, doc); }
readJSONFile(file, function(err, oldDoc){
if (err || !oldDoc) { return callback(null, doc); }
Object.keys(oldDoc.versions || {}).forEach(function(version){
if (!doc.versions[version]) {
doc.versions[version] = oldDoc.versions[version];
}
});
callback(null, doc);
});
});
}
function putBall(info, callback) {
info.tarball = path.join(info.path);
if (!info.tarball.match(/^.*\.tgz/)) {
// not a real tarball! skip
return callback();
}
hooks.tarball(info, callback, function () {
setImmediate(function() {
verify.verify(info, function (err) {
if (err) {
return callback(err);
}
hooks.afterTarball(info, callback, callback);
});
});
});
}
function saveTarballs (tarballs, callback) {
async.eachLimit(tarballs, options.limit, putBall, callback);
}
exports.saveTarballs = saveTarballs;
//Always write it even if it is there.
function putPart (info, callback) {
if (!info.json) {
return callback();
}
hooks.versionJson(info, callback, function() {
writeJSONFile(path.join(info.json.name, info.version, 'index.json'), info.json, callback);
});
}
//Always write it even if it is there.
function putJSON (info, callback) {
var doc = info.json;
if (!doc.name || doc.error) {
return callback(doc.error);
}
var putAllParts = function(err) {
if (err) {
return callback(err);
}
info.versions.forEach(function(item, key) {
if (item.json) {
item.json.name = item.json.name || doc.name;
info.versions[key] = item;
}
});
async.eachLimit(info.versions, 5, putPart, callback);
};
newDoc(doc, function(err, doc) {
var seq = info.seq;
var latestSeq = info.latestSeq;
process.registryStaticStats.seq = seq;
process.registryStaticStats.latestSeq = latestSeq;
hooks.indexJson(info, putAllParts, function() {
var file = path.join(doc.name, 'index.json');
log.info('[' + seq + '/' + latestSeq + ']', 'writing json for', doc.name, 'to', file);
writeJSONFile(file, doc, function(err) {
if (err) {
return putJSON(info, callback);
}
if (!info.versions || !info.versions.length) {
return callback();
}
setImmediate(putAllParts);
});
});
});
}
exports.saveJSON = putJSON;