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