UNPKG

piral-base

Version:

The base library for creating a Piral instance.

1,182 lines (1,180 loc) • 550 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // ../../../node_modules/systemjs/dist/system-node.cjs var require_system_node = __commonJS({ "../../../node_modules/systemjs/dist/system-node.cjs"(exports, module) { (() => { var __webpack_modules__ = { /***/ 18: ( /***/ (module2) => { var toString = Object.prototype.toString; var isModern = typeof Buffer !== "undefined" && typeof Buffer.alloc === "function" && typeof Buffer.allocUnsafe === "function" && typeof Buffer.from === "function"; function isArrayBuffer(input) { return toString.call(input).slice(8, -1) === "ArrayBuffer"; } function fromArrayBuffer(obj, byteOffset, length) { byteOffset >>>= 0; var maxLength = obj.byteLength - byteOffset; if (maxLength < 0) { throw new RangeError("'offset' is out of bounds"); } if (length === void 0) { length = maxLength; } else { length >>>= 0; if (length > maxLength) { throw new RangeError("'length' is out of bounds"); } } return isModern ? Buffer.from(obj.slice(byteOffset, byteOffset + length)) : new Buffer(new Uint8Array(obj.slice(byteOffset, byteOffset + length))); } function fromString(string, encoding) { if (typeof encoding !== "string" || encoding === "") { encoding = "utf8"; } if (!Buffer.isEncoding(encoding)) { throw new TypeError('"encoding" must be a valid string encoding'); } return isModern ? Buffer.from(string, encoding) : new Buffer(string, encoding); } function bufferFrom(value, encodingOrOffset, length) { if (typeof value === "number") { throw new TypeError('"value" argument must not be a number'); } if (isArrayBuffer(value)) { return fromArrayBuffer(value, encodingOrOffset, length); } if (typeof value === "string") { return fromString(value, encodingOrOffset); } return isModern ? Buffer.from(value) : new Buffer(value); } module2.exports = bufferFrom; } ), /***/ 467: ( /***/ (module2, exports2, __nccwpck_require__2) => { "use strict"; Object.defineProperty(exports2, "__esModule", { value: true }); function _interopDefault(ex) { return ex && typeof ex === "object" && "default" in ex ? ex["default"] : ex; } var Stream = _interopDefault(__nccwpck_require__2(781)); var http = _interopDefault(__nccwpck_require__2(685)); var Url = _interopDefault(__nccwpck_require__2(310)); var whatwgUrl = _interopDefault(__nccwpck_require__2(665)); var https = _interopDefault(__nccwpck_require__2(687)); var zlib = _interopDefault(__nccwpck_require__2(796)); const Readable = Stream.Readable; const BUFFER = Symbol("buffer"); const TYPE = Symbol("type"); class Blob { constructor() { this[TYPE] = ""; const blobParts = arguments[0]; const options = arguments[1]; const buffers = []; let size = 0; if (blobParts) { const a = blobParts; const length = Number(a.length); for (let i = 0; i < length; i++) { const element = a[i]; let buffer; 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); let type = options && options.type !== void 0 && String(options.type).toLowerCase(); if (type && !/[^\u0020-\u007E]/.test(type)) { this[TYPE] = type; } } get size() { return this[BUFFER].length; } get type() { return this[TYPE]; } text() { return Promise.resolve(this[BUFFER].toString()); } arrayBuffer() { const buf = this[BUFFER]; const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); return Promise.resolve(ab); } stream() { const readable = new Readable(); readable._read = function() { }; readable.push(this[BUFFER]); readable.push(null); return readable; } toString() { return "[object Blob]"; } slice() { const size = this.size; const start = arguments[0]; const end = arguments[1]; let relativeStart, relativeEnd; if (start === void 0) { relativeStart = 0; } else if (start < 0) { relativeStart = Math.max(size + start, 0); } else { relativeStart = Math.min(start, size); } if (end === void 0) { relativeEnd = size; } else if (end < 0) { relativeEnd = Math.max(size + end, 0); } else { relativeEnd = Math.min(end, size); } const span = Math.max(relativeEnd - relativeStart, 0); const buffer = this[BUFFER]; const slicedBuffer = buffer.slice(relativeStart, relativeStart + span); const blob = new Blob([], { type: arguments[2] }); blob[BUFFER] = slicedBuffer; 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 }); function FetchError(message, type, systemError) { Error.call(this, message); this.message = message; this.type = type; if (systemError) { this.code = this.errno = systemError.code; } Error.captureStackTrace(this, this.constructor); } FetchError.prototype = Object.create(Error.prototype); FetchError.prototype.constructor = FetchError; FetchError.prototype.name = "FetchError"; let convert; try { convert = __nccwpck_require__2(877).convert; } catch (e) { } const INTERNALS = Symbol("Body internals"); const PassThrough = Stream.PassThrough; function Body(body) { var _this = this; var _ref = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, _ref$size = _ref.size; let size = _ref$size === void 0 ? 0 : _ref$size; var _ref$timeout = _ref.timeout; let timeout = _ref$timeout === void 0 ? 0 : _ref$timeout; if (body == null) { body = null; } else if (isURLSearchParams(body)) { body = Buffer.from(body.toString()); } else if (isBlob(body)) ; else if (Buffer.isBuffer(body)) ; else if (Object.prototype.toString.call(body) === "[object ArrayBuffer]") { body = Buffer.from(body); } else if (ArrayBuffer.isView(body)) { body = Buffer.from(body.buffer, body.byteOffset, body.byteLength); } else if (body instanceof Stream) ; else { body = Buffer.from(String(body)); } this[INTERNALS] = { body, disturbed: false, error: null }; this.size = size; this.timeout = timeout; if (body instanceof Stream) { body.on("error", function(err) { const error = err.name === "AbortError" ? err : new FetchError(`Invalid response body while trying to fetch ${_this.url}: ${err.message}`, "system", err); _this[INTERNALS].error = error; }); } } Body.prototype = { get body() { return this[INTERNALS].body; }, get bodyUsed() { return this[INTERNALS].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() { let 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); }); } }; 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 (const name of Object.getOwnPropertyNames(Body.prototype)) { if (!(name in proto)) { const desc = Object.getOwnPropertyDescriptor(Body.prototype, name); Object.defineProperty(proto, name, desc); } } }; function consumeBody() { var _this4 = this; if (this[INTERNALS].disturbed) { return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`)); } this[INTERNALS].disturbed = true; if (this[INTERNALS].error) { return Body.Promise.reject(this[INTERNALS].error); } let body = this.body; if (body === null) { return Body.Promise.resolve(Buffer.alloc(0)); } if (isBlob(body)) { body = body.stream(); } if (Buffer.isBuffer(body)) { return Body.Promise.resolve(body); } if (!(body instanceof Stream)) { return Body.Promise.resolve(Buffer.alloc(0)); } let accum = []; let accumBytes = 0; let abort = false; return new Body.Promise(function(resolve, reject) { let resTimeout; 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); } body.on("error", function(err) { if (err.name === "AbortError") { abort = true; reject(err); } else { 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) { reject(new FetchError(`Could not create Buffer from response body for ${_this4.url}: ${err.message}`, "system", err)); } }); }); } function convertBody(buffer, headers) { if (typeof convert !== "function") { throw new Error("The package `encoding` must be installed to use the textConverted() function"); } const ct = headers.get("content-type"); let charset = "utf-8"; let res, str; if (ct) { res = /charset=([^;]*)/i.exec(ct); } str = buffer.slice(0, 1024).toString(); if (!res && str) { res = /<meta.+?charset=(['"])(.+?)\1/i.exec(str); } 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(); } } if (res) { res = /charset=(.*)/i.exec(res.pop()); } } if (!res && str) { res = /<\?xml.+?encoding=(['"])(.+?)\1/i.exec(str); } if (res) { charset = res.pop(); if (charset === "gb2312" || charset === "gbk") { charset = "gb18030"; } } return convert(buffer, "UTF-8", charset).toString(); } function isURLSearchParams(obj) { 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; } return obj.constructor.name === "URLSearchParams" || Object.prototype.toString.call(obj) === "[object URLSearchParams]" || typeof obj.sort === "function"; } 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]); } function clone(instance) { let p1, p2; let body = instance.body; if (instance.bodyUsed) { throw new Error("cannot clone body after it is used"); } if (body instanceof Stream && typeof body.getBoundary !== "function") { p1 = new PassThrough(); p2 = new PassThrough(); body.pipe(p1); body.pipe(p2); instance[INTERNALS].body = p1; body = p2; } return body; } function extractContentType(body) { if (body === null) { return null; } else if (typeof body === "string") { return "text/plain;charset=UTF-8"; } else if (isURLSearchParams(body)) { return "application/x-www-form-urlencoded;charset=UTF-8"; } else if (isBlob(body)) { return body.type || null; } else if (Buffer.isBuffer(body)) { return null; } else if (Object.prototype.toString.call(body) === "[object ArrayBuffer]") { return null; } else if (ArrayBuffer.isView(body)) { return null; } else if (typeof body.getBoundary === "function") { return `multipart/form-data;boundary=${body.getBoundary()}`; } else if (body instanceof Stream) { return null; } else { return "text/plain;charset=UTF-8"; } } function getTotalBytes(instance) { const body = instance.body; if (body === null) { return 0; } else if (isBlob(body)) { return body.size; } else if (Buffer.isBuffer(body)) { return body.length; } else if (body && typeof body.getLengthSync === "function") { if (body._lengthRetrievers && body._lengthRetrievers.length == 0 || // 1.x body.hasKnownLength && body.hasKnownLength()) { return body.getLengthSync(); } return null; } else { return null; } } function writeToStream(dest, instance) { const body = instance.body; if (body === null) { dest.end(); } else if (isBlob(body)) { body.stream().pipe(dest); } else if (Buffer.isBuffer(body)) { dest.write(body); dest.end(); } else { body.pipe(dest); } } Body.Promise = global.Promise; const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/; const 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`); } } function find(map, name) { name = name.toLowerCase(); for (const key in map) { if (key.toLowerCase() === name) { return key; } } return void 0; } const MAP = Symbol("map"); class Headers { /** * Headers class * * @param Object headers Response headers * @return Void */ constructor() { let init = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : void 0; this[MAP] = /* @__PURE__ */ Object.create(null); if (init instanceof Headers) { const rawHeaders = init.raw(); const headerNames = Object.keys(rawHeaders); for (const headerName of headerNames) { for (const value of rawHeaders[headerName]) { this.append(headerName, value); } } return; } if (init == null) ; else if (typeof init === "object") { const method = init[Symbol.iterator]; if (method != null) { if (typeof method !== "function") { throw new TypeError("Header pairs must be iterable"); } const pairs = []; for (const pair of init) { if (typeof pair !== "object" || typeof pair[Symbol.iterator] !== "function") { throw new TypeError("Each header pair must be iterable"); } pairs.push(Array.from(pair)); } for (const pair of pairs) { if (pair.length !== 2) { throw new TypeError("Each header pair must be a name/value tuple"); } this.append(pair[0], pair[1]); } } else { for (const key of Object.keys(init)) { const 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 */ get(name) { name = `${name}`; validateName(name); const key = find(this[MAP], name); if (key === void 0) { 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 */ forEach(callback) { let thisArg = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : void 0; let pairs = getHeaders(this); let i = 0; while (i < pairs.length) { var _pairs$i = pairs[i]; const 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 */ set(name, value) { name = `${name}`; value = `${value}`; validateName(name); validateValue(value); const key = find(this[MAP], name); this[MAP][key !== void 0 ? key : name] = [value]; } /** * Append a value onto existing header * * @param String name Header name * @param String value Header value * @return Void */ append(name, value) { name = `${name}`; value = `${value}`; validateName(name); validateValue(value); const key = find(this[MAP], name); if (key !== void 0) { this[MAP][key].push(value); } else { this[MAP][name] = [value]; } } /** * Check for header name existence * * @param String name Header name * @return Boolean */ has(name) { name = `${name}`; validateName(name); return find(this[MAP], name) !== void 0; } /** * Delete all header values given name * * @param String name Header name * @return Void */ delete(name) { name = `${name}`; validateName(name); const key = find(this[MAP], name); if (key !== void 0) { delete this[MAP][key]; } } /** * Return raw headers (non-spec api) * * @return Object */ raw() { return this[MAP]; } /** * Get an iterator on keys. * * @return Iterator */ keys() { return createHeadersIterator(this, "key"); } /** * Get an iterator on values. * * @return Iterator */ values() { return createHeadersIterator(this, "value"); } /** * Get an iterator on entries. * * This is the default iterator of the Headers object. * * @return Iterator */ [Symbol.iterator]() { return createHeadersIterator(this, "key+value"); } } 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) { let kind = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "key+value"; const 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(", ")]; }); } const INTERNAL = Symbol("internal"); function createHeadersIterator(target, kind) { const iterator = Object.create(HeadersIteratorPrototype); iterator[INTERNAL] = { target, kind, index: 0 }; return iterator; } const HeadersIteratorPrototype = Object.setPrototypeOf({ next() { if (!this || Object.getPrototypeOf(this) !== HeadersIteratorPrototype) { throw new TypeError("Value of `this` is not a HeadersIterator"); } var _INTERNAL = this[INTERNAL]; const target = _INTERNAL.target, kind = _INTERNAL.kind, index = _INTERNAL.index; const values = getHeaders(target, kind); const len = values.length; if (index >= len) { return { value: void 0, 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 }); function exportNodeCompatibleHeaders(headers) { const obj = Object.assign({ __proto__: null }, headers[MAP]); const hostHeaderKey = find(headers[MAP], "Host"); if (hostHeaderKey !== void 0) { obj[hostHeaderKey] = obj[hostHeaderKey][0]; } return obj; } function createHeadersLenient(obj) { const headers = new Headers(); for (const name of Object.keys(obj)) { if (invalidTokenRegex.test(name)) { continue; } if (Array.isArray(obj[name])) { for (const val of obj[name]) { if (invalidHeaderCharRegex.test(val)) { continue; } if (headers[MAP][name] === void 0) { headers[MAP][name] = [val]; } else { headers[MAP][name].push(val); } } } else if (!invalidHeaderCharRegex.test(obj[name])) { headers[MAP][name] = [obj[name]]; } } return headers; } const INTERNALS$1 = Symbol("Response internals"); const STATUS_CODES = http.STATUS_CODES; class Response { constructor() { let body = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null; let opts = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; Body.call(this, body, opts); const status = opts.status || 200; const headers = new Headers(opts.headers); if (body != null && !headers.has("Content-Type")) { const 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 }; } get url() { return this[INTERNALS$1].url || ""; } get status() { return this[INTERNALS$1].status; } /** * Convenience property representing if the request ended normally */ get ok() { return this[INTERNALS$1].status >= 200 && this[INTERNALS$1].status < 300; } get redirected() { return this[INTERNALS$1].counter > 0; } get statusText() { return this[INTERNALS$1].statusText; } get headers() { return this[INTERNALS$1].headers; } /** * Clone this response * * @return Response */ clone() { return new Response(clone(this), { url: this.url, status: this.status, statusText: this.statusText, headers: this.headers, ok: this.ok, redirected: this.redirected }); } } 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 }); const INTERNALS$2 = Symbol("Request internals"); const URL2 = Url.URL || whatwgUrl.URL; const parse_url = Url.parse; const format_url = Url.format; function parseURL(urlStr) { if (/^[a-zA-Z][a-zA-Z\d+\-.]*:/.exec(urlStr)) { urlStr = new URL2(urlStr).toString(); } return parse_url(urlStr); } const streamDestructionSupported = "destroy" in Stream.Readable.prototype; function isRequest(input) { return typeof input === "object" && typeof input[INTERNALS$2] === "object"; } function isAbortSignal(signal) { const proto = signal && typeof signal === "object" && Object.getPrototypeOf(signal); return !!(proto && proto.constructor.name === "AbortSignal"); } class Request { constructor(input) { let init = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}; let parsedURL; if (!isRequest(input)) { if (input && input.href) { parsedURL = parseURL(input.href); } else { parsedURL = parseURL(`${input}`); } input = {}; } else { parsedURL = parseURL(input.url); } let 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"); } let 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 }); const headers = new Headers(init.headers || input.headers || {}); if (inputBody != null && !headers.has("Content-Type")) { const contentType = extractContentType(inputBody); if (contentType) { headers.append("Content-Type", contentType); } } let 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$2] = { method, redirect: init.redirect || input.redirect || "follow", headers, parsedURL, signal }; this.follow = init.follow !== void 0 ? init.follow : input.follow !== void 0 ? input.follow : 20; this.compress = init.compress !== void 0 ? init.compress : input.compress !== void 0 ? input.compress : true; this.counter = init.counter || input.counter || 0; this.agent = init.agent || input.agent; } get method() { return this[INTERNALS$2].method; } get url() { return format_url(this[INTERNALS$2].parsedURL); } get headers() { return this[INTERNALS$2].headers; } get redirect() { return this[INTERNALS$2].redirect; } get signal() { return this[INTERNALS$2].signal; } /** * Clone this request * * @return Request */ clone() { return new Request(this); } } 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 } }); function getNodeRequestOptions(request) { const parsedURL = request[INTERNALS$2].parsedURL; const headers = new Headers(request[INTERNALS$2].headers); if (!headers.has("Accept")) { headers.set("Accept", "*/*"); } 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.Readable && !streamDestructionSupported) { throw new Error("Cancellation of streamed requests with AbortSignal is not supported in node < 8"); } let contentLengthValue = null; if (request.body == null && /^(POST|PUT)$/i.test(request.method)) { contentLengthValue = "0"; } if (request.body != null) { const totalBytes = getTotalBytes(request); if (typeof totalBytes === "number") { contentLengthValue = String(totalBytes); } } if (contentLengthValue) { headers.set("Content-Length", contentLengthValue); } if (!headers.has("User-Agent")) { headers.set("User-Agent", "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"); } if (request.compress && !headers.has("Accept-Encoding")) { headers.set("Accept-Encoding", "gzip,deflate"); } let agent = request.agent; if (typeof agent === "function") { agent = agent(parsedURL); } if (!headers.has("Connection") && !agent) { headers.set("Connection", "close"); } return Object.assign({}, parsedURL, { method: request.method, headers: exportNodeCompatibleHeaders(headers), agent }); } function AbortError(message) { Error.call(this, message); this.type = "aborted"; this.message = message; Error.captureStackTrace(this, this.constructor); } AbortError.prototype = Object.create(Error.prototype); AbortError.prototype.constructor = AbortError; AbortError.prototype.name = "AbortError"; const URL$1 = Url.URL || whatwgUrl.URL; const PassThrough$1 = Stream.PassThrough; const isDomainOrSubdomain = function isDomainOrSubdomain2(destination, original) { const orig = new URL$1(original).hostname; const dest = new URL$1(destination).hostname; return orig === dest || orig[orig.length - dest.length - 1] === "." && orig.endsWith(dest); }; function fetch2(url, opts) { if (!fetch2.Promise) { throw new Error("native promise missing, set fetch.Promise to your favorite alternative"); } Body.Promise = fetch2.Promise; return new fetch2.Promise(function(resolve, reject) { const request = new Request(url, opts); const options = getNodeRequestOptions(request); const send = (options.protocol === "https:" ? https : http).request; const signal = request.signal; let response = null; const abort = function abort2() { let error = new AbortError("The user aborted a request."); reject(error); if (request.body && request.body instanceof Stream.Readable) { request.body.destroy(error); } if (!response || !response.body) return; response.body.emit("error", error); }; if (signal && signal.aborted) { abort(); return; } const abortAndFinalize = function abortAndFinalize2() { abort(); finalize(); }; const req = send(options); let 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); const headers = createHeadersLenient(res.headers); if (fetch2.isRedirect(res.statusCode)) { const location2 = headers.get("Location"); let locationURL = null; try { locationURL = location2 === null ? null : new URL$1(location2, request.url).toString(); } catch (err) { if (request.redirect !== "manual") { reject(new FetchError(`uri requested responds with an invalid redirect URL: ${location2}`, "invalid-redirect")); finalize(); return; } } 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": if (locationURL !== null) { try { headers.set("Location", locationURL); } catch (err) { reject(err); } } break; case "follow": if (locationURL === null) { break; } if (request.counter >= request.follow) { reject(new FetchError(`maximum redirect reached at: ${request.url}`, "max-redirect")); finalize(); return; } const 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 (const name of ["authorization", "www-authenticate", "cookie", "cookie2"]) { requestOpts.headers.delete(name);