@srfnstack/spliffy 1.1.5 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srfnstack/spliffy",
3
- "version": "1.1.5",
3
+ "version": "1.2.0",
4
4
  "author": "snowbldr",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/narcolepticsnowman/spliffy",
@@ -33,7 +33,7 @@
33
33
  "cookie": "^0.4.1",
34
34
  "etag": "^1.8.1",
35
35
  "uuid": "^8.3.2",
36
- "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.34.0"
36
+ "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.40.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "helmet": "^4.6.0",
package/src/decorator.mjs CHANGED
@@ -45,20 +45,15 @@ export function decorateRequest (uwsReq, pathParameters, res, {
45
45
  const query = uwsReq.getQuery()
46
46
  req.path = uwsReq.getUrl()
47
47
  req.url = `${req.path}${query ? '?' + query : ''}`
48
+ const paramToIndex = pathParameters.reduce((acc, cur, i) => {
49
+ acc[cur] = i
50
+ return acc
51
+ }, {})
48
52
  req.spliffyUrl = {
49
53
  path: req.path,
50
54
  query: (query && parseQuery(query, decodeQueryParameters)) || {},
51
- pathParameters: {}
55
+ param: name => uwsReq.getParameter(paramToIndex[name])
52
56
  }
53
- if (pathParameters && pathParameters.length > 0) {
54
- for (const i in pathParameters) {
55
- req.spliffyUrl.pathParameters[pathParameters[i]] =
56
- decodePathParameters
57
- ? decodeURIComponent(uwsReq.getParameter(i))
58
- : uwsReq.getParameter(i)
59
- }
60
- }
61
- req.params = req.spliffyUrl.pathParameters
62
57
  req.query = req.spliffyUrl.query
63
58
  req.headers = {}
64
59
  uwsReq.forEach((header, value) => { req.headers[header] = value })
package/src/handler.mjs CHANGED
@@ -47,7 +47,11 @@ const endError = (res, e, refId, errorTransformer) => {
47
47
  e = errorTransformer(e, refId)
48
48
  }
49
49
  res.headers['x-ref-id'] = refId
50
- end(res, e.statusCode || 500, null, e.body || '')
50
+ const status = e.statusCode || 500
51
+ if (status === 500) {
52
+ log.error(e)
53
+ }
54
+ end(res, status, null, e.body || '')
51
55
  }
52
56
 
53
57
  const end = (res, defaultStatusCode, statusCodeOverride, body) => {
@@ -236,7 +240,7 @@ export const createNotFoundHandler = config => {
236
240
  res.statusCode = 500
237
241
  })
238
242
  } else {
239
- res.statusCode = 405
243
+ res.statusCode = 404
240
244
  res.end()
241
245
  }
242
246
  } else {
package/src/server.mjs CHANGED
@@ -39,7 +39,7 @@ const startHttpRedirect = (host, port) => {
39
39
  uws.App().any('/*',
40
40
  (req, res) => {
41
41
  try {
42
- res.writeHead(301, { Location: `https://${req.headers.host}:${port}${req.url}` })
42
+ res.writeHead(301, { Location: `https://${req.headers.get('host')}:${port}${req.url}` })
43
43
  res.end()
44
44
  } catch (e) {
45
45
  log.error(`Failed to handle http request on port ${port}`, req.url, e)
@@ -91,18 +91,27 @@ export async function startServer (config) {
91
91
  if (config.defaultRoute && config.defaultRoute.match(routePattern)) {
92
92
  config.defaultRouteHandler = route
93
93
  }
94
+ let hadSlash = false
95
+ if (route.urlPath.endsWith('/')) {
96
+ hadSlash = true
97
+ route.urlPath = route.urlPath.substring(0, route.urlPath.length - 1)
98
+ }
94
99
  for (const method in route.handlers) {
95
100
  const theHandler = createHandler(route.handlers[method], route.middleware, route.pathParameters, config)
96
101
  app[appMethods[method]](route.urlPath, theHandler)
97
- if (route.urlPath.endsWith('/') && route.urlPath.length > 1) {
98
- app[appMethods[method]](route.urlPath.substr(0, route.urlPath.length - 1), theHandler)
102
+ if (hadSlash && config.serveRoutesWithSlash) {
103
+ app[appMethods[method]](route.urlPath + '/', theHandler)
99
104
  }
100
105
  if (route.urlPath.endsWith('/*')) {
101
106
  app[appMethods[method]](route.urlPath.substr(0, route.urlPath.length - 2), theHandler)
102
107
  }
103
108
  }
104
109
  if (config.autoOptions && !route.handlers.OPTIONS) {
105
- app.options(route.urlPath, optionsHandler(config, route.middleware, Object.keys(route.handlers).join(', ')))
110
+ const theHandler = optionsHandler(config, route.middleware, Object.keys(route.handlers).join(', '))
111
+ app.options(route.urlPath, theHandler)
112
+ if (hadSlash && config.serveRoutesWithSlash) {
113
+ app.options(route.urlPath + '/', theHandler)
114
+ }
106
115
  }
107
116
  }
108
117