|
| 1 | +/***************************************************/ |
| 2 | +/* Part of Bosca Ceoil Blue */ |
| 3 | +/* Copyright (c) 2024 Yuri Sizov and contributors */ |
| 4 | +/* Provided under MIT */ |
| 5 | +/***************************************************/ |
| 6 | + |
1 | 7 | // Monkey-patch fetch to intercept Godot loading its bits and pieces.
|
2 |
| -const _orig_fetch = window.fetch; |
3 |
| -window.fetch = async function(resource, options) { |
4 |
| - if (!(resource in BOSCA_FILE_SIZES)) { |
5 |
| - return await _orig_fetch(resource, options); |
6 |
| - } |
7 |
| - |
8 |
| - const response = await _orig_fetch(resource, options); |
9 |
| - const innerStream = new ReadableStream( |
10 |
| - { |
11 |
| - async start(controller) { |
12 |
| - const totalBytes = BOSCA_FILE_SIZES[resource]; |
13 |
| - let loadedBytes = 0; |
14 |
| - |
15 |
| - const reader = response.body.getReader(); |
16 |
| - |
17 |
| - while (true) { |
18 |
| - const { value, done } = await reader.read(); |
19 |
| - if (done) { |
20 |
| - bosca.setLoadingProgress(resource, (totalBytes - loadedBytes)); |
21 |
| - break; |
| 8 | +(function(window){ |
| 9 | + const _orig_fetch = window.fetch; |
| 10 | + window.fetch = async function(resource, options) { |
| 11 | + if (!(resource in BOSCA_FILE_SIZES)) { |
| 12 | + return await _orig_fetch(resource, options); |
| 13 | + } |
| 14 | + |
| 15 | + const response = await _orig_fetch(resource, options); |
| 16 | + const innerStream = new ReadableStream( |
| 17 | + { |
| 18 | + async start(controller) { |
| 19 | + const totalBytes = BOSCA_FILE_SIZES[resource]; |
| 20 | + let loadedBytes = 0; |
| 21 | + |
| 22 | + const reader = response.body.getReader(); |
| 23 | + |
| 24 | + while (true) { |
| 25 | + const { value, done } = await reader.read(); |
| 26 | + if (done) { |
| 27 | + bosca.setLoadingProgress(resource, (totalBytes - loadedBytes)); |
| 28 | + break; |
| 29 | + } |
| 30 | + |
| 31 | + loadedBytes += value.byteLength |
| 32 | + bosca.setLoadingProgress(resource, loadedBytes); |
| 33 | + controller.enqueue(value); |
22 | 34 | }
|
23 | 35 |
|
24 |
| - loadedBytes += value.byteLength |
25 |
| - bosca.setLoadingProgress(resource, loadedBytes); |
26 |
| - controller.enqueue(value); |
| 36 | + reader.releaseLock(); |
| 37 | + controller.close(); |
27 | 38 | }
|
28 |
| - |
29 |
| - reader.releaseLock(); |
30 |
| - controller.close(); |
| 39 | + }, |
| 40 | + { |
| 41 | + status: response.status, |
| 42 | + statusText: response.statusText |
31 | 43 | }
|
32 |
| - }, |
33 |
| - { |
34 |
| - status: response.status, |
35 |
| - statusText: response.statusText |
| 44 | + ) |
| 45 | + |
| 46 | + const forwardedResponse = new Response(innerStream); |
| 47 | + for (const pair of response.headers.entries()) { |
| 48 | + forwardedResponse.headers.set(pair[0], pair[1]); |
36 | 49 | }
|
37 |
| - ) |
38 |
| - |
39 |
| - const forwardedResponse = new Response(innerStream); |
40 |
| - for (const pair of response.headers.entries()) { |
41 |
| - forwardedResponse.headers.set(pair[0], pair[1]); |
| 50 | + |
| 51 | + return forwardedResponse; |
42 | 52 | }
|
43 |
| - |
44 |
| - return forwardedResponse; |
45 |
| -} |
| 53 | +})(window); |
0 commit comments