mirror of https://github.com/sveltejs/svelte
[site] migrate to SvelteKit (#6811)
Co-authored-by: Conduitry <git@chor.date> Co-authored-by: bluwy <bjornlu.dev@gmail.com>pull/6865/head
parent
883c47b45d
commit
42076a7502
@ -1,13 +1,13 @@
|
|||||||
NODE_ENV=
|
NODE_ENV=
|
||||||
|
|
||||||
PORT=
|
PORT=3000
|
||||||
BASEURL=
|
BASEURL=http://localhost:3000
|
||||||
GITHUB_CLIENT_ID=
|
GITHUB_CLIENT_ID=
|
||||||
GITHUB_CLIENT_SECRET=
|
GITHUB_CLIENT_SECRET=
|
||||||
MAPBOX_ACCESS_TOKEN=
|
VITE_MAPBOX_ACCESS_TOKEN=
|
||||||
|
|
||||||
PGHOST=hostname
|
PGHOST=localhost
|
||||||
PGPORT=port
|
PGPORT=5432
|
||||||
PGUSER=username
|
PGUSER=username
|
||||||
PGPASSWORD=password
|
PGPASSWORD=password
|
||||||
PGDATABASE=database_name
|
PGDATABASE=database_name
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
sudo: false
|
|
||||||
language: node_js
|
|
||||||
node_js:
|
|
||||||
- "stable"
|
|
||||||
env:
|
|
||||||
global:
|
|
||||||
- BUILD_TIMEOUT=10000
|
|
||||||
install:
|
|
||||||
- npm install
|
|
||||||
- npm install cypress
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
|||||||
version: "{build}"
|
|
||||||
|
|
||||||
shallow_clone: true
|
|
||||||
|
|
||||||
init:
|
|
||||||
- git config --global core.autocrlf false
|
|
||||||
|
|
||||||
build: off
|
|
||||||
|
|
||||||
environment:
|
|
||||||
matrix:
|
|
||||||
# node.js
|
|
||||||
- nodejs_version: stable
|
|
||||||
|
|
||||||
install:
|
|
||||||
- ps: Install-Product node $env:nodejs_version
|
|
||||||
- npm install
|
|
@ -0,0 +1,15 @@
|
|||||||
|
version: "3.0"
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:13.4
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
environment:
|
||||||
|
- POSTGRES_DB=${PGDATABASE}
|
||||||
|
- POSTGRES_USER=${PGUSER}
|
||||||
|
- POSTGRES_PASSWORD=${PGPASSWORD}
|
||||||
|
adminer:
|
||||||
|
image: adminer
|
||||||
|
restart: always
|
||||||
|
ports:
|
||||||
|
- "4000:8080"
|
File diff suppressed because it is too large
Load Diff
@ -1,122 +0,0 @@
|
|||||||
import 'dotenv/config';
|
|
||||||
import babel from '@rollup/plugin-babel';
|
|
||||||
import commonjs from '@rollup/plugin-commonjs';
|
|
||||||
import json from '@rollup/plugin-json';
|
|
||||||
import replace from '@rollup/plugin-replace';
|
|
||||||
import resolve from '@rollup/plugin-node-resolve';
|
|
||||||
import svelte from 'rollup-plugin-svelte';
|
|
||||||
import { terser } from 'rollup-plugin-terser';
|
|
||||||
import config from 'sapper/config/rollup.js';
|
|
||||||
import pkg from './package.json';
|
|
||||||
|
|
||||||
const mode = process.env.NODE_ENV;
|
|
||||||
const dev = mode === 'development';
|
|
||||||
const legacy = !!process.env.SAPPER_LEGACY_BUILD;
|
|
||||||
|
|
||||||
if (!dev && !process.env.MAPBOX_ACCESS_TOKEN) {
|
|
||||||
throw new Error('MAPBOX_ACCESS_TOKEN is missing. Please add the token in the .env file before generating the production build.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const onwarn = (warning, onwarn) => (warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || onwarn(warning);
|
|
||||||
const dedupe = importee => importee === 'svelte' || importee.startsWith('svelte/');
|
|
||||||
|
|
||||||
export default {
|
|
||||||
client: {
|
|
||||||
input: config.client.input(),
|
|
||||||
output: config.client.output(),
|
|
||||||
plugins: [
|
|
||||||
replace({
|
|
||||||
'process.browser': true,
|
|
||||||
'process.env.NODE_ENV': JSON.stringify(mode),
|
|
||||||
'process.env.MAPBOX_ACCESS_TOKEN': JSON.stringify(process.env.MAPBOX_ACCESS_TOKEN)
|
|
||||||
}),
|
|
||||||
svelte({
|
|
||||||
compilerOptions: {
|
|
||||||
dev,
|
|
||||||
hydratable: true
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
resolve({
|
|
||||||
browser: true,
|
|
||||||
dedupe
|
|
||||||
}),
|
|
||||||
commonjs(),
|
|
||||||
json(),
|
|
||||||
|
|
||||||
legacy && babel({
|
|
||||||
extensions: ['.js', '.mjs', '.html', '.svelte'],
|
|
||||||
babelHelpers: 'runtime',
|
|
||||||
exclude: ['node_modules/@babel/**'],
|
|
||||||
presets: [
|
|
||||||
['@babel/preset-env', {
|
|
||||||
targets: '> 0.25%, not dead'
|
|
||||||
}]
|
|
||||||
],
|
|
||||||
plugins: [
|
|
||||||
'@babel/plugin-syntax-dynamic-import',
|
|
||||||
['@babel/plugin-transform-runtime', {
|
|
||||||
useESModules: true
|
|
||||||
}]
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
|
|
||||||
!dev && terser({
|
|
||||||
module: true
|
|
||||||
})
|
|
||||||
],
|
|
||||||
|
|
||||||
preserveEntrySignatures: false,
|
|
||||||
onwarn
|
|
||||||
},
|
|
||||||
|
|
||||||
server: {
|
|
||||||
input: config.server.input(),
|
|
||||||
output: config.server.output(),
|
|
||||||
plugins: [
|
|
||||||
replace({
|
|
||||||
'process.browser': false,
|
|
||||||
'process.env.NODE_ENV': JSON.stringify(mode)
|
|
||||||
}),
|
|
||||||
svelte({
|
|
||||||
compilerOptions: {
|
|
||||||
dev,
|
|
||||||
generate: 'ssr',
|
|
||||||
hydratable: true
|
|
||||||
},
|
|
||||||
emitCss: false
|
|
||||||
}),
|
|
||||||
resolve({
|
|
||||||
dedupe
|
|
||||||
}),
|
|
||||||
commonjs(),
|
|
||||||
json()
|
|
||||||
],
|
|
||||||
external: [
|
|
||||||
'yootils',
|
|
||||||
'codemirror',
|
|
||||||
...Object.keys(pkg.dependencies || {}).concat(
|
|
||||||
require('module').builtinModules || Object.keys(process.binding('natives'))
|
|
||||||
)
|
|
||||||
],
|
|
||||||
|
|
||||||
preserveEntrySignatures: 'strict',
|
|
||||||
onwarn
|
|
||||||
},
|
|
||||||
|
|
||||||
serviceworker: {
|
|
||||||
input: config.serviceworker.input(),
|
|
||||||
output: config.serviceworker.output(),
|
|
||||||
plugins: [
|
|
||||||
resolve(),
|
|
||||||
replace({
|
|
||||||
'process.browser': true,
|
|
||||||
'process.env.NODE_ENV': JSON.stringify(mode)
|
|
||||||
}),
|
|
||||||
commonjs(),
|
|
||||||
!dev && terser()
|
|
||||||
],
|
|
||||||
|
|
||||||
preserveEntrySignatures: false,
|
|
||||||
onwarn
|
|
||||||
}
|
|
||||||
};
|
|
@ -0,0 +1,26 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang='en' class="theme-default typo-default">
|
||||||
|
<head>
|
||||||
|
<meta charset='utf-8'>
|
||||||
|
<meta name='viewport' content='width=device-width,initial-scale=1'>
|
||||||
|
<meta name='theme-color' content='#ff3e00'>
|
||||||
|
|
||||||
|
<base href='/'>
|
||||||
|
|
||||||
|
<link rel='stylesheet' href='global.css'>
|
||||||
|
<link rel='stylesheet' href='prism.css'>
|
||||||
|
<link rel='manifest' href='manifest.json'>
|
||||||
|
<link rel='icon' type='image/png' href='favicon.png'>
|
||||||
|
|
||||||
|
<meta name='twitter:card' content='summary_large_image'>
|
||||||
|
<meta name='twitter:site' content='@sveltejs'>
|
||||||
|
<meta name='twitter:creator' content='@sveltejs'>
|
||||||
|
<meta name='twitter:image' content='https://svelte.dev/images/twitter-card.png'>
|
||||||
|
<meta name='og:image' content='https://svelte.dev/images/twitter-card.png'>
|
||||||
|
|
||||||
|
%svelte.head%
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id='svelte'>%svelte.body%</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,6 +0,0 @@
|
|||||||
import '@sveltejs/site-kit/base.css';
|
|
||||||
import * as sapper from '@sapper/app';
|
|
||||||
|
|
||||||
sapper.start({
|
|
||||||
target: document.querySelector('#sapper')
|
|
||||||
});
|
|
@ -0,0 +1,31 @@
|
|||||||
|
// need to do this first before importing database, etc.
|
||||||
|
import dotenv from 'dotenv';
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
import * as cookie from 'cookie';
|
||||||
|
import { get_user, sanitize_user } from './utils/auth';
|
||||||
|
import { query } from './utils/db';
|
||||||
|
|
||||||
|
/** @type {import('@sveltejs/kit').Handle} */
|
||||||
|
export async function handle({ request, resolve }) {
|
||||||
|
if (process.env['PGHOST']) {
|
||||||
|
// this is a convenient time to clear out expired sessions
|
||||||
|
query('delete from sessions where expiry < now()');
|
||||||
|
|
||||||
|
request.locals.cookies = cookie.parse(request.headers.cookie || '');
|
||||||
|
request.locals.user = await get_user(request.locals.cookies.sid);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await resolve(request);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {import('@sveltejs/kit').GetSession} */
|
||||||
|
export function getSession(request) {
|
||||||
|
return request.locals.user
|
||||||
|
? {
|
||||||
|
user: sanitize_user(request.locals.user)
|
||||||
|
}
|
||||||
|
: {};
|
||||||
|
}
|
@ -1,26 +1,25 @@
|
|||||||
import send from '@polka/send';
|
import { query as db_query } from '../../utils/db';
|
||||||
import { query } from '../../utils/db';
|
|
||||||
|
|
||||||
export async function get(req, res) {
|
export async function get({ query, locals }) {
|
||||||
if (req.user) {
|
if (locals.user) {
|
||||||
const page_size = 100;
|
const page_size = 100;
|
||||||
const offset = req.query.offset ? parseInt(req.query.offset) : 0;
|
const offset = query.get('offset') ? parseInt(query.get('offset')) : 0;
|
||||||
const rows = await query(`
|
const rows = await db_query(`
|
||||||
select g.uid, g.name, coalesce(g.updated_at, g.created_at) as updated_at
|
select g.uid, g.name, coalesce(g.updated_at, g.created_at) as updated_at
|
||||||
from gists g
|
from gists g
|
||||||
where g.user_id = $1
|
where g.user_id = $1
|
||||||
order by id desc
|
order by id desc
|
||||||
limit ${page_size + 1}
|
limit ${page_size + 1}
|
||||||
offset $2
|
offset $2
|
||||||
`, [req.user.id, offset]);
|
`, [locals.user.id, offset]);
|
||||||
|
|
||||||
rows.forEach(row => {
|
rows.forEach(row => {
|
||||||
row.uid = row.uid.replace(/-/g, '');
|
row.uid = row.uid.replace(/-/g, '');
|
||||||
});
|
});
|
||||||
|
|
||||||
const more = rows.length > page_size;
|
const more = rows.length > page_size;
|
||||||
send(res, 200, { apps: rows.slice(0, page_size), offset: more ? offset + page_size : null });
|
return { body: { apps: rows.slice(0, page_size), offset: more ? offset + page_size : null }};
|
||||||
} else {
|
} else {
|
||||||
send(res, 401);
|
return { status: 401 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
export const oauth = 'https://github.com/login/oauth';
|
export const oauth = 'https://github.com/login/oauth';
|
||||||
export const baseurl = process.env.BASEURL;
|
export const baseurl = process.env['BASEURL'];
|
||||||
export const secure = baseurl && baseurl.startsWith('https:');
|
export const secure = baseurl && baseurl.startsWith('https:');
|
||||||
|
|
||||||
export const client_id = process.env.GITHUB_CLIENT_ID;
|
export const client_id = process.env['GITHUB_CLIENT_ID'];
|
||||||
export const client_secret = process.env.GITHUB_CLIENT_SECRET;
|
export const client_secret = process.env['GITHUB_CLIENT_SECRET'];
|
||||||
|
@ -1,17 +1,18 @@
|
|||||||
import send from '@polka/send';
|
|
||||||
import * as cookie from 'cookie';
|
import * as cookie from 'cookie';
|
||||||
import { secure } from './_config.js';
|
import { secure } from './_config.js';
|
||||||
import { delete_session } from '../../utils/auth.js';
|
import { delete_session } from '../../utils/auth.js';
|
||||||
|
|
||||||
export async function get(req, res) {
|
export async function get(request) {
|
||||||
await delete_session(req.cookies.sid);
|
await delete_session(request.locals.cookies.sid);
|
||||||
|
|
||||||
send(res, 200, '', {
|
return {
|
||||||
|
headers: {
|
||||||
'Set-Cookie': cookie.serialize('sid', '', {
|
'Set-Cookie': cookie.serialize('sid', '', {
|
||||||
maxAge: -1,
|
maxAge: -1,
|
||||||
path: '/',
|
path: '/',
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure
|
secure
|
||||||
})
|
})
|
||||||
});
|
}
|
||||||
|
};
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
export function get(req, res) {
|
export function get() {
|
||||||
res.writeHead(302, { Location: 'https://discord.gg/yy75DKs' });
|
return {
|
||||||
res.end();
|
status: 302,
|
||||||
|
headers: { Location: 'https://discord.gg/yy75DKs' },
|
||||||
|
};
|
||||||
}
|
}
|
@ -1,12 +1,13 @@
|
|||||||
import send from '@polka/send';
|
|
||||||
import get_sections from './_sections.js';
|
import get_sections from './_sections.js';
|
||||||
|
|
||||||
let json;
|
let json;
|
||||||
|
|
||||||
export function get(req, res) {
|
export function get() {
|
||||||
if (!json || process.env.NODE_ENV !== 'production') {
|
if (!json || process.env.NODE_ENV !== 'production') {
|
||||||
json = get_sections();
|
json = get_sections();
|
||||||
}
|
}
|
||||||
|
|
||||||
send(res, 200, json);
|
return {
|
||||||
|
body: json
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,22 @@
|
|||||||
import send from '@polka/send';
|
|
||||||
import { get_examples } from './_examples.js';
|
import { get_examples } from './_examples.js';
|
||||||
|
|
||||||
let cached;
|
let cached;
|
||||||
|
|
||||||
export function get(req, res) {
|
export function get() {
|
||||||
try {
|
|
||||||
if (!cached || process.env.NODE_ENV !== 'production') {
|
if (!cached || process.env.NODE_ENV !== 'production') {
|
||||||
cached = get_examples().filter(section => section.title);
|
cached = get_examples().filter(section => section.title);
|
||||||
}
|
}
|
||||||
|
|
||||||
send(res, 200, cached);
|
try {
|
||||||
} catch (e) {
|
return {
|
||||||
send(res, e.status || 500, {
|
body: cached
|
||||||
|
};
|
||||||
|
} catch(err) {
|
||||||
|
return {
|
||||||
|
status: e.status || 500,
|
||||||
|
body: {
|
||||||
message: e.message
|
message: e.message
|
||||||
});
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,31 @@
|
|||||||
import send from '@polka/send';
|
|
||||||
import body from './_utils/body.js';
|
|
||||||
import { query } from '../../utils/db';
|
import { query } from '../../utils/db';
|
||||||
|
|
||||||
export async function post(req, res) {
|
export async function post({ locals, body }) {
|
||||||
const { user } = req;
|
const { user } = locals;
|
||||||
if (!user) return; // response already sent
|
if (!user) return; // response already sent
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { name, files } = await body(req);
|
const { name, files } = body;
|
||||||
|
|
||||||
const [row] = await query(`
|
const [row] = await query(`
|
||||||
insert into gists(user_id, name, files)
|
insert into gists(user_id, name, files)
|
||||||
values ($1, $2, $3) returning *`, [user.id, name, JSON.stringify(files)]);
|
values ($1, $2, $3) returning *`, [user.id, name, JSON.stringify(files)]);
|
||||||
|
|
||||||
send(res, 201, {
|
return {
|
||||||
|
status: 201,
|
||||||
|
body: {
|
||||||
uid: row.uid.replace(/-/g, ''),
|
uid: row.uid.replace(/-/g, ''),
|
||||||
name: row.name,
|
name: row.name,
|
||||||
files: row.files,
|
files: row.files,
|
||||||
owner: user.uid,
|
owner: user.uid,
|
||||||
});
|
}
|
||||||
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
send(res, 500, {
|
return {
|
||||||
|
status: 500,
|
||||||
|
body: {
|
||||||
error: err.message
|
error: err.message
|
||||||
});
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,22 @@
|
|||||||
<script context="module">
|
<script context="module">
|
||||||
export function preload({ query }) {
|
export function load({ page: { query }}) {
|
||||||
const { gist, example, version } = query;
|
const { gist, example, version } = query;
|
||||||
|
|
||||||
// redirect to v2 REPL if appropriate
|
// redirect to v2 REPL if appropriate
|
||||||
if (/^[^>]?[12]/.test(version)) {
|
if (/^[^>]?[12]/.test(version)) {
|
||||||
const q = Object.keys(query).map(key => `${key}=${query[key]}`).join('&');
|
const q = Object.keys(query).map(key => `${key}=${query[key]}`).join('&');
|
||||||
return this.redirect(302, `https://v2.svelte.dev/repl?${q}`);
|
return {
|
||||||
|
status: 302,
|
||||||
|
redirect: `https://v2.svelte.dev/repl?${q}`
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = gist || example || 'hello-world';
|
const id = gist || example || 'hello-world';
|
||||||
const q = version ? `?version=${version}` : ``;
|
const q = version ? `?version=${version}` : ``;
|
||||||
|
|
||||||
this.redirect(301, `repl/${id}${q}`);
|
return {
|
||||||
|
status: 301,
|
||||||
|
redirect: `/repl/${id}${q}`
|
||||||
|
};
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
@ -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)
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<script context="module">
|
||||||
|
export async function load({ fetch }) {
|
||||||
|
const sections = await fetch(`/tutorial.json`).then(r => r.json());
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
sections
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { setContext } from 'svelte';
|
||||||
|
|
||||||
|
export let sections;
|
||||||
|
setContext('tutorial', { sections });
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<slot></slot>
|
@ -1,15 +0,0 @@
|
|||||||
<script context="module">
|
|
||||||
export async function preload() {
|
|
||||||
const sections = await this.fetch(`tutorial.json`).then(r => r.json());
|
|
||||||
return { sections };
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import { setContext } from 'svelte';
|
|
||||||
|
|
||||||
export let sections;
|
|
||||||
setContext('tutorial', { sections });
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<slot></slot>
|
|
@ -1,5 +1,8 @@
|
|||||||
<script context="module">
|
<script context="module">
|
||||||
export function preload() {
|
export function load() {
|
||||||
this.redirect(301, 'tutorial/basics');
|
return {
|
||||||
|
status: 301,
|
||||||
|
redirect: '/tutorial/basics'
|
||||||
|
};
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
@ -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
|
// fail sometimes
|
||||||
if (Math.random() < 0.333) {
|
if (Math.random() < 0.333) {
|
||||||
res.statusCode = 400;
|
return {
|
||||||
res.end(`Failed to generate random number. Please try again`);
|
status: 400,
|
||||||
return;
|
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)
|
||||||
|
};
|
||||||
}
|
}
|
@ -1,43 +0,0 @@
|
|||||||
import polka from 'polka';
|
|
||||||
import send from '@polka/send';
|
|
||||||
import sirv from 'sirv';
|
|
||||||
import * as sapper from '@sapper/server';
|
|
||||||
import { sanitize_user, authenticate } from './utils/auth';
|
|
||||||
|
|
||||||
if (!process.env.PORT) {
|
|
||||||
process.env.PORT = 3000;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { PORT } = process.env;
|
|
||||||
|
|
||||||
const app = polka({
|
|
||||||
onError: (err, req, res) => {
|
|
||||||
const error = err.message || err;
|
|
||||||
const code = err.code || err.status || 500;
|
|
||||||
res.headersSent || send(res, code, { error }, {
|
|
||||||
'content-type': 'text/plain'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (process.env.PGHOST) {
|
|
||||||
app.use(authenticate());
|
|
||||||
}
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
sirv('static', {
|
|
||||||
dev: process.env.NODE_ENV === 'development',
|
|
||||||
setHeaders(res) {
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.hasHeader('Cache-Control') || res.setHeader('Cache-Control', 'max-age=600'); // 10min default
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
sapper.middleware({
|
|
||||||
session: req => ({
|
|
||||||
user: sanitize_user(req.user)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
app.listen(PORT);
|
|
@ -1,87 +0,0 @@
|
|||||||
import { timestamp, files, shell } from '@sapper/service-worker';
|
|
||||||
|
|
||||||
const ASSETS = `cache${timestamp}`;
|
|
||||||
|
|
||||||
// `shell` is an array of all the files generated by Rollup,
|
|
||||||
// `files` is an array of everything in the `static` directory
|
|
||||||
const to_cache = shell.concat(files.filter(file => {
|
|
||||||
const basename = file.split('/').pop();
|
|
||||||
if (basename[0] === '.') return false; // .DS_Store and friends
|
|
||||||
if (basename.endsWith('.mp3')) return false; // TODO others?
|
|
||||||
return true;
|
|
||||||
}));
|
|
||||||
const cached = new Set(to_cache);
|
|
||||||
|
|
||||||
self.addEventListener('install', event => {
|
|
||||||
event.waitUntil(
|
|
||||||
caches
|
|
||||||
.open(ASSETS)
|
|
||||||
.then(cache => cache.addAll(to_cache))
|
|
||||||
.then(() => {
|
|
||||||
self.skipWaiting();
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
self.addEventListener('activate', event => {
|
|
||||||
event.waitUntil(
|
|
||||||
caches.keys().then(async keys => {
|
|
||||||
// delete old caches
|
|
||||||
for (const key of keys) {
|
|
||||||
if (key !== ASSETS) await caches.delete(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.clients.claim();
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
self.addEventListener('fetch', event => {
|
|
||||||
if (event.request.method !== 'GET' || event.request.headers.has('range')) return;
|
|
||||||
|
|
||||||
const url = new URL(event.request.url);
|
|
||||||
|
|
||||||
// don't try to handle e.g. data: URIs
|
|
||||||
if (!url.protocol.startsWith('http')) return;
|
|
||||||
|
|
||||||
// ignore dev server requests
|
|
||||||
if (url.hostname === self.location.hostname && url.port !== self.location.port) return;
|
|
||||||
|
|
||||||
// always serve static files and Rollup-generated assets from cache
|
|
||||||
if (url.host === self.location.host && cached.has(url.pathname)) {
|
|
||||||
event.respondWith(caches.match(event.request));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// for pages, you might want to serve a shell `index.html` file,
|
|
||||||
// which Sapper has generated for you. It's not right for every
|
|
||||||
// app, but if it's right for yours then uncomment this section
|
|
||||||
/*
|
|
||||||
if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
|
|
||||||
event.respondWith(caches.match('/index.html'));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (event.request.cache === 'only-if-cached') return;
|
|
||||||
|
|
||||||
// for everything else, try the network first, falling back to
|
|
||||||
// cache if the user is offline. (If the pages never change, you
|
|
||||||
// might prefer a cache-first approach to a network-first one.)
|
|
||||||
event.respondWith(
|
|
||||||
caches
|
|
||||||
.open(`offline${timestamp}`)
|
|
||||||
.then(async cache => {
|
|
||||||
try {
|
|
||||||
const response = await fetch(event.request);
|
|
||||||
cache.put(event.request, response.clone());
|
|
||||||
return response;
|
|
||||||
} catch (err) {
|
|
||||||
const response = await cache.match(event.request);
|
|
||||||
if (response) return response;
|
|
||||||
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
@ -1,40 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang='en' class="theme-default typo-default">
|
|
||||||
<head>
|
|
||||||
<meta charset='utf-8'>
|
|
||||||
<meta name='viewport' content='width=device-width,initial-scale=1'>
|
|
||||||
<meta name='theme-color' content='#ff3e00'>
|
|
||||||
|
|
||||||
%sapper.base%
|
|
||||||
|
|
||||||
<link href=global.css rel=stylesheet>
|
|
||||||
<link href=prism.css rel=stylesheet>
|
|
||||||
<link rel='manifest' href='manifest.json'>
|
|
||||||
<link rel='icon' type='image/png' href='favicon.png'>
|
|
||||||
|
|
||||||
<meta name='twitter:card' content='summary_large_image'>
|
|
||||||
<meta name='twitter:site' content='@sveltejs'>
|
|
||||||
<meta name='twitter:creator' content='@sveltejs'>
|
|
||||||
<meta name='twitter:image' content='https://svelte.dev/images/twitter-card.png'>
|
|
||||||
<meta name='og:image' content='https://svelte.dev/images/twitter-card.png'>
|
|
||||||
|
|
||||||
<!-- Sapper generates a <style> tag containing critical CSS
|
|
||||||
for the current page. CSS for the rest of the app is
|
|
||||||
lazily loaded when it precaches secondary pages -->
|
|
||||||
%sapper.styles%
|
|
||||||
|
|
||||||
<!-- This contains the contents of the <svelte:head> component, if
|
|
||||||
the current page has one -->
|
|
||||||
%sapper.head%
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- The application will be rendered inside this element,
|
|
||||||
because `app/client.js` references it -->
|
|
||||||
<div id='sapper'>%sapper.html%</div>
|
|
||||||
|
|
||||||
<!-- Sapper creates a <script> tag containing `app/client.js`
|
|
||||||
and anything else it needs to hydrate the app and
|
|
||||||
initialise the router -->
|
|
||||||
%sapper.scripts%
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -0,0 +1,36 @@
|
|||||||
|
import adapter from '@sveltejs/adapter-node';
|
||||||
|
|
||||||
|
/** @type {import('@sveltejs/kit').Config} */
|
||||||
|
export default {
|
||||||
|
kit: {
|
||||||
|
adapter: adapter(),
|
||||||
|
target: '#svelte',
|
||||||
|
prerender: {
|
||||||
|
enabled: false
|
||||||
|
},
|
||||||
|
vite: () => ({
|
||||||
|
optimizeDeps: {
|
||||||
|
include: [
|
||||||
|
'codemirror',
|
||||||
|
'codemirror/mode/javascript/javascript.js',
|
||||||
|
'codemirror/mode/handlebars/handlebars.js',
|
||||||
|
'codemirror/mode/htmlmixed/htmlmixed.js',
|
||||||
|
'codemirror/mode/xml/xml.js',
|
||||||
|
'codemirror/mode/css/css.js',
|
||||||
|
'codemirror/mode/markdown/markdown.js',
|
||||||
|
'codemirror/addon/edit/closebrackets.js',
|
||||||
|
'codemirror/addon/edit/closetag.js',
|
||||||
|
'codemirror/addon/edit/continuelist.js',
|
||||||
|
'codemirror/addon/comment/comment.js',
|
||||||
|
'codemirror/addon/fold/foldcode.js',
|
||||||
|
'codemirror/addon/fold/foldgutter.js',
|
||||||
|
'codemirror/addon/fold/brace-fold.js',
|
||||||
|
'codemirror/addon/fold/xml-fold.js',
|
||||||
|
'codemirror/addon/fold/indent-fold.js',
|
||||||
|
'codemirror/addon/fold/markdown-fold.js',
|
||||||
|
'codemirror/addon/fold/comment-fold.js'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in new issue