Skip to content

Commit 88f5f20

Browse files
committed
TCLOUD-4780: Use preexisting file as redirect file template
This avoids problems with creating temporary files.
1 parent 7f0f6b0 commit 88f5f20

File tree

2 files changed

+22
-51
lines changed

2 files changed

+22
-51
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!doctype html><title>?</title>

.github/workflows/scripts/generate_redirects.mjs

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import crypto from 'node:crypto';
21
import fs from 'node:fs';
3-
import os from 'node:os';
42
import path from 'node:path';
53
import { spawn } from 'node:child_process';
64

@@ -155,63 +153,35 @@ const createOrUpdateS3ObjectAsync = (dryRun, bucket, prefix, subPath, metadata,
155153
* @param {Map<string, string>} redirectsByLocation
156154
*/
157155
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');
169158

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`;
173164

174-
let allSettled = Promise.resolve();
165+
// Remove leading slash from location
166+
const subPath = locationIndexHtml.startsWith('/') ? locationIndexHtml.slice(1) : locationIndexHtml;
175167

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 = {};
182170

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
185173

186-
// Build metadata headers
187-
const metadata = {};
174+
// Add redirect location header
175+
metadata[`redirect-location-${i}`] = redirect.redirect;
188176

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;
213180
}
214181
});
182+
183+
// Create or update the S3 object
184+
yield createOrUpdateS3ObjectAsync(dryRun, bucket, prefix, subPath, metadata, newFileTemplate);
215185
}
216186
}
217187

0 commit comments

Comments
 (0)