mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-19 05:45:27 +00:00
Remove async/await from lib
This commit is contained in:
119
lib/index.js
119
lib/index.js
@@ -23,9 +23,11 @@ function connect_dev() {
|
|||||||
heartbeat: 10 * 1000
|
heartbeat: 10 * 1000
|
||||||
}),
|
}),
|
||||||
|
|
||||||
async (req, res, next) => {
|
(req, res, next) => {
|
||||||
asset_cache = await watcher.ready;
|
watcher.ready.then(cache => {
|
||||||
next();
|
asset_cache = cache;
|
||||||
|
next();
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
set_req_pathname,
|
set_req_pathname,
|
||||||
@@ -126,7 +128,7 @@ function get_asset_handler(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get_route_handler(fn) {
|
function get_route_handler(fn) {
|
||||||
return async function handle_route(req, res, next) {
|
return function handle_route(req, res, next) {
|
||||||
const url = req.pathname;
|
const url = req.pathname;
|
||||||
|
|
||||||
const { client, server } = fn();
|
const { client, server } = fn();
|
||||||
@@ -136,57 +138,66 @@ function get_route_handler(fn) {
|
|||||||
'Content-Type': 'text/html'
|
'Content-Type': 'text/html'
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
let i = 0;
|
||||||
for (const route of route_manager.routes) {
|
function handle_route_inner() {
|
||||||
if (route.test(url)) {
|
if (i === route_manager.routes.length) {
|
||||||
req.params = route.exec(url);
|
next();
|
||||||
|
return;
|
||||||
const mod = require(server.entry)[route.id];
|
|
||||||
|
|
||||||
if (route.type === 'page') {
|
|
||||||
// preload main.js and current route
|
|
||||||
// TODO detect other stuff we can preload? images, CSS, fonts?
|
|
||||||
res.set('Link', `<${client.main_file}>;rel="preload";as="script", <${client.routes[route.id]}>;rel="preload";as="script"`);
|
|
||||||
|
|
||||||
const data = { params: req.params, query: req.query };
|
|
||||||
|
|
||||||
if (mod.preload) {
|
|
||||||
const promise = Promise.resolve(mod.preload(req)).then(preloaded => {
|
|
||||||
Object.assign(data, preloaded);
|
|
||||||
return mod.render(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
await templates.stream(res, 200, {
|
|
||||||
main: client.main_file,
|
|
||||||
html: promise.then(rendered => rendered.html),
|
|
||||||
head: promise.then(({ head }) => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`),
|
|
||||||
styles: promise.then(({ css }) => (css && css.code ? `<style>${css.code}</style>` : ''))
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
const { html, head, css } = mod.render(data);
|
|
||||||
|
|
||||||
const page = templates.render(200, {
|
|
||||||
main: client.main_file,
|
|
||||||
html,
|
|
||||||
head: `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`,
|
|
||||||
styles: (css && css.code ? `<style>${css.code}</style>` : '')
|
|
||||||
});
|
|
||||||
|
|
||||||
res.end(page);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
const handler = mod[req.method.toLowerCase()];
|
|
||||||
if (handler) handler(req, res, next);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
const route = route_manager.routes[i];
|
||||||
} catch(err) {
|
|
||||||
|
if (route.test(url)) {
|
||||||
|
req.params = route.exec(url);
|
||||||
|
|
||||||
|
const mod = require(server.entry)[route.id];
|
||||||
|
|
||||||
|
if (route.type === 'page') {
|
||||||
|
// preload main.js and current route
|
||||||
|
// TODO detect other stuff we can preload? images, CSS, fonts?
|
||||||
|
res.set('Link', `<${client.main_file}>;rel="preload";as="script", <${client.routes[route.id]}>;rel="preload";as="script"`);
|
||||||
|
|
||||||
|
const data = { params: req.params, query: req.query };
|
||||||
|
|
||||||
|
if (mod.preload) {
|
||||||
|
const promise = Promise.resolve(mod.preload(req)).then(preloaded => {
|
||||||
|
Object.assign(data, preloaded);
|
||||||
|
return mod.render(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
return templates.stream(res, 200, {
|
||||||
|
main: client.main_file,
|
||||||
|
html: promise.then(rendered => rendered.html),
|
||||||
|
head: promise.then(({ head }) => `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`),
|
||||||
|
styles: promise.then(({ css }) => (css && css.code ? `<style>${css.code}</style>` : ''))
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const { html, head, css } = mod.render(data);
|
||||||
|
|
||||||
|
const page = templates.render(200, {
|
||||||
|
main: client.main_file,
|
||||||
|
html,
|
||||||
|
head: `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`,
|
||||||
|
styles: (css && css.code ? `<style>${css.code}</style>` : '')
|
||||||
|
});
|
||||||
|
|
||||||
|
res.end(page);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
const handler = mod[req.method.toLowerCase()];
|
||||||
|
if (handler) handler(req, res, next);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
++i;
|
||||||
|
return handle_route_inner();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve().then(handle_route_inner).catch(err => {
|
||||||
res.status(500);
|
res.status(500);
|
||||||
res.end(templates.render(500, {
|
res.end(templates.render(500, {
|
||||||
title: (err && err.name) || 'Internal server error',
|
title: (err && err.name) || 'Internal server error',
|
||||||
@@ -194,7 +205,7 @@ function get_route_handler(fn) {
|
|||||||
error: escape_html(err && (err.details || err.message || err) || 'Unknown error'),
|
error: escape_html(err && (err.details || err.message || err) || 'Unknown error'),
|
||||||
stack: err && err.stack.split('\n').slice(1).join('\n')
|
stack: err && err.stack.split('\n').slice(1).join('\n')
|
||||||
}));
|
}));
|
||||||
}
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,10 +31,14 @@ function create_templates() {
|
|||||||
return key in data ? data[key] : '';
|
return key in data ? data[key] : '';
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
stream: async (res, data) => {
|
stream: (res, data) => {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
|
|
||||||
do {
|
function stream_inner() {
|
||||||
|
if (i >= template.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const start = template.indexOf('%sapper', i);
|
const start = template.indexOf('%sapper', i);
|
||||||
|
|
||||||
if (start === -1) {
|
if (start === -1) {
|
||||||
@@ -53,9 +57,14 @@ function create_templates() {
|
|||||||
const match = /sapper\.(\w+)/.exec(tag);
|
const match = /sapper\.(\w+)/.exec(tag);
|
||||||
if (!match || !(match[1] in data)) throw new Error(`Bad template`); // TODO ditto
|
if (!match || !(match[1] in data)) throw new Error(`Bad template`); // TODO ditto
|
||||||
|
|
||||||
res.write(await data[match[1]]);
|
return Promise.resolve(data[match[1]]).then(datamatch => {
|
||||||
i = end + 1;
|
res.write(datamatch);
|
||||||
} while (i < template.length);
|
i = end + 1;
|
||||||
|
return stream_inner();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve().then(stream_inner);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user