slower 1.1.26 → 2.0.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/readme.md DELETED
@@ -1,204 +0,0 @@
1
- # Slower
2
-
3
- Slower is a small web framework, express-like, but simpler and limited.
4
- It allows for generic route-declaration, fallback pages, and multiple middleware functions.
5
-
6
- ### API Methods:
7
-
8
- ```
9
- app.enableStrictHeaders(): this
10
-
11
- > Enables the use of the set of 'Strict Headers'.
12
- > These headers increase security levels, and are a good practice to apply.
13
- > However, using these headers in testing scenarios are not a need,
14
- and may have buggy or negative effects. So, apply those to simulate scenarios only.
15
- > Headers configured:
16
- > Content-Security-Policy
17
- > Cross-Origin-Opener-Policy
18
- > Cross-Origin-Resource-Policy
19
- > Origin-Agent-Cluster
20
- > Referrer-Policy
21
- > X-DNS-Prefetch-Control (disabled)
22
- > X-Download-Options
23
- > X-Frame-Options
24
- > X-XSS-Protection (disabled)
25
- > X-Powered-By (removed)
26
- > Returns the own object instance, so that methods can be chained.
27
- ```
28
- ```
29
- app.disableStrictHeaders(): this
30
-
31
- > Disables the use of the set of 'Strict Headers'.
32
- > See 'enableStrictHeaders()' for more information.
33
- > Returns the own object instance, so that methods can be chained.
34
- ```
35
- ```
36
- app.setRoute(string: path = '/', string: type = 'GET', function: callback): this
37
-
38
- > Creates a new route for path defined in 'path', responding to the HTTP verb defined in 'type' argument.
39
- > The callback is executed when the route is accessed.
40
- > Returns the own object instance, so that methods can be chained.
41
- ```
42
- ```
43
- app.setMiddleware(function: callback): this
44
-
45
- > Sets a new middleware function: callback function will be accessed for every server access.
46
- > Many middlewares can be defined, and will be applied in the order they are defined.
47
- > Returns the own object instance, so that methods can be chained.
48
- ```
49
- ```
50
- app.setDynamic(string: path, string: file = '', string: mime = '', object: replacementData = null): this
51
-
52
- > Creates a new GET route for path defined in 'path'.
53
- > This is a custom file-response route, configured for template rendering just before response.
54
- > Providing an object as 'replacementData' in this format { valueToBeReplaced: valueToReplace },
55
- allows for template rendering. The value to replace in the file, uses this notation: '<{content}>'.
56
- > URL reference in filename:
57
- > For direct references, it is possible to use the token '{%}' to replace the filename for the URL.
58
- > Ex:
59
- app.setStatic('/login', './templates/{%}.html', 'text/html');
60
- This will access the 'login.html' file when the route '/login' is accessed.
61
- > Example:
62
- Responding a route for '/custom' with file 'custom.html':
63
- app.setDynamic('/custom', './templates/custom.html', 'text/html', { smile: ':)' })
64
- In file './templates/custom.html':
65
- "<h2> This is a custom thing: <{smile}> </h2>"
66
- Rendered in browser:
67
- <h2> This is a custom thing: :) </h2>
68
- > Returns the own object instance, so that methods can be chained.
69
- ```
70
- ```
71
- app.setStatic(string: path, string: file = '', string: mime = ''): this
72
-
73
- > Creates a new GET route for path defined in 'path', responding with the specified file and MIME type.
74
- > URL reference in filename:
75
- > For direct references, it is possible to use the token '{%}' to replace the filename for the URL.
76
- > Ex:
77
- app.setStatic('/login', './templates/{%}.html', 'text/html');
78
- This will access the 'login.html' file when the route '/login' is accessed.
79
- > Example: A route for '/login' page, responding with 'login.html' file
80
- setStatic('/login', __dirname+'/public/static/views/login.html', 'text/html');
81
- > Returns the own object instance, so that methods can be chained.
82
- ```
83
- ```
84
- app.setFallback(function: callback): this
85
-
86
- > Creates a function for fallback state. When no other routes intercept the route, this will be used.
87
- > Special use for 'page not found' fallback pages or highly customized routes and situations.
88
- > Returns the own object instance, so that methods can be chained.
89
- ```
90
- ```
91
- app.setFallbackFile (string: file = '', string: mime = '', object: replacementData = null): this
92
-
93
- > Creates a function for fallback state. When no other routes intercept the route, this will be used.
94
- > Equivalent to setFallback, but responds with a file. Allows for template rendering.
95
- > See 'setDynamic' for more information about template rendering.
96
- > Special use for 'page not found' fallback pages, ex: './e404.html'.
97
- > Returns the own object instance, so that methods can be chained.
98
- ```
99
- ```
100
- app.setAllowedMethods(array: methods = []): this
101
-
102
- > Sets a list of methods to respond to.
103
- > By using this, it is possible to restrict the application to avoid
104
- responding to dangerous HTTP verbs, such as 'DELETE'.
105
- > By default, all methods are allowed (see Slower.constructor.http_methods)
106
- > Calling this function without parameters is an easy way to block responses to all requests (lock server).
107
- > Returns the own object instance, so that methods can be chained.
108
- ```
109
- ```
110
- app.start(number|string: port = 8080, string: host = undefined, function: callback = ()=>{}): this
111
-
112
- > Starts the server, at a specific host and port, then calling the callback function.
113
- > Not defining a specific port or host will start the server at '0.0.0.0:8080'.
114
- > Returns the own object instance, so that methods can be chained.
115
- ```
116
-
117
- Example usage:
118
- ```
119
- const Slower = require('slower');
120
- const port = 8080;
121
-
122
- let app = Slower();
123
-
124
- app.setMiddleware((req, res) => {
125
- req.time = Date.now();
126
- console.log(`${req.time} - ${req.method} : ${req.url}`);
127
- });
128
-
129
- app.setStatic('/favicon.ico', __dirname+'/public/{%}');
130
-
131
- app.setRoute('/', 'GET', (req, res) => {
132
- res.writeHead(200, { 'Content-Type': 'text/html' });
133
- res.write('<html><body><p>This is the / page.</p></body></html>');
134
- res.end();
135
- });
136
-
137
- app.setRoute('/{*}.css', 'GET', (req, res) => {
138
- let data = fs.readFileSync(...some.css.file...)
139
- res.writeHead(200, { 'Content-Type': 'text/css' });
140
- res.write(data);
141
- res.end();
142
- });
143
-
144
- const generateDownloadNumber = () => { Math.round(Math.random() * 10) }
145
- app.setDynamic('/download/{?}/', './main-download.txt', { DowloadName: generateDownloadNumber });
146
- // Responds to download routes such as '/download/2/'
147
-
148
- app.setFallback((req, res) => {
149
- res.sendHTML('<html><body><p>This is the fallback page.</p></body></html>', 404);
150
- res.end();
151
- });
152
-
153
- // Start app listening on all interfaces (0.0.0.0)
154
- app.start(port, null, () => {
155
- console.log(`Running on localhost:${port}`);
156
- console.log(app);
157
- });
158
-
159
- ```
160
- ### API modifications on 'net.Socket' instances:
161
- - The API modifies every ```net.Socket``` instance BEFORE it is passed
162
- to ```app.connectionListener```. This means that all events receiving
163
- a socket will receive the modified socket instead.
164
- - The modifications adds the following properties to the socket instance:
165
- ```
166
- <socket>.session: Object => A container for persistent data appended to sockets
167
- <socket>.session.port: Number => The local port number
168
- <socket>.session.rport: Number => The remote port number
169
- <socket>.session.host: String => The local host interface address
170
- <socket>.session.rhost: String => The remote host interface address
171
- ```
172
- - Only on 'response' ```net.Socket```:
173
- ```
174
- <socket>.sendText: Function (data, code) => A helper for responding with plaintext data. No need to declare writeHead() or end() if using this.
175
- <socket>.sendJSON: Function (data, code) => A helper for responding with JSON data. No need to declare writeHead() or end() if using this.
176
-
177
- ```
178
- - It is possible to use the ```socket.session``` object to append data that will persist
179
- during the lifetime of a single connection. Useful for keeping short-life local variables.
180
-
181
- - In HTTP ```http.IncomingMessage``` instances, the 'socket' instance is found over 'request'.
182
- So, considering the common callback of ```(req, res)```, the session container will be ```req.session```
183
-
184
- ### API security headers implementation:
185
- - It is possible to enforce a higher set of security headers on responses
186
- without having to set them manually. The API 'enableStrictHeaders' and 'disableStrictHeaders'
187
- methods do exacly that. The strict headers are disabled by default, as some resources are too strict,
188
- but it is also possible to enable them all, and then set a middleware to override any header.
189
- - Headers set by 'enableStrictHeaders':
190
- ```
191
- Content-Security-Policy: default-src=none; script-src=self; connect-src=self; img-src=self;
192
- style-src=self; frame-ancestors=none; form-action=self;
193
- Cross-Origin-Opener-Policy: same-origin
194
- Cross-Origin-Resource-Policy: same-site
195
- Origin-Agent-Cluster: ?1
196
- Referrer-Policy: no-referrer
197
- Strict-Transport-Security: max-age=31536000; includeSubDomains // temporarily disabled for maintenance
198
- X-Content-Type-Options: nosniff // temporarily disabled for maintenance
199
- X-DNS-Prefetch-Control: off
200
- X-Download-Options: noopen
201
- X-Frame-Options: DENY
202
- X-Powered-By: (This header is removed if a response includes it)
203
- X-XSS-Protection: 0
204
- ```
File without changes