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

node-fetch

Package Overview
Dependencies
Maintainers
2
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-fetch - npm Package Compare versions

Comparing version 2.0.0-alpha.1 to 2.0.0-alpha.3

10

CHANGELOG.md

@@ -8,2 +8,12 @@

## v2.0.0-alpha.3
- Major: overwrite user's `Content-Length` if we can be sure our information is correct
- Fix: exhaust list in `Headers` constructor before processing
## v2.0.0-alpha.2
- Major: remove `headers.getAll()`; make `get()` return all headers delimited by commas (per spec)
- Major: remove undocumented `FOLLOW_SPEC` switch -- it is now the default
## v2.0.0-alpha.1

@@ -10,0 +20,0 @@

243

lib/index.es.js

@@ -26,3 +26,2 @@ import _getIterator from 'babel-runtime/core-js/get-iterator';

import _Symbol$iterator from 'babel-runtime/core-js/symbol/iterator';
import _typeof from 'babel-runtime/helpers/typeof';

@@ -699,3 +698,2 @@ // Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js

var MAP = _Symbol('map');
var FOLLOW_SPEC = _Symbol('followSpec');

@@ -709,26 +707,44 @@ var Headers = function () {

*/
function Headers(headers) {
function Headers() {
var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
_classCallCheck(this, Headers);
this[MAP] = _Object$create(null);
this[FOLLOW_SPEC] = Headers.FOLLOW_SPEC;
// Headers
if (headers instanceof Headers) {
var init = headers.raw();
for (var _iterator = _Object$keys(init), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
var _ref;
// We don't worry about converting prop to ByteString here as append()
// will handle it.
if (init == null) {
// no op
} else if (typeof init === 'object') {
var method = init[_Symbol$iterator];
if (method != null) {
if (typeof method !== 'function') {
throw new TypeError('Header pairs must be iterable');
}
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
// sequence<sequence<ByteString>>
// Note: per spec we have to first exhaust the lists then process them
var pairs = [];
for (var _iterator = init, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var pair = _ref;
if (typeof pair !== 'object' || typeof pair[_Symbol$iterator] !== 'function') {
throw new TypeError('Each header pair must be iterable');
}
pairs.push(_Array$from(pair));
}
var name = _ref;
for (var _iterator2 = init[name], _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _getIterator(_iterator2);;) {
for (var _iterator2 = pairs, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _getIterator(_iterator2);;) {
var _ref2;

@@ -745,53 +761,30 @@

var value = _ref2;
var _pair = _ref2;
this.append(name, value);
if (_pair.length !== 2) {
throw new TypeError('Each header pair must be a name/value tuple');
}
this.append(_pair[0], _pair[1]);
}
}
} else if ((typeof headers === 'undefined' ? 'undefined' : _typeof(headers)) === 'object' && headers[_Symbol$iterator]) {
// array of tuples
for (var _iterator3 = headers, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _getIterator(_iterator3);;) {
var _ref3;
} else {
// record<ByteString, ByteString>
for (var _iterator3 = _Object$keys(init), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _getIterator(_iterator3);;) {
var _ref3;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
var el = _ref3;
var key = _ref3;
if ((typeof el === 'undefined' ? 'undefined' : _typeof(el)) !== 'object' || !el[_Symbol$iterator]) {
throw new TypeError('Header pairs must be an iterable object');
var value = init[key];
this.append(key, value);
}
el = _Array$from(el);
if (el.length !== 2) {
throw new TypeError('Header pairs must contain exactly two items');
}
this.append(el[0], el[1]);
}
} else if ((typeof headers === 'undefined' ? 'undefined' : _typeof(headers)) === 'object') {
// plain object
for (var _iterator4 = _Object$keys(headers), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _getIterator(_iterator4);;) {
var _ref4;
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
_ref4 = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
_ref4 = _i4.value;
}
var prop = _ref4;
// We don't worry about converting prop to ByteString here as append()
// will handle it.
this.append(prop, headers[prop]);
}
} else if (headers != null) {
} else {
throw new TypeError('Provided initializer must be an object');

@@ -822,22 +815,6 @@ }

return this[FOLLOW_SPEC] ? list.join(',') : list[0];
return list.join(',');
};
/**
* Return all header values given name
*
* @param String name Header name
* @return Array
*/
Headers.prototype.getAll = function getAll(name) {
if (!this.has(name)) {
return [];
}
return this[MAP][sanitizeName(name)];
};
/**
* Iterate over all headers

@@ -979,33 +956,8 @@ *

function getHeaderPairs(headers, kind) {
if (headers[FOLLOW_SPEC]) {
var keys = _Object$keys(headers[MAP]).sort();
return keys.map(kind === 'key' ? function (k) {
return [k];
} : function (k) {
return [k, headers.get(k)];
});
}
var values = [];
for (var name in headers[MAP]) {
for (var _iterator5 = headers[MAP][name], _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _getIterator(_iterator5);;) {
var _ref5;
if (_isArray5) {
if (_i5 >= _iterator5.length) break;
_ref5 = _iterator5[_i5++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
_ref5 = _i5.value;
}
var value = _ref5;
values.push([name, value]);
}
}
return values;
var keys = _Object$keys(headers[MAP]).sort();
return keys.map(kind === 'key' ? function (k) {
return [k];
} : function (k) {
return [k, headers.get(k)];
});
}

@@ -1079,4 +1031,2 @@

Headers.FOLLOW_SPEC = false;
/**

@@ -1215,3 +1165,3 @@ * response.js

_this.method = method;
_this.method = method.toUpperCase();
_this.redirect = init.redirect || input.redirect || 'follow';

@@ -1234,3 +1184,2 @@ _this.headers = new Headers(init.headers || input.headers || {});

_this[PARSED_URL] = parsedURL;
_Object$defineProperty(_this, _Symbol$toStringTag, {

@@ -1271,35 +1220,49 @@ value: 'Request',

function normalizeHeaders(request) {
function getNodeRequestOptions(request) {
var headers = new Headers(request.headers);
if (request.compress) {
headers.set('accept-encoding', 'gzip,deflate');
// fetch step 3
if (!headers.has('Accept')) {
headers.set('Accept', '*/*');
}
if (!headers.has('user-agent')) {
headers.set('user-agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
// Basic fetch
if (!/^https?:$/.test(request[PARSED_URL].protocol)) {
throw new Error('only http(s) protocols are supported');
}
if (!headers.has('connection') && !request.agent) {
headers.set('connection', 'close');
// HTTP-network-or-cache fetch steps 5-9
var contentLengthValue = null;
if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
contentLengthValue = '0';
}
if (!headers.has('accept')) {
headers.set('accept', '*/*');
}
if (!headers.has('content-length') && /post|put|patch|delete/i.test(request.method)) {
if (request.body != null) {
var totalBytes = getTotalBytes(request);
if (typeof totalBytes === 'number') {
headers.set('content-length', totalBytes);
contentLengthValue = String(totalBytes);
}
}
if (contentLengthValue) {
headers.set('Content-Length', contentLengthValue);
}
return headers;
}
// HTTP-network-or-cache fetch step 12
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
}
function getNodeRequestOptions(request) {
// HTTP-network-or-cache fetch step 16
if (request.compress) {
headers.set('Accept-Encoding', 'gzip,deflate');
}
if (!headers.has('Connection') && !request.agent) {
headers.set('Connection', 'close');
}
// HTTP-network fetch step 4
// chunked encoding is handled by Node.js
return _Object$assign({}, request[PARSED_URL], {
method: request.method,
headers: normalizeHeaders(request).raw(),
headers: headers.raw(),
agent: request.agent

@@ -1330,3 +1293,2 @@ });

Body.Promise = fetch.Promise;
Headers.FOLLOW_SPEC = fetch.FOLLOW_SPEC;

@@ -1344,6 +1306,2 @@ // wrap http.request into fetch

if (options.protocol !== 'http:' && options.protocol !== 'https:') {
throw new Error('only http(s) protocols are supported');
}
var send = (options.protocol === 'https:' ? https : http).request;

@@ -1524,12 +1482,3 @@

fetch.Promise = global.Promise;
/**
* Option to make newly constructed Headers objects conformant to the
* **latest** version of the Fetch Standard. Note, that most other
* implementations of fetch() have not yet been updated to the latest
* version, so enabling this option almost certainly breaks any isomorphic
* attempt. Also, changing this variable will only affect new Headers
* objects; existing objects are not affected.
*/
fetch.FOLLOW_SPEC = false;
export { Headers, Request, Response };export default fetch;

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

var _Symbol$iterator = _interopDefault(require('babel-runtime/core-js/symbol/iterator'));
var _typeof = _interopDefault(require('babel-runtime/helpers/typeof'));

@@ -703,3 +702,2 @@ // Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js

var MAP = _Symbol('map');
var FOLLOW_SPEC = _Symbol('followSpec');

@@ -713,26 +711,44 @@ var Headers = function () {

*/
function Headers(headers) {
function Headers() {
var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
_classCallCheck(this, Headers);
this[MAP] = _Object$create(null);
this[FOLLOW_SPEC] = Headers.FOLLOW_SPEC;
// Headers
if (headers instanceof Headers) {
var init = headers.raw();
for (var _iterator = _Object$keys(init), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
var _ref;
// We don't worry about converting prop to ByteString here as append()
// will handle it.
if (init == null) {
// no op
} else if (typeof init === 'object') {
var method = init[_Symbol$iterator];
if (method != null) {
if (typeof method !== 'function') {
throw new TypeError('Header pairs must be iterable');
}
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
// sequence<sequence<ByteString>>
// Note: per spec we have to first exhaust the lists then process them
var pairs = [];
for (var _iterator = init, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var pair = _ref;
if (typeof pair !== 'object' || typeof pair[_Symbol$iterator] !== 'function') {
throw new TypeError('Each header pair must be iterable');
}
pairs.push(_Array$from(pair));
}
var name = _ref;
for (var _iterator2 = init[name], _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _getIterator(_iterator2);;) {
for (var _iterator2 = pairs, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _getIterator(_iterator2);;) {
var _ref2;

@@ -749,53 +765,30 @@

var value = _ref2;
var _pair = _ref2;
this.append(name, value);
if (_pair.length !== 2) {
throw new TypeError('Each header pair must be a name/value tuple');
}
this.append(_pair[0], _pair[1]);
}
}
} else if ((typeof headers === 'undefined' ? 'undefined' : _typeof(headers)) === 'object' && headers[_Symbol$iterator]) {
// array of tuples
for (var _iterator3 = headers, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _getIterator(_iterator3);;) {
var _ref3;
} else {
// record<ByteString, ByteString>
for (var _iterator3 = _Object$keys(init), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _getIterator(_iterator3);;) {
var _ref3;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
var el = _ref3;
var key = _ref3;
if ((typeof el === 'undefined' ? 'undefined' : _typeof(el)) !== 'object' || !el[_Symbol$iterator]) {
throw new TypeError('Header pairs must be an iterable object');
var value = init[key];
this.append(key, value);
}
el = _Array$from(el);
if (el.length !== 2) {
throw new TypeError('Header pairs must contain exactly two items');
}
this.append(el[0], el[1]);
}
} else if ((typeof headers === 'undefined' ? 'undefined' : _typeof(headers)) === 'object') {
// plain object
for (var _iterator4 = _Object$keys(headers), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _getIterator(_iterator4);;) {
var _ref4;
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
_ref4 = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
_ref4 = _i4.value;
}
var prop = _ref4;
// We don't worry about converting prop to ByteString here as append()
// will handle it.
this.append(prop, headers[prop]);
}
} else if (headers != null) {
} else {
throw new TypeError('Provided initializer must be an object');

@@ -826,22 +819,6 @@ }

return this[FOLLOW_SPEC] ? list.join(',') : list[0];
return list.join(',');
};
/**
* Return all header values given name
*
* @param String name Header name
* @return Array
*/
Headers.prototype.getAll = function getAll(name) {
if (!this.has(name)) {
return [];
}
return this[MAP][sanitizeName(name)];
};
/**
* Iterate over all headers

@@ -983,33 +960,8 @@ *

function getHeaderPairs(headers, kind) {
if (headers[FOLLOW_SPEC]) {
var keys = _Object$keys(headers[MAP]).sort();
return keys.map(kind === 'key' ? function (k) {
return [k];
} : function (k) {
return [k, headers.get(k)];
});
}
var values = [];
for (var name in headers[MAP]) {
for (var _iterator5 = headers[MAP][name], _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _getIterator(_iterator5);;) {
var _ref5;
if (_isArray5) {
if (_i5 >= _iterator5.length) break;
_ref5 = _iterator5[_i5++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
_ref5 = _i5.value;
}
var value = _ref5;
values.push([name, value]);
}
}
return values;
var keys = _Object$keys(headers[MAP]).sort();
return keys.map(kind === 'key' ? function (k) {
return [k];
} : function (k) {
return [k, headers.get(k)];
});
}

@@ -1083,4 +1035,2 @@

Headers.FOLLOW_SPEC = false;
/**

@@ -1219,3 +1169,3 @@ * response.js

_this.method = method;
_this.method = method.toUpperCase();
_this.redirect = init.redirect || input.redirect || 'follow';

@@ -1238,3 +1188,2 @@ _this.headers = new Headers(init.headers || input.headers || {});

_this[PARSED_URL] = parsedURL;
_Object$defineProperty(_this, _Symbol$toStringTag, {

@@ -1275,35 +1224,49 @@ value: 'Request',

function normalizeHeaders(request) {
function getNodeRequestOptions(request) {
var headers = new Headers(request.headers);
if (request.compress) {
headers.set('accept-encoding', 'gzip,deflate');
// fetch step 3
if (!headers.has('Accept')) {
headers.set('Accept', '*/*');
}
if (!headers.has('user-agent')) {
headers.set('user-agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
// Basic fetch
if (!/^https?:$/.test(request[PARSED_URL].protocol)) {
throw new Error('only http(s) protocols are supported');
}
if (!headers.has('connection') && !request.agent) {
headers.set('connection', 'close');
// HTTP-network-or-cache fetch steps 5-9
var contentLengthValue = null;
if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
contentLengthValue = '0';
}
if (!headers.has('accept')) {
headers.set('accept', '*/*');
}
if (!headers.has('content-length') && /post|put|patch|delete/i.test(request.method)) {
if (request.body != null) {
var totalBytes = getTotalBytes(request);
if (typeof totalBytes === 'number') {
headers.set('content-length', totalBytes);
contentLengthValue = String(totalBytes);
}
}
if (contentLengthValue) {
headers.set('Content-Length', contentLengthValue);
}
return headers;
}
// HTTP-network-or-cache fetch step 12
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
}
function getNodeRequestOptions(request) {
// HTTP-network-or-cache fetch step 16
if (request.compress) {
headers.set('Accept-Encoding', 'gzip,deflate');
}
if (!headers.has('Connection') && !request.agent) {
headers.set('Connection', 'close');
}
// HTTP-network fetch step 4
// chunked encoding is handled by Node.js
return _Object$assign({}, request[PARSED_URL], {
method: request.method,
headers: normalizeHeaders(request).raw(),
headers: headers.raw(),
agent: request.agent

@@ -1334,3 +1297,2 @@ });

Body.Promise = fetch.Promise;
Headers.FOLLOW_SPEC = fetch.FOLLOW_SPEC;

@@ -1348,6 +1310,2 @@ // wrap http.request into fetch

if (options.protocol !== 'http:' && options.protocol !== 'https:') {
throw new Error('only http(s) protocols are supported');
}
var send = (options.protocol === 'https:' ? https : http).request;

@@ -1528,11 +1486,2 @@

fetch.Promise = global.Promise;
/**
* Option to make newly constructed Headers objects conformant to the
* **latest** version of the Fetch Standard. Note, that most other
* implementations of fetch() have not yet been updated to the latest
* version, so enabling this option almost certainly breaks any isomorphic
* attempt. Also, changing this variable will only affect new Headers
* objects; existing objects are not affected.
*/
fetch.FOLLOW_SPEC = false;

@@ -1539,0 +1488,0 @@ module.exports = exports = fetch;

{
"name": "node-fetch",
"version": "2.0.0-alpha.1",
"version": "2.0.0-alpha.3",
"description": "A light-weight module that brings window.fetch to node.js",
"main": "lib/index.js",
"jsnext:main": "lib/index.es.js",
"module": "lib/index.es.js",
"files": [

@@ -36,3 +36,3 @@ "lib/index.js",

"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-env": "^1.1.8",
"babel-register": "^6.16.3",

@@ -39,0 +39,0 @@ "bluebird": "^3.3.4",

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