Skip to content

Commit ddfbc69

Browse files
committed
build: abort download task on abnormal status code
1 parent cae2810 commit ddfbc69

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

data/resources.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,12 +870,12 @@ const resources = [
870870
emoji: 'https://unicode.org/Public/17.0.0/ucd/emoji/emoji-data.txt',
871871
// Emoji_Keycap_Sequence, Emoji_Flag_Sequence, Emoji_Modifier_Sequence
872872
'emoji-sequences':
873-
'https://www.unicode.org/Public/17.0.0/emoji/emoji-sequences.txt',
873+
'https://unicode.org/Public/17.0.0/emoji/emoji-sequences.txt',
874874
// Emoji_ZWJ_Sequence
875875
'emoji-zwj-sequences':
876-
'https://www.unicode.org/Public/17.0.0/emoji/emoji-zwj-sequences.txt',
876+
'https://unicode.org/Public/17.0.0/emoji/emoji-zwj-sequences.txt',
877877
// Emoji_Test (not an official property)
878-
'emoji-test': 'https://www.unicode.org/Public/17.0.0/emoji/emoji-test.txt',
878+
'emoji-test': 'https://unicode.org/Public/17.0.0/emoji/emoji-test.txt',
879879
},
880880
];
881881

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"regenerate": "^1.4.2",
4747
"unicode-loose-match": "^2.8.0",
4848
"unicode-property-aliases": "^2.1.0",
49-
"unicode-property-value-aliases": "^3.9.0",
50-
"when": "^3.7.8"
49+
"unicode-property-value-aliases": "^3.9.0"
5150
}
5251
}

scripts/download.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const fs = require('fs');
4-
const guard = require('when/guard');
54
const path = require('path');
65
const { Readable } = require('stream');
76
const { finished } = require('stream/promises');
@@ -17,12 +16,35 @@ const download = async function(url, version, type) {
1716
);
1817
console.log(' ', url, '→', path.basename(file));
1918
//console.log(`curl ${url} > data/${path.basename(file)};`);
19+
if (!res.ok) {
20+
throw new Error(`Failed to download ${url}: ${res.status} ${res.statusText}`);
21+
}
2022
return finished(
2123
Readable.fromWeb(res.body).pipe(fs.createWriteStream(file))
2224
);
2325
};
26+
27+
async function parallelLimit(tasks, limit) {
28+
const results = new Array(tasks.length);
29+
const iterator = tasks.entries();
30+
31+
async function worker() {
32+
for (const [index, task] of iterator) {
33+
results[index] = await task();
34+
}
35+
}
36+
37+
const workers = new Array(limit).fill(null).map(() => worker());
38+
await Promise.all(workers);
39+
return results;
40+
}
41+
42+
const tasks = [];
43+
const addDownloadTask = (url, version, type) => tasks.push(() => download(url, version, type));
44+
2445
// Limit maximum parallelism to something reasonable
25-
const guardedDownload = guard(guard.n(PARALLEL_REQUEST_LIMIT), download);
46+
parallelLimit(tasks, PARALLEL_REQUEST_LIMIT);
47+
const guardedDownload = () => parallelLimit(tasks, PARALLEL_REQUEST_LIMIT);
2648

2749
console.log('Downloading resources…');
2850

@@ -53,10 +75,12 @@ const TYPES = [
5375

5476
for (const resource of resources) {
5577
const version = resource.version;
56-
guardedDownload(resource.main, version, 'database');
78+
addDownloadTask(resource.main, version, 'database');
5779
for (const type of TYPES) {
5880
if (resource[type]) {
59-
guardedDownload(resource[type], version, type);
81+
addDownloadTask(resource[type], version, type);
6082
}
6183
}
6284
}
85+
86+
guardedDownload();

0 commit comments

Comments
 (0)