Skip to content

Commit

Permalink
Resolve #17271 staticDirs path issue on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
justrhysism committed Mar 7, 2022
1 parent 761501b commit 6f9c8a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/core-server/src/utils/__tests__/server-statics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ describe('parseStaticDir', () => {
targetDir: './custom-endpoint',
targetEndpoint: '/custom-endpoint',
});

await expect(parseStaticDir('C:\\foo\\bar:\\custom-endpoint')).resolves.toEqual({
staticDir: expect.any(String), // can't test this properly on unix
staticPath: path.resolve('C:\\foo\\bar'),
targetDir: './custom-endpoint',
targetEndpoint: '/custom-endpoint',
});
});

it('pins relative endpoint at root', async () => {
Expand Down
14 changes: 11 additions & 3 deletions lib/core-server/src/utils/server-statics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ export async function useStatics(router: any, options: Options) {
}

export const parseStaticDir = async (arg: string) => {
// Split on ':' only if not followed by '\', for Windows compatibility (e.g. 'C:\some\dir')
const [rawDir, target = '/'] = arg.split(/:(?!\\)/);
// Split on last index of ':', for Windows compatibility (e.g. 'C:\some\dir:\foo')
const lastColonIndex = arg.lastIndexOf(':');
const isWindowsAbsolute = path.win32.isAbsolute(arg);
const isWindowsRawDirOnly = isWindowsAbsolute && lastColonIndex === 1; // e.g. 'C:\some\dir'
const splitIndex = lastColonIndex !== -1 && !isWindowsRawDirOnly ? lastColonIndex : arg.length;

const targetRaw = arg.substring(splitIndex + 1) || '/';
const target = targetRaw.split(path.sep).join(path.posix.sep); // Ensure target has forward-slash path

const rawDir = arg.substring(0, splitIndex);
const staticDir = path.isAbsolute(rawDir) ? rawDir : `./${rawDir}`;
const staticPath = path.resolve(staticDir);
const targetDir = target.replace(/^\/?/, './');
const targetEndpoint = targetDir.substr(1);
const targetEndpoint = targetDir.substring(1);

if (!(await pathExists(staticPath))) {
throw new Error(
Expand Down

0 comments on commit 6f9c8a5

Please sign in to comment.