Skip to content

Commit edef5b6

Browse files
committed
Scripts: Use NodeJS-native fetch
Since we're using NodeJS 18 now, we can use the node-native fetch() implementation instead of the one from node-fetch. See if this resolves issues around downloads just hanging. Signed-off-by: Mark Yen <[email protected]>
1 parent fb0312e commit edef5b6

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

scripts/lib/download.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import crypto from 'crypto';
77
import fs from 'fs';
88
import os from 'os';
99
import path from 'path';
10-
11-
import fetch from 'node-fetch';
10+
import stream from 'stream';
1211

1312
import { simpleSpawn } from 'scripts/simple_process';
1413

@@ -33,7 +32,7 @@ export type ArchiveDownloadOptions = DownloadOptions & {
3332
async function fetchWithRetry(url: string) {
3433
while (true) {
3534
try {
36-
return await fetch(url);
35+
return await fetch(url, { redirect: 'follow' });
3736
} catch (ex: any) {
3837
if (ex && ex.errno === 'EAI_AGAIN') {
3938
console.log(`Recoverable error downloading ${ url }, retrying...`);
@@ -76,14 +75,15 @@ export async function download(url: string, destPath: string, options: DownloadO
7675
if (!response.ok) {
7776
throw new Error(`Error downloading ${ url }: ${ response.statusText }`);
7877
}
78+
if (!response.body) {
79+
throw new Error(`Error downloading ${ url }: did not receive response body`);
80+
}
7981
const tempPath = `${ destPath }.download`;
8082

8183
try {
8284
const file = fs.createWriteStream(tempPath);
83-
const promise = new Promise(resolve => file.on('finish', resolve));
8485

85-
response.body.pipe(file);
86-
await promise;
86+
await response.body.pipeTo(stream.Writable.toWeb(file));
8787

8888
if (expectedChecksum) {
8989
const actualChecksum = await getChecksumForFile(tempPath, checksumAlgorithm);

0 commit comments

Comments
 (0)