-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (43 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const debug = require('debug')('koa-mount-html');
const compose = require('koa-compose');
const assert = require('assert');
const path = require('path');
const url = require('url');
const CONTENT_TYPE_HTML = 'text/html';
function acceptsHtml (acceptsString) {
return acceptsString.indexOf('application/json') !== 0
&& (acceptsString.indexOf('text/html') !== -1 || acceptsString.indexOf('*/*') !== -1)
}
function *noop(){}
exports = module.exports = function mountHtml(app, opts) {
opts = opts || {};
const downstream = app.middleware
? compose(app.middleware)
: app;
assert(downstream, 'koa app / middleware required');
return function* (upstream) {
if (opts.defer) {
yield* upstream;
// response is already handled
if (this.body != null || this.status != 404) return;
}
if (this.method === 'GET' && this.accepts('text/html')) {
const parsedUrl = url.parse(this.url);
if (opts.includeDotPaths || parsedUrl.pathname.indexOf('.') === -1) {
debug('mounting for url "%s"', this.url);
if (opts.defer) {
yield* downstream.call(this, noop());
}
else {
yield* downstream.call(this, function * callback(){
yield* upstream;
}.call(this));
}
}
else {
debug('not mounting request for a path containing a \'.\' "%s"', parsedUrl.pathname);
}
}
yield *upstream;
};
};