slower 2.1.2 → 2.1.4
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/package.json +1 -1
- package/src/slower.js +61 -31
package/package.json
CHANGED
package/src/slower.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
const http = require('node:http');
|
|
3
3
|
const https = require('node:https');
|
|
4
4
|
const path = require('node:path');
|
|
5
|
+
const fs = require('node:fs');
|
|
5
6
|
const { createReadStream } = require('node:fs');
|
|
6
7
|
const { pipeline } = require('node:stream/promises');
|
|
7
8
|
|
|
@@ -67,8 +68,9 @@ class SlowerRouter {
|
|
|
67
68
|
// Get all routes that match the URL and join with middlewares to cycle
|
|
68
69
|
let foundRoutes = utils.getMatchingRoute(req.url, req.method, self.layers);
|
|
69
70
|
let layers = foundRoutes;
|
|
70
|
-
//
|
|
71
|
-
|
|
71
|
+
// Add an Error:404 special layer to the end of the layers list
|
|
72
|
+
// This prevents accidental requests hanging
|
|
73
|
+
layers.push(utils.noLayersFoundFallback);
|
|
72
74
|
|
|
73
75
|
// Set properties on request and response objects
|
|
74
76
|
req = await setupRequest(req);
|
|
@@ -171,6 +173,9 @@ class SlowerRouter {
|
|
|
171
173
|
*/
|
|
172
174
|
static (directoryPath, mountPath = '') {
|
|
173
175
|
const absoluteDir = path.resolve(directoryPath);
|
|
176
|
+
if (!fs.existsSync(absoluteDir))
|
|
177
|
+
throw new Error(`Invalid directory provided for [SlowerRouter].static(): [${directoryPath}]`);
|
|
178
|
+
|
|
174
179
|
for (const file of utils.getFiles(absoluteDir)) {
|
|
175
180
|
// Get only the file name from the absolute file path 'c:\u\a.txt' -> 'a.txt'
|
|
176
181
|
let fileAsURLPath = file.replaceAll(/\\/g, '/').split('/').slice(-1)[0];
|
|
@@ -238,21 +243,24 @@ class SlowerRouter {
|
|
|
238
243
|
|
|
239
244
|
|
|
240
245
|
/**
|
|
241
|
-
* Used as the class for
|
|
242
|
-
*
|
|
243
|
-
* only method acessors (.get(), .post(), .put(), ...)
|
|
244
|
-
* and .all() and .use()
|
|
245
|
-
*
|
|
246
|
-
* .static() is not allowed
|
|
246
|
+
* Used as the class for modular routers
|
|
247
|
+
* used in "slower.Router()"
|
|
247
248
|
*/
|
|
248
|
-
class
|
|
249
|
-
constructor (
|
|
250
|
-
this.
|
|
251
|
-
this.layers =
|
|
249
|
+
class SlowerSubRouter {
|
|
250
|
+
constructor () {
|
|
251
|
+
this.METHODS = HTTP_VERBS;
|
|
252
|
+
this.layers = new Array();
|
|
253
|
+
|
|
254
|
+
// Create basic route shortcuts
|
|
255
|
+
// get(), post(), put(), delete()
|
|
256
|
+
for (let verb of HTTP_VERBS) {
|
|
257
|
+
this[verb] = function (path, callback) {
|
|
258
|
+
return this.#setRoute(verb, path, callback);
|
|
259
|
+
};
|
|
260
|
+
}
|
|
252
261
|
}
|
|
253
262
|
|
|
254
|
-
#setRoute (method, handler) {
|
|
255
|
-
const path = this.path;
|
|
263
|
+
#setRoute (method, path, handler) {
|
|
256
264
|
if (typeof method !== 'string')
|
|
257
265
|
throw new Error('<SlowerSubRouter>.route :: "method" parameter must be of type String');
|
|
258
266
|
if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
|
|
@@ -263,8 +271,7 @@ class SlowerMicroRouter {
|
|
|
263
271
|
return this;
|
|
264
272
|
}
|
|
265
273
|
|
|
266
|
-
all (handler) {
|
|
267
|
-
const path = this.path;
|
|
274
|
+
all (path, handler) {
|
|
268
275
|
if (typeof path === 'string')
|
|
269
276
|
for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
|
|
270
277
|
else if (typeof path !== 'function')
|
|
@@ -274,28 +281,54 @@ class SlowerMicroRouter {
|
|
|
274
281
|
}
|
|
275
282
|
// Just a more comprehensive call to app.all for defining middlewares
|
|
276
283
|
use (...b) { this.all(...b); return this; };
|
|
284
|
+
|
|
285
|
+
static (directoryPath, mountPath = '') {
|
|
286
|
+
this.layers.push({ static: true, directoryPath, mountPath });
|
|
287
|
+
return this;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Creates a temporary synced mini-router for a specific path
|
|
292
|
+
* Allows to declare multiple handlers for methods of a specific path
|
|
293
|
+
* @param {string} path
|
|
294
|
+
* @returns {SlowerMicroRouter}
|
|
295
|
+
* @example
|
|
296
|
+
* app.route('/books')
|
|
297
|
+
* .get((req, res) => console.log('you retrieved a book with GET'))
|
|
298
|
+
* .post((req, res) => console.log('you added a book with POST'))
|
|
299
|
+
* ;
|
|
300
|
+
*/
|
|
301
|
+
route (path) {
|
|
302
|
+
return new SlowerMicroRouter(path, this.layers);
|
|
303
|
+
}
|
|
277
304
|
}
|
|
278
305
|
|
|
279
306
|
|
|
307
|
+
|
|
280
308
|
/**
|
|
281
|
-
* Used as the class for
|
|
282
|
-
*
|
|
309
|
+
* Used as the class for micro-routing with "app.route"
|
|
310
|
+
*
|
|
311
|
+
* only method acessors (.get(), .post(), .put(), ...)
|
|
312
|
+
* and .all() and .use()
|
|
313
|
+
*
|
|
314
|
+
* .static() is not allowed
|
|
283
315
|
*/
|
|
284
|
-
class
|
|
285
|
-
constructor () {
|
|
286
|
-
this.
|
|
287
|
-
this.layers =
|
|
316
|
+
class SlowerMicroRouter {
|
|
317
|
+
constructor (path, layerPool) {
|
|
318
|
+
this.path = path;
|
|
319
|
+
this.layers = layerPool;
|
|
288
320
|
|
|
289
321
|
// Create basic route shortcuts
|
|
290
322
|
// get(), post(), put(), delete()
|
|
291
323
|
for (let verb of HTTP_VERBS) {
|
|
292
|
-
this[verb] = function (
|
|
293
|
-
return this.#setRoute(verb,
|
|
324
|
+
this[verb] = function (callback) {
|
|
325
|
+
return this.#setRoute(verb, callback);
|
|
294
326
|
};
|
|
295
327
|
}
|
|
296
328
|
}
|
|
297
329
|
|
|
298
|
-
#setRoute (method,
|
|
330
|
+
#setRoute (method, handler) {
|
|
331
|
+
const path = this.path;
|
|
299
332
|
if (typeof method !== 'string')
|
|
300
333
|
throw new Error('<SlowerSubRouter>.route :: "method" parameter must be of type String');
|
|
301
334
|
if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
|
|
@@ -306,7 +339,8 @@ class SlowerSubRouter {
|
|
|
306
339
|
return this;
|
|
307
340
|
}
|
|
308
341
|
|
|
309
|
-
all (
|
|
342
|
+
all (handler) {
|
|
343
|
+
const path = this.path;
|
|
310
344
|
if (typeof path === 'string')
|
|
311
345
|
for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
|
|
312
346
|
else if (typeof path !== 'function')
|
|
@@ -316,13 +350,9 @@ class SlowerSubRouter {
|
|
|
316
350
|
}
|
|
317
351
|
// Just a more comprehensive call to app.all for defining middlewares
|
|
318
352
|
use (...b) { this.all(...b); return this; };
|
|
319
|
-
|
|
320
|
-
static (directoryPath, mountPath = '') {
|
|
321
|
-
this.layers.push({ static: true, directoryPath, mountPath });
|
|
322
|
-
return this;
|
|
323
|
-
}
|
|
324
353
|
}
|
|
325
354
|
|
|
355
|
+
|
|
326
356
|
// Create basic main routing creation
|
|
327
357
|
function slower (options) {
|
|
328
358
|
return new SlowerRouter(options);
|