@kaliber/build 0.0.151 → 0.0.152-beta.2
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/serve.js +55 -26
- package/package.json +1 -1
package/lib/serve.js
CHANGED
@@ -69,12 +69,7 @@ app.use((err, req, res, next) => {
|
|
69
69
|
console.error(err)
|
70
70
|
if (reportError) reportError(err, req)
|
71
71
|
|
72
|
-
|
73
|
-
if (isProduction) {
|
74
|
-
findFile(req.path, internalServerError)
|
75
|
-
.then(file => file ? response.sendFile(file) : next())
|
76
|
-
.catch(next)
|
77
|
-
} else response.send(`<pre><title style='display: block;'>${err.stack || err.toString()}</title><pre>`)
|
72
|
+
serveInternalServerError(err, { req, res, next })
|
78
73
|
})
|
79
74
|
|
80
75
|
app.listen(port, () => console.log(`Server listening at port ${port}`))
|
@@ -84,7 +79,7 @@ async function resolveFile(req, res, next) {
|
|
84
79
|
const { path } = req
|
85
80
|
/** @type {Array<[string, (file:any) => any]>} */
|
86
81
|
const combinations = [
|
87
|
-
[indexWithRouting, file => serveIndexWithRouting(req, res,
|
82
|
+
[indexWithRouting, file => serveIndexWithRouting(file, { req, res, next })],
|
88
83
|
[notFound, file => res.status(404).sendFile(file)],
|
89
84
|
[index, file => res.status(200).sendFile(file)],
|
90
85
|
]
|
@@ -112,7 +107,7 @@ async function findFile(path, file) {
|
|
112
107
|
return null
|
113
108
|
}
|
114
109
|
|
115
|
-
|
110
|
+
function fileExists(path) {
|
116
111
|
return isProduction
|
117
112
|
? (!fileExists.cache || fileExists.cache[path] === undefined)
|
118
113
|
? accessFile(path).then(exists => (addPathToCache(path, exists), exists))
|
@@ -124,7 +119,7 @@ async function fileExists(path) {
|
|
124
119
|
}
|
125
120
|
}
|
126
121
|
|
127
|
-
|
122
|
+
function accessFile(path) {
|
128
123
|
return new Promise(resolve => access(path, err => resolve(!err)))
|
129
124
|
}
|
130
125
|
|
@@ -138,26 +133,60 @@ function possibleDirectories(path) {
|
|
138
133
|
return possibleDirectories
|
139
134
|
}
|
140
135
|
|
141
|
-
function serveIndexWithRouting(req, res,
|
136
|
+
function serveIndexWithRouting(file, { req, res, next }) {
|
142
137
|
const envRequire = isProduction ? require : require('import-fresh')
|
138
|
+
|
143
139
|
const routeTemplate = envRequire(file)
|
144
140
|
|
145
|
-
const routes = routeTemplate.routes
|
146
141
|
const location = parsePath(req.url)
|
147
142
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
143
|
+
const [dataOrPromise, template] = getDataAndRouteTemplate(routeTemplate, { location, req })
|
144
|
+
|
145
|
+
if (dataOrPromise.then)
|
146
|
+
dataOrPromise
|
147
|
+
.then(({ status, headers, data }) => {
|
148
|
+
const html = renderTemplate(template, location, { data })
|
149
|
+
res.status(status).set(headers).send(html)
|
150
|
+
})
|
151
|
+
.catch(error => serveInternalServerError(error, { req, res, next }))
|
152
|
+
else {
|
153
|
+
try {
|
154
|
+
const { data, status, headers } = dataOrPromise
|
155
|
+
const html = renderTemplate(template, location, { data })
|
156
|
+
res.status(status).set(headers).send(html)
|
157
|
+
} catch (error) {
|
158
|
+
serveInternalServerError(error, { req, res, next })
|
159
|
+
}
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
function getDataAndRouteTemplate(routeTemplate, { location, req }) {
|
164
|
+
const envRequire = isProduction ? require : require('import-fresh')
|
165
|
+
|
166
|
+
const routes = routeTemplate.routes
|
167
|
+
const dataOrPromise = (routes && routes.match(location, req)) || { status: 200, data: null }
|
168
|
+
|
169
|
+
if (!routes || !routes.resolveIndex)
|
170
|
+
return [dataOrPromise, routeTemplate]
|
171
|
+
|
172
|
+
const indexLocation = routes.resolveIndex(location, req)
|
173
|
+
if (!indexLocation)
|
174
|
+
return [dataOrPromise, routeTemplate]
|
175
|
+
|
176
|
+
const indexPath = resolve(target, publicPathDir, indexLocation, indexWithRouting)
|
177
|
+
return [dataOrPromise, envRequire(indexPath)]
|
178
|
+
}
|
179
|
+
|
180
|
+
function renderTemplate(template, location, { data }) {
|
181
|
+
const html = template({ location, data })
|
182
|
+
return html
|
183
|
+
}
|
184
|
+
|
185
|
+
function serveInternalServerError(error, { res, req, next }) {
|
186
|
+
const response = res.status(500)
|
187
|
+
if (isProduction) {
|
188
|
+
findFile(req.path, internalServerError)
|
189
|
+
.then(file => file ? response.sendFile(file) : next())
|
190
|
+
.catch(next)
|
191
|
+
} else response.send(`<pre><title style='display: block;'>${error.stack || error.toString()}</title><pre>`)
|
163
192
|
}
|
package/package.json
CHANGED