prevent unnecessary promise chains, and hoist handler function

This commit is contained in:
Rich Harris
2018-01-14 00:01:10 -05:00
3 changed files with 258 additions and 219 deletions

View File

@@ -23,9 +23,11 @@ function connect_dev() {
heartbeat: 10 * 1000
}),
async (req, res, next) => {
asset_cache = await watcher.ready;
(req, res, next) => {
watcher.ready.then(cache => {
asset_cache = cache;
next();
});
},
set_req_pathname,
@@ -125,21 +127,11 @@ function get_asset_handler(opts) {
};
}
const resolved = Promise.resolve();
function get_route_handler(fn) {
return async function handle_route(req, res, next) {
const url = req.pathname;
const { client, server } = fn();
// whatever happens, we're going to serve some HTML
res.set({
'Content-Type': 'text/html'
});
try {
for (const route of route_manager.routes) {
if (route.test(url)) {
req.params = route.exec(url);
function handle_route(route, req, res, next, { client, server }) {
req.params = route.exec(req.pathname);
const mod = require(server.entry)[route.id];
@@ -156,7 +148,7 @@ function get_route_handler(fn) {
return mod.render(data);
});
await templates.stream(res, 200, {
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>`),
@@ -174,10 +166,9 @@ function get_route_handler(fn) {
res.end(page);
}
return;
}
else {
const method = req.method.toLowerCase();
// 'delete' cannot be exported from a module because it is a keyword,
// so check for 'del' instead
@@ -185,13 +176,31 @@ function get_route_handler(fn) {
const handler = mod[method_export];
if (handler) {
handler(req, res, next);
return;
} else {
// no matching handler for method — 404
next();
}
}
}
return function find_route(req, res, next) {
const url = req.pathname;
// whatever happens, we're going to serve some HTML
res.set({
'Content-Type': 'text/html'
});
resolved
.then(() => {
for (const route of route_manager.routes) {
if (route.test(url)) return handle_route(route, req, res, next, fn());
}
// no matching route — 404
next();
} catch(err) {
})
.catch(err => {
res.status(500);
res.end(templates.render(500, {
title: (err && err.name) || 'Internal server error',
@@ -199,7 +208,7 @@ function get_route_handler(fn) {
error: escape_html(err && (err.details || err.message || err) || 'Unknown error'),
stack: err && err.stack.split('\n').slice(1).join('\n')
}));
}
});
};
}

View File

@@ -31,10 +31,14 @@ function create_templates() {
return key in data ? data[key] : '';
});
},
stream: async (res, data) => {
stream: (res, data) => {
let i = 0;
do {
function stream_inner() {
if (i >= template.length) {
return;
}
const start = template.indexOf('%sapper', i);
if (start === -1) {
@@ -53,9 +57,14 @@ function create_templates() {
const match = /sapper\.(\w+)/.exec(tag);
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 => {
res.write(datamatch);
i = end + 1;
} while (i < template.length);
return stream_inner();
});
}
return Promise.resolve().then(stream_inner);
}
};
})

View File

@@ -62,21 +62,27 @@ function run(env) {
});
}
before(async () => {
before(() => {
process.chdir(path.resolve(__dirname, '../app'));
process.env.NODE_ENV = env;
let exec_promise = Promise.resolve();
let sapper;
if (env === 'production') {
const cli = path.resolve(__dirname, '../../cli/index.js');
await exec(`${cli} build`);
exec_promise = exec(`${cli} build`);
}
return exec_promise.then(() => {
const resolved = require.resolve('../..');
delete require.cache[resolved];
const sapper = require(resolved);
sapper = require(resolved);
PORT = await getPort();
return getPort();
}).then(port => {
PORT = port;
base = `http://localhost:${PORT}`;
global.fetch = (url, opts) => {
@@ -85,11 +91,12 @@ function run(env) {
};
let captured;
capture = async fn => {
capture = fn => {
const result = captured = [];
await fn();
return fn().then(() => {
captured = null;
return result;
});
};
const app = express();
@@ -111,6 +118,7 @@ function run(env) {
});
});
});
});
after(() => {
server.close();
@@ -137,158 +145,171 @@ function run(env) {
});
});
afterEach(async () => {
await nightmare.end();
afterEach(() => {
return nightmare.end();
});
it('serves /', async () => {
const title = await nightmare
it('serves /', () => {
return nightmare
.goto(base)
.evaluate(() => document.querySelector('h1').textContent);
.evaluate(() => document.querySelector('h1').textContent)
.then(title => {
assert.equal(title, 'Great success!');
});
});
it('serves static route', async () => {
const title = await nightmare
it('serves static route', () => {
return nightmare
.goto(`${base}/about`)
.evaluate(() => document.querySelector('h1').textContent);
.evaluate(() => document.querySelector('h1').textContent)
.then(title => {
assert.equal(title, 'About this site');
});
});
it('serves dynamic route', async () => {
const title = await nightmare
it('serves dynamic route', () => {
return nightmare
.goto(`${base}/blog/what-is-sapper`)
.evaluate(() => document.querySelector('h1').textContent);
.evaluate(() => document.querySelector('h1').textContent)
.then(title => {
assert.equal(title, 'What is Sapper?');
});
it('navigates to a new page without reloading', async () => {
await nightmare.goto(base).wait(() => window.READY).wait(200);
const requests = await capture(async () => {
await nightmare.click('a[href="/about"]');
});
assert.equal(
await nightmare.path(),
'/about'
);
it('navigates to a new page without reloading', () => {
let requests;
return nightmare
.goto(base).wait(() => window.READY).wait(200)
.then(() => {
return capture(() => {
return nightmare.click('a[href="/about"]');
});
})
.then(reqs => {
requests = reqs;
assert.equal(
await nightmare.evaluate(() => document.title),
'About'
);
return nightmare.path();
})
.then(path => {
assert.equal(path, '/about');
return nightmare.evaluate(() => document.title);
})
.then(title => {
assert.equal(title, 'About');
assert.deepEqual(requests.map(r => r.url), []);
});
});
it('navigates programmatically', async () => {
await nightmare
it('navigates programmatically', () => {
return nightmare
.goto(`${base}/about`)
.wait(() => window.READY)
.click('.goto')
.wait(() => window.location.pathname === '/blog/what-is-sapper')
.wait(100);
assert.equal(
await nightmare.evaluate(() => document.title),
'What is Sapper?'
);
.wait(100)
.then(() => nightmare.evaluate(() => document.title))
.then(title => {
assert.equal(title, 'What is Sapper?');
});
});
it('prefetches programmatically', async () => {
await nightmare
it('prefetches programmatically', () => {
return nightmare
.goto(`${base}/about`)
.wait(() => window.READY);
const requests = await capture(async () => {
return await nightmare
.wait(() => window.READY)
.then(() => {
return capture(() => {
return nightmare
.click('.prefetch')
.wait(100);
});
})
.then(requests => {
assert.ok(!!requests.find(r => r.url === '/api/blog/why-the-name'));
});
});
it('scrolls to active deeplink', async () => {
const scrollY = await nightmare
it('scrolls to active deeplink', () => {
return nightmare
.goto(`${base}/blog/a-very-long-post#four`)
.wait(() => window.READY)
.wait(100)
.evaluate(() => window.scrollY);
.evaluate(() => window.scrollY)
.then(scrollY => {
assert.ok(scrollY > 0, scrollY);
});
});
it('reuses prefetch promise', async () => {
await nightmare
it('reuses prefetch promise', () => {
return nightmare
.goto(`${base}/blog`)
.wait(() => window.READY)
.wait(200);
const mouseover_requests = (await capture(async () => {
await nightmare
.wait(200)
.then(() => {
return capture(() => {
return nightmare
.mouseover('[href="/blog/what-is-sapper"]')
.wait(200);
})).map(r => r.url);
assert.deepEqual(mouseover_requests, [
});
})
.then(mouseover_requests => {
assert.deepEqual(mouseover_requests.map(r => r.url), [
'/api/blog/what-is-sapper'
]);
const click_requests = (await capture(async () => {
await nightmare
return capture(() => {
return nightmare
.click('[href="/blog/what-is-sapper"]')
.wait(200);
})).map(r => r.url);
assert.deepEqual(click_requests, []);
});
})
.then(click_requests => {
assert.deepEqual(click_requests.map(r => r.url), []);
});
});
it('cancels navigation if subsequent navigation occurs during preload', async () => {
await nightmare
it('cancels navigation if subsequent navigation occurs during preload', () => {
return nightmare
.goto(base)
.wait(() => window.READY)
.click('a[href="/slow-preload"]')
.wait(100)
.click('a[href="/about"]')
.wait(100);
.wait(100)
.then(() => nightmare.path())
.then(path => {
assert.equal(path, '/about');
assert.equal(
await nightmare.path(),
'/about'
);
return nightmare.evaluate(() => document.querySelector('h1').textContent);
})
.then(header_text => {
assert.equal(header_text, 'About this site');
assert.equal(
await nightmare.evaluate(() => document.querySelector('h1').textContent),
'About this site'
);
return nightmare.evaluate(() => window.fulfil({})).wait(100);
})
.then(() => nightmare.path())
.then(path => {
assert.equal(path, '/about');
await nightmare
.evaluate(() => window.fulfil({}))
.wait(100);
return nightmare.evaluate(() => document.querySelector('h1').textContent);
})
.then(header_text => {
assert.equal(header_text, 'About this site');
assert.equal(
await nightmare.path(),
'/about'
);
assert.equal(
await nightmare.evaluate(() => document.querySelector('h1').textContent),
'About this site'
);
return nightmare.evaluate(() => window.fulfil({})).wait(100);
});
});
it('passes entire request object to preload', async () => {
const html = await nightmare
it('passes entire request object to preload', () => {
return nightmare
.goto(`${base}/show-url`)
.evaluate(() => document.querySelector('p').innerHTML);
.evaluate(() => document.querySelector('p').innerHTML)
.then(html => {
assert.equal(html, `URL is /show-url`);
});
});
it('calls a delete handler', async () => {
await nightmare
@@ -305,9 +326,8 @@ function run(env) {
});
describe('headers', () => {
it('sets Content-Type and Link...preload headers', async () => {
const { headers } = await get('/');
it('sets Content-Type and Link...preload headers', () => {
return get('/').then(({ headers }) => {
assert.equal(
headers['Content-Type'],
'text/html'
@@ -320,6 +340,7 @@ function run(env) {
});
});
});
});
}
function exec(cmd) {