slower 1.1.20 → 1.1.22

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 ADDED
@@ -0,0 +1,22 @@
1
+
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Tomás Luchesi
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/lib/router.js CHANGED
@@ -26,15 +26,6 @@ class SlowerRouter {
26
26
  ];
27
27
 
28
28
  static mime_table = mimetable;
29
- // {
30
- // 'txt' : ['text/plain', 'utf-8'],
31
- // 'html': ['text/html', 'utf-8'],
32
- // 'js' : ['text/javascript', 'utf-8'],
33
- // 'css' : ['text/css', 'utf-8'],
34
- // 'ico' : ['image/png'],
35
- // 'json': ['application/json'],
36
- // 'default': ['application/octet-stream']
37
- // }
38
29
 
39
30
  constructor () {
40
31
  this.strictHeaders = false;
@@ -44,6 +35,7 @@ class SlowerRouter {
44
35
  this.allowedMethods = clone(SlowerRouter.http_methods);
45
36
  this.blockedMethodCallback = noop;
46
37
  this.tls = { use: false, key: null, cert: null };
38
+ this.server = null;
47
39
  }
48
40
 
49
41
  enableStrictHeaders () {
@@ -372,14 +364,12 @@ class SlowerRouter {
372
364
  let fallback = this.fallback;
373
365
  let allowedMethods = this.allowedMethods;
374
366
  let blockedMethodCallback = this.blockedMethodCallback;
375
-
376
- let server;
377
367
 
378
368
  if (this.tls.use && this.tls.cert && this.tls.key) {
379
- server = https.createServer({ key:this.tls.key, cert: this.tls.cert }, mainServerHandler);
369
+ this.server = https.createServer({ key:this.tls.key, cert: this.tls.cert }, mainServerHandler);
380
370
 
381
371
  } else {
382
- server = http.createServer({}, mainServerHandler);
372
+ this.server = http.createServer({}, mainServerHandler);
383
373
  }
384
374
 
385
375
  function mainServerHandler (req, res) {
@@ -409,27 +399,31 @@ class SlowerRouter {
409
399
  ) {
410
400
  i = routes.length;
411
401
  route.callback(req, res);
412
- if (res.writableEnded) return; // if res.end() was called
413
- else res.end(); // if res.end() was not called
402
+ // if (res.writableEnded) return; // if res.end() was called
403
+ // else res.end(); // if res.end() was not called
414
404
  return;
415
405
  }
416
406
  }
417
407
  fallback(req,res);
418
- if (!res.writableEnded) res.end(); // No need to call res.end() on handlers anymore
408
+ if (!res.writableEnded) res.end();
419
409
  } else {
420
410
  blockedMethodCallback(req, res);
421
- if (!res.writableEnded) res.end(); // No need to call res.end() on handlers anymore
411
+ if (!res.writableEnded) res.end();
422
412
  }
423
413
  } catch(err) {
424
414
  console.log(err);
425
415
  }
426
416
  }
427
417
 
428
- callback(server);
418
+ callback(this.server);
429
419
  if (!host) host = undefined; // Turn falsy values into undefined, for default behaviour
430
- server.listen(port, host);
420
+ this.server.listen(port, host);
431
421
  return this;
432
422
  }
423
+
424
+ close = function () {
425
+ this.server.close();
426
+ }
433
427
  }
434
428
 
435
429
  const Slower = function () { return new SlowerRouter(); }
package/lib/utils.js CHANGED
@@ -90,10 +90,17 @@ const setResponseAssessors = (resSocket) => {
90
90
  resSocket.sendText = (data, code = 200) => {
91
91
  resSocket.writeHead(code, { 'Content-Type': 'text/plain' });
92
92
  resSocket.write(data);
93
+ resSocket.end();
93
94
  };
94
95
  resSocket.sendJSON = (data, code = 200) => {
95
96
  resSocket.writeHead(code, { 'Content-Type': 'application/json' });
97
+ resSocket.write(typeof data === 'string' ? data : JSON.stringify(data));
98
+ resSocket.end();
99
+ };
100
+ resSocket.sendHTML = (data, code = 200) => {
101
+ resSocket.writeHead(code, { 'Content-Type': 'text/html' });
96
102
  resSocket.write(data);
103
+ resSocket.end();
97
104
  };
98
105
  }
99
106
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slower",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "description": "A package for simple HTTP server routing.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -10,6 +10,6 @@
10
10
  "test": "echo \"Error: no test specified\" && exit 1"
11
11
  },
12
12
  "keywords": [],
13
- "author": "no.mad.devtech@gmail.com",
14
- "license": "ISC"
13
+ "author": "Tomás Luchesi <no.mad.devtech@gmail.com>",
14
+ "license": "MIT"
15
15
  }
package/readme.md CHANGED
@@ -146,8 +146,7 @@ app.setDynamic('/download/{?}/', './main-download.txt', { DowloadName: generateD
146
146
  // Responds to download routes such as '/download/2/'
147
147
 
148
148
  app.setFallback((req, res) => {
149
- res.writeHead(200, { 'Content-Type': 'text/html' });
150
- res.write('<html><body><p>This is the fallback page.</p></body></html>');
149
+ res.sendHTML('<html><body><p>This is the fallback page.</p></body></html>', 404);
151
150
  res.end();
152
151
  });
153
152