1 | "use strict";
|
2 |
|
3 | module.exports = exports = install;
|
4 |
|
5 | exports.usage = 'Attempts to install pre-built binary for module';
|
6 |
|
7 | var fs = require('fs');
|
8 | var path = require('path');
|
9 | var zlib = require('zlib');
|
10 | var log = require('npmlog');
|
11 | var existsAsync = fs.exists || path.exists;
|
12 | var versioning = require('./util/versioning.js');
|
13 | var testbinary = require('./testbinary.js');
|
14 | var clean = require('./clean.js');
|
15 |
|
16 | function download(uri,opts,callback) {
|
17 | log.http('GET', uri);
|
18 |
|
19 | var req = null;
|
20 | var requestOpts = {
|
21 | uri: uri.replace('+','%2B'),
|
22 | headers: {
|
23 | 'User-Agent': 'node-pre-gyp (node ' + process.version + ')'
|
24 | }
|
25 | };
|
26 |
|
27 | var proxyUrl = opts.proxy ||
|
28 | process.env.http_proxy ||
|
29 | process.env.HTTP_PROXY ||
|
30 | process.env.npm_config_proxy;
|
31 | if (proxyUrl) {
|
32 | if (/^https?:\/\//i.test(proxyUrl)) {
|
33 | log.verbose('download', 'using proxy url: "%s"', proxyUrl);
|
34 | requestOpts.proxy = proxyUrl;
|
35 | } else {
|
36 | log.warn('download', 'ignoring invalid "proxy" config setting: "%s"', proxyUrl);
|
37 | }
|
38 | }
|
39 | try {
|
40 | req = require('request')(requestOpts);
|
41 | } catch (e) {
|
42 | return callback(e);
|
43 | }
|
44 | if (req) {
|
45 | req.on('response', function (res) {
|
46 | log.http(res.statusCode, uri);
|
47 | });
|
48 | }
|
49 | return callback(null,req);
|
50 | }
|
51 |
|
52 | function place_binary(from,to,opts,callback) {
|
53 | download(from,opts,function(err,req) {
|
54 | if (err) return callback(err);
|
55 | if (!req) return callback(new Error("empty req"));
|
56 | var badDownload = false;
|
57 | var extractCount = 0;
|
58 | var gunzip = zlib.createGunzip();
|
59 | var extracter = require('tar').Extract({ path: to, strip: 1});
|
60 |
|
61 | function afterTarball(err) {
|
62 | if (err) return callback(err);
|
63 | if (badDownload) return callback(new Error("bad download"));
|
64 | if (extractCount === 0) {
|
65 | return callback(new Error('There was a fatal problem while downloading/extracting the tarball'));
|
66 | }
|
67 | log.info('tarball', 'done parsing tarball');
|
68 | callback();
|
69 | }
|
70 |
|
71 | function filter_func(entry) {
|
72 |
|
73 |
|
74 | entry.props.mode |= (entry.props.mode >>> 2) & parseInt('0111',8);
|
75 | log.info('install','unpacking ' + entry.path);
|
76 | extractCount++;
|
77 | }
|
78 |
|
79 | gunzip.on('error', callback);
|
80 | extracter.on('entry', filter_func);
|
81 | extracter.on('error', callback);
|
82 | extracter.on('end', afterTarball);
|
83 |
|
84 | req.on('error', function(err) {
|
85 | badDownload = true;
|
86 | return callback(err);
|
87 | });
|
88 |
|
89 | req.on('close', function () {
|
90 | if (extractCount === 0) {
|
91 | return callback(new Error('Connection closed while downloading tarball file'));
|
92 | }
|
93 | });
|
94 |
|
95 | req.on('response', function(res) {
|
96 | if (res.statusCode !== 200) {
|
97 | badDownload = true;
|
98 | if (res.statusCode == 404) {
|
99 | return callback(new Error('Pre-built binary not available for your system, looked for ' + from));
|
100 | } else {
|
101 | return callback(new Error(res.statusCode + ' status code downloading tarball ' + from));
|
102 | }
|
103 | }
|
104 |
|
105 | req.pipe(gunzip).pipe(extracter);
|
106 | });
|
107 | });
|
108 | }
|
109 |
|
110 | function do_build(gyp,argv,callback) {
|
111 | gyp.todo.push( { name: 'build', args: ['rebuild'] } );
|
112 | process.nextTick(callback);
|
113 | }
|
114 |
|
115 | function print_fallback_error(err,opts,package_json) {
|
116 | var fallback_message = ' (falling back to source compile with node-gyp)';
|
117 | var full_message = "Pre-built binaries not found for " + package_json.name + "@" + package_json.version;
|
118 | full_message += " and " + opts.runtime + "@" + (opts.target || process.versions.node) + " (" + opts.node_abi + " ABI)";
|
119 | full_message += fallback_message;
|
120 | log.error("Tried to download: " + opts.hosted_tarball);
|
121 | log.error(full_message);
|
122 | log.http(err.message);
|
123 | }
|
124 |
|
125 | function install(gyp, argv, callback) {
|
126 | var package_json = JSON.parse(fs.readFileSync('./package.json'));
|
127 | var source_build = gyp.opts['build-from-source'] || gyp.opts.build_from_source;
|
128 | var update_binary = gyp.opts['update-binary'] || gyp.opts.update_binary;
|
129 | var should_do_source_build = source_build === package_json.name || (source_build === true || source_build === 'true');
|
130 | var no_rollback = gyp.opts.hasOwnProperty('rollback') && gyp.opts.rollback === false;
|
131 | if (should_do_source_build) {
|
132 | log.info('build','requesting source compile');
|
133 | return do_build(gyp,argv,callback);
|
134 | } else {
|
135 | var fallback_to_build = gyp.opts['fallback-to-build'] || gyp.opts.fallback_to_build;
|
136 | var should_do_fallback_build = fallback_to_build === package_json.name || (fallback_to_build === true || fallback_to_build === 'true');
|
137 |
|
138 | if (process.env.npm_config_argv) {
|
139 | var cooked = JSON.parse(process.env.npm_config_argv).cooked;
|
140 | var match = cooked.indexOf("--fallback-to-build");
|
141 | if (match > -1 && cooked.length > match && cooked[match+1] == "false") {
|
142 | should_do_fallback_build = false;
|
143 | log.info('install','Build fallback disabled via npm flag: --fallback-to-build=false');
|
144 | }
|
145 | }
|
146 | var opts;
|
147 | try {
|
148 | opts = versioning.evaluate(package_json, gyp.opts);
|
149 | } catch (err) {
|
150 | return callback(err);
|
151 | }
|
152 | var from = opts.hosted_tarball;
|
153 | var to = opts.module_path;
|
154 | var binary_module = path.join(to,opts.module_name + '.node');
|
155 | if (existsAsync(binary_module,function(found) {
|
156 | if (found && !update_binary) {
|
157 | testbinary(gyp, argv, function(err) {
|
158 | if (err) {
|
159 | console.error('['+package_json.name+'] ' + err.message);
|
160 | log.error("Testing local pre-built binary failed, attempting to re-download");
|
161 | place_binary(from,to,opts,function(err) {
|
162 | if (err) {
|
163 | if (should_do_fallback_build) {
|
164 | print_fallback_error(err,opts,package_json);
|
165 | return do_build(gyp,argv,callback);
|
166 | } else {
|
167 | return callback(err);
|
168 | }
|
169 | } else {
|
170 | console.log('['+package_json.name+'] Success: "' + binary_module + '" is reinstalled via remote');
|
171 | return callback();
|
172 | }
|
173 | });
|
174 | } else {
|
175 | console.log('['+package_json.name+'] Success: "' + binary_module + '" already installed');
|
176 | console.log('Pass --update-binary to reinstall or --build-from-source to recompile');
|
177 | return callback();
|
178 | }
|
179 | });
|
180 | } else {
|
181 | if (!update_binary) log.info('check','checked for "' + binary_module + '" (not found)');
|
182 | place_binary(from,to,opts,function(err) {
|
183 | if (err && should_do_fallback_build) {
|
184 | print_fallback_error(err,opts,package_json);
|
185 | return do_build(gyp,argv,callback);
|
186 | } else if (err) {
|
187 | return callback(err);
|
188 | } else {
|
189 | testbinary(gyp, argv, function(err) {
|
190 | if (err) {
|
191 | if (no_rollback) {
|
192 | return callback(err);
|
193 | }
|
194 | gyp.opts.silent_clean = true;
|
195 | clean(gyp, argv, function(error) {
|
196 | if (error) console.log(error);
|
197 | if (should_do_fallback_build) {
|
198 | console.error('['+package_json.name+'] ' + err.message);
|
199 | log.error("Testing pre-built binary failed, attempting to source compile");
|
200 | return do_build(gyp,argv,callback);
|
201 | } else {
|
202 | return callback(err);
|
203 | }
|
204 | });
|
205 | } else {
|
206 | console.log('['+package_json.name+'] Success: "' + binary_module + '" is installed via remote');
|
207 | return callback();
|
208 | }
|
209 | });
|
210 | }
|
211 | });
|
212 | }
|
213 | }));
|
214 | }
|
215 | }
|