Skip to content

Commit

Permalink
feat: improve KV detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Jun 7, 2023
1 parent 2e14d6c commit 462b260
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
24 changes: 24 additions & 0 deletions src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ export function isPublicAssetURL(id = '') {
return false
}
const keyStartsWith = (literalObj, needle) => Object.keys(literalObj).some((k) => k.startsWith(needle));
const findKey = (literalObj, needle) => Object.keys(literalObj).find((k) => k.startsWith(needle));
export const isInKv = (id = "") => {
if (
assets[id] ||
keyStartsWith(assets, id) ||
keyStartsWith(publicAssetBases, id)
) {
return true
}
return false
}
export const getKVMatch = (id = "") => {
const assetMatch = findKey(assets, id)
if(assetMatch) return [assetMatch, assets[assetMatch]]
const publicAssetMatch = findKey(publicAssetBases, id)
if(publicAssetMatch) return [publicAssetMatch, publicAssetBases[publicAssetMatch]]
return null
}
export function getPublicAssetMeta(id = '') {
for (const base in publicAssetBases) {
if (id.startsWith(base)) { return publicAssetBases[base] }
Expand Down
23 changes: 6 additions & 17 deletions src/runtime/entries/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { requestHasBody } from "../utils";
import { nitroApp } from "#internal/nitro/app";
import { useRuntimeConfig } from "#internal/nitro";
import {
getKVMatch,
getPublicAssetMeta,
publicAssetBases,
isInKv,
} from "#internal/nitro/virtual/public-assets";
import assets from "#internal/nitro/virtual/public-assets-data";

interface CFModuleEnv {
[key: string]: any;
Expand All @@ -30,10 +30,11 @@ export default {
const url = new URL(request.url);
try {
if (isInKv(url.pathname)) {
const [match] = getKVMatch(url.pathname);
// https://github.com/cloudflare/kv-asset-handler#es-modules
return await getAssetFromKV(
{
request,
request: new Request(new URL("http://localhost" + match)),
waitUntil(promise) {
return context.waitUntil(promise);
},
Expand All @@ -47,7 +48,8 @@ export default {
);
}
} catch {
// Ignore
// getAssetFromKV fail to return files with no extensions,
// so we can catch here and let Nitro handle it.
}

let body;
Expand Down Expand Up @@ -91,16 +93,3 @@ const baseURLModifier = (request: Request) => {
const url = withoutBase(request.url, useRuntimeConfig().app.baseURL);
return mapRequestToAsset(new Request(url, request));
};

const keyStartsWith = (literalObj: Record<string, unknown>, needle: string) =>
Object.keys(literalObj).some((k) => k.startsWith(needle));
const isInKv = (id = "") => {
if (
assets[id] ||
keyStartsWith(assets, id) ||
keyStartsWith(publicAssetBases, id)
) {
return true;
}
return false;
};
14 changes: 11 additions & 3 deletions src/runtime/entries/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from "@cloudflare/workers-types";
import { requestHasBody } from "#internal/nitro/utils";
import { nitroApp } from "#internal/nitro/app";
import { isPublicAssetURL } from "#internal/nitro/virtual/public-assets";
import { isInKv, getKVMatch } from "#internal/nitro/virtual/public-assets";

/**
* Reference: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#parameters
Expand All @@ -27,8 +27,16 @@ export default {
context: EventContext<CFPagesEnv, string, any>
) {
const url = new URL(request.url);
if (isPublicAssetURL(url.pathname)) {
return env.ASSETS.fetch(request);
if (isInKv(url.pathname)) {
try {
const [match] = getKVMatch(url.pathname);
return await env.ASSETS.fetch(
new Request(new URL("http://localhost" + match))
);
} catch (error) {
console.error(error);
throw error;
}
}

let body;
Expand Down
16 changes: 1 addition & 15 deletions src/runtime/entries/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { nitroApp } from "#internal/nitro/app";
import { useRuntimeConfig } from "#internal/nitro";
import {
getPublicAssetMeta,
publicAssetBases,
isInKv,
} from "#internal/nitro/virtual/public-assets";
import assets from "#internal/nitro/virtual/public-assets-data";

addEventListener("fetch", (event: any) => {
event.respondWith(handleEvent(event));
Expand Down Expand Up @@ -83,16 +82,3 @@ function normalizeOutgoingHeaders(
Array.isArray(v) ? v.join(",") : v,
]);
}

const keyStartsWith = (literalObj: Record<string, unknown>, needle: string) =>
Object.keys(literalObj).some((k) => k.startsWith(needle));
const isInKv = (id = "") => {
if (
assets[id] ||
keyStartsWith(assets, id) ||
keyStartsWith(publicAssetBases, id)
) {
return true;
}
return false;
};
2 changes: 2 additions & 0 deletions src/runtime/virtual/public-assets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const isPublicAssetURL: (id: string) => boolean;
export const getPublicAssetMeta: (id: string) => { maxAge?: number };
export const readAsset: (id: string) => Promise<Buffer>;
export const getAsset: (id: string) => any;
export const isInKv: (id: string) => boolean;
export const getKVMatch: (id: string) => [string, string] | null;

0 comments on commit 462b260

Please sign in to comment.