From 03d933299033e46f01403ed6ceef6215ec22ae40 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Wed, 13 Oct 2021 15:01:44 -0400 Subject: [PATCH] update more endpoints for SvelteKit --- site/src/routes/repl/local/[...file].js | 17 ------------- site/src/routes/repl/local/[...path].js | 11 ++++++++ site/src/routes/tutorial/index.json.js | 2 +- site/src/routes/tutorial/random-number.js | 31 +++++++++++++---------- 4 files changed, 29 insertions(+), 32 deletions(-) delete mode 100644 site/src/routes/repl/local/[...file].js create mode 100644 site/src/routes/repl/local/[...path].js diff --git a/site/src/routes/repl/local/[...file].js b/site/src/routes/repl/local/[...file].js deleted file mode 100644 index 7bd1c6a82c..0000000000 --- a/site/src/routes/repl/local/[...file].js +++ /dev/null @@ -1,17 +0,0 @@ -import { createReadStream } from 'fs'; - -export function get(req, res) { - const path = req.params.file.join('/'); - if (process.env.NODE_ENV !== 'development' || ('/' + path).includes('/.')) { - res.writeHead(403); - res.end(); - return; - } - createReadStream('../' + path) - .on('error', () => { - res.writeHead(403); - res.end(); - }) - .pipe(res); - res.writeHead(200, { 'Content-Type': 'text/javascript' }); -} diff --git a/site/src/routes/repl/local/[...path].js b/site/src/routes/repl/local/[...path].js new file mode 100644 index 0000000000..72bf70722f --- /dev/null +++ b/site/src/routes/repl/local/[...path].js @@ -0,0 +1,11 @@ +import { readFileSync } from 'fs'; + +export function get({ params: { path } }) { + if (process.env.NODE_ENV !== 'development' || ('/' + path).includes('/.')) { + return { status: 403 }; + } + return { + headers: { 'Content-Type': 'text/javascript' }, + body: readFileSync('../' + path) + }; +} diff --git a/site/src/routes/tutorial/index.json.js b/site/src/routes/tutorial/index.json.js index 2467491de8..9df41f9e26 100644 --- a/site/src/routes/tutorial/index.json.js +++ b/site/src/routes/tutorial/index.json.js @@ -47,7 +47,7 @@ function get_sections() { return sections; } -export function get(req, res) { +export function get() { try { if (!json || process.env.NODE_ENV !== 'production') { json = get_sections(); diff --git a/site/src/routes/tutorial/random-number.js b/site/src/routes/tutorial/random-number.js index 818a38fe6a..5a4d94e1f4 100644 --- a/site/src/routes/tutorial/random-number.js +++ b/site/src/routes/tutorial/random-number.js @@ -1,20 +1,23 @@ -export function get(req, res) { +export async function get(req) { let { min = '0', max = '100' } = req.query; min = +min; max = +max; - res.setHeader('Access-Control-Allow-Origin', '*'); - // simulate a long delay - setTimeout(() => { - // fail sometimes - if (Math.random() < 0.333) { - res.statusCode = 400; - res.end(`Failed to generate random number. Please try again`); - return; - } + await new Promise((res) => setTimeout(res, 1000)); + + // fail sometimes + if (Math.random() < 0.333) { + return { + status: 400, + headers: { 'Access-Control-Allow-Origin': '*' }, + body: `Failed to generate random number. Please try again` + }; + } - const num = min + Math.round(Math.random() * (max - min)); - res.end(String(num)); - }, 1000); -} \ No newline at end of file + const num = min + Math.round(Math.random() * (max - min)); + return { + headers: { 'Access-Control-Allow-Origin': '*' }, + body: String(num) + }; +}