|
1 | 1 | const defaultChunkSize = 1024 * 1024 // 1MB |
2 | 2 |
|
| 3 | +export function toUrl(text: string): { |
| 4 | + url: string |
| 5 | + fileSize: number |
| 6 | + revoke: () => void |
| 7 | +} { |
| 8 | + // add an extra space to fix https://github.com/nodejs/node/issues/60382 |
| 9 | + const blob = new Blob([text + ' ']) |
| 10 | + const url = URL.createObjectURL(blob) |
| 11 | + return { |
| 12 | + url, |
| 13 | + fileSize: blob.size - 1, // subtract the extra space |
| 14 | + revoke: () => { |
| 15 | + URL.revokeObjectURL(url) |
| 16 | + }, |
| 17 | + } |
| 18 | +} |
| 19 | + |
3 | 20 | export async function* parse( |
4 | 21 | url: string, |
5 | 22 | options: { |
6 | 23 | chunkSize?: number |
7 | | - isUrl?: boolean |
| 24 | + fileSize?: number |
8 | 25 | }, |
9 | | -) { |
| 26 | +): AsyncGenerator<string> { |
10 | 27 | const chunkSize = options.chunkSize ?? defaultChunkSize |
11 | | - const isUrl = options.isUrl ?? true |
12 | | - // See https://github.com/nodejs/node/issues/60382 |
13 | | - let objectURLWorkaround = false |
14 | | - if (!isUrl) { |
15 | | - // transform to a URL object |
16 | | - // See https://github.com/nodejs/node/issues/60382 |
17 | | - objectURLWorkaround = true |
18 | | - url = URL.createObjectURL(new Blob([url + ' '], { type: 'text/plain' })) |
19 | | - } |
20 | | - |
| 28 | + let fileSize = options.fileSize |
21 | 29 | const decoder = new TextDecoder('utf-8') |
| 30 | + |
22 | 31 | let rangeStart = 0 |
23 | | - let fileSize = Infinity |
24 | | - while (rangeStart < fileSize) { |
25 | | - const rangeEnd = rangeStart + chunkSize - 1 + (objectURLWorkaround ? 1 : 0) |
26 | | - const response = await fetch(url, { |
27 | | - headers: { |
28 | | - Range: `bytes=${rangeStart}-${rangeEnd}`, |
29 | | - }, |
30 | | - }) |
31 | | - if (response.status === 416) { |
32 | | - // Requested Range Not Satisfiable |
33 | | - throw new Error( |
34 | | - `Requested range not satisfiable: ${rangeStart}-${rangeEnd}`, |
35 | | - ) |
36 | | - } |
37 | | - if (response.status === 200) { |
38 | | - // Server ignored Range header |
39 | | - throw new Error(`Server did not support range requests.`) |
40 | | - } |
41 | | - if (response.status !== 206) { |
42 | | - throw new Error( |
43 | | - `Failed to fetch chunk: ${response.status} ${response.statusText}`, |
44 | | - ) |
45 | | - } |
46 | | - // Check the content-range header |
47 | | - const contentRange = response.headers.get('content-range') |
48 | | - const contentLength = response.headers.get('content-length') |
49 | | - if (!contentRange || !contentLength) { |
50 | | - throw new Error(`Missing content-range or content-length header.`) |
51 | | - } |
52 | | - const last = contentRange.split('/')[1] |
53 | | - if (last === undefined) { |
54 | | - throw new Error(`Invalid content-range header: ${contentRange}`) |
55 | | - } |
56 | | - fileSize = parseInt(last) |
| 32 | + while (rangeStart < (fileSize ?? Number.POSITIVE_INFINITY)) { |
| 33 | + // Always request one extra byte, due to https://github.com/nodejs/node/issues/60382 |
| 34 | + const extraByte = 1 |
| 35 | + const rangeEnd = rangeStart + chunkSize - 1 + extraByte |
| 36 | + |
| 37 | + const result = await fetchChunk({ url, rangeStart, rangeEnd }) |
57 | 38 |
|
58 | | - // Decode exactly chunkSize bytes or less if it's the last chunk |
59 | | - const bytes = await response.bytes() |
| 39 | + fileSize ??= result.fileSize |
60 | 40 | const bytesToDecode = Math.min(chunkSize, fileSize - rangeStart) |
61 | | - const chunk = decoder.decode(bytes.subarray(0, bytesToDecode)) |
| 41 | + const chunk = decoder.decode(result.bytes.subarray(0, bytesToDecode)) |
| 42 | + |
62 | 43 | yield chunk |
63 | 44 |
|
64 | 45 | rangeStart += chunkSize |
65 | 46 | } |
66 | 47 | } |
| 48 | + |
| 49 | +async function fetchChunk({ url, rangeStart, rangeEnd }: { url: string, rangeStart: number, rangeEnd: number }): Promise<{ |
| 50 | + bytes: Uint8Array |
| 51 | + fileSize: number |
| 52 | +}> { |
| 53 | + const response = await fetch(url, { |
| 54 | + headers: { |
| 55 | + Range: `bytes=${rangeStart}-${rangeEnd}`, |
| 56 | + }, |
| 57 | + }) |
| 58 | + if (response.status === 416) { |
| 59 | + // Requested Range Not Satisfiable |
| 60 | + throw new Error( |
| 61 | + `Requested range not satisfiable: ${rangeStart}-${rangeEnd}`, |
| 62 | + ) |
| 63 | + } |
| 64 | + if (response.status === 200) { |
| 65 | + // Server ignored Range header |
| 66 | + throw new Error(`Server did not support range requests.`) |
| 67 | + } |
| 68 | + if (response.status !== 206) { |
| 69 | + throw new Error( |
| 70 | + `Failed to fetch chunk: ${response.status} ${response.statusText}`, |
| 71 | + ) |
| 72 | + } |
| 73 | + // Check the content-range header |
| 74 | + const contentRange = response.headers.get('content-range') |
| 75 | + const contentLength = response.headers.get('content-length') |
| 76 | + if (!contentRange || !contentLength) { |
| 77 | + throw new Error(`Missing content-range or content-length header.`) |
| 78 | + } |
| 79 | + const last = contentRange.split('/')[1] |
| 80 | + if (last === undefined) { |
| 81 | + throw new Error(`Invalid content-range header: ${contentRange}`) |
| 82 | + } |
| 83 | + const fileSize = parseInt(last) |
| 84 | + if (isNaN(fileSize)) { |
| 85 | + throw new Error(`Invalid file size in content-range header: ${last}`) |
| 86 | + } |
| 87 | + |
| 88 | + // Decode exactly chunkSize bytes or less if it's the last chunk |
| 89 | + const bytes = await response.bytes() |
| 90 | + |
| 91 | + return { bytes, fileSize } |
| 92 | +} |
0 commit comments