New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@bbc/http-transport

Package Overview
Dependencies
Maintainers
0
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bbc/http-transport - npm Package Compare versions

Comparing version 4.5.1 to 4.6.0

3

index.d.ts

@@ -103,2 +103,3 @@ import * as fetch from 'node-fetch';

retryDelay(retryDelay: number): HttpTransportBuilder<ContextCurrent>;
criticalErrorDetector(criticalErrorDetector: (err, ctx: Context) => boolean);
use<ContextExtra = {}>(

@@ -145,3 +146,3 @@ fn: Plugin<ContextExtra, ContextCurrent>

asResponse<ResponseBody = ContextCurrent["res"]["body"]>(): Promise<
AsResponse<ContextCurrent["res"], ResponseBody>
AsResponse<ContextCurrent["res"], ResponseBody>
>;

@@ -148,0 +149,0 @@ }

@@ -73,2 +73,32 @@ 'use strict';

/**
* default the criticalErrorDetector which parses errors and decides if they are critical. This mainly enables retry logic.
* @param {function} criticalErrorDetector (err, ctx) => bool - a function that takes the error and the context and returns a boolean which evaluates whether an error is a critical error.
* This is useful if you want to customise error behaviour. See below for example that prevents circuitBreaker errors from being classed as critical errors.
* criticalErrors trigger retry behaviour and so may not be desirable in all scenarios.
* @return a HttpTransportBuilder instance
* @example
* const httpTransport = require('@bbc/http-transport');
*
* const builder = httpTransport.createBuilder();
* builder.criticalErrorDetector(() => {
* if (err && (err.statusCode < 500 || err.isBrokenCircuitError)) {
* return false;
* }
* return true;
* });
*
* @default
* (err, ctx) => {
* if (err && err.statusCode < 500) {
* return false;
* }
* return true;
* }
*/
criticalErrorDetector(criticalErrorDetector) {
_.set(this._defaults, 'ctx.criticalErrorDetector', criticalErrorDetector);
return this;
}
/**
* Registers a global plugin, which is used for all requests

@@ -75,0 +105,0 @@ *

@@ -342,3 +342,5 @@ 'use strict';

function isCriticalError(err) {
function isCriticalError(err, ctx) {
if (ctx.criticalErrorDetector) return ctx.criticalErrorDetector(err, ctx);
if (err && err.statusCode < 500) {

@@ -367,3 +369,3 @@ return false;

.catch((err) => {
if (i < maxAttempts && isCriticalError(err) && ctx.retryDelay && ctx.retryDelay > 0) {
if (i < maxAttempts && isCriticalError(err, ctx) && ctx.retryDelay && ctx.retryDelay > 0) {
const delayBy = rejectedPromise(ctx.retryDelay);

@@ -375,3 +377,3 @@ return delayBy(err);

.catch((err) => {
if (i < maxAttempts && isCriticalError(err)) {
if (i < maxAttempts && isCriticalError(err, ctx)) {
ctx.retryAttempts.push(toRetry(err));

@@ -378,0 +380,0 @@ return attempt(++i);

@@ -22,2 +22,3 @@ 'use strict';

this.res = Response.create();
this.criticalErrorDetector = undefined;
if (defaults) this._applyDefaults(defaults);

@@ -46,2 +47,3 @@ }

this.retryDelay = defaults.ctx?.retryDelay || RETRY_DELAY;
this.criticalErrorDetector = defaults.ctx?.criticalErrorDetector;
}

@@ -48,0 +50,0 @@

{
"name": "@bbc/http-transport",
"version": "4.5.1",
"version": "4.6.0",
"description": "A flexible, modular REST client built for ease-of-use and resilience.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -144,2 +144,18 @@ 'use strict';

});
it('sets default criticalErrorDetector in the context', async () => {
const transport = new Transport();
sandbox.stub(transport, 'execute').returns(Promise.resolve());
const client = HttpTransport.createBuilder(transport)
.criticalErrorDetector(() => false)
.createClient();
await client
.get(url)
.asResponse();
const ctx = transport.execute.getCall(0).args[0];
assert.equal(ctx.criticalErrorDetector.toString(), (() => false).toString());
});
});

@@ -146,0 +162,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc