update more endpoints for SvelteKit

pull/6811/head
Conduitry 5 years ago
parent cc6713c3fd
commit 03d9332990

@ -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' });
}

@ -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)
};
}

@ -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();

@ -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);
}
const num = min + Math.round(Math.random() * (max - min));
return {
headers: { 'Access-Control-Allow-Origin': '*' },
body: String(num)
};
}

Loading…
Cancel
Save