install.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const fs_1 = require("fs");
  13. const https_1 = require("https");
  14. const path_1 = require("path");
  15. const stream_1 = require("stream");
  16. const versions_1 = require("./versions");
  17. /**
  18. * Post-install script. Downloads the binary for the current Node.js version
  19. * from the Gitub releases page, if it's available.
  20. */
  21. const builtPlatforms = {
  22. win32: 'windows-latest',
  23. linux: 'ubuntu-latest',
  24. darwin: 'macos-latest',
  25. };
  26. const { version } = require('../../package.json');
  27. const repoUrl = process.env.BLAKE3_REPO_URL || 'https://github.com/connor4312/blake3';
  28. const targets = require('../../targets.json');
  29. const bindingPath = path_1.join(__dirname, '..', 'native.node');
  30. function install() {
  31. return __awaiter(this, void 0, void 0, function* () {
  32. const current = versions_1.parseVersion(process.version);
  33. const api = getBestAbiVersion(current);
  34. if (!api) {
  35. console.error('Your Node.js release is out of LTS and BLAKE3 bindings are not built for it. Update it to use native BLAKE3 bindings.');
  36. return fallback();
  37. }
  38. const platform = builtPlatforms[process.platform];
  39. if (!platform) {
  40. console.error(`BLAKE3 bindings are not built for your platform (${process.platform})`);
  41. return fallback();
  42. }
  43. console.log(`Retrieving native BLAKE3 bindings for Node ${api.nodeVersion} on ${process.platform}...`);
  44. yield download(`${repoUrl}/releases/download/v${version}/${platform}-${api.abiVersion}.node`);
  45. try {
  46. require(bindingPath);
  47. }
  48. catch (e) {
  49. console.log(`Error trying to import bindings: ${e.message}`);
  50. return fallback();
  51. }
  52. useNativeImport();
  53. console.log('BLAKE3 bindings retrieved');
  54. });
  55. }
  56. function getBestAbiVersion(current) {
  57. for (const targetVersion of Object.keys(targets)) {
  58. const parsed = versions_1.parseVersion(targetVersion);
  59. if (versions_1.compareVersion(current, parsed) >= 0) {
  60. return { nodeVersion: targetVersion, abiVersion: targets[targetVersion] };
  61. }
  62. }
  63. return undefined;
  64. }
  65. function fallback() {
  66. console.error('BLAKE3 will use slower WebAssembly bindings when required in Node.js');
  67. }
  68. function download(url) {
  69. return __awaiter(this, void 0, void 0, function* () {
  70. return new Promise(resolve => {
  71. const onError = (err) => {
  72. console.error(`Could not download binding from ${url}: ${err.stack || err.message}`);
  73. resolve(false);
  74. };
  75. const req = https_1.get(url, res => {
  76. if (res.headers.location) {
  77. resolve(download(res.headers.location));
  78. return;
  79. }
  80. if (!res.statusCode || res.statusCode >= 300) {
  81. console.error(`Unexpected ${res.statusCode} from ${url}`);
  82. resolve(false);
  83. return;
  84. }
  85. stream_1.pipeline(res, fs_1.createWriteStream(bindingPath), err => (err ? onError(err) : resolve(true)));
  86. });
  87. req.on('error', onError);
  88. });
  89. });
  90. }
  91. function useNativeImport() {
  92. const indexFile = path_1.join(__dirname, '..', 'index.js');
  93. const contents = fs_1.readFileSync(indexFile, 'utf-8');
  94. fs_1.writeFileSync(indexFile, contents.replace('"./node"', '"./node-native"'));
  95. }
  96. install().catch(err => {
  97. console.error(`There was an uncaught error installing native bindings: ${err.stack}`);
  98. fallback();
  99. });
  100. //# sourceMappingURL=install.js.map