UNPKG

appium-xcuitest-driver

Version:

Appium driver for iOS using XCUITest for backend

84 lines 3.42 kB
"use strict"; /* * derived from jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createJSCookie = createJSCookie; exports.createJWPCookie = createJWPCookie; exports.getValue = getValue; exports.expireCookie = expireCookie; // needed to communicate/translate between JSONWire cookies and regular JavaScript cookies const lodash_1 = __importDefault(require("lodash")); const support_1 = require("appium/support"); const log = support_1.logger.getLogger('Cookie'); // parses the value if needed and converts the value if a converter is provided // internal function, not exported function convertCookie(value, converter) { if (value.indexOf('"') === 0) { // this is a quoted cookied according to RFC2068 // remove enclosing quotes and internal quotes and backslashes value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); } let parsedValue; try { parsedValue = decodeURIComponent(value.replace(/\+/g, ' ')); } catch (e) { // no need to fail if we can't decode log.warn(e); } return converter ? converter(parsedValue) : parsedValue; } // takes arguments given and creates a JavaScript Cookie function createJSCookie(key, value, options = {}) { return [ encodeURIComponent(key), '=', value, options.expires ? `; expires=${options.expires}` : '', options.path ? `; path=${options.path}` : '', options.domain ? `; domain=${options.domain}` : '', options.secure ? '; secure' : '', ].join(''); } // takes the JavaScript cookieString and translates it into a JSONWire formatted cookie function createJWPCookie(key, cookieString, converter = null) { let result = {}; let cookies = cookieString ? cookieString.split('; ') : []; for (let cookie of cookies) { let parts = cookie.split('='); // get the first and second element as name and value let name = decodeURIComponent(parts.shift()); let val = parts[0]; // if name is key, this is the central element of the cookie, so add as `name` // otherwise it is an optional element if (key && key === name) { result.name = key; result.value = convertCookie(val, converter); } else { result[name] = convertCookie(val, converter); } } return result; } // takes a JavaScript cookiestring and parses it for the value given the key function getValue(key, cookieString, converter = null) { let result = createJWPCookie(key, cookieString, converter); // if `key` is undefined we want the entire cookie return lodash_1.default.isUndefined(key) ? result : result.value; } // returns a cookie that expires on 01 Jan 1970 // assign the returned cookie to an existing cookie to delete that cookie function expireCookie(key, options) { // override `expires` in `options`, and then make the cookie return createJSCookie(key, '', lodash_1.default.assign({}, options, { expires: 'Thu, 01 Jan 1970 00:00:00 GMT', })); } exports.default = { createJSCookie, createJWPCookie, getValue, expireCookie }; //# sourceMappingURL=cookies.js.map