Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stubborn-fetch

Package Overview
Dependencies
Maintainers
7
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stubborn-fetch - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

157

dist/index.es.js

@@ -0,7 +1,12 @@

var global =
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof self !== 'undefined' && self) ||
(typeof global !== 'undefined' && global);
var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
searchParams: 'URLSearchParams' in global,
iterable: 'Symbol' in global && 'iterator' in Symbol,
blob:
'FileReader' in self &&
'Blob' in self &&
'FileReader' in global &&
'Blob' in global &&
(function() {

@@ -15,4 +20,4 @@ try {

})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
formData: 'FormData' in global,
arrayBuffer: 'ArrayBuffer' in global
};

@@ -48,4 +53,4 @@

}
if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
throw new TypeError('Invalid character in header field name')
if (/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(name) || name === '') {
throw new TypeError('Invalid character in header field name: "' + name + '"')
}

@@ -214,2 +219,13 @@ return name.toLowerCase()

this._initBody = function(body) {
/*
fetch-mock wraps the Response object in an ES6 Proxy to
provide useful test harness features such as flush. However, on
ES5 browsers without fetch or Proxy support pollyfills must be used;
the proxy-pollyfill is unable to proxy an attribute unless it exists
on the object before the Proxy is created. This change ensures
Response.bodyUsed exists on the instance, while maintaining the
semantic of setting Request.bodyUsed in the constructor before
_initBody is called.
*/
this.bodyUsed = this.bodyUsed;
this._bodyInit = body;

@@ -267,3 +283,16 @@ if (!body) {

if (this._bodyArrayBuffer) {
return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
var isConsumed = consumed(this);
if (isConsumed) {
return isConsumed
}
if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
return Promise.resolve(
this._bodyArrayBuffer.buffer.slice(
this._bodyArrayBuffer.byteOffset,
this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength
)
)
} else {
return Promise.resolve(this._bodyArrayBuffer)
}
} else {

@@ -314,2 +343,6 @@ return this.blob().then(readBlobAsArrayBuffer)

function Request(input, options) {
if (!(this instanceof Request)) {
throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
}
options = options || {};

@@ -351,2 +384,17 @@ var body = options.body;

this._initBody(body);
if (this.method === 'GET' || this.method === 'HEAD') {
if (options.cache === 'no-store' || options.cache === 'no-cache') {
// Search for a '_' parameter in the query string
var reParamSearch = /([?&])_=[^&]*/;
if (reParamSearch.test(this.url)) {
// If it already exists then set the value with the current time
this.url = this.url.replace(reParamSearch, '$1_=' + new Date().getTime());
} else {
// Otherwise add a new '_' parameter to the end with the current time
var reQueryString = /\?/;
this.url += (reQueryString.test(this.url) ? '&' : '?') + '_=' + new Date().getTime();
}
}
}
}

@@ -379,10 +427,18 @@

var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
preProcessedHeaders.split(/\r?\n/).forEach(function(line) {
var parts = line.split(':');
var key = parts.shift().trim();
if (key) {
var value = parts.join(':').trim();
headers.append(key, value);
}
});
// Avoiding split via regex to work around a common IE11 bug with the core-js 3.6.0 regex polyfill
// https://github.com/github/fetch/issues/748
// https://github.com/zloirock/core-js/issues/751
preProcessedHeaders
.split('\r')
.map(function(header) {
return header.indexOf('\n') === 0 ? header.substr(1, header.length) : header
})
.forEach(function(line) {
var parts = line.split(':');
var key = parts.shift().trim();
if (key) {
var value = parts.join(':').trim();
headers.append(key, value);
}
});
return headers

@@ -394,2 +450,5 @@ }

function Response(bodyInit, options) {
if (!(this instanceof Response)) {
throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
}
if (!options) {

@@ -402,3 +461,3 @@ options = {};

this.ok = this.status >= 200 && this.status < 300;
this.statusText = 'statusText' in options ? options.statusText : 'OK';
this.statusText = options.statusText === undefined ? '' : '' + options.statusText;
this.headers = new Headers(options.headers);

@@ -436,3 +495,3 @@ this.url = options.url || '';

var DOMException = self.DOMException;
var DOMException = global.DOMException;
try {

@@ -473,19 +532,35 @@ new DOMException();

var body = 'response' in xhr ? xhr.response : xhr.responseText;
resolve(new Response(body, options));
setTimeout(function() {
resolve(new Response(body, options));
}, 0);
};
xhr.onerror = function() {
reject(new TypeError('Network request failed'));
setTimeout(function() {
reject(new TypeError('Network request failed'));
}, 0);
};
xhr.ontimeout = function() {
reject(new TypeError('Network request failed'));
setTimeout(function() {
reject(new TypeError('Network request failed'));
}, 0);
};
xhr.onabort = function() {
reject(new DOMException('Aborted', 'AbortError'));
setTimeout(function() {
reject(new DOMException('Aborted', 'AbortError'));
}, 0);
};
xhr.open(request.method, request.url, true);
function fixUrl(url) {
try {
return url === '' && global.location.href ? global.location.href : url
} catch (e) {
return url
}
}
xhr.open(request.method, fixUrl(request.url), true);
if (request.credentials === 'include') {

@@ -497,9 +572,23 @@ xhr.withCredentials = true;

if ('responseType' in xhr && support.blob) {
xhr.responseType = 'blob';
if ('responseType' in xhr) {
if (support.blob) {
xhr.responseType = 'blob';
} else if (
support.arrayBuffer &&
request.headers.get('Content-Type') &&
request.headers.get('Content-Type').indexOf('application/octet-stream') !== -1
) {
xhr.responseType = 'arraybuffer';
}
}
request.headers.forEach(function(value, name) {
xhr.setRequestHeader(name, value);
});
if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers)) {
Object.getOwnPropertyNames(init.headers).forEach(function(name) {
xhr.setRequestHeader(name, normalizeValue(init.headers[name]));
});
} else {
request.headers.forEach(function(value, name) {
xhr.setRequestHeader(name, value);
});
}

@@ -523,7 +612,7 @@ if (request.signal) {

if (!self.fetch) {
self.fetch = fetch$1;
self.Headers = Headers;
self.Request = Request;
self.Response = Response;
if (!global.fetch) {
global.fetch = fetch$1;
global.Headers = Headers;
global.Request = Request;
global.Response = Response;
}

@@ -530,0 +619,0 @@

@@ -5,8 +5,13 @@ 'use strict';

var global =
(typeof globalThis !== 'undefined' && globalThis) ||
(typeof self !== 'undefined' && self) ||
(typeof global !== 'undefined' && global);
var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
searchParams: 'URLSearchParams' in global,
iterable: 'Symbol' in global && 'iterator' in Symbol,
blob:
'FileReader' in self &&
'Blob' in self &&
'FileReader' in global &&
'Blob' in global &&
(function() {

@@ -20,4 +25,4 @@ try {

})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
formData: 'FormData' in global,
arrayBuffer: 'ArrayBuffer' in global
};

@@ -53,4 +58,4 @@

}
if (/[^a-z0-9\-#$%&'*+.^_`|~]/i.test(name)) {
throw new TypeError('Invalid character in header field name')
if (/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(name) || name === '') {
throw new TypeError('Invalid character in header field name: "' + name + '"')
}

@@ -219,2 +224,13 @@ return name.toLowerCase()

this._initBody = function(body) {
/*
fetch-mock wraps the Response object in an ES6 Proxy to
provide useful test harness features such as flush. However, on
ES5 browsers without fetch or Proxy support pollyfills must be used;
the proxy-pollyfill is unable to proxy an attribute unless it exists
on the object before the Proxy is created. This change ensures
Response.bodyUsed exists on the instance, while maintaining the
semantic of setting Request.bodyUsed in the constructor before
_initBody is called.
*/
this.bodyUsed = this.bodyUsed;
this._bodyInit = body;

@@ -272,3 +288,16 @@ if (!body) {

if (this._bodyArrayBuffer) {
return consumed(this) || Promise.resolve(this._bodyArrayBuffer)
var isConsumed = consumed(this);
if (isConsumed) {
return isConsumed
}
if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
return Promise.resolve(
this._bodyArrayBuffer.buffer.slice(
this._bodyArrayBuffer.byteOffset,
this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength
)
)
} else {
return Promise.resolve(this._bodyArrayBuffer)
}
} else {

@@ -319,2 +348,6 @@ return this.blob().then(readBlobAsArrayBuffer)

function Request(input, options) {
if (!(this instanceof Request)) {
throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
}
options = options || {};

@@ -356,2 +389,17 @@ var body = options.body;

this._initBody(body);
if (this.method === 'GET' || this.method === 'HEAD') {
if (options.cache === 'no-store' || options.cache === 'no-cache') {
// Search for a '_' parameter in the query string
var reParamSearch = /([?&])_=[^&]*/;
if (reParamSearch.test(this.url)) {
// If it already exists then set the value with the current time
this.url = this.url.replace(reParamSearch, '$1_=' + new Date().getTime());
} else {
// Otherwise add a new '_' parameter to the end with the current time
var reQueryString = /\?/;
this.url += (reQueryString.test(this.url) ? '&' : '?') + '_=' + new Date().getTime();
}
}
}
}

@@ -384,10 +432,18 @@

var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
preProcessedHeaders.split(/\r?\n/).forEach(function(line) {
var parts = line.split(':');
var key = parts.shift().trim();
if (key) {
var value = parts.join(':').trim();
headers.append(key, value);
}
});
// Avoiding split via regex to work around a common IE11 bug with the core-js 3.6.0 regex polyfill
// https://github.com/github/fetch/issues/748
// https://github.com/zloirock/core-js/issues/751
preProcessedHeaders
.split('\r')
.map(function(header) {
return header.indexOf('\n') === 0 ? header.substr(1, header.length) : header
})
.forEach(function(line) {
var parts = line.split(':');
var key = parts.shift().trim();
if (key) {
var value = parts.join(':').trim();
headers.append(key, value);
}
});
return headers

@@ -399,2 +455,5 @@ }

function Response(bodyInit, options) {
if (!(this instanceof Response)) {
throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
}
if (!options) {

@@ -407,3 +466,3 @@ options = {};

this.ok = this.status >= 200 && this.status < 300;
this.statusText = 'statusText' in options ? options.statusText : 'OK';
this.statusText = options.statusText === undefined ? '' : '' + options.statusText;
this.headers = new Headers(options.headers);

@@ -441,3 +500,3 @@ this.url = options.url || '';

var DOMException = self.DOMException;
var DOMException = global.DOMException;
try {

@@ -478,19 +537,35 @@ new DOMException();

var body = 'response' in xhr ? xhr.response : xhr.responseText;
resolve(new Response(body, options));
setTimeout(function() {
resolve(new Response(body, options));
}, 0);
};
xhr.onerror = function() {
reject(new TypeError('Network request failed'));
setTimeout(function() {
reject(new TypeError('Network request failed'));
}, 0);
};
xhr.ontimeout = function() {
reject(new TypeError('Network request failed'));
setTimeout(function() {
reject(new TypeError('Network request failed'));
}, 0);
};
xhr.onabort = function() {
reject(new DOMException('Aborted', 'AbortError'));
setTimeout(function() {
reject(new DOMException('Aborted', 'AbortError'));
}, 0);
};
xhr.open(request.method, request.url, true);
function fixUrl(url) {
try {
return url === '' && global.location.href ? global.location.href : url
} catch (e) {
return url
}
}
xhr.open(request.method, fixUrl(request.url), true);
if (request.credentials === 'include') {

@@ -502,9 +577,23 @@ xhr.withCredentials = true;

if ('responseType' in xhr && support.blob) {
xhr.responseType = 'blob';
if ('responseType' in xhr) {
if (support.blob) {
xhr.responseType = 'blob';
} else if (
support.arrayBuffer &&
request.headers.get('Content-Type') &&
request.headers.get('Content-Type').indexOf('application/octet-stream') !== -1
) {
xhr.responseType = 'arraybuffer';
}
}
request.headers.forEach(function(value, name) {
xhr.setRequestHeader(name, value);
});
if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers)) {
Object.getOwnPropertyNames(init.headers).forEach(function(name) {
xhr.setRequestHeader(name, normalizeValue(init.headers[name]));
});
} else {
request.headers.forEach(function(value, name) {
xhr.setRequestHeader(name, value);
});
}

@@ -528,7 +617,7 @@ if (request.signal) {

if (!self.fetch) {
self.fetch = fetch$1;
self.Headers = Headers;
self.Request = Request;
self.Response = Response;
if (!global.fetch) {
global.fetch = fetch$1;
global.Headers = Headers;
global.Request = Request;
global.Response = Response;
}

@@ -535,0 +624,0 @@

{
"name": "stubborn-fetch",
"version": "0.2.6",
"version": "0.2.7",
"description": "Fetch wrapper with built in retry",

@@ -54,3 +54,3 @@ "main": "dist/index.js",

"extendable-error-class": "0.1.1",
"isomorphic-fetch": "2.2.1"
"isomorphic-fetch": "3.0.0"
},

@@ -75,3 +75,3 @@ "devDependencies": {

"husky": "^0.14.3",
"jest": "^22.4.0",
"jest": "^26.6.3",
"lint-staged": "^6.0.0",

@@ -78,0 +78,0 @@ "lodash": "^4.17.5",

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