main.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env node
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. // npm/lib/node-path.ts
  30. var node_path_exports = {};
  31. __export(node_path_exports, {
  32. compatibilityDate: () => compatibilityDate,
  33. default: () => node_path_default,
  34. version: () => version
  35. });
  36. module.exports = __toCommonJS(node_path_exports);
  37. // npm/lib/node-platform.ts
  38. var import_fs = __toESM(require("fs"));
  39. var import_os = __toESM(require("os"));
  40. var import_path = __toESM(require("path"));
  41. var knownPackages = {
  42. "darwin arm64 LE": "@cloudflare/workerd-darwin-arm64",
  43. "darwin x64 LE": "@cloudflare/workerd-darwin-64",
  44. "linux arm64 LE": "@cloudflare/workerd-linux-arm64",
  45. "linux x64 LE": "@cloudflare/workerd-linux-64",
  46. "win32 x64 LE": "@cloudflare/workerd-windows-64"
  47. };
  48. var maybeExeExtension = process.platform === "win32" ? ".exe" : "";
  49. function pkgAndSubpathForCurrentPlatform() {
  50. let pkg;
  51. let subpath;
  52. let platformKey = `${process.platform} ${import_os.default.arch()} ${import_os.default.endianness()}`;
  53. if (platformKey in knownPackages) {
  54. pkg = knownPackages[platformKey];
  55. subpath = `bin/workerd${maybeExeExtension}`;
  56. } else {
  57. throw new Error(`Unsupported platform: ${platformKey}`);
  58. }
  59. return { pkg, subpath };
  60. }
  61. function pkgForSomeOtherPlatform() {
  62. const libMain = require.resolve("workerd");
  63. const nodeModulesDirectory = import_path.default.dirname(
  64. import_path.default.dirname(import_path.default.dirname(libMain))
  65. );
  66. if (import_path.default.basename(nodeModulesDirectory) === "node_modules") {
  67. for (const unixKey in knownPackages) {
  68. try {
  69. const pkg = knownPackages[unixKey];
  70. if (import_fs.default.existsSync(import_path.default.join(nodeModulesDirectory, pkg)))
  71. return pkg;
  72. } catch {
  73. }
  74. }
  75. }
  76. return null;
  77. }
  78. function downloadedBinPath(pkg, subpath) {
  79. const libDir = import_path.default.dirname(require.resolve("workerd"));
  80. return import_path.default.join(libDir, `downloaded-${pkg.replace("/", "-")}-${import_path.default.basename(subpath)}${maybeExeExtension}`);
  81. }
  82. function generateBinPath() {
  83. const { pkg, subpath } = pkgAndSubpathForCurrentPlatform();
  84. let binPath2;
  85. try {
  86. binPath2 = require.resolve(`${pkg}/${subpath}`);
  87. } catch (e) {
  88. binPath2 = downloadedBinPath(pkg, subpath);
  89. if (!import_fs.default.existsSync(binPath2)) {
  90. try {
  91. require.resolve(pkg);
  92. } catch {
  93. const otherPkg = pkgForSomeOtherPlatform();
  94. if (otherPkg) {
  95. throw new Error(`
  96. You installed workerd on another platform than the one you're currently using.
  97. This won't work because workerd is written with native code and needs to
  98. install a platform-specific binary executable.
  99. Specifically the "${otherPkg}" package is present but this platform
  100. needs the "${pkg}" package instead. People often get into this
  101. situation by installing workerd on macOS and copying "node_modules"
  102. into a Docker image that runs Linux.
  103. If you are installing with npm, you can try not copying the "node_modules"
  104. directory when you copy the files over, and running "npm ci" or "npm install"
  105. on the destination platform after the copy. Or you could consider using yarn
  106. instead which has built-in support for installing a package on multiple
  107. platforms simultaneously.
  108. If you are installing with yarn, you can try listing both this platform and the
  109. other platform in your ".yarnrc.yml" file using the "supportedArchitectures"
  110. feature: https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
  111. Keep in mind that this means multiple copies of workerd will be present.
  112. `);
  113. }
  114. throw new Error(`The package "${pkg}" could not be found, and is needed by workerd.
  115. If you are installing workerd with npm, make sure that you don't specify the
  116. "--no-optional" flag. The "optionalDependencies" package.json feature is used
  117. by workerd to install the correct binary executable for your current platform.`);
  118. }
  119. throw e;
  120. }
  121. }
  122. let pnpapi;
  123. try {
  124. pnpapi = require("pnpapi");
  125. } catch (e) {
  126. }
  127. if (pnpapi) {
  128. const root = pnpapi.getPackageInformation(pnpapi.topLevel).packageLocation;
  129. const binTargetPath = import_path.default.join(
  130. root,
  131. "node_modules",
  132. ".cache",
  133. "workerd",
  134. `pnpapi-${pkg.replace("/", "-")}-${"1.20230814.1"}-${import_path.default.basename(subpath)}`
  135. );
  136. if (!import_fs.default.existsSync(binTargetPath)) {
  137. import_fs.default.mkdirSync(import_path.default.dirname(binTargetPath), { recursive: true });
  138. import_fs.default.copyFileSync(binPath2, binTargetPath);
  139. import_fs.default.chmodSync(binTargetPath, 493);
  140. }
  141. return { binPath: binTargetPath };
  142. }
  143. return { binPath: binPath2 };
  144. }
  145. // npm/lib/node-path.ts
  146. var { binPath } = generateBinPath();
  147. var node_path_default = binPath;
  148. var compatibilityDate = "2023-08-14";
  149. var version = "1.20230814.1";
  150. // Annotate the CommonJS export names for ESM import in node:
  151. 0 && (module.exports = {
  152. compatibilityDate,
  153. version
  154. });