-
Notifications
You must be signed in to change notification settings - Fork 132
/
util.js
416 lines (384 loc) · 13.1 KB
/
util.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const path = require("path");
const fs = require("fs");
const os = require("os");
const assert = require("assert");
const standardInstallDirectory = path.join(__dirname, "installed");
module.exports.standardInstallDirectory = standardInstallDirectory;
/**
* Make directory, creating missing parent directories as well.
* Equivalent to fs.mkdirSync(p, {recursive: true});
* @param {string} dirname
*/
module.exports.mkDirRecursive = function mkDirRecursive(dirname) {
if (!path.isAbsolute(dirname)) {
dirname = path.join(process.cwd(), dirname);
}
dirname = path.normalize(dirname);
let parts = dirname.split(path.sep);
for (let i = 2; i <= parts.length; i++) {
let p = parts.slice(0, i).join(path.sep);
if (fs.existsSync(p)) {
let i = fs.lstatSync(p);
if (!i.isDirectory()) {
throw new Error("cannot mkdir '" + dirname + "'. '" + p + "' is not a directory.");
}
} else {
fs.mkdirSync(p);
}
}
};
/**
* @typedef {Object} DistEntry
* @property {string} name
* @property {string} version
* @property {string} protocPath
* @property {string} includePath
*/
/**
* @param {string} installDir
* @return {DistEntry[]}
*/
module.exports.listInstalled = function listInstalled(installDir = standardInstallDirectory) {
let entries = [];
for (let name of fs.readdirSync(installDir)) {
let abs = path.join(installDir, name);
if (!fs.lstatSync(abs).isDirectory()) {
continue;
}
// looking for directory names "protoc-3.13.0-win32"
if (!name.startsWith("protoc-")) {
continue;
}
let version = name.split("-")[1];
let protocPath = path.join(abs, "bin/protoc.exe");
if (!fs.existsSync(protocPath)) {
protocPath = path.join(abs, "bin/protoc");
}
let includePath = path.join(abs, "include/")
entries.push({name, version, protocPath, includePath});
}
return entries;
};
/**
* Download url into path. Returns path.
* @param {string} url
* @returns {Promise<Buffer>}
*/
module.exports.httpDownload = function download(url) {
assert(typeof url === "string" && url.length > 0);
assert(url.startsWith("https://") || url.startsWith("http://"));
const chunks = [];
return new Promise((resolve, reject) => {
httpGet(url, []).then(
response => {
response.setEncoding("binary");
response.on("data", chunk => {
chunks.push(Buffer.from(chunk, "binary"))
});
response.on("end", () => {
resolve(Buffer.concat(chunks));
})
},
reason => reject(reason)
);
});
};
/**
* @param {string} url
* @return {Promise<string>}
*/
module.exports.httpGetRedirect = function httpGetRedirect(url) {
assert(typeof url === "string" && url.length > 0);
assert(url.startsWith("https://") || url.startsWith("http://"));
const client = url.startsWith("https") ? require("https") : require("http");
return new Promise((resolve, reject) => {
const request = client.get(url, (response) => {
if (response.statusCode >= 300 && response.statusCode < 400) {
let location = response.headers.location;
assert(location && location.length > 0);
resolve(location);
} else if (response.statusCode !== 200) {
reject(new Error(`HTTP ${response.statusCode} for ${url}`));
} else {
reject(new Error(`Did not get expected redirect for ${url}`));
}
});
request.on("error", reject);
});
};
/**
* HTTP GET, follows up to 3 redirects
* @param {string} url
* @param {string[]} redirects
* @returns {Promise<IncomingMessage>}
*/
function httpGet(url, redirects) {
assert(typeof url === "string" && url.length > 0);
assert(url.startsWith("https://") || url.startsWith("http://"));
assert(Array.isArray(redirects));
assert(redirects.length <= 3);
const client = url.startsWith("https") ? require("https") : require("http");
return new Promise((resolve, reject) => {
const request = client.get(url, (response) => {
if (response.statusCode >= 300 && response.statusCode < 400) {
let location = response.headers.location;
assert(location && location.length > 0);
let follow = httpGet(location, redirects.concat(location));
resolve(follow);
} else if (response.statusCode !== 200) {
reject(new Error(`HTTP ${response.statusCode} for ${url}`));
} else {
resolve(response);
}
});
request.on("error", reject);
});
}
/**
* @typedef {Object} ReleaseParameters
* @property {NodeJS.Platform} platform
* @property {CPUArchitecture} arch
* @property {string} version - without leading "v"
*/
/**
* @typedef {("arm" | "arm64" | "ia32" | "mips" | "mipsel" | "ppc" | "ppc64" | "s390" | "s390x" | "x32" | "x64")} CPUArchitecture
*/
/**
* protoc-3.13.0-linux-aarch_64.zip
* protoc-3.13.0-linux-ppcle_64.zip
* protoc-3.13.0-linux-s390x.zip
* protoc-3.13.0-linux-x86_32.zip
* protoc-3.13.0-linux-x86_64.zip
* protoc-3.13.0-osx-x86_64.zip
* protoc-3.13.0-win32.zip
* protoc-3.13.0-win64.zip
*
* @param {ReleaseParameters} params
* @return {string}
*/
module.exports.makeReleaseName = function makeReleaseName(params) {
let build = `${params.platform}-${params.arch}`;
switch (params.platform) {
case "darwin":
build = 'osx-x86_64'
break;
case "linux":
if (params.arch === "x64") {
build = 'linux-x86_64'
} else if (params.arch === "x32") {
build = 'linux-x86_32'
}
break;
case "win32":
if (params.arch === "x64") {
build = 'win64'
} else if (params.arch === "x32") {
build = 'win32'
}
break;
}
return `protoc-${params.version}-${build}`;
}
/**
* Reads the package json from the given path if it exists and
* looks for config.protocVersion.
*
* If the package.json does not exist or does not specify a
* config.protocVersion value, walk the file system up until
* a package.json with a config.protocVersion is found.
*
* If nothing was found, return undefined.
*
* @param {string} cwd
* @returns {string | undefined}
*/
module.exports.findProtocVersionConfig = function findProtocVersionConfig(cwd) {
let version = undefined;
let dirname = cwd;
while (true) {
version = tryReadProtocVersion(path.join(dirname, "package.json"));
if (version !== undefined) {
break;
}
let parent = path.dirname(dirname);
if (parent === dirname) {
break;
}
dirname = parent;
}
return version;
};
function tryReadProtocVersion(pkgPath) {
if (!fs.existsSync(pkgPath)) {
return undefined;
}
let json = fs.readFileSync(pkgPath, "utf8");
let pkg;
try {
pkg = JSON.parse(json);
} catch (e) {
return undefined;
}
if (typeof pkg === "object" && typeof pkg.config === "object" && pkg.config !== null) {
if (pkg.config.hasOwnProperty("protocVersion") && typeof pkg.config.protocVersion == "string") {
let version = pkg.config.protocVersion;
if (typeof version === "string") {
return version;
}
}
}
return undefined;
}
/**
* @param {string} cwd
* @returns {string|undefined}
*/
module.exports.findProtobufTs = function (cwd) {
let plugin = path.join(cwd, "node_modules", "@protobuf-ts", "plugin");
return fs.existsSync(plugin) ? plugin : undefined;
}
/**
* @param {string} cwd
* @returns {string[]}
*/
module.exports.findProtocPlugins = function (cwd) {
let plugins = [];
let binDir = path.join(cwd, "node_modules", ".bin");
if (!fs.existsSync(binDir)) {
return plugins;
}
if (!fs.lstatSync(binDir).isDirectory()) {
return plugins;
}
for (let name of fs.readdirSync(binDir)) {
if (!name.startsWith("protoc-gen-")) {
continue;
}
let plugin = path.join("node_modules", ".bin", name);
plugins.push(plugin);
}
return plugins;
};
/**
* @param {string|undefined} envPath from process.env.PATH
* @returns {string|undefined}
*/
module.exports.findProtocInPath = function (envPath) {
if (typeof envPath !== "string") {
return undefined;
}
const candidates = envPath.split(path.delimiter)
.filter(p => !p.endsWith(`node_modules${path.sep}.bin`)) // make sure to exlude ...
.filter(p => !p.endsWith(`.npm-global${path.sep}bin`)) // ...
.map(p => path.join(p, os.platform() === "win32" ? "protoc.exe" : "protoc")) // we are looking for "protoc"
.map(p => p[0] === "~" ? path.join(os.homedir(), p.slice(1)) : p) // try expand "~"
;
for (let c of candidates) {
if (fs.existsSync(c)) {
return c;
}
}
return undefined;
};
/**
* @callback fileCallback
* @param {Buffer} data
* @param {LocalHeader} header
*/
/**
* @param {Buffer} buffer
* @param {fileCallback} onFile
*/
module.exports.unzip = function unzip(buffer, onFile) {
const
zlib = require("zlib"),
localHeaderSig = 0x04034b50, // Local file header signature
centralHeaderSig = 0x02014b50, // Central directory file header signature
eocdRecordSig = 0x06054b50, // End of central directory record (EOCD)
optDataDescSig = 0x08074b50, // Optional data descriptor signature
methodStored = 0,
methodDeflated = 8;
let pos = 0;
let cenFound = false;
while (pos < buffer.byteLength && !cenFound) {
if (buffer.byteLength - pos < 2) {
throw new Error("Signature too short at " + pos);
}
let sig = buffer.readUInt32LE(pos);
switch (sig) {
case localHeaderSig:
let header = readLocalHeader();
let compressedData = buffer.subarray(pos, pos + header.compressedSize);
let uncompressedData;
switch (header.compressionMethod) {
case methodDeflated:
uncompressedData = zlib.inflateRawSync(compressedData);
break;
case methodStored:
uncompressedData = compressedData;
break;
default:
throw new Error("Unsupported compression method " + header.compressionMethod);
}
if (header.filename[header.filename.length - 1] !== "/") {
onFile(uncompressedData, header);
}
pos += header.compressedSize;
break;
case centralHeaderSig:
cenFound = true;
break;
default:
throw new Error("Unexpected signature " + sig);
}
}
/**
* @typedef {Object} LocalHeader
* @property {number} version
* @property {number} generalPurposeFlags
* @property {number} compressionMethod
* @property {number} lastModificationTime
* @property {number} lastModificationDate
* @property {number} crc32
* @property {number} compressedSize
* @property {number} uncompressedSize
* @property {string} filename
* @property {Buffer} extraField
*
* @returns {undefined|LocalHeader}
*/
function readLocalHeader() {
let sig = buffer.readUInt32LE(pos);
if (sig !== localHeaderSig) {
throw new Error("Unexpected local header signature " + sig.toString(16) + " at " + pos);
}
if (buffer.byteLength - pos < 30) {
throw new Error("Local header too short at " + pos);
}
let version = buffer.readUInt32LE(pos + 4);
let generalPurposeFlags = buffer.readUInt16LE(pos + 6);
let compressionMethod = buffer.readUInt16LE(pos + 8);
let lastModificationTime = buffer.readUInt16LE(pos + 10);
let lastModificationDate = buffer.readUInt16LE(pos + 12);
let crc32 = buffer.readUInt32LE(pos + 14);
let compressedSize = buffer.readUInt32LE(pos + 18);
let uncompressedSize = buffer.readUInt32LE(pos + 22);
let filenameLength = buffer.readUInt16LE(pos + 26);
let extraFieldLength = buffer.readUInt16LE(pos + 28);
let filename = buffer.subarray(pos + 30, pos + 30 + filenameLength).toString();
let extraField = buffer.subarray(pos + 30 + filenameLength, pos + 30 + filenameLength + extraFieldLength);
pos += 30 + filenameLength + extraFieldLength;
return {
version,
generalPurposeFlags,
compressionMethod,
lastModificationTime,
lastModificationDate,
crc32,
compressedSize,
uncompressedSize,
filename,
extraField
};
}
}