Commit 660b2e2
authored
fix: prevent silent exit on premature download close (#195)
## Problem
Downloads that get truncated or aborted mid-stream (e.g. a dropped
connection during a Docker build) could cause `pkg-fetch` to **exit
silently with an incomplete file** instead of failing loudly.
The old `downloadUrl` piped `res.body` (from `node-fetch`) into the
write stream and only watched the **destination** via
`stream.finished(ws, ...)`. A premature close errors on the **source**,
which never propagated to the destination's `finished` callback — so the
promise settled as success and a partial/corrupt asset was left behind.
## Fix
Stream the response body through `stream/promises` `pipeline(body, ws)`,
which propagates errors from **both** source and destination. A
premature close now rejects loudly and the partial `.downloading`
tempfile is cleaned up.
While here, migrate from `node-fetch` + `https-proxy-agent` to
**undici**:
- `undici` is what Node's native `fetch` is built on (maintained by the
Node team).
- Net **fewer** dependencies: removes `node-fetch`, `https-proxy-agent`,
and `@types/node-fetch`; adds one runtime dep (`undici`, self-typed).
- `ProxyAgent` preserves existing `HTTPS_PROXY`/`HTTP_PROXY` support —
native `fetch` cannot proxy on its own, so undici is required
regardless.
- `@types/node` bumped to `^20` for `Readable.fromWeb`.
## Verification
- `yarn build` ✅ `yarn lint` ✅
- Functional test against a local HTTP server (no Node build):
- **Full download** → correct SHA-256, tempfile cleaned. ✅
- **Truncated download** (Content-Length claims full size, socket
destroyed mid-stream) → now **rejects** (`TypeError: terminated`) and
removes the `.downloading` tempfile, instead of silently resolving. ✅
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent 1a783a2 commit 660b2e2
3 files changed
Lines changed: 76 additions & 100 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | | - | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
33 | 37 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
38 | 42 | | |
39 | | - | |
40 | | - | |
41 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
42 | 47 | | |
43 | | - | |
44 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
45 | 51 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
65 | 62 | | |
66 | 63 | | |
67 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
68 | 83 | | |
69 | 84 | | |
70 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | 21 | | |
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
| 25 | + | |
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | | - | |
32 | | - | |
| 30 | + | |
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
287 | 287 | | |
288 | 288 | | |
289 | 289 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | 290 | | |
299 | 291 | | |
300 | 292 | | |
301 | 293 | | |
302 | 294 | | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
307 | 301 | | |
308 | 302 | | |
309 | 303 | | |
| |||
447 | 441 | | |
448 | 442 | | |
449 | 443 | | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | 444 | | |
458 | 445 | | |
459 | 446 | | |
| |||
652 | 639 | | |
653 | 640 | | |
654 | 641 | | |
655 | | - | |
656 | | - | |
657 | | - | |
658 | | - | |
659 | | - | |
660 | 642 | | |
661 | 643 | | |
662 | 644 | | |
| |||
976 | 958 | | |
977 | 959 | | |
978 | 960 | | |
979 | | - | |
980 | | - | |
981 | | - | |
982 | | - | |
983 | | - | |
984 | | - | |
985 | | - | |
986 | 961 | | |
987 | 962 | | |
988 | 963 | | |
| |||
1314 | 1289 | | |
1315 | 1290 | | |
1316 | 1291 | | |
1317 | | - | |
1318 | | - | |
1319 | | - | |
1320 | | - | |
1321 | | - | |
1322 | 1292 | | |
1323 | 1293 | | |
1324 | 1294 | | |
| |||
1883 | 1853 | | |
1884 | 1854 | | |
1885 | 1855 | | |
1886 | | - | |
1887 | | - | |
1888 | | - | |
1889 | | - | |
1890 | | - | |
1891 | | - | |
1892 | | - | |
1893 | | - | |
1894 | | - | |
1895 | 1856 | | |
1896 | 1857 | | |
1897 | 1858 | | |
| |||
2243 | 2204 | | |
2244 | 2205 | | |
2245 | 2206 | | |
2246 | | - | |
2247 | | - | |
2248 | | - | |
2249 | | - | |
2250 | | - | |
2251 | | - | |
2252 | | - | |
2253 | | - | |
2254 | 2207 | | |
2255 | 2208 | | |
2256 | 2209 | | |
| |||
2968 | 2921 | | |
2969 | 2922 | | |
2970 | 2923 | | |
2971 | | - | |
| 2924 | + | |
2972 | 2925 | | |
2973 | 2926 | | |
2974 | 2927 | | |
| |||
3070 | 3023 | | |
3071 | 3024 | | |
3072 | 3025 | | |
3073 | | - | |
| 3026 | + | |
3074 | 3027 | | |
3075 | 3028 | | |
3076 | 3029 | | |
| |||
4297 | 4250 | | |
4298 | 4251 | | |
4299 | 4252 | | |
| 4253 | + | |
| 4254 | + | |
| 4255 | + | |
| 4256 | + | |
| 4257 | + | |
| 4258 | + | |
| 4259 | + | |
| 4260 | + | |
| 4261 | + | |
| 4262 | + | |
4300 | 4263 | | |
4301 | 4264 | | |
4302 | 4265 | | |
| |||
0 commit comments