-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add download/hash scripts for sandbox (#15218)
* chore: add download/hash scripts for sandbox * remove logging
- Loading branch information
1 parent
d39d8c0
commit ed1cf75
Showing
6 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters