-
Notifications
You must be signed in to change notification settings - Fork 91
feat: add support for serving assets from the routing layer #768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/cloudflare": minor | ||
| --- | ||
|
|
||
| Add an asset resolver to support `run_worker_first=true` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/cloudflare/src/api/overrides/asset-resolver/index.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { isUserWorkerFirst } from "./index.js"; | ||
|
|
||
| describe("isUserWorkerFirst", () => { | ||
| test("run_worker_first = false", () => { | ||
| expect(isUserWorkerFirst(false, "/test")).toBe(false); | ||
| expect(isUserWorkerFirst(false, "/")).toBe(false); | ||
| }); | ||
|
|
||
| test("run_worker_first is undefined", () => { | ||
| expect(isUserWorkerFirst(undefined, "/test")).toBe(false); | ||
| expect(isUserWorkerFirst(undefined, "/")).toBe(false); | ||
| }); | ||
|
|
||
| test("run_worker_first = true", () => { | ||
| expect(isUserWorkerFirst(true, "/test")).toBe(true); | ||
| expect(isUserWorkerFirst(true, "/")).toBe(true); | ||
| }); | ||
|
|
||
| describe("run_worker_first is an array", () => { | ||
| test("positive string match", () => { | ||
| expect(isUserWorkerFirst(["/test.ext"], "/test.ext")).toBe(true); | ||
| expect(isUserWorkerFirst(["/a", "/b", "/test.ext"], "/test.ext")).toBe(true); | ||
| expect(isUserWorkerFirst(["/a", "/b", "/test.ext"], "/test")).toBe(false); | ||
| expect(isUserWorkerFirst(["/before/test.ext"], "/test.ext")).toBe(false); | ||
| expect(isUserWorkerFirst(["/test.ext/after"], "/test.ext")).toBe(false); | ||
| }); | ||
|
|
||
| test("negative string match", () => { | ||
| expect(isUserWorkerFirst(["!/test.ext"], "/test.ext")).toBe(false); | ||
| expect(isUserWorkerFirst(["!/a", "!/b", "!/test.ext"], "/test.ext")).toBe(false); | ||
| }); | ||
|
|
||
| test("positive patterns", () => { | ||
| expect(isUserWorkerFirst(["/images/*"], "/images/pic.jpg")).toBe(true); | ||
| expect(isUserWorkerFirst(["/images/*"], "/other/pic.jpg")).toBe(false); | ||
| }); | ||
|
|
||
| test("negative patterns", () => { | ||
| expect(isUserWorkerFirst(["/*", "!/images/*"], "/images/pic.jpg")).toBe(false); | ||
| expect(isUserWorkerFirst(["/*", "!/images/*"], "/index.html")).toBe(true); | ||
| expect(isUserWorkerFirst(["!/images/*", "/*"], "/images/pic.jpg")).toBe(false); | ||
| expect(isUserWorkerFirst(["!/images/*", "/*"], "/index.html")).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
96 changes: 96 additions & 0 deletions
96
packages/cloudflare/src/api/overrides/asset-resolver/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import type { InternalEvent, InternalResult } from "@opennextjs/aws/types/open-next"; | ||
| import type { AssetResolver } from "@opennextjs/aws/types/overrides"; | ||
|
|
||
| import { getCloudflareContext } from "../../cloudflare-context.js"; | ||
|
|
||
| /** | ||
| * Serves assets when `run_worker_first` is set to true. | ||
| * | ||
| * When `run_worker_first` is `false`, the assets are served directly bypassing Next routing. | ||
| * | ||
| * When it is `true`, assets are served from the routing layer. It should be used when assets | ||
| * should be behind the middleware or when skew protection is enabled. | ||
| * | ||
| * See https://developers.cloudflare.com/workers/static-assets/binding/#run_worker_first | ||
| */ | ||
| const resolver: AssetResolver = { | ||
| name: "cloudflare-asset-resolver", | ||
| async maybeGetAssetResult(event: InternalEvent) { | ||
| const { ASSETS } = getCloudflareContext().env; | ||
|
|
||
| if (!ASSETS || !isUserWorkerFirst(globalThis.__ASSETS_RUN_WORKER_FIRST__, event.rawPath)) { | ||
| // Only handle assets when the user worker runs first for the path | ||
| return undefined; | ||
| } | ||
|
|
||
| const { method, headers } = event; | ||
|
|
||
| if (method !== "GET" && method != "HEAD") { | ||
| return undefined; | ||
| } | ||
|
|
||
| const url = new URL(event.rawPath, "https://assets.local"); | ||
| const response = await ASSETS.fetch(url, { | ||
| headers, | ||
| method, | ||
| }); | ||
|
|
||
| if (response.status === 404) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| type: "core", | ||
| statusCode: response.status, | ||
| headers: Object.fromEntries(response.headers.entries()), | ||
| // Workers and Node types differ. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| body: response.body || (new ReadableStream() as any), | ||
| isBase64Encoded: false, | ||
| } satisfies InternalResult; | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * @param runWorkerFirst `run_worker_first` config | ||
| * @param pathname pathname of the request | ||
| * @returns Whether the user worker runs first | ||
| */ | ||
| export function isUserWorkerFirst(runWorkerFirst: boolean | string[] | undefined, pathname: string): boolean { | ||
| if (!Array.isArray(runWorkerFirst)) { | ||
| return runWorkerFirst ?? false; | ||
| } | ||
|
|
||
| let hasPositiveMatch = false; | ||
|
|
||
| for (let rule of runWorkerFirst) { | ||
| let isPositiveRule = true; | ||
|
|
||
| if (rule.startsWith("!")) { | ||
| rule = rule.slice(1); | ||
| isPositiveRule = false; | ||
| } else if (hasPositiveMatch) { | ||
| // Do not look for more positive rules once we have a match | ||
| continue; | ||
| } | ||
|
|
||
| // - Escapes special characters | ||
| // - Replaces * with .* | ||
| const match = new RegExp(`^${rule.replace(/([[\]().*+?^$|{}\\])/g, "\\$1").replace("\\*", ".*")}$`).test( | ||
| pathname | ||
| ); | ||
|
|
||
| if (match) { | ||
| if (isPositiveRule) { | ||
| hasPositiveMatch = true; | ||
| } else { | ||
| // Exit early when there is a negative match | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return hasPositiveMatch; | ||
| } | ||
|
|
||
| export default resolver; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { getFlagValue, getWranglerConfigFlag, getWranglerEnvironmentFlag } from "./run-wrangler.js"; | ||
|
|
||
| describe("getFlagValue", () => { | ||
| test("long", () => { | ||
| expect(getFlagValue(["--flag", "value"], "--flag", "-f")).toEqual("value"); | ||
| expect(getFlagValue(["--flag=value"], "--flag", "-f")).toEqual("value"); | ||
| }); | ||
|
|
||
| test("short", () => { | ||
| expect(getFlagValue(["-f", "value"], "--flag", "-f")).toEqual("value"); | ||
| expect(getFlagValue(["-f=value"], "--flag", "-f")).toEqual("value"); | ||
| }); | ||
|
|
||
| test("not found", () => { | ||
| expect(getFlagValue(["--some", "value"], "--other", "-o")).toBeUndefined(); | ||
| expect(getFlagValue(["--some=value"], "--other", "-o")).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getWranglerEnvironmentFlag", () => { | ||
| test("long", () => { | ||
| expect(getWranglerEnvironmentFlag(["--env", "value"])).toEqual("value"); | ||
| expect(getWranglerEnvironmentFlag(["--env=value"])).toEqual("value"); | ||
| }); | ||
|
|
||
| test("short", () => { | ||
| expect(getWranglerEnvironmentFlag(["-e", "value"])).toEqual("value"); | ||
| expect(getWranglerEnvironmentFlag(["-e=value"])).toEqual("value"); | ||
| }); | ||
|
|
||
| test("not found", () => { | ||
| expect(getWranglerEnvironmentFlag(["--some", "value"])).toBeUndefined(); | ||
| expect(getWranglerEnvironmentFlag(["--some=value"])).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getWranglerConfigFlag", () => { | ||
| test("long", () => { | ||
| expect(getWranglerConfigFlag(["--config", "path/to/wrangler.jsonc"])).toEqual("path/to/wrangler.jsonc"); | ||
| expect(getWranglerConfigFlag(["--config=path/to/wrangler.jsonc"])).toEqual("path/to/wrangler.jsonc"); | ||
| }); | ||
|
|
||
| test("short", () => { | ||
| expect(getWranglerConfigFlag(["-c", "path/to/wrangler.jsonc"])).toEqual("path/to/wrangler.jsonc"); | ||
| expect(getWranglerConfigFlag(["-c=path/to/wrangler.jsonc"])).toEqual("path/to/wrangler.jsonc"); | ||
| }); | ||
|
|
||
| test("not found", () => { | ||
| expect(getWranglerConfigFlag(["--some", "value"])).toBeUndefined(); | ||
| expect(getWranglerConfigFlag(["--some=value"])).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.