|
1 | | -import crypto from 'node:crypto'; |
2 | 1 | import fs from 'node:fs'; |
3 | | -import os from 'node:os'; |
4 | 2 | import path from 'node:path'; |
5 | 3 | import { spawn } from 'node:child_process'; |
6 | 4 |
|
@@ -155,63 +153,35 @@ const createOrUpdateS3ObjectAsync = (dryRun, bucket, prefix, subPath, metadata, |
155 | 153 | * @param {Map<string, string>} redirectsByLocation |
156 | 154 | */ |
157 | 155 | function* generateRedirectObjectsAsync(dryRun, bucket, prefix, redirectsByLocation) { |
158 | | - // Create empty index.html content for the redirect |
159 | | - const htmlContent = `<!DOCTYPE html> |
160 | | -<html> |
161 | | -<head> |
162 | | - <meta charset="utf-8"> |
163 | | - <title>Redirecting...</title> |
164 | | -</head> |
165 | | -<body> |
166 | | - <p>Redirecting...</p> |
167 | | -</body> |
168 | | -</html>`; |
| 156 | + // create path to empty template |
| 157 | + const newFileTemplate = path.join(import.meta.dirname, 'empty.html'); |
169 | 158 |
|
170 | | - // Write temporary file |
171 | | - const newFileTemplate = path.join(os.tmpdir(), `redirect-${crypto.randomBytes(16).toString('hex')}.html`); |
172 | | - fs.writeFileSync(newFileTemplate, htmlContent); |
| 159 | + for (const [location, locationRedirects] of redirectsByLocation) { |
| 160 | + // Create S3 object path by appending index.html to location |
| 161 | + const locationIndexHtml = location.endsWith('/') |
| 162 | + ? `${location}index.html` |
| 163 | + : `${location}/index.html`; |
173 | 164 |
|
174 | | - let allSettled = Promise.resolve(); |
| 165 | + // Remove leading slash from location |
| 166 | + const subPath = locationIndexHtml.startsWith('/') ? locationIndexHtml.slice(1) : locationIndexHtml; |
175 | 167 |
|
176 | | - try { |
177 | | - for (const [location, locationRedirects] of redirectsByLocation) { |
178 | | - // Create S3 object path by appending index.html to location |
179 | | - const locationIndexHtml = location.endsWith('/') |
180 | | - ? `${location}index.html` |
181 | | - : `${location}/index.html`; |
| 168 | + // Build metadata headers |
| 169 | + const metadata = {}; |
182 | 170 |
|
183 | | - // Remove leading slash from location |
184 | | - const subPath = locationIndexHtml.startsWith('/') ? locationIndexHtml.slice(1) : locationIndexHtml; |
| 171 | + locationRedirects.forEach((redirect, index) => { |
| 172 | + const i = index + 1; // 1-based indexing as requested |
185 | 173 |
|
186 | | - // Build metadata headers |
187 | | - const metadata = {}; |
| 174 | + // Add redirect location header |
| 175 | + metadata[`redirect-location-${i}`] = redirect.redirect; |
188 | 176 |
|
189 | | - locationRedirects.forEach((redirect, index) => { |
190 | | - const i = index + 1; // 1-based indexing as requested |
191 | | - |
192 | | - // Add redirect location header |
193 | | - metadata[`redirect-location-${i}`] = redirect.redirect; |
194 | | - |
195 | | - // Add pattern header if it exists |
196 | | - if (redirect.pattern !== undefined) { |
197 | | - metadata[`redirect-pattern-${i}`] = redirect.pattern; |
198 | | - } |
199 | | - }); |
200 | | - |
201 | | - // Create or update the S3 object |
202 | | - const task = createOrUpdateS3ObjectAsync(dryRun, bucket, prefix, subPath, metadata, newFileTemplate); |
203 | | - yield task; |
204 | | - // throw away task success/failure info, just keep task settled status |
205 | | - const taskSettled = task.then(() => { }, () => { }); |
206 | | - allSettled = allSettled.then(() => taskSettled); |
207 | | - } |
208 | | - } finally { |
209 | | - allSettled.then(() => { |
210 | | - // Clean up temporary file |
211 | | - if (fs.existsSync(newFileTemplate)) { |
212 | | - fs.unlinkSync(newFileTemplate); |
| 177 | + // Add pattern header if it exists |
| 178 | + if (redirect.pattern !== undefined) { |
| 179 | + metadata[`redirect-pattern-${i}`] = redirect.pattern; |
213 | 180 | } |
214 | 181 | }); |
| 182 | + |
| 183 | + // Create or update the S3 object |
| 184 | + yield createOrUpdateS3ObjectAsync(dryRun, bucket, prefix, subPath, metadata, newFileTemplate); |
215 | 185 | } |
216 | 186 | } |
217 | 187 |
|
|
0 commit comments