install.js 4.2 KB

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