Skip to content

Commit

Permalink
fix: read non-encoded data URIs (#12347)
Browse files Browse the repository at this point in the history
* fix: read non-encoded data URIs

* optimise
  • Loading branch information
Rich-Harris committed Jun 14, 2024
1 parent 28f6014 commit 0a99f0e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-penguins-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: read non-encoded data URIs
11 changes: 6 additions & 5 deletions packages/kit/src/runtime/app/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ export function read(asset) {
}

// handle inline assets internally
if (asset.startsWith('data:')) {
const [prelude, data] = asset.split(';');
const type = prelude.slice(5) || 'application/octet-stream';
const match = /^data:([^;,]+)?(;base64)?,/.exec(asset);
if (match) {
const type = match[1] ?? 'application/octet-stream';
const data = asset.slice(match[0].length);

if (data.startsWith('base64,')) {
const decoded = b64_decode(data.slice(7));
if (match[2] !== undefined) {
const decoded = b64_decode(data);

return new Response(decoded, {
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export async function load() {
return {
auto: await read(auto).text(),
url: await read(url).text(),
local_glob: await read(Object.values(local_glob)[0].default).text(),
external_glob: await read(Object.values(external_glob)[0].default).text()
local_glob: await read(local_glob['./assets/[file].txt'].default).text(),
external_glob: await read(Object.values(external_glob)[0].default).text(),
svg: await read(local_glob['./assets/icon.svg'].default).text()
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
<p data-testid="url">{data.url}</p>
<p data-testid="local_glob">{data.local_glob}</p>
<p data-testid="external_glob">{data.external_glob}</p>
<div data-testid="svg">{@html data.svg}</div>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/kit/test/apps/basics/test/cross-platform/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ test.describe('$app/server', () => {
const url = await page.textContent('[data-testid="url"]');
const local_glob = await page.textContent('[data-testid="local_glob"]');
const external_glob = await page.textContent('[data-testid="external_glob"]');
const svg = await page.innerHTML('[data-testid="svg"]');

// the emoji is there to check that base64 decoding works correctly
expect(auto.trim()).toBe('Imported without ?url 😎');
Expand All @@ -1050,5 +1051,6 @@ test.describe('$app/server', () => {
expect(external_glob.trim()).toBe(
'Imported with url glob from the read-file test in basics. Placed here outside the app folder to force a /@fs prefix 😎'
);
expect(svg).toContain('<rect width="24" height="24" rx="2" fill="#ff3e00"></rect>');
});
});

0 comments on commit 0a99f0e

Please sign in to comment.