@srfnstack/spliffy 1.2.3 → 1.2.4
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 +1 -1
- package/src/handler.mjs +1 -1
- package/src/routes.mjs +4 -2
- package/src/server.mjs +18 -12
package/package.json
CHANGED
package/src/handler.mjs
CHANGED
|
@@ -185,7 +185,7 @@ const handleRequest = async (req, res, handler, middleware, errorTransformer) =>
|
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
export const HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE']
|
|
188
|
+
export const HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE', 'WEBSOCKET']
|
|
189
189
|
|
|
190
190
|
let currentDate = new Date().toISOString()
|
|
191
191
|
setInterval(() => { currentDate = new Date().toISOString() }, 1000)
|
package/src/routes.mjs
CHANGED
|
@@ -140,8 +140,10 @@ const buildJSHandlerRoute = async (name, filePath, urlPath, inheritedMiddleware,
|
|
|
140
140
|
if (typeof loadedHandler.handler === 'function') {
|
|
141
141
|
handler = loadedHandler.handler
|
|
142
142
|
}
|
|
143
|
-
if (typeof handler !== 'function') {
|
|
144
|
-
throw new Error(`Request method ${method} in file ${filePath} must be a function. Got: ${handlers[method]}`)
|
|
143
|
+
if (typeof handler !== 'function' && method !== 'WEBSOCKET') {
|
|
144
|
+
throw new Error(`Request method ${method} in file ${filePath} must be a function. Got: ${typeof handlers[method]}`)
|
|
145
|
+
} else if(method === 'WEBSOCKET' && typeof handler !== 'object') {
|
|
146
|
+
throw new Error(`Websocket in file ${filePath} must be an object. Got: ${typeof handlers[method]}`)
|
|
145
147
|
}
|
|
146
148
|
if (!('streamRequestBody' in loadedHandler)) {
|
|
147
149
|
handler.streamRequestBody = handlers.streamRequestBody
|
package/src/server.mjs
CHANGED
|
@@ -19,18 +19,19 @@ const appMethods = {
|
|
|
19
19
|
OPTIONS: 'options',
|
|
20
20
|
HEAD: 'head',
|
|
21
21
|
CONNECT: 'connect',
|
|
22
|
-
TRACE: 'trace'
|
|
22
|
+
TRACE: 'trace',
|
|
23
|
+
WEBSOCKET: 'ws'
|
|
23
24
|
}
|
|
24
25
|
const optionsHandler = (config, middleware, methods) => {
|
|
25
|
-
return createHandler(() => ({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
return createHandler(() => ( {
|
|
27
|
+
headers: {
|
|
28
|
+
allow: methods
|
|
29
|
+
},
|
|
30
|
+
statusCode: 204
|
|
31
|
+
} ),
|
|
32
|
+
middleware,
|
|
33
|
+
[],
|
|
34
|
+
config
|
|
34
35
|
)
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -68,7 +69,7 @@ const getHttpsApp = (key, cert) => {
|
|
|
68
69
|
export async function startServer (config) {
|
|
69
70
|
if (!state.initialized) {
|
|
70
71
|
state.initialized = true
|
|
71
|
-
const routes = [...getNodeModuleRoutes(config), ...(await findRoutes(config))]
|
|
72
|
+
const routes = [...getNodeModuleRoutes(config), ...( await findRoutes(config) )]
|
|
72
73
|
let app, port
|
|
73
74
|
if (config.httpsKeyFile) {
|
|
74
75
|
app = getHttpsApp(config.secure)
|
|
@@ -97,7 +98,12 @@ export async function startServer (config) {
|
|
|
97
98
|
route.urlPath = route.urlPath.substring(0, route.urlPath.length - 1)
|
|
98
99
|
}
|
|
99
100
|
for (const method in route.handlers) {
|
|
100
|
-
|
|
101
|
+
let theHandler = null
|
|
102
|
+
if (method === 'WEBSOCKET') {
|
|
103
|
+
theHandler = route.handlers[method]
|
|
104
|
+
} else {
|
|
105
|
+
theHandler = createHandler(route.handlers[method], route.middleware, route.pathParameters, config)
|
|
106
|
+
}
|
|
101
107
|
app[appMethods[method]](route.urlPath, theHandler)
|
|
102
108
|
if (hadSlash && config.serveRoutesWithSlash) {
|
|
103
109
|
app[appMethods[method]](route.urlPath + '/', theHandler)
|