Comparing version 5.2.0 to 5.3.0
@@ -7,4 +7,4 @@ 'use strict' | ||
function middie (complete) { | ||
var middlewares = [] | ||
var pool = reusify(Holder) | ||
const middlewares = [] | ||
const pool = reusify(Holder) | ||
@@ -22,3 +22,3 @@ return { | ||
var regexp | ||
let regexp | ||
if (url) { | ||
@@ -32,3 +32,3 @@ regexp = pathToRegexp(sanitizePrefixUrl(url), [], { | ||
if (Array.isArray(f)) { | ||
for (var val of f) { | ||
for (const val of f) { | ||
middlewares.push({ | ||
@@ -57,3 +57,3 @@ regexp, | ||
var holder = pool.get() | ||
const holder = pool.get() | ||
holder.req = req | ||
@@ -74,9 +74,9 @@ holder.res = res | ||
var that = this | ||
const that = this | ||
this.done = function (err) { | ||
var req = that.req | ||
var res = that.res | ||
var url = that.url | ||
var context = that.context | ||
var i = that.i++ | ||
const req = that.req | ||
const res = that.res | ||
const url = that.url | ||
const context = that.context | ||
const i = that.i++ | ||
@@ -102,7 +102,7 @@ req.url = req.originalUrl | ||
} else { | ||
var middleware = middlewares[i] | ||
var fn = middleware.fn | ||
var regexp = middleware.regexp | ||
const middleware = middlewares[i] | ||
const fn = middleware.fn | ||
const regexp = middleware.regexp | ||
if (regexp) { | ||
var result = regexp.exec(url) | ||
const result = regexp.exec(url) | ||
if (result) { | ||
@@ -126,4 +126,5 @@ req.url = req.url.replace(result[0], '') | ||
function sanitizeUrl (url) { | ||
/* eslint-disable-next-line no-var */ | ||
for (var i = 0, len = url.length; i < len; i++) { | ||
var charCode = url.charCodeAt(i) | ||
const charCode = url.charCodeAt(i) | ||
if (charCode === 63 || charCode === 35) { | ||
@@ -130,0 +131,0 @@ return url.slice(0, i) |
@@ -10,6 +10,3 @@ 'use strict' | ||
function middiePlugin (fastify, options, next) { | ||
// TODO: we should use decorate, but `use` is already | ||
// a public API of Fastify. In Fastify v3 it will be deprecated, | ||
// so we will able to use `decorate` | ||
fastify.use = use | ||
fastify.decorate('use', use) | ||
fastify[kMiddlewares] = [] | ||
@@ -59,3 +56,3 @@ fastify[kMiddie] = Middie(onMiddieEnd) | ||
instance[kMiddie] = Middie(onMiddieEnd) | ||
instance.use = use | ||
instance.decorate('use', use) | ||
for (const middleware of middlewares) { | ||
@@ -62,0 +59,0 @@ instance.use(...middleware) |
{ | ||
"name": "middie", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Middleware engine for Fastify", | ||
@@ -8,4 +8,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "standard && tap --no-coverage test/*.test.js && tsd", | ||
"coverage": "tap --cov --coverage-report=html test.js" | ||
"coverage": "tap --cov --coverage-report=html test.js", | ||
"test": "standard && tap --no-coverage test/*.test.js && tsd" | ||
}, | ||
@@ -36,3 +36,3 @@ "keywords": [ | ||
"@types/connect": "^3.4.33", | ||
"@types/node": "^14.0.20", | ||
"@types/node": "^15.0.0", | ||
"cors": "^2.8.5", | ||
@@ -44,6 +44,6 @@ "fastify": "^3.0.0", | ||
"simple-get": "^4.0.0", | ||
"standard": "^14.3.1", | ||
"tap": "^14.10.5", | ||
"tsd": "^0.13.1", | ||
"typescript": "4.0.3" | ||
"standard": "^16.0.1", | ||
"tap": "^15.0.1", | ||
"tsd": "^0.17.0", | ||
"typescript": "4.3.4" | ||
}, | ||
@@ -50,0 +50,0 @@ "dependencies": { |
# middie | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![CI workflow](https://github.com/fastify/middie/workflows/CI%20workflow/badge.svg) | ||
![CI](https://github.com/fastify/middie/workflows/CI/badge.svg) | ||
[![NPM version](https://img.shields.io/npm/v/middie.svg?style=flat)](https://www.npmjs.com/package/middie) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/middie/badge.svg)](https://snyk.io/test/github/fastify/middie) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
*middie* is the plugin that add middlewares support on steroids to [Fastify](https://www.npmjs.com/package/fastify). | ||
*middie* is the plugin that adds middleware support on steroids to [Fastify](https://www.npmjs.com/package/fastify). | ||
@@ -17,3 +20,3 @@ The syntax style is the same as [express](http://npm.im/express)/[connect](https://www.npmjs.com/package/connect). | ||
## Usage | ||
Register the plugin and start using your middlewares. | ||
Register the plugin and start using your middleware. | ||
```js | ||
@@ -65,3 +68,3 @@ const Fastify = require('fastify') | ||
### Hooks and middlewares | ||
### Hooks and middleware | ||
@@ -198,5 +201,5 @@ Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important. | ||
## Middlewares alternatives | ||
## Middleware alternatives | ||
Fastify offers some alternatives to the most commonly used middlewares, following, you can find a list. | ||
Fastify offers some alternatives to the most commonly used Express middleware: | ||
@@ -212,6 +215,6 @@ | Express Middleware | Fastify Plugin | | ||
This project is kindly sponsored by: | ||
- [nearForm](http://nearform.com) | ||
- [nearForm](https://nearform.com) | ||
Past sponsors: | ||
- [LetzDoIt](http://www.letzdoitapp.com/) | ||
- [LetzDoIt](https://www.letzdoitapp.com/) | ||
@@ -218,0 +221,0 @@ ## License |
@@ -47,3 +47,3 @@ 'use strict' | ||
t.tearDown(fastify.close.bind(fastify)) | ||
t.teardown(fastify.close.bind(fastify)) | ||
@@ -60,3 +60,3 @@ fastify.listen(0, err => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
t.equal(response.statusCode, 404) | ||
}) | ||
@@ -115,3 +115,3 @@ }) | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
t.equal(response.statusCode, 404) | ||
}) | ||
@@ -176,4 +176,4 @@ }) | ||
t.error(err) | ||
t.strictEqual(body.toString(), 'this was not found') | ||
t.strictEqual(response.statusCode, 404) | ||
t.equal(body.toString(), 'this was not found') | ||
t.equal(response.statusCode, 404) | ||
}) | ||
@@ -250,3 +250,3 @@ }) | ||
t.tearDown(fastify.close.bind(fastify)) | ||
t.teardown(fastify.close.bind(fastify)) | ||
@@ -263,3 +263,3 @@ fastify.listen(0, err => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
t.equal(response.statusCode, 404) | ||
}) | ||
@@ -286,3 +286,3 @@ }) | ||
t.tearDown(fastify.close.bind(fastify)) | ||
t.teardown(fastify.close.bind(fastify)) | ||
@@ -299,3 +299,3 @@ fastify.listen(0, err => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
t.equal(response.statusCode, 404) | ||
}) | ||
@@ -331,3 +331,3 @@ }) | ||
t.tearDown(fastify.close.bind(fastify)) | ||
t.teardown(fastify.close.bind(fastify)) | ||
@@ -344,5 +344,5 @@ fastify.listen(0, err => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
t.equal(response.statusCode, 404) | ||
}) | ||
}) | ||
}) |
@@ -33,3 +33,3 @@ 'use strict' | ||
}) | ||
t.deepEqual(JSON.parse(data), { hello: 'world' }) | ||
t.same(JSON.parse(data), { hello: 'world' }) | ||
}) | ||
@@ -61,3 +61,3 @@ }) | ||
}) | ||
t.deepEqual(JSON.parse(data), { hello: 'world' }) | ||
t.same(JSON.parse(data), { hello: 'world' }) | ||
resolve() | ||
@@ -91,3 +91,3 @@ }) | ||
}) | ||
t.deepEqual(JSON.parse(data), { hello: 'world' }) | ||
t.same(JSON.parse(data), { hello: 'world' }) | ||
resolve() | ||
@@ -432,3 +432,3 @@ }) | ||
function middleware1 (req, res, next) { | ||
t.strictEqual(order.shift(), 1) | ||
t.equal(order.shift(), 1) | ||
next() | ||
@@ -438,3 +438,3 @@ } | ||
function middleware2 (req, res, next) { | ||
t.strictEqual(order.shift(), 2) | ||
t.equal(order.shift(), 2) | ||
next() | ||
@@ -444,3 +444,3 @@ } | ||
function middleware3 (req, res, next) { | ||
t.strictEqual(order.shift(), 3) | ||
t.equal(order.shift(), 3) | ||
next() | ||
@@ -475,3 +475,3 @@ } | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 500) | ||
t.equal(res.statusCode, 500) | ||
fastify.close() | ||
@@ -500,3 +500,3 @@ }) | ||
fastify.setErrorHandler((err, req, reply) => { | ||
t.is(err.message, 'middleware2') | ||
t.equal(err.message, 'middleware2') | ||
reply.send(err) | ||
@@ -525,3 +525,3 @@ }) | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 500) | ||
t.equal(res.statusCode, 500) | ||
fastify.close() | ||
@@ -591,3 +591,3 @@ }) | ||
t.error(err) | ||
t.deepEqual(data, { hello: 'world' }) | ||
t.same(data, { hello: 'world' }) | ||
fastify.close() | ||
@@ -633,6 +633,6 @@ }) | ||
t.error(err) | ||
t.strictEqual(res.headers['x-foo'], 'bar') | ||
t.deepEqual(JSON.parse(data), { hello: 'world' }) | ||
t.equal(res.headers['x-foo'], 'bar') | ||
t.same(JSON.parse(data), { hello: 'world' }) | ||
}) | ||
}) | ||
}) |
@@ -60,9 +60,9 @@ 'use strict' | ||
const res = {} | ||
var counter = 0 | ||
let counter = 0 | ||
instance.use(function (req, res, next) { | ||
t.is(counter++, 0, 'first function called') | ||
t.equal(counter++, 0, 'first function called') | ||
next() | ||
}).use(function (req, res, next) { | ||
t.is(counter++, 1, 'second function called') | ||
t.equal(counter++, 1, 'second function called') | ||
next() | ||
@@ -366,3 +366,3 @@ }) | ||
http.get(`http://localhost:${server.address().port}/README.md`, function (res) { | ||
t.is(res.statusCode, 200) | ||
t.equal(res.statusCode, 200) | ||
res.resume() | ||
@@ -385,7 +385,7 @@ server.close() | ||
const server = http.createServer(instance.run.bind(instance)) | ||
var req | ||
let req | ||
server.listen(0, function () { | ||
req = http.get(`http://localhost:${server.address().port}/assets/README.md`, function (res) { | ||
t.is(res.statusCode, 200) | ||
t.equal(res.statusCode, 200) | ||
res.resume() | ||
@@ -403,3 +403,3 @@ server.close() | ||
t.error(err) | ||
t.deepEqual(req, { | ||
t.same(req, { | ||
url: '/inner/in/depth', | ||
@@ -451,3 +451,3 @@ originalUrl: '/inner/in/depth', | ||
t.error(err) | ||
t.deepEqual(req, { | ||
t.same(req, { | ||
url: '/path', | ||
@@ -454,0 +454,0 @@ originalUrl: '/path' |
@@ -19,7 +19,7 @@ 'use strict' | ||
fastify.get('/', async (req, reply) => { | ||
t.strictEqual(req.raw.originalUrl, req.raw.url) | ||
t.strictEqual(req.raw.id, req.id) | ||
t.strictEqual(req.raw.hostname, req.hostname) | ||
t.strictEqual(req.raw.ip, req.ip) | ||
t.deepEqual(req.raw.ips, req.ips) | ||
t.equal(req.raw.originalUrl, req.raw.url) | ||
t.equal(req.raw.id, req.id) | ||
t.equal(req.raw.hostname, req.hostname) | ||
t.equal(req.raw.ip, req.ip) | ||
t.same(req.raw.ips, req.ips) | ||
t.ok(req.raw.log) | ||
@@ -49,7 +49,7 @@ t.ok(reply.raw.log) | ||
fastify.get('/', async (req, reply) => { | ||
t.strictEqual(req.raw.originalUrl, undefined) | ||
t.strictEqual(req.raw.id, undefined) | ||
t.strictEqual(req.raw.hostname, undefined) | ||
t.strictEqual(req.raw.ip, undefined) | ||
t.strictEqual(req.raw.ips, undefined) | ||
t.equal(req.raw.originalUrl, undefined) | ||
t.equal(req.raw.id, undefined) | ||
t.equal(req.raw.hostname, undefined) | ||
t.equal(req.raw.ip, undefined) | ||
t.equal(req.raw.ips, undefined) | ||
t.notOk(req.raw.log) | ||
@@ -56,0 +56,0 @@ t.notOk(reply.raw.log) |
@@ -21,4 +21,4 @@ 'use strict' | ||
fastify.addHook('onSend', function (request, reply, payload, next) { | ||
t.strictEqual(request.testDecorator, 'testDecoratorVal') | ||
t.strictEqual(reply.testDecorator, 'testDecoratorVal') | ||
t.equal(request.testDecorator, 'testDecoratorVal') | ||
t.equal(reply.testDecorator, 'testDecoratorVal') | ||
next() | ||
@@ -36,4 +36,4 @@ }) | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 500) | ||
t.equal(res.statusCode, 500) | ||
}) | ||
}) |
@@ -37,3 +37,3 @@ 'use strict' | ||
t.tearDown(instance.server.close.bind(instance.server)) | ||
t.teardown(instance.server.close.bind(instance.server)) | ||
@@ -45,5 +45,5 @@ sget({ | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(response.headers['content-length'], '' + body.length) | ||
t.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
t.equal(response.statusCode, 200) | ||
t.equal(response.headers['content-length'], '' + body.length) | ||
t.same(JSON.parse(body), { hello: 'world' }) | ||
}) | ||
@@ -69,3 +69,3 @@ }) | ||
t.tearDown(instance.server.close.bind(instance.server)) | ||
t.teardown(instance.server.close.bind(instance.server)) | ||
@@ -98,3 +98,3 @@ sget({ | ||
t.tearDown(instance.server.close.bind(instance.server)) | ||
t.teardown(instance.server.close.bind(instance.server)) | ||
@@ -128,3 +128,3 @@ sget({ | ||
t.tearDown(instance.server.close.bind(instance.server)) | ||
t.teardown(instance.server.close.bind(instance.server)) | ||
@@ -187,3 +187,3 @@ sget({ | ||
t.error(err) | ||
t.tearDown(instance.server.close.bind(instance.server)) | ||
t.teardown(instance.server.close.bind(instance.server)) | ||
@@ -198,3 +198,3 @@ t.test('/', t => { | ||
t.error(err) | ||
t.deepEqual(body, { | ||
t.same(body, { | ||
global: true, | ||
@@ -215,3 +215,3 @@ global2: true, | ||
t.error(err) | ||
t.deepEqual(body, { | ||
t.same(body, { | ||
prefixed: true, | ||
@@ -234,3 +234,3 @@ global: true, | ||
t.error(err) | ||
t.deepEqual(body, { | ||
t.same(body, { | ||
prefixed: true, | ||
@@ -253,3 +253,3 @@ slashed: true, | ||
t.error(err) | ||
t.deepEqual(body, { | ||
t.same(body, { | ||
prefixed: true, | ||
@@ -308,4 +308,4 @@ slashed: true, | ||
t.error(err) | ||
t.is(res.statusCode, 200) | ||
t.is(res.payload, 'hello') | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.payload, 'hello') | ||
}) | ||
@@ -359,3 +359,3 @@ }) | ||
t.error(err) | ||
t.is(res.statusCode, 200) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
@@ -393,4 +393,4 @@ }) | ||
t.error(err) | ||
t.is(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { hello: 'world' }) | ||
t.equal(res.statusCode, 200) | ||
t.same(JSON.parse(res.payload), { hello: 'world' }) | ||
}) | ||
@@ -406,3 +406,3 @@ }) | ||
instance.use(function (req, res, next) { | ||
t.strictEqual(req.previous, undefined) | ||
t.equal(req.previous, undefined) | ||
req.previous = 1 | ||
@@ -414,3 +414,3 @@ next() | ||
i.use(function (req, res, next) { | ||
t.strictEqual(req.previous, 2) | ||
t.equal(req.previous, 2) | ||
req.previous = 3 | ||
@@ -423,3 +423,3 @@ next() | ||
instance.use(function (req, res, next) { | ||
t.strictEqual(req.previous, 1) | ||
t.equal(req.previous, 1) | ||
req.previous = 2 | ||
@@ -434,3 +434,3 @@ next() | ||
instance.use(function (req, res, next) { | ||
t.strictEqual(req.previous, 3) | ||
t.equal(req.previous, 3) | ||
req.previous = 4 | ||
@@ -441,3 +441,3 @@ next() | ||
instance.get('/', function (request, reply) { | ||
t.strictEqual(request.raw.previous, 5) | ||
t.equal(request.raw.previous, 5) | ||
reply.send({ hello: 'world' }) | ||
@@ -448,3 +448,3 @@ }) | ||
i.use(function (req, res, next) { | ||
t.strictEqual(req.previous, 4) | ||
t.equal(req.previous, 4) | ||
req.previous = 5 | ||
@@ -461,5 +461,5 @@ next() | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { hello: 'world' }) | ||
t.equal(res.statusCode, 200) | ||
t.same(JSON.parse(res.payload), { hello: 'world' }) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58263
220
1864