@fastify/circuit-breaker
Advanced tools
Comparing version 3.2.0 to 4.0.0-pre.fv5.1
'use strict' | ||
const fp = require('fastify-plugin') | ||
const lru = require('tiny-lru').lru | ||
const { FifoObject } = require('toad-cache') | ||
const createError = require('@fastify/error') | ||
@@ -20,3 +20,3 @@ | ||
const onTimeout = opts.onTimeout | ||
const cache = lru(opts.cache || 500) | ||
const cache = new FifoObject(opts.cache || 500) | ||
@@ -23,0 +23,0 @@ let routeId = 0 |
{ | ||
"name": "@fastify/circuit-breaker", | ||
"version": "3.2.0", | ||
"version": "4.0.0-pre.fv5.1", | ||
"description": "A low overhead circuit breaker for your routes", | ||
@@ -29,13 +29,13 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@types/node": "^18.0.0", | ||
"fastify": "^4.0.0-rc.2", | ||
"standard": "^17.0.0", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.25.0" | ||
"@fastify/pre-commit": "^2.1.0", | ||
"@types/node": "^20.11.30", | ||
"fastify": "^5.0.0-alpha.2", | ||
"standard": "^17.1.0", | ||
"tap": "^20.0.1", | ||
"tsd": "^0.31.1" | ||
}, | ||
"dependencies": { | ||
"@fastify/error": "^3.0.0", | ||
"fastify-plugin": "^4.0.0", | ||
"tiny-lru": "^10.0.0" | ||
"@fastify/error": "^4.0.0", | ||
"fastify-plugin": "^5.0.0-pre.fv5.1", | ||
"toad-cache": "^3.7.0" | ||
}, | ||
@@ -42,0 +42,0 @@ "bugs": { |
@@ -713,1 +713,107 @@ 'use strict' | ||
}) | ||
test('Should not throw error if no options is passed', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
const fastify2 = Fastify() | ||
const fastify3 = Fastify() | ||
t.equal(circuitBreaker(fastify, undefined, () => {}), undefined) | ||
t.equal(circuitBreaker(fastify2, null, () => {}), undefined) | ||
t.equal(circuitBreaker(fastify3, {}, () => {}), undefined) | ||
}) | ||
test('Should throw error on route status open and circuit open', t => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
fastify.register(circuitBreaker, { | ||
threshold: 1, | ||
timeout: 1000, | ||
resetTimeout: 1500, | ||
onCircuitOpen: async (req, reply) => { | ||
reply.statusCode = 500 | ||
return JSON.stringify({ err: 'custom error' }) | ||
} | ||
}) | ||
fastify.after(() => { | ||
fastify.get('/', { preHandler: fastify.circuitBreaker() }, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
reply.send(new Error('kaboom')) | ||
}, 0) | ||
}) | ||
}) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
}) | ||
setTimeout(() => { | ||
fastify.inject('/?error=false', (err, res) => { | ||
t.equal(null, err) | ||
t.equal(res.statusCode, 500) | ||
t.same(res.json(), { err: 'custom error' }) | ||
}) | ||
}, 1000) | ||
}) | ||
test('Should throw error on route status half open and circuit open', t => { | ||
t.plan(15) | ||
const fastify = Fastify() | ||
fastify.register(circuitBreaker, { | ||
threshold: 2, | ||
timeout: 1000, | ||
resetTimeout: 500, | ||
onCircuitOpen: async (req, reply) => { | ||
reply.statusCode = 500 | ||
return JSON.stringify({ err: 'custom error' }) | ||
} | ||
}) | ||
fastify.after(() => { | ||
opts.preHandler = fastify.circuitBreaker() | ||
fastify.get('/', opts, (req, reply) => { | ||
t.equal(typeof req._cbTime, 'number') | ||
setTimeout(() => { | ||
reply.send(new Error('kaboom')) | ||
}, 0) | ||
}) | ||
}) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same(res.json(), { err: 'custom error' }) | ||
}) | ||
setTimeout(() => { | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 500) | ||
t.same({ | ||
error: 'Internal Server Error', | ||
message: 'kaboom', | ||
statusCode: 500 | ||
}, JSON.parse(res.payload)) | ||
}) | ||
fastify.inject('/?error=true', (err, res) => { | ||
t.equal(null, err) | ||
t.equal(res.statusCode, 500) | ||
t.same(res.json(), { err: 'custom error' }) | ||
}) | ||
}, 1000) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
37658
13
1031
0
1
+ Addedtoad-cache@^3.7.0
+ Added@fastify/error@4.0.0(transitive)
+ Addedfastify-plugin@5.0.1(transitive)
+ Addedtoad-cache@3.7.0(transitive)
- Removedtiny-lru@^10.0.0
- Removed@fastify/error@3.4.1(transitive)
- Removedfastify-plugin@4.5.1(transitive)
- Removedtiny-lru@10.4.1(transitive)
Updated@fastify/error@^4.0.0