Skip to content

Commit

Permalink
refactor: rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Jun 7, 2023
1 parent 462b260 commit 84b3324
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
13 changes: 2 additions & 11 deletions src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,11 @@ ${
export const publicAssetBases = ${JSON.stringify(publicAssetBases)}
export function isPublicAssetURL(id = '') {
if (assets[id]) {
return true
}
for (const base in publicAssetBases) {
if (id.startsWith(base)) { return true }
}
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 = "") => {
export const isPublicAssetURL = (id = "") => {
if (
assets[id] ||
keyStartsWith(assets, id) ||
Expand All @@ -128,7 +119,7 @@ export const isInKv = (id = "") => {
return false
}
export const getKVMatch = (id = "") => {
export const getPublicAssetMatch = (id = "") => {
const assetMatch = findKey(assets, id)
if(assetMatch) return [assetMatch, assets[assetMatch]]
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/entries/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { requestHasBody } from "../utils";
import { nitroApp } from "#internal/nitro/app";
import { useRuntimeConfig } from "#internal/nitro";
import {
getKVMatch,
getPublicAssetMatch,
getPublicAssetMeta,
isInKv,
isPublicAssetURL,
} from "#internal/nitro/virtual/public-assets";

interface CFModuleEnv {
Expand All @@ -29,8 +29,8 @@ export default {
) {
const url = new URL(request.url);
try {
if (isInKv(url.pathname)) {
const [match] = getKVMatch(url.pathname);
if (isPublicAssetURL(url.pathname)) {
const [match] = getPublicAssetMatch(url.pathname);
// https://github.com/cloudflare/kv-asset-handler#es-modules
return await getAssetFromKV(
{
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/entries/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type {
} from "@cloudflare/workers-types";
import { requestHasBody } from "#internal/nitro/utils";
import { nitroApp } from "#internal/nitro/app";
import { isInKv, getKVMatch } from "#internal/nitro/virtual/public-assets";
import {
isPublicAssetURL,
getPublicAssetMatch,
} from "#internal/nitro/virtual/public-assets";

/**
* Reference: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#parameters
Expand All @@ -27,9 +30,9 @@ export default {
context: EventContext<CFPagesEnv, string, any>
) {
const url = new URL(request.url);
if (isInKv(url.pathname)) {
if (isPublicAssetURL(url.pathname)) {
try {
const [match] = getKVMatch(url.pathname);
const [match] = getPublicAssetMatch(url.pathname);
return await env.ASSETS.fetch(
new Request(new URL("http://localhost" + match))
);
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/entries/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { nitroApp } from "#internal/nitro/app";
import { useRuntimeConfig } from "#internal/nitro";
import {
getPublicAssetMeta,
isInKv,
isPublicAssetURL,
} from "#internal/nitro/virtual/public-assets";

addEventListener("fetch", (event: any) => {
Expand All @@ -19,7 +19,7 @@ addEventListener("fetch", (event: any) => {
async function handleEvent(event: FetchEvent) {
const url = new URL(event.request.url);
try {
if (isInKv(url.pathname)) {
if (isPublicAssetURL(url.pathname)) {
return await getAssetFromKV(event, {
cacheControl: assetsCacheControl,
mapRequestToAsset: baseURLModifier,
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/virtual/public-assets.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ 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;
export const getPublicAssetMatch: (id: string) => [string, string] | null;

0 comments on commit 84b3324

Please sign in to comment.