Skip to content

Commit

Permalink
fix: make sure BASE_PATH is honored on URL without origin (absolute p…
Browse files Browse the repository at this point in the history
…aths)
  • Loading branch information
esroyo committed Jan 31, 2024
1 parent 0dd0867 commit 8c67c6f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions src/esm-proxy-request-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,78 @@ Deno.test('esmProxyRequestHandler', async (t) => {
},
);

await t.step(
'should prefix the absolute paths (missing $esmOrigin) with the $basePath',
async () => {
Deno.env.set('BASE_PATH', '/sub-dir/234');
const fetchStub = stub(
_internals,
'fetch',
returnsNext([fetchReturn(`
import "/stable/@vue/runtime-dom@3.3.4/es2022/runtime-dom.mjs";
export * from "/stable/vue@3.3.4/es2022/vue.mjs";
`)]),
);
const req = new Request(`${SELF_ORIGIN}/vue`);
const res = await esmProxyRequestHandler(req);
const systemjsCode = await res.text();
assertEquals(
!!systemjsCode.match(
new RegExp(
'\'/sub-dir/234/stable/@vue/runtime-dom@3.3.4/es2022/runtime-dom.mjs\''
),
),
true,
);
assertEquals(
!!systemjsCode.match(
new RegExp(
'\'/sub-dir/234/stable/vue@3.3.4/es2022/vue.mjs\''
),
),
true,
);
fetchStub.restore();
Deno.env.set('BASE_PATH', '');
},
);

await t.step(
'should do nothing to the absolute paths (missing $esmOrigin) when the $basePath is empty',
async () => {
Deno.env.set('BASE_PATH', '');
const fetchStub = stub(
_internals,
'fetch',
returnsNext([fetchReturn(`
import "/stable/@vue/runtime-dom@3.3.4/es2022/runtime-dom.mjs";
export * from "/stable/vue@3.3.4/es2022/vue.mjs";
`)]),
);
const req = new Request(`${SELF_ORIGIN}/vue`);
const res = await esmProxyRequestHandler(req);
const systemjsCode = await res.text();
assertEquals(
!!systemjsCode.match(
new RegExp(
'\'/stable/@vue/runtime-dom@3.3.4/es2022/runtime-dom.mjs\''
),
),
true,
);
assertEquals(
!!systemjsCode.match(
new RegExp(
'\'/stable/vue@3.3.4/es2022/vue.mjs\''
),
),
true,
);
fetchStub.restore();
Deno.env.set('BASE_PATH', '');
},
);

await t.step(
'should replace the $esmOrigin by the X-Real-Origin host if exists including $basePath',
async () => {
Expand Down
12 changes: 11 additions & 1 deletion src/esm-proxy-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ export async function esmProxyRequestHandler(
const esmUrl = new URL(req.url.replace(selfOriginActual, ''), esmOrigin);
const replaceOrigin = (() => {
const esmOriginRegExp = new RegExp(esmOrigin, 'ig');
return (str: string) => str.replace(esmOriginRegExp, selfOriginFinal);
const registerRegExp = /register\(\[(?:['"][^'"]+['"](?:,\s*)?)*\],/gm;
const absolutePathRegExp = /['"][^'"]+['"]/gm;
const absolutePathReplaceRegExp = /^(['"])\//;
return (str: string) => {
return str.replace(esmOriginRegExp, selfOriginFinal)
.replace(registerRegExp, (registerMatch) => {
return registerMatch.replace(absolutePathRegExp, (absolutePathMatch) => {
return absolutePathMatch.replace(absolutePathReplaceRegExp, `$1${basePath}`);
});
});
}
})();
const replaceOriginHeaders = (pair: [string, string] | null) => (pair === null ? pair : [pair[0], typeof pair[1] === 'string' ? replaceOrigin(pair[1]) : pair[1]] as [string, string]);
const reqHeaders = cloneHeaders(req.headers, denyHeaders);
Expand Down
1 change: 1 addition & 0 deletions src/to-systemjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const toSystemjsWorker = async (
const worker = new Worker(import.meta.resolve('./to-systemjs-worker.ts'), { type: 'module' });
return new Promise((resolve) => {
worker.addEventListener('message', (event: MessageEvent<{ code: string }>) => {
worker.terminate();
resolve(event.data.code);
}, false);
worker.postMessage({
Expand Down

0 comments on commit 8c67c6f

Please sign in to comment.