mirror of https://github.com/sveltejs/svelte
chore: add download/hash scripts for sandbox (#15218)
* chore: add download/hash scripts for sandbox * remove loggingpull/15221/head
parent
d39d8c0675
commit
ed1cf75b79
@ -0,0 +1,52 @@
|
|||||||
|
import fs from 'node:fs';
|
||||||
|
|
||||||
|
const arg = process.argv[2];
|
||||||
|
|
||||||
|
/** @type {URL} */
|
||||||
|
let url;
|
||||||
|
|
||||||
|
try {
|
||||||
|
url = new URL(arg);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`${arg} is not a URL`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.origin !== 'https://svelte.dev' || !url.pathname.startsWith('/playground/')) {
|
||||||
|
console.error(`${arg} is not a Svelte playground URL`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let files;
|
||||||
|
|
||||||
|
if (url.hash.length > 1) {
|
||||||
|
const decoded = atob(url.hash.slice(1).replaceAll('-', '+').replaceAll('_', '/'));
|
||||||
|
// putting it directly into the blob gives a corrupted file
|
||||||
|
const u8 = new Uint8Array(decoded.length);
|
||||||
|
for (let i = 0; i < decoded.length; i++) {
|
||||||
|
u8[i] = decoded.charCodeAt(i);
|
||||||
|
}
|
||||||
|
const stream = new Blob([u8]).stream().pipeThrough(new DecompressionStream('gzip'));
|
||||||
|
const json = await new Response(stream).text();
|
||||||
|
|
||||||
|
files = JSON.parse(json).files;
|
||||||
|
} else {
|
||||||
|
const id = url.pathname.split('/')[2];
|
||||||
|
const response = await fetch(`https://svelte.dev/playground/api/${id}.json`);
|
||||||
|
|
||||||
|
files = (await response.json()).components.map((data) => {
|
||||||
|
const basename = `${data.name}.${data.type}`;
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'file',
|
||||||
|
name: basename,
|
||||||
|
basename,
|
||||||
|
contents: data.source,
|
||||||
|
text: true
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
fs.writeFileSync(`src/${file.name}`, file.contents);
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
import fs from 'node:fs';
|
||||||
|
|
||||||
|
const files = [];
|
||||||
|
|
||||||
|
for (const basename of fs.readdirSync('src')) {
|
||||||
|
if (fs.statSync(`src/${basename}`).isDirectory()) continue;
|
||||||
|
|
||||||
|
files.push({
|
||||||
|
type: 'file',
|
||||||
|
name: basename,
|
||||||
|
basename,
|
||||||
|
contents: fs.readFileSync(`src/${basename}`, 'utf-8'),
|
||||||
|
text: true // TODO might not be
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = JSON.stringify({
|
||||||
|
name: 'sandbox',
|
||||||
|
files
|
||||||
|
});
|
||||||
|
|
||||||
|
async function compress(payload) {
|
||||||
|
const reader = new Blob([payload])
|
||||||
|
.stream()
|
||||||
|
.pipeThrough(new CompressionStream('gzip'))
|
||||||
|
.getReader();
|
||||||
|
|
||||||
|
let buffer = '';
|
||||||
|
for (;;) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
reader.releaseLock();
|
||||||
|
return btoa(buffer).replaceAll('+', '-').replaceAll('/', '_');
|
||||||
|
} else {
|
||||||
|
for (let i = 0; i < value.length; i++) {
|
||||||
|
// decoding as utf-8 will make btoa reject the string
|
||||||
|
buffer += String.fromCharCode(value[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const hash = await compress(payload);
|
||||||
|
console.log(`https://svelte.dev/playground/untitled#${hash}`);
|
Loading…
Reference in new issue