@slide-computer/signer-web
Version:
JavaScript and TypeScript library to communicate with web signers on the Internet Computer
61 lines • 4.33 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _PostMessageTransport_options;
import {} from "@slide-computer/signer";
import { PostMessageChannel } from "./postMessageChannel";
import { urlIsSecureContext } from "../utils";
import { HeartbeatClient } from "./heartbeat";
const NON_CLICK_ESTABLISHMENT_LINK = "https://github.com/slide-computer/signer-js/blob/main/packages/signer-web/README.md#channels-must-be-established-in-a-click-handler";
export class PostMessageTransportError extends Error {
constructor(message) {
super(message);
Object.setPrototypeOf(this, PostMessageTransportError.prototype);
}
}
// Boolean that tracks click events to check if the popup is opened within a click context
let withinClick = false;
if (globalThis.window) {
globalThis.window.addEventListener("click", () => (withinClick = true), true);
globalThis.window.addEventListener("click", () => (withinClick = false));
}
export class PostMessageTransport {
constructor(options) {
_PostMessageTransport_options.set(this, void 0);
if (!urlIsSecureContext(options.url)) {
throw new PostMessageTransportError("Invalid signer RPC url");
}
__classPrivateFieldSet(this, _PostMessageTransport_options, Object.assign({ windowOpenerFeatures: "", window: globalThis.window, establishTimeout: 120000, disconnectTimeout: 2000, statusPollingRate: 300, crypto: globalThis.crypto, manageFocus: true, closeOnEstablishTimeout: true, detectNonClickEstablishment: true }, options), "f");
}
async establishChannel() {
if (__classPrivateFieldGet(this, _PostMessageTransport_options, "f").detectNonClickEstablishment && !withinClick) {
throw new PostMessageTransportError(`Signer window should not be opened outside of click handler, see: ${NON_CLICK_ESTABLISHMENT_LINK}`);
}
const signerWindow = __classPrivateFieldGet(this, _PostMessageTransport_options, "f").window.open(__classPrivateFieldGet(this, _PostMessageTransport_options, "f").url, "signerWindow", __classPrivateFieldGet(this, _PostMessageTransport_options, "f").windowOpenerFeatures);
if (!signerWindow) {
throw new PostMessageTransportError("Signer window could not be opened");
}
return new Promise((resolve, reject) => {
let channel;
new HeartbeatClient(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _PostMessageTransport_options, "f")), { signerWindow, onEstablish: (origin) => {
channel = new PostMessageChannel(Object.assign(Object.assign({}, __classPrivateFieldGet(this, _PostMessageTransport_options, "f")), { signerOrigin: origin, signerWindow: signerWindow }));
resolve(channel);
}, onEstablishTimeout: () => {
if (__classPrivateFieldGet(this, _PostMessageTransport_options, "f").closeOnEstablishTimeout) {
signerWindow.close();
}
reject(new PostMessageTransportError("Communication channel could not be established within a reasonable time"));
}, onDisconnect: () => channel.close() }));
});
}
}
_PostMessageTransport_options = new WeakMap();
//# sourceMappingURL=postMessageTransport.js.map