UNPKG

@tanglelabs/open-id-qr

Version:

Provides web components for oid4vc

81 lines 2.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const qr_code_styling_1 = __importDefault(require("qr-code-styling")); const ws_1 = require("../utils/ws"); class OpenIdQr extends HTMLElement { constructor() { super(); this.requestUri = ""; this.eventStreamUri = ""; this.size = 300; const shadow = this.attachShadow({ mode: "open" }); const canvasElement = document.createElement("div"); canvasElement.id = "qr-canvas"; shadow.appendChild(canvasElement); this.qrTarget = canvasElement; } connectedCallback() { const requestUri = this.getAttribute("requestUri"); if (!requestUri) throw new Error("`requestUri` is required"); const size = this.getAttribute("size"); this.requestUri = requestUri; this.eventStreamUri = this.getAttribute("eventStreamURI"); this.size = Number(size !== null && size !== void 0 ? size : this.size); const qr = new qr_code_styling_1.default({ type: "svg", width: this.size, height: this.size, data: this.requestUri, backgroundOptions: { color: "#ffffff", }, dotsOptions: { color: "#1d1d1d", }, }); qr.append(this.qrTarget); if (this.eventStreamUri) this.watchEventStream(this.eventStreamUri); } dispatchOpenIdEvent(message) { const event = new CustomEvent(`open-id-qr-${message.status}`, { bubbles: true, cancelable: true, detail: message, }); this.dispatchEvent(event); } watchSSEStream(uri) { const eventSource = new EventSource(uri); eventSource.onmessage = (event) => { const data = JSON.parse(event.data); this.dispatchOpenIdEvent(data); }; } watchWebSocketStream(uri) { const ws = (0, ws_1.createWebsocket)(uri); ws.onmessage = (event) => { const data = JSON.parse(event.data); this.dispatchOpenIdEvent(data); }; } watchEventStream(uri) { if (uri.startsWith("wss://") || uri.startsWith("ws://")) { this.watchWebSocketStream(uri); } else if (uri.startsWith("http://") || uri.startsWith("https://")) { this.watchSSEStream(uri); } else { const protocol = uri.split("://")[0]; throw new Error(`protocol \`${protocol}\` is not supported`); } } disconnectedCallback() { } } customElements.define("open-id-qr", OpenIdQr); //# sourceMappingURL=qr.js.map