Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions packages/core/src/server/assets-middleware/getFileFromUrl.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
import type { Stats as FSStats } from 'node:fs';
import path from 'node:path';
import { unescape as qsUnescape } from 'node:querystring';
import { parse } from 'node:url';
import type { EnvironmentContext } from '../../types';
import type { OutputFileSystem } from './index';
import { memorize } from './memorize';

// TODO: type the cache options instead of using any for the second parameter
const memoizedParse = memorize(parse, undefined, (value: any) => {
if (value.pathname) {
value.pathname = decode(value.pathname);
}

return value;
});

const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;

function decode(input: string): string {
return qsUnescape(input);
}

export function getFileFromUrl(
url: string,
outputFileSystem: OutputFileSystem,
environments: Record<string, EnvironmentContext>,
): { filename: string; fsStats: FSStats } | { errorCode: number } | undefined {
let urlObject: URL;
let pathname: string | undefined;

try {
urlObject = memoizedParse(url, false, true) as URL;
const urlObject = new URL(url, 'http://localhost');
if (urlObject.pathname) {
pathname = qsUnescape(urlObject.pathname);
}
} catch {
return;
}

const { pathname } = urlObject;
if (!pathname) {
return;
}
Expand Down
Loading