@srfnstack/spliffy 1.2.5 → 1.2.6
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/LICENSE.txt +20 -20
- package/README.md +43 -43
- package/package.json +47 -44
- package/src/content.mjs +97 -97
- package/src/decorator.mjs +209 -209
- package/src/handler.mjs +256 -256
- package/src/index.mjs +30 -30
- package/src/log.mjs +59 -59
- package/src/middleware.mjs +89 -89
- package/src/nodeModuleHandler.mjs +75 -75
- package/src/routes.mjs +205 -205
- package/src/server.mjs +141 -141
- package/src/serverConfig.mjs +100 -100
- package/src/start.mjs +25 -25
- package/src/staticHandler.mjs +74 -74
- package/src/url.mjs +24 -24
package/LICENSE.txt
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2019 Robert Kempton
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Robert Kempton
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
# Spliffy
|
|
2
|
-
|
|
3
|
-
> directory based routing with js request handlers and static file serving
|
|
4
|
-
|
|
5
|
-
## Getting started
|
|
6
|
-
Create a directories for your app
|
|
7
|
-
|
|
8
|
-
`mkdir -p ~/app/www`
|
|
9
|
-
|
|
10
|
-
Install spliffy
|
|
11
|
-
|
|
12
|
-
`cd ~/app && npm install spliffy`
|
|
13
|
-
|
|
14
|
-
Create a handler for the desired route name
|
|
15
|
-
|
|
16
|
-
`vi ~/app/www/spliffy.js`
|
|
17
|
-
```js
|
|
18
|
-
module.exports = {
|
|
19
|
-
GET: () => ({hello: "spliffy"})
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
Create the start script, ```vi ~/app/serve.js```
|
|
24
|
-
```js
|
|
25
|
-
require('spliffy')({routeDir: __dirname+ '/www'})
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
because the routeDir is ~/app/www, the filename `spliffy.js` creates the path `/spliffy`
|
|
29
|
-
|
|
30
|
-
The object passed to spliffy is the config. See the [Config](#Config) section for more information.
|
|
31
|
-
|
|
32
|
-
routeDir is the only required property and should be an absolute path.
|
|
33
|
-
|
|
34
|
-
`10420` is the default port for http, and can be changed by setting the port in the config
|
|
35
|
-
|
|
36
|
-
start the server
|
|
37
|
-
`node ~/app/serve.js`
|
|
38
|
-
|
|
39
|
-
Go to `localhost:10420/spliffy`
|
|
40
|
-
|
|
41
|
-
# [Documentation](https://srfnstack.github.io/spliffy/)
|
|
42
|
-
|
|
43
|
-
#### [Examples](https://github.com/narcolepticsnowman/spliffy/tree/master/example)
|
|
1
|
+
# Spliffy
|
|
2
|
+
|
|
3
|
+
> directory based routing with js request handlers and static file serving
|
|
4
|
+
|
|
5
|
+
## Getting started
|
|
6
|
+
Create a directories for your app
|
|
7
|
+
|
|
8
|
+
`mkdir -p ~/app/www`
|
|
9
|
+
|
|
10
|
+
Install spliffy
|
|
11
|
+
|
|
12
|
+
`cd ~/app && npm install spliffy`
|
|
13
|
+
|
|
14
|
+
Create a handler for the desired route name
|
|
15
|
+
|
|
16
|
+
`vi ~/app/www/spliffy.js`
|
|
17
|
+
```js
|
|
18
|
+
module.exports = {
|
|
19
|
+
GET: () => ({hello: "spliffy"})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
Create the start script, ```vi ~/app/serve.js```
|
|
24
|
+
```js
|
|
25
|
+
require('spliffy')({routeDir: __dirname+ '/www'})
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
because the routeDir is ~/app/www, the filename `spliffy.js` creates the path `/spliffy`
|
|
29
|
+
|
|
30
|
+
The object passed to spliffy is the config. See the [Config](#Config) section for more information.
|
|
31
|
+
|
|
32
|
+
routeDir is the only required property and should be an absolute path.
|
|
33
|
+
|
|
34
|
+
`10420` is the default port for http, and can be changed by setting the port in the config
|
|
35
|
+
|
|
36
|
+
start the server
|
|
37
|
+
`node ~/app/serve.js`
|
|
38
|
+
|
|
39
|
+
Go to `localhost:10420/spliffy`
|
|
40
|
+
|
|
41
|
+
# [Documentation](https://srfnstack.github.io/spliffy/)
|
|
42
|
+
|
|
43
|
+
#### [Examples](https://github.com/narcolepticsnowman/spliffy/tree/master/example)
|
package/package.json
CHANGED
|
@@ -1,44 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@srfnstack/spliffy",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"author": "snowbldr",
|
|
5
|
-
"private": false,
|
|
6
|
-
"homepage": "https://github.com/SRFNStack/spliffy",
|
|
7
|
-
"license": "MIT",
|
|
8
|
-
"type": "module",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@srfnstack/spliffy",
|
|
3
|
+
"version": "1.2.6",
|
|
4
|
+
"author": "snowbldr",
|
|
5
|
+
"private": false,
|
|
6
|
+
"homepage": "https://github.com/SRFNStack/spliffy",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=16 <=20"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src/*",
|
|
14
|
+
"LICENSE.txt",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"main": "src/index.mjs",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git@github.com:SRFNStack/spliffy.git"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "npm run lint:fix && jest",
|
|
24
|
+
"lint": "standard --env jest src ./*js && standard --env jest --env browser --global Prism docs example",
|
|
25
|
+
"lint:fix": "standard --env jest --fix src ./*js && standard --env jest --env browser --global Prism --fix docs example"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"node",
|
|
29
|
+
"http",
|
|
30
|
+
"server",
|
|
31
|
+
"web",
|
|
32
|
+
"framework",
|
|
33
|
+
"rest"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"cookie": "^1.0.2",
|
|
37
|
+
"etag": "^1.8.1",
|
|
38
|
+
"uuid": "^11.1.0",
|
|
39
|
+
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.74.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"helmet": "^4.6.0",
|
|
43
|
+
"jest": "^27.3.1",
|
|
44
|
+
"node-fetch": "^2.6.7",
|
|
45
|
+
"standard": "^16.0.4"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/content.mjs
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import contentTypes from './content-types.mjs'
|
|
2
|
-
import { parseQuery } from './url.mjs'
|
|
3
|
-
|
|
4
|
-
const defaultHandler = {
|
|
5
|
-
deserialize: o => {
|
|
6
|
-
if (!o) return o
|
|
7
|
-
try {
|
|
8
|
-
return JSON.parse(o && o.toString())
|
|
9
|
-
} catch (e) {
|
|
10
|
-
return o
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
serialize: o => {
|
|
14
|
-
if (!o) return { data: o }
|
|
15
|
-
if (typeof o === 'string') {
|
|
16
|
-
return {
|
|
17
|
-
contentType: 'text/plain',
|
|
18
|
-
data: o
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (o instanceof Buffer) {
|
|
22
|
-
return {
|
|
23
|
-
contentType: 'application/octet-stream',
|
|
24
|
-
data: o
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
contentType: 'application/json',
|
|
29
|
-
data: JSON.stringify(o)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const toFormData = (key, value) => {
|
|
35
|
-
if (Array.isArray(value)) {
|
|
36
|
-
return value.map(toFormData).flat()
|
|
37
|
-
} else if (typeof value === 'object') {
|
|
38
|
-
return Object.keys(value).map(k => toFormData(`${key}.${k}`, value[k])).flat()
|
|
39
|
-
} else {
|
|
40
|
-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const contentHandlers = {
|
|
45
|
-
'application/json': {
|
|
46
|
-
deserialize: s => s && JSON.parse(s && s.toString()),
|
|
47
|
-
serialize: o => o && JSON.stringify(o)
|
|
48
|
-
},
|
|
49
|
-
'text/plain': {
|
|
50
|
-
deserialize: s => s && s.toString(),
|
|
51
|
-
serialize: o => o && o.toString()
|
|
52
|
-
},
|
|
53
|
-
'application/octet-stream': defaultHandler,
|
|
54
|
-
'application/x-www-form-urlencoded': {
|
|
55
|
-
deserialize: s => s && parseQuery(s.toString(), true),
|
|
56
|
-
serialize: o => o && Object.keys(o).map(toFormData).flat().join('&')
|
|
57
|
-
},
|
|
58
|
-
'*/*': defaultHandler
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function getHandler (contentType, acceptsDefault) {
|
|
62
|
-
if (!contentType) return contentHandlers[acceptsDefault]
|
|
63
|
-
// content-type is singular https://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.14.17
|
|
64
|
-
let handler = contentHandlers[contentType]
|
|
65
|
-
if (!handler && contentType.indexOf(';') > -1) {
|
|
66
|
-
handler = contentHandlers[contentType.split(';')[0].trim()]
|
|
67
|
-
}
|
|
68
|
-
if (handler && typeof handler) {
|
|
69
|
-
if (typeof handler.serialize !== 'function') {
|
|
70
|
-
throw new Error(`Content handlers must provide a serialize function. ${handler}`)
|
|
71
|
-
}
|
|
72
|
-
if (typeof handler.deserialize !== 'function') {
|
|
73
|
-
throw new Error(`Content handlers must provide a deserialize function. ${handler}`)
|
|
74
|
-
}
|
|
75
|
-
return handler
|
|
76
|
-
}
|
|
77
|
-
return contentHandlers[acceptsDefault]
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function getContentTypeByExtension (name, staticContentTypes) {
|
|
81
|
-
const extension = name.indexOf('.') > -1 ? name.slice(name.lastIndexOf('.')).toLowerCase() : 'default'
|
|
82
|
-
const contentType = staticContentTypes?.[extension] || null
|
|
83
|
-
|
|
84
|
-
return contentType || contentTypes[extension]
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function serializeBody (content, contentType, acceptsDefault) {
|
|
88
|
-
return getHandler(contentType && contentType.toLowerCase(), acceptsDefault).serialize(content)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function deserializeBody (content, contentType, acceptsDefault) {
|
|
92
|
-
return getHandler(contentType && contentType.toLowerCase(), acceptsDefault).deserialize(content)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function initContentHandlers (handlers) {
|
|
96
|
-
return Object.assign({}, contentHandlers, handlers)
|
|
97
|
-
}
|
|
1
|
+
import contentTypes from './content-types.mjs'
|
|
2
|
+
import { parseQuery } from './url.mjs'
|
|
3
|
+
|
|
4
|
+
const defaultHandler = {
|
|
5
|
+
deserialize: o => {
|
|
6
|
+
if (!o) return o
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(o && o.toString())
|
|
9
|
+
} catch (e) {
|
|
10
|
+
return o
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
serialize: o => {
|
|
14
|
+
if (!o) return { data: o }
|
|
15
|
+
if (typeof o === 'string') {
|
|
16
|
+
return {
|
|
17
|
+
contentType: 'text/plain',
|
|
18
|
+
data: o
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (o instanceof Buffer) {
|
|
22
|
+
return {
|
|
23
|
+
contentType: 'application/octet-stream',
|
|
24
|
+
data: o
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
contentType: 'application/json',
|
|
29
|
+
data: JSON.stringify(o)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const toFormData = (key, value) => {
|
|
35
|
+
if (Array.isArray(value)) {
|
|
36
|
+
return value.map(toFormData).flat()
|
|
37
|
+
} else if (typeof value === 'object') {
|
|
38
|
+
return Object.keys(value).map(k => toFormData(`${key}.${k}`, value[k])).flat()
|
|
39
|
+
} else {
|
|
40
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const contentHandlers = {
|
|
45
|
+
'application/json': {
|
|
46
|
+
deserialize: s => s && JSON.parse(s && s.toString()),
|
|
47
|
+
serialize: o => o && JSON.stringify(o)
|
|
48
|
+
},
|
|
49
|
+
'text/plain': {
|
|
50
|
+
deserialize: s => s && s.toString(),
|
|
51
|
+
serialize: o => o && o.toString()
|
|
52
|
+
},
|
|
53
|
+
'application/octet-stream': defaultHandler,
|
|
54
|
+
'application/x-www-form-urlencoded': {
|
|
55
|
+
deserialize: s => s && parseQuery(s.toString(), true),
|
|
56
|
+
serialize: o => o && Object.keys(o).map(toFormData).flat().join('&')
|
|
57
|
+
},
|
|
58
|
+
'*/*': defaultHandler
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getHandler (contentType, acceptsDefault) {
|
|
62
|
+
if (!contentType) return contentHandlers[acceptsDefault]
|
|
63
|
+
// content-type is singular https://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.14.17
|
|
64
|
+
let handler = contentHandlers[contentType]
|
|
65
|
+
if (!handler && contentType.indexOf(';') > -1) {
|
|
66
|
+
handler = contentHandlers[contentType.split(';')[0].trim()]
|
|
67
|
+
}
|
|
68
|
+
if (handler && typeof handler) {
|
|
69
|
+
if (typeof handler.serialize !== 'function') {
|
|
70
|
+
throw new Error(`Content handlers must provide a serialize function. ${handler}`)
|
|
71
|
+
}
|
|
72
|
+
if (typeof handler.deserialize !== 'function') {
|
|
73
|
+
throw new Error(`Content handlers must provide a deserialize function. ${handler}`)
|
|
74
|
+
}
|
|
75
|
+
return handler
|
|
76
|
+
}
|
|
77
|
+
return contentHandlers[acceptsDefault]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function getContentTypeByExtension (name, staticContentTypes) {
|
|
81
|
+
const extension = name.indexOf('.') > -1 ? name.slice(name.lastIndexOf('.')).toLowerCase() : 'default'
|
|
82
|
+
const contentType = staticContentTypes?.[extension] || null
|
|
83
|
+
|
|
84
|
+
return contentType || contentTypes[extension]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function serializeBody (content, contentType, acceptsDefault) {
|
|
88
|
+
return getHandler(contentType && contentType.toLowerCase(), acceptsDefault).serialize(content)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function deserializeBody (content, contentType, acceptsDefault) {
|
|
92
|
+
return getHandler(contentType && contentType.toLowerCase(), acceptsDefault).deserialize(content)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function initContentHandlers (handlers) {
|
|
96
|
+
return Object.assign({}, contentHandlers, handlers)
|
|
97
|
+
}
|