| 1 | const { fork } = require("child_process"); |
| 2 | const fs = require("fs"); |
| 3 | const https = require("https"); |
| 4 | const path = require("path"); |
| 5 | |
| 6 | const { rimrafSync } = require("rimraf"); |
| 7 | |
| 8 | const { execCmdSync } = require("../../../scripts/util"); |
| 9 | |
| 10 | /** |
| 11 | * download a file using nodejs http |
| 12 | * @param {string} url |
| 13 | * @param {string} outputPath |
| 14 | * @param {number} maxRedirects |
| 15 | */ |
| 16 | async function downloadFile(url, outputPath, maxRedirects = 5) { |
| 17 | return new Promise((resolve, reject) => { |
| 18 | const downloadWithRedirects = (currentUrl, redirectCount = 0) => { |
| 19 | if (redirectCount > maxRedirects) { |
| 20 | return reject(new Error(`Too many redirects (${maxRedirects})`)); |
| 21 | } |
| 22 | |
| 23 | const request = https.get(currentUrl, (response) => { |
| 24 | if ( |
| 25 | response.statusCode >= 300 && |
| 26 | response.statusCode < 400 && |
| 27 | response.headers.location |
| 28 | ) { |
| 29 | return downloadWithRedirects( |
| 30 | response.headers.location, |
| 31 | redirectCount + 1, |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | if (response.statusCode !== 200) { |
| 36 | return reject( |
| 37 | new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`), |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | const outputDir = path.dirname(outputPath); |
| 42 | if (!fs.existsSync(outputDir)) { |
| 43 | fs.mkdirSync(outputDir, { recursive: true }); |
| 44 | } |
| 45 | |
| 46 | const writeStream = fs.createWriteStream(outputPath); |
| 47 | |
| 48 | const totalSize = parseInt(response.headers["content-length"], 10); |
| 49 | let downloadedSize = 0; |
| 50 | |
| 51 | response.on("data", (chunk) => { |
| 52 | downloadedSize += chunk.length; |
| 53 | if (totalSize) { |
| 54 | const percent = ((downloadedSize / totalSize) * 100).toFixed(1); |
| 55 | process.stdout.write( |
| 56 | `\rDownloading: ${percent}% (${downloadedSize}/${totalSize} bytes)`, |
| 57 | ); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | response.pipe(writeStream); |
| 62 | |
| 63 | writeStream.on("finish", () => { |
| 64 | console.log(`\nDownload completed: ${outputPath}`); |
| 65 | resolve(outputPath); |
| 66 | }); |
| 67 | |
| 68 | writeStream.on("error", reject); |
| 69 | response.on("error", reject); |
| 70 | }); |
| 71 | |
| 72 | request.on("error", reject); |
| 73 | request.setTimeout(30000, () => { |
| 74 | request.destroy(); |
| 75 | reject(new Error("Request timeout")); |
| 76 | }); |
| 77 | }; |
| 78 | |
| 79 | downloadWithRedirects(url); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * |
| 85 | * @param {string} target platform specific target |
| 86 | * @param {string} targetDir the directory to download into |
| 87 | */ |
| 88 | async function downloadSqlite(target, targetDir) { |
| 89 | const downloadUrl = |
| 90 | // node-sqlite3 doesn't have a pre-built binary for win32-arm64 |
| 91 | target === "win32-arm64" |
| 92 | ? "https://continue-server-binaries.s3.us-west-1.amazonaws.com/win32-arm64/node_sqlite3.tar.gz" |
| 93 | : `https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.7/sqlite3-v5.1.7-napi-v6-${ |
| 94 | target |
| 95 | }.tar.gz`; |
| 96 | await downloadFile(downloadUrl, targetDir); |
| 97 | } |
| 98 | |
| 99 | async function installAndCopySqlite(target) { |
| 100 | // Replace the installed with pre-built |
| 101 | console.log("[info] Downloading pre-built sqlite3 binary"); |
| 102 | rimrafSync("../../core/node_modules/sqlite3/build"); |
| 103 | await downloadSqlite(target, "../../core/node_modules/sqlite3/build.tar.gz"); |
| 104 | execCmdSync("cd ../../core/node_modules/sqlite3 && tar -xvzf build.tar.gz"); |
| 105 | fs.unlinkSync("../../core/node_modules/sqlite3/build.tar.gz"); |
| 106 | } |
| 107 | |
| 108 | async function installAndCopyEsbuild(target) { |
| 109 | // Download and unzip esbuild |
| 110 | console.log("[info] Downloading pre-built esbuild binary"); |
| 111 | rimrafSync("node_modules/@esbuild"); |
| 112 | fs.mkdirSync("node_modules/@esbuild", { recursive: true }); |
| 113 | await downloadFile( |
| 114 | `https://continue-server-binaries.s3.us-west-1.amazonaws.com/${target}/esbuild.zip`, |
| 115 | "node_modules/@esbuild/esbuild.zip", |
| 116 | ); |
| 117 | execCmdSync("cd node_modules/@esbuild && unzip esbuild.zip"); |
| 118 | fs.unlinkSync("node_modules/@esbuild/esbuild.zip"); |
| 119 | } |
| 120 | |
| 121 | process.on("message", (msg) => { |
| 122 | const { operation, target } = msg.payload; |
| 123 | if (operation === "sqlite") { |
| 124 | installAndCopySqlite(target) |
| 125 | .then(() => process.send({ done: true })) |
| 126 | .catch((error) => { |
| 127 | console.error(error); // show the error in the parent process |
| 128 | process.send({ error: true }); |
| 129 | }); |
| 130 | } |
| 131 | if (operation === "esbuild") { |
| 132 | installAndCopyEsbuild(target) |
| 133 | .then(() => process.send({ done: true })) |
| 134 | .catch((error) => { |
| 135 | console.error(error); // show the error in the parent process |
| 136 | process.send({ error: true }); |
| 137 | }); |
| 138 | } |
| 139 | }); |
| 140 | |
| 141 | /** |
| 142 | * @param {string} target the platform to build for |
| 143 | */ |
| 144 | async function copySqlite(target) { |
| 145 | const child = fork(__filename, { stdio: "inherit", cwd: process.cwd() }); |
| 146 | child.send({ |
| 147 | payload: { |
| 148 | operation: "sqlite", |
| 149 | target, |
| 150 | }, |
| 151 | }); |
| 152 | |
| 153 | return new Promise((resolve, reject) => { |
| 154 | child.on("message", (msg) => { |
| 155 | if (msg.error) { |
| 156 | reject(); |
| 157 | } else { |
| 158 | resolve(); |
| 159 | } |
| 160 | }); |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @param {string} target the platform to build for |
| 166 | */ |
| 167 | async function copyEsbuild(target) { |
| 168 | const child = fork(__filename, { stdio: "inherit", cwd: process.cwd() }); |
| 169 | child.send({ |
| 170 | payload: { |
| 171 | operation: "esbuild", |
| 172 | target, |
| 173 | }, |
| 174 | }); |
| 175 | |
| 176 | return new Promise((resolve, reject) => { |
| 177 | child.on("message", (msg) => { |
| 178 | if (msg.error) { |
| 179 | reject(); |
| 180 | } else { |
| 181 | resolve(); |
| 182 | } |
| 183 | }); |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | module.exports = { |
| 188 | downloadSqlite, |
| 189 | copySqlite, |
| 190 | copyEsbuild, |
| 191 | }; |