UNPKG

@daml/hub-react

Version:

Daml React functions for Daml Hub

119 lines 5.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.usePolling = exports.asyncFileReader = exports.delay = exports.getCookieValue = exports.deleteCookie = exports.isRunningOnHub = exports.damlHubEnvironment = exports.detectAppDomainType = exports.DomainType = void 0; var react_1 = __importDefault(require("react")); var log_1 = __importDefault(require("./log")); var DomainType; (function (DomainType) { DomainType[DomainType["APP_DOMAIN"] = 0] = "APP_DOMAIN"; DomainType[DomainType["LOCALHOST"] = 1] = "LOCALHOST"; DomainType[DomainType["NON_HUB_DOMAIN"] = 2] = "NON_HUB_DOMAIN"; })(DomainType = exports.DomainType || (exports.DomainType = {})); var detectAppDomainType = function () { var hn = window.location.hostname; if (hn.includes('daml') && hn.includes('.app')) { (0, log_1.default)('domain').debug('App running on daml.app domain'); return DomainType.APP_DOMAIN; } else if (hn.includes('localhost') || /^127(\.\d{1,3}){3}$/.exec(hn)) { (0, log_1.default)('domain').debug('App running on localhost'); return DomainType.LOCALHOST; } else { (0, log_1.default)('domain').debug('App UI does not seem to be running on Daml Hub'); return DomainType.NON_HUB_DOMAIN; } }; exports.detectAppDomainType = detectAppDomainType; /** * Fetch information about the Daml Hub environment that * the library is running against. Includes hostname, * baseURL, wsURL, and a ledgerId (if discoverable). * * Set 'nonHubDomain' to true if running your app on a hub ledger with a * ledger not provided by Daml Hub. * * Returns undefined if not running on Hub */ var damlHubEnvironment = function (nonHubDomain) { if (nonHubDomain === void 0) { nonHubDomain = false; } var hostname = window.location.hostname.split('.').slice(1).join('.'); var ledgerId = undefined; var baseURL = hubBaseURL(); var wsURL = hubWsURL(); return (0, exports.isRunningOnHub)(nonHubDomain) ? { hostname: hostname, baseURL: baseURL, wsURL: wsURL, ledgerId: ledgerId } : undefined; }; exports.damlHubEnvironment = damlHubEnvironment; var hubBaseURL = function () { var domainType = (0, exports.detectAppDomainType)(); return domainType === DomainType.APP_DOMAIN || domainType === DomainType.NON_HUB_DOMAIN ? "".concat(window.location.origin, "/") : undefined; }; var hubWsURL = function () { var domainType = (0, exports.detectAppDomainType)(); return domainType === DomainType.APP_DOMAIN || domainType === DomainType.NON_HUB_DOMAIN ? "wss://".concat(window.location.hostname, "/") : undefined; }; /** * Determine if the app is running on Daml Hub via domain detection. Set 'nonHubDomain' to * true if running your app on a hub ledger with a ledger not provided by Daml Hub * @returns boolean */ var isRunningOnHub = function (nonHubDomain) { if (nonHubDomain === void 0) { nonHubDomain = false; } var domainType = (0, exports.detectAppDomainType)(); return nonHubDomain ? domainType !== DomainType.LOCALHOST : domainType === DomainType.APP_DOMAIN; }; exports.isRunningOnHub = isRunningOnHub; var deleteCookie = function (name, domain) { var base = "".concat(name, "=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; secure;"); if (domain) { base = base + "domain=".concat(domain, ";"); } document.cookie = base; }; exports.deleteCookie = deleteCookie; var getCookieValue = function (name) { var _a; return (_a = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')) === null || _a === void 0 ? void 0 : _a.pop(); }; exports.getCookieValue = getCookieValue; var delay = function (ms) { return new Promise(function (res) { return setTimeout(res, ms); }); }; exports.delay = delay; var asyncFileReader = function (file) { return new Promise(function (resolve, reject) { var reader = new FileReader(); reader.onload = function (event) { if (event.target && typeof event.target.result === 'string') { resolve(event.target.result); } }; reader.onerror = function (event) { reject(event); }; reader.readAsText(file); }); }; exports.asyncFileReader = asyncFileReader; var usePolling = function (fn, interval, nonHubDomain) { if (nonHubDomain === void 0) { nonHubDomain = false; } react_1.default.useEffect(function () { if (!(0, exports.isRunningOnHub)(nonHubDomain)) { (0, log_1.default)('polling').debug('Disabling polling, app is not running on Daml Hub'); return function () { }; } else if (interval > 0) { fn(); var intervalID_1 = setInterval(fn, interval); return function () { return clearInterval(intervalID_1); }; } else { fn(); // Run once return function () { }; } }, [fn, interval]); }; exports.usePolling = usePolling; //# sourceMappingURL=utils.js.map