Skip to content

Commit

Permalink
Allow Workers Assets to be mounted to a path
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Dec 12, 2024
1 parent 75be0d5 commit 1b5762a
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 142 deletions.
7 changes: 7 additions & 0 deletions .changeset/early-baboons-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": minor
---

feat: allow routing to Workers with Assets on any HTTP route, not just the root. For example, `example.com/blog/*` can now be used to serve assets.
These assets will be served as though the assets directly were mounted to the root.
For example, if you have `asset.directory = "./public/"` then `./public/blog/logo.png` will be available at `example.com/blog/logo.png`. Assets outside of directories which match the configured HTTP routes can still be accessed with the [Assets binding](https://developers.cloudflare.com/workers/static-assets/binding/#binding) or with a [Service binding](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/) to this Worker.
50 changes: 50 additions & 0 deletions packages/workers-shared/asset-worker/tests/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { vi } from "vitest";
import { applyConfigurationDefaults } from "../src/configuration";
import { handleRequest } from "../src/handler";
import type { AssetConfig } from "../../utils/types";

Expand Down Expand Up @@ -100,4 +101,53 @@ describe("[Asset Worker] `handleRequest`", () => {

expect(response.status).toBe(200);
});

it("cannot fetch assets outside of configured path", async () => {
const assets: Record<string, string> = {
"/blog/test.html": "aaaaaaaaaa",
"/blog/index.html": "bbbbbbbbbb",
"/index.html": "cccccccccc",
"/test.html": "dddddddddd",
};

// Attempt to path traverse down to the root /test within asset-server
let response = await handleRequest(
new Request("https://example.com/blog/../test"),
applyConfigurationDefaults({}),
async (pathname: string) => {
if (pathname.startsWith("/blog/")) {
// our route
return assets[pathname] ?? null;
} else {
return null;
}
},
async (_: string) => ({
readableStream: new ReadableStream(),
contentType: "text/html",
})
);

expect(response.status).toBe(404);

// Attempt to path traverse down to the root /test within asset-server
response = await handleRequest(
new Request("https://example.com/blog/%2E%2E/test"),
applyConfigurationDefaults({}),
async (pathname: string) => {
if (pathname.startsWith("/blog/")) {
// our route
return assets[pathname] ?? null;
} else {
return null;
}
},
async (_: string) => ({
readableStream: new ReadableStream(),
contentType: "text/html",
})
);

expect(response.status).toBe(404);
});
});
6 changes: 5 additions & 1 deletion packages/workers-shared/asset-worker/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types/experimental", "vitest/globals"]
"types": [
"@cloudflare/workers-types/experimental",
"vitest/globals",
"@cloudflare/vitest-pool-workers"
]
},
"include": ["**/*.ts"]
}
Loading

0 comments on commit 1b5762a

Please sign in to comment.