diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/koa-mount | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/koa-mount')
| -rw-r--r-- | node_modules/koa-mount/History.md | 58 | ||||
| -rw-r--r-- | node_modules/koa-mount/Readme.md | 127 | ||||
| -rw-r--r-- | node_modules/koa-mount/index.js | 94 | ||||
| -rw-r--r-- | node_modules/koa-mount/package.json | 35 |
4 files changed, 314 insertions, 0 deletions
diff --git a/node_modules/koa-mount/History.md b/node_modules/koa-mount/History.md new file mode 100644 index 00000000..975455bc --- /dev/null +++ b/node_modules/koa-mount/History.md @@ -0,0 +1,58 @@ + +3.0.0 / 2017-03-26 +================== + + * use async function + * bump deps + * update examples + +2.0.0 / 2016-03-07 +================== + + * update to koa v2 api (breaking koa v1) + * travis: only test on node 4+ + +1.3.0 / 2014-05-08 +================== + + * add .mountPath + +1.2.5 / 2014-03-15 + + * fix matching `/prefix` against `/prefix*` + +1.2.4 / 2014-03-25 +================== + + * use `yield* next` + * bump deps + +1.2.3 / 2014-01-12 +================== + + * optimize: compose before closure. Closes #12 + +1.2.2 / 2013-12-22 +================== + + * fix for koa 0.1.0 + * update koa-compose + +1.2.1 / 2013-12-17 +================== + + * restore prefix before going further downstream + +1.2.0 / 2013-11-29 +================== + + * add error propagation example + * add socket errors example + * add some debug() calls + * update to new middleware signature + +1.1.0 / 2013-09-08 +================== + + * add middleware mounting support + * add support for pathless mounting diff --git a/node_modules/koa-mount/Readme.md b/node_modules/koa-mount/Readme.md new file mode 100644 index 00000000..3d22b5f8 --- /dev/null +++ b/node_modules/koa-mount/Readme.md @@ -0,0 +1,127 @@ +# koa-mount + + Mount other Koa applications as middleware. The `path` passed to `mount()` is stripped + from the URL temporarily until the stack unwinds. This is useful for creating entire + apps or middleware that will function correctly regardless of which path segment(s) + they should operate on. + +## Installation + +```js +$ npm install koa-mount +``` + +## Examples + + View the [./examples](examples) directory for working examples. + +### Mounting Applications + + Entire applications mounted at specific paths. For example you could mount + a blog application at "/blog", with a router that matches paths such as + "GET /", "GET /posts", and will behave properly for "GET /blog/posts" etc + when mounted. + +```js +const mount = require('koa-mount'); +const Koa = require('koa'); + +// hello + +const a = new Koa(); + +a.use(async function (ctx, next){ + await next(); + ctx.body = 'Hello'; +}); + +// world + +const b = new Koa(); + +b.use(async function (ctx, next){ + await next(); + ctx.body = 'World'; +}); + +// app + +const app = new Koa(); + +app.use(mount('/hello', a)); +app.use(mount('/world', b)); + +app.listen(3000); +console.log('listening on port 3000'); +``` + + Try the following requests: + +``` +$ GET / +Not Found + +$ GET /hello +Hello + +$ GET /world +World +``` + +### Mounting Middleware + + Mount middleware at specific paths, allowing them to operate independently + of the prefix, as they're not aware of it. + +```js +const mount = require('koa-mount'); +const Koa = require('koa'); + +async function hello(ctx, next){ + await next(); + ctx.body = 'Hello'; +} + +async function world(ctx, next){ + await next(); + ctx.body = 'World'; +} + +const app = new Koa(); + +app.use(mount('/hello', hello)); +app.use(mount('/world', world)); + +app.listen(3000); +console.log('listening on port 3000'); +``` + +### Optional Paths + + The path argument is optional, defaulting to "/": + +```js +app.use(mount(a)); +app.use(mount(b)); +``` + +## Debugging + + Use the __DEBUG__ environement variable to whitelist + koa-mount debug output: + +``` +$ DEBUG=koa-mount node myapp.js & +$ GET /foo/bar/baz + + koa-mount enter /foo/bar/baz -> /bar/baz +2s + koa-mount enter /bar/baz -> /baz +0ms + koa-mount enter /baz -> / +0ms + koa-mount leave /baz -> / +1ms + koa-mount leave /bar/baz -> /baz +0ms + koa-mount leave /foo/bar/baz -> /bar/baz +0ms +``` + +## License + + MIT diff --git a/node_modules/koa-mount/index.js b/node_modules/koa-mount/index.js new file mode 100644 index 00000000..3ca60aa8 --- /dev/null +++ b/node_modules/koa-mount/index.js @@ -0,0 +1,94 @@ + +/** + * Module dependencies. + */ + +const debug = require('debug')('koa-mount'); +const compose = require('koa-compose'); +const assert = require('assert'); + +/** + * Expose `mount()`. + */ + +module.exports = mount; + +/** + * Mount `app` with `prefix`, `app` + * may be a Koa application or + * middleware function. + * + * @param {String|Application|Function} prefix, app, or function + * @param {Application|Function} [app or function] + * @return {Function} + * @api public + */ + +function mount(prefix, app) { + if ('string' != typeof prefix) { + app = prefix; + prefix = '/'; + } + + assert('/' == prefix[0], 'mount path must begin with "/"'); + + // compose + const downstream = app.middleware + ? compose(app.middleware) + : app; + + // don't need to do mounting here + if ('/' == prefix) return downstream; + + const trailingSlash = '/' == prefix.slice(-1); + + const name = app.name || 'unnamed'; + debug('mount %s %s', prefix, name); + + return async function (ctx, upstream){ + const prev = ctx.path; + const newPath = match(prev); + debug('mount %s %s -> %s', prefix, name, newPath); + if (!newPath) return await upstream(); + + ctx.mountPath = prefix; + ctx.path = newPath; + debug('enter %s -> %s', prev, ctx.path); + + await downstream(ctx, async () => { + ctx.path = prev; + await upstream(); + ctx.path = newPath; + }); + + debug('leave %s -> %s', prev, ctx.path); + ctx.path = prev; + }; + + /** + * Check if `prefix` satisfies a `path`. + * Returns the new path. + * + * match('/images/', '/lkajsldkjf') => false + * match('/images', '/images') => / + * match('/images/', '/images') => false + * match('/images/', '/images/asdf') => /asdf + * + * @param {String} prefix + * @param {String} path + * @return {String|Boolean} + * @api private + */ + + function match(path) { + // does not match prefix at all + if (0 != path.indexOf(prefix)) return false; + + const newPath = path.replace(prefix, '') || '/'; + if (trailingSlash) return newPath; + + // `/mount` does not match `/mountlkjalskjdf` + if ('/' != newPath[0]) return false; + return newPath; + } +} diff --git a/node_modules/koa-mount/package.json b/node_modules/koa-mount/package.json new file mode 100644 index 00000000..872e31f0 --- /dev/null +++ b/node_modules/koa-mount/package.json @@ -0,0 +1,35 @@ +{ + "name": "koa-mount", + "description": "Mounting middleware for koa", + "repository": "koajs/mount", + "version": "3.0.0", + "keywords": [ + "koa", + "middleware", + "mount", + "mounting" + ], + "files": [ + "index.js" + ], + "devDependencies": { + "istanbul": "^0.4.5", + "koa": "^2.0.1", + "mocha": "^3.2.0", + "should": "^11.2.0", + "supertest": "^3.0.0" + }, + "license": "MIT", + "dependencies": { + "debug": "^2.6.1", + "koa-compose": "^3.2.1" + }, + "scripts": { + "test": "NODE_ENV=test mocha --reporter spec", + "test-cov": "NODE_ENV=test istanbul cover ./node_modules/.bin/_mocha", + "test-travis": "NODE_ENV=test istanbul cover ./node_modules/.bin/_mocha --report lcovonly" + }, + "engines": { + "node": ">= 7.6.0" + } +} |
