UNPKG

teko-oauth2

Version:

Teko Identity OAuth 2 Javascript Library for Web App Client

196 lines (163 loc) 6.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; require("regenerator-runtime/runtime"); var _constants = require("./constants"); function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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); return Constructor; } // callback should be a promise, which first parameter is the EventMessage. // It will be executed whenever IframeWindow listens to a new message, // It should resolve or reject in 3 cases: // 1. Resolve empty value if the message is not the one the callback wants to listen. // 2. Resolve data, which is the data IframeWindow wants to return after call navigate. // 3. Reject error, which is the error IframeWindow wants to throw if there are errors when handles the message. var IframeWindow = /*#__PURE__*/function () { function IframeWindow(options) { _classCallCheck(this, IframeWindow); options = options || {}; var _options = options, src = _options.src, callback = _options.callback, _options$cleanupFrame = _options.cleanupFrame, cleanupFrame = _options$cleanupFrame === void 0 ? true : _options$cleanupFrame; if (!src) throw new Error('No src provided'); this._callback = callback; this._cleanupFrame = cleanupFrame; this._frame = window.document.createElement('iframe'); this._frame.src = src; this._frame.style.visibility = 'hidden'; this._frame.style.position = 'absolute'; this._frame.style.display = 'none'; this._frame.style.width = 0; this._frame.style.height = 0; window.document.body.appendChild(this._frame); this._listening = false; } _createClass(IframeWindow, [{ key: "postMessage", value: function postMessage(message, targetOrigin) { if (this._frame) { this._frame.contentWindow.postMessage(message, targetOrigin); } } }, { key: "listen", value: function () { var _listen = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var _this = this; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!this._listening) { _context.next = 2; break; } return _context.abrupt("return", this._promise); case 2: if (this._frame) { _context.next = 4; break; } throw new Error('Cannot listen message after frame unmounted'); case 4: this._listening = true; this._promise = new Promise(function (resolve, reject) { _this._resolve = resolve; _this._reject = reject; }); this._boundMessageEvent = this._onMessage.bind(this); window.addEventListener('message', this._boundMessageEvent, false); this._timer = window.setTimeout(function () { _this._error(new Error('Frame window timed out')); }, _constants.DEFAULT_IFRAME_MESSAGE_TIMEOUT * 1000); return _context.abrupt("return", this._promise); case 10: case "end": return _context.stop(); } } }, _callee, this); })); function listen() { return _listen.apply(this, arguments); } return listen; }() }, { key: "_cleanup", value: function _cleanup() { this._listening = false; window.removeEventListener('message', this._boundMessageEvent, false); window.clearTimeout(this._timer); if (this._cleanupFrame && this._frame) { window.document.body.removeChild(this._frame); this._frame = null; this._callback = null; } this._timer = null; } }, { key: "_success", value: function _success(data) { this._cleanup(); this._resolve(data); } }, { key: "_error", value: function _error(err) { this._cleanup(); this._reject(err); } }, { key: "_onMessage", value: function () { var _onMessage2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(e) { var data; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.prev = 0; _context2.next = 3; return this._callback(e); case 3: data = _context2.sent; if (!(data !== undefined)) { _context2.next = 6; break; } return _context2.abrupt("return", this._success(data)); case 6: _context2.next = 11; break; case 8: _context2.prev = 8; _context2.t0 = _context2["catch"](0); return _context2.abrupt("return", this._error(_context2.t0)); case 11: case "end": return _context2.stop(); } } }, _callee2, this, [[0, 8]]); })); function _onMessage(_x) { return _onMessage2.apply(this, arguments); } return _onMessage; }() }, { key: "frame", get: function get() { return this._frame; } }]); return IframeWindow; }(); exports["default"] = IframeWindow;