Skip to content

Commit d8d94cc

Browse files
committed
fix: absolute URLs for the Workers ASSETS binding
env.ASSETS.fetch('/path') throws 'Invalid URL' in the Workers runtime; the binding requires a full URL (host is ignored, routing is path-only). Test doubles extract the basename instead of assuming the path prefix.
1 parent fbadcbc commit d8d94cc

8 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎dist/src/index.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/src/maps.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
import { type CompiledMap, type SystemCode } from "interscript-ts";
1212
export interface MapAssets {
13-
fetch(url: string): Promise<Response>;
13+
fetch(input: RequestInfo): Promise<Response>;
1414
}
1515
export declare function bundledSystemCodes(): readonly SystemCode[];
1616
export declare function detectableSystemCodes(): readonly SystemCode[];

‎dist/src/maps.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export async function loadMap(assets, code) {
3131
const cached = cache.get(code);
3232
if (cached)
3333
return cached;
34-
const response = await assets.fetch(`/maps/${code}.json`);
34+
// The Workers ASSETS binding requires an absolute URL; the host is
35+
// ignored (routing is path-only within the binding).
36+
const response = await assets.fetch(new Request(`https://assets.internal/maps/${code}.json`));
3537
if (!response.ok) {
3638
throw new MapNotFoundError(code);
3739
}

‎dist/test/parity.test.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import { app } from "../src/server.js";
99
const env = {
1010
ASSETS: {
1111
fetch: async (url) => {
12-
const code = url.replace("/maps/", "").replace(".json", "");
12+
const path = typeof url === "string" ? url : url.url;
13+
const name = path.substring(path.lastIndexOf("/") + 1);
14+
const code = name.replace(".json", "");
1315
try {
1416
return new Response(readFileSync(`maps/${code}.json`), { status: 200 });
1517
}

‎dist/test/server.test.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { app } from "../src/server.js";
1010
// deployer ships as static assets, served from the local filesystem.
1111
const assets = {
1212
fetch: async (url) => {
13-
const code = url.replace("/maps/", "").replace(".json", "");
13+
const path = typeof url === "string" ? url : url.url;
14+
const name = path.substring(path.lastIndexOf("/") + 1);
15+
const code = name.replace(".json", "");
1416
try {
1517
const body = readFileSync(`maps/${code}.json`);
1618
return new Response(body, { status: 200 });

‎src/maps.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const addressable = systemCodes.filter((code) => !LIBRARIES.has(code))
2929
const detectable = addressable.filter((code) => !ML_BACKED.has(code))
3030

3131
export interface MapAssets {
32-
fetch(url: string): Promise<Response>
32+
fetch(input: RequestInfo): Promise<Response>
3333
}
3434

3535
const cache = new Map<SystemCode, CompiledMap>()
@@ -45,7 +45,11 @@ export function detectableSystemCodes(): readonly SystemCode[] {
4545
export async function loadMap(assets: MapAssets, code: SystemCode): Promise<CompiledMap> {
4646
const cached = cache.get(code)
4747
if (cached) return cached
48-
const response = await assets.fetch(`/maps/${code}.json`)
48+
// The Workers ASSETS binding requires an absolute URL; the host is
49+
// ignored (routing is path-only within the binding).
50+
const response = await assets.fetch(
51+
new Request(`https://assets.internal/maps/${code}.json`),
52+
)
4953
if (!response.ok) {
5054
throw new MapNotFoundError(code)
5155
}

‎test/parity.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import type { Env } from "../src/resolvers.js"
1010

1111
const env: Env = {
1212
ASSETS: {
13-
fetch: async (url: string): Promise<Response> => {
14-
const code = url.replace("/maps/", "").replace(".json", "")
13+
fetch: async (url: RequestInfo): Promise<Response> => {
14+
const path = typeof url === "string" ? url : url.url
15+
const name = path.substring(path.lastIndexOf("/") + 1)
16+
const code = name.replace(".json", "")
1517
try {
1618
return new Response(readFileSync(`maps/${code}.json`), { status: 200 })
1719
} catch {

‎test/server.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import { app } from "../src/server.js"
1010
// Test double of the Workers ASSETS binding: the same maps directory the
1111
// deployer ships as static assets, served from the local filesystem.
1212
const assets = {
13-
fetch: async (url: string): Promise<Response> => {
14-
const code = url.replace("/maps/", "").replace(".json", "")
13+
fetch: async (url: RequestInfo): Promise<Response> => {
14+
const path = typeof url === "string" ? url : url.url
15+
const name = path.substring(path.lastIndexOf("/") + 1)
16+
const code = name.replace(".json", "")
1517
try {
1618
const body = readFileSync(`maps/${code}.json`)
1719
return new Response(body, { status: 200 })

0 commit comments

Comments
 (0)