wrangler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env node
  2. const { spawn } = require("child_process");
  3. const path = require("path");
  4. const fs = require("fs");
  5. const os = require("os");
  6. const MIN_NODE_VERSION = "16.13.0";
  7. const debug =
  8. process.env["WRANGLER_LOG"] === "debug"
  9. ? (...args) => console.log(...args)
  10. : () => {};
  11. let wranglerProcess;
  12. /**
  13. * Executes ../wrangler-dist/cli.js
  14. */
  15. function runWrangler() {
  16. if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) {
  17. // Note Volta and nvm are also recommended in the official docs:
  18. // https://developers.cloudflare.com/workers/get-started/guide#2-install-the-workers-cli
  19. console.error(
  20. `Wrangler requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}. Please update your version of Node.js.
  21. Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.`
  22. );
  23. process.exitCode = 1;
  24. return;
  25. }
  26. let pathToCACerts = process.env.NODE_EXTRA_CA_CERTS;
  27. if (pathToCACerts) {
  28. // TODO:
  29. // - should we log a warning here?
  30. // - maybe we can generate a certificate that concatenates with ours?
  31. //
  32. // I do think it'll be rare that someone wants to add a cert AND
  33. // use Cloudflare WARP, but let's wait till the situation actually
  34. // arises before we do anything about it
  35. } else {
  36. const osTempDir = os.tmpdir();
  37. const certDir = path.join(osTempDir, "wrangler-cert");
  38. const certPath = path.join(certDir, "Cloudflare_CA.pem");
  39. // copy cert to the system temp dir if needed
  40. if (!fs.existsSync(certPath)) {
  41. fs.mkdirSync(certDir, { recursive: true });
  42. fs.writeFileSync(
  43. certPath,
  44. fs.readFileSync(path.join(__dirname, "../Cloudflare_CA.pem"), "utf-8")
  45. );
  46. }
  47. pathToCACerts = certPath;
  48. }
  49. return spawn(
  50. process.execPath,
  51. [
  52. "--no-warnings",
  53. "--experimental-vm-modules",
  54. ...process.execArgv,
  55. path.join(__dirname, "../wrangler-dist/cli.js"),
  56. ...process.argv.slice(2),
  57. ],
  58. {
  59. stdio: ["inherit", "inherit", "inherit", "ipc"],
  60. env: {
  61. ...process.env,
  62. NODE_EXTRA_CA_CERTS: pathToCACerts,
  63. },
  64. }
  65. )
  66. .on("exit", (code) =>
  67. process.exit(code === undefined || code === null ? 0 : code)
  68. )
  69. .on("message", (message) => {
  70. if (process.send) {
  71. process.send(message);
  72. }
  73. });
  74. }
  75. // semiver implementation via https://github.com/lukeed/semiver/blob/ae7eebe6053c96be63032b14fb0b68e2553fcac4/src/index.js
  76. /**
  77. MIT License
  78. Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com)
  79. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  80. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  81. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  82. */
  83. var fn = new Intl.Collator(0, { numeric: 1 }).compare;
  84. function semiver(a, b, bool) {
  85. a = a.split(".");
  86. b = b.split(".");
  87. return (
  88. fn(a[0], b[0]) ||
  89. fn(a[1], b[1]) ||
  90. ((b[2] = b.slice(2).join(".")),
  91. (bool = /[.-]/.test((a[2] = a.slice(2).join(".")))),
  92. bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1)
  93. );
  94. }
  95. // end semiver implementation
  96. if (module === require.main) {
  97. wranglerProcess = runWrangler();
  98. process.on("SIGINT", () => {
  99. wranglerProcess && wranglerProcess.kill();
  100. });
  101. process.on("SIGTERM", () => {
  102. wranglerProcess && wranglerProcess.kill();
  103. });
  104. }