Skip to content

Commit 8ec58ee

Browse files
fix script
1 parent c6b1906 commit 8ec58ee

23 files changed

Lines changed: 874 additions & 0 deletions

File tree

public/demos/animated-text-renderer-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["AnimatedTextRendererExample.wasm"], [], [

public/demos/bunnies-stress-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["BunniesStressExample.wasm"], [], [

public/demos/button-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["ButtonExample.wasm"], [], [

public/demos/custom-material-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["CustomMaterialExample.wasm"], [], [

public/demos/custom-tile-map-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["CustomTileMapExample.wasm"], [], [

public/demos/gamepad-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["GamepadExample.wasm"], [], [

public/demos/kanban-board-example/main.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function run() {
9595
updateLoader(68, "Loading resources…");
9696
const response = await fetch(new URL("./ada-resource-manifest.json", import.meta.url));
9797
const manifest = response.ok ? await response.json() : [];
98+
installResourceFetchResolver(manifest);
9899
const root = new Map();
99100

100101
let loadedResources = 0;
@@ -126,6 +127,43 @@ async function run() {
126127
];
127128
}
128129

130+
function installResourceFetchResolver(manifest) {
131+
const originalFetch = globalThis.fetch?.bind(globalThis);
132+
if (!originalFetch || globalThis.__adaResourceFetchResolverInstalled) return;
133+
134+
const resourceURLsByPath = new Map();
135+
for (const entry of manifest) {
136+
if (!entry || typeof entry.path !== "string" || typeof entry.url !== "string") continue;
137+
const resourceURL = new URL(entry.url, import.meta.url).href;
138+
const relativePath = entry.path.replace(/^\/+/, "");
139+
resourceURLsByPath.set(entry.path, resourceURL);
140+
resourceURLsByPath.set(relativePath, resourceURL);
141+
resourceURLsByPath.set(`/${relativePath}`, resourceURL);
142+
resourceURLsByPath.set(`./${relativePath}`, resourceURL);
143+
}
144+
145+
const resolveResourceURL = (input) => {
146+
const rawURL = typeof input === "string" || input instanceof URL ? String(input) : input?.url;
147+
if (!rawURL) return null;
148+
if (resourceURLsByPath.has(rawURL)) return resourceURLsByPath.get(rawURL);
149+
const pathname = new URL(rawURL, import.meta.url).pathname.replace(/^\/+/, "");
150+
if (resourceURLsByPath.has(pathname)) return resourceURLsByPath.get(pathname);
151+
for (const [resourcePath, resourceURL] of resourceURLsByPath) {
152+
if (pathname.endsWith(resourcePath.replace(/^\/+/, ""))) return resourceURL;
153+
}
154+
return null;
155+
};
156+
157+
globalThis.fetch = (input, init) => {
158+
const resourceURL = resolveResourceURL(input);
159+
if (resourceURL) {
160+
return originalFetch(resourceURL, init);
161+
}
162+
return originalFetch(input, init);
163+
};
164+
globalThis.__adaResourceFetchResolverInstalled = true;
165+
}
166+
129167
const resourcePreopens = await makeResourcePreopenDirectories();
130168
updateLoader(82, "Configuring WASI…");
131169
const wasi = new WASI(["KanbanBoardExample.wasm"], [], [

0 commit comments

Comments
 (0)