| 1 | const { fork } = require("child_process"); |
| 2 | const fs = require("fs"); |
| 3 | const path = require("path"); |
| 4 | |
| 5 | const { ProxyAgent } = require("undici"); |
| 6 | const { rimrafSync } = require("rimraf"); |
| 7 | |
| 8 | const { execCmdSync } = require("../../../scripts/util"); |
| 9 | |
| 10 | /** |
| 11 | * download a file using fetch API |
| 12 | * @param {string} url |
| 13 | * @param {string} outputPath |
| 14 | */ |
| 15 | async function downloadFile(url, outputPath) { |
| 16 | // Use proxy if set in environment variables |
| 17 | const proxy = process.env.https_proxy || process.env.HTTPS_PROXY; |
| 18 | const agent = proxy ? new ProxyAgent(proxy) : undefined; |
| 19 | |
| 20 | const response = await fetch(url, { |
| 21 | redirect: "follow", // Automatically follow redirects |
| 22 | dispatcher: agent, |
| 23 | }); |
| 24 | |
| 25 | if (!response.ok) { |
| 26 | throw new Error(`Failed to download file, status code: ${response.status}`); |
| 27 | } |
| 28 | |
| 29 | // Create output directory if it doesn't exist |
| 30 | const outputDir = path.dirname(outputPath); |
| 31 | if (!fs.existsSync(outputDir)) { |
| 32 | fs.mkdirSync(outputDir, { recursive: true }); |
| 33 | } |
| 34 | |
| 35 | // Get the response as an array buffer and write it to the file |
| 36 | const buffer = await response.arrayBuffer(); |
| 37 | fs.writeFileSync(outputPath, Buffer.from(buffer)); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @param {string} target platform specific target |
| 43 | * @param {string} targetDir the directory to download into |
| 44 | */ |
| 45 | async function downloadSqlite(target, targetDir) { |
| 46 | const downloadUrl = |
| 47 | // node-sqlite3 doesn't have a pre-built binary for win32-arm64 |
| 48 | target === "win32-arm64" |
| 49 | ? "https://continue-server-binaries.s3.us-west-1.amazonaws.com/win32-arm64/node_sqlite3.tar.gz" |
| 50 | : `https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-${ |
| 51 | target |
| 52 | }.tar.gz`; |
| 53 | await downloadFile(downloadUrl, targetDir); |
| 54 | } |
| 55 | |
| 56 | async function installAndCopySqlite(target) { |
| 57 | // Replace the installed with pre-built |
| 58 | console.log("[info] Downloading pre-built sqlite3 binary"); |
| 59 | rimrafSync("../../core/node_modules/sqlite3/build"); |
| 60 | await downloadSqlite(target, "../../core/node_modules/sqlite3/build.tar.gz"); |
| 61 | execCmdSync("cd ../../core/node_modules/sqlite3 && tar -xvzf build.tar.gz"); |
| 62 | fs.unlinkSync("../../core/node_modules/sqlite3/build.tar.gz"); |
| 63 | } |
| 64 | |
| 65 | async function installAndCopyEsbuild(target) { |
| 66 | // Download and unzip esbuild |
| 67 | console.log("[info] Downloading pre-built esbuild binary"); |
| 68 | rimrafSync("node_modules/@esbuild"); |
| 69 | fs.mkdirSync("node_modules/@esbuild", { recursive: true }); |
| 70 | await downloadFile( |
| 71 | `https://continue-server-binaries.s3.us-west-1.amazonaws.com/${target}/esbuild.zip`, |
| 72 | "node_modules/@esbuild/esbuild.zip", |
| 73 | ); |
| 74 | execCmdSync("cd node_modules/@esbuild && unzip esbuild.zip"); |
| 75 | fs.unlinkSync("node_modules/@esbuild/esbuild.zip"); |
| 76 | } |
| 77 | |
| 78 | process.on("message", (msg) => { |
| 79 | const { operation, target } = msg.payload; |
| 80 | if (operation === "sqlite") { |
| 81 | installAndCopySqlite(target) |
| 82 | .then(() => process.send({ done: true })) |
| 83 | .catch((error) => { |
| 84 | console.error(error); // show the error in the parent process |
| 85 | process.send({ error: true }); |
| 86 | }); |
| 87 | } |
| 88 | if (operation === "esbuild") { |
| 89 | installAndCopyEsbuild(target) |
| 90 | .then(() => process.send({ done: true })) |
| 91 | .catch((error) => { |
| 92 | console.error(error); // show the error in the parent process |
| 93 | process.send({ error: true }); |
| 94 | }); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | /** |
| 99 | * @param {string} target the platform to build for |
| 100 | */ |
| 101 | async function copySqlite(target) { |
| 102 | const child = fork(__filename, { stdio: "inherit", cwd: process.cwd() }); |
| 103 | child.send({ |
| 104 | payload: { |
| 105 | operation: "sqlite", |
| 106 | target, |
| 107 | }, |
| 108 | }); |
| 109 | |
| 110 | return new Promise((resolve, reject) => { |
| 111 | child.on("message", (msg) => { |
| 112 | if (msg.error) { |
| 113 | reject(); |
| 114 | } else { |
| 115 | resolve(); |
| 116 | } |
| 117 | }); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param {string} target the platform to build for |
| 123 | */ |
| 124 | async function copyEsbuild(target) { |
| 125 | const child = fork(__filename, { stdio: "inherit", cwd: process.cwd() }); |
| 126 | child.send({ |
| 127 | payload: { |
| 128 | operation: "esbuild", |
| 129 | target, |
| 130 | }, |
| 131 | }); |
| 132 | |
| 133 | return new Promise((resolve, reject) => { |
| 134 | child.on("message", (msg) => { |
| 135 | if (msg.error) { |
| 136 | reject(); |
| 137 | } else { |
| 138 | resolve(); |
| 139 | } |
| 140 | }); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | module.exports = { |
| 145 | downloadSqlite, |
| 146 | copySqlite, |
| 147 | copyEsbuild, |
| 148 | }; |