This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
forked from paixaop/node-sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
executable file
·376 lines (344 loc) · 11.2 KB
/
install.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
/**
* Node Sodium install script to help support Windows
*
* @Author Pedro Paixao
* @email paixaop at gmail dot com
* @License MIT
*/
var https = require('https');
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var os = require('os');
var libFiles = [
'libsodium.dll',
'libsodium.exp',
'libsodium.lib',
'libsodium.pdb',
];
var includeFiles = [
'include/sodium/core.h',
'include/sodium/crypto_aead_aes256gcm.h',
'include/sodium/crypto_aead_chacha20poly1305.h',
'include/sodium/crypto_aead_xchacha20poly1305.h',
'include/sodium/crypto_auth.h',
'include/sodium/crypto_auth_hmacsha256.h',
'include/sodium/crypto_auth_hmacsha512.h',
'include/sodium/crypto_auth_hmacsha512256.h',
'include/sodium/crypto_box.h',
'include/sodium/crypto_box_curve25519xchacha20poly1305.h',
'include/sodium/crypto_box_curve25519xsalsa20poly1305.h',
'include/sodium/crypto_core_ed25519.h',
'include/sodium/crypto_core_hchacha20.h',
'include/sodium/crypto_core_hsalsa20.h',
'include/sodium/crypto_core_salsa20.h',
'include/sodium/crypto_core_salsa2012.h',
'include/sodium/crypto_core_salsa208.h',
'include/sodium/crypto_generichash.h',
'include/sodium/crypto_generichash_blake2b.h',
'include/sodium/crypto_hash.h',
'include/sodium/crypto_hash_sha256.h',
'include/sodium/crypto_hash_sha512.h',
'include/sodium/crypto_kdf.h',
'include/sodium/crypto_kdf_blake2b.h',
'include/sodium/crypto_kx.h',
'include/sodium/crypto_onetimeauth.h',
'include/sodium/crypto_onetimeauth_poly1305.h',
'include/sodium/crypto_pwhash.h',
'include/sodium/crypto_pwhash_argon2i.h',
'include/sodium/crypto_pwhash_argon2id.h',
'include/sodium/crypto_pwhash_scryptsalsa208sha256.h',
'include/sodium/crypto_scalarmult.h',
'include/sodium/crypto_scalarmult_curve25519.h',
'include/sodium/crypto_scalarmult_ed25519.h',
'include/sodium/crypto_secretbox.h',
'include/sodium/crypto_secretbox_xchacha20poly1305.h',
'include/sodium/crypto_secretbox_xsalsa20poly1305.h',
'include/sodium/crypto_secretstream_xchacha20poly1305.h',
'include/sodium/crypto_shorthash.h',
'include/sodium/crypto_shorthash_siphash24.h',
'include/sodium/crypto_sign.h',
'include/sodium/crypto_sign_ed25519.h',
'include/sodium/crypto_sign_edwards25519sha512batch.h',
'include/sodium/crypto_stream.h',
'include/sodium/crypto_stream_chacha20.h',
'include/sodium/crypto_stream_salsa20.h',
'include/sodium/crypto_stream_salsa2012.h',
'include/sodium/crypto_stream_salsa208.h',
'include/sodium/crypto_stream_xchacha20.h',
'include/sodium/crypto_stream_xsalsa20.h',
'include/sodium/crypto_verify_16.h',
'include/sodium/crypto_verify_32.h',
'include/sodium/crypto_verify_64.h',
'include/sodium/export.h',
'include/sodium/randombytes.h',
'include/sodium/randombytes_salsa20_random.h',
'include/sodium/randombytes_sysrandom.h',
'include/sodium/runtime.h',
'include/sodium/utils.h',
'include/sodium/version.h',
'include/sodium.h'
];
function recursePathList(paths) {
if (0 === paths.length) {
return;
}
var file = paths.shift();
if (!fs.existsSync(file)) {
try {
fs.mkdirSync(file, 0o755);
} catch (e) {
throw new Error("Failed to create path: " + file + " with " + e.toString());
}
}
recursePathList(paths);
}
function createFullPath(fullPath) {
var normPath = path.normalize(fullPath);
var file = '';
var pathList = [];
var parts = [];
if (normPath.indexOf('/') !== -1) {
parts = normPath.split('/');
} else {
parts = normPath.split('\\');
}
for (var i = 0, max = parts.length; i < max; i++) {
if (parts[i]) {
file = path.join(file, parts[i]);
pathList.push(file);
}
}
if (0 === pathList.length)
throw new Error("Path list was empty");
else
recursePathList(pathList);
}
function exists(path) {
try {
fs.accessSync(path, fs.F_OK);
return true;
} catch (e) {
return false;
}
}
function download(url, dest, cb) {
if(exists(dest)) {
console.log('File ' + dest + ' alredy exists, run make clean if you want to download it again.');
cb(null);
}
var file = fs.createWriteStream(dest);
var request = https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err);
});
}
function getPlatformToolsVersion() {
var platformTools = {
2010: 'v100',
2012: 'v110',
2013: 'v120',
2015: 'v140',
2017: 'v140',
}
var ver = platformTools[resolveMSVSVersion()];
if (!ver) {
throw new Error('Please set msvs_version');
}
return ver;
}
function downloadAll(files, baseURL, basePath, next) {
if (0 === files.length) {
next();
return;
}
var file = files.shift();
var url = baseURL + '/' + file;
var path = basePath + '/' + file;
console.log('Download: ' + url);
download(url, path, function(err) {
if (err) {
throw err;
}
downloadAll(files, baseURL, basePath, next);
});
}
function copyFile(source, target, cb) {
var cbCalled = false;
var rd = fs.createReadStream(source);
rd.on("error", function(err) {
done(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function(err) {
done(err);
});
wr.on("close", function(ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!cbCalled) {
cb(err);
cbCalled = true;
}
}
}
function copyFiles(files, next) {
if (0 === files.length) {
next();
return;
}
var file = files.shift();
var from = 'deps/build/lib/' + file;
var to = 'build/Release/' + file;
copyFile(from, to, function(err) {
if (err) {
throw err;
}
console.log('Copy ' + from + ' to ' + to);
copyFiles(files, next);
})
}
function gypConfigure(next) {
var gyp = exec('node-gyp configure');
gyp.stdout.on('data', function(data) {
process.stdout.write(data.toString());
});
gyp.stderr.on('data', function(data) {
process.stdout.write(data.toString());
});
gyp.on('close', function(code) {
console.log('Done.');
next();
});
}
function doDownloads(next) {
var baseURL = 'https://raw.githubusercontent.com/paixaop/libsodium-bin/master';
console.log('Download libsodium.lib');
var ver = getPlatformToolsVersion();
console.log('Platform Tool is ' + ver);
switch (os.arch()) {
case 'x64':
arch = 'x64';
break;
case 'ia32':
arch = 'Win32';
break;
default:
throw new Error('No pre-compiled binary available for this platform ' + os.arch());
}
// Older versions of node-sodium will try and download from the 'root' of baseURL
// Added libsodium_version to package.json to support multiple binary versions of
// libsodium
var package = require('./package.json');
if( package.libsodium_version ) {
baseURL += '/' + package.libsodium_version;
}
var libURL = baseURL + '/' + arch + '/Release/' + ver + '/dynamic';
files = libFiles.slice(0); // clone array
downloadAll(files, libURL, 'deps/build/lib', function() {
console.log('Libs for version ' + ver + ' downloaded.');
downloadAll(includeFiles, baseURL, 'deps/build', function() {
console.log('Include files downloaded.');
next();
});
});
}
function run(cmdLine, expectedExitCode, next) {
var child = spawn(cmdLine, { shell: true });
if (typeof expectedExitCode === 'undefined') {
expectedExitCode = 0;
}
child.stdout.on('data', function(data) {
process.stdout.write(data.toString());
});
child.stderr.on('data', function(data) {
process.stdout.write(data.toString());
});
child.on('exit', function(code) {
if (code !== expectedExitCode) {
throw new Error(cmdLine + ' exited with code ' + code);
}
if (!next) process.exit(0);
next();
});
}
function errorSetMSVSVersion() {
console.log('Please set your Microsoft Visual Studio version before you run npm install');
console.log('Example for Visual Studio 2022:\n');
console.log(' For you user only:\n');
console.log(' npm config set msvs_version 2022\n');
console.log(' Global:\n');
console.log(' npm config set msvs_version 2022 --global\n');
console.log('Supported values are 2010, 2012, 2013, 2015, 2017, 2019, 2022\n');
process.exit(1);
}
function errorInvalidMSVSVersion() {
console.log('Invalid msvs_version ' + process.env.npm_config_msvs_version + '\n');
console.log('Please set your Microsoft Visual Studio version before you run npm install');
console.log('Example for Visual Studio 2022:\n');
console.log(' For you user only:\n');
console.log(' npm config set msvs_version 2022\n');
console.log(' Global:\n');
console.log(' npm config set msvs_version 2022 --global\n');
console.log('Supported values are 2010, 2012, 2013, 2015, 2017, 2019, 2022\n');
process.exit(1);
}
function resolveMSVSVersion() {
if (!process.env.npm_config_msvs_version) {
errorSetMSVSVersion();
}
console.log('MS Version: ' + process.env.npm_config_msvs_version);
var parsed = Math.min(parseInt(process.env.npm_config_msvs_version), 2017).toString();
if (parsed.search(/^2010|2012|2013|2015|2017$/)) {
errorInvalidMSVSVersion();
}
return parsed;
}
function isPreInstallMode() {
if (!process.argv[2] || process.argv[2].search(/^--preinstall|--install/)) {
console.log('please call install with --preinstall or --install');
process.exit(1);
}
if (process.argv[2] === '--preinstall') {
return true;
}
return false;
}
// Start
if (os.platform() !== 'win32') {
if (isPreInstallMode()) {
run('make libsodium');
} else {
run('make nodesodium');
}
} else {
resolveMSVSVersion();
if (isPreInstallMode()) {
console.log('Preinstall Mode');
createFullPath("deps/build/include/sodium");
createFullPath("deps/build/lib");
createFullPath("build/Release");
doDownloads(function() {
console.log('Prebuild steps completed. Binary libsodium distribution installed in ./deps/build');
process.exit(0);
});
} else {
console.log('Install Mode');
run('node-gyp rebuild', 0, function() {
console.log('Copy lib files to Release folder');
files = libFiles.slice(0); // clone array
copyFiles(files, function() {
console.log('Done copying files.');
process.exit(0);
});
});
}
}