UNPKG

amazon-cognito-identity-js

Version:

Amazon Cognito Identity Provider JavaScript SDK

218 lines (213 loc) • 8.77 kB
"use strict"; exports.__esModule = true; exports["default"] = void 0; require("isomorphic-unfetch"); var _UserAgent = require("./UserAgent"); function _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); } function _wrapNativeSuper(t) { var r = "function" == typeof Map ? new Map() : void 0; return _wrapNativeSuper = function _wrapNativeSuper(t) { if (null === t || !_isNativeFunction(t)) return t; if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function"); if (void 0 !== r) { if (r.has(t)) return r.get(t); r.set(t, Wrapper); } function Wrapper() { return _construct(t, arguments, _getPrototypeOf(this).constructor); } return Wrapper.prototype = Object.create(t.prototype, { constructor: { value: Wrapper, enumerable: !1, writable: !0, configurable: !0 } }), _setPrototypeOf(Wrapper, t); }, _wrapNativeSuper(t); } function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _isNativeFunction(t) { try { return -1 !== Function.toString.call(t).indexOf("[native code]"); } catch (n) { return "function" == typeof t; } } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } var CognitoError = /*#__PURE__*/function (_Error) { function CognitoError(message, code, name, statusCode) { var _this; _this = _Error.call(this, message) || this; _this.code = code; _this.name = name; _this.statusCode = statusCode; return _this; } _inheritsLoose(CognitoError, _Error); return CognitoError; }(/*#__PURE__*/_wrapNativeSuper(Error)); /** @class */ var Client = exports["default"] = /*#__PURE__*/function () { /** * Constructs a new AWS Cognito Identity Provider client object * @param {string} region AWS region * @param {string} endpoint endpoint * @param {object} fetchOptions options for fetch API (only credentials is supported) */ function Client(region, endpoint, fetchOptions) { this.endpoint = endpoint || "https://cognito-idp." + region + ".amazonaws.com/"; var _ref = fetchOptions || {}, credentials = _ref.credentials; this.fetchOptions = credentials ? { credentials: credentials } : {}; } /** * Makes an unauthenticated request on AWS Cognito Identity Provider API * using fetch * @param {string} operation API operation * @param {object} params Input parameters * @returns Promise<object> */ var _proto = Client.prototype; _proto.promisifyRequest = function promisifyRequest(operation, params) { var _this2 = this; return new Promise(function (resolve, reject) { _this2.request(operation, params, function (err, data) { if (err) { reject(new CognitoError(err.message, err.code, err.name, err.statusCode)); } else { resolve(data); } }); }); }; _proto.requestWithRetry = function requestWithRetry(operation, params, callback) { var _this3 = this; var MAX_DELAY_IN_MILLIS = 5 * 1000; jitteredExponentialRetry(function (p) { return new Promise(function (res, rej) { _this3.request(operation, p, function (error, result) { if (error) { rej(error); } else { res(result); } }); }); }, [params], MAX_DELAY_IN_MILLIS).then(function (result) { return callback(null, result); })["catch"](function (error) { return callback(error); }); } /** * Makes an unauthenticated request on AWS Cognito Identity Provider API * using fetch * @param {string} operation API operation * @param {object} params Input parameters * @param {function} callback Callback called when a response is returned * @returns {void} */; _proto.request = function request(operation, params, callback) { var headers = { 'Content-Type': 'application/x-amz-json-1.1', 'X-Amz-Target': "AWSCognitoIdentityProviderService." + operation, 'X-Amz-User-Agent': (0, _UserAgent.getAmplifyUserAgent)(), 'Cache-Control': 'no-store' }; var options = Object.assign({}, this.fetchOptions, { headers: headers, method: 'POST', mode: 'cors', body: JSON.stringify(params) }); var response; var responseJsonData; fetch(this.endpoint, options).then(function (resp) { response = resp; return resp; }, function (err) { // If error happens here, the request failed // if it is TypeError throw network error if (err instanceof TypeError) { throw new Error('Network error'); } throw err; }).then(function (resp) { return resp.json()["catch"](function () { return {}; }); }).then(function (data) { // return parsed body stream if (response.ok) return callback(null, data); responseJsonData = data; // Taken from aws-sdk-js/lib/protocol/json.js // eslint-disable-next-line no-underscore-dangle var code = (data.__type || data.code).split('#').pop(); var error = new Error(data.message || data.Message || null); error.name = code; error.code = code; return callback(error); })["catch"](function (err) { // first check if we have a service error if (response && response.headers && response.headers.get('x-amzn-errortype')) { try { var code = response.headers.get('x-amzn-errortype').split(':')[0]; var error = new Error(response.status ? response.status.toString() : null); error.code = code; error.name = code; error.statusCode = response.status; return callback(error); } catch (ex) { return callback(err); } // otherwise check if error is Network error } else if (err instanceof Error && err.message === 'Network error') { err.code = 'NetworkError'; } return callback(err); }); }; return Client; }(); var logger = { debug: function debug() { // Intentionally blank. This package doesn't have logging } }; /** * For now, all errors are retryable. */ var NonRetryableError = /*#__PURE__*/function (_Error2) { function NonRetryableError(message) { var _this4; _this4 = _Error2.call(this, message) || this; _this4.nonRetryable = true; return _this4; } _inheritsLoose(NonRetryableError, _Error2); return NonRetryableError; }(/*#__PURE__*/_wrapNativeSuper(Error)); var isNonRetryableError = function isNonRetryableError(obj) { var key = 'nonRetryable'; return obj && obj[key]; }; function retry(functionToRetry, args, delayFn, attempt) { if (attempt === void 0) { attempt = 1; } if (typeof functionToRetry !== 'function') { throw Error('functionToRetry must be a function'); } logger.debug(functionToRetry.name + " attempt #" + attempt + " with args: " + JSON.stringify(args)); return functionToRetry.apply(void 0, args)["catch"](function (err) { logger.debug("error on " + functionToRetry.name, err); if (isNonRetryableError(err)) { logger.debug(functionToRetry.name + " non retryable error", err); throw err; } var retryIn = delayFn(attempt, args, err); logger.debug(functionToRetry.name + " retrying in " + retryIn + " ms"); if (retryIn !== false) { return new Promise(function (res) { return setTimeout(res, retryIn); }).then(function () { return retry(functionToRetry, args, delayFn, attempt + 1); }); } else { throw err; } }); } function jitteredBackoff(maxDelayMs) { var BASE_TIME_MS = 100; var JITTER_FACTOR = 100; return function (attempt) { var delay = Math.pow(2, attempt) * BASE_TIME_MS + JITTER_FACTOR * Math.random(); return delay > maxDelayMs ? false : delay; }; } var MAX_DELAY_MS = 5 * 60 * 1000; function jitteredExponentialRetry(functionToRetry, args, maxDelayMs) { if (maxDelayMs === void 0) { maxDelayMs = MAX_DELAY_MS; } return retry(functionToRetry, args, jitteredBackoff(maxDelayMs)); }