-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathsuperstatic.js
More file actions
88 lines (69 loc) · 2.19 KB
/
superstatic.js
File metadata and controls
88 lines (69 loc) · 2.19 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Copyright 2015 Google Inc. All Rights Reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://github.com/firebase/superstatic/blob/master/LICENSE
*/
'use strict';
var _ = require('lodash');
var makerouter = require('router');
var fsProvider = require('./providers/fs');
var Responder = require('./responder');
var activator = require('./activator');
var notFound = require('./middleware/missing');
var promiseback = require('./utils/promiseback');
var loadConfigFile = require('./loaders/config-file');
var defaultCompressor = require('compression')();
var CWD = process.cwd();
var superstatic = function(spec) {
spec = _.assign({
stack: 'default'
}, spec);
if (!_.has(spec, 'fallthrough')) {
spec.fallthrough = true;
}
if (_.isString(spec.stack) && _.has(superstatic.stacks, spec.stack)) {
spec.stack = superstatic.stacks[spec.stack];
}
var router = makerouter();
var cwd = spec.cwd || CWD;
// Load data
var config = spec.config = loadConfigFile(spec.config);
config.errorPage = config.errorPage || '/404.html';
// Set up provider
var provider = spec.provider ? promiseback(spec.provider, 2) : fsProvider(_.extend({
cwd: cwd, // default current working directory
symlink: spec.symlink // symlink
}, config));
// Select compression middleware
var compressor;
if (_.isFunction(spec.compression)) {
compressor = spec.compression;
} else if (spec.compression || spec.gzip) {
compressor = defaultCompressor;
} else {
compressor = null;
}
// Setup helpers
router.use(function(req, res, next) {
res.superstatic = new Responder(req, res, {
provider: provider,
config: config,
compressor: compressor,
rewriters: spec.rewriters,
symlink: spec.symlink
});
next();
});
router.use(activator(spec, provider));
// Handle not found pages
if (!spec.fallthrough) {
router.use(notFound(spec));
}
return router;
};
superstatic.stacks = {
default: ['protect', 'redirects', 'headers', 'env', 'files', 'rewrites', 'missing'],
strict: ['redirects', 'headers', 'files', 'rewrites', 'missing']
};
module.exports = superstatic;