mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-11 19:04:30 +00:00
Refactor compose_handlers and ignore
This commit is contained in:
@@ -1 +0,0 @@
|
||||
export const IGNORE = '__SAPPER__IGNORE__';
|
||||
@@ -5,7 +5,6 @@ import cookie from 'cookie';
|
||||
import devalue from 'devalue';
|
||||
import fetch from 'node-fetch';
|
||||
import URL from 'url';
|
||||
import { IGNORE } from '../constants';
|
||||
import { Manifest, Page, Props, Req, Res } from './types';
|
||||
import { build_dir, dev, src_dir } from '@sapper/internal/manifest-server';
|
||||
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) {
|
||||
if (req[IGNORE]) return next();
|
||||
|
||||
if (req.path === '/service-worker-index.html') {
|
||||
const homePage = pages.find(page => page.pattern.test('/'));
|
||||
handle_page(homePage, req, res);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { IGNORE } from '../constants';
|
||||
import { Req, Res, ServerRoute } from './types';
|
||||
|
||||
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) {
|
||||
if (req[IGNORE]) return next();
|
||||
|
||||
for (const route of routes) {
|
||||
if (route.pattern.test(req.path)) {
|
||||
handle_route(route, req, res, next);
|
||||
@@ -75,4 +72,4 @@ export function get_server_route_handler(routes: ServerRoute[]) {
|
||||
|
||||
next();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import { Handler, Req, Res } from './types';
|
||||
import { get_server_route_handler } from './get_server_route_handler';
|
||||
import { get_page_handler } from './get_page_handler';
|
||||
import { lookup } from './mime';
|
||||
import { IGNORE } from '../constants';
|
||||
|
||||
export default function middleware(opts: {
|
||||
session?: (req: Req, res: Res) => any,
|
||||
@@ -15,15 +14,8 @@ export default function middleware(opts: {
|
||||
|
||||
let emitted_basepath = false;
|
||||
|
||||
return compose_handlers([
|
||||
ignore && ((req: Req, res: Res, next: () => void) => {
|
||||
req[IGNORE] = should_ignore(req.path, ignore);
|
||||
next();
|
||||
}),
|
||||
|
||||
return compose_handlers(ignore, [
|
||||
(req: Req, res: Res, next: () => void) => {
|
||||
if (req[IGNORE]) return next();
|
||||
|
||||
if (req.baseUrl === undefined) {
|
||||
let { originalUrl } = req;
|
||||
if (req.url === '/' && originalUrl[originalUrl.length - 1] !== '/') {
|
||||
@@ -73,24 +65,26 @@ export default function middleware(opts: {
|
||||
].filter(Boolean));
|
||||
}
|
||||
|
||||
export function compose_handlers(handlers: Handler[]) {
|
||||
return (req: Req, res: Res, next: () => void) => {
|
||||
let i = 0;
|
||||
function go() {
|
||||
const handler = handlers[i];
|
||||
export function compose_handlers(ignore: any, handlers: Handler[]): Handler {
|
||||
const total = handlers.length;
|
||||
|
||||
if (handler) {
|
||||
handler(req, res, () => {
|
||||
i += 1;
|
||||
go();
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
function nth_handler(n: number, req: Req, res: Res, next: () => void) {
|
||||
if (n >= total) {
|
||||
return 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) {
|
||||
@@ -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)
|
||||
|
||||
return (req: Req, res: Res, next: () => void) => {
|
||||
if (req[IGNORE]) return next();
|
||||
|
||||
if (filter(req)) {
|
||||
const type = lookup(req.path);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user