|
1 | 1 | import { execFile } from 'node:child_process'; |
2 | 2 | import { createHash, randomUUID } from 'node:crypto'; |
| 3 | +import { existsSync } from 'node:fs'; |
3 | 4 | import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
4 | 5 | import { tmpdir } from 'node:os'; |
5 | | -import { extname, join } from 'node:path'; |
| 6 | +import { dirname, extname, join } from 'node:path'; |
6 | 7 | import { promisify } from 'node:util'; |
7 | 8 |
|
8 | 9 | import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; |
@@ -148,19 +149,94 @@ const getS3Client = () => { |
148 | 149 | return s3Client; |
149 | 150 | }; |
150 | 151 |
|
| 152 | +const findWorkspaceBinary = (binaryName: string) => { |
| 153 | + let currentDir = process.cwd(); |
| 154 | + |
| 155 | + while (true) { |
| 156 | + const candidate = join(currentDir, 'node_modules', '.bin', binaryName); |
| 157 | + if (existsSync(candidate)) { |
| 158 | + return candidate; |
| 159 | + } |
| 160 | + |
| 161 | + const parentDir = dirname(currentDir); |
| 162 | + if (parentDir === currentDir) { |
| 163 | + break; |
| 164 | + } |
| 165 | + |
| 166 | + currentDir = parentDir; |
| 167 | + } |
| 168 | + |
| 169 | + return binaryName; |
| 170 | +}; |
| 171 | + |
| 172 | +const getWranglerExecutable = () => { |
| 173 | + const configured = process.env.WRANGLER_BIN?.trim(); |
| 174 | + if (configured) { |
| 175 | + return configured; |
| 176 | + } |
| 177 | + |
| 178 | + return findWorkspaceBinary('wrangler'); |
| 179 | +}; |
| 180 | + |
| 181 | +const toTrimmedOutput = (value: unknown) => { |
| 182 | + if (typeof value !== 'string') { |
| 183 | + return undefined; |
| 184 | + } |
| 185 | + |
| 186 | + const normalized = value.trim(); |
| 187 | + if (!normalized) { |
| 188 | + return undefined; |
| 189 | + } |
| 190 | + |
| 191 | + return normalized.slice(0, 400); |
| 192 | +}; |
| 193 | + |
| 194 | +const runWrangler = async (args: string[]) => { |
| 195 | + const env = getEnv(); |
| 196 | + const executable = getWranglerExecutable(); |
| 197 | + |
| 198 | + try { |
| 199 | + return await execFileAsync(executable, args, { |
| 200 | + env: { |
| 201 | + ...process.env, |
| 202 | + CLOUDFLARE_ACCOUNT_ID: env.r2AccountId, |
| 203 | + }, |
| 204 | + }); |
| 205 | + } catch (error) { |
| 206 | + if (typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT') { |
| 207 | + throw serviceUnavailable('Wrangler CLI is not available for R2 uploads', { |
| 208 | + hint: 'set WRANGLER_BIN or expose node_modules/.bin on PATH', |
| 209 | + executable, |
| 210 | + }); |
| 211 | + } |
| 212 | + |
| 213 | + const details: Record<string, unknown> = { |
| 214 | + hint: 'check the mounted Wrangler profile, CLI login state, and bucket permissions', |
| 215 | + executable, |
| 216 | + reason: error instanceof Error ? error.message : String(error), |
| 217 | + }; |
| 218 | + const stderr = typeof error === 'object' && error !== null && 'stderr' in error ? toTrimmedOutput(error.stderr) : undefined; |
| 219 | + const stdout = typeof error === 'object' && error !== null && 'stdout' in error ? toTrimmedOutput(error.stdout) : undefined; |
| 220 | + if (stderr) { |
| 221 | + details.stderr = stderr; |
| 222 | + } |
| 223 | + if (stdout) { |
| 224 | + details.stdout = stdout; |
| 225 | + } |
| 226 | + |
| 227 | + throw serviceUnavailable('Wrangler CLI failed while accessing R2', { |
| 228 | + ...details, |
| 229 | + }); |
| 230 | + } |
| 231 | +}; |
| 232 | + |
151 | 233 | const getWranglerPublicBaseUrl = async (bucket: string) => { |
152 | 234 | const cached = wranglerDevUrlCache.get(bucket); |
153 | 235 | if (cached) { |
154 | 236 | return cached; |
155 | 237 | } |
156 | 238 |
|
157 | | - const env = getEnv(); |
158 | | - const result = await execFileAsync('wrangler', ['r2', 'bucket', 'dev-url', 'get', bucket], { |
159 | | - env: { |
160 | | - ...process.env, |
161 | | - CLOUDFLARE_ACCOUNT_ID: env.r2AccountId, |
162 | | - }, |
163 | | - }); |
| 239 | + const result = await runWrangler(['r2', 'bucket', 'dev-url', 'get', bucket]); |
164 | 240 |
|
165 | 241 | const output = `${result.stdout}\n${result.stderr}`; |
166 | 242 | const match = output.match(/https:\/\/[^\s]+/); |
@@ -212,16 +288,7 @@ const uploadWithWrangler = async (objectKey: string, body: Buffer, mimeType: str |
212 | 288 |
|
213 | 289 | try { |
214 | 290 | await writeFile(tempFile, body); |
215 | | - await execFileAsync( |
216 | | - 'wrangler', |
217 | | - ['r2', 'object', 'put', `${env.r2Bucket}/${objectKey}`, '--remote', '--file', tempFile, '--content-type', mimeType], |
218 | | - { |
219 | | - env: { |
220 | | - ...process.env, |
221 | | - CLOUDFLARE_ACCOUNT_ID: env.r2AccountId, |
222 | | - }, |
223 | | - }, |
224 | | - ); |
| 291 | + await runWrangler(['r2', 'object', 'put', `${env.r2Bucket}/${objectKey}`, '--remote', '--file', tempFile, '--content-type', mimeType]); |
225 | 292 | } finally { |
226 | 293 | await rm(tempDir, { recursive: true, force: true }); |
227 | 294 | } |
|
0 commit comments