forked from firebase/superstatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuperstatic.js
More file actions
107 lines (89 loc) · 3.1 KB
/
superstatic.js
File metadata and controls
107 lines (89 loc) · 3.1 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright (c) 2022 Google LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const makerouter = require("router");
const fsProvider = require("./providers/fs");
const Responder = require("./responder");
const activator = require("./activator");
const notFound = require("./middleware/missing");
const promiseback = require("./utils/promiseback");
const loadConfigFile = require("./loaders/config-file");
const defaultCompressor = require("compression")();
const CWD = process.cwd();
/**
* Superstatic returns a router that can be used in a server.
* @param {MiddlewareOptions} spec superstatic options.
* @returns {HandleFunction} router handler.
*/
const superstatic = function (spec = {}) {
spec.stack ??= "default";
spec.fallthrough ??= true;
if (typeof spec.stack === "string" && superstatic.stacks[spec.stack]) {
spec.stack = superstatic.stacks[spec.stack];
}
const router = makerouter();
const cwd = spec.cwd ?? CWD;
// Load data
/** @type {Configuration} */
const config = (spec.config = loadConfigFile(spec.config));
config.errorPage = config.errorPage ?? "/404.html";
// Set up provider
const provider = spec.provider
? promiseback(spec.provider, 2)
: fsProvider({ cwd, ...config });
// Select compression middleware
let compressor;
if (typeof spec.compression === "function") {
compressor = spec.compression;
} else if (spec.compression ?? spec.gzip) {
compressor = defaultCompressor;
} else {
compressor = null;
}
// Setup helpers
router.use((req, res, next) => {
res.superstatic = new Responder(req, res, {
provider: provider,
config: config,
compressor: compressor,
rewriters: spec.rewriters,
});
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;