Skip to content

Commit ed1cf75

Browse files
authored
chore: add download/hash scripts for sandbox (#15218)
* chore: add download/hash scripts for sandbox * remove logging
1 parent d39d8c0 commit ed1cf75

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

playgrounds/sandbox/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<script type="module">
1414
import { mount, hydrate, unmount } from 'svelte';
15-
import App from '/src/main.svelte';
15+
import App from '/src/App.svelte';
1616

1717
const root = document.getElementById('root');
1818
const render = root.firstChild?.nextSibling ? hydrate : mount;

playgrounds/sandbox/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"ssr": "node --conditions=development ./ssr-dev.js",
1010
"build": "vite build --outDir dist/client && vite build --outDir dist/server --ssr ssr-prod.js",
1111
"prod": "npm run build && node dist/server/ssr-prod",
12-
"preview": "vite preview"
12+
"preview": "vite preview",
13+
"download": "node scripts/download.js",
14+
"hash": "node scripts/hash.js | pbcopy && echo \"copied URL to clipboard\""
1315
},
1416
"devDependencies": {
1517
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.6",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fs from 'node:fs';
2+
3+
const arg = process.argv[2];
4+
5+
/** @type {URL} */
6+
let url;
7+
8+
try {
9+
url = new URL(arg);
10+
} catch (e) {
11+
console.error(`${arg} is not a URL`);
12+
process.exit(1);
13+
}
14+
15+
if (url.origin !== 'https://svelte.dev' || !url.pathname.startsWith('/playground/')) {
16+
console.error(`${arg} is not a Svelte playground URL`);
17+
process.exit(1);
18+
}
19+
20+
let files;
21+
22+
if (url.hash.length > 1) {
23+
const decoded = atob(url.hash.slice(1).replaceAll('-', '+').replaceAll('_', '/'));
24+
// putting it directly into the blob gives a corrupted file
25+
const u8 = new Uint8Array(decoded.length);
26+
for (let i = 0; i < decoded.length; i++) {
27+
u8[i] = decoded.charCodeAt(i);
28+
}
29+
const stream = new Blob([u8]).stream().pipeThrough(new DecompressionStream('gzip'));
30+
const json = await new Response(stream).text();
31+
32+
files = JSON.parse(json).files;
33+
} else {
34+
const id = url.pathname.split('/')[2];
35+
const response = await fetch(`https://svelte.dev/playground/api/${id}.json`);
36+
37+
files = (await response.json()).components.map((data) => {
38+
const basename = `${data.name}.${data.type}`;
39+
40+
return {
41+
type: 'file',
42+
name: basename,
43+
basename,
44+
contents: data.source,
45+
text: true
46+
};
47+
});
48+
}
49+
50+
for (const file of files) {
51+
fs.writeFileSync(`src/${file.name}`, file.contents);
52+
}

playgrounds/sandbox/scripts/hash.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import fs from 'node:fs';
2+
3+
const files = [];
4+
5+
for (const basename of fs.readdirSync('src')) {
6+
if (fs.statSync(`src/${basename}`).isDirectory()) continue;
7+
8+
files.push({
9+
type: 'file',
10+
name: basename,
11+
basename,
12+
contents: fs.readFileSync(`src/${basename}`, 'utf-8'),
13+
text: true // TODO might not be
14+
});
15+
}
16+
17+
const payload = JSON.stringify({
18+
name: 'sandbox',
19+
files
20+
});
21+
22+
async function compress(payload) {
23+
const reader = new Blob([payload])
24+
.stream()
25+
.pipeThrough(new CompressionStream('gzip'))
26+
.getReader();
27+
28+
let buffer = '';
29+
for (;;) {
30+
const { done, value } = await reader.read();
31+
32+
if (done) {
33+
reader.releaseLock();
34+
return btoa(buffer).replaceAll('+', '-').replaceAll('/', '_');
35+
} else {
36+
for (let i = 0; i < value.length; i++) {
37+
// decoding as utf-8 will make btoa reject the string
38+
buffer += String.fromCharCode(value[i]);
39+
}
40+
}
41+
}
42+
}
43+
44+
const hash = await compress(payload);
45+
console.log(`https://svelte.dev/playground/untitled#${hash}`);

playgrounds/sandbox/ssr-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ polka()
2222
.use(async (req, res) => {
2323
const template = fs.readFileSync(path.resolve(__dirname, 'index.html'), 'utf-8');
2424
const transformed_template = await vite.transformIndexHtml(req.url, template);
25-
const { default: App } = await vite.ssrLoadModule('/src/main.svelte');
25+
const { default: App } = await vite.ssrLoadModule('/src/App.svelte');
2626
const { head, body } = render(App);
2727

2828
const html = transformed_template

playgrounds/sandbox/ssr-prod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import polka from 'polka';
44
import { render } from 'svelte/server';
5-
import App from './src/main.svelte';
5+
import App from './src/App.svelte';
66

77
const { head, body } = render(App);
88

0 commit comments

Comments
 (0)