@learncard/chapi-plugin 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1574 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/utils.js
5
+ var RPC_ERRORS = {
6
+ ParseError: {
7
+ message: "Parse error",
8
+ code: -32700
9
+ },
10
+ InvalidRequest: {
11
+ message: "Invalid Request",
12
+ code: -32600
13
+ },
14
+ MethodNotFound: {
15
+ message: "Method not found",
16
+ code: -32601
17
+ },
18
+ InvalidParams: {
19
+ message: "Invalid params",
20
+ code: -32602
21
+ },
22
+ InternalError: {
23
+ message: "Internal Error",
24
+ code: -32603
25
+ },
26
+ ServerError: {
27
+ message: "Server error",
28
+ code: -32e3
29
+ }
30
+ };
31
+ function parseUrl(url, base) {
32
+ if (base === void 0) {
33
+ base = window.location.href;
34
+ }
35
+ if (typeof URL === "function") {
36
+ return new URL(url, base);
37
+ }
38
+ if (typeof url !== "string") {
39
+ throw new TypeError('"url" must be a string.');
40
+ }
41
+ if (!url.includes(":")) {
42
+ if (base.startsWith("http") && !url.startsWith("/")) {
43
+ url = base + "/" + url;
44
+ } else {
45
+ url = base + url;
46
+ }
47
+ }
48
+ const parser = document.createElement("a");
49
+ parser.href = url;
50
+ let origin = (parser.protocol || window.location.protocol) + "//";
51
+ if (parser.host) {
52
+ if (parser.protocol === "http:" && parser.port === "80" || parser.protocol === "https:" && parser.port === "443") {
53
+ origin += parser.hostname;
54
+ } else {
55
+ origin += parser.host;
56
+ }
57
+ } else {
58
+ origin += window.location.host;
59
+ }
60
+ let pathname = parser.pathname;
61
+ if (!pathname.startsWith("/")) {
62
+ pathname = "/" + pathname;
63
+ }
64
+ return {
65
+ host: parser.host || window.location.host,
66
+ hostname: parser.hostname,
67
+ origin,
68
+ protocol: parser.protocol,
69
+ pathname
70
+ };
71
+ }
72
+ __name(parseUrl, "parseUrl");
73
+ function uuidv4(a, b) {
74
+ for (b = a = ""; a++ < 36; b += a * 51 & 52 ? (a ^ 15 ? 8 ^ Math.random() * (a ^ 20 ? 16 : 4) : 4).toString(16) : "-")
75
+ ;
76
+ return b;
77
+ }
78
+ __name(uuidv4, "uuidv4");
79
+ function isValidMessage(message) {
80
+ return message && typeof message === "object" && message.jsonrpc === "2.0" && message.id && typeof message.id === "string";
81
+ }
82
+ __name(isValidMessage, "isValidMessage");
83
+ function isValidRequest(message) {
84
+ return isValidMessage(message) && Array.isArray(message.params);
85
+ }
86
+ __name(isValidRequest, "isValidRequest");
87
+ function isValidResponse(message) {
88
+ return isValidMessage(message) && !!("result" in message ^ "error" in message) && (!("error" in message) || isValidError(message.error));
89
+ }
90
+ __name(isValidResponse, "isValidResponse");
91
+ function isValidError(error) {
92
+ return error && typeof error === "object" && typeof error.code === "number" && typeof error.message === "string";
93
+ }
94
+ __name(isValidError, "isValidError");
95
+ function serializeError(error) {
96
+ const err = {
97
+ message: error.message
98
+ };
99
+ if (error.constructor.name !== "Error") {
100
+ err.constructor = error.constructor.name;
101
+ }
102
+ if ("name" in error) {
103
+ err.name = error.name;
104
+ }
105
+ if ("code" in error) {
106
+ err.code = error.code;
107
+ } else {
108
+ err.code = RPC_ERRORS.ServerError.code;
109
+ }
110
+ if ("details" in error) {
111
+ err.details = error.details;
112
+ }
113
+ return err;
114
+ }
115
+ __name(serializeError, "serializeError");
116
+ function deserializeError(error) {
117
+ let err;
118
+ if (error.constructor === "DOMException") {
119
+ err = new DOMException(error.message, error.name);
120
+ } else {
121
+ err = new Error(error.message);
122
+ if ("code" in error) {
123
+ err.code = error.code;
124
+ }
125
+ }
126
+ if (error.details) {
127
+ err.details = error.details;
128
+ }
129
+ return err;
130
+ }
131
+ __name(deserializeError, "deserializeError");
132
+ function createMessageListener({ listener: listener2, origin, handle, expectRequest }) {
133
+ if (isHandlePromise(handle)) {
134
+ const promise = handle;
135
+ handle = false;
136
+ promise.then((h) => handle = h);
137
+ }
138
+ return (e) => {
139
+ if (!(e.source === handle && e.origin === origin && (expectRequest && isValidRequest(e.data) || !expectRequest && isValidResponse(e.data)))) {
140
+ return;
141
+ }
142
+ listener2(e.data, e);
143
+ };
144
+ }
145
+ __name(createMessageListener, "createMessageListener");
146
+ function destructureMethodName(fqMethodName) {
147
+ let [name, ...rest] = fqMethodName.split(".");
148
+ const method = rest.pop();
149
+ name = [name, ...rest].join(".");
150
+ return { name, method };
151
+ }
152
+ __name(destructureMethodName, "destructureMethodName");
153
+ function isHandlePromise(handle) {
154
+ try {
155
+ return typeof handle.then === "function";
156
+ } catch (e) {
157
+ }
158
+ return false;
159
+ }
160
+ __name(isHandlePromise, "isHandlePromise");
161
+
162
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/Client.js
163
+ var RPC_CLIENT_CALL_TIMEOUT = 3e4;
164
+ var Client = class {
165
+ constructor() {
166
+ this.origin = null;
167
+ this._handle = null;
168
+ this._listener = null;
169
+ this._pending = /* @__PURE__ */ new Map();
170
+ }
171
+ async connect(origin, options) {
172
+ if (this._listener) {
173
+ throw new Error("Already connected.");
174
+ }
175
+ options = options || {};
176
+ const self = this;
177
+ self.origin = parseUrl(origin).origin;
178
+ self._handle = options.handle || window.opener || window.parent;
179
+ const pending = self._pending;
180
+ self._listener = createMessageListener({
181
+ origin: self.origin,
182
+ handle: self._handle,
183
+ expectRequest: false,
184
+ listener: (message) => {
185
+ if (!pending.has(message.id)) {
186
+ return;
187
+ }
188
+ const { resolve, reject, cancelTimeout } = pending.get(message.id);
189
+ cancelTimeout();
190
+ if ("result" in message) {
191
+ return resolve(message.result);
192
+ }
193
+ reject(deserializeError(message.error));
194
+ }
195
+ });
196
+ window.addEventListener("message", self._listener);
197
+ return new Injector(self);
198
+ }
199
+ async send(qualifiedMethodName, parameters, {
200
+ timeout = RPC_CLIENT_CALL_TIMEOUT
201
+ }) {
202
+ if (!this._listener) {
203
+ throw new Error("RPC client not connected.");
204
+ }
205
+ const self = this;
206
+ const message = {
207
+ jsonrpc: "2.0",
208
+ id: uuidv4(),
209
+ method: qualifiedMethodName,
210
+ params: parameters
211
+ };
212
+ if (isHandlePromise(self._handle)) {
213
+ const handle = await self._handle;
214
+ handle.postMessage(message, self.origin);
215
+ } else {
216
+ self._handle.postMessage(message, self.origin);
217
+ }
218
+ return new Promise((resolve, reject) => {
219
+ const pending = self._pending;
220
+ let cancelTimeout;
221
+ if (timeout > 0) {
222
+ const timeoutId = setTimeout(() => {
223
+ pending.delete(message.id);
224
+ reject(new Error("RPC call timed out."));
225
+ }, timeout);
226
+ cancelTimeout = /* @__PURE__ */ __name(() => {
227
+ pending.delete(message.id);
228
+ clearTimeout(timeoutId);
229
+ }, "cancelTimeout");
230
+ } else {
231
+ cancelTimeout = /* @__PURE__ */ __name(() => {
232
+ pending.delete(message.id);
233
+ }, "cancelTimeout");
234
+ }
235
+ pending.set(message.id, { resolve, reject, cancelTimeout });
236
+ });
237
+ }
238
+ close() {
239
+ if (this._listener) {
240
+ window.removeEventListener("message", this._listener);
241
+ this._handle = this.origin = this._listener = null;
242
+ for (const value of this._pending.values()) {
243
+ value.reject(new Error("RPC client closed."));
244
+ }
245
+ this._pending = /* @__PURE__ */ new Map();
246
+ }
247
+ }
248
+ };
249
+ __name(Client, "Client");
250
+ var Injector = class {
251
+ constructor(client) {
252
+ this.client = client;
253
+ this._apis = /* @__PURE__ */ new Map();
254
+ }
255
+ define(name, definition) {
256
+ if (!(name && typeof name === "string")) {
257
+ throw new TypeError("`name` must be a non-empty string.");
258
+ }
259
+ if (!(definition && typeof definition === "object" && Array.isArray(definition.functions))) {
260
+ throw new TypeError(
261
+ "`definition.function` must be an array of function names or function definition objects to be defined."
262
+ );
263
+ }
264
+ const self = this;
265
+ const api = {};
266
+ definition.functions.forEach((fn) => {
267
+ if (typeof fn === "string") {
268
+ fn = { name: fn, options: {} };
269
+ }
270
+ api[fn.name] = async function() {
271
+ return self.client.send(
272
+ name + "." + fn.name,
273
+ [...arguments],
274
+ fn.options
275
+ );
276
+ };
277
+ });
278
+ self._apis[name] = api;
279
+ return api;
280
+ }
281
+ get(name, definition) {
282
+ const api = this._apis[name];
283
+ if (!api) {
284
+ if (definition) {
285
+ return this.define(name, definition);
286
+ }
287
+ throw new Error(`API "${name}" has not been defined.`);
288
+ }
289
+ return this._apis[name];
290
+ }
291
+ };
292
+ __name(Injector, "Injector");
293
+
294
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/EventEmitter.js
295
+ var EventEmitter = class {
296
+ constructor({ deserialize = /* @__PURE__ */ __name((e) => e, "deserialize"), waitUntil = /* @__PURE__ */ __name(async () => {
297
+ }, "waitUntil") } = {}) {
298
+ this._listeners = [];
299
+ this._deserialize = deserialize;
300
+ this._waitUntil = waitUntil;
301
+ }
302
+ async emit(event) {
303
+ event = this._deserialize(event);
304
+ (this._listeners[event.type] || []).forEach((l) => l(event));
305
+ return this._waitUntil(event);
306
+ }
307
+ addEventListener(eventType, fn) {
308
+ if (!this._listeners[eventType]) {
309
+ this._listeners[eventType] = [fn];
310
+ } else {
311
+ this._listeners[eventType].push(fn);
312
+ }
313
+ }
314
+ removeEventListener(eventType, fn) {
315
+ const listeners = this._listeners[eventType];
316
+ if (!listeners) {
317
+ return;
318
+ }
319
+ const idx = listeners.indexOf(fn);
320
+ if (idx !== -1) {
321
+ listeners.splice(idx, 1);
322
+ }
323
+ }
324
+ };
325
+ __name(EventEmitter, "EventEmitter");
326
+
327
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/Server.js
328
+ var Server = class {
329
+ constructor() {
330
+ this.origin = null;
331
+ this._handle = null;
332
+ this._apis = /* @__PURE__ */ new Map();
333
+ }
334
+ define(name, api) {
335
+ if (!(name && typeof name === "string")) {
336
+ throw new TypeError("`name` must be a non-empty string.");
337
+ }
338
+ if (!(api && api !== "object")) {
339
+ throw new TypeError("`api` must be an object.");
340
+ }
341
+ if (name in this._apis) {
342
+ throw new Error(`The "${name}" API is already defined.`);
343
+ }
344
+ this._apis[name] = api;
345
+ }
346
+ async listen(origin, options) {
347
+ if (this._listener) {
348
+ throw new Error("Already listening.");
349
+ }
350
+ options = options || {};
351
+ const self = this;
352
+ self.origin = parseUrl(origin).origin;
353
+ self._handle = options.handle || window.opener || window.parent;
354
+ const ignoreUnknownApi = options.ignoreUnknownApi === "true" || false;
355
+ self._listener = createMessageListener({
356
+ origin: self.origin,
357
+ handle: self._handle,
358
+ expectRequest: true,
359
+ listener: (message) => {
360
+ const { name, method } = destructureMethodName(message.method);
361
+ const api = self._apis[name];
362
+ if (method && method.startsWith("_")) {
363
+ return sendMethodNotFound(self._handle, self.origin, message);
364
+ }
365
+ if (!api && ignoreUnknownApi) {
366
+ return;
367
+ }
368
+ if (!api || typeof api[method] !== "function") {
369
+ return sendMethodNotFound(self._handle, self.origin, message);
370
+ }
371
+ const fn = api[method];
372
+ (async () => {
373
+ const response = {
374
+ jsonrpc: "2.0",
375
+ id: message.id
376
+ };
377
+ try {
378
+ response.result = await fn.apply(api, message.params);
379
+ } catch (e) {
380
+ response.error = serializeError(e);
381
+ }
382
+ if (self._handle) {
383
+ if (isHandlePromise(self._handle)) {
384
+ self._handle.then((h) => h.postMessage(response, self.origin));
385
+ } else {
386
+ self._handle.postMessage(response, self.origin);
387
+ }
388
+ }
389
+ })();
390
+ }
391
+ });
392
+ window.addEventListener("message", self._listener);
393
+ }
394
+ close() {
395
+ if (this._listener) {
396
+ window.removeEventListener("message", this._listener);
397
+ this._handle = this.origin = this._listener = null;
398
+ }
399
+ }
400
+ };
401
+ __name(Server, "Server");
402
+ function sendMethodNotFound(handle, origin, message) {
403
+ const response = {
404
+ jsonrpc: "2.0",
405
+ id: message.id,
406
+ error: Object.assign({}, RPC_ERRORS.MethodNotFound)
407
+ };
408
+ if (isHandlePromise(handle)) {
409
+ return handle.then((h) => h.postMessage(response, origin));
410
+ } else {
411
+ return handle.postMessage(response, origin);
412
+ }
413
+ }
414
+ __name(sendMethodNotFound, "sendMethodNotFound");
415
+
416
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/WebApp.js
417
+ var WebApp = class {
418
+ constructor(relyingOrigin) {
419
+ this.relyingOrigin = parseUrl(relyingOrigin).origin;
420
+ this.client = null;
421
+ this.injector = null;
422
+ this.client = new Client();
423
+ this.server = new Server();
424
+ this._control = null;
425
+ this._connected = false;
426
+ }
427
+ async connect() {
428
+ this.injector = await this.client.connect(this.relyingOrigin);
429
+ this._connected = true;
430
+ this._control = this.injector.define("core.control", {
431
+ functions: ["ready", "show", "hide"]
432
+ });
433
+ this.server.listen(this.relyingOrigin);
434
+ return this.injector;
435
+ }
436
+ async ready() {
437
+ if (!this._connected) {
438
+ throw new Error('WebApp not connected. Did you call ".connect()"?');
439
+ }
440
+ await this._control.ready();
441
+ return this;
442
+ }
443
+ close() {
444
+ if (this._connected) {
445
+ this.server.close();
446
+ this.client.close();
447
+ this._connected = false;
448
+ }
449
+ }
450
+ async show() {
451
+ if (!this._connected) {
452
+ throw new Error(
453
+ 'Cannot "show" yet; not connected. Did you call ".connect()"?'
454
+ );
455
+ }
456
+ return this._control.show();
457
+ }
458
+ async hide() {
459
+ if (!this._connected) {
460
+ throw new Error(
461
+ 'Cannot "hide" yet; not connected. Did you call ".connect()?"'
462
+ );
463
+ }
464
+ return this._control.hide();
465
+ }
466
+ };
467
+ __name(WebApp, "WebApp");
468
+
469
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/WebAppWindowDialog.js
470
+ var WebAppWindowDialog = class {
471
+ constructor() {
472
+ this._closeEventListeners = /* @__PURE__ */ new Set();
473
+ }
474
+ addEventListener(name, listener2) {
475
+ if (name !== "close") {
476
+ throw new Error(`Unknown event "${name}".`);
477
+ }
478
+ if (typeof listener2 !== "function") {
479
+ throw new TypeError('"listener" must be a function.');
480
+ }
481
+ this._closeEventListeners.add(listener2);
482
+ }
483
+ removeEventListener(name, listener2) {
484
+ if (name !== "close") {
485
+ throw new Error(`Unknown event "${name}".`);
486
+ }
487
+ if (typeof listener2 !== "function") {
488
+ throw new TypeError('"listener" must be a function.');
489
+ }
490
+ this._closeEventListeners.delete(listener2);
491
+ }
492
+ show() {
493
+ }
494
+ close() {
495
+ for (const listener2 of this._closeEventListeners) {
496
+ listener2({});
497
+ }
498
+ }
499
+ destroy() {
500
+ this._closeEventListeners.clear();
501
+ }
502
+ };
503
+ __name(WebAppWindowDialog, "WebAppWindowDialog");
504
+
505
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/WebAppWindowInlineDialog.js
506
+ var WebAppWindowInlineDialog = class extends WebAppWindowDialog {
507
+ constructor({ url, handle, className }) {
508
+ super();
509
+ this.url = url;
510
+ this.handle = handle;
511
+ this.dialog = document.createElement("dialog");
512
+ applyStyle(this.dialog, {
513
+ position: "fixed",
514
+ top: 0,
515
+ left: 0,
516
+ width: "100%",
517
+ height: "100%",
518
+ "max-width": "100%",
519
+ "max-height": "100%",
520
+ display: "none",
521
+ margin: 0,
522
+ padding: 0,
523
+ border: "none",
524
+ background: "transparent",
525
+ color: "black",
526
+ "box-sizing": "border-box",
527
+ overflow: "hidden",
528
+ "user-select": "none",
529
+ "z-index": 1e6
530
+ });
531
+ this.dialog.className = "web-app-window";
532
+ if (typeof className === "string") {
533
+ this.dialog.className = this.dialog.className + " " + className;
534
+ }
535
+ const style = document.createElement("style");
536
+ style.appendChild(
537
+ document.createTextNode(`dialog.web-app-window::backdrop {
538
+ background-color: transparent;
539
+ }`)
540
+ );
541
+ this.container = document.createElement("div");
542
+ applyStyle(this.container, {
543
+ position: "relative",
544
+ width: "100%",
545
+ height: "100%",
546
+ margin: 0,
547
+ padding: 0,
548
+ display: "flex",
549
+ "flex-direction": "column"
550
+ });
551
+ this.container.className = "web-app-window-backdrop";
552
+ this.iframe = document.createElement("iframe");
553
+ this.iframe.src = url;
554
+ this.iframe.scrolling = "auto";
555
+ applyStyle(this.iframe, {
556
+ position: "fixed",
557
+ top: 0,
558
+ left: 0,
559
+ width: "100%",
560
+ height: "100%",
561
+ border: "none",
562
+ background: "transparent",
563
+ overflow: "hidden",
564
+ margin: 0,
565
+ padding: 0,
566
+ "flex-grow": 1,
567
+ "user-select": "none"
568
+ });
569
+ this.dialog.appendChild(style);
570
+ this.container.appendChild(this.iframe);
571
+ this.dialog.appendChild(this.container);
572
+ this.dialog.addEventListener("cancel", (e) => {
573
+ e.preventDefault();
574
+ this.hide();
575
+ });
576
+ document.body.appendChild(this.dialog);
577
+ this.handle = this.iframe.contentWindow;
578
+ }
579
+ show() {
580
+ this.dialog.style.display = "block";
581
+ if (this.dialog.showModal) {
582
+ this.dialog.showModal();
583
+ }
584
+ try {
585
+ this.dialog.style.pointerEvents = "none";
586
+ } catch (e) {
587
+ }
588
+ setTimeout(() => {
589
+ try {
590
+ this.dialog.style.pointerEvents = "";
591
+ } catch (e) {
592
+ }
593
+ }, 32);
594
+ }
595
+ close() {
596
+ this.dialog.style.display = "none";
597
+ if (this.dialog.close) {
598
+ try {
599
+ this.dialog.close();
600
+ } catch (e) {
601
+ console.error(e);
602
+ }
603
+ }
604
+ super.close();
605
+ }
606
+ destroy() {
607
+ this.dialog.parentNode.removeChild(this.dialog);
608
+ super.destroy();
609
+ }
610
+ };
611
+ __name(WebAppWindowInlineDialog, "WebAppWindowInlineDialog");
612
+ function applyStyle(element, style) {
613
+ for (const name in style) {
614
+ element.style[name] = style[name];
615
+ }
616
+ }
617
+ __name(applyStyle, "applyStyle");
618
+
619
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/WebAppWindowPopupDialog.js
620
+ var WebAppWindowPopupDialog = class extends WebAppWindowDialog {
621
+ constructor({ url, handle, bounds = { width: 500, height: 400 } }) {
622
+ super();
623
+ this.url = url;
624
+ this.handle = handle;
625
+ this._locationChanging = false;
626
+ if (!handle) {
627
+ this._openWindow({ url, name: "web-app-window", bounds });
628
+ }
629
+ this.destroyed = false;
630
+ this._removeListeners = () => {
631
+ };
632
+ }
633
+ show() {
634
+ }
635
+ close() {
636
+ this.destroy();
637
+ }
638
+ destroy() {
639
+ if (this.handle && !this.destroyed) {
640
+ this.handle.close();
641
+ super.close();
642
+ this.handle = null;
643
+ this.destroyed = true;
644
+ this._removeListeners();
645
+ super.destroy();
646
+ }
647
+ }
648
+ isClosed() {
649
+ return !this.handle || this.handle.closed;
650
+ }
651
+ _openWindow({ url, name, bounds }) {
652
+ const { x, y } = bounds;
653
+ let { width = 500, height = 400 } = bounds;
654
+ width = Math.min(width, window.innerWidth);
655
+ height = Math.min(height, window.innerHeight);
656
+ const left = Math.floor(x !== void 0 ? x : window.screenX + (window.innerWidth - width) / 2);
657
+ const top = Math.floor(y !== void 0 ? y : window.screenY + (window.innerHeight - height) / 2);
658
+ const features = `popup=yes,menubar=no,location=no,resizable=no,scrollbars=no,status=no,width=${width},height=${height},left=${left},top=${top}`;
659
+ this._locationChanging = true;
660
+ this.handle = window.open(url, name, features);
661
+ this._addListeners();
662
+ }
663
+ setLocation(url) {
664
+ this.url = url;
665
+ this._locationChanging = true;
666
+ this.handle.location.replace(url);
667
+ }
668
+ _addListeners() {
669
+ const destroyDialog = /* @__PURE__ */ __name(() => this.destroy(), "destroyDialog");
670
+ const loadDialog = /* @__PURE__ */ __name(() => {
671
+ this._locationChanging = false;
672
+ }, "loadDialog");
673
+ const unloadDialog = /* @__PURE__ */ __name(() => {
674
+ if (this._locationChanging) {
675
+ return;
676
+ }
677
+ this.destroy();
678
+ }, "unloadDialog");
679
+ this.handle.addEventListener("unload", unloadDialog);
680
+ this.handle.addEventListener("load", loadDialog);
681
+ window.addEventListener("beforeUnload", destroyDialog, { once: true });
682
+ const intervalId = setInterval(() => {
683
+ if (this.isClosed()) {
684
+ this.destroy();
685
+ clearInterval(intervalId);
686
+ }
687
+ }, 250);
688
+ this._removeListeners = () => {
689
+ clearInterval(intervalId);
690
+ this.handle.removeListener("unload", unloadDialog);
691
+ this.handle.removeListener("load", loadDialog);
692
+ window.removeEventListener("beforeUnload", destroyDialog);
693
+ };
694
+ }
695
+ };
696
+ __name(WebAppWindowPopupDialog, "WebAppWindowPopupDialog");
697
+
698
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/WebAppWindow.js
699
+ var LOAD_WINDOW_TIMEOUT = 6e4;
700
+ var WebAppWindow = class {
701
+ constructor(url, {
702
+ timeout = LOAD_WINDOW_TIMEOUT,
703
+ dialog = null,
704
+ handle,
705
+ popup = false,
706
+ className = null,
707
+ customize = null,
708
+ bounds
709
+ } = {}) {
710
+ this.visible = false;
711
+ this.dialog = dialog;
712
+ this.handle = null;
713
+ this.popup = popup;
714
+ this.windowControl = null;
715
+ this._destroyed = false;
716
+ this._ready = false;
717
+ this._private = {};
718
+ this._timeoutId = null;
719
+ if (handle && handle._dialog) {
720
+ this.dialog = dialog = handle._dialog;
721
+ }
722
+ this._private._readyPromise = new Promise((resolve, reject) => {
723
+ this._timeoutId = setTimeout(
724
+ () => reject(new DOMException(
725
+ "Loading Web application window timed out.",
726
+ "TimeoutError"
727
+ )),
728
+ timeout
729
+ );
730
+ this._private._resolveReady = (value) => {
731
+ clearTimeout(this.timeoutId);
732
+ this._timeoutId = null;
733
+ resolve(value);
734
+ };
735
+ this._private._rejectReady = (err) => {
736
+ clearTimeout(this.timeoutId);
737
+ this._timeoutId = null;
738
+ reject(err);
739
+ };
740
+ });
741
+ this._private.isReady = async () => {
742
+ return this._private._readyPromise;
743
+ };
744
+ this._private.destroy = () => {
745
+ if (this._timeoutId) {
746
+ this._private._rejectReady(new DOMException(
747
+ "Web application window closed before ready.",
748
+ "AbortError"
749
+ ));
750
+ }
751
+ if (!this._destroyed) {
752
+ this.dialog.destroy();
753
+ this.dialog = null;
754
+ this._destroyed = true;
755
+ }
756
+ };
757
+ if (customize) {
758
+ if (!typeof customize === "function") {
759
+ throw new TypeError("`options.customize` must be a function.");
760
+ }
761
+ }
762
+ if (!this.dialog) {
763
+ if (this.popup) {
764
+ this.dialog = new WebAppWindowPopupDialog({ url, handle, bounds });
765
+ } else {
766
+ this.dialog = new WebAppWindowInlineDialog({ url, handle, className });
767
+ }
768
+ }
769
+ if (this.popup && bounds) {
770
+ let { x, y, width = 500, height = 400 } = bounds;
771
+ width = Math.min(width, window.innerWidth);
772
+ height = Math.min(height + 30, window.innerHeight);
773
+ x = Math.floor(x !== void 0 ? x : window.screenX + (window.innerWidth - width) / 2);
774
+ y = Math.floor(y !== void 0 ? y : window.screenY + (window.innerHeight - height) / 2 + 15);
775
+ this.dialog.handle.resizeTo(width, height);
776
+ this.dialog.handle.moveTo(x, y);
777
+ }
778
+ this.handle = this.dialog.handle;
779
+ if (customize) {
780
+ try {
781
+ customize({
782
+ dialog: this.dialog.dialog,
783
+ container: this.dialog.container,
784
+ iframe: this.dialog.iframe,
785
+ webAppWindow: this
786
+ });
787
+ } catch (e) {
788
+ console.error(e);
789
+ }
790
+ }
791
+ }
792
+ ready() {
793
+ this._ready = true;
794
+ this._private._resolveReady(true);
795
+ }
796
+ show() {
797
+ if (!this.visible) {
798
+ this.visible = true;
799
+ const body = document.querySelector("body");
800
+ this._bodyOverflowStyle = body.style.overflow;
801
+ body.style.overflow = "hidden";
802
+ if (!this._destroyed) {
803
+ this.dialog.show();
804
+ } else if (this.windowControl.show) {
805
+ this.windowControl.show();
806
+ }
807
+ }
808
+ }
809
+ hide() {
810
+ if (this.visible) {
811
+ this.visible = false;
812
+ const body = document.querySelector("body");
813
+ if (this._bodyOverflowStyle) {
814
+ body.style.overflow = this._bodyOverflowStyle;
815
+ } else {
816
+ body.style.overflow = "";
817
+ }
818
+ if (!this._destroyed) {
819
+ this.dialog.close();
820
+ } else if (this.windowControl.hide) {
821
+ this.windowControl.hide();
822
+ }
823
+ }
824
+ }
825
+ };
826
+ __name(WebAppWindow, "WebAppWindow");
827
+
828
+ // ../../../node_modules/.pnpm/web-request-rpc@2.0.3/node_modules/web-request-rpc/WebAppContext.js
829
+ var WEB_APP_CONTEXT_LOAD_TIMEOUT = 1e4;
830
+ var WebAppContext = class {
831
+ constructor() {
832
+ this.client = new Client();
833
+ this.server = new Server();
834
+ this.injector = null;
835
+ this.control = null;
836
+ this.loaded = false;
837
+ this.closed = false;
838
+ }
839
+ async createWindow(url, {
840
+ timeout = WEB_APP_CONTEXT_LOAD_TIMEOUT,
841
+ iframe,
842
+ dialog = null,
843
+ popup = false,
844
+ handle,
845
+ windowControl,
846
+ className,
847
+ customize,
848
+ bounds
849
+ } = {}) {
850
+ if (this.loaded) {
851
+ throw new Error("AppContext already loaded.");
852
+ }
853
+ this.loaded = true;
854
+ this.control = new WebAppWindow(url, {
855
+ timeout,
856
+ dialog,
857
+ iframe,
858
+ popup,
859
+ handle,
860
+ windowControl,
861
+ className,
862
+ customize,
863
+ bounds
864
+ });
865
+ window.addEventListener("pagehide", () => this.close(), { once: true });
866
+ this.server.define("core.control", this.control);
867
+ const origin = parseUrl(url).origin;
868
+ this.server.listen(origin, {
869
+ handle: this.control.handle,
870
+ ignoreUnknownApi: true
871
+ });
872
+ await this.control._private.isReady();
873
+ this.injector = await this.client.connect(origin, {
874
+ handle: this.control.handle
875
+ });
876
+ return this.injector;
877
+ }
878
+ close() {
879
+ if (!this.closed) {
880
+ this.closed = true;
881
+ this.control._private.destroy();
882
+ this.server.close();
883
+ this.client.close();
884
+ }
885
+ }
886
+ };
887
+ __name(WebAppContext, "WebAppContext");
888
+
889
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialRequestEvent.js
890
+ var CredentialRequestEvent = class {
891
+ constructor({
892
+ credentialHandler,
893
+ credentialRequestOrigin,
894
+ credentialRequestOptions,
895
+ hintKey
896
+ }) {
897
+ this.type = "credentialrequest";
898
+ this._credentialHandler = credentialHandler;
899
+ this.credentialRequestOrigin = credentialRequestOrigin;
900
+ this.credentialRequestOptions = credentialRequestOptions;
901
+ this.hintKey = hintKey;
902
+ }
903
+ async openWindow(url) {
904
+ await this._credentialHandler.show();
905
+ const appWindow = new WebAppWindow(url, {
906
+ className: "credential-handler"
907
+ });
908
+ appWindow.ready();
909
+ appWindow.show();
910
+ appWindow.handle._dialog = appWindow.dialog;
911
+ return appWindow.handle;
912
+ }
913
+ respondWith(handlerResponse) {
914
+ this._promise = handlerResponse;
915
+ }
916
+ };
917
+ __name(CredentialRequestEvent, "CredentialRequestEvent");
918
+
919
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialStoreEvent.js
920
+ var CredentialStoreEvent = class {
921
+ constructor({
922
+ credentialHandler,
923
+ credentialRequestOrigin,
924
+ credential,
925
+ hintKey
926
+ }) {
927
+ this.type = "credentialstore";
928
+ this._credentialHandler = credentialHandler;
929
+ this.credentialRequestOrigin = credentialRequestOrigin;
930
+ this.credential = credential;
931
+ this.hintKey = hintKey;
932
+ }
933
+ async openWindow(url) {
934
+ await this._credentialHandler.show();
935
+ const appWindow = new WebAppWindow(url);
936
+ appWindow.ready();
937
+ appWindow.show();
938
+ appWindow.handle._dialog = appWindow.dialog;
939
+ return appWindow.handle;
940
+ }
941
+ respondWith(handlerResponse) {
942
+ this._promise = handlerResponse;
943
+ }
944
+ };
945
+ __name(CredentialStoreEvent, "CredentialStoreEvent");
946
+
947
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialHandlerService.js
948
+ var CredentialHandlerService = class {
949
+ constructor(credentialHandler) {
950
+ this._credentialHandler = credentialHandler;
951
+ }
952
+ async request(credentialRequestEvent) {
953
+ return await this._credentialHandler._emitter.emit(
954
+ new CredentialRequestEvent(Object.assign(
955
+ { credentialHandler: this._credentialHandler },
956
+ credentialRequestEvent
957
+ ))
958
+ );
959
+ }
960
+ async store(credentialStoreEvent) {
961
+ return await this._credentialHandler._emitter.emit(
962
+ new CredentialStoreEvent(Object.assign(
963
+ { credentialHandler: this._credentialHandler },
964
+ credentialStoreEvent
965
+ ))
966
+ );
967
+ }
968
+ };
969
+ __name(CredentialHandlerService, "CredentialHandlerService");
970
+
971
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialHandler.js
972
+ var EVENT_TYPES = ["credentialrequest", "credentialstore"];
973
+ var CredentialHandler = class extends WebApp {
974
+ constructor(mediatorOrigin, inline = false) {
975
+ if (typeof mediatorOrigin !== "string") {
976
+ throw new TypeError('"mediatorOrigin" must be a string.');
977
+ }
978
+ super(mediatorOrigin, inline);
979
+ this._emitter = new EventEmitter({
980
+ async waitUntil(event) {
981
+ return event._promise || Promise.reject(
982
+ new DOMException(
983
+ 'No "credentialrequest" event handler found.',
984
+ "NotFoundError"
985
+ )
986
+ );
987
+ }
988
+ });
989
+ }
990
+ async connect() {
991
+ const injector = await super.connect();
992
+ this.server.define("credentialHandler", new CredentialHandlerService(this));
993
+ await this.ready();
994
+ return injector;
995
+ }
996
+ addEventListener(eventType, fn) {
997
+ if (!EVENT_TYPES.includes(eventType)) {
998
+ throw new DOMException(
999
+ `Unsupported event type "${eventType}"`,
1000
+ "NotSupportedError"
1001
+ );
1002
+ }
1003
+ return this._emitter.addEventListener(eventType, fn);
1004
+ }
1005
+ removeEventListener(eventType, fn) {
1006
+ if (!EVENT_TYPES.includes(eventType)) {
1007
+ throw new DOMException(
1008
+ `Unsupported event type "${eventType}"`,
1009
+ "NotSupportedError"
1010
+ );
1011
+ }
1012
+ return this._emitter.removeEventListener(eventType, fn);
1013
+ }
1014
+ };
1015
+ __name(CredentialHandler, "CredentialHandler");
1016
+
1017
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialHints.js
1018
+ var CredentialHints = class {
1019
+ constructor(url, injector) {
1020
+ const remote = injector.get("credentialHints", {
1021
+ functions: ["delete", "get", "keys", "has", "set", "clear"]
1022
+ });
1023
+ for (let methodName in remote) {
1024
+ if (methodName !== "set") {
1025
+ const method = remote[methodName].bind(this, url);
1026
+ this[methodName] = function(...args) {
1027
+ this._deprecateNotice();
1028
+ return method(...args);
1029
+ };
1030
+ }
1031
+ }
1032
+ this._remoteSet = remote.set.bind(this, url);
1033
+ }
1034
+ async set(hintKey, credentialHint) {
1035
+ this._deprecateNotice();
1036
+ credentialHint.icons = credentialHint.icons || [];
1037
+ const promises = credentialHint.icons.map((icon) => imageToDataUrl(icon.src).then((fetchedImage) => {
1038
+ icon.fetchedImage = fetchedImage;
1039
+ }));
1040
+ await Promise.all(promises);
1041
+ return this._remoteSet(hintKey, credentialHint);
1042
+ }
1043
+ _deprecateNotice() {
1044
+ console.warn("Credential hints are deprecated and no longer used.");
1045
+ }
1046
+ };
1047
+ __name(CredentialHints, "CredentialHints");
1048
+ function imageToDataUrl(url) {
1049
+ return new Promise((resolve) => {
1050
+ const img = new Image();
1051
+ img.crossOrigin = "Anonymous";
1052
+ img.onload = () => {
1053
+ let canvas = document.createElement("canvas");
1054
+ const ctx = canvas.getContext("2d");
1055
+ canvas.height = img.height;
1056
+ canvas.width = img.width;
1057
+ ctx.drawImage(img, 0, 0);
1058
+ const dataUrl = canvas.toDataURL();
1059
+ resolve(dataUrl);
1060
+ canvas = null;
1061
+ };
1062
+ img.onerror = () => resolve(null);
1063
+ img.src = url;
1064
+ });
1065
+ }
1066
+ __name(imageToDataUrl, "imageToDataUrl");
1067
+
1068
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialManager.js
1069
+ var CredentialManager = class {
1070
+ constructor(url, injector) {
1071
+ if (!(url && typeof url === "string")) {
1072
+ throw new TypeError('"url" must be a non-empty string.');
1073
+ }
1074
+ this.hints = new CredentialHints(url, injector);
1075
+ }
1076
+ static async requestPermission() {
1077
+ const status = await navigator.credentialsPolyfill.permissions.request(
1078
+ { name: "credentialhandler" }
1079
+ );
1080
+ return status.state;
1081
+ }
1082
+ };
1083
+ __name(CredentialManager, "CredentialManager");
1084
+
1085
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialHandlerRegistration.js
1086
+ var CredentialHandlerRegistration = class {
1087
+ constructor(url, injector) {
1088
+ if (!(url && typeof url === "string")) {
1089
+ throw new TypeError('"url" must be a non-empty string.');
1090
+ }
1091
+ this.credentialManager = new CredentialManager(url, injector);
1092
+ }
1093
+ };
1094
+ __name(CredentialHandlerRegistration, "CredentialHandlerRegistration");
1095
+
1096
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialHandlers.js
1097
+ var CredentialHandlers = class {
1098
+ constructor(injector) {
1099
+ this._init = (async () => {
1100
+ this._injector = await injector;
1101
+ this._remote = this._injector.get("credentialHandlers", {
1102
+ functions: [
1103
+ "register",
1104
+ "unregister",
1105
+ "getRegistration",
1106
+ "hasRegistration"
1107
+ ]
1108
+ });
1109
+ })();
1110
+ }
1111
+ async register(url) {
1112
+ this._deprecateNotice();
1113
+ await this._init;
1114
+ url = await this._remote.register("credential", url);
1115
+ return new CredentialHandlerRegistration(url, this._injector);
1116
+ }
1117
+ async unregister(url) {
1118
+ this._deprecateNotice();
1119
+ await this._init;
1120
+ return this._remote.unregister("credential", url);
1121
+ }
1122
+ async getRegistration(url) {
1123
+ this._deprecateNotice();
1124
+ await this._init;
1125
+ url = await this._remote.getRegistration("credential", url);
1126
+ if (!url) {
1127
+ return null;
1128
+ }
1129
+ return new CredentialHandlerRegistration(url, this._injector);
1130
+ }
1131
+ async hasRegistration(url) {
1132
+ this._deprecateNotice();
1133
+ await this._init;
1134
+ return await this._remote.hasRegistration("credential", url);
1135
+ }
1136
+ _deprecateNotice() {
1137
+ console.warn(
1138
+ 'Credential handler registration APIs are deprecated. The credential handler specified in "manifest.json" is now automatically registered when a user grants permission to install a credential handler via "CredentialManager.requestPermission()".'
1139
+ );
1140
+ }
1141
+ };
1142
+ __name(CredentialHandlers, "CredentialHandlers");
1143
+
1144
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/WebCredential.js
1145
+ var WebCredential2 = class {
1146
+ constructor(dataType, data, { recommendedHandlerOrigins = [] } = {}) {
1147
+ if (typeof dataType !== "string") {
1148
+ throw new TypeError('"dataType" must be a string.');
1149
+ }
1150
+ this.type = "web";
1151
+ this.dataType = dataType;
1152
+ this.data = data;
1153
+ this.options = { recommendedHandlerOrigins };
1154
+ }
1155
+ };
1156
+ __name(WebCredential2, "WebCredential");
1157
+
1158
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/CredentialsContainer.js
1159
+ var CREDENTIAL_GET_TIMEOUT = 0;
1160
+ var CREDENTIAL_STORE_TIMEOUT = 0;
1161
+ var CredentialsContainer = class {
1162
+ constructor(injector) {
1163
+ this._nativeCredentialsContainer = {
1164
+ get: navigator.credentials && navigator.credentials.get && navigator.credentials.get.bind(navigator.credentials),
1165
+ store: navigator.credentials && navigator.credentials.store && navigator.credentials.store.bind(navigator.credentials)
1166
+ };
1167
+ this._init = (async () => {
1168
+ this._remote = (await injector).get("credentialsContainer", {
1169
+ functions: [
1170
+ { name: "get", options: { timeout: CREDENTIAL_GET_TIMEOUT } },
1171
+ { name: "store", options: { timeout: CREDENTIAL_STORE_TIMEOUT } }
1172
+ ]
1173
+ });
1174
+ })();
1175
+ }
1176
+ async get(options = {}) {
1177
+ if (options.web) {
1178
+ await this._init;
1179
+ const credential = await this._remote.get(options);
1180
+ if (!credential) {
1181
+ return null;
1182
+ }
1183
+ return new WebCredential2(credential.dataType, credential.data);
1184
+ }
1185
+ if (this._nativeCredentialsContainer.get) {
1186
+ return this._nativeCredentialsContainer.get(options);
1187
+ }
1188
+ throw new DOMException("Not implemented.", "NotSupportedError");
1189
+ }
1190
+ async store(credential) {
1191
+ if (credential instanceof WebCredential2) {
1192
+ await this._init;
1193
+ const result = await this._remote.store(credential);
1194
+ if (!result) {
1195
+ return null;
1196
+ }
1197
+ return new WebCredential2(result.dataType, result.data);
1198
+ }
1199
+ if (this._nativeCredentialsContainer.store) {
1200
+ return this._nativeCredentialsContainer.store(credential);
1201
+ }
1202
+ throw new DOMException("Not implemented.", "NotSupportedError");
1203
+ }
1204
+ };
1205
+ __name(CredentialsContainer, "CredentialsContainer");
1206
+
1207
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/PermissionManager.js
1208
+ var PERMISSION_REQUEST_TIMEOUT = 0;
1209
+ var PermissionManager = class {
1210
+ constructor(injector) {
1211
+ this._init = (async () => {
1212
+ this._remote = (await injector).get("permissionManager", {
1213
+ functions: [
1214
+ "query",
1215
+ { name: "request", options: { timeout: PERMISSION_REQUEST_TIMEOUT } },
1216
+ "revoke"
1217
+ ]
1218
+ });
1219
+ })();
1220
+ }
1221
+ async query(permissionDesc) {
1222
+ await this._init;
1223
+ return await this._remote.query(permissionDesc);
1224
+ }
1225
+ async request(permissionDesc) {
1226
+ await this._init;
1227
+ return await this._remote.request(permissionDesc);
1228
+ }
1229
+ async revoke(permissionDesc) {
1230
+ await this._init;
1231
+ return await this._remote.revoke(permissionDesc);
1232
+ }
1233
+ };
1234
+ __name(PermissionManager, "PermissionManager");
1235
+
1236
+ // ../../../node_modules/.pnpm/credential-handler-polyfill@3.1.0/node_modules/credential-handler-polyfill/index.js
1237
+ var DEFAULT_MEDIATOR_ORIGIN = "https://authn.io";
1238
+ var loaded;
1239
+ async function loadOnce(options) {
1240
+ if (loaded) {
1241
+ return loaded;
1242
+ }
1243
+ loaded = true;
1244
+ return load(options);
1245
+ }
1246
+ __name(loadOnce, "loadOnce");
1247
+ async function load(options = {
1248
+ mediatorOrigin: DEFAULT_MEDIATOR_ORIGIN
1249
+ }) {
1250
+ _assertSecureContext();
1251
+ let mediatorUrl;
1252
+ if (typeof options === "string") {
1253
+ mediatorUrl = options;
1254
+ } else if (options && typeof options === "object" && typeof options.mediatorOrigin === "string") {
1255
+ mediatorUrl = `${options.mediatorOrigin}/mediator`;
1256
+ } else {
1257
+ throw new Error(
1258
+ '"options.mediatorOrigin" must be a string expressing the origin of the mediator.'
1259
+ );
1260
+ }
1261
+ const appContext = new WebAppContext();
1262
+ const injector = appContext.createWindow(mediatorUrl, {
1263
+ className: "credential-mediator",
1264
+ timeout: 3e4
1265
+ });
1266
+ const style = document.createElement("style");
1267
+ style.appendChild(document.createTextNode(
1268
+ `dialog.web-app-window.credential-mediator > .web-app-window-backdrop {
1269
+ background-color: rgba(0, 0, 0, 0.25);
1270
+ }`
1271
+ ));
1272
+ document.body.appendChild(style);
1273
+ const polyfill = {};
1274
+ polyfill.permissions = new PermissionManager(injector);
1275
+ polyfill.CredentialHandlers = new CredentialHandlers(injector);
1276
+ polyfill.CredentialHandler = CredentialHandler;
1277
+ polyfill.CredentialManager = CredentialManager;
1278
+ polyfill.credentials = new CredentialsContainer(injector);
1279
+ polyfill.WebCredential = WebCredential2;
1280
+ navigator.credentialsPolyfill = polyfill;
1281
+ if ("credentials" in navigator) {
1282
+ navigator.credentials.get = polyfill.credentials.get.bind(
1283
+ polyfill.credentials
1284
+ );
1285
+ navigator.credentials.store = polyfill.credentials.store.bind(
1286
+ polyfill.credentials
1287
+ );
1288
+ } else {
1289
+ navigator.credentials = polyfill.credentials;
1290
+ }
1291
+ window.CredentialManager = CredentialManager;
1292
+ window.WebCredential = WebCredential2;
1293
+ return polyfill;
1294
+ }
1295
+ __name(load, "load");
1296
+ function _assertSecureContext() {
1297
+ if (!window.isSecureContext) {
1298
+ throw new DOMException("SecurityError", "The operation is insecure.");
1299
+ }
1300
+ }
1301
+ __name(_assertSecureContext, "_assertSecureContext");
1302
+
1303
+ // ../../../node_modules/.pnpm/web-credential-handler@2.0.2/node_modules/web-credential-handler/CredentialEventProxy.js
1304
+ var PROXY_EVENT_TIMEOUT = 6e4;
1305
+ var CredentialEventProxy = class extends WebApp {
1306
+ constructor() {
1307
+ super(window.location.origin);
1308
+ }
1309
+ async receive() {
1310
+ const self = this;
1311
+ await self.connect();
1312
+ return new Promise((resolveReceive, rejectReceive) => {
1313
+ const timeoutId = setTimeout(() => {
1314
+ rejectReceive(new Error("Timed out waiting to receive event."));
1315
+ }, PROXY_EVENT_TIMEOUT);
1316
+ self.server.define("credentialEventProxy", {
1317
+ async send(event) {
1318
+ resolveReceive(event);
1319
+ clearTimeout(timeoutId);
1320
+ return new Promise((resolveSend, rejectSend) => {
1321
+ event.respondWith = (promise) => {
1322
+ try {
1323
+ resolveSend(promise);
1324
+ } catch (e) {
1325
+ rejectSend(e);
1326
+ }
1327
+ };
1328
+ });
1329
+ }
1330
+ });
1331
+ self.ready();
1332
+ });
1333
+ }
1334
+ };
1335
+ __name(CredentialEventProxy, "CredentialEventProxy");
1336
+
1337
+ // ../../../node_modules/.pnpm/web-credential-handler@2.0.2/node_modules/web-credential-handler/index.js
1338
+ var DEFAULT_MEDIATOR = "https://authn.io";
1339
+ async function installHandler() {
1340
+ const CredentialManager2 = navigator.credentialsPolyfill.CredentialManager;
1341
+ const result = await CredentialManager2.requestPermission();
1342
+ if (result !== "granted") {
1343
+ throw new Error("Permission denied.");
1344
+ }
1345
+ }
1346
+ __name(installHandler, "installHandler");
1347
+ async function activateHandler({
1348
+ mediatorOrigin = DEFAULT_MEDIATOR,
1349
+ get,
1350
+ store
1351
+ }) {
1352
+ if (!(get || store)) {
1353
+ throw new Error('"get" or "store" function(s) must be specified.');
1354
+ }
1355
+ const CredentialHandler2 = navigator.credentialsPolyfill.CredentialHandler;
1356
+ const self = new CredentialHandler2(mediatorOrigin);
1357
+ if (get) {
1358
+ if (typeof get !== "function") {
1359
+ throw new TypeError('"get" must be a function.');
1360
+ }
1361
+ self.addEventListener("credentialrequest", (event) => listener({ event, get }));
1362
+ }
1363
+ if (store) {
1364
+ if (typeof store !== "function") {
1365
+ throw new TypeError('"store" must be a function.');
1366
+ }
1367
+ self.addEventListener("credentialstore", (event) => listener({ event, store }));
1368
+ }
1369
+ await self.connect();
1370
+ }
1371
+ __name(activateHandler, "activateHandler");
1372
+ async function receiveCredentialEvent() {
1373
+ const proxy = new CredentialEventProxy();
1374
+ return proxy.receive();
1375
+ }
1376
+ __name(receiveCredentialEvent, "receiveCredentialEvent");
1377
+ function listener({ event, get, store }) {
1378
+ event.respondWith(createResponse({ event, get, store }));
1379
+ }
1380
+ __name(listener, "listener");
1381
+ async function createResponse({ event, get, store }) {
1382
+ const result = await (get || store)({ event });
1383
+ if (!(result && typeof result === "object")) {
1384
+ throw new TypeError(
1385
+ 'Return value of "get" or "store" hook must be an object.'
1386
+ );
1387
+ }
1388
+ if (result.type === "response") {
1389
+ return { dataType: result.dataType, data: result.data };
1390
+ }
1391
+ if (result.type === "redirect") {
1392
+ const appContext = new WebAppContext();
1393
+ const handle = await event.openWindow(result.url);
1394
+ const windowReady = appContext.createWindow(result.url, {
1395
+ handle,
1396
+ popup: false,
1397
+ timeout: 6e5
1398
+ });
1399
+ const injector = await windowReady;
1400
+ const proxy = injector.get("credentialEventProxy", {
1401
+ functions: [{ name: "send", options: { timeout: 0 } }]
1402
+ });
1403
+ return proxy.send({
1404
+ type: event.type,
1405
+ credentialRequestOptions: event.credentialRequestOptions,
1406
+ credentialRequestOrigin: event.credentialRequestOrigin,
1407
+ credential: event.credential,
1408
+ hintKey: event.hintKey
1409
+ });
1410
+ }
1411
+ throw new Error(
1412
+ 'Return value of "get" or "store" must have a type of "response" or "redirect".'
1413
+ );
1414
+ }
1415
+ __name(createResponse, "createResponse");
1416
+
1417
+ // src/chapi.ts
1418
+ var getCHAPIPlugin = /* @__PURE__ */ __name(async () => {
1419
+ if (typeof window === "undefined") {
1420
+ return {
1421
+ name: "CHAPI",
1422
+ methods: {
1423
+ installChapiHandler: async () => {
1424
+ throw new Error("CHAPI is only available inside of a browser!");
1425
+ },
1426
+ activateChapiHandler: async () => {
1427
+ throw new Error("CHAPI is only available inside of a browser!");
1428
+ },
1429
+ receiveChapiEvent: async () => {
1430
+ throw new Error("CHAPI is only available inside of a browser!");
1431
+ },
1432
+ storeCredentialViaChapiDidAuth: async () => {
1433
+ throw new Error("CHAPI is only available inside of a browser!");
1434
+ },
1435
+ storePresentationViaChapi: async () => {
1436
+ throw new Error("CHAPI is only available inside of a browser!");
1437
+ }
1438
+ }
1439
+ };
1440
+ }
1441
+ await loadOnce();
1442
+ return {
1443
+ name: "CHAPI",
1444
+ displayName: "CHAPI",
1445
+ description: "Credential Handler API. Allows sending/retrieving credentials across wallets and issuers",
1446
+ methods: {
1447
+ installChapiHandler: async () => installHandler(),
1448
+ activateChapiHandler: async (_learnCard, {
1449
+ mediatorOrigin = `https://authn.io/mediator?${encodeURIComponent(
1450
+ window.location.origin
1451
+ )}`,
1452
+ get,
1453
+ store
1454
+ }) => {
1455
+ return activateHandler({ mediatorOrigin, get, store });
1456
+ },
1457
+ receiveChapiEvent: async () => receiveCredentialEvent(),
1458
+ storeCredentialViaChapiDidAuth: async (_learnCard, credential) => {
1459
+ const challenge = crypto.randomUUID();
1460
+ const domain = window.location.origin;
1461
+ const vpr = {
1462
+ web: {
1463
+ VerifiablePresentation: {
1464
+ query: { type: "DIDAuthentication" },
1465
+ challenge,
1466
+ domain
1467
+ }
1468
+ }
1469
+ };
1470
+ const res = await navigator.credentials.get(vpr);
1471
+ if (!res)
1472
+ return { success: false, reason: "did not auth" };
1473
+ const verification = await _learnCard.invoke.verifyPresentation(res.data, {
1474
+ challenge,
1475
+ domain,
1476
+ proofPurpose: "authentication"
1477
+ });
1478
+ if (verification.warnings.length > 0 || verification.errors.length > 0) {
1479
+ return { success: false, reason: "auth failed verification" };
1480
+ }
1481
+ const subject = res.data?.proof?.verificationMethod?.split("#")[0];
1482
+ if (!Array.isArray(credential.credentialSubject)) {
1483
+ credential.credentialSubject.id = subject;
1484
+ }
1485
+ const vp = await _learnCard.invoke.getTestVp(
1486
+ await _learnCard.invoke.issueCredential(credential)
1487
+ );
1488
+ const success = await _learnCard.invoke.storePresentationViaChapi(vp);
1489
+ if (success)
1490
+ return { success: true };
1491
+ return { success: false, reason: "did not store" };
1492
+ },
1493
+ storePresentationViaChapi: async (_learnCard, presentation) => {
1494
+ const wc = new WebCredential("VerifiablePresentation", presentation);
1495
+ return window.navigator.credentials.store(wc);
1496
+ }
1497
+ }
1498
+ };
1499
+ }, "getCHAPIPlugin");
1500
+ export {
1501
+ getCHAPIPlugin
1502
+ };
1503
+ /*!
1504
+ * A CredentialHandlerRegistration provides a CredentialManager to enable Web
1505
+ * apps to register Profiles that can be presented to websites.
1506
+ *
1507
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1508
+ */
1509
+ /*!
1510
+ * A CredentialRequestEvent is emitted when a request has been made for
1511
+ * credentials.
1512
+ *
1513
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1514
+ */
1515
+ /*!
1516
+ * A CredentialStoreEvent is emitted when a request has been made to
1517
+ * store a credential.
1518
+ *
1519
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1520
+ */
1521
+ /*!
1522
+ * A WebApp is a remote application that runs in a WebAppContext.
1523
+ *
1524
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1525
+ */
1526
+ /*!
1527
+ * A WebCredential is a Credential that can be retrieved from or stored by a
1528
+ * "credential handler" that runs in a third party Web application.
1529
+ *
1530
+ * Copyright (c) 2017-2021 Digital Bazaar, Inc. All rights reserved.
1531
+ */
1532
+ /*!
1533
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1534
+ */
1535
+ /*!
1536
+ * Copyright (c) 2017-2018 Digital Bazaar, Inc. All rights reserved.
1537
+ */
1538
+ /*!
1539
+ * Copyright (c) 2017-2022 Digital Bazaar, Inc. All rights reserved.
1540
+ */
1541
+ /*!
1542
+ * Copyright (c) 2017-2023 Digital Bazaar, Inc. All rights reserved.
1543
+ */
1544
+ /*!
1545
+ * Copyright (c) 2018-2022 Digital Bazaar, Inc. All rights reserved.
1546
+ */
1547
+ /*!
1548
+ * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved.
1549
+ */
1550
+ /*!
1551
+ * Copyright (c) 2022-2023 Digital Bazaar, Inc. All rights reserved.
1552
+ */
1553
+ /*!
1554
+ * JSON-RPC for Web Request Polyfills.
1555
+ *
1556
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1557
+ */
1558
+ /*!
1559
+ * The core CredentialHandler class.
1560
+ *
1561
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1562
+ */
1563
+ /*!
1564
+ * Utilities for Web Request RPC.
1565
+ *
1566
+ * Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
1567
+ */
1568
+ /*!
1569
+ * Wrapper for native CredentialsContainer that uses remote Credential Mediator
1570
+ * for WebCredential-related operations.
1571
+ *
1572
+ * Copyright (c) 2017-2018 Digital Bazaar, Inc. All rights reserved.
1573
+ */
1574
+ //# sourceMappingURL=chapi-plugin.esm.js.map