%PDF- %PDF-
| Direktori : /usr/share/nodejs/node-fetch/lib/ |
| Current File : //usr/share/nodejs/node-fetch/lib/index.js |
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var Url = require('url');
var http = require('http');
var https = require('https');
var zlib = require('zlib');
var Stream = require('stream');
var whatwgUrl = require('whatwg-url');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var Url__default = /*#__PURE__*/_interopDefaultLegacy(Url);
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib);
var Stream__default = /*#__PURE__*/_interopDefaultLegacy(Stream);
var whatwgUrl__default = /*#__PURE__*/_interopDefaultLegacy(whatwgUrl);
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
Object.defineProperty(Constructor, "prototype", {
writable: false
});
return Constructor;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
if (it) return (it = it.call(o)).next.bind(it);
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
if (it) o = it;
var i = 0;
return function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var Readable = Stream__default["default"].Readable;
var BUFFER = Symbol('buffer');
var TYPE = Symbol('type');
var Blob = /*#__PURE__*/function () {
function Blob() {
this[TYPE] = '';
var blobParts = arguments[0];
var options = arguments[1];
var buffers = [];
var size = 0;
if (blobParts) {
var a = blobParts;
var length = Number(a.length);
for (var i = 0; i < length; i++) {
var element = a[i];
var buffer = void 0;
if (element instanceof Buffer) {
buffer = element;
} else if (ArrayBuffer.isView(element)) {
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
} else if (element instanceof ArrayBuffer) {
buffer = Buffer.from(element);
} else if (element instanceof Blob) {
buffer = element[BUFFER];
} else {
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
}
size += buffer.length;
buffers.push(buffer);
}
}
this[BUFFER] = Buffer.concat(buffers);
var type = options && options.type !== undefined && String(options.type).toLowerCase();
if (type && !/[^\u0020-\u007E]/.test(type)) {
this[TYPE] = type;
}
}
var _proto = Blob.prototype;
_proto.text = function text() {
return Promise.resolve(this[BUFFER].toString());
};
_proto.arrayBuffer = function arrayBuffer() {
var buf = this[BUFFER];
var ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
return Promise.resolve(ab);
};
_proto.stream = function stream() {
var readable = new Readable();
readable._read = function () {};
readable.push(this[BUFFER]);
readable.push(null);
return readable;
};
_proto.toString = function toString() {
return '[object Blob]';
};
_proto.slice = function slice() {
var size = this.size;
var start = arguments[0];
var end = arguments[1];
var relativeStart, relativeEnd;
if (start === undefined) {
relativeStart = 0;
} else if (start < 0) {
relativeStart = Math.max(size + start, 0);
} else {
relativeStart = Math.min(start, size);
}
if (end === undefined) {
relativeEnd = size;
} else if (end < 0) {
relativeEnd = Math.max(size + end, 0);
} else {
relativeEnd = Math.min(end, size);
}
var span = Math.max(relativeEnd - relativeStart, 0);
var buffer = this[BUFFER];
var slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
var blob = new Blob([], {
type: arguments[2]
});
blob[BUFFER] = slicedBuffer;
return blob;
};
_createClass(Blob, [{
key: "size",
get: function get() {
return this[BUFFER].length;
}
}, {
key: "type",
get: function get() {
return this[TYPE];
}
}]);
return Blob;
}();
Object.defineProperties(Blob.prototype, {
size: {
enumerable: true
},
type: {
enumerable: true
},
slice: {
enumerable: true
}
});
Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
value: 'Blob',
writable: false,
enumerable: false,
configurable: true
});
/**
* fetch-error.js
*
* FetchError interface for operational errors
*/
/**
* Create FetchError instance
*
* @param String message Error message for human
* @param String type Error type for machine
* @param String systemError For Node.js system error
* @return FetchError
*/
function FetchError(message, type, systemError) {
Error.call(this, message);
this.message = message;
this.type = type; // when err.type is `system`, err.code contains system error code
if (systemError) {
this.code = this.errno = systemError.code;
} // hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);
}
FetchError.prototype = Object.create(Error.prototype);
FetchError.prototype.constructor = FetchError;
FetchError.prototype.name = 'FetchError';
var convert;
try {
convert = require('encoding').convert;
} catch (e) {}
var INTERNALS$2 = Symbol('Body internals'); // fix an issue where "PassThrough" isn't a named export for node <10
var PassThrough$1 = Stream__default["default"].PassThrough;
/**
* Body mixin
*
* Ref: https://fetch.spec.whatwg.org/#body
*
* @param Stream body Readable stream
* @param Object opts Response options
* @return Void
*/
function Body(body, _temp) {
var _this = this;
var _ref = _temp === void 0 ? {} : _temp,
_ref$size = _ref.size,
size = _ref$size === void 0 ? 0 : _ref$size,
_ref$timeout = _ref.timeout,
timeout = _ref$timeout === void 0 ? 0 : _ref$timeout;
if (body == null) {
// body is undefined or null
body = null;
} else if (isURLSearchParams(body)) {
// body is a URLSearchParams
body = Buffer.from(body.toString());
} else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
// body is ArrayBuffer
body = Buffer.from(body);
} else if (ArrayBuffer.isView(body)) {
// body is ArrayBufferView
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
} else if (body instanceof Stream__default["default"]) ; else {
// none of the above
// coerce to string then buffer
body = Buffer.from(String(body));
}
this[INTERNALS$2] = {
body,
disturbed: false,
error: null
};
this.size = size;
this.timeout = timeout;
if (body instanceof Stream__default["default"]) {
body.on('error', function (err) {
var error = err.name === 'AbortError' ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, 'system', err);
_this[INTERNALS$2].error = error;
});
}
}
Body.prototype = {
get body() {
return this[INTERNALS$2].body;
},
get bodyUsed() {
return this[INTERNALS$2].disturbed;
},
/**
* Decode response as ArrayBuffer
*
* @return Promise
*/
arrayBuffer() {
return consumeBody.call(this).then(function (buf) {
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
});
},
/**
* Return raw response as Blob
*
* @return Promise
*/
blob() {
var ct = this.headers && this.headers.get('content-type') || '';
return consumeBody.call(this).then(function (buf) {
return Object.assign( // Prevent copying
new Blob([], {
type: ct.toLowerCase()
}), {
[BUFFER]: buf
});
});
},
/**
* Decode response as json
*
* @return Promise
*/
json() {
var _this2 = this;
return consumeBody.call(this).then(function (buffer) {
try {
return JSON.parse(buffer.toString());
} catch (err) {
return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));
}
});
},
/**
* Decode response as text
*
* @return Promise
*/
text() {
return consumeBody.call(this).then(function (buffer) {
return buffer.toString();
});
},
/**
* Decode response as buffer (non-spec api)
*
* @return Promise
*/
buffer() {
return consumeBody.call(this);
},
/**
* Decode response as text, while automatically detecting the encoding and
* trying to decode to UTF-8 (non-spec api)
*
* @return Promise
*/
textConverted() {
var _this3 = this;
return consumeBody.call(this).then(function (buffer) {
return convertBody(buffer, _this3.headers);
});
}
}; // In browsers, all properties are enumerable.
Object.defineProperties(Body.prototype, {
body: {
enumerable: true
},
bodyUsed: {
enumerable: true
},
arrayBuffer: {
enumerable: true
},
blob: {
enumerable: true
},
json: {
enumerable: true
},
text: {
enumerable: true
}
});
Body.mixIn = function (proto) {
for (var _iterator = _createForOfIteratorHelperLoose(Object.getOwnPropertyNames(Body.prototype)), _step; !(_step = _iterator()).done;) {
var name = _step.value;
// istanbul ignore else: future proof
if (!(name in proto)) {
var desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
Object.defineProperty(proto, name, desc);
}
}
};
/**
* Consume and convert an entire Body to a Buffer.
*
* Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
*
* @return Promise
*/
function consumeBody() {
var _this4 = this;
if (this[INTERNALS$2].disturbed) {
return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
}
this[INTERNALS$2].disturbed = true;
if (this[INTERNALS$2].error) {
return Body.Promise.reject(this[INTERNALS$2].error);
}
var body = this.body; // body is null
if (body === null) {
return Body.Promise.resolve(Buffer.alloc(0));
} // body is blob
if (isBlob(body)) {
body = body.stream();
} // body is buffer
if (Buffer.isBuffer(body)) {
return Body.Promise.resolve(body);
} // istanbul ignore if: should never happen
if (!(body instanceof Stream__default["default"])) {
return Body.Promise.resolve(Buffer.alloc(0));
} // body is stream
// get ready to actually consume the body
var accum = [];
var accumBytes = 0;
var abort = false;
return new Body.Promise(function (resolve, reject) {
var resTimeout; // allow timeout on slow response body
if (_this4.timeout) {
resTimeout = setTimeout(function () {
abort = true;
reject(new FetchError(`Response timeout while trying to fetch ${_this4.url} (over ${_this4.timeout}ms)`, 'body-timeout'));
}, _this4.timeout);
} // handle stream errors
body.on('error', function (err) {
if (err.name === 'AbortError') {
// if the request was aborted, reject with this Error
abort = true;
reject(err);
} else {
// other errors, such as incorrect content-encoding
reject(new FetchError(`Invalid response body while trying to fetch ${_this4.url}: ${err.message}`, 'system', err));
}
});
body.on('data', function (chunk) {
if (abort || chunk === null) {
return;
}
if (_this4.size && accumBytes + chunk.length > _this4.size) {
abort = true;
reject(new FetchError(`content size at ${_this4.url} over limit: ${_this4.size}`, 'max-size'));
return;
}
accumBytes += chunk.length;
accum.push(chunk);
});
body.on('end', function () {
if (abort) {
return;
}
clearTimeout(resTimeout);
try {
resolve(Buffer.concat(accum, accumBytes));
} catch (err) {
// handle streams that have accumulated too much data (issue #414)
reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, 'system', err));
}
});
});
}
/**
* Detect buffer encoding and convert to target encoding
* ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding
*
* @param Buffer buffer Incoming buffer
* @param String encoding Target encoding
* @return String
*/
function convertBody(buffer, headers) {
if (typeof convert !== 'function') {
throw new Error('The package `encoding` must be installed to use the textConverted() function');
}
var ct = headers.get('content-type');
var charset = 'utf-8';
var res, str; // header
if (ct) {
res = /charset=([^;]*)/i.exec(ct);
} // no charset in content type, peek at response body for at most 1024 bytes
str = buffer.slice(0, 1024).toString(); // html5
if (!res && str) {
res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
} // html4
if (!res && str) {
res = /<meta[\s]+?http-equiv=(['"])content-type\1[\s]+?content=(['"])(.+?)\2/i.exec(str);
if (!res) {
res = /<meta[\s]+?content=(['"])(.+?)\1[\s]+?http-equiv=(['"])content-type\3/i.exec(str);
if (res) {
res.pop(); // drop last quote
}
}
if (res) {
res = /charset=(.*)/i.exec(res.pop());
}
} // xml
if (!res && str) {
res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str);
} // found charset
if (res) {
charset = res.pop(); // prevent decode issues when sites use incorrect encoding
// ref: https://hsivonen.fi/encoding-menu/
if (charset === 'gb2312' || charset === 'gbk') {
charset = 'gb18030';
}
} // turn raw buffers into a single utf-8 buffer
return convert(buffer, 'UTF-8', charset).toString();
}
/**
* Detect a URLSearchParams object
* ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
*
* @param Object obj Object to detect by type or brand
* @return String
*/
function isURLSearchParams(obj) {
// Duck-typing as a necessary condition.
if (typeof obj !== 'object' || typeof obj.append !== 'function' || typeof obj.delete !== 'function' || typeof obj.get !== 'function' || typeof obj.getAll !== 'function' || typeof obj.has !== 'function' || typeof obj.set !== 'function') {
return false;
} // Brand-checking and more duck-typing as optional condition.
return obj.constructor.name === 'URLSearchParams' || Object.prototype.toString.call(obj) === '[object URLSearchParams]' || typeof obj.sort === 'function';
}
/**
* Check if `obj` is a W3C `Blob` object (which `File` inherits from)
* @param {*} obj
* @return {boolean}
*/
function isBlob(obj) {
return typeof obj === 'object' && typeof obj.arrayBuffer === 'function' && typeof obj.type === 'string' && typeof obj.stream === 'function' && typeof obj.constructor === 'function' && typeof obj.constructor.name === 'string' && /^(Blob|File)$/.test(obj.constructor.name) && /^(Blob|File)$/.test(obj[Symbol.toStringTag]);
}
/**
* Clone body given Res/Req instance
*
* @param Mixed instance Response or Request instance
* @return Mixed
*/
function clone(instance) {
var p1, p2;
var body = instance.body; // don't allow cloning a used body
if (instance.bodyUsed) {
throw new Error('cannot clone body after it is used');
} // check that body is a stream and not form-data object
// note: we can't clone the form-data object without having it as a dependency
if (body instanceof Stream__default["default"] && typeof body.getBoundary !== 'function') {
// tee instance body
p1 = new PassThrough$1();
p2 = new PassThrough$1();
body.pipe(p1);
body.pipe(p2); // set instance body to teed body and return the other teed body
instance[INTERNALS$2].body = p1;
body = p2;
}
return body;
}
/**
* Performs the operation "extract a `Content-Type` value from |object|" as
* specified in the specification:
* https://fetch.spec.whatwg.org/#concept-bodyinit-extract
*
* This function assumes that instance.body is present.
*
* @param Mixed instance Any options.body input
*/
function extractContentType(body) {
if (body === null) {
// body is null
return null;
} else if (typeof body === 'string') {
// body is string
return 'text/plain;charset=UTF-8';
} else if (isURLSearchParams(body)) {
// body is a URLSearchParams
return 'application/x-www-form-urlencoded;charset=UTF-8';
} else if (isBlob(body)) {
// body is blob
return body.type || null;
} else if (Buffer.isBuffer(body)) {
// body is buffer
return null;
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
// body is ArrayBuffer
return null;
} else if (ArrayBuffer.isView(body)) {
// body is ArrayBufferView
return null;
} else if (typeof body.getBoundary === 'function') {
// detect form data input from form-data module
return `multipart/form-data;boundary=${body.getBoundary()}`;
} else if (body instanceof Stream__default["default"]) {
// body is stream
// can't really do much about this
return null;
} else {
// Body constructor defaults other things to string
return 'text/plain;charset=UTF-8';
}
}
/**
* The Fetch Standard treats this as if "total bytes" is a property on the body.
* For us, we have to explicitly get it with a function.
*
* ref: https://fetch.spec.whatwg.org/#concept-body-total-bytes
*
* @param Body instance Instance of Body
* @return Number? Number of bytes, or null if not possible
*/
function getTotalBytes(instance) {
var body = instance.body;
if (body === null) {
// body is null
return 0;
} else if (isBlob(body)) {
return body.size;
} else if (Buffer.isBuffer(body)) {
// body is buffer
return body.length;
} else if (body && typeof body.getLengthSync === 'function') {
// detect form data input from form-data module
if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x
body.hasKnownLength && body.hasKnownLength()) {
// 2.x
return body.getLengthSync();
}
return null;
} else {
// body is stream
return null;
}
}
/**
* Write a Body to a Node.js WritableStream (e.g. http.Request) object.
*
* @param Body instance Instance of Body
* @return Void
*/
function writeToStream(dest, instance) {
var body = instance.body;
if (body === null) {
// body is null
dest.end();
} else if (isBlob(body)) {
body.stream().pipe(dest);
} else if (Buffer.isBuffer(body)) {
// body is buffer
dest.write(body);
dest.end();
} else {
// body is stream
body.pipe(dest);
}
} // expose Promise
Body.Promise = global.Promise;
/**
* headers.js
*
* Headers class offers convenient helpers
*/
var invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
var invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
function validateName(name) {
name = `${name}`;
if (invalidTokenRegex.test(name) || name === '') {
throw new TypeError(`${name} is not a legal HTTP header name`);
}
}
function validateValue(value) {
value = `${value}`;
if (invalidHeaderCharRegex.test(value)) {
throw new TypeError(`${value} is not a legal HTTP header value`);
}
}
/**
* Find the key in the map object given a header name.
*
* Returns undefined if not found.
*
* @param String name Header name
* @return String|Undefined
*/
function find(map, name) {
name = name.toLowerCase();
for (var key in map) {
if (key.toLowerCase() === name) {
return key;
}
}
return undefined;
}
var MAP = Symbol('map');
var Headers = /*#__PURE__*/function (_Symbol$iterator) {
/**
* Headers class
*
* @param Object headers Response headers
* @return Void
*/
function Headers(init) {
if (init === void 0) {
init = undefined;
}
this[MAP] = Object.create(null);
if (init instanceof Headers) {
var rawHeaders = init.raw();
var headerNames = Object.keys(rawHeaders);
for (var _i = 0, _headerNames = headerNames; _i < _headerNames.length; _i++) {
var headerName = _headerNames[_i];
for (var _iterator = _createForOfIteratorHelperLoose(rawHeaders[headerName]), _step; !(_step = _iterator()).done;) {
var value = _step.value;
this.append(headerName, value);
}
}
return;
} // We don't worry about converting prop to ByteString here as append()
// will handle it.
if (init == null) ; 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');
} // sequence<sequence<ByteString>>
// Note: per spec we have to first exhaust the lists then process them
var pairs = [];
for (var _iterator2 = _createForOfIteratorHelperLoose(init), _step2; !(_step2 = _iterator2()).done;) {
var _pair = _step2.value;
if (typeof _pair !== 'object' || typeof _pair[Symbol.iterator] !== 'function') {
throw new TypeError('Each header pair must be iterable');
}
pairs.push(Array.from(_pair));
}
for (var _i2 = 0, _pairs = pairs; _i2 < _pairs.length; _i2++) {
var pair = _pairs[_i2];
if (pair.length !== 2) {
throw new TypeError('Each header pair must be a name/value tuple');
}
this.append(pair[0], pair[1]);
}
} else {
// record<ByteString, ByteString>
for (var _i3 = 0, _Object$keys = Object.keys(init); _i3 < _Object$keys.length; _i3++) {
var key = _Object$keys[_i3];
var _value = init[key];
this.append(key, _value);
}
}
} else {
throw new TypeError('Provided initializer must be an object');
}
}
/**
* Return combined header value given name
*
* @param String name Header name
* @return Mixed
*/
var _proto = Headers.prototype;
_proto.get = function get(name) {
name = `${name}`;
validateName(name);
var key = find(this[MAP], name);
if (key === undefined) {
return null;
}
return this[MAP][key].join(', ');
}
/**
* Iterate over all headers
*
* @param Function callback Executed for each item with parameters (value, name, thisArg)
* @param Boolean thisArg `this` context for callback function
* @return Void
*/
;
_proto.forEach = function forEach(callback, thisArg) {
if (thisArg === void 0) {
thisArg = undefined;
}
var pairs = getHeaders(this);
var i = 0;
while (i < pairs.length) {
var _pairs$i = pairs[i],
name = _pairs$i[0],
value = _pairs$i[1];
callback.call(thisArg, value, name, this);
pairs = getHeaders(this);
i++;
}
}
/**
* Overwrite header values given name
*
* @param String name Header name
* @param String value Header value
* @return Void
*/
;
_proto.set = function set(name, value) {
name = `${name}`;
value = `${value}`;
validateName(name);
validateValue(value);
var key = find(this[MAP], name);
this[MAP][key !== undefined ? key : name] = [value];
}
/**
* Append a value onto existing header
*
* @param String name Header name
* @param String value Header value
* @return Void
*/
;
_proto.append = function append(name, value) {
name = `${name}`;
value = `${value}`;
validateName(name);
validateValue(value);
var key = find(this[MAP], name);
if (key !== undefined) {
this[MAP][key].push(value);
} else {
this[MAP][name] = [value];
}
}
/**
* Check for header name existence
*
* @param String name Header name
* @return Boolean
*/
;
_proto.has = function has(name) {
name = `${name}`;
validateName(name);
return find(this[MAP], name) !== undefined;
}
/**
* Delete all header values given name
*
* @param String name Header name
* @return Void
*/
;
_proto.delete = function _delete(name) {
name = `${name}`;
validateName(name);
var key = find(this[MAP], name);
if (key !== undefined) {
delete this[MAP][key];
}
};
/**
* Return raw headers (non-spec api)
*
* @return Object
*/
_proto.raw = function raw() {
return this[MAP];
}
/**
* Get an iterator on keys.
*
* @return Iterator
*/
;
_proto.keys = function keys() {
return createHeadersIterator(this, 'key');
}
/**
* Get an iterator on values.
*
* @return Iterator
*/
;
_proto.values = function values() {
return createHeadersIterator(this, 'value');
}
/**
* Get an iterator on entries.
*
* This is the default iterator of the Headers object.
*
* @return Iterator
*/
;
_proto[_Symbol$iterator] = function () {
return createHeadersIterator(this, 'key+value');
};
return Headers;
}(Symbol.iterator);
Headers.prototype.entries = Headers.prototype[Symbol.iterator];
Object.defineProperty(Headers.prototype, Symbol.toStringTag, {
value: 'Headers',
writable: false,
enumerable: false,
configurable: true
});
Object.defineProperties(Headers.prototype, {
get: {
enumerable: true
},
forEach: {
enumerable: true
},
set: {
enumerable: true
},
append: {
enumerable: true
},
has: {
enumerable: true
},
delete: {
enumerable: true
},
keys: {
enumerable: true
},
values: {
enumerable: true
},
entries: {
enumerable: true
}
});
function getHeaders(headers, kind) {
if (kind === void 0) {
kind = 'key+value';
}
var keys = Object.keys(headers[MAP]).sort();
return keys.map(kind === 'key' ? function (k) {
return k.toLowerCase();
} : kind === 'value' ? function (k) {
return headers[MAP][k].join(', ');
} : function (k) {
return [k.toLowerCase(), headers[MAP][k].join(', ')];
});
}
var INTERNAL = Symbol('internal');
function createHeadersIterator(target, kind) {
var iterator = Object.create(HeadersIteratorPrototype);
iterator[INTERNAL] = {
target,
kind,
index: 0
};
return iterator;
}
var HeadersIteratorPrototype = Object.setPrototypeOf({
next() {
// istanbul ignore if
if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) {
throw new TypeError('Value of `this` is not a HeadersIterator');
}
var _this$INTERNAL = this[INTERNAL],
target = _this$INTERNAL.target,
kind = _this$INTERNAL.kind,
index = _this$INTERNAL.index;
var values = getHeaders(target, kind);
var len = values.length;
if (index >= len) {
return {
value: undefined,
done: true
};
}
this[INTERNAL].index = index + 1;
return {
value: values[index],
done: false
};
}
}, Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())));
Object.defineProperty(HeadersIteratorPrototype, Symbol.toStringTag, {
value: 'HeadersIterator',
writable: false,
enumerable: false,
configurable: true
});
/**
* Export the Headers object in a form that Node.js can consume.
*
* @param Headers headers
* @return Object
*/
function exportNodeCompatibleHeaders(headers) {
var obj = Object.assign({
__proto__: null
}, headers[MAP]); // http.request() only supports string as Host header. This hack makes
// specifying custom Host header possible.
var hostHeaderKey = find(headers[MAP], 'Host');
if (hostHeaderKey !== undefined) {
obj[hostHeaderKey] = obj[hostHeaderKey][0];
}
return obj;
}
/**
* Create a Headers object from an object of headers, ignoring those that do
* not conform to HTTP grammar productions.
*
* @param Object obj Object of headers
* @return Headers
*/
function createHeadersLenient(obj) {
var headers = new Headers();
for (var _i4 = 0, _Object$keys2 = Object.keys(obj); _i4 < _Object$keys2.length; _i4++) {
var name = _Object$keys2[_i4];
if (invalidTokenRegex.test(name)) {
continue;
}
if (Array.isArray(obj[name])) {
for (var _iterator3 = _createForOfIteratorHelperLoose(obj[name]), _step3; !(_step3 = _iterator3()).done;) {
var val = _step3.value;
if (invalidHeaderCharRegex.test(val)) {
continue;
}
if (headers[MAP][name] === undefined) {
headers[MAP][name] = [val];
} else {
headers[MAP][name].push(val);
}
}
} else if (!invalidHeaderCharRegex.test(obj[name])) {
headers[MAP][name] = [obj[name]];
}
}
return headers;
}
var INTERNALS$1 = Symbol('Response internals'); // fix an issue where "STATUS_CODES" aren't a named export for node <10
var STATUS_CODES = http__default["default"].STATUS_CODES;
/**
* Response class
*
* @param Stream body Readable stream
* @param Object opts Response options
* @return Void
*/
var Response = /*#__PURE__*/function () {
function Response(body, opts) {
if (body === void 0) {
body = null;
}
if (opts === void 0) {
opts = {};
}
Body.call(this, body, opts);
var status = opts.status || 200;
var headers = new Headers(opts.headers);
if (body != null && !headers.has('Content-Type')) {
var contentType = extractContentType(body);
if (contentType) {
headers.append('Content-Type', contentType);
}
}
this[INTERNALS$1] = {
url: opts.url,
status,
statusText: opts.statusText || STATUS_CODES[status],
headers,
counter: opts.counter
};
}
var _proto = Response.prototype;
/**
* Clone this response
*
* @return Response
*/
_proto.clone = function clone$1() {
return new Response(clone(this), {
url: this.url,
status: this.status,
statusText: this.statusText,
headers: this.headers,
ok: this.ok,
redirected: this.redirected
});
};
_createClass(Response, [{
key: "url",
get: function get() {
return this[INTERNALS$1].url || '';
}
}, {
key: "status",
get: function get() {
return this[INTERNALS$1].status;
}
/**
* Convenience property representing if the request ended normally
*/
}, {
key: "ok",
get: function get() {
return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300;
}
}, {
key: "redirected",
get: function get() {
return this[INTERNALS$1].counter > 0;
}
}, {
key: "statusText",
get: function get() {
return this[INTERNALS$1].statusText;
}
}, {
key: "headers",
get: function get() {
return this[INTERNALS$1].headers;
}
}]);
return Response;
}();
Body.mixIn(Response.prototype);
Object.defineProperties(Response.prototype, {
url: {
enumerable: true
},
status: {
enumerable: true
},
ok: {
enumerable: true
},
redirected: {
enumerable: true
},
statusText: {
enumerable: true
},
headers: {
enumerable: true
},
clone: {
enumerable: true
}
});
Object.defineProperty(Response.prototype, Symbol.toStringTag, {
value: 'Response',
writable: false,
enumerable: false,
configurable: true
});
var INTERNALS = Symbol('Request internals');
var URL$1 = Url__default["default"].URL || whatwgUrl__default["default"].URL; // fix an issue where "format", "parse" aren't a named export for node <10
var parse_url = Url__default["default"].parse;
var format_url = Url__default["default"].format;
/**
* Wrapper around `new URL` to handle arbitrary URLs
*
* @param {string} urlStr
* @return {void}
*/
function parseURL(urlStr) {
/*
Check whether the URL is absolute or not
Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
*/
if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.exec(urlStr)) {
urlStr = new URL$1(urlStr).toString();
} // Fallback to old implementation for arbitrary URLs
return parse_url(urlStr);
}
var streamDestructionSupported = ('destroy' in Stream__default["default"].Readable.prototype);
/**
* Check if a value is an instance of Request.
*
* @param Mixed input
* @return Boolean
*/
function isRequest(input) {
return typeof input === 'object' && typeof input[INTERNALS] === 'object';
}
function isAbortSignal(signal) {
var proto = signal && typeof signal === 'object' && Object.getPrototypeOf(signal);
return !!(proto && proto.constructor.name === 'AbortSignal');
}
/**
* Request class
*
* @param Mixed input Url or Request instance
* @param Object init Custom options
* @return Void
*/
var Request = /*#__PURE__*/function () {
function Request(input, init) {
if (init === void 0) {
init = {};
}
var parsedURL; // normalize input
if (!isRequest(input)) {
if (input && input.href) {
// in order to support Node.js' Url objects; though WHATWG's URL objects
// will fall into this branch also (since their `toString()` will return
// `href` property anyway)
parsedURL = parseURL(input.href);
} else {
// coerce input to a string before attempting to parse
parsedURL = parseURL(`${input}`);
}
input = {};
} else {
parsedURL = parseURL(input.url);
}
var method = init.method || input.method || 'GET';
method = method.toUpperCase();
if ((init.body != null || isRequest(input) && input.body !== null) && (method === 'GET' || method === 'HEAD')) {
throw new TypeError('Request with GET/HEAD method cannot have body');
}
var inputBody = init.body != null ? init.body : isRequest(input) && input.body !== null ? clone(input) : null;
Body.call(this, inputBody, {
timeout: init.timeout || input.timeout || 0,
size: init.size || input.size || 0
});
var headers = new Headers(init.headers || input.headers || {});
if (inputBody != null && !headers.has('Content-Type')) {
var contentType = extractContentType(inputBody);
if (contentType) {
headers.append('Content-Type', contentType);
}
}
var signal = isRequest(input) ? input.signal : null;
if ('signal' in init) signal = init.signal;
if (signal != null && !isAbortSignal(signal)) {
throw new TypeError('Expected signal to be an instanceof AbortSignal');
}
this[INTERNALS] = {
method,
redirect: init.redirect || input.redirect || 'follow',
headers,
parsedURL,
signal
}; // node-fetch-only options
this.follow = init.follow !== undefined ? init.follow : input.follow !== undefined ? input.follow : 20;
this.compress = init.compress !== undefined ? init.compress : input.compress !== undefined ? input.compress : true;
this.counter = init.counter || input.counter || 0;
this.agent = init.agent || input.agent;
}
var _proto = Request.prototype;
/**
* Clone this request
*
* @return Request
*/
_proto.clone = function clone() {
return new Request(this);
};
_createClass(Request, [{
key: "method",
get: function get() {
return this[INTERNALS].method;
}
}, {
key: "url",
get: function get() {
return format_url(this[INTERNALS].parsedURL);
}
}, {
key: "headers",
get: function get() {
return this[INTERNALS].headers;
}
}, {
key: "redirect",
get: function get() {
return this[INTERNALS].redirect;
}
}, {
key: "signal",
get: function get() {
return this[INTERNALS].signal;
}
}]);
return Request;
}();
Body.mixIn(Request.prototype);
Object.defineProperty(Request.prototype, Symbol.toStringTag, {
value: 'Request',
writable: false,
enumerable: false,
configurable: true
});
Object.defineProperties(Request.prototype, {
method: {
enumerable: true
},
url: {
enumerable: true
},
headers: {
enumerable: true
},
redirect: {
enumerable: true
},
clone: {
enumerable: true
},
signal: {
enumerable: true
}
});
/**
* Convert a Request to Node.js http request options.
*
* @param Request A Request instance
* @return Object The options object to be passed to http.request
*/
function getNodeRequestOptions(request) {
var parsedURL = request[INTERNALS].parsedURL;
var headers = new Headers(request[INTERNALS].headers); // fetch step 1.3
if (!headers.has('Accept')) {
headers.set('Accept', '*/*');
} // Basic fetch
if (!parsedURL.protocol || !parsedURL.hostname) {
throw new TypeError('Only absolute URLs are supported');
}
if (!/^https?:$/.test(parsedURL.protocol)) {
throw new TypeError('Only HTTP(S) protocols are supported');
}
if (request.signal && request.body instanceof Stream__default["default"].Readable && !streamDestructionSupported) {
throw new Error('Cancellation of streamed requests with AbortSignal is not supported in node < 8');
} // HTTP-network-or-cache fetch steps 2.4-2.7
var contentLengthValue = null;
if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
contentLengthValue = '0';
}
if (request.body != null) {
var totalBytes = getTotalBytes(request);
if (typeof totalBytes === 'number') {
contentLengthValue = String(totalBytes);
}
}
if (contentLengthValue) {
headers.set('Content-Length', contentLengthValue);
} // HTTP-network-or-cache fetch step 2.11
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
} // HTTP-network-or-cache fetch step 2.15
if (request.compress && !headers.has('Accept-Encoding')) {
headers.set('Accept-Encoding', 'gzip,deflate');
}
var agent = request.agent;
if (typeof agent === 'function') {
agent = agent(parsedURL);
}
if (!headers.has('Connection') && !agent) {
headers.set('Connection', 'close');
} // HTTP-network fetch step 4.2
// chunked encoding is handled by Node.js
return Object.assign({}, parsedURL, {
method: request.method,
headers: exportNodeCompatibleHeaders(headers),
agent
});
}
/**
* abort-error.js
*
* AbortError interface for cancelled requests
*/
/**
* Create AbortError instance
*
* @param String message Error message for human
* @return AbortError
*/
function AbortError(message) {
Error.call(this, message);
this.type = 'aborted';
this.message = message; // hide custom error implementation details from end-users
Error.captureStackTrace(this, this.constructor);
}
AbortError.prototype = Object.create(Error.prototype);
AbortError.prototype.constructor = AbortError;
AbortError.prototype.name = 'AbortError';
/**
* index.js
*
* a request API compatible with window.fetch
*
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
*/
var URL = Url__default["default"].URL || whatwgUrl__default["default"].URL; // fix an issue where "PassThrough", "resolve" aren't a named export for node <10
var PassThrough = Stream__default["default"].PassThrough;
var isDomainOrSubdomain = function isDomainOrSubdomain(destination, original) {
var orig = new URL(original).hostname;
var dest = new URL(destination).hostname;
return orig === dest || orig[orig.length - dest.length - 1] === '.' && orig.endsWith(dest);
};
/**
* Fetch function
*
* @param Mixed url Absolute url or Request instance
* @param Object opts Fetch options
* @return Promise
*/
function fetch(url, opts) {
// allow custom promise
if (!fetch.Promise) {
throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
}
Body.Promise = fetch.Promise; // wrap http.request into fetch
return new fetch.Promise(function (resolve, reject) {
// build request object
var request = new Request(url, opts);
var options = getNodeRequestOptions(request);
var send = (options.protocol === 'https:' ? https__default["default"] : http__default["default"]).request;
var signal = request.signal;
var response = null;
var abort = function abort() {
var error = new AbortError('The user aborted a request.');
reject(error);
if (request.body && request.body instanceof Stream__default["default"].Readable) {
request.body.destroy(error);
}
if (!response || !response.body) return;
response.body.emit('error', error);
};
if (signal && signal.aborted) {
abort();
return;
}
var abortAndFinalize = function abortAndFinalize() {
abort();
finalize();
}; // send request
var req = send(options);
var reqTimeout;
if (signal) {
signal.addEventListener('abort', abortAndFinalize);
}
function finalize() {
req.abort();
if (signal) signal.removeEventListener('abort', abortAndFinalize);
clearTimeout(reqTimeout);
}
if (request.timeout) {
req.once('socket', function (socket) {
reqTimeout = setTimeout(function () {
reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
finalize();
}, request.timeout);
});
}
req.on('error', function (err) {
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
finalize();
});
req.on('response', function (res) {
clearTimeout(reqTimeout);
var headers = createHeadersLenient(res.headers); // HTTP fetch step 5
if (fetch.isRedirect(res.statusCode)) {
// HTTP fetch step 5.2
var location = headers.get('Location'); // HTTP fetch step 5.3
var locationURL = null;
try {
locationURL = location === null ? null : new URL(location, request.url).toString();
} catch (err) {
// error here can only be invalid URL in Location: header
// do not throw when options.redirect == manual
// let the user extract the errorneous redirect URL
if (request.redirect !== 'manual') {
reject(new FetchError(`uri requested responds with an invalid redirect URL: ${location}`, 'invalid-redirect'));
finalize();
return;
}
} // HTTP fetch step 5.5
switch (request.redirect) {
case 'error':
reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
finalize();
return;
case 'manual':
// node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
if (locationURL !== null) {
// handle corrupted header
try {
headers.set('Location', locationURL);
} catch (err) {
// istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
reject(err);
}
}
break;
case 'follow':
// HTTP-redirect fetch step 2
if (locationURL === null) {
break;
} // HTTP-redirect fetch step 5
if (request.counter >= request.follow) {
reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
finalize();
return;
} // HTTP-redirect fetch step 6 (counter increment)
// Create a new Request object.
var requestOpts = {
headers: new Headers(request.headers),
follow: request.follow,
counter: request.counter + 1,
agent: request.agent,
compress: request.compress,
method: request.method,
body: request.body,
signal: request.signal,
timeout: request.timeout,
size: request.size
};
if (!isDomainOrSubdomain(request.url, locationURL)) {
for (var _i = 0, _arr = ['authorization', 'www-authenticate', 'cookie', 'cookie2']; _i < _arr.length; _i++) {
var name = _arr[_i];
requestOpts.headers.delete(name);
}
} // HTTP-redirect fetch step 9
if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
finalize();
return;
} // HTTP-redirect fetch step 11
if (res.statusCode === 303 || (res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST') {
requestOpts.method = 'GET';
requestOpts.body = undefined;
requestOpts.headers.delete('content-length');
} // HTTP-redirect fetch step 15
resolve(fetch(new Request(locationURL, requestOpts)));
finalize();
return;
}
} // prepare response
res.once('end', function () {
if (signal) signal.removeEventListener('abort', abortAndFinalize);
});
var body = res.pipe(new PassThrough());
var response_options = {
url: request.url,
status: res.statusCode,
statusText: res.statusMessage,
headers: headers,
size: request.size,
timeout: request.timeout,
counter: request.counter
}; // HTTP-network fetch step 12.1.1.3
var codings = headers.get('Content-Encoding'); // HTTP-network fetch step 12.1.1.4: handle content codings
// in following scenarios we ignore compression support
// 1. compression support is disabled
// 2. HEAD request
// 3. no Content-Encoding header
// 4. no content response (204)
// 5. content not modified response (304)
if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
response = new Response(body, response_options);
resolve(response);
return;
} // For Node v6+
// Be less strict when decoding compressed responses, since sometimes
// servers send slightly invalid responses that are still accepted
// by common browsers.
// Always using Z_SYNC_FLUSH is what cURL does.
var zlibOptions = {
flush: zlib__default["default"].Z_SYNC_FLUSH,
finishFlush: zlib__default["default"].Z_SYNC_FLUSH
}; // for gzip
if (codings == 'gzip' || codings == 'x-gzip') {
body = body.pipe(zlib__default["default"].createGunzip(zlibOptions));
response = new Response(body, response_options);
resolve(response);
return;
} // for deflate
if (codings == 'deflate' || codings == 'x-deflate') {
// handle the infamous raw deflate response from old servers
// a hack for old IIS and Apache servers
var raw = res.pipe(new PassThrough());
raw.once('data', function (chunk) {
// see http://stackoverflow.com/questions/37519828
if ((chunk[0] & 0x0F) === 0x08) {
body = body.pipe(zlib__default["default"].createInflate());
} else {
body = body.pipe(zlib__default["default"].createInflateRaw());
}
response = new Response(body, response_options);
resolve(response);
});
return;
} // for br
if (codings == 'br' && typeof zlib__default["default"].createBrotliDecompress === 'function') {
body = body.pipe(zlib__default["default"].createBrotliDecompress());
response = new Response(body, response_options);
resolve(response);
return;
} // otherwise, use response as-is
response = new Response(body, response_options);
resolve(response);
});
writeToStream(req, request);
});
}
/**
* Redirect code matching
*
* @param Number code Status code
* @return Boolean
*/
fetch.isRedirect = function (code) {
return code === 301 || code === 302 || code === 303 || code === 307 || code === 308;
}; // expose Promise
fetch.Promise = global.Promise;
module.exports = exports = fetch;
exports.FetchError = FetchError;
exports.Headers = Headers;
exports.Request = Request;
exports.Response = Response;
exports["default"] = fetch;