Skip to content

Commit

Permalink
allow symlinked assets (#5089)
Browse files Browse the repository at this point in the history
* allow symlinked assets

prettier

skip test on windows

Revert "skip test on windows"

This reverts commit f8e55e6.

skip test completely

* fix windows path

* fix

* i guess we need to recurse

* note to self

* fix some stuff

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
gaarf and Rich-Harris authored May 30, 2022
1 parent 821592f commit 981bc23
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-walls-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

allow symlinked static assets in dev
25 changes: 22 additions & 3 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ export const sveltekit = function (svelte_config) {
const file = svelte_config.kit.files.assets + pathname;

if (fs.existsSync(file) && !fs.statSync(file).isDirectory()) {
const has_correct_case = fs.realpathSync.native(file) === path.resolve(file);

if (has_correct_case) {
if (has_correct_case(file, svelte_config.kit.files.assets)) {
req.url = encodeURI(pathname); // don't need query/hash
asset_server(req, res);
return;
Expand Down Expand Up @@ -501,6 +499,27 @@ async function find_deps(vite, node, deps) {
await Promise.all(branches);
}

/**
* Determine if a file is being requested with the correct case,
* to ensure consistent behaviour between dev and prod and across
* operating systems. Note that we can't use realpath here,
* because we don't want to follow symlinks
* @param {string} file
* @param {string} assets
* @returns {boolean}
*/
function has_correct_case(file, assets) {
if (file === assets) return true;

const parent = path.dirname(file);

if (fs.readdirSync(parent).includes(path.basename(file))) {
return has_correct_case(parent, assets);
}

return false;
}

/**
* @param {import('types').ValidatedConfig} svelte_config
* @return {import('vite').Plugin[]}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/static/symlink-from
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/static/symlink-to/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2583,6 +2583,12 @@ test.describe.parallel('Static files', () => {
const response = await request.get('/static.JSON');
expect(response.status()).toBe(404);
});

test('Serves symlinked asset', async ({ request }) => {
const response = await request.get('/symlink-from/hello.txt');
expect(response.status()).toBe(200);
expect(await response.text()).toBe('hello');
});
});

test.describe.parallel('Matchers', () => {
Expand Down

0 comments on commit 981bc23

Please sign in to comment.