-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall-from-cache.js
executable file
·233 lines (216 loc) · 7.72 KB
/
install-from-cache.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
#!/usr/bin/env node
'use strict';
const {promises: fsp} = require('fs');
const path = require('path');
const zlib = require('zlib');
const {promisify} = require('util');
const http = require('http');
const https = require('https');
const {exec, spawnSync} = require('child_process');
const spawnOptions = {encoding: 'utf8', env: process.env};
const getPlatform = () => {
let platform = process.env.npm_config_platform;
if (platform) return platform;
platform = process.platform;
if (platform !== 'linux') return platform;
// detecting musl using algorithm from /~https://github.com/lovell/detect-libc under Apache License 2.0
let result = spawnSync('getconf', ['GNU_LIBC_VERSION'], spawnOptions);
if (!result.status && !result.signal) return platform;
result = spawnSync('ldd', ['--version'], spawnOptions);
if (result.signal) return platform;
if ((!result.status && result.stdout.toString().indexOf('musl') >= 0) || (result.status === 1 && result.stderr.toString().indexOf('musl') >= 0))
return platform + '-musl';
return platform;
};
const platform = getPlatform(),
platformArch = process.env.npm_config_platform_arch || process.arch,
platformABI = process.env.npm_config_platform_abi || process.versions.modules;
const isParamPresent = name => process.argv.indexOf('--' + name) > 0;
const getParam = (name, defaultValue = '') => {
const index = process.argv.indexOf('--' + name);
if (index > 0) return process.argv[index + 1] || '';
return defaultValue;
};
const artifactPath = getParam('artifact'),
prefix = getParam('prefix'),
suffix = getParam('suffix'),
mirrorHost = getParam('host'),
mirrorEnvVar = getParam('host-var') || 'DOWNLOAD_HOST',
skipPath = isParamPresent('skip-path'),
skipPathVar = getParam('skip-path-var') || 'DOWNLOAD_SKIP_PATH',
skipVer = isParamPresent('skip-ver'),
skipVerVar = getParam('skip-ver-var') || 'DOWNLOAD_SKIP_VER';
const parseUrl = [
/^(?:https?|git|git\+ssh|git\+https?):\/\/github.com\/([^\/]+)\/([^\/\.]+)(?:\/|\.git\b|$)/i,
/^github:([^\/]+)\/([^#]+)(?:#|$)/i,
/^([^:\/]+)\/([^#]+)(?:#|$)/i
];
const isHttp = /^http:\/\//i,
isHttps = /^https:\/\//i;
const getRepo = url => {
if (!url) return null;
for (const re of parseUrl) {
const result = re.exec(url);
if (result) return result;
}
return null;
};
const getAssetUrlPrefix = () => {
const url = process.env.npm_package_github || (process.env.npm_package_repository_type === 'git' && process.env.npm_package_repository_url),
result = getRepo(url);
if (!result) return null;
let assetUrl = mirrorHost || process.env[mirrorEnvVar] || 'https://github.com';
if (!skipPath && !process.env[skipPathVar]) {
assetUrl += `/${result[1]}/${result[2]}/releases/download`;
}
if (!skipVer && !process.env[skipVerVar]) {
assetUrl += '/' + process.env.npm_package_version;
}
assetUrl += `/${prefix}${platform}-${platformArch}-${platformABI}${suffix}`;
return assetUrl;
};
const isDev = async () => {
if (process.env.DEVELOPMENT_SKIP_GETTING_ASSET) return true;
try {
await fsp.access('.development');
return true;
} catch (e) {
// squelch
}
return false;
};
const run = (cmd, suppressOutput) =>
new Promise((resolve, reject) => {
const p = exec(cmd);
let closed = false;
p.on('exit', (code, signal) => {
if (closed) return;
closed = true;
(signal || code) && reject(signal || code);
resolve(0);
});
p.on('error', error => !closed && ((closed = true), reject(error)));
if (!suppressOutput || process.env.DEVELOPMENT_SHOW_VERIFICATION_RESULTS) {
p.stdout.on('data', data => process.stdout.write(data));
p.stderr.on('data', data => process.stderr.write(data));
}
});
const isVerified = async () => {
if (process.env.npm_config_platform || process.env.npm_config_platform_arch || process.env.npm_config_platform_abi) {
console.log(`Fetched for the custom platform "${platform}-${platformArch}-${platformABI}" -- skipping the verification.`);
return true;
}
try {
if (process.env.npm_package_scripts_verify_build) {
await run('npm run verify-build', true);
} else if (process.env.npm_package_scripts_test) {
await run('npm test', true);
} else {
console.log('No verify-build nor test scripts were found -- no way to verify the build automatically.');
return false;
}
} catch (e) {
console.log('The verification has failed: building from sources ...');
return false;
}
return true;
};
const get = url =>
new Promise((resolve, reject) => {
const httpLib = isHttps.test(url) ? https : isHttp.test(url) ? http : null;
if (!httpLib) {
// local file
fsp.readFile(url).then(resolve, reject);
return;
}
let buffer = null;
httpLib
.get(url, {agent: false}, res => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers && res.headers.location) {
get(res.headers.location).then(resolve, reject);
return;
}
if (res.statusCode != 200) {
reject(Error(`Status ${res.statusCode} for ${url}`));
return;
}
res.on('data', data => {
if (buffer) {
buffer = Buffer.concat([buffer, data]);
} else {
buffer = data;
}
});
res.on('end', () => resolve(buffer));
})
.on('error', e => reject(e))
.end();
});
const write = async (name, data) => {
await fsp.mkdir(path.dirname(name), {recursive: true});
await fsp.writeFile(name, data);
};
const main = async () => {
checks: {
if (process.env.npm_package_json && /\bpackage\.json$/i.test(process.env.npm_package_json)) {
// for NPM >= 7
try {
// read the package info
const pkg = JSON.parse(await fsp.readFile(process.env.npm_package_json));
// populate necessary environment variables locally
process.env.npm_package_github = pkg.github || '';
process.env.npm_package_repository_type = (pkg.repository && pkg.repository.type) || '';
process.env.npm_package_repository_url = (pkg.repository && pkg.repository.url) || '';
process.env.npm_package_version = pkg.version || '';
process.env.npm_package_scripts_verify_build = (pkg.scripts && pkg.scripts['verify-build']) || '';
process.env.npm_package_scripts_test = (pkg.scripts && pkg.scripts.test) || '';
} catch (error) {
console.log('Could not retrieve and parse package.json.');
break checks;
}
}
if (!artifactPath) {
console.log('No artifact path was specified with --artifact.');
break checks;
}
if (await isDev()) {
console.log('Development flag was detected.');
break checks;
}
const prefix = getAssetUrlPrefix();
if (!prefix) {
console.log('No github repository was identified.');
break checks;
}
let copied = false;
// let's try brotli
if (zlib.brotliDecompress) {
try {
console.log(`Trying ${prefix}.br ...`);
const artifact = await get(prefix + '.br');
console.log(`Writing to ${artifactPath} ...`);
await write(artifactPath, await promisify(zlib.brotliDecompress)(artifact));
copied = true;
} catch (e) {
// squelch
}
}
// let's try gzip
if (!copied && zlib.gunzip) {
try {
console.log(`Trying ${prefix}.gz ...`);
const artifact = await get(prefix + '.gz');
console.log(`Writing to ${artifactPath} ...`);
await write(artifactPath, await promisify(zlib.gunzip)(artifact));
copied = true;
} catch (e) {
// squelch
}
}
// verify the install
if (copied && (await isVerified())) return console.log('Done.');
}
console.log('Building locally ...');
await run('npm run rebuild');
};
main();