install.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. module.exports = exports = install;
  3. exports.usage = 'Attempts to install pre-built binary for module';
  4. const fs = require('fs');
  5. const path = require('path');
  6. const log = require('npmlog');
  7. const existsAsync = fs.exists || path.exists;
  8. const versioning = require('./util/versioning.js');
  9. const napi = require('./util/napi.js');
  10. const makeDir = require('make-dir');
  11. // for fetching binaries
  12. const fetch = require('node-fetch');
  13. const tar = require('tar');
  14. let npgVersion = 'unknown';
  15. try {
  16. // Read own package.json to get the current node-pre-pyp version.
  17. const ownPackageJSON = fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8');
  18. npgVersion = JSON.parse(ownPackageJSON).version;
  19. } catch (e) {
  20. // do nothing
  21. }
  22. function place_binary(uri, targetDir, opts, callback) {
  23. log.http('GET', uri);
  24. // Try getting version info from the currently running npm.
  25. const envVersionInfo = process.env.npm_config_user_agent ||
  26. 'node ' + process.version;
  27. const sanitized = uri.replace('+', '%2B');
  28. const requestOpts = {
  29. uri: sanitized,
  30. headers: {
  31. 'User-Agent': 'node-pre-gyp (v' + npgVersion + ', ' + envVersionInfo + ')'
  32. },
  33. follow_max: 10
  34. };
  35. if (opts.cafile) {
  36. try {
  37. requestOpts.ca = fs.readFileSync(opts.cafile);
  38. } catch (e) {
  39. return callback(e);
  40. }
  41. } else if (opts.ca) {
  42. requestOpts.ca = opts.ca;
  43. }
  44. const proxyUrl = opts.proxy ||
  45. process.env.http_proxy ||
  46. process.env.HTTP_PROXY ||
  47. process.env.npm_config_proxy;
  48. let agent;
  49. if (proxyUrl) {
  50. const ProxyAgent = require('https-proxy-agent');
  51. agent = new ProxyAgent(proxyUrl);
  52. log.http('download', 'proxy agent configured using: "%s"', proxyUrl);
  53. }
  54. fetch(sanitized, { agent })
  55. .then((res) => {
  56. if (!res.ok) {
  57. throw new Error(`response status ${res.status} ${res.statusText} on ${sanitized}`);
  58. }
  59. const dataStream = res.body;
  60. return new Promise((resolve, reject) => {
  61. let extractions = 0;
  62. const countExtractions = (entry) => {
  63. extractions += 1;
  64. log.info('install', 'unpacking %s', entry.path);
  65. };
  66. dataStream.pipe(extract(targetDir, countExtractions))
  67. .on('error', (e) => {
  68. reject(e);
  69. });
  70. dataStream.on('end', () => {
  71. resolve(`extracted file count: ${extractions}`);
  72. });
  73. dataStream.on('error', (e) => {
  74. reject(e);
  75. });
  76. });
  77. })
  78. .then((text) => {
  79. log.info(text);
  80. callback();
  81. })
  82. .catch((e) => {
  83. log.error(`install ${e.message}`);
  84. callback(e);
  85. });
  86. }
  87. function extract(to, onentry) {
  88. return tar.extract({
  89. cwd: to,
  90. strip: 1,
  91. onentry
  92. });
  93. }
  94. function extract_from_local(from, targetDir, callback) {
  95. if (!fs.existsSync(from)) {
  96. return callback(new Error('Cannot find file ' + from));
  97. }
  98. log.info('Found local file to extract from ' + from);
  99. // extract helpers
  100. let extractCount = 0;
  101. function countExtractions(entry) {
  102. extractCount += 1;
  103. log.info('install', 'unpacking ' + entry.path);
  104. }
  105. function afterExtract(err) {
  106. if (err) return callback(err);
  107. if (extractCount === 0) {
  108. return callback(new Error('There was a fatal problem while extracting the tarball'));
  109. }
  110. log.info('tarball', 'done parsing tarball');
  111. callback();
  112. }
  113. fs.createReadStream(from).pipe(extract(targetDir, countExtractions))
  114. .on('close', afterExtract)
  115. .on('error', afterExtract);
  116. }
  117. function do_build(gyp, argv, callback) {
  118. const args = ['rebuild'].concat(argv);
  119. gyp.todo.push({ name: 'build', args: args });
  120. process.nextTick(callback);
  121. }
  122. function print_fallback_error(err, opts, package_json) {
  123. const fallback_message = ' (falling back to source compile with node-gyp)';
  124. let full_message = '';
  125. if (err.statusCode !== undefined) {
  126. // If we got a network response it but failed to download
  127. // it means remote binaries are not available, so let's try to help
  128. // the user/developer with the info to debug why
  129. full_message = 'Pre-built binaries not found for ' + package_json.name + '@' + package_json.version;
  130. full_message += ' and ' + opts.runtime + '@' + (opts.target || process.versions.node) + ' (' + opts.node_abi + ' ABI, ' + opts.libc + ')';
  131. full_message += fallback_message;
  132. log.warn('Tried to download(' + err.statusCode + '): ' + opts.hosted_tarball);
  133. log.warn(full_message);
  134. log.http(err.message);
  135. } else {
  136. // If we do not have a statusCode that means an unexpected error
  137. // happened and prevented an http response, so we output the exact error
  138. full_message = 'Pre-built binaries not installable for ' + package_json.name + '@' + package_json.version;
  139. full_message += ' and ' + opts.runtime + '@' + (opts.target || process.versions.node) + ' (' + opts.node_abi + ' ABI, ' + opts.libc + ')';
  140. full_message += fallback_message;
  141. log.warn(full_message);
  142. log.warn('Hit error ' + err.message);
  143. }
  144. }
  145. //
  146. // install
  147. //
  148. function install(gyp, argv, callback) {
  149. const package_json = gyp.package_json;
  150. const napi_build_version = napi.get_napi_build_version_from_command_args(argv);
  151. const source_build = gyp.opts['build-from-source'] || gyp.opts.build_from_source;
  152. const update_binary = gyp.opts['update-binary'] || gyp.opts.update_binary;
  153. const should_do_source_build = source_build === package_json.name || (source_build === true || source_build === 'true');
  154. if (should_do_source_build) {
  155. log.info('build', 'requesting source compile');
  156. return do_build(gyp, argv, callback);
  157. } else {
  158. const fallback_to_build = gyp.opts['fallback-to-build'] || gyp.opts.fallback_to_build;
  159. let should_do_fallback_build = fallback_to_build === package_json.name || (fallback_to_build === true || fallback_to_build === 'true');
  160. // but allow override from npm
  161. if (process.env.npm_config_argv) {
  162. const cooked = JSON.parse(process.env.npm_config_argv).cooked;
  163. const match = cooked.indexOf('--fallback-to-build');
  164. if (match > -1 && cooked.length > match && cooked[match + 1] === 'false') {
  165. should_do_fallback_build = false;
  166. log.info('install', 'Build fallback disabled via npm flag: --fallback-to-build=false');
  167. }
  168. }
  169. let opts;
  170. try {
  171. opts = versioning.evaluate(package_json, gyp.opts, napi_build_version);
  172. } catch (err) {
  173. return callback(err);
  174. }
  175. opts.ca = gyp.opts.ca;
  176. opts.cafile = gyp.opts.cafile;
  177. const from = opts.hosted_tarball;
  178. const to = opts.module_path;
  179. const binary_module = path.join(to, opts.module_name + '.node');
  180. existsAsync(binary_module, (found) => {
  181. if (!update_binary) {
  182. if (found) {
  183. console.log('[' + package_json.name + '] Success: "' + binary_module + '" already installed');
  184. console.log('Pass --update-binary to reinstall or --build-from-source to recompile');
  185. return callback();
  186. }
  187. log.info('check', 'checked for "' + binary_module + '" (not found)');
  188. }
  189. makeDir(to).then(() => {
  190. const fileName = from.startsWith('file://') && from.slice('file://'.length);
  191. if (fileName) {
  192. extract_from_local(fileName, to, after_place);
  193. } else {
  194. place_binary(from, to, opts, after_place);
  195. }
  196. }).catch((err) => {
  197. after_place(err);
  198. });
  199. function after_place(err) {
  200. if (err && should_do_fallback_build) {
  201. print_fallback_error(err, opts, package_json);
  202. return do_build(gyp, argv, callback);
  203. } else if (err) {
  204. return callback(err);
  205. } else {
  206. console.log('[' + package_json.name + '] Success: "' + binary_module + '" is installed via remote');
  207. return callback();
  208. }
  209. }
  210. });
  211. }
  212. }