@srfnstack/spliffy 0.6.1 → 0.8.2

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/src/handler.js DELETED
@@ -1,275 +0,0 @@
1
- const log = require( './log' )
2
- const serverConfig = require( './serverConfig' )
3
- const content = require( './content' )
4
- const { executeMiddleware } = require( "./middleware" );
5
- const { decorateResponse } = require( './expressShim.js' )
6
- const { decorateRequest } = require( './expressShim.js' )
7
- const uuid = require( 'uuid' ).v4
8
- const { Readable } = require( 'stream' )
9
-
10
- /**
11
- * Execute the handler
12
- * @param url The url being requested
13
- * @param res The uws response object
14
- * @param req The uws request object
15
- * @param body The request body
16
- * @param handler The handler function for the route
17
- * @param middleware The middleware that applies to this request
18
- */
19
- const executeHandler = async ( url, res, req, body, handler, middleware ) => {
20
- try {
21
- if( body )
22
- body = content.deserialize( body, req.headers['content-type'] )
23
- } catch( e ) {
24
- log.error( 'Failed to parse request.', e )
25
- end( res, 400, 'Failed to parse request body' )
26
- return
27
- }
28
-
29
- try {
30
- let handled = handler(
31
- {
32
- url,
33
- body,
34
- headers: req.headers,
35
- req,
36
- res
37
- } )
38
- if( handled && typeof handled.then == 'function' ) {
39
- await handled.then( ( h ) => finalizeResponse( req, res, h ) )
40
- .catch(
41
- e => {
42
- let refId = uuid()
43
- log.error( 'handler failed', e, refId )
44
- endError( res, e, refId )
45
- }
46
- )
47
- } else {
48
- finalizeResponse( req, res, handled )
49
- }
50
-
51
- } catch( e ) {
52
- let refId = uuid()
53
- log.error( 'handler failed', e, refId )
54
- await tryExecuteMiddleware( middleware, req, res, e, refId )
55
- endError( res, e, refId )
56
- }
57
- }
58
-
59
- const endError = ( res, e, refId ) => {
60
- if( e.body && typeof e.body !== 'string' ) {
61
- e.body = JSON.stringify( e.body )
62
- }
63
- if( serverConfig.current.errorTransformer ) {
64
- e = serverConfig.current.errorTransformer( e, refId )
65
- }
66
- end( res, e.statusCode || e.status || 500, ( e.statusMessage || e.message || 'Internal Server Error' ) + ' refId: ' + refId, e.body || '' )
67
- }
68
-
69
- const end = ( res, code, message, body ) => {
70
- if( !res.statusCode ) res.statusCode = code
71
- if( !res.statusMessage ) res.statusMessage = message
72
- if( body instanceof Readable ) {
73
- streamResponse( res, body )
74
- } else {
75
- res.end( serializeBody( body, res ) || '' )
76
- }
77
- }
78
-
79
- const logAccess = function( req, res ) {
80
- const start = new Date().getTime()
81
- return () => {
82
- log.access( req.remoteAddress, res.proxiedRemoteAddress || '', res.statusCode, req.method, req.url, new Date().getTime() - start + 'ms' )
83
- }
84
- }
85
-
86
- const finalizeResponse = ( req, res, handled ) => {
87
- if( !res.finalized ) {
88
- res.finalized = true
89
- if( !handled ) {
90
- //if no error was thrown, assume everything is fine. Otherwise each handler must return truthy which is un-necessary for methods that don't need to return anything
91
- end( res, 200, 'OK' )
92
- } else {
93
- if( handled.body || handled.status || handled.statusMessage || handled.statusCode || handled.headers ) {
94
- if( handled.headers ) {
95
- res.assignHeaders( handled.headers )
96
- }
97
- end( res, handled.statusCode || 200, handled.statusMessage || 'OK', handled.body )
98
- } else {
99
- end( res, 200, 'OK', handled )
100
- }
101
- }
102
- }
103
- }
104
-
105
- function onAbortedOrFinishedResponse( res, readStream ) {
106
- if( res.id === -1 ) {
107
- log.error( "ERROR! onAbortedOrFinishedResponse called twice for the same res!" )
108
- } else {
109
- readStream.destroy();
110
- }
111
- res.id = -1;
112
- }
113
-
114
- function toArrayBuffer( buffer ) {
115
- return buffer.buffer.slice( buffer.byteOffset, buffer.byteOffset + buffer.byteLength );
116
- }
117
-
118
- const streamResponse = ( res, readStream ) => {
119
- let totalSize = res.getHeader( 'content-length' );
120
- if( !totalSize ) {
121
- throw new Error( 'the Content-Length header must be set when responding with a stream body' )
122
- }
123
- totalSize = parseInt( totalSize )
124
- if( totalSize <= 0 ) {
125
- readStream.close()
126
- res.end( '' )
127
- return
128
- }
129
- //borrowed from https://github.com/uNetworking/uWebSockets.js/blob/8ba1edc0bbd05f97f6b1c8a03fd93be89bec458d/examples/VideoStreamer.js#L38
130
- readStream.on( 'data', chunk => {
131
- const ab = toArrayBuffer( chunk );
132
- let lastOffset = res.getWriteOffset();
133
- let [ok, done] = res.tryEnd( ab, totalSize );
134
- if( done ) {
135
- onAbortedOrFinishedResponse( res, readStream );
136
- res.writableEnded = true
137
- res.end()
138
- } else if( !ok ) {
139
- readStream.pause();
140
- res.ab = ab;
141
- res.abOffset = lastOffset;
142
- res.onWritable( ( offset ) => {
143
- let [ok, done] = res.tryEnd( res.ab.slice( offset - res.abOffset ), totalSize );
144
- if( done ) {
145
- onAbortedOrFinishedResponse( res, readStream );
146
- res.writableEnded = true
147
- res.end()
148
- } else if( ok ) {
149
- readStream.resume();
150
- }
151
- return ok;
152
- } );
153
- }
154
- } )
155
- .on( 'error', e => endError( res, e, uuid() ) )
156
- }
157
-
158
- const serializeBody = ( body, res ) => {
159
- let contentType = res.getHeader( 'content-type' )
160
- if( typeof body === 'string' ) {
161
- if( !contentType ) {
162
- res.headers['content-type'] = 'text/plain'
163
- }
164
- return body
165
- } else if( body instanceof Readable ) {
166
- return body
167
- }
168
- let serialized = content.serialize( body, contentType )
169
-
170
- if( serialized && serialized.contentType && !contentType ) {
171
- res.headers['content-type'] = serialized.contentType
172
- }
173
- return serialized && serialized.data || ''
174
- }
175
-
176
- async function tryExecuteMiddleware( middleware, req, res, e, refId ) {
177
- if( !middleware ) return
178
-
179
- let applicableMiddleware = middleware[req.method]
180
- if( middleware.ALL ) {
181
- if( applicableMiddleware ) applicableMiddleware = middleware.ALL.concat( applicableMiddleware )
182
- else applicableMiddleware = middleware.ALL
183
- }
184
-
185
- if( !applicableMiddleware || applicableMiddleware.length === 0 ) {
186
- return
187
- }
188
- const errorMiddleware = applicableMiddleware.filter( mw => mw.length === 4 )
189
- const middlewarez = applicableMiddleware.filter( mw => mw.length === 3 )
190
- try {
191
- if( e )
192
- await executeMiddleware( middlewarez, errorMiddleware, req, res, e )
193
- else
194
- await executeMiddleware( middlewarez, errorMiddleware, req, res )
195
- } catch( e ) {
196
- if( !refId ) refId = uuid()
197
- console.log( 'Failed executing middleware while handling error: ' + e )
198
- endError( res, e, refId )
199
- }
200
- }
201
-
202
- let currentDate = new Date().toUTCString()
203
- setInterval( () => currentDate = new Date().toUTCString(), 1000 )
204
-
205
- const handleRequest = async ( req, res, handler, middleware, pathParameters ) => {
206
- res.headers['server'] = 'uWebSockets.js'
207
- res.headers['date'] = currentDate
208
- try {
209
- let buffer
210
- let reqBody = await new Promise(
211
- resolve =>
212
- res.onData( async ( data, isLast ) => {
213
- if( isLast ) {
214
- buffer = data.byteLength > 0 ? Buffer.concat( [buffer, Buffer.from( data )].filter( b => b ) ) : buffer
215
- resolve( buffer )
216
- }
217
- buffer = Buffer.concat( [buffer, Buffer.from( data )].filter( b => b ) )
218
- } )
219
- )
220
- await tryExecuteMiddleware( middleware, req, res )
221
- if( !res.writableEnded ) {
222
- await executeHandler( req.spliffyUrl, res, req, reqBody, handler, middleware )
223
- }
224
- } catch( e ) {
225
- let refId = uuid()
226
- log.error( 'Handling request failed', e, refId )
227
- await tryExecuteMiddleware( middleware, req, res, e, refId );
228
- if( !res.writableEnded )
229
- endError( res, e, refId )
230
- }
231
- }
232
- const HTTP_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE']
233
- module.exports =
234
- {
235
- create: ( handler, middleware, pathParameters ) => function( res, req ) {
236
- try {
237
- req = decorateRequest( req, pathParameters, res );
238
- res = decorateResponse( res, req, finalizeResponse );
239
-
240
- if( serverConfig.current.logAccess ) {
241
- res.onEnd = logAccess( req, res )
242
- }
243
- handleRequest( req, res, handler, middleware, pathParameters )
244
- } catch( e ) {
245
- log.error( 'Failed handling request', e )
246
- }
247
- },
248
- notFound: ( res, req ) => {
249
- try {
250
- let params = serverConfig.current.defaultRoute && serverConfig.current.defaultRoute.pathParameters || []
251
- req = decorateRequest( req, params, res );
252
- res = decorateResponse( res, req, finalizeResponse );
253
- if( serverConfig.current.logAccess ) {
254
- res.onEnd = logAccess( req, res )
255
- }
256
- if(serverConfig.current.defaultRoute && typeof serverConfig.current.defaultRoute){
257
- let route = serverConfig.current.defaultRoute
258
- if(route.handlers && route.handlers[req.method]){
259
- handleRequest(req, res, serverConfig.current.defaultRoute.handlers[req.method])
260
- } else {
261
- res.statusCode = 405
262
- res.statusMessage = 'Method Not Allowed'
263
- res.end()
264
- }
265
- } else {
266
- res.statusCode = 404
267
- res.statusMessage = 'Not Found'
268
- res.end()
269
- }
270
- } catch( e ) {
271
- log.error( 'Failed handling request', e )
272
- }
273
- },
274
- HTTP_METHODS
275
- }
package/src/index.js DELETED
@@ -1,25 +0,0 @@
1
- const log = require( './log' )
2
-
3
- /**
4
- * Startup function for the spliffy server
5
- * Startup will exponentially back off on consecutive failures
6
- * @param config See https://github.com/narcolepticsnowman/spliffy#config
7
- * @returns {Promise<Server>} Either the https server if https is configured or the http server
8
- */
9
- const spliffy = ( config ) => require( './start' )( config )
10
-
11
- /**
12
- * A helper for creating a redirect handler
13
- * @param location The location to redirect to
14
- * @param permanent Whether this is a permanent redirect or not
15
- */
16
- spliffy.redirect = ( location, permanent = true ) => () => ( {
17
- statusCode: permanent ? 301 : 302,
18
- headers: {
19
- 'location': location
20
- }
21
- } )
22
-
23
- spliffy.log = log
24
-
25
- module.exports = spliffy
package/src/log.js DELETED
@@ -1,43 +0,0 @@
1
- const inspect = require( 'util' ).inspect
2
- const levelOrder = { DEBUG: 0, INFO: 1, 'GOOD NEWS EVERYONE!': 1, WARN: 2, ERROR: 3, NONE: 4 }
3
- let logAccess = true
4
- let logLevel = levelOrder.INFO
5
-
6
- const ifLevelEnabled = ( fn, level, args ) => {
7
- const configLevel = levelOrder[logLevel] || levelOrder.INFO
8
- if( !levelOrder[level] || levelOrder[level] >= configLevel ) {
9
- fn( `[${new Date().toISOString()}] [${level}] ${args.map( a => typeof a === 'string' ? a : inspect( a, { depth: null } ) ).join( ' ' )}` )
10
- }
11
- }
12
-
13
- module.exports = {
14
- setLogAccess( enable ) {
15
- logAccess = !!enable
16
- },
17
- setLogLevel( level ) {
18
- if( !levelOrder.hasOwnProperty( level ) ) {
19
- throw `Invalid level: ${level}`
20
- }
21
- logLevel = level
22
- },
23
- warning( e ) {
24
- ifLevelEnabled( console.warn, 'WARN', [...arguments] )
25
- },
26
- warn( e ) {
27
- ifLevelEnabled( console.warn, 'WARN', [...arguments] )
28
- },
29
- info( e ) {
30
- ifLevelEnabled( console.info, 'INFO', [...arguments] )
31
- },
32
- gne( e ) {
33
- ifLevelEnabled( console.info, 'GOOD NEWS EVERYONE!', [...arguments] )
34
- },
35
- access( e ) {
36
- if( logAccess ) {
37
- ifLevelEnabled( console.info, 'ACCESS', [...arguments] )
38
- }
39
- },
40
- error( e ) {
41
- ifLevelEnabled( console.error, 'ERROR', [...arguments].map( arg => arg.stack ? arg.stack : arg ) )
42
- }
43
- }
package/src/middleware.js DELETED
@@ -1,116 +0,0 @@
1
- const log = require( './log' )
2
- /**
3
- * middleware is stored as an object where the properties are request methods the middleware applies to
4
- * if a middleware applies to all methods, the property ALL is used
5
- * example:
6
- * {
7
- * GET: [(req,res,next)=>console.log('ice cream man')]
8
- * POST: [(req,res,next)=>console.log('gelato')]
9
- * ALL: [(req,res,next)=>console.log('bruce banner')]
10
- * }
11
- */
12
- const mergeMiddleware = ( incoming, existing ) => {
13
- const mergeInto = cloneMiddleware( existing )
14
-
15
- validateMiddleware( incoming )
16
- if( Array.isArray( incoming ) ) {
17
- mergeInto.ALL = ( existing.ALL || [] ).concat( incoming )
18
- } else if( typeof incoming === 'object' ) {
19
- for( let method in incoming ) {
20
- let upMethod = method.toUpperCase()
21
- mergeInto[upMethod] = ( mergeInto[method] || [] ).concat( incoming[upMethod] || [] )
22
- }
23
- }
24
- return mergeInto
25
- }
26
-
27
- const cloneMiddleware = ( middleware ) => {
28
- const clone = { ...middleware }
29
- for( let method in middleware ) {
30
- clone[method] = [...( middleware[method] || [] )]
31
- }
32
- return clone
33
- }
34
-
35
- /**
36
- * Ensure the given middleware is valid
37
- * @param middleware
38
- */
39
- const validateMiddleware = ( middleware ) => {
40
- if( Array.isArray( middleware ) ) {
41
- validateMiddlewareArray( middleware )
42
- } else if( typeof middleware === 'object' ) {
43
- for( let method in middleware ) {
44
- //ensure methods are always available as uppercase
45
- let upMethod = method.toUpperCase()
46
- middleware[upMethod] = middleware[method]
47
- validateMiddlewareArray( middleware[upMethod] )
48
- }
49
- } else {
50
- throw new Error( 'Invalid middleware definition: ' + middleware )
51
- }
52
- }
53
-
54
- const validateMiddlewareArray = ( arr ) => {
55
- if( !Array.isArray( arr ) ) {
56
- throw 'middleware must be an array of functions'
57
- }
58
- for(let f of arr){
59
- if( typeof f !== 'function' ) {
60
- throw 'Each element in the array of middleware must be a function'
61
- }
62
- }
63
- }
64
-
65
- async function executeMiddleware( middleware, errorMiddleware, req, res, reqErr ) {
66
- let err
67
- await new Promise( ( resolve, reject ) => {
68
- let current = -1
69
- let isError = false
70
- const next = ( mwErr ) => {
71
- if( !isError && mwErr ) {
72
- isError = true
73
- current = -1
74
- }
75
- if( res.writableEnded ) {
76
- resolve()
77
- return
78
- }
79
- current++
80
- if( ( isError && current === errorMiddleware.length ) ||
81
- ( !isError && current === middleware.length )
82
- ) {
83
- if( mwErr )
84
- reject( mwErr )
85
- resolve()
86
- } else {
87
- try {
88
- let mw = isError ? errorMiddleware[current] : middleware[current]
89
- if( mwErr ) {
90
- mw( mwErr, req, res, next )
91
- } else {
92
- mw( req, res, next )
93
- }
94
- } catch( e ) {
95
-
96
- log.error( 'Middleware threw exception', e )
97
- next( e )
98
- }
99
- }
100
- }
101
-
102
- next( reqErr )
103
- } ).catch( e => {
104
- err = e
105
- return null
106
- } )
107
- if(err) throw err
108
- }
109
-
110
- module.exports = {
111
- validateMiddlewareArray,
112
- cloneMiddleware,
113
- validateMiddleware,
114
- mergeMiddleware,
115
- executeMiddleware
116
- }
@@ -1,61 +0,0 @@
1
- const fs = require( 'fs' )
2
- const path = require( 'path' )
3
- const serverConfig = require( './serverConfig' )
4
- const { mergeMiddleware } = require( "./middleware" );
5
- const staticHandler = require( './staticHandler' )
6
- const { getContentTypeByExtension } = require( './content' )
7
-
8
- const stripLeadingSlash = p => p.startsWith( '/' ) ? p.substr( 1 ) : p
9
-
10
- module.exports = {
11
- /**
12
- This method will add all of the configured node_module files to the given routes.
13
- The configured node moduleRoutes must be explicit files, no pattern matching is supported.
14
- Generating the list of files using pattern matching yourself is highly discouraged.
15
- It is much safer to explicitly list every file you wish to be served so you don't inadvertently serve additional files.
16
- */
17
- getNodeModuleRoutes() {
18
- let nodeModuleRoutes = serverConfig.current.nodeModuleRoutes;
19
- let routes = []
20
- if( nodeModuleRoutes && typeof nodeModuleRoutes === 'object' ) {
21
- const nodeModulesDir = nodeModuleRoutes.nodeModulesPath ? path.resolve( nodeModuleRoutes.nodeModulesPath ) : path.resolve( serverConfig.current.routeDir, '..', 'node_modules' )
22
- if( !fs.existsSync( nodeModulesDir ) ) {
23
- throw new Error( `Unable to find node_modules dir at ${nodeModulesDir}` )
24
- }
25
- let prefix = stripLeadingSlash(nodeModuleRoutes.routePrefix || 'lib')
26
- if( !Array.isArray( nodeModuleRoutes.files ) ) {
27
- nodeModuleRoutes.files = [nodeModuleRoutes.files]
28
- }
29
- for( let file of nodeModuleRoutes.files ) {
30
- let filePath, urlPath
31
- if( file && typeof file === 'object' ) {
32
- filePath = path.join( nodeModulesDir, file.modulePath )
33
- urlPath = `/${prefix}/${stripLeadingSlash( file.urlPath || file.modulePath )}`
34
- } else if( file && typeof file === 'string' ) {
35
- filePath = path.join( nodeModulesDir, file )
36
- urlPath = `/${prefix}/${stripLeadingSlash( file )}`
37
- } else {
38
- throw new Error( 'Invalid node_module file: ' + file )
39
- }
40
-
41
- if( fs.existsSync( filePath ) ) {
42
- let parts = urlPath.split( '/' )
43
- let lastPart = parts.pop()
44
- let mw = {}
45
- mergeMiddleware( serverConfig.current.middleware, mw )
46
- mergeMiddleware( nodeModuleRoutes.middleware || {}, mw )
47
- routes.push( {
48
- pathParameters: [],
49
- urlPath,
50
- filePath,
51
- handlers: staticHandler.create( filePath, getContentTypeByExtension(lastPart, serverConfig.current.staticContentTypes) ),
52
- middleware: mw
53
- } )
54
- } else {
55
- console.warn( `The specified node_modules file: ${file} does not exist and will not be served.` )
56
- }
57
- }
58
- }
59
- return routes
60
- }
61
- }
package/src/routes.js DELETED
@@ -1,125 +0,0 @@
1
- const { validateMiddleware, mergeMiddleware } = require( "./middleware" )
2
- const staticHandler = require( './staticHandler' )
3
- const { getContentTypeByExtension } = require( "./content" )
4
- const fs = require( 'fs' )
5
- const path = require( 'path' )
6
- const serverConfig = require( './serverConfig' )
7
- const { HTTP_METHODS } = require( './handler' )
8
-
9
- const isVariable = part => part.startsWith( '$' )
10
- const getVariableName = part => part.substr( 1 )
11
- const getPathPart = name => {
12
- if(name === 'index'){
13
- return ''
14
- } if( name.startsWith( '$' ) ) {
15
- return `:${name.substr( 1 )}`
16
- } else if( name.endsWith( '+' ) ) {
17
- return `${name.substr( 0, name.length - 1 )}/*`
18
- } else {
19
- return name
20
- }
21
- }
22
- const doFindRoutes = ( currentFile, filePath, urlPath, pathParameters, inheritedMiddleware ) => {
23
- let routes = []
24
- let name = currentFile.name;
25
- if( currentFile.isDirectory() ) {
26
- if( isVariable( name ) ) {
27
- pathParameters = pathParameters.concat( getVariableName( name ) )
28
- }
29
- const files = fs.readdirSync( filePath, { withFileTypes: true } )
30
-
31
- const dirMiddleware = files
32
- .filter( f => f.name.endsWith( '.mw.js' ) )
33
- .map( f => {
34
- let mw = require( filePath + '/' + f.name )
35
- if( !mw.middleware ) {
36
- throw new Error( '.mw.js files must export a middleware property' )
37
- }
38
- validateMiddleware( mw.middleware )
39
- return mw.middleware
40
- } )
41
- .reduce( ( result, incoming ) => mergeMiddleware( incoming, result ), inheritedMiddleware )
42
-
43
- routes = routes.concat( (
44
- files
45
- .filter( f => !f.name.endsWith( '.mw.js' ) )
46
- .map(
47
- ( f ) => doFindRoutes(
48
- f,
49
- filePath + '/' + f.name,
50
- urlPath + '/' + getPathPart( name ),
51
- pathParameters,
52
- dirMiddleware
53
- )
54
- )
55
- ).flat()
56
- )
57
- } else if( !serverConfig.current.staticMode && name.endsWith( '.rt.js' ) ) {
58
- name = name.substr( 0, name.length - '.rt.js'.length )
59
- if( isVariable( name ) ) {
60
- pathParameters = pathParameters.concat( getVariableName( name ) )
61
- }
62
- let route = {
63
- pathParameters,
64
- urlPath: `${urlPath}/${getPathPart( name )}`,
65
- filePath,
66
- handlers: {}
67
- }
68
- const handlers = require( filePath )
69
- route.middleware = mergeMiddleware( handlers.middleware || [], inheritedMiddleware )
70
- for( let method of Object.keys( handlers ).filter( k => k !== 'middleware' ) ) {
71
- if( HTTP_METHODS.indexOf( method ) === -1 ) {
72
- throw `Method: ${method} in file ${filePath} is not a valid http method. It must be one of: ${HTTP_METHODS}. Method names must be all uppercase.`
73
- }
74
- let handler = handlers[method]
75
- if( typeof handler !== 'function' ) {
76
- throw `Request method ${method} must be a function. Got: ${handlers[method]}`
77
- }
78
- route.handlers[method] = handler
79
- }
80
- routes.push( route )
81
- } else {
82
- if( isVariable( name ) ) {
83
- pathParameters = pathParameters.concat( getVariableName( name ) )
84
- }
85
- let contentType = getContentTypeByExtension( name, serverConfig.current.staticContentTypes )
86
- let route = {
87
- pathParameters,
88
- urlPath: `${urlPath}/${getPathPart( name )}`,
89
- filePath,
90
- handlers: staticHandler.create( filePath, contentType ),
91
- middleware: inheritedMiddleware
92
- }
93
-
94
- if( !route.handlers.OPTIONS ) {
95
-
96
- }
97
-
98
- routes.push( route )
99
-
100
- for( let ext of serverConfig.current.resolveWithoutExtension ) {
101
- if( name.endsWith( ext ) ) {
102
- let noExtRoute = Object.assign( {}, route )
103
- noExtRoute.urlPath = `${urlPath}/${getPathPart( name.substr( 0, name.length - ext.length ) )}`
104
- routes.push( noExtRoute )
105
- }
106
- }
107
- }
108
- return routes
109
- }
110
-
111
-
112
- module.exports = {
113
- findRoutes(){
114
- const fullRouteDir = path.resolve( serverConfig.current.routeDir )
115
- if( !fs.existsSync( fullRouteDir ) ) {
116
- throw `can't find route directory: ${fullRouteDir}`
117
- }
118
- let appMiddleware = mergeMiddleware( serverConfig.current.middleware || [], {} )
119
- return fs.readdirSync( fullRouteDir, { withFileTypes: true } )
120
- .map(
121
- f => doFindRoutes( f, fullRouteDir + '/' + f.name, '', [], appMiddleware )
122
- )
123
- .flat()
124
- }
125
- }
package/src/secure.js DELETED
@@ -1,42 +0,0 @@
1
- const serverConfig = require( './serverConfig' )
2
- const fs = require( 'fs' )
3
- const log = require( './log' )
4
- const path = require( 'path' )
5
- const uws = require( 'uWebSockets.js' )
6
-
7
- const startHttpRedirect = () => {
8
- //redirect http to https
9
- let port = serverConfig.current.port;
10
- uws.App().any( "/*",
11
- ( req, res ) => {
12
- try {
13
- res.writeHead( 301, { 'Location': `https://${req.headers.host}:${serverConfig.current.port}${req.url}` } )
14
- res.end()
15
- } catch( e ) {
16
- log.error( `Failed to handle http request on port ${serverConfig.current.port}`, req.url, e )
17
- }
18
- }
19
- ).listen( serverConfig.current.host || '0.0.0.0', port, ( token ) => {
20
- if( token ) {
21
- log.gne( `Http redirect server initialized at ${new Date().toISOString()} and listening on port ${port}` )
22
- } else {
23
- throw new Error( `Failed to start server on port ${port}` )
24
- }
25
- } )
26
- }
27
-
28
- let getHttpsApp = () => {
29
- const secure = serverConfig.current.secure
30
- if( !secure || !secure.key || !secure.cert ) throw 'You must set secure.key and secure.cert in the config to use https!'
31
- let keyPath = path.resolve( secure.key )
32
- let certPath = path.resolve( secure.cert )
33
- if( !fs.existsSync( keyPath ) ) throw `Can't find https key file: ${keyPath}`
34
- if( !fs.existsSync( certPath ) ) throw `Can't find https cert file: ${keyPath}`
35
- return uws.App( {
36
- key_file_name: secure.key,
37
- cert_file_name: secure.cert
38
- } )
39
- }
40
- module.exports = {
41
- getHttpsApp, startHttpRedirect
42
- }