Skip to content

Commit a4ac2cf

Browse files
authored
Merge pull request #847 from CorrelAid/new_build_method
make serve-static more flexible
2 parents f1e749c + f7bcecd commit a4ac2cf

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

.github/workflows/cd.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ jobs:
4545
run: npm run build
4646

4747
- name: Run tests on static build
48+
env:
49+
PUBLIC_API_URL: https://cms.correlaid.org
50+
PUBLIC_PRERENDER: ALL
51+
PUBLIC_ADAPTER: STATIC
52+
PUBLIC_PREVIEW: FALSE
53+
PUBLIC_ON_CMS_ERROR: FAIL
54+
BUILD_DIR: 'build/'
4855
run: npm run e2e
4956

5057
- uses: actions/upload-artifact@v4

dl_external_imgs.js

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,6 @@ const REPLACEMENT_ASSET_ID = 'a525ce03-7e70-446f-9eff-1edd222aa002';
2929

3030
const startTime = Date.now();
3131

32-
function formatDuration(ms) {
33-
const seconds = Math.floor(ms / 1000);
34-
const minutes = Math.floor(seconds / 60);
35-
const hours = Math.floor(minutes / 60);
36-
37-
const remainingMinutes = minutes % 60;
38-
const remainingSeconds = seconds % 60;
39-
40-
const parts = [];
41-
if (hours > 0) parts.push(`${hours}h`);
42-
if (remainingMinutes > 0) parts.push(`${remainingMinutes}m`);
43-
parts.push(`${remainingSeconds}s`);
44-
45-
return parts.join(' ');
46-
}
47-
4832
async function postbuild() {
4933
console.log('Starting replacement of files from external sources');
5034

@@ -104,7 +88,6 @@ async function postbuild() {
10488
}
10589

10690
const duration = Date.now() - startTime;
107-
console.log(`\nCompleted in ${formatDuration(duration)}`);
10891
console.log(`Files processed: ${completedFiles}/${targetFiles.length}`);
10992
process.exit();
11093
} catch (error) {
@@ -194,14 +177,13 @@ async function retryOnTimeout(url, maxRetries = 5, initialBackoff = 2000) {
194177
const response = await fetch(url, {
195178
signal: controller.signal,
196179
headers: {
197-
Connection: 'close',
198180
'User-Agent': getRandomUserAgent(),
199181
'Accept-Encoding': 'gzip,deflate',
200182
'Cache-Control': 'no-cache',
201183
Pragma: 'no-cache',
202184
Accept: '*/*',
203185
},
204-
keepalive: false,
186+
keepalive: true,
205187
});
206188

207189
clearTimeout(timeout);
@@ -267,37 +249,34 @@ async function retryOnTimeout(url, maxRetries = 5, initialBackoff = 2000) {
267249
}
268250

269251
async function downloadFile(url, downloadPath) {
270-
let fileStream;
271252
try {
272253
const response = await retryOnTimeout(url);
273254
await mkdir(path.dirname(downloadPath), {recursive: true});
274-
fileStream = createWriteStream(downloadPath);
255+
const fileStream = createWriteStream(downloadPath);
275256

276257
return new Promise((resolve, reject) => {
277-
const stream = response.body.pipe(fileStream);
258+
const stream = response.body;
278259

279-
// Add error handler for the response body
280-
response.body.on('error', (error) => {
281-
fileStream.close();
282-
unlink(downloadPath).catch(console.error);
283-
reject(error);
260+
// Handle backpressure
261+
stream.on('data', (chunk) => {
262+
if (!fileStream.write(chunk)) {
263+
stream.pause();
264+
}
284265
});
285266

286-
stream.on('finish', () => {
287-
fileStream.close();
288-
resolve();
267+
fileStream.on('drain', () => {
268+
stream.resume();
289269
});
290270

291-
stream.on('error', (error) => {
292-
fileStream.close();
293-
unlink(downloadPath).catch(console.error);
294-
reject(error);
271+
stream.on('end', () => {
272+
fileStream.end();
273+
resolve();
295274
});
275+
276+
stream.on('error', reject);
277+
fileStream.on('error', reject);
296278
});
297279
} catch (error) {
298-
if (fileStream) {
299-
fileStream.close();
300-
}
301280
await unlink(downloadPath).catch(() => {});
302281
throw error;
303282
}

0 commit comments

Comments
 (0)