This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
update-rootcerts.js
215 lines (174 loc) · 4.81 KB
/
update-rootcerts.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
#!/usr/bin/env node
var fs = require('fs');
var url = require('url');
var http = require('http');
var path = require('path');
var Stream = require('stream').Stream;
var StringDecoder = require('string_decoder').StringDecoder;
var certUrl = 'https://raw.githubusercontent.com/nodejs/node/master/src/node_root_certs.h';
/**
* Get Root Certs
*/
function getRootCerts(callback) {
return request(certUrl, function(err, res, body) {
if (err) return callback(err);
// Delete preprocesor macros
body = body.replace(/#[^\n]+/g, '');
// Delete the trailing comma
body = body.replace(/,\s*$/, '');
// Make sure each C string is concatenated
body = body.replace(/"\r?\n"/g, '');
// Make sue we turn the cert names into property names
body = body.replace(/\/\*([^*]+)\*\/\n(?=")/g, function(_, name) {
var key = name.trim();
return '"' + key + '":\n';
});
// Delete Comments
body = body.replace(/\/\* tools\/\.\.\/src[^\0]+?\*\//, '');
body = body.replace(/\/\* @\(#\)[^\n]+/, '');
// \xff -> \u00ff
body = body.replace(/\\x([0-9a-fA-F]{2})/g, '\\u00$1');
// Trim Whitespace
body = body.trim();
// Wrap into a JSON Object
body = '{\n' + body + '\n}\n';
// Ensure JSON parses properly
try {
body = JSON.stringify(JSON.parse(body), null, 2);
} catch (e) {
return callback(e);
}
return callback(null, body);
});
}
/**
* Helpers
*/
function request(options, callback) {
if (typeof options === 'string' || options.hostname) {
options = { uri: options };
}
var uri = options.uri || options.url
, body = options.json
? JSON.stringify(options.json)
: options.body || '';
if (typeof uri !== 'object') {
uri = url.parse(uri);
}
if (options.qs) {
var query = uri.query ? qs.parse(uri.query) : {};
Object.keys(options.qs).forEach(function(key) {
query[key] = options.qs[key];
});
uri.path = uri.pathname + '?' + qs.stringify(query);
}
var protocol = uri.protocol === 'https:'
? require('https')
: http;
options.method = options.method || (body ? 'POST' : 'GET');
options.method = options.method.toUpperCase();
options.headers = options.headers || {};
options.headers['Accept'] = options.headers['Accept'] || 'text/plain; charset=utf-8';
if (options.json) {
options.headers['Content-Type'] = 'application/json; charset=utf-8';
options.headers['Accept'] = 'application/json';
}
if (options.method !== 'GET' && options.method !== 'HEAD') {
options.headers['Content-Length'] = Buffer.byteLength(body);
}
var opt = {
auth: uri.auth,
host: uri.hostname,
port: uri.port || (protocol === http ? 80 : 443),
path: uri.path,
method: options.method,
headers: options.headers
};
var req = protocol.request(opt)
, response = new Stream;
req.on('error', function(err) {
if (callback) {
callback(err);
} else {
response.emit('error', err);
}
});
req.on('response', function(res) {
var decoder = new StringDecoder('utf8')
, done = false
, body = '';
function end() {
if (done) return;
done = true;
if (callback) {
res.body = body;
if (options.json) {
try {
body = JSON.parse(body);
} catch (e) {
;
}
}
callback(null, res, body);
} else {
response.emit('end');
}
res.socket.removeListener('error', error);
res.socket.removeListener('end', end);
}
function error(err) {
res.destroy();
if (callback) {
callback(err);
} else {
response.emit('error', err);
}
}
res.on('data', function(data) {
if (callback) {
body += decoder.write(data);
} else {
response.emit('data', data);
}
});
res.on('error', error);
res.socket.on('error', error);
res.on('end', end);
// An agent socket's `end` sometimes
// wont be emitted on the response.
res.socket.on('end', end);
});
req.end(body);
return response;
}
/**
* Execute
*/
function main(argv, callback) {
if (!callback) {
callback = argv;
argv = null;
}
console.log('Retrieving root certs from: %s', certUrl);
return getRootCerts(function(err, certs) {
if (err) {
console.log('Error', err);
return callback();
}
var file = path.resolve(__dirname, 'lib', 'rootcerts.json');
return fs.writeFile(file, certs, function(err) {
if (err) return callback(err);
console.log('Root cert code generated at: %s.', file);
return callback();
});
});
}
if (!module.parent) {
process.title = 'root-certs';
main(process.argv.slice(), function(err, code) {
if (err) throw err;
return process.exit(code || 0);
});
} else {
module.exports = main;
}