From 99c1d9af689e5325f3cf535c4007b3aeb8325229 Mon Sep 17 00:00:00 2001 From: Minteck Date: Tue, 10 Jan 2023 14:54:04 +0100 Subject: Update - This is an automated commit --- school/node_modules/trouter/index.js | 45 ++++++++ school/node_modules/trouter/license.md | 21 ++++ school/node_modules/trouter/package.json | 34 ++++++ school/node_modules/trouter/readme.md | 179 +++++++++++++++++++++++++++++++ 4 files changed, 279 insertions(+) create mode 100644 school/node_modules/trouter/index.js create mode 100644 school/node_modules/trouter/license.md create mode 100644 school/node_modules/trouter/package.json create mode 100644 school/node_modules/trouter/readme.md (limited to 'school/node_modules/trouter') diff --git a/school/node_modules/trouter/index.js b/school/node_modules/trouter/index.js new file mode 100644 index 0000000..a9e7f4b --- /dev/null +++ b/school/node_modules/trouter/index.js @@ -0,0 +1,45 @@ +const { exec, match, parse } = require('matchit'); + +class Trouter { + constructor(opts) { + this.opts = opts || {}; + this.routes = {}; + this.handlers = {}; + + this.all = this.add.bind(this, '*'); + this.get = this.add.bind(this, 'GET'); + this.head = this.add.bind(this, 'HEAD'); + this.patch = this.add.bind(this, 'PATCH'); + this.options = this.add.bind(this, 'OPTIONS'); + this.connect = this.add.bind(this, 'CONNECT'); + this.delete = this.add.bind(this, 'DELETE'); + this.trace = this.add.bind(this, 'TRACE'); + this.post = this.add.bind(this, 'POST'); + this.put = this.add.bind(this, 'PUT'); + } + + add(method, pattern, ...fns) { + // Save decoded pattern info + if (this.routes[method] === void 0) this.routes[method]=[]; + this.routes[method].push(parse(pattern)); + // Save route handler(s) + if (this.handlers[method] === void 0) this.handlers[method]={}; + this.handlers[method][pattern] = fns; + // Allow chainable + return this; + } + + find(method, url) { + let arr = match(url, this.routes[method] || []); + if (arr.length === 0) { + arr = match(url, this.routes[method='*'] || []); + if (!arr.length) return false; + } + return { + params: exec(url, arr), + handlers: this.handlers[method][arr[0].old] + }; + } +} + +module.exports = Trouter; diff --git a/school/node_modules/trouter/license.md b/school/node_modules/trouter/license.md new file mode 100644 index 0000000..a3f96f8 --- /dev/null +++ b/school/node_modules/trouter/license.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Luke Edwards (lukeed.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/school/node_modules/trouter/package.json b/school/node_modules/trouter/package.json new file mode 100644 index 0000000..d079296 --- /dev/null +++ b/school/node_modules/trouter/package.json @@ -0,0 +1,34 @@ +{ + "name": "trouter", + "version": "2.0.1", + "description": "🐟 A fast, small-but-mighty, familiar ~fish~ router", + "repository": "lukeed/trouter", + "license": "MIT", + "files": [ + "index.js" + ], + "author": { + "name": "Luke Edwards", + "email": "luke.edwards05@gmail.com", + "url": "lukeed.com" + }, + "engines": { + "node": ">=6" + }, + "scripts": { + "bench": "node bench", + "test": "tape test/*.js | tap-spec" + }, + "keywords": [ + "route", + "router", + "routing" + ], + "dependencies": { + "matchit": "^1.0.0" + }, + "devDependencies": { + "tap-spec": "^4.1.1", + "tape": "^4.6.3" + } +} diff --git a/school/node_modules/trouter/readme.md b/school/node_modules/trouter/readme.md new file mode 100644 index 0000000..0f25d3f --- /dev/null +++ b/school/node_modules/trouter/readme.md @@ -0,0 +1,179 @@ +# trouter [![Build Status](https://travis-ci.org/lukeed/trouter.svg?branch=master)](https://travis-ci.org/lukeed/trouter) + +> 🐟 A fast, small-but-mighty, familiar ~fish~ router + + +## Install + +``` +$ npm install --save trouter +``` + + +## Usage + +```js +const Trouter = require('trouter'); +const router = new Trouter(); + +// Define all routes +router + .get('/users', _ => { + console.log('> Getting all users'); + }) + .add('POST', '/users', _ => { + console.log('~> Adding a user'); + }) + .get('/users/:id', val => { + console.log('~> Getting user with ID:', val); + }); + +// Find a route definition +let obj = router.find('GET', '/users/123'); +//=> obj.params ~> { id:123 } +//=> obj.handlers ~> Array + +// Execute the handlers, passing value +obj.handlers.forEach(fn => { + fn(obj.params.id); +}); +//=> ~> Getting user with ID: 123 + +// Returns `false` if no match +router.find('DELETE', '/foo'); +//=> false +``` + +## API + +### Trouter() + +Initializes a new `Trouter` instance. Currently accepts no options. + +### trouter.add(method, pattern, ...handlers) +Returns: `self` + +Stores a `method` + `pattern` pairing internally, along with its handler(s). + +#### method +Type: `String` + +Any lowercased, [valid HTTP/1.1 verb](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods#Specifications) — choose from one of the following: + +``` +GET HEAD PATCH OPTIONS CONNECT DELETE TRACE POST PUT +``` + +#### pattern +Type: `String` + +Unlike most router libraries, Trouter does not use `RegExp` to determine pathname matches. Instead, it uses string comparison which is much faster, but also limits the pattern complexity. + +The supported pattern types are: + +* static (`/users`) +* named parameters (`/users/:id`) +* nested parameters (`/users/:id/books/:title`) +* optional parameters (`/users/:id?/books/:title?`) +* any match / wildcards (`/users/*`) + +#### handlers +Type: `Array|Function` + +The function(s) that should be tied to this `pattern`. + +Because this is a [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), whatever you pass will _always_ be cast to an Array. + +> **Important:** Trouter does not care what your function signature looks like!
You are not bound to the `(req, res)` standard, or even passing a `Function` at all! + +### trouter.all(pattern, ...handlers) +Returns: `self` + +This is an alias for [`trouter.add('*', pattern, ...handlers)`](#trouteraddmethod-pattern-handlers), matching **all** HTTP methods. + +> **Important:** If the `pattern` used within `all()` exists for a specific `method` as well, then **only** the method-specific entry will be returned! + +```js +router.post('/hello', () => 'FROM POST'); +router.add('GET', '/hello', () => 'FROM GET'); +router.all('/hello', () => 'FROM ALL'); + +let { handlers } = router.find('GET', '/hello'); +handlers[0](); +//=> 'FROM GET' + +router.find('POST', '/hello').handlers[0](); +//=> 'FROM POST' + +router.find('DELETE', '/hello').handlers[0](); +//=> 'FROM ALL' + +router.find('PUT', '/hello').handlers[0](); +//=> 'FROM ALL' +``` + +### trouter.METHOD(pattern, ...handlers) + +This is an alias for [`trouter.add(METHOD, pattern, ...handlers)`](#trouteraddmethod-pattern-handlers), where `METHOD` is any lowercased HTTP verb. + +```js +const noop = _ => {}: +const app = new Trouter(); + +app.get('/users/:id', noop); +app.post('/users', noop); +app.patch('/users/:id', noop); + +// less common methods too +app.trace('/foo', noop); +app.connect('/bar', noop); +``` + +### trouter.find(method, url) +Returns: `Object|Boolean`
+Searches within current instance for a `method` + `pattern` pairing that matches the current `method` + `url`. + +This method will return `false` if no match is found. Otherwise it returns an Object with `params` and `handlers` keys. + +* `params` — Object whose keys are the named parameters of your route pattern. +* `handlers` — Array containing the `...handlers` provided to [`.add()`](#trouteraddmethod-pattern-handlers) or [`.METHOD()`](#troutermethodpattern-handlers) + + +#### method +Type: `String` + +Any valid HTTP method name. + +#### url +Type: `String` + +The URL used to match against pattern definitions. This is typically `req.url`. + + +## Benchmarks + +> Run on Node v8.9.0 + +``` +GET / ON / + --> 9,548,621 ops/sec ±0.65% (96 runs sampled) + +POST /users ON /users + --> 2,324,166 ops/sec ±0.52% (93 runs sampled) + +GET /users/123 ON /users/:id + --> 1,704,811 ops/sec ±0.50% (95 runs sampled) + +PUT /users/123/books ON /users/:id/books/:title? + --> 1,396,875 ops/sec ±0.14% (94 runs sampled) + +DELETE /users/123/books/foo ON /users/:id/books/:title + --> 1,266,708 ops/sec ±0.59% (95 runs sampled) + +HEAD /hello on /hello -- via all() + --> 1,641,558 ops/sec ±0.14% (96 runs sampled) +``` + +## License + +MIT © [Luke Edwards](https://lukeed.com) -- cgit