Skip to content

Commit

Permalink
feat: redirect to ESM_SERVICE_HOST if request empty
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed May 15, 2023
1 parent 6823b3d commit 848f62d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"importMap": "./importmap.json",
"tasks": {
"deploy": "deployctl deploy --project=esroyo-systemjs-sh src/main.ts",
"dev": "deno run --watch src/main.ts",
"test": "deno test --allow-env --allow-read"
}
Expand Down
1 change: 0 additions & 1 deletion importmap.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"imports": {
"@rollup/plugin-terser": "https://esm.sh/@rollup/plugin-terser",
"@rollup/plugin-virtual": "https://esm.sh/@rollup/plugin-virtual",
"rollup": "https://esm.sh/rollup",
"testing/asserts": "https://deno.land/std/testing/asserts.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/esm-proxy-request-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ Deno.test('esmProxyRequestHandler', async (t) => {
const fetchReturn = Promise.resolve({ text: () => Promise.resolve(`export * from "https://${ESM_SERVICE_HOST}/stable/vue@3.3.2/es2022/vue.mjs";`) } as unknown as Response);
const fetchStub = stub(globalThis, 'fetch', returnsNext([fetchReturn, fetchReturn, fetchReturn]));

await t.step('should redirect to ESM_SERVICE_HOST on request empty', async () => {
const req = new Request(`https://${SELF_HOST}/`);
const res = await esmProxyRequestHandler(req);
assertEquals(res.status, 308);
assertEquals(res.headers.get('location'), `https://${ESM_SERVICE_HOST}/`);
});

await t.step('should forward the request to ESM_SERVICE_HOST keeping the parameters', async () => {
const req = new Request(`https://${SELF_HOST}/foo?bundle`);
await esmProxyRequestHandler(req);
Expand Down
3 changes: 3 additions & 0 deletions src/esm-proxy-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const ESM_SERVICE_HOST = 'esm.sh';

export async function esmProxyRequestHandler(req: Request): Promise<Response | never> {
const modifiedUrl = new URL(req.url);
if (modifiedUrl.pathname === '/') {
return Response.redirect(`https://${ESM_SERVICE_HOST}`, 308);
}
const selfHost = modifiedUrl.host;
modifiedUrl.host = ESM_SERVICE_HOST;
const esmCode = await fetch(modifiedUrl.toString(), { headers: req.headers }).then((res) => res.text());
Expand Down
9 changes: 0 additions & 9 deletions src/main_bench.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/to-systemjs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ModuleFormat, rollup } from 'rollup';
//import terser from '@rollup/plugin-terser';
import virtual from '@rollup/plugin-virtual';

export const toSystemjs = async (esmCode: string): Promise<string> => {
Expand All @@ -9,7 +8,6 @@ export const toSystemjs = async (esmCode: string): Promise<string> => {
input: 'esmCode',
plugins: [
virtual({ esmCode }),
//terser(),
],
treeshake: false,
};
Expand Down

0 comments on commit 848f62d

Please sign in to comment.