Skip to content

Commit

Permalink
fix: default to pesimistic build es2015
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Oct 18, 2024
1 parent 1ff5d1d commit f722243
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/create-request-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,44 @@ Deno.test('should forward the request to $UPSTREAM_ORIGIN keeping the parameters
);
});

Deno.test('should replace the user-agent when requesting to $UPSTREAM_ORIGIN if browser is unknown', async () => {
const fetchMock = spy((() => fetchReturn()) as typeof globalThis.fetch);
const handler = createRequestHandler(
baseConfig,
undefined,
fetchMock,
);
const req = new Request(`${SELF_ORIGIN}foo?bundle`, {
headers: { 'user-agent': 'potato' },
});
await handler(req);
assertEquals(
// @ts-ignore
(fetchMock.calls?.[0]?.args?.[1]?.headers as Headers).get('user-agent'),
'HeadlessChrome/51',
);
});

Deno.test('should NOT replace the user-agent when requesting to $UPSTREAM_ORIGIN if browser is known', async () => {
const fetchMock = spy((() => fetchReturn()) as typeof globalThis.fetch);
const handler = createRequestHandler(
baseConfig,
undefined,
fetchMock,
);
const chromeUserAgent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.0.0 Safari/537.36';
const req = new Request(`${SELF_ORIGIN}foo?bundle`, {
headers: { 'user-agent': chromeUserAgent },
});
await handler(req);
assertEquals(
// @ts-ignore
(fetchMock.calls?.[0]?.args?.[1]?.headers as Headers).get('user-agent'),
chromeUserAgent,
);
});

Deno.test('should handle $UPSTREAM_ORIGIN with ending slash', async () => {
const UPSTREAM_ORIGIN = 'https://esm.sh/';
const fetchMock = spy(() => fetchReturn());
Expand Down
12 changes: 10 additions & 2 deletions src/create-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
cloneHeaders,
denyHeaders,
filterUpstreamHeaders,
getBuildTarget,
isForbidden,
isJsResponse,
isNotFound,
Expand All @@ -14,7 +15,6 @@ import {
} from './utils.ts';
import { toSystemjs } from './to-systemjs.ts';
import { type Pool } from 'generic-pool';
import { getBuildTargetFromUA } from 'esm-compat';
import { basename } from '@std/url';
import type { Cache, Config, OpenTelemetry, ResponseProps } from './types.ts';

Expand Down Expand Up @@ -130,7 +130,10 @@ export function createRequestHandler(

// Step: tracing
const rootSpan = otel.trace.getActiveSpan();
const buildTarget = getBuildTargetFromUA(req.headers.get('user-agent'));
const originalUserAgent = req.headers.get('user-agent') ?? '';
const [buildTarget, upstreamUserAgent] = getBuildTarget(
originalUserAgent,
);
const selfUrl = new URL(req.url);
const basePath = BASE_PATH === '/' ? BASE_PATH : `${BASE_PATH}/`;
if (selfUrl.pathname === BASE_PATH || selfUrl.pathname === basePath) {
Expand Down Expand Up @@ -257,6 +260,11 @@ export function createRequestHandler(
req.headers,
denyHeaders,
filterUpstreamHeaders,
(
pair: [string, string] | null,
) => (pair && pair[0] === 'user-agent'
? [pair[0], upstreamUserAgent]
: pair),
),
redirect: 'manual',
});
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import request from 'request';
import { dirname, join } from '@std/url';
import { getEsmaVersionFromUA } from 'esm-compat';
import type { HttpZResponseModel, SourceModule } from './types.ts';

export const nodeRequest = async (
Expand Down Expand Up @@ -204,3 +205,12 @@ export const buildSourceModule = async (
return input;
}
};

export const getBuildTarget = (userAgent: string): [string, string] => {
const esmBuildTarget = getEsmaVersionFromUA(userAgent);
if (esmBuildTarget === 'esnext') {
// this is the default for unknown browsers
return ['es2015', 'HeadlessChrome/51'];
}
return [esmBuildTarget, userAgent];
};

0 comments on commit f722243

Please sign in to comment.