penambahan web socket
This commit is contained in:
187
examples/nuxt3-websocket-client/node_modules/@koa/router/HISTORY.md
generated
vendored
Normal file
187
examples/nuxt3-websocket-client/node_modules/@koa/router/HISTORY.md
generated
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
# History
|
||||
|
||||
**[History has moved to the Releases tab of GitHub](https://github.com/koajs/router/releases).**
|
||||
|
||||
|
||||
## Log
|
||||
|
||||
|
||||
## 9.0.0 / 2020-04-09
|
||||
|
||||
* Update `path-to-regexp`. Migration path: change usage of `'*'` in routes to `(.*)` or `:splat*`.
|
||||
* Example: `router.get('*', ....)` becomes `router.get('(.*)') ....)`
|
||||
|
||||
|
||||
## 8.0.0 / 2019-06-16
|
||||
|
||||
**others**
|
||||
|
||||
* [b5dd5e8](http://github.com/koajs/koa-router/commit/b5dd5e8f00e841b7061a62ab6228cbe96a999470)] - chore: rename to @koa/router (dead-horse)
|
||||
|
||||
---
|
||||
|
||||
Changelogs inherit from koa-router.
|
||||
|
||||
|
||||
## 7.4.0
|
||||
|
||||
* Fix router.url() for multiple nested routers [#407](https://github.com/alexmingoia/koa-router/pull/407)
|
||||
* `layer.name` added to `ctx` at `ctx.routerName` during routing [#412](https://github.com/alexmingoia/koa-router/pull/412)
|
||||
* Router.use() was erroneously settings `(.*)` as a prefix to all routers nested with .use that did not pass an explicit prefix string as the first argument. This resulted in routes being matched that should not have been, included the running of multiple route handlers in error. [#369](https://github.com/alexmingoia/koa-router/issues/369) and [#410](https://github.com/alexmingoia/koa-router/issues/410) include information on this issue.
|
||||
|
||||
|
||||
## 7.3.0
|
||||
|
||||
* Router#url() now accepts query parameters to add to generated urls [#396](https://github.com/alexmingoia/koa-router/pull/396)
|
||||
|
||||
|
||||
## 7.2.1
|
||||
|
||||
* Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
|
||||
|
||||
|
||||
## 7.2.0
|
||||
|
||||
* Fix a bug in Router#url and append Router object to ctx. [#350](https://github.com/alexmingoia/koa-router/pull/350)
|
||||
* Adds `_matchedRouteName` to context [#337](https://github.com/alexmingoia/koa-router/pull/337)
|
||||
* Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)
|
||||
|
||||
|
||||
## 7.1.1
|
||||
|
||||
* Fix bug where param handlers were run out of order [#282](https://github.com/alexmingoia/koa-router/pull/282)
|
||||
|
||||
|
||||
## 7.1.0
|
||||
|
||||
* Backports: merge 5.4 work into the 7.x upstream. See 5.4.0 updates for more details.
|
||||
|
||||
|
||||
## 7.0.1
|
||||
|
||||
* Fix: allowedMethods should be ctx.method not this.method [#215](https://github.com/alexmingoia/koa-router/pull/215)
|
||||
|
||||
|
||||
## 7.0.0
|
||||
|
||||
* The API has changed to match the new promise-based middleware
|
||||
signature of koa 2. See the
|
||||
[koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
|
||||
information.
|
||||
* Middleware is now always run in the order declared by `.use()` (or `.get()`,
|
||||
etc.), which matches Express 4 API.
|
||||
|
||||
|
||||
## 5.4.0
|
||||
|
||||
* Expose matched route at `ctx._matchedRoute`.
|
||||
|
||||
|
||||
## 5.3.0
|
||||
|
||||
* Register multiple routes with array of paths [#203](https://github.com/alexmingoia/koa-router/issue/143).
|
||||
* Improved router.url() [#143](https://github.com/alexmingoia/koa-router/pull/143)
|
||||
* Adds support for named routes and regular expressions
|
||||
[#152](https://github.com/alexmingoia/koa-router/pull/152)
|
||||
* Add support for custom throw functions for 405 and 501 responses [#206](https://github.com/alexmingoia/koa-router/pull/206)
|
||||
|
||||
|
||||
## 5.2.3
|
||||
|
||||
* Fix for middleware running twice when nesting routes [#184](https://github.com/alexmingoia/koa-router/issues/184)
|
||||
|
||||
|
||||
## 5.2.2
|
||||
|
||||
* Register routes without params before those with params [#183](https://github.com/alexmingoia/koa-router/pull/183)
|
||||
* Fix for allowed methods [#182](https://github.com/alexmingoia/koa-router/issues/182)
|
||||
|
||||
|
||||
## 5.2.0
|
||||
|
||||
* Add support for async/await. Resolves [#130](https://github.com/alexmingoia/koa-router/issues/130).
|
||||
* Add support for array of paths by Router#use(). Resolves [#175](https://github.com/alexmingoia/koa-router/issues/175).
|
||||
* Inherit param middleware when nesting routers. Fixes [#170](https://github.com/alexmingoia/koa-router/issues/170).
|
||||
* Default router middleware without path to root. Fixes [#161](https://github.com/alexmingoia/koa-router/issues/161), [#155](https://github.com/alexmingoia/koa-router/issues/155), [#156](https://github.com/alexmingoia/koa-router/issues/156).
|
||||
* Run nested router middleware after parent's. Fixes [#156](https://github.com/alexmingoia/koa-router/issues/156).
|
||||
* Remove dependency on koa-compose.
|
||||
|
||||
|
||||
## 5.1.1
|
||||
|
||||
* Match routes in order they were defined. Fixes #131.
|
||||
|
||||
|
||||
## 5.1.0
|
||||
|
||||
* Support mounting router middleware at a given path.
|
||||
|
||||
|
||||
## 5.0.1
|
||||
|
||||
* Fix bug with missing parameters when nesting routers.
|
||||
|
||||
|
||||
## 5.0.0
|
||||
|
||||
* Remove confusing API for extending koa app with router methods. Router#use()
|
||||
does not have the same behavior as app#use().
|
||||
* Add support for nesting routes.
|
||||
* Remove support for regular expression routes to achieve nestable routers and
|
||||
enable future trie-based routing optimizations.
|
||||
|
||||
|
||||
## 4.3.2
|
||||
|
||||
* Do not send 405 if route matched but status is 404. Fixes #112, closes #114.
|
||||
|
||||
|
||||
## 4.3.1
|
||||
|
||||
* Do not run middleware if not yielded to by previous middleware. Fixes #115.
|
||||
|
||||
|
||||
## 4.3.0
|
||||
|
||||
* Add support for router prefixes.
|
||||
* Add MIT license.
|
||||
|
||||
|
||||
## 4.2.0
|
||||
|
||||
* Fixed issue with router middleware being applied even if no route was
|
||||
matched.
|
||||
* Router.url - new static method to generate url from url pattern and data
|
||||
|
||||
|
||||
## 4.1.0
|
||||
|
||||
Private API changed to separate context parameter decoration from route
|
||||
matching. `Router#match` and `Route#match` are now pure functions that return
|
||||
an array of routes that match the URL path.
|
||||
|
||||
For modules using this private API that need to determine if a method and path
|
||||
match a route, `route.methods` must be checked against the routes returned from
|
||||
`router.match()`:
|
||||
|
||||
```javascript
|
||||
var matchedRoute = router.match(path).filter(function (route) {
|
||||
return ~route.methods.indexOf(method);
|
||||
}).shift();
|
||||
```
|
||||
|
||||
|
||||
## 4.0.0
|
||||
|
||||
405, 501, and OPTIONS response handling was moved into separate middleware
|
||||
`router.allowedMethods()`. This resolves incorrect 501 or 405 responses when
|
||||
using multiple routers.
|
||||
|
||||
### Breaking changes
|
||||
|
||||
4.x is mostly backwards compatible with 3.x, except for the following:
|
||||
|
||||
* Instantiating a router with `new` and `app` returns the router instance,
|
||||
whereas 3.x returns the router middleware. When creating a router in 4.x, the
|
||||
only time router middleware is returned is when creating using the
|
||||
`Router(app)` signature (with `app` and without `new`).
|
||||
21
examples/nuxt3-websocket-client/node_modules/@koa/router/LICENSE
generated
vendored
Normal file
21
examples/nuxt3-websocket-client/node_modules/@koa/router/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Alexander C. Mingoia and @koajs contributors
|
||||
|
||||
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.
|
||||
85
examples/nuxt3-websocket-client/node_modules/@koa/router/README.md
generated
vendored
Normal file
85
examples/nuxt3-websocket-client/node_modules/@koa/router/README.md
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
# [@koa/router](https://github.com/koajs/router)
|
||||
|
||||
> Router middleware for [Koa](https://github.com/koajs/koa). Maintained by [Forward Email][forward-email] and [Lad][].
|
||||
|
||||
[](https://github.com/koajs/router/actions/workflows/ci.yml)
|
||||
[](https://github.com/sindresorhus/xo)
|
||||
[](https://github.com/prettier/prettier)
|
||||
[](https://lass.js.org)
|
||||
[](LICENSE)
|
||||
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* [Features](#features)
|
||||
* [Migrating to 7 / Koa 2](#migrating-to-7--koa-2)
|
||||
* [Install](#install)
|
||||
* [Typescript Support](#typescript-support)
|
||||
* [API Reference](#api-reference)
|
||||
* [Contributors](#contributors)
|
||||
* [License](#license)
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
* Express-style routing (`app.get`, `app.put`, `app.post`, etc.)
|
||||
* Named URL parameters
|
||||
* Named routes with URL generation
|
||||
* Match routes with specific host
|
||||
* Responds to `OPTIONS` requests with allowed methods
|
||||
* Support for `405 Method Not Allowed` and `501 Not Implemented`
|
||||
* Multiple route middleware
|
||||
* Multiple and nestable routers
|
||||
* `async/await` support
|
||||
|
||||
|
||||
## Migrating to 7 / Koa 2
|
||||
|
||||
* The API has changed to match the new promise-based middleware
|
||||
signature of koa 2. See the [koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
|
||||
information.
|
||||
* Middleware is now always run in the order declared by `.use()` (or `.get()`,
|
||||
etc.), which matches Express 4 API.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
[npm][]:
|
||||
|
||||
```sh
|
||||
npm install @koa/router
|
||||
```
|
||||
|
||||
|
||||
## Typescript Support
|
||||
|
||||
```sh
|
||||
npm install --save-dev @types/koa__router
|
||||
```
|
||||
|
||||
|
||||
## API Reference
|
||||
|
||||
See [API Reference](./API.md) for more documentation.
|
||||
|
||||
|
||||
## Contributors
|
||||
|
||||
| Name |
|
||||
| ---------------- |
|
||||
| **Alex Mingoia** |
|
||||
| **@koajs** |
|
||||
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © Alex Mingoia
|
||||
|
||||
|
||||
##
|
||||
|
||||
[forward-email]: https://forwardemail.net
|
||||
|
||||
[lad]: https://lad.js.org
|
||||
|
||||
[npm]: https//www.npmjs.com
|
||||
7
examples/nuxt3-websocket-client/node_modules/@koa/router/lib/API_tpl.hbs
generated
vendored
Normal file
7
examples/nuxt3-websocket-client/node_modules/@koa/router/lib/API_tpl.hbs
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
## API Reference
|
||||
{{#module name="koa-router"~}}
|
||||
{{>body~}}
|
||||
{{>member-index~}}
|
||||
{{>members~}}
|
||||
{{/module~}}
|
||||
244
examples/nuxt3-websocket-client/node_modules/@koa/router/lib/layer.js
generated
vendored
Normal file
244
examples/nuxt3-websocket-client/node_modules/@koa/router/lib/layer.js
generated
vendored
Normal file
@@ -0,0 +1,244 @@
|
||||
const { parse: parseUrl, format: formatUrl } = require('url');
|
||||
const { pathToRegexp, compile, parse } = require('path-to-regexp');
|
||||
|
||||
module.exports = Layer;
|
||||
|
||||
/**
|
||||
* Initialize a new routing Layer with given `method`, `path`, and `middleware`.
|
||||
*
|
||||
* @param {String|RegExp} path Path string or regular expression.
|
||||
* @param {Array} methods Array of HTTP verbs.
|
||||
* @param {Array} middleware Layer callback/middleware or series of.
|
||||
* @param {Object=} opts
|
||||
* @param {String=} opts.name route name
|
||||
* @param {String=} opts.sensitive case sensitive (default: false)
|
||||
* @param {String=} opts.strict require the trailing slash (default: false)
|
||||
* @returns {Layer}
|
||||
* @private
|
||||
*/
|
||||
|
||||
function Layer(path, methods, middleware, opts = {}) {
|
||||
this.opts = opts;
|
||||
this.name = this.opts.name || null;
|
||||
this.methods = [];
|
||||
this.paramNames = [];
|
||||
this.stack = Array.isArray(middleware) ? middleware : [middleware];
|
||||
|
||||
for (const method of methods) {
|
||||
const l = this.methods.push(method.toUpperCase());
|
||||
if (this.methods[l - 1] === 'GET') this.methods.unshift('HEAD');
|
||||
}
|
||||
|
||||
// ensure middleware is a function
|
||||
for (let i = 0; i < this.stack.length; i++) {
|
||||
const fn = this.stack[i];
|
||||
const type = typeof fn;
|
||||
if (type !== 'function')
|
||||
throw new Error(
|
||||
`${methods.toString()} \`${
|
||||
this.opts.name || path
|
||||
}\`: \`middleware\` must be a function, not \`${type}\``
|
||||
);
|
||||
}
|
||||
|
||||
this.path = path;
|
||||
this.regexp = pathToRegexp(path, this.paramNames, this.opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether request `path` matches route.
|
||||
*
|
||||
* @param {String} path
|
||||
* @returns {Boolean}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Layer.prototype.match = function (path) {
|
||||
return this.regexp.test(path);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns map of URL parameters for given `path` and `paramNames`.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Array.<String>} captures
|
||||
* @param {Object=} params
|
||||
* @returns {Object}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Layer.prototype.params = function (path, captures, params = {}) {
|
||||
for (let len = captures.length, i = 0; i < len; i++) {
|
||||
if (this.paramNames[i]) {
|
||||
const c = captures[i];
|
||||
if (c && c.length > 0)
|
||||
params[this.paramNames[i].name] = c ? safeDecodeURIComponent(c) : c;
|
||||
}
|
||||
}
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns array of regexp url path captures.
|
||||
*
|
||||
* @param {String} path
|
||||
* @returns {Array.<String>}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Layer.prototype.captures = function (path) {
|
||||
return this.opts.ignoreCaptures ? [] : path.match(this.regexp).slice(1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate URL for route using given `params`.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* const route = new Layer('/users/:id', ['GET'], fn);
|
||||
*
|
||||
* route.url({ id: 123 }); // => "/users/123"
|
||||
* ```
|
||||
*
|
||||
* @param {Object} params url parameters
|
||||
* @returns {String}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Layer.prototype.url = function (params, options) {
|
||||
let args = params;
|
||||
const url = this.path.replace(/\(\.\*\)/g, '');
|
||||
|
||||
if (typeof params !== 'object') {
|
||||
args = Array.prototype.slice.call(arguments);
|
||||
if (typeof args[args.length - 1] === 'object') {
|
||||
options = args[args.length - 1];
|
||||
args = args.slice(0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
const toPath = compile(url, { encode: encodeURIComponent, ...options });
|
||||
let replaced;
|
||||
|
||||
const tokens = parse(url);
|
||||
let replace = {};
|
||||
|
||||
if (Array.isArray(args)) {
|
||||
for (let len = tokens.length, i = 0, j = 0; i < len; i++) {
|
||||
if (tokens[i].name) replace[tokens[i].name] = args[j++];
|
||||
}
|
||||
} else if (tokens.some((token) => token.name)) {
|
||||
replace = params;
|
||||
} else if (!options) {
|
||||
options = params;
|
||||
}
|
||||
|
||||
replaced = toPath(replace);
|
||||
|
||||
if (options && options.query) {
|
||||
replaced = parseUrl(replaced);
|
||||
if (typeof options.query === 'string') {
|
||||
replaced.search = options.query;
|
||||
} else {
|
||||
replaced.search = undefined;
|
||||
replaced.query = options.query;
|
||||
}
|
||||
|
||||
return formatUrl(replaced);
|
||||
}
|
||||
|
||||
return replaced;
|
||||
};
|
||||
|
||||
/**
|
||||
* Run validations on route named parameters.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* router
|
||||
* .param('user', function (id, ctx, next) {
|
||||
* ctx.user = users[id];
|
||||
* if (!ctx.user) return ctx.status = 404;
|
||||
* next();
|
||||
* })
|
||||
* .get('/users/:user', function (ctx, next) {
|
||||
* ctx.body = ctx.user;
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @param {String} param
|
||||
* @param {Function} middleware
|
||||
* @returns {Layer}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Layer.prototype.param = function (param, fn) {
|
||||
const { stack } = this;
|
||||
const params = this.paramNames;
|
||||
const middleware = function (ctx, next) {
|
||||
return fn.call(this, ctx.params[param], ctx, next);
|
||||
};
|
||||
|
||||
middleware.param = param;
|
||||
|
||||
const names = params.map(function (p) {
|
||||
return p.name;
|
||||
});
|
||||
|
||||
const x = names.indexOf(param);
|
||||
if (x > -1) {
|
||||
// iterate through the stack, to figure out where to place the handler fn
|
||||
stack.some(function (fn, i) {
|
||||
// param handlers are always first, so when we find an fn w/o a param property, stop here
|
||||
// if the param handler at this part of the stack comes after the one we are adding, stop here
|
||||
if (!fn.param || names.indexOf(fn.param) > x) {
|
||||
// inject this param handler right before the current item
|
||||
stack.splice(i, 0, middleware);
|
||||
return true; // then break the loop
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prefix route path.
|
||||
*
|
||||
* @param {String} prefix
|
||||
* @returns {Layer}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Layer.prototype.setPrefix = function (prefix) {
|
||||
if (this.path) {
|
||||
this.path =
|
||||
this.path !== '/' || this.opts.strict === true
|
||||
? `${prefix}${this.path}`
|
||||
: prefix;
|
||||
this.paramNames = [];
|
||||
this.regexp = pathToRegexp(this.path, this.paramNames, this.opts);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Safe decodeURIComponent, won't throw any error.
|
||||
* If `decodeURIComponent` error happen, just return the original value.
|
||||
*
|
||||
* @param {String} text
|
||||
* @returns {String} URL decode original string.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function safeDecodeURIComponent(text) {
|
||||
try {
|
||||
return decodeURIComponent(text);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
845
examples/nuxt3-websocket-client/node_modules/@koa/router/lib/router.js
generated
vendored
Normal file
845
examples/nuxt3-websocket-client/node_modules/@koa/router/lib/router.js
generated
vendored
Normal file
@@ -0,0 +1,845 @@
|
||||
/**
|
||||
* RESTful resource routing middleware for koa.
|
||||
*
|
||||
* @author Alex Mingoia <talk@alexmingoia.com>
|
||||
* @link https://github.com/alexmingoia/koa-router
|
||||
*/
|
||||
|
||||
const compose = require('koa-compose');
|
||||
const HttpError = require('http-errors');
|
||||
const methods = require('methods');
|
||||
const { pathToRegexp } = require('path-to-regexp');
|
||||
const Layer = require('./layer');
|
||||
const debug = require('debug')('koa-router');
|
||||
|
||||
/**
|
||||
* @module koa-router
|
||||
*/
|
||||
|
||||
module.exports = Router;
|
||||
|
||||
/**
|
||||
* Create a new router.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* Basic usage:
|
||||
*
|
||||
* ```javascript
|
||||
* const Koa = require('koa');
|
||||
* const Router = require('@koa/router');
|
||||
*
|
||||
* const app = new Koa();
|
||||
* const router = new Router();
|
||||
*
|
||||
* router.get('/', (ctx, next) => {
|
||||
* // ctx.router available
|
||||
* });
|
||||
*
|
||||
* app
|
||||
* .use(router.routes())
|
||||
* .use(router.allowedMethods());
|
||||
* ```
|
||||
*
|
||||
* @alias module:koa-router
|
||||
* @param {Object=} opts
|
||||
* @param {Boolean=false} opts.exclusive only run last matched route's controller when there are multiple matches
|
||||
* @param {String=} opts.prefix prefix router paths
|
||||
* @param {String|RegExp=} opts.host host for router match
|
||||
* @constructor
|
||||
*/
|
||||
|
||||
function Router(opts = {}) {
|
||||
if (!(this instanceof Router)) return new Router(opts);
|
||||
|
||||
this.opts = opts;
|
||||
this.methods = this.opts.methods || [
|
||||
'HEAD',
|
||||
'OPTIONS',
|
||||
'GET',
|
||||
'PUT',
|
||||
'PATCH',
|
||||
'POST',
|
||||
'DELETE'
|
||||
];
|
||||
this.exclusive = Boolean(this.opts.exclusive);
|
||||
|
||||
this.params = {};
|
||||
this.stack = [];
|
||||
this.host = this.opts.host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create `router.verb()` methods, where *verb* is one of the HTTP verbs such
|
||||
* as `router.get()` or `router.post()`.
|
||||
*
|
||||
* Match URL patterns to callback functions or controller actions using `router.verb()`,
|
||||
* where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()`.
|
||||
*
|
||||
* Additionally, `router.all()` can be used to match against all methods.
|
||||
*
|
||||
* ```javascript
|
||||
* router
|
||||
* .get('/', (ctx, next) => {
|
||||
* ctx.body = 'Hello World!';
|
||||
* })
|
||||
* .post('/users', (ctx, next) => {
|
||||
* // ...
|
||||
* })
|
||||
* .put('/users/:id', (ctx, next) => {
|
||||
* // ...
|
||||
* })
|
||||
* .del('/users/:id', (ctx, next) => {
|
||||
* // ...
|
||||
* })
|
||||
* .all('/users/:id', (ctx, next) => {
|
||||
* // ...
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* When a route is matched, its path is available at `ctx._matchedRoute` and if named,
|
||||
* the name is available at `ctx._matchedRouteName`
|
||||
*
|
||||
* Route paths will be translated to regular expressions using
|
||||
* [path-to-regexp](https://github.com/pillarjs/path-to-regexp).
|
||||
*
|
||||
* Query strings will not be considered when matching requests.
|
||||
*
|
||||
* #### Named routes
|
||||
*
|
||||
* Routes can optionally have names. This allows generation of URLs and easy
|
||||
* renaming of URLs during development.
|
||||
*
|
||||
* ```javascript
|
||||
* router.get('user', '/users/:id', (ctx, next) => {
|
||||
* // ...
|
||||
* });
|
||||
*
|
||||
* router.url('user', 3);
|
||||
* // => "/users/3"
|
||||
* ```
|
||||
*
|
||||
* #### Multiple middleware
|
||||
*
|
||||
* Multiple middleware may be given:
|
||||
*
|
||||
* ```javascript
|
||||
* router.get(
|
||||
* '/users/:id',
|
||||
* (ctx, next) => {
|
||||
* return User.findOne(ctx.params.id).then(function(user) {
|
||||
* ctx.user = user;
|
||||
* next();
|
||||
* });
|
||||
* },
|
||||
* ctx => {
|
||||
* console.log(ctx.user);
|
||||
* // => { id: 17, name: "Alex" }
|
||||
* }
|
||||
* );
|
||||
* ```
|
||||
*
|
||||
* ### Nested routers
|
||||
*
|
||||
* Nesting routers is supported:
|
||||
*
|
||||
* ```javascript
|
||||
* const forums = new Router();
|
||||
* const posts = new Router();
|
||||
*
|
||||
* posts.get('/', (ctx, next) => {...});
|
||||
* posts.get('/:pid', (ctx, next) => {...});
|
||||
* forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
|
||||
*
|
||||
* // responds to "/forums/123/posts" and "/forums/123/posts/123"
|
||||
* app.use(forums.routes());
|
||||
* ```
|
||||
*
|
||||
* #### Router prefixes
|
||||
*
|
||||
* Route paths can be prefixed at the router level:
|
||||
*
|
||||
* ```javascript
|
||||
* const router = new Router({
|
||||
* prefix: '/users'
|
||||
* });
|
||||
*
|
||||
* router.get('/', ...); // responds to "/users"
|
||||
* router.get('/:id', ...); // responds to "/users/:id"
|
||||
* ```
|
||||
*
|
||||
* #### URL parameters
|
||||
*
|
||||
* Named route parameters are captured and added to `ctx.params`.
|
||||
*
|
||||
* ```javascript
|
||||
* router.get('/:category/:title', (ctx, next) => {
|
||||
* console.log(ctx.params);
|
||||
* // => { category: 'programming', title: 'how-to-node' }
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is
|
||||
* used to convert paths to regular expressions.
|
||||
*
|
||||
*
|
||||
* ### Match host for each router instance
|
||||
*
|
||||
* ```javascript
|
||||
* const router = new Router({
|
||||
* host: 'example.domain' // only match if request host exactly equal `example.domain`
|
||||
* });
|
||||
*
|
||||
* ```
|
||||
*
|
||||
* OR host cloud be a regexp
|
||||
*
|
||||
* ```javascript
|
||||
* const router = new Router({
|
||||
* host: /.*\.?example\.domain$/ // all host end with .example.domain would be matched
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @name get|put|post|patch|delete|del
|
||||
* @memberof module:koa-router.prototype
|
||||
* @param {String} path
|
||||
* @param {Function=} middleware route middleware(s)
|
||||
* @param {Function} callback route callback
|
||||
* @returns {Router}
|
||||
*/
|
||||
|
||||
for (const method_ of methods) {
|
||||
function setMethodVerb(method) {
|
||||
Router.prototype[method] = function (name, path, middleware) {
|
||||
if (typeof path === 'string' || path instanceof RegExp) {
|
||||
middleware = Array.prototype.slice.call(arguments, 2);
|
||||
} else {
|
||||
middleware = Array.prototype.slice.call(arguments, 1);
|
||||
path = name;
|
||||
name = null;
|
||||
}
|
||||
|
||||
// Sanity check to ensure we have a viable path candidate (eg: string|regex|non-empty array)
|
||||
if (
|
||||
typeof path !== 'string' &&
|
||||
!(path instanceof RegExp) &&
|
||||
(!Array.isArray(path) || path.length === 0)
|
||||
)
|
||||
throw new Error(
|
||||
`You have to provide a path when adding a ${method} handler`
|
||||
);
|
||||
|
||||
this.register(path, [method], middleware, { name });
|
||||
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
setMethodVerb(method_);
|
||||
}
|
||||
|
||||
// Alias for `router.delete()` because delete is a reserved word
|
||||
// eslint-disable-next-line dot-notation
|
||||
Router.prototype.del = Router.prototype['delete'];
|
||||
|
||||
/**
|
||||
* Use given middleware.
|
||||
*
|
||||
* Middleware run in the order they are defined by `.use()`. They are invoked
|
||||
* sequentially, requests start at the first middleware and work their way
|
||||
* "down" the middleware stack.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* // session middleware will run before authorize
|
||||
* router
|
||||
* .use(session())
|
||||
* .use(authorize());
|
||||
*
|
||||
* // use middleware only with given path
|
||||
* router.use('/users', userAuth());
|
||||
*
|
||||
* // or with an array of paths
|
||||
* router.use(['/users', '/admin'], userAuth());
|
||||
*
|
||||
* app.use(router.routes());
|
||||
* ```
|
||||
*
|
||||
* @param {String=} path
|
||||
* @param {Function} middleware
|
||||
* @param {Function=} ...
|
||||
* @returns {Router}
|
||||
*/
|
||||
|
||||
Router.prototype.use = function () {
|
||||
const router = this;
|
||||
const middleware = Array.prototype.slice.call(arguments);
|
||||
let path;
|
||||
|
||||
// support array of paths
|
||||
if (Array.isArray(middleware[0]) && typeof middleware[0][0] === 'string') {
|
||||
const arrPaths = middleware[0];
|
||||
for (const p of arrPaths) {
|
||||
router.use.apply(router, [p].concat(middleware.slice(1)));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
const hasPath = typeof middleware[0] === 'string';
|
||||
if (hasPath) path = middleware.shift();
|
||||
|
||||
for (const m of middleware) {
|
||||
if (m.router) {
|
||||
const cloneRouter = Object.assign(
|
||||
Object.create(Router.prototype),
|
||||
m.router,
|
||||
{
|
||||
stack: [...m.router.stack]
|
||||
}
|
||||
);
|
||||
|
||||
for (let j = 0; j < cloneRouter.stack.length; j++) {
|
||||
const nestedLayer = cloneRouter.stack[j];
|
||||
const cloneLayer = Object.assign(
|
||||
Object.create(Layer.prototype),
|
||||
nestedLayer
|
||||
);
|
||||
|
||||
if (path) cloneLayer.setPrefix(path);
|
||||
if (router.opts.prefix) cloneLayer.setPrefix(router.opts.prefix);
|
||||
router.stack.push(cloneLayer);
|
||||
cloneRouter.stack[j] = cloneLayer;
|
||||
}
|
||||
|
||||
if (router.params) {
|
||||
function setRouterParams(paramArr) {
|
||||
const routerParams = paramArr;
|
||||
for (const key of routerParams) {
|
||||
cloneRouter.param(key, router.params[key]);
|
||||
}
|
||||
}
|
||||
|
||||
setRouterParams(Object.keys(router.params));
|
||||
}
|
||||
} else {
|
||||
const keys = [];
|
||||
pathToRegexp(router.opts.prefix || '', keys);
|
||||
const routerPrefixHasParam = router.opts.prefix && keys.length;
|
||||
router.register(path || '([^/]*)', [], m, {
|
||||
end: false,
|
||||
ignoreCaptures: !hasPath && !routerPrefixHasParam
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the path prefix for a Router instance that was already initialized.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* router.prefix('/things/:thing_id')
|
||||
* ```
|
||||
*
|
||||
* @param {String} prefix
|
||||
* @returns {Router}
|
||||
*/
|
||||
|
||||
Router.prototype.prefix = function (prefix) {
|
||||
prefix = prefix.replace(/\/$/, '');
|
||||
|
||||
this.opts.prefix = prefix;
|
||||
|
||||
for (let i = 0; i < this.stack.length; i++) {
|
||||
const route = this.stack[i];
|
||||
route.setPrefix(prefix);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns router middleware which dispatches a route matching the request.
|
||||
*
|
||||
* @returns {Function}
|
||||
*/
|
||||
|
||||
Router.prototype.routes = Router.prototype.middleware = function () {
|
||||
const router = this;
|
||||
|
||||
const dispatch = function dispatch(ctx, next) {
|
||||
debug('%s %s', ctx.method, ctx.path);
|
||||
|
||||
const hostMatched = router.matchHost(ctx.host);
|
||||
|
||||
if (!hostMatched) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const path =
|
||||
router.opts.routerPath || ctx.newRouterPath || ctx.path || ctx.routerPath;
|
||||
const matched = router.match(path, ctx.method);
|
||||
let layerChain;
|
||||
|
||||
if (ctx.matched) {
|
||||
ctx.matched.push.apply(ctx.matched, matched.path);
|
||||
} else {
|
||||
ctx.matched = matched.path;
|
||||
}
|
||||
|
||||
ctx.router = router;
|
||||
|
||||
if (!matched.route) return next();
|
||||
|
||||
const matchedLayers = matched.pathAndMethod;
|
||||
const mostSpecificLayer = matchedLayers[matchedLayers.length - 1];
|
||||
ctx._matchedRoute = mostSpecificLayer.path;
|
||||
if (mostSpecificLayer.name) {
|
||||
ctx._matchedRouteName = mostSpecificLayer.name;
|
||||
}
|
||||
|
||||
layerChain = (
|
||||
router.exclusive ? [mostSpecificLayer] : matchedLayers
|
||||
).reduce(function (memo, layer) {
|
||||
memo.push(function (ctx, next) {
|
||||
ctx.captures = layer.captures(path, ctx.captures);
|
||||
ctx.params = ctx.request.params = layer.params(
|
||||
path,
|
||||
ctx.captures,
|
||||
ctx.params
|
||||
);
|
||||
ctx.routerPath = layer.path;
|
||||
ctx.routerName = layer.name;
|
||||
ctx._matchedRoute = layer.path;
|
||||
if (layer.name) {
|
||||
ctx._matchedRouteName = layer.name;
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
return memo.concat(layer.stack);
|
||||
}, []);
|
||||
|
||||
return compose(layerChain)(ctx, next);
|
||||
};
|
||||
|
||||
dispatch.router = this;
|
||||
|
||||
return dispatch;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns separate middleware for responding to `OPTIONS` requests with
|
||||
* an `Allow` header containing the allowed methods, as well as responding
|
||||
* with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* const Koa = require('koa');
|
||||
* const Router = require('@koa/router');
|
||||
*
|
||||
* const app = new Koa();
|
||||
* const router = new Router();
|
||||
*
|
||||
* app.use(router.routes());
|
||||
* app.use(router.allowedMethods());
|
||||
* ```
|
||||
*
|
||||
* **Example with [Boom](https://github.com/hapijs/boom)**
|
||||
*
|
||||
* ```javascript
|
||||
* const Koa = require('koa');
|
||||
* const Router = require('@koa/router');
|
||||
* const Boom = require('boom');
|
||||
*
|
||||
* const app = new Koa();
|
||||
* const router = new Router();
|
||||
*
|
||||
* app.use(router.routes());
|
||||
* app.use(router.allowedMethods({
|
||||
* throw: true,
|
||||
* notImplemented: () => new Boom.notImplemented(),
|
||||
* methodNotAllowed: () => new Boom.methodNotAllowed()
|
||||
* }));
|
||||
* ```
|
||||
*
|
||||
* @param {Object=} options
|
||||
* @param {Boolean=} options.throw throw error instead of setting status and header
|
||||
* @param {Function=} options.notImplemented throw the returned value in place of the default NotImplemented error
|
||||
* @param {Function=} options.methodNotAllowed throw the returned value in place of the default MethodNotAllowed error
|
||||
* @returns {Function}
|
||||
*/
|
||||
|
||||
Router.prototype.allowedMethods = function (options = {}) {
|
||||
const implemented = this.methods;
|
||||
|
||||
return function allowedMethods(ctx, next) {
|
||||
return next().then(function () {
|
||||
const allowed = {};
|
||||
|
||||
if (!ctx.status || ctx.status === 404) {
|
||||
for (let i = 0; i < ctx.matched.length; i++) {
|
||||
const route = ctx.matched[i];
|
||||
for (let j = 0; j < route.methods.length; j++) {
|
||||
const method = route.methods[j];
|
||||
allowed[method] = method;
|
||||
}
|
||||
}
|
||||
|
||||
const allowedArr = Object.keys(allowed);
|
||||
|
||||
if (!~implemented.indexOf(ctx.method)) {
|
||||
if (options.throw) {
|
||||
const notImplementedThrowable =
|
||||
typeof options.notImplemented === 'function'
|
||||
? options.notImplemented() // set whatever the user returns from their function
|
||||
: new HttpError.NotImplemented();
|
||||
|
||||
throw notImplementedThrowable;
|
||||
} else {
|
||||
ctx.status = 501;
|
||||
ctx.set('Allow', allowedArr.join(', '));
|
||||
}
|
||||
} else if (allowedArr.length > 0) {
|
||||
if (ctx.method === 'OPTIONS') {
|
||||
ctx.status = 200;
|
||||
ctx.body = '';
|
||||
ctx.set('Allow', allowedArr.join(', '));
|
||||
} else if (!allowed[ctx.method]) {
|
||||
if (options.throw) {
|
||||
const notAllowedThrowable =
|
||||
typeof options.methodNotAllowed === 'function'
|
||||
? options.methodNotAllowed() // set whatever the user returns from their function
|
||||
: new HttpError.MethodNotAllowed();
|
||||
|
||||
throw notAllowedThrowable;
|
||||
} else {
|
||||
ctx.status = 405;
|
||||
ctx.set('Allow', allowedArr.join(', '));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Register route with all methods.
|
||||
*
|
||||
* @param {String} name Optional.
|
||||
* @param {String} path
|
||||
* @param {Function=} middleware You may also pass multiple middleware.
|
||||
* @param {Function} callback
|
||||
* @returns {Router}
|
||||
*/
|
||||
|
||||
Router.prototype.all = function (name, path, middleware) {
|
||||
if (typeof path === 'string') {
|
||||
middleware = Array.prototype.slice.call(arguments, 2);
|
||||
} else {
|
||||
middleware = Array.prototype.slice.call(arguments, 1);
|
||||
path = name;
|
||||
name = null;
|
||||
}
|
||||
|
||||
// Sanity check to ensure we have a viable path candidate (eg: string|regex|non-empty array)
|
||||
if (
|
||||
typeof path !== 'string' &&
|
||||
!(path instanceof RegExp) &&
|
||||
(!Array.isArray(path) || path.length === 0)
|
||||
)
|
||||
throw new Error('You have to provide a path when adding an all handler');
|
||||
|
||||
this.register(path, methods, middleware, { name });
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Redirect `source` to `destination` URL with optional 30x status `code`.
|
||||
*
|
||||
* Both `source` and `destination` can be route names.
|
||||
*
|
||||
* ```javascript
|
||||
* router.redirect('/login', 'sign-in');
|
||||
* ```
|
||||
*
|
||||
* This is equivalent to:
|
||||
*
|
||||
* ```javascript
|
||||
* router.all('/login', ctx => {
|
||||
* ctx.redirect('/sign-in');
|
||||
* ctx.status = 301;
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @param {String} source URL or route name.
|
||||
* @param {String} destination URL or route name.
|
||||
* @param {Number=} code HTTP status code (default: 301).
|
||||
* @returns {Router}
|
||||
*/
|
||||
|
||||
Router.prototype.redirect = function (source, destination, code) {
|
||||
// lookup source route by name
|
||||
if (typeof source === 'symbol' || source[0] !== '/') {
|
||||
source = this.url(source);
|
||||
if (source instanceof Error) throw source;
|
||||
}
|
||||
|
||||
// lookup destination route by name
|
||||
if (
|
||||
typeof destination === 'symbol' ||
|
||||
(destination[0] !== '/' && !destination.includes('://'))
|
||||
) {
|
||||
destination = this.url(destination);
|
||||
if (destination instanceof Error) throw destination;
|
||||
}
|
||||
|
||||
return this.all(source, (ctx) => {
|
||||
ctx.redirect(destination);
|
||||
ctx.status = code || 301;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Create and register a route.
|
||||
*
|
||||
* @param {String} path Path string.
|
||||
* @param {Array.<String>} methods Array of HTTP verbs.
|
||||
* @param {Function} middleware Multiple middleware also accepted.
|
||||
* @returns {Layer}
|
||||
* @private
|
||||
*/
|
||||
|
||||
Router.prototype.register = function (path, methods, middleware, opts = {}) {
|
||||
const router = this;
|
||||
const { stack } = this;
|
||||
|
||||
// support array of paths
|
||||
if (Array.isArray(path)) {
|
||||
for (const curPath of path) {
|
||||
router.register.call(router, curPath, methods, middleware, opts);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// create route
|
||||
const route = new Layer(path, methods, middleware, {
|
||||
end: opts.end === false ? opts.end : true,
|
||||
name: opts.name,
|
||||
sensitive: opts.sensitive || this.opts.sensitive || false,
|
||||
strict: opts.strict || this.opts.strict || false,
|
||||
prefix: opts.prefix || this.opts.prefix || '',
|
||||
ignoreCaptures: opts.ignoreCaptures
|
||||
});
|
||||
|
||||
if (this.opts.prefix) {
|
||||
route.setPrefix(this.opts.prefix);
|
||||
}
|
||||
|
||||
// add parameter middleware
|
||||
for (let i = 0; i < Object.keys(this.params).length; i++) {
|
||||
const param = Object.keys(this.params)[i];
|
||||
route.param(param, this.params[param]);
|
||||
}
|
||||
|
||||
stack.push(route);
|
||||
|
||||
debug('defined route %s %s', route.methods, route.path);
|
||||
|
||||
return route;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lookup route with given `name`.
|
||||
*
|
||||
* @param {String} name
|
||||
* @returns {Layer|false}
|
||||
*/
|
||||
|
||||
Router.prototype.route = function (name) {
|
||||
const routes = this.stack;
|
||||
|
||||
for (let len = routes.length, i = 0; i < len; i++) {
|
||||
if (routes[i].name && routes[i].name === name) return routes[i];
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate URL for route. Takes a route name and map of named `params`.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* router.get('user', '/users/:id', (ctx, next) => {
|
||||
* // ...
|
||||
* });
|
||||
*
|
||||
* router.url('user', 3);
|
||||
* // => "/users/3"
|
||||
*
|
||||
* router.url('user', { id: 3 });
|
||||
* // => "/users/3"
|
||||
*
|
||||
* router.use((ctx, next) => {
|
||||
* // redirect to named route
|
||||
* ctx.redirect(ctx.router.url('sign-in'));
|
||||
* })
|
||||
*
|
||||
* router.url('user', { id: 3 }, { query: { limit: 1 } });
|
||||
* // => "/users/3?limit=1"
|
||||
*
|
||||
* router.url('user', { id: 3 }, { query: "limit=1" });
|
||||
* // => "/users/3?limit=1"
|
||||
* ```
|
||||
*
|
||||
* @param {String} name route name
|
||||
* @param {Object} params url parameters
|
||||
* @param {Object} [options] options parameter
|
||||
* @param {Object|String} [options.query] query options
|
||||
* @returns {String|Error}
|
||||
*/
|
||||
|
||||
Router.prototype.url = function (name, params) {
|
||||
const route = this.route(name);
|
||||
|
||||
if (route) {
|
||||
const args = Array.prototype.slice.call(arguments, 1);
|
||||
return route.url.apply(route, args);
|
||||
}
|
||||
|
||||
return new Error(`No route found for name: ${String(name)}`);
|
||||
};
|
||||
|
||||
/**
|
||||
* Match given `path` and return corresponding routes.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {String} method
|
||||
* @returns {Object.<path, pathAndMethod>} returns layers that matched path and
|
||||
* path and method.
|
||||
* @private
|
||||
*/
|
||||
|
||||
Router.prototype.match = function (path, method) {
|
||||
const layers = this.stack;
|
||||
let layer;
|
||||
const matched = {
|
||||
path: [],
|
||||
pathAndMethod: [],
|
||||
route: false
|
||||
};
|
||||
|
||||
for (let len = layers.length, i = 0; i < len; i++) {
|
||||
layer = layers[i];
|
||||
|
||||
debug('test %s %s', layer.path, layer.regexp);
|
||||
|
||||
// eslint-disable-next-line unicorn/prefer-regexp-test
|
||||
if (layer.match(path)) {
|
||||
matched.path.push(layer);
|
||||
|
||||
if (layer.methods.length === 0 || ~layer.methods.indexOf(method)) {
|
||||
matched.pathAndMethod.push(layer);
|
||||
if (layer.methods.length > 0) matched.route = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return matched;
|
||||
};
|
||||
|
||||
/**
|
||||
* Match given `input` to allowed host
|
||||
* @param {String} input
|
||||
* @returns {boolean}
|
||||
*/
|
||||
|
||||
Router.prototype.matchHost = function (input) {
|
||||
const { host } = this;
|
||||
|
||||
if (!host) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!input) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof host === 'string') {
|
||||
return input === host;
|
||||
}
|
||||
|
||||
if (typeof host === 'object' && host instanceof RegExp) {
|
||||
return host.test(input);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Run middleware for named route parameters. Useful for auto-loading or
|
||||
* validation.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* router
|
||||
* .param('user', (id, ctx, next) => {
|
||||
* ctx.user = users[id];
|
||||
* if (!ctx.user) return ctx.status = 404;
|
||||
* return next();
|
||||
* })
|
||||
* .get('/users/:user', ctx => {
|
||||
* ctx.body = ctx.user;
|
||||
* })
|
||||
* .get('/users/:user/friends', ctx => {
|
||||
* return ctx.user.getFriends().then(function(friends) {
|
||||
* ctx.body = friends;
|
||||
* });
|
||||
* })
|
||||
* // /users/3 => {"id": 3, "name": "Alex"}
|
||||
* // /users/3/friends => [{"id": 4, "name": "TJ"}]
|
||||
* ```
|
||||
*
|
||||
* @param {String} param
|
||||
* @param {Function} middleware
|
||||
* @returns {Router}
|
||||
*/
|
||||
|
||||
Router.prototype.param = function (param, middleware) {
|
||||
this.params[param] = middleware;
|
||||
for (let i = 0; i < this.stack.length; i++) {
|
||||
const route = this.stack[i];
|
||||
route.param(param, middleware);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate URL from url pattern and given `params`.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```javascript
|
||||
* const url = Router.url('/users/:id', {id: 1});
|
||||
* // => "/users/1"
|
||||
* ```
|
||||
*
|
||||
* @param {String} path url pattern
|
||||
* @param {Object} params url parameters
|
||||
* @returns {String}
|
||||
*/
|
||||
Router.url = function (path) {
|
||||
const args = Array.prototype.slice.call(arguments, 1);
|
||||
return Layer.prototype.url.apply({ path }, args);
|
||||
};
|
||||
73
examples/nuxt3-websocket-client/node_modules/@koa/router/package.json
generated
vendored
Normal file
73
examples/nuxt3-websocket-client/node_modules/@koa/router/package.json
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"name": "@koa/router",
|
||||
"description": "Router middleware for koa. Maintained by Forward Email and Lad.",
|
||||
"version": "12.0.2",
|
||||
"author": "Alex Mingoia <talk@alexmingoia.com>",
|
||||
"bugs": {
|
||||
"url": "https://github.com/koajs/router/issues",
|
||||
"email": "niftylettuce@gmail.com"
|
||||
},
|
||||
"contributors": [
|
||||
"Alex Mingoia <talk@alexmingoia.com>",
|
||||
"@koajs"
|
||||
],
|
||||
"dependencies": {
|
||||
"debug": "^4.3.4",
|
||||
"http-errors": "^2.0.0",
|
||||
"koa-compose": "^4.1.0",
|
||||
"methods": "^1.1.2",
|
||||
"path-to-regexp": "^6.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^17.7.2",
|
||||
"@commitlint/config-conventional": "^17.7.0",
|
||||
"@ladjs/env": "^4.0.0",
|
||||
"ava": "^5.3.1",
|
||||
"cross-env": "^7.0.3",
|
||||
"eslint": "8.39.0",
|
||||
"eslint-config-xo-lass": "^2.0.1",
|
||||
"expect.js": "^0.3.1",
|
||||
"fixpack": "^4.0.0",
|
||||
"husky": "^8.0.3",
|
||||
"jsdoc-to-markdown": "^8.0.0",
|
||||
"koa": "^2.14.2",
|
||||
"lint-staged": "^14.0.1",
|
||||
"mocha": "^10.2.0",
|
||||
"nyc": "^15.1.0",
|
||||
"remark-cli": "11",
|
||||
"remark-preset-github": "^4.0.4",
|
||||
"should": "^13.2.3",
|
||||
"supertest": "^6.3.3",
|
||||
"wrk": "^1.2.1",
|
||||
"xo": "0.53.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 12"
|
||||
},
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"homepage": "https://github.com/koajs/router",
|
||||
"keywords": [
|
||||
"koa",
|
||||
"middleware",
|
||||
"route",
|
||||
"router"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "lib/router.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/koajs/router.git"
|
||||
},
|
||||
"scripts": {
|
||||
"bench": "make -C bench",
|
||||
"coverage": "nyc npm run test",
|
||||
"docs": "NODE_ENV=test jsdoc2md -t ./lib/API_tpl.hbs --src ./lib/*.js >| API.md",
|
||||
"lint": "xo --fix && remark . -qfo && fixpack",
|
||||
"prepare": "husky install",
|
||||
"prextest": "npm run lint",
|
||||
"test": "mocha test/**/*.js",
|
||||
"test:watch": "mocha test/**/*.js --watch"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user