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; return sections;
} }
export function get(req, res) { export function get() {
try { try {
if (!json || process.env.NODE_ENV !== 'production') { if (!json || process.env.NODE_ENV !== 'production') {
json = get_sections(); json = get_sections();

@ -1,20 +1,23 @@
export function get(req, res) { export async function get(req) {
let { min = '0', max = '100' } = req.query; let { min = '0', max = '100' } = req.query;
min = +min; min = +min;
max = +max; max = +max;
res.setHeader('Access-Control-Allow-Origin', '*');
// simulate a long delay // simulate a long delay
setTimeout(() => { await new Promise((res) => setTimeout(res, 1000));
// fail sometimes
if (Math.random() < 0.333) { // fail sometimes
res.statusCode = 400; if (Math.random() < 0.333) {
res.end(`Failed to generate random number. Please try again`); return {
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)); const num = min + Math.round(Math.random() * (max - min));
res.end(String(num)); return {
}, 1000); headers: { 'Access-Control-Allow-Origin': '*' },
} body: String(num)
};
}

Loading…
Cancel
Save