mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-20 14:25:07 +00:00
@@ -1 +0,0 @@
|
|||||||
export const IGNORE = '__SAPPER__IGNORE__';
|
|
||||||
@@ -5,7 +5,6 @@ import cookie from 'cookie';
|
|||||||
import devalue from 'devalue';
|
import devalue from 'devalue';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import URL from 'url';
|
import URL from 'url';
|
||||||
import { IGNORE } from '../constants';
|
|
||||||
import { Manifest, Page, Props, Req, Res } from './types';
|
import { Manifest, Page, Props, Req, Res } from './types';
|
||||||
import { build_dir, dev, src_dir } from '@sapper/internal/manifest-server';
|
import { build_dir, dev, src_dir } from '@sapper/internal/manifest-server';
|
||||||
import { stores } from '@sapper/internal/shared';
|
import { stores } from '@sapper/internal/shared';
|
||||||
@@ -328,8 +327,6 @@ export function get_page_handler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return function find_route(req: Req, res: Res, next: () => void) {
|
return function find_route(req: Req, res: Res, next: () => void) {
|
||||||
if (req[IGNORE]) return next();
|
|
||||||
|
|
||||||
if (req.path === '/service-worker-index.html') {
|
if (req.path === '/service-worker-index.html') {
|
||||||
const homePage = pages.find(page => page.pattern.test('/'));
|
const homePage = pages.find(page => page.pattern.test('/'));
|
||||||
handle_page(homePage, req, res);
|
handle_page(homePage, req, res);
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { IGNORE } from '../constants';
|
|
||||||
import { Req, Res, ServerRoute } from './types';
|
import { Req, Res, ServerRoute } from './types';
|
||||||
|
|
||||||
export function get_server_route_handler(routes: ServerRoute[]) {
|
export function get_server_route_handler(routes: ServerRoute[]) {
|
||||||
@@ -64,8 +63,6 @@ export function get_server_route_handler(routes: ServerRoute[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return function find_route(req: Req, res: Res, next: () => void) {
|
return function find_route(req: Req, res: Res, next: () => void) {
|
||||||
if (req[IGNORE]) return next();
|
|
||||||
|
|
||||||
for (const route of routes) {
|
for (const route of routes) {
|
||||||
if (route.pattern.test(req.path)) {
|
if (route.pattern.test(req.path)) {
|
||||||
handle_route(route, req, res, next);
|
handle_route(route, req, res, next);
|
||||||
@@ -75,4 +72,4 @@ export function get_server_route_handler(routes: ServerRoute[]) {
|
|||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { Handler, Req, Res } from './types';
|
|||||||
import { get_server_route_handler } from './get_server_route_handler';
|
import { get_server_route_handler } from './get_server_route_handler';
|
||||||
import { get_page_handler } from './get_page_handler';
|
import { get_page_handler } from './get_page_handler';
|
||||||
import { lookup } from './mime';
|
import { lookup } from './mime';
|
||||||
import { IGNORE } from '../constants';
|
|
||||||
|
|
||||||
export default function middleware(opts: {
|
export default function middleware(opts: {
|
||||||
session?: (req: Req, res: Res) => any,
|
session?: (req: Req, res: Res) => any,
|
||||||
@@ -15,15 +14,8 @@ export default function middleware(opts: {
|
|||||||
|
|
||||||
let emitted_basepath = false;
|
let emitted_basepath = false;
|
||||||
|
|
||||||
return compose_handlers([
|
return compose_handlers(ignore, [
|
||||||
ignore && ((req: Req, res: Res, next: () => void) => {
|
|
||||||
req[IGNORE] = should_ignore(req.path, ignore);
|
|
||||||
next();
|
|
||||||
}),
|
|
||||||
|
|
||||||
(req: Req, res: Res, next: () => void) => {
|
(req: Req, res: Res, next: () => void) => {
|
||||||
if (req[IGNORE]) return next();
|
|
||||||
|
|
||||||
if (req.baseUrl === undefined) {
|
if (req.baseUrl === undefined) {
|
||||||
let { originalUrl } = req;
|
let { originalUrl } = req;
|
||||||
if (req.url === '/' && originalUrl[originalUrl.length - 1] !== '/') {
|
if (req.url === '/' && originalUrl[originalUrl.length - 1] !== '/') {
|
||||||
@@ -73,24 +65,26 @@ export default function middleware(opts: {
|
|||||||
].filter(Boolean));
|
].filter(Boolean));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function compose_handlers(handlers: Handler[]) {
|
export function compose_handlers(ignore: any, handlers: Handler[]): Handler {
|
||||||
return (req: Req, res: Res, next: () => void) => {
|
const total = handlers.length;
|
||||||
let i = 0;
|
|
||||||
function go() {
|
|
||||||
const handler = handlers[i];
|
|
||||||
|
|
||||||
if (handler) {
|
function nth_handler(n: number, req: Req, res: Res, next: () => void) {
|
||||||
handler(req, res, () => {
|
if (n >= total) {
|
||||||
i += 1;
|
return next();
|
||||||
go();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go();
|
handlers[n](req, res, () => nth_handler(n+1, req, res, next));
|
||||||
};
|
}
|
||||||
|
|
||||||
|
return !ignore
|
||||||
|
? (req, res, next) => nth_handler(0, req, res, next)
|
||||||
|
: (req, res, next) => {
|
||||||
|
if (should_ignore(req.path, ignore)) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
nth_handler(0, req, res, next);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function should_ignore(uri: string, val: any) {
|
export function should_ignore(uri: string, val: any) {
|
||||||
@@ -116,8 +110,6 @@ export function serve({ prefix, pathname, cache_control }: {
|
|||||||
: (file: string) => (cache.has(file) ? cache : cache.set(file, fs.readFileSync(path.resolve(build_dir, file)))).get(file)
|
: (file: string) => (cache.has(file) ? cache : cache.set(file, fs.readFileSync(path.resolve(build_dir, file)))).get(file)
|
||||||
|
|
||||||
return (req: Req, res: Res, next: () => void) => {
|
return (req: Req, res: Res, next: () => void) => {
|
||||||
if (req[IGNORE]) return next();
|
|
||||||
|
|
||||||
if (filter(req)) {
|
if (filter(req)) {
|
||||||
const type = lookup(req.path);
|
const type = lookup(req.path);
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,19 @@ export default function create_manifest_data(cwd: string): ManifestData {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function find_layout(file_name: string, component_name: string, dir: string = '') {
|
||||||
|
const ext = component_extensions.find((ext) => fs.existsSync(path.join(cwd, dir, `${file_name}${ext}`)));
|
||||||
|
const file = posixify(path.join(dir, `${file_name}${ext}`))
|
||||||
|
|
||||||
|
return ext
|
||||||
|
? {
|
||||||
|
name: component_name,
|
||||||
|
file: file,
|
||||||
|
has_preload: has_preload(file)
|
||||||
|
}
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
const components: PageComponent[] = [];
|
const components: PageComponent[] = [];
|
||||||
const pages: Page[] = [];
|
const pages: Page[] = [];
|
||||||
const server_routes: ServerRoute[] = [];
|
const server_routes: ServerRoute[] = [];
|
||||||
@@ -61,11 +74,14 @@ export default function create_manifest_data(cwd: string): ManifestData {
|
|||||||
const is_dir = fs.statSync(resolved).isDirectory();
|
const is_dir = fs.statSync(resolved).isDirectory();
|
||||||
|
|
||||||
const ext = path.extname(basename);
|
const ext = path.extname(basename);
|
||||||
|
|
||||||
|
if (basename[0] === '_') return null;
|
||||||
|
if (basename[0] === '.' && basename !== '.well-known') return null;
|
||||||
if (!is_dir && !/^\.[a-z]+$/i.test(ext)) return null; // filter out tmp files etc
|
if (!is_dir && !/^\.[a-z]+$/i.test(ext)) return null; // filter out tmp files etc
|
||||||
|
|
||||||
const segment = is_dir
|
const segment = is_dir
|
||||||
? basename
|
? basename
|
||||||
: basename.slice(0, -path.extname(basename).length);
|
: basename.slice(0, -ext.length);
|
||||||
|
|
||||||
const parts = get_parts(segment);
|
const parts = get_parts(segment);
|
||||||
const is_index = is_dir ? false : basename.startsWith('index.');
|
const is_index = is_dir ? false : basename.startsWith('index.');
|
||||||
@@ -95,22 +111,17 @@ export default function create_manifest_data(cwd: string): ManifestData {
|
|||||||
.sort(comparator);
|
.sort(comparator);
|
||||||
|
|
||||||
items.forEach(item => {
|
items.forEach(item => {
|
||||||
if (item.basename[0] === '_') return;
|
|
||||||
|
|
||||||
if (item.basename[0] === '.') {
|
|
||||||
if (item.file !== '.well-known') return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const segments = parent_segments.slice();
|
const segments = parent_segments.slice();
|
||||||
|
|
||||||
if (item.is_index && segments.length > 0) {
|
if (item.is_index && segments.length > 0) {
|
||||||
const last_segment = segments[segments.length - 1].slice();
|
|
||||||
const suffix = item.basename
|
const suffix = item.basename
|
||||||
.slice(0, -path.extname(item.basename).length).
|
.slice(0, -item.ext.length)
|
||||||
replace('index', '');
|
.replace('index', '');
|
||||||
|
|
||||||
if (suffix) {
|
if (suffix) {
|
||||||
|
const last_segment = segments[segments.length - 1].slice();
|
||||||
const last_part = last_segment[last_segment.length - 1];
|
const last_part = last_segment[last_segment.length - 1];
|
||||||
|
|
||||||
if (last_part.dynamic) {
|
if (last_part.dynamic) {
|
||||||
last_segment.push({ dynamic: false, content: suffix });
|
last_segment.push({ dynamic: false, content: suffix });
|
||||||
} else {
|
} else {
|
||||||
@@ -130,16 +141,7 @@ export default function create_manifest_data(cwd: string): ManifestData {
|
|||||||
params.push(...item.parts.filter(p => p.dynamic).map(p => p.content));
|
params.push(...item.parts.filter(p => p.dynamic).map(p => p.content));
|
||||||
|
|
||||||
if (item.is_dir) {
|
if (item.is_dir) {
|
||||||
const ext = component_extensions.find((ext: string) => {
|
const component = find_layout('_layout', `${get_slug(item.file)}__layout`, item.file);
|
||||||
const index = path.join(dir, item.basename, `_layout${ext}`);
|
|
||||||
return fs.existsSync(index);
|
|
||||||
});
|
|
||||||
|
|
||||||
const component = ext && {
|
|
||||||
name: `${get_slug(item.file)}__layout`,
|
|
||||||
file: `${item.file}/_layout${ext}`,
|
|
||||||
has_preload: has_preload(`${item.file}/_layout${ext}`)
|
|
||||||
};
|
|
||||||
|
|
||||||
if (component) components.push(component);
|
if (component) components.push(component);
|
||||||
|
|
||||||
@@ -187,23 +189,8 @@ export default function create_manifest_data(cwd: string): ManifestData {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const root_ext = component_extensions.find(ext => fs.existsSync(path.join(cwd, `_layout${ext}`)));
|
const root = find_layout('_layout', 'main') || default_layout;
|
||||||
const root = root_ext
|
const error = find_layout('_error', 'error') || default_error;
|
||||||
? {
|
|
||||||
name: 'main',
|
|
||||||
file: `_layout${root_ext}`,
|
|
||||||
has_preload: has_preload(`_layout${root_ext}`)
|
|
||||||
}
|
|
||||||
: default_layout;
|
|
||||||
|
|
||||||
const error_ext = component_extensions.find(ext => fs.existsSync(path.join(cwd, `_error${ext}`)));
|
|
||||||
const error = error_ext
|
|
||||||
? {
|
|
||||||
name: 'error',
|
|
||||||
file: `_error${error_ext}`,
|
|
||||||
has_preload: has_preload(`_error${error_ext}`)
|
|
||||||
}
|
|
||||||
: default_error;
|
|
||||||
|
|
||||||
walk(cwd, [], [], []);
|
walk(cwd, [], [], []);
|
||||||
|
|
||||||
@@ -249,7 +236,7 @@ type Part = {
|
|||||||
spread?: boolean;
|
spread?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function is_spead(path: string) {
|
function is_spread(path: string) {
|
||||||
const spread_pattern = /\[\.{3}/g;
|
const spread_pattern = /\[\.{3}/g;
|
||||||
return spread_pattern.test(path)
|
return spread_pattern.test(path)
|
||||||
}
|
}
|
||||||
@@ -259,9 +246,9 @@ function comparator(
|
|||||||
b: { basename: string, parts: Part[], file: string, is_index: boolean }
|
b: { basename: string, parts: Part[], file: string, is_index: boolean }
|
||||||
) {
|
) {
|
||||||
if (a.is_index !== b.is_index) {
|
if (a.is_index !== b.is_index) {
|
||||||
if (a.is_index) return is_spead(a.file) ? 1 : -1;
|
if (a.is_index) return is_spread(a.file) ? 1 : -1;
|
||||||
|
|
||||||
return is_spead(b.file) ? -1 : 1;
|
return is_spread(b.file) ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const max = Math.max(a.parts.length, b.parts.length);
|
const max = Math.max(a.parts.length, b.parts.length);
|
||||||
|
|||||||
Reference in New Issue
Block a user