{"tsd":{"directory":"test"},"bugs":{"url":"https://github.com/fastify/middleman/issues"},"dist":{"shasum":"ad15defe9ede424af52726ca78be3e421a974a77","tarball":"https://registry.npmjs.org/middie/-/middie-7.0.0.tgz","fileCount":16,"integrity":"sha512-gAX1DrlTOMsDW6MskZrCJQW1j56I7VHqKFdwGSMtZuIbymye2j89R1juPLLCyEMrx0sZR9EZ+VqnWqOh9KH4PQ==","signatures":[{"sig":"MEYCIQC2C3ePwcmE534kTKEzLHDvkiT/xfXf0JPRFcRXvdLcHwIhANnySeuDjP/BoGFNRdfaHIVC+qXXJ9ZPWBzIa0FsQNSu","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":61036,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJie48dACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpUFRAAkJEVhqYcQ79H6bZCKxkE7BNpu3WLUgkGqEQwnf7j7lAFtL8S\r\n5tXnki8PKPQ/uvuwiQe93Qfj3QqbMFjeYIBNA2deN5Iv7yfqB9gm3iLNoDO3\r\nHNz5qAb5Sxu/nf2654CoLNI7CjtIvHGYGGtqcedYe+EGqYCQU/oHf6/U53Cf\r\nKStwC05MAsjQf6BAYe9xKnHNymuj1Jsz3JgV9in6i98Xv2nftmunSXg335GZ\r\nHz+vMWqZXGFnDwuDtXXJcwVqPNfD7eeuEmrVIUcVKvaEWRSQ2lrvnEzfM88I\r\nPBTWY32Z1KpRIDYjiv54OI51swRe4ruhfqV3A56GuxG/x2Lt7QJ/CL5xyKEX\r\nke2v7qD0braaY29nZBpaD2pljBs4o151hBcOOvyLRTMW2IajUq/eFe5Mk04a\r\nSn4UfP3hpS8Xjyr1t+CyBu2CwfCnNRRDWmxIOeRExFmhxHH5a1SCvZdTYssl\r\nLHVTj/N/4z+4I6FKAqxiSaohmbKMygQdyoniKmU8gOGfNg0oyk2hH6Vmr0hI\r\neYhvb78W856kPCgQ24avU5s82klNegjv8Xrw+9GmIluYZA0yEeECDmH2NvtP\r\nP0vdEf0J8WHjmKF1J3JJdeBf2Ywf3xkANmp1FUfyKrL54QRteVpHLasQdHhw\r\nAg0B1Ad0J801pLgSux99WJc62CH+0oVOnnU=\r\n=g8zT\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","name":"middie","types":"index.d.ts","author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"readme":"# middie\n\n![CI](https://github.com/fastify/middie/workflows/CI/badge.svg)\n[![NPM version](https://img.shields.io/npm/v/middie.svg?style=flat)](https://www.npmjs.com/package/middie)\n[![Known Vulnerabilities](https://snyk.io/test/github/fastify/middie/badge.svg)](https://snyk.io/test/github/fastify/middie)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)\n\n*middie* is the plugin that adds middleware support on steroids to [Fastify](https://www.npmjs.com/package/fastify).\n\nThe syntax style is the same as [express](http://npm.im/express)/[connect](https://www.npmjs.com/package/connect).\nDoes not support the full syntax `middleware(err, req, res, next)`, because error handling is done inside Fastify.\n\n## Install\n\n```\nnpm install middie\n```\n\n## Usage\nRegister the plugin and start using your middleware.\n```js\nconst Fastify = require('fastify')\n\nasync function build () {\n  const fastify = Fastify()\n  await fastify.register(require('middie'), {\n    hook: 'onRequest' // default\n  })\n  // do you know we also have cors support?\n  // https://github.com/fastify/fastify-cors\n  fastify.use(require('cors')())\n  return fastify\n}\n\nbuild()\n  .then(fastify => fastify.listen(3000))\n  .catch(console.log)\n```\n\n### Encapsulation support\n\nThe encapsulation works as usual with Fastify, you can register the plugin in a subsystem and your code will work only inside there, or you can declare the middie plugin top level and register a middleware in a nested plugin, and the middleware will be executed only for the nested routes of the specific plugin.\n\n*Register the plugin in its own subsystem:*\n```js\nconst fastify = require('fastify')()\n\nfastify.register(subsystem)\n\nasync function subsystem (fastify, opts) {\n  await fastify.register(require('middie'))\n  fastify.use(require('cors')())\n}\n```\n\n*Register a middleware in a specific plugin:*\n```js\nconst fastify = require('fastify')()\n\nfastify\n  .register(require('middie'))\n  .register(subsystem)\n\nasync function subsystem (fastify, opts) {\n  fastify.use(require('cors')())\n}\n```\n\n### Hooks and middleware\n\n__Every registered middleware will be run during the `onRequest` hook phase__, so the registration order is important.\nTake a look at the [Lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/) documentation page to understand better how every request is executed.\n\n```js\nconst fastify = require('fastify')()\n\nfastify\n  .register(require('middie'))\n  .register(subsystem)\n\nasync function subsystem (fastify, opts) {\n  fastify.addHook('onRequest', async (req, reply) => {\n    console.log('first')\n  })\n\n  fastify.use((req, res, next) => {\n    console.log('second')\n    next()\n  })\n\n  fastify.addHook('onRequest', async (req, reply) => {\n    console.log('third')\n  })\n}\n```\n\nIf you want to change the Fastify hook that the middleware will be attached to, pass a `hook` option like so:\n\n*Note you can access `req.body` from the `preValidation` lifecycle step onwards. Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/) documentation page to see the order of the steps.*\n\n```js\nconst fastify = require('fastify')()\n\nfastify\n  .register(require('middie'), { hook: 'preHandler' })\n  .register(subsystem)\n\nasync function subsystem (fastify, opts) {\n  fastify.addHook('onRequest', async (req, reply) => {\n    console.log('first')\n  })\n\n  fastify.use((req, res, next) => {\n    console.log('third')\n    next()\n  })\n\n  fastify.addHook('onRequest', async (req, reply) => {\n    console.log('second')\n  })\n\n  fastify.addHook('preHandler', async (req, reply) => {\n    console.log('fourth')\n  })\n}\n```\n\n### Restrict middleware execution to a certain path(s)\n\nIf you need to run a middleware only under certain path(s), just pass the path as first parameter to use and you are done!\n\n```js\nconst fastify = require('fastify')()\nconst path = require('path')\nconst serveStatic = require('serve-static')\n\nfastify\n  .register(require('middie'))\n  .register(subsystem)\n\nasync function subsystem (fastify, opts) {\n  // Single path\n  fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))\n\n  // Wildcard path\n  fastify.use('/css/*', serveStatic(path.join(__dirname, '/assets')))\n\n  // Multiple paths\n  fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))\n}\n```\n\n# Middie Engine\n\nYou can also use the engine itself without the Fastify plugin system.\n\n## Usage\n```js\nconst Middie = require('middie/engine')\nconst http = require('http')\nconst helmet = require('helmet')\nconst cors = require('cors')\n\nconst middie = Middie(_runMiddlewares)\nmiddie.use(helmet())\nmiddie.use(cors())\n\nhttp\n  .createServer(function handler (req, res) {\n    middie.run(req, res)\n  })\n  .listen(3000)\n\nfunction _runMiddlewares (err, req, res) {\n  if (err) {\n    console.log(err)\n    res.end(err)\n    return\n  }\n\n  // => routing function\n}\n```\n<a name=\"keep-context\"></a>\n#### Keep the context\nIf you need it you can also keep the context of the calling function by calling `run` with `run(req, res, this)`, in this way you can avoid closures allocation.\n\n```js\nhttp\n  .createServer(function handler (req, res) {\n    middie.run(req, res, { context: 'object' })\n  })\n  .listen(3000)\n\nfunction _runMiddlewares (err, req, res, ctx) {\n  if (err) {\n    console.log(err)\n    res.end(err)\n    return\n  }\n  console.log(ctx)\n}\n```\n\n<a name=\"restrict-usage\"></a>\n#### Restrict middleware execution to a certain path(s)\nIf you need to run a middleware only under certains path(s), just pass the path as first parameter to `use` and you are done!\n\n*Note that this does support routes with parameters, e.g. `/user/:id/comments`, but all the matched parameters will be discarded*\n\n```js\n// Single path\nmiddie.use('/public', staticFiles('/assets'))\n\n// Multiple middleware\nmiddie.use('/public', [cors(), staticFiles('/assets')])\n\n// Multiple paths\nmiddie.use(['/public', '/dist'], staticFiles('/assets'))\n\n// Multiple paths and multiple middleware\nmiddie.use(['/public', '/dist'], [cors(), staticFiles('/assets')])\n```\n\nTo guarantee compatibility with Express, adding a prefix uses [`path-to-regexp`](https://www.npmjs.com/package/path-to-regexp) to compute\na `RegExp`, which is then used to math every request: it is significantly slower.\n\n## TypeScript support\n\nTo use this module with TypeScript, make sure to install `@types/connect`.\n\n## Middleware alternatives\n\nFastify offers some alternatives to the most commonly used Express middleware:\n\n| Express Middleware | Fastify Plugin |\n| ------------- |---------------|\n| [`helmet`](https://github.com/helmetjs/helmet) | [`fastify-helmet`](https://github.com/fastify/fastify-helmet) |\n| [`cors`](https://github.com/expressjs/cors) | [`fastify-cors`](https://github.com/fastify/fastify-cors) |\n| [`serve-static`](https://github.com/expressjs/serve-static) | [`fastify-static`](https://github.com/fastify/fastify-static) |\n\n## Acknowledgements\n\nThis project is kindly sponsored by:\n- [nearForm](https://nearform.com)\n\nPast sponsors:\n- [LetzDoIt](https://www.letzdoitapp.com/)\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","gitHead":"0fec59048b3a8d6581e74071b3915f71c662ac64","license":"MIT","scripts":{"test":"standard && tap --no-coverage test/*.test.js && tsd","coverage":"tap --cov --coverage-report=html test.js"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"homepage":"https://github.com/fastify/middleman#readme","keywords":["fastify","middleware","webframework","performances"],"repository":{"url":"git+https://github.com/fastify/middleman.git","type":"git"},"_npmVersion":"8.5.5","description":"Middleware engine for Fastify","directories":{},"maintainers":[{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"delvedor","email":"tommydelved@gmail.com"}],"_nodeVersion":"16.14.0","contributors":[{"url":"http://delved.org","name":"Tomas Della Vedova"}],"dependencies":{"reusify":"^1.0.4","fastify-plugin":"^3.0.0","path-to-regexp":"^6.1.0"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"tap":"^16.0.0","tsd":"^0.20.0","cors":"^2.8.5","helmet":"^5.0.0","fastify":"^4.0.0-rc.2","standard":"^17.0.0","pre-commit":"^1.2.2","simple-get":"^4.0.0","typescript":"4.6.4","@types/node":"^17.0.0","serve-static":"^1.14.1","@types/connect":"^3.4.33"},"_npmOperationalInternal":{"tmp":"tmp/middie_7.0.0_1652264733128_0.8832759437666442","host":"s3://npm-registry-packages"},"_id":"middie@7.0.0","version":"7.0.0"}