Skip to content

Commit

Permalink
refactor!: rename CACHE option to CACHE_ENABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Jan 27, 2025
1 parent e4aedd5 commit 83db5aa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/create-main-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dotenvLoad({ export: true });

const baseConfig: Config = {
BASE_PATH: '/',
CACHE: false,
CACHE_ENABLE: false,
HOMEPAGE: 'https://home/page',
UPSTREAM_ORIGIN: 'https://esm.sh/',
OUTPUT_BANNER: '',
Expand Down Expand Up @@ -575,7 +575,7 @@ Deno.test(
};
const config = {
...baseConfig,
CACHE: true,
CACHE_ENABLE: true,
};
const handler = createMainHandler(
config,
Expand Down Expand Up @@ -617,7 +617,7 @@ Deno.test(
};
const config = {
...baseConfig,
CACHE: true,
CACHE_ENABLE: true,
};
const handler = createMainHandler(
config,
Expand Down Expand Up @@ -675,7 +675,7 @@ Deno.test(
};
const config = {
...baseConfig,
CACHE: true,
CACHE_ENABLE: true,
REDIRECT_FASTPATH: true,
};
const handler = createMainHandler(
Expand Down Expand Up @@ -729,7 +729,7 @@ Deno.test(
};
const config = {
...baseConfig,
CACHE: true,
CACHE_ENABLE: true,
CACHE_CLIENT_REDIRECT: 60,
REDIRECT_FASTPATH: true,
};
Expand Down Expand Up @@ -788,7 +788,7 @@ Deno.test(
};
const config = {
...baseConfig,
CACHE: true,
CACHE_ENABLE: true,
CACHE_CLIENT_REDIRECT: 600,
};
const handler = createMainHandler(
Expand Down Expand Up @@ -853,7 +853,7 @@ Deno.test(
};
const config = {
...baseConfig,
CACHE: true,
CACHE_ENABLE: true,
CACHE_REDIRECT: 600,
REDIRECT_FASTPATH: true,
};
Expand Down
11 changes: 6 additions & 5 deletions src/create-main-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function createMainHandler(
): (request: Request) => Promise<Response> {
const {
BASE_PATH,
CACHE,
CACHE_ENABLE,
CACHE_REDIRECT,
HOMEPAGE,
UPSTREAM_ORIGIN,
Expand Down Expand Up @@ -120,15 +120,15 @@ export function createMainHandler(
'http.url': publicUrl,
});

if (isMapRequest && !CACHE) {
if (isMapRequest && !CACHE_ENABLE) {
// Sourcemaps are only enabled with CACHE
// otherwise they are served as inlined data uris
// thus it is not possible to receive a sourcemap request when !CACHE
return new Response(null, { status: 404 });
}

if (
CACHE &&
CACHE_ENABLE &&
cache /* && !request.headers.get('cache-control')?.includes('no-cache') */
) {
const cacheReadSpan = tracer.startSpan('cache-read', {
Expand Down Expand Up @@ -185,7 +185,7 @@ export function createMainHandler(
const canGenerateSourcemap =
!!(typeof sourceModule === 'object' && sourceModule.map);
const sourcemap = canGenerateSourcemap
? (CACHE ? true : 'inline')
? (CACHE_ENABLE ? true : 'inline')
: false;
const sourcemapFileNames = sourcemap === true
? `${basename(publicUrl)}.map`
Expand Down Expand Up @@ -234,7 +234,8 @@ export function createMainHandler(
isNotFound(response) ||
isOk(response) ||
isActualRedirect;
const shouldCache = !!(CACHE && (!isActualRedirect || CACHE_REDIRECT));
const shouldCache =
!!(CACHE_ENABLE && (!isActualRedirect || CACHE_REDIRECT));
const willCache = shouldCache && isCacheable && cache;

let synthMapRequest: Request | null = null;
Expand Down
2 changes: 1 addition & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dotenvLoad({ export: true });

export const config: Config = {
BASE_PATH: sanitizeBasePath(Deno.env.get('BASE_PATH') ?? '/'),
CACHE: Deno.env.get('CACHE') === 'true',
CACHE_ENABLE: Deno.env.get('CACHE_ENABLE') === 'true',
CACHE_CONN_MAX: Number(Deno.env.get('CACHE_CONN_MAX')) || 20,
CACHE_CONN_MIN: Number(Deno.env.get('CACHE_CONN_MIN')) || 2,
CACHE_REDIRECT: Number(Deno.env.get('CACHE_REDIRECT') as string) || 600,
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { instrumentRequestHandler } from './instrument-request-handler.ts';
import { getBuildTarget } from './utils.ts';

let cache: CacheLike | undefined;
if (config.CACHE) {
if (config.CACHE_ENABLE) {
const headerNormalizer = (
headerName: string,
headerValue: string | null,
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface HttpZResponseModel {

export type Config = {
BASE_PATH: string;
CACHE: boolean;
CACHE_ENABLE: boolean;
CACHE_CONN_MAX?: number;
CACHE_CONN_MIN?: number;
CACHE_REDIRECT?: number;
Expand All @@ -47,8 +47,6 @@ export type Config = {
OTEL_EXPORTER_OTLP_ENDPOINT?: string;
OTEL_EXPORTER_ENABLE?: boolean;
OTEL_EXPORTER_OTLP_HEADERS?: Record<string, string>;
/** @deprecated */
DD_TRACE_ENABLED?: boolean;
HOMEPAGE: string;
OUTPUT_BANNER?: string;
REDIRECT_FASTPATH?: boolean;
Expand Down

0 comments on commit 83db5aa

Please sign in to comment.