cabbie-async
Version:
An asynchronous webdriver client
369 lines (275 loc) • 11.6 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _cookie = require('./cookie');
var _driver = require('./driver');
var _driver2 = _interopRequireDefault(_driver);
var _addDebugging = require('./add-debugging');
var _addDebugging2 = _interopRequireDefault(_addDebugging);
var _url = require('url');
var _baseClass = require('./base-class');
var _baseClass2 = _interopRequireDefault(_baseClass);
var _flowRuntime = require('flow-runtime');
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
*
* This file is generated automatically, run npm run build to re-generate.
**/
var Driver = _flowRuntime2.default.tdz(function () {
return _driver2.default;
});
/*
* Validate the cookie data
*
* @private
*/
var Cookie = _flowRuntime2.default.tdz(function () {
return _cookie.Cookie;
});
function validateCookie(cookie /*: Cookie*/) {
var completed /*: boolean*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var _cookieType = _flowRuntime2.default.ref(Cookie);
var _completedType = _flowRuntime2.default.boolean();
_flowRuntime2.default.param('cookie', _cookieType).assert(cookie);
_flowRuntime2.default.param('completed', _completedType).assert(completed);
if (completed) {
if (!cookie.name) {
throw new Error('A cookie "name" is required.');
}
if (!cookie.value) {
throw new Error('a cookie "value" is required.');
}
}
if (!cookie.path) {
cookie.path = '/';
}
// localhost is a special case, the domain must be ""
if (cookie.domain === 'localhost') cookie.domain = '';
return cookie;
}
/*
* Managing cookie-storage
*/
_flowRuntime2.default.annotate(validateCookie, _flowRuntime2.default.function(_flowRuntime2.default.param('cookie', _flowRuntime2.default.ref(Cookie)), _flowRuntime2.default.param('completed', _flowRuntime2.default.boolean())));
var CookieStorage = function (_BaseClass) {
(0, _inherits3.default)(CookieStorage, _BaseClass);
function CookieStorage(driver /*: Driver*/) {
(0, _classCallCheck3.default)(this, CookieStorage);
var _driverType = _flowRuntime2.default.ref(Driver);
_flowRuntime2.default.param('driver', _driverType).assert(driver);
return (0, _possibleConstructorReturn3.default)(this, (CookieStorage.__proto__ || (0, _getPrototypeOf2.default)(CookieStorage)).call(this, driver, '/cookie'));
}
/*
* Retrieve all cookies visible to the current page.
*/
(0, _createClass3.default)(CookieStorage, [{
key: 'getCookies',
value: function getCookies() /*: Promise<Array<Cookie>>*/ {
var _returnType;
return _regenerator2.default.async(function getCookies$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_returnType = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.array(_flowRuntime2.default.ref(Cookie)), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.array(_flowRuntime2.default.ref(Cookie)))));
_context.t0 = _returnType;
_context.next = 4;
return _regenerator2.default.awrap(this.requestJSON('GET', ''));
case 4:
_context.t1 = _context.sent;
return _context.abrupt('return', _context.t0.assert.call(_context.t0, _context.t1));
case 6:
case 'end':
return _context.stop();
}
}
}, null, this);
}
/*
* Retrieve all cookie names visible to the current page.
*/
}, {
key: 'getKeys',
value: function getKeys() /*: Promise<Array<string>>*/ {
var _returnType2, cookies;
return _regenerator2.default.async(function getKeys$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.array(_flowRuntime2.default.string()), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.array(_flowRuntime2.default.string()))));
_context2.next = 3;
return _regenerator2.default.awrap(this.requestJSON('GET', ''));
case 3:
cookies = _context2.sent;
return _context2.abrupt('return', _returnType2.assert(cookies.map(function (cookie) {
return cookie.name;
})));
case 5:
case 'end':
return _context2.stop();
}
}
}, null, this);
}
/*
* Retrieve a specific cookie by name that is visible to the current page.
*/
}, {
key: 'getCookie',
value: function getCookie(name /*: string*/) /*: Promise<Cookie | void>*/ {
var _nameType, _returnType3, cookies;
return _regenerator2.default.async(function getCookie$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_nameType = _flowRuntime2.default.string();
_returnType3 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.union(_flowRuntime2.default.ref(Cookie), _flowRuntime2.default.void()), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.union(_flowRuntime2.default.ref(Cookie), _flowRuntime2.default.void()))));
_flowRuntime2.default.param('name', _nameType).assert(name);
_context3.next = 5;
return _regenerator2.default.awrap(this.getCookies());
case 5:
cookies = _context3.sent;
cookies = cookies.filter(function (cookie) {
return cookie.name == name;
});
return _context3.abrupt('return', _returnType3.assert(cookies.length ? cookies[0] : undefined));
case 8:
case 'end':
return _context3.stop();
}
}
}, null, this);
}
/*
* Set a cookie that is visible to the current page.
*/
}, {
key: 'setCookie',
value: function setCookie(cookie /*: Cookie*/) /*: Promise<void>*/ {
var _cookieType2, _returnType4, base, hostname;
return _regenerator2.default.async(function setCookie$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_cookieType2 = _flowRuntime2.default.ref(Cookie);
_returnType4 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_flowRuntime2.default.param('cookie', _cookieType2).assert(cookie);
validateCookie(cookie, true);
if (!(cookie.domain != null)) {
_context4.next = 9;
break;
}
_context4.next = 7;
return _regenerator2.default.awrap(this.requestJSON('POST', '', { cookie: cookie }));
case 7:
_context4.next = 19;
break;
case 9:
_context4.next = 11;
return _regenerator2.default.awrap(this.driver.activeWindow.getUrl());
case 11:
base = _context4.sent;
hostname = (0, _url.parse)(base).hostname;
if (!(hostname == null)) {
_context4.next = 15;
break;
}
throw new Error('The hostname should never be undefined. This should never happen.');
case 15:
cookie.domain = hostname;
validateCookie(cookie, true);
_context4.next = 19;
return _regenerator2.default.awrap(this.requestJSON('POST', '', { cookie: cookie }));
case 19:
case 'end':
return _context4.stop();
}
}
}, null, this);
}
/*
* Delete the cookie with the given name.
*/
}, {
key: 'removeCookie',
value: function removeCookie(name /*: string*/) /*: Promise<void>*/ {
var _nameType2, _returnType5;
return _regenerator2.default.async(function removeCookie$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_nameType2 = _flowRuntime2.default.string();
_returnType5 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_flowRuntime2.default.param('name', _nameType2).assert(name);
_context5.next = 5;
return _regenerator2.default.awrap(this.requestJSON('DELETE', '/' + name));
case 5:
case 'end':
return _context5.stop();
}
}
}, null, this);
}
/*
* Delete all cookies visible to the current page.
*/
}, {
key: 'clear',
value: function clear() /*: Promise<void>*/ {
var _returnType6;
return _regenerator2.default.async(function clear$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_returnType6 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void())));
_context6.next = 3;
return _regenerator2.default.awrap(this.requestJSON('DELETE', ''));
case 3:
case 'end':
return _context6.stop();
}
}
}, null, this);
}
/*
* Get the number of items in the storage
*/
}, {
key: 'getSize',
value: function getSize() /*: Promise<number>*/ {
var _returnType7, cookies;
return _regenerator2.default.async(function getSize$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_returnType7 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.number(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.number())));
_context7.next = 3;
return _regenerator2.default.awrap(this.getCookies());
case 3:
cookies = _context7.sent;
return _context7.abrupt('return', _returnType7.assert(cookies.length));
case 5:
case 'end':
return _context7.stop();
}
}
}, null, this);
}
}]);
return CookieStorage;
}(_baseClass2.default);
(0, _addDebugging2.default)(CookieStorage);
exports.default = CookieStorage;