@srfnstack/spliffy 0.7.0 → 0.8.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/src/server.js DELETED
@@ -1,83 +0,0 @@
1
- const log = require( './log' )
2
- const { getNodeModuleRoutes } = require( "./nodeModuleHandler" );
3
- const uws = require( 'uWebSockets.js' )
4
- const handler = require( './handler' )
5
- const { findRoutes } = require( './routes' )
6
- const { getHttpsApp, startHttpRedirect } = require( './secure' )
7
-
8
- const state = {
9
- routes: {},
10
- initialized: false
11
- }
12
- const appMethods = {
13
- GET: 'get',
14
- POST: 'post',
15
- PUT: 'put',
16
- PATCH: 'patch',
17
- DELETE: 'del',
18
- OPTIONS: 'options',
19
- HEAD: 'head',
20
- CONNECT: 'connect',
21
- TRACE: 'trace'
22
- }
23
- const optionsHandler = methods => {
24
- return () => ( {
25
- headers: {
26
- allow: methods,
27
- },
28
- statusCode: 204
29
- } )
30
- };
31
-
32
- /**
33
- * Load the routes
34
- */
35
- const start = config => {
36
- if( !state.initialized ) {
37
- state.initialized = true
38
- const routes = [...findRoutes( config ), ...getNodeModuleRoutes( config )]
39
- let app, port
40
- if( config.secure ) {
41
- app = getHttpsApp( config.secure )
42
- port = config.secure.port || 14420
43
- startHttpRedirect( config.host, config.port )
44
- } else {
45
- app = uws.App()
46
- port = config.port
47
- }
48
- for( let route of routes ) {
49
- if( config.printRoutes ) {
50
- log.info( 'Configured Route: ', route )
51
- }
52
- let routePattern = `^${route.urlPath.replace( /:[^/]+/g, "[^/]+" ).replace( /\*/g, ".*" )}$`
53
- if( config.notFoundRoute && config.notFoundRoute.match( routePattern ) ) {
54
- config.defaultRoute = route
55
- }
56
- for( let method in route.handlers ) {
57
- let theHandler = handler.create( route.handlers[method], route.middleware, route.pathParameters, config );
58
- app[appMethods[method]]( route.urlPath, theHandler )
59
- if( route.urlPath.endsWith( '/' ) && route.urlPath.length > 1 ) {
60
- app[appMethods[method]]( route.urlPath.substr( 0, route.urlPath.length - 1 ), theHandler )
61
- }
62
- }
63
- if( !route.handlers.OPTIONS ) {
64
- app.options( route.urlPath, optionsHandler( Object.keys( route.handlers ).join( ', ' ) ) )
65
- }
66
- }
67
-
68
- if( config.notFoundRoute && !config.defaultRoute ) {
69
- log.warn( 'No route matched default route: ' + config.notFoundRoute )
70
- }
71
- app.any( '/*', handler.notFound( config ) )
72
- app.listen( config.host || '0.0.0.0', config.port, ( token ) => {
73
- if( token ) {
74
- log.gne( `Server initialized at ${new Date().toISOString()} and listening on port ${port}` )
75
- } else {
76
- throw new Error( `Failed to start server on port ${port}` )
77
- }
78
- } )
79
- }
80
- }
81
- module.exports = {
82
- start
83
- }
@@ -1,69 +0,0 @@
1
- const content = require( './content' )
2
- const { validateMiddleware } = require( './middleware.js' )
3
- const log = require( './log' )
4
-
5
- const defaultHeaders = {
6
- acceptsDefault: '*/*',
7
- defaultContentType: '*/*'
8
- }
9
- //this is mainly for performance reason
10
- const nonsense = [
11
- `I'm toasted`,
12
- 'that hurt',
13
- 'feels bad man :(',
14
- `squirrel! ~-{ }:>`,
15
- 'your interwebs!',
16
- 'I see a light...',
17
- `totally zooted`,
18
- 'misplaced my bits',
19
- 'maybe reboot?',
20
- 'what was I doing again?',
21
- 'my cabbages!!!',
22
- 'Leeerrroooyyy Jeeenkins',
23
- 'at least I have chicken'
24
- ]
25
-
26
- module.exports = {
27
- randomNonsense: () => `[OH NO, ${nonsense[Math.floor( Math.random() * nonsense.length )]}]`,
28
- init( userConfig ) {
29
- const config = Object.assign( {}, userConfig )
30
-
31
- if( !config.hasOwnProperty( 'decodePathParameters' ) ) {
32
- config.decodePathParameters = true
33
- }
34
-
35
- if( !config.hasOwnProperty( 'parseCookie' ) ) {
36
- config.parseCookie = true
37
- }
38
-
39
- config.acceptsDefault = config.acceptsDefault || defaultHeaders.acceptsDefault
40
- config.defaultContentType = config.defaultContentType || defaultHeaders.defaultContentType
41
-
42
- content.initContentHandlers(config.contentHandlers||{}, config.acceptsDefault)
43
- config.resolveWithoutExtension = config.resolveWithoutExtension || []
44
- if( !Array.isArray( config.resolveWithoutExtension ) ) {
45
- config.resolveWithoutExtension = [config.resolveWithoutExtension]
46
- }
47
-
48
- if( config.resolveWithoutExtension.indexOf( '.htm' ) === -1 ) {
49
- config.resolveWithoutExtension.push( '.htm' )
50
- }
51
- if( config.resolveWithoutExtension.indexOf( '.html' ) === -1 ) {
52
- config.resolveWithoutExtension.push( '.html' )
53
- }
54
-
55
- if( config.middleware ) {
56
- validateMiddleware( config.middleware )
57
- }
58
-
59
- if( !config.hasOwnProperty( 'logAccess' ) ) {
60
- config.logAccess = true
61
- }
62
- log.setLogAccess( config.logAccess )
63
- if( config.hasOwnProperty( 'logLevel' ) ) {
64
- log.setLogLevel( config.logLevel )
65
- }
66
- config.port = config.port || 10420
67
- return config
68
- }
69
- }
package/src/start.js DELETED
@@ -1,21 +0,0 @@
1
- const serverConfig = require( './serverConfig' )
2
- const log = require( './log' )
3
- const server = require('./server')
4
- const { randomNonsense } = require( "./serverConfig" );
5
-
6
- module.exports = async function( config ) {
7
- if( !config || !config.routeDir ) {
8
- throw 'You must supply a config object with at least a routeDir property. routeDir should be a full path.'
9
- }
10
- process
11
- .on( 'unhandledRejection', ( reason, p ) => {
12
- log.error( randomNonsense(), reason, 'Unhandled Rejection at Promise', p )
13
- } )
14
- .on( 'uncaughtException', ( err, origin ) => {
15
- log.error( randomNonsense(), `Caught unhandled exception: ${err}\n` +
16
- `Exception origin: ${origin}` )
17
- } )
18
-
19
- log.gne( 'Starting Spliffy!' )
20
- server.start(serverConfig.init( config ))
21
- }
@@ -1,77 +0,0 @@
1
- const fs = require( 'fs' )
2
- const etag = require( 'etag' )
3
-
4
- const readFile = async ( fullPath ) => await new Promise(
5
- ( resolve, reject ) =>
6
- fs.readFile( fullPath, ( err, data ) => {
7
- if( err ) reject( err )
8
- else resolve( data )
9
- }
10
- )
11
- )
12
-
13
- const writeHeaders = ( req, res, tag, stat, contentType, staticCacheControl ) => {
14
- if( req.headers['if-none-match'] === tag ) {
15
- res.statusCode = 304
16
- res.statusMessage = 'Not Modified'
17
- return
18
- }
19
- res.writeHead( 200, {
20
- 'Content-Type': contentType,
21
- 'Content-Length': stat.size,
22
- 'Cache-Control': staticCacheControl || 'max-age=600',
23
- 'ETag': tag
24
- } )
25
- }
26
-
27
- const readStat = async path => new Promise( ( resolve, reject ) =>
28
- fs.stat( path, ( err, stats ) =>
29
- err ? reject( err ) : resolve( stats )
30
- ) )
31
-
32
- module.exports = {
33
- create: ( fullPath, contentType, cacheStatic, staticCacheControl ) => {
34
- const cache = {}
35
- return {
36
- GET: async ( { req, res } ) => {
37
- if( cacheStatic ) {
38
- if( !cache.exists || !cache.stat ) {
39
- cache.exists = fs.existsSync( fullPath )
40
- if( cache.exists ) {
41
- cache.stat = await readStat( fullPath )
42
- cache.content = await readFile( fullPath )
43
- cache.etag = etag( cache.content )
44
- }
45
- }
46
- if( !cache.exists ) {
47
- return {
48
- statusCode: 404,
49
- statusMessage: 'Not Found'
50
- }
51
- }
52
- writeHeaders( req, res, cache.etag, cache.stat, contentType, staticCacheControl )
53
- if( res.statusCode === 304 ) {
54
- return
55
- }
56
- return cache.content
57
- } else {
58
- if( !fs.existsSync( fullPath ) ) {
59
- return {
60
- statusCode: 404,
61
- statusMessage: 'Not Found'
62
- }
63
- }
64
- let stat = await readStat( fullPath )
65
- writeHeaders( req, res, etag( stat ), stat, contentType, staticCacheControl )
66
- if( res.statusCode === 304 ) {
67
- return
68
- }
69
- if( stat.size === 0 ) {
70
- return ""
71
- }
72
- return fs.createReadStream( fullPath )
73
- }
74
- }
75
- }
76
- }
77
- }
package/src/url.js DELETED
@@ -1,26 +0,0 @@
1
-
2
- module.exports = {
3
- parseQuery: ( query, decodeQueryParams ) => {
4
- let parsed = {}
5
- if( query ) {
6
- if( decodeQueryParams ) {
7
- query = decodeURIComponent( query.replace( /\+/g, '%20' ) )
8
- }
9
- for(let param of query.split('&')){
10
- let eq = param.indexOf('=')
11
- module.exports.setMultiValueKey(parsed, param.substr(0, eq), param.substr(eq+1))
12
- }
13
- }
14
- return parsed
15
- },
16
- setMultiValueKey: ( obj, key, value ) => {
17
- if( obj.hasOwnProperty(key) ) {
18
- if( !Array.isArray( obj[key] ) ) {
19
- obj[key] = [obj[key]]
20
- }
21
- obj[key].push( value )
22
- } else {
23
- obj[key] = value
24
- }
25
- }
26
- }