-
Notifications
You must be signed in to change notification settings - Fork 5
/
master.js
142 lines (117 loc) · 4.22 KB
/
master.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
"use strict";
var config = require("./local");
var changes = require("./_changes");
var async = require("async");
var s3 = require("knox").createClient( config.s3 );
var fs = require("fs");
var worker = require("./lib/worker");
var url_module = require("url");
var q = async.queue(function(task, q_callback) {
if (task.deleted) {
return q_callback();
}
async.waterfall([
function get_package_index(w_callback) {
worker.get_package_index(task.id, config.source_registry, w_callback);
}, function clone_tarballs(package_index, w_callback) {
var package_download = async.queue(function(dist, package_callback) {
var url = url_module.parse(dist.tarball),
path = url_module.parse(url).path;
// Hack to work around broken package uploads
url.host = "registry.npmjs.org";
url.protocol = "http:";
url = url.format();
worker.get_tarball_stream(url, function(err, res) {
if (err) {
return package_callback(err);
}
console.log("GET " + url + " returned HTTP " + res.statusCode + " " + res.headers["content-length"]);
// Who needs a tarball? Not us!
if (res.statusCode === 404) {
return package_callback();
}
if (res.statusCode !== 200) {
return package_callback("GET " + url + " returned HTTP " + res.statusCode);
}
var headers = {
"Content-MD5": res.headers["content-md5"],
"Content-Length": res.headers["content-length"],
"Content-Type": res.headers["content-type"]
};
s3.putStream(res, path, headers, function(err, s3_res) {
if (err) {
return package_callback(err);
}
console.log("PUT Object " + path + " returned HTTP " + s3_res.statusCode + " " + res.headers["content-length"]);
if (s3_res.statusCode !== 200) {
return package_callback("PUT Object " + path + " returned HTTP " + s3_res.statusCode);
}
package_callback();
}).on("error", package_callback);
res.on("error", package_callback);
});
}, 4);
package_download.drain = function() {
w_callback(null, package_index);
};
if (!package_index.versions || Object.keys(package_index.versions).length === 0) {
return w_callback(null, package_index);
}
Object.keys(package_index.versions).forEach(function(version) {
var pkg = package_index.versions[version].dist;
pkg.version = version;
package_download.push(pkg, function(err) {
if (err) {
w_callback(err);
}
});
});
}, function rewrite_index(package_index, w_callback) {
worker.rewrite_package_dist(config.package_host, package_index);
w_callback(null, package_index);
}, function upload_index(package_index, w_callback) {
var body = new Buffer(JSON.stringify(package_index));
var path = "/" + task.id;
var headers = {
"Content-Length": body.length,
"Content-Type": "application/json; charset=utf-8"
};
s3.putBuffer(body, path, headers, function(err, s3_res) {
if (err) {
return w_callback(err);
}
if (s3_res.statusCode !== 200) {
return w_callback("PUT Object " + path + " returned HTTP " + s3_res.statusCode);
}
console.log("PUT Object " + path + " returned HTTP " + s3_res.statusCode + " " + body.length);
w_callback();
});
}
], function(err) {
if (err) {
throw err;
}
console.log("completed processing %j", task);
config.last_seq = task.seq;
var body = new Buffer(JSON.stringify(config, null, 2));
fs.writeFile("local.json", body, function(err) {
if (err) {
return q_callback(err);
}
console.log("checkpointed last_seq %d", config.last_seq);
q_callback();
});
});
}, 1);
q.drain = function() {
console.log("completed syncing");
process.exit(0);
};
var cur = 0, max = 10000;
for (var i = 0; i < changes.results.length && cur < max; i += 1) {
if (changes.results[i].seq <= config.last_seq) {
continue;
}
cur += 1;
q.push(changes.results[i]);
}