Skip to content

Commit 6bdb325

Browse files
committed
Make monkey-patching safer in web exports
Also adds missing copyright statements.
1 parent 473ab37 commit 6bdb325

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

dist/web_assets/boscaweb.main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/***************************************************/
2+
/* Part of Bosca Ceoil Blue */
3+
/* Copyright (c) 2024 Yuri Sizov and contributors */
4+
/* Provided under MIT */
5+
/***************************************************/
6+
17
const BOSCAWEB_STATE_INITIAL = 0;
28
const BOSCAWEB_STATE_PROGRESS = 1;
39
const BOSCAWEB_STATE_READY = 2;
Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
1+
/***************************************************/
2+
/* Part of Bosca Ceoil Blue */
3+
/* Copyright (c) 2024 Yuri Sizov and contributors */
4+
/* Provided under MIT */
5+
/***************************************************/
6+
17
// 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);
2234
}
2335

24-
loadedBytes += value.byteLength
25-
bosca.setLoadingProgress(resource, loadedBytes);
26-
controller.enqueue(value);
36+
reader.releaseLock();
37+
controller.close();
2738
}
28-
29-
reader.releaseLock();
30-
controller.close();
39+
},
40+
{
41+
status: response.status,
42+
statusText: response.statusText
3143
}
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]);
3649
}
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;
4252
}
43-
44-
return forwardedResponse;
45-
}
53+
})(window);

0 commit comments

Comments
 (0)