niimbot-canvas-sdk 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,36 +1,683 @@
1
- var d = Object.defineProperty;
2
- var g = (n, t, e) => t in n ? d(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
- var i = (n, t, e) => (g(n, typeof t != "symbol" ? t + "" : t, e), e);
4
- import { WindowMessenger as u, connect as l, debug as m } from "penpal";
5
- import b from "eventemitter3";
6
- import { customAlphabet as A } from "nanoid";
7
- class h extends b {
8
- on(t, e, s) {
9
- return super.on(t, e, s);
1
+ var me = Object.defineProperty;
2
+ var be = (e, n, t) => n in e ? me(e, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[n] = t;
3
+ var g = (e, n, t) => (be(e, typeof n != "symbol" ? n + "" : n, t), t), X = (e, n, t) => {
4
+ if (!n.has(e))
5
+ throw TypeError("Cannot " + t);
6
+ };
7
+ var h = (e, n, t) => (X(e, n, "read from private field"), t ? t.call(e) : n.get(e)), A = (e, n, t) => {
8
+ if (n.has(e))
9
+ throw TypeError("Cannot add the same private member more than once");
10
+ n instanceof WeakSet ? n.add(e) : n.set(e, t);
11
+ }, _ = (e, n, t, s) => (X(e, n, "write to private field"), s ? s.call(e, t) : n.set(e, t), t);
12
+ var Ae = class extends Error {
13
+ constructor(n, t) {
14
+ super(t);
15
+ g(this, "code");
16
+ this.name = "PenpalError", this.code = n;
10
17
  }
11
- once(t, e, s) {
12
- return super.once(t, e, s);
18
+ }, P = Ae, Ee = (e) => ({
19
+ name: e.name,
20
+ message: e.message,
21
+ stack: e.stack,
22
+ penpalCode: e instanceof P ? e.code : void 0
23
+ }), we = ({
24
+ name: e,
25
+ message: n,
26
+ stack: t,
27
+ penpalCode: s
28
+ }) => {
29
+ const r = s ? new P(s, n) : new Error(n);
30
+ return r.name = e, r.stack = t, r;
31
+ }, Ie = Symbol("Reply"), G, re, Me = (re = class {
32
+ constructor(e, n) {
33
+ g(this, "value");
34
+ g(this, "transferables");
35
+ // Allows TypeScript to distinguish between an actual instance of this
36
+ // class versus an object that looks structurally similar.
37
+ // eslint-disable-next-line no-unused-private-class-members
38
+ A(this, G, Ie);
39
+ this.value = e, this.transferables = n == null ? void 0 : n.transferables;
13
40
  }
14
- off(t, e, s) {
15
- return super.off(t, e, s);
41
+ }, G = new WeakMap(), re), Pe = Me, M = "penpal", W = (e) => typeof e == "object" && e !== null, ce = (e) => typeof e == "function", Ce = (e) => W(e) && e.namespace === M, V = (e) => e.type === "SYN", Z = (e) => e.type === "ACK1", B = (e) => e.type === "ACK2", de = (e) => e.type === "CALL", he = (e) => e.type === "REPLY", _e = (e) => e.type === "DESTROY", ue = (e, n = []) => {
42
+ const t = [];
43
+ for (const s of Object.keys(e)) {
44
+ const r = e[s];
45
+ ce(r) ? t.push([...n, s]) : W(r) && t.push(
46
+ ...ue(r, [...n, s])
47
+ );
16
48
  }
17
- emit(t, ...e) {
18
- return super.emit(t, ...e);
49
+ return t;
50
+ }, Oe = (e, n) => {
51
+ const t = e.reduce(
52
+ (s, r) => W(s) ? s[r] : void 0,
53
+ n
54
+ );
55
+ return ce(t) ? t : void 0;
56
+ }, T = (e) => e.join("."), ee = (e, n, t) => ({
57
+ namespace: M,
58
+ channel: e,
59
+ type: "REPLY",
60
+ callId: n,
61
+ isError: !0,
62
+ ...t instanceof Error ? { value: Ee(t), isSerializedErrorInstance: !0 } : { value: t }
63
+ }), Se = (e, n, t, s) => {
64
+ let r = !1;
65
+ const v = async (u) => {
66
+ if (r || !de(u))
67
+ return;
68
+ s == null || s(`Received ${T(u.methodPath)}() call`, u);
69
+ const { methodPath: l, args: c, id: a } = u;
70
+ let i, o;
71
+ try {
72
+ const f = Oe(l, n);
73
+ if (!f)
74
+ throw new P(
75
+ "METHOD_NOT_FOUND",
76
+ `Method \`${T(l)}\` is not found.`
77
+ );
78
+ let p = await f(...c);
79
+ p instanceof Pe && (o = p.transferables, p = await p.value), i = {
80
+ namespace: M,
81
+ channel: t,
82
+ type: "REPLY",
83
+ callId: a,
84
+ value: p
85
+ };
86
+ } catch (f) {
87
+ i = ee(t, a, f);
88
+ }
89
+ if (!r)
90
+ try {
91
+ s == null || s(`Sending ${T(l)}() reply`, i), e.sendMessage(i, o);
92
+ } catch (f) {
93
+ throw f.name === "DataCloneError" && (i = ee(t, a, f), s == null || s(`Sending ${T(l)}() reply`, i), e.sendMessage(i)), f;
94
+ }
95
+ };
96
+ return e.addMessageHandler(v), () => {
97
+ r = !0, e.removeMessageHandler(v);
98
+ };
99
+ }, Re = Se, ie, le = ((ie = crypto.randomUUID) == null ? void 0 : ie.bind(crypto)) ?? (() => new Array(4).fill(0).map(
100
+ () => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)
101
+ ).join("-")), Te = Symbol("CallOptions"), J, ae, De = (ae = class {
102
+ constructor(e) {
103
+ g(this, "transferables");
104
+ g(this, "timeout");
105
+ // Allows TypeScript to distinguish between an actual instance of this
106
+ // class versus an object that looks structurally similar.
107
+ // eslint-disable-next-line no-unused-private-class-members
108
+ A(this, J, Te);
109
+ this.transferables = e == null ? void 0 : e.transferables, this.timeout = e == null ? void 0 : e.timeout;
110
+ }
111
+ }, J = new WeakMap(), ae), Ne = De, ke = /* @__PURE__ */ new Set(["apply", "call", "bind"]), fe = (e, n, t = []) => new Proxy(
112
+ t.length ? () => {
113
+ } : /* @__PURE__ */ Object.create(null),
114
+ {
115
+ get(s, r) {
116
+ if (r !== "then")
117
+ return t.length && ke.has(r) ? Reflect.get(s, r) : fe(e, n, [...t, r]);
118
+ },
119
+ apply(s, r, v) {
120
+ return e(t, v);
121
+ }
122
+ }
123
+ ), te = (e) => new P(
124
+ "CONNECTION_DESTROYED",
125
+ `Method call ${T(
126
+ e
127
+ )}() failed due to destroyed connection`
128
+ ), Le = (e, n, t) => {
129
+ let s = !1;
130
+ const r = /* @__PURE__ */ new Map(), v = (c) => {
131
+ if (!he(c))
132
+ return;
133
+ const { callId: a, value: i, isError: o, isSerializedErrorInstance: f } = c, p = r.get(a);
134
+ p && (r.delete(a), t == null || t(
135
+ `Received ${T(p.methodPath)}() call`,
136
+ c
137
+ ), o ? p.reject(
138
+ f ? we(i) : i
139
+ ) : p.resolve(i));
140
+ };
141
+ return e.addMessageHandler(v), {
142
+ remoteProxy: fe((c, a) => {
143
+ if (s)
144
+ throw te(c);
145
+ const i = le(), o = a[a.length - 1], f = o instanceof Ne, { timeout: p, transferables: b } = f ? o : {}, E = f ? a.slice(0, -1) : a;
146
+ return new Promise((d, w) => {
147
+ const C = p !== void 0 ? window.setTimeout(() => {
148
+ r.delete(i), w(
149
+ new P(
150
+ "METHOD_CALL_TIMEOUT",
151
+ `Method call ${T(
152
+ c
153
+ )}() timed out after ${p}ms`
154
+ )
155
+ );
156
+ }, p) : void 0;
157
+ r.set(i, { methodPath: c, resolve: d, reject: w, timeoutId: C });
158
+ try {
159
+ const y = {
160
+ namespace: M,
161
+ channel: n,
162
+ type: "CALL",
163
+ id: i,
164
+ methodPath: c,
165
+ args: E
166
+ };
167
+ t == null || t(`Sending ${T(c)}() call`, y), e.sendMessage(y, b);
168
+ } catch (y) {
169
+ w(
170
+ new P("TRANSMISSION_FAILED", y.message)
171
+ );
172
+ }
173
+ });
174
+ }, t),
175
+ destroy: () => {
176
+ s = !0, e.removeMessageHandler(v);
177
+ for (const { methodPath: c, reject: a, timeoutId: i } of r.values())
178
+ clearTimeout(i), a(te(c));
179
+ r.clear();
180
+ }
181
+ };
182
+ }, xe = Le, He = () => {
183
+ let e, n;
184
+ return {
185
+ promise: new Promise((s, r) => {
186
+ e = s, n = r;
187
+ }),
188
+ resolve: e,
189
+ reject: n
190
+ };
191
+ }, Ue = He, $e = class extends Error {
192
+ constructor(e) {
193
+ super(
194
+ `You've hit a bug in Penpal. Please file an issue with the following information: ${e}`
195
+ );
196
+ }
197
+ }, Y = $e, Q = "deprecated-penpal", je = (e) => W(e) && "penpal" in e, qe = (e) => e.split("."), ne = (e) => e.join("."), pe = (e) => new Y(
198
+ `Unexpected message to translate: ${JSON.stringify(e)}`
199
+ ), Fe = (e) => {
200
+ if (e.penpal === "syn")
201
+ return {
202
+ namespace: M,
203
+ channel: void 0,
204
+ type: "SYN",
205
+ participantId: Q
206
+ };
207
+ if (e.penpal === "ack")
208
+ return {
209
+ namespace: M,
210
+ channel: void 0,
211
+ type: "ACK2"
212
+ };
213
+ if (e.penpal === "call")
214
+ return {
215
+ namespace: M,
216
+ channel: void 0,
217
+ type: "CALL",
218
+ // Actually converting the ID to a string would break communication.
219
+ id: e.id,
220
+ methodPath: qe(e.methodName),
221
+ args: e.args
222
+ };
223
+ if (e.penpal === "reply")
224
+ return e.resolution === "fulfilled" ? {
225
+ namespace: M,
226
+ channel: void 0,
227
+ type: "REPLY",
228
+ // Actually converting the ID to a string would break communication.
229
+ callId: e.id,
230
+ value: e.returnValue
231
+ } : {
232
+ namespace: M,
233
+ channel: void 0,
234
+ type: "REPLY",
235
+ // Actually converting the ID to a string would break communication.
236
+ callId: e.id,
237
+ isError: !0,
238
+ ...e.returnValueIsError ? {
239
+ value: e.returnValue,
240
+ isSerializedErrorInstance: !0
241
+ } : {
242
+ value: e.returnValue
243
+ }
244
+ };
245
+ throw pe(e);
246
+ }, ze = (e) => {
247
+ if (Z(e))
248
+ return {
249
+ penpal: "synAck",
250
+ methodNames: e.methodPaths.map(ne)
251
+ };
252
+ if (de(e))
253
+ return {
254
+ penpal: "call",
255
+ // Actually converting the ID to a number would break communication.
256
+ id: e.id,
257
+ methodName: ne(e.methodPath),
258
+ args: e.args
259
+ };
260
+ if (he(e))
261
+ return e.isError ? {
262
+ penpal: "reply",
263
+ // Actually converting the ID to a number would break communication.
264
+ id: e.callId,
265
+ resolution: "rejected",
266
+ ...e.isSerializedErrorInstance ? {
267
+ returnValue: e.value,
268
+ returnValueIsError: !0
269
+ } : { returnValue: e.value }
270
+ } : {
271
+ penpal: "reply",
272
+ // Actually converting the ID to a number would break communication.
273
+ id: e.callId,
274
+ resolution: "fulfilled",
275
+ returnValue: e.value
276
+ };
277
+ throw pe(e);
278
+ }, Ve = ({
279
+ messenger: e,
280
+ methods: n,
281
+ timeout: t,
282
+ channel: s,
283
+ log: r
284
+ }) => {
285
+ const v = le();
286
+ let u;
287
+ const l = [];
288
+ let c = !1;
289
+ const a = ue(n), { promise: i, resolve: o, reject: f } = Ue(), p = t !== void 0 ? setTimeout(() => {
290
+ f(
291
+ new P(
292
+ "CONNECTION_TIMEOUT",
293
+ `Connection timed out after ${t}ms`
294
+ )
295
+ );
296
+ }, t) : void 0, b = () => {
297
+ for (const m of l)
298
+ m();
299
+ }, E = () => {
300
+ if (c)
301
+ return;
302
+ l.push(Re(e, n, s, r));
303
+ const { remoteProxy: m, destroy: D } = xe(e, s, r);
304
+ l.push(D), clearTimeout(p), c = !0, o({
305
+ remoteProxy: m,
306
+ destroy: b
307
+ });
308
+ }, d = () => {
309
+ const m = {
310
+ namespace: M,
311
+ type: "SYN",
312
+ channel: s,
313
+ participantId: v
314
+ };
315
+ r == null || r("Sending handshake SYN", m);
316
+ try {
317
+ e.sendMessage(m);
318
+ } catch (D) {
319
+ f(new P("TRANSMISSION_FAILED", D.message));
320
+ }
321
+ }, w = (m) => {
322
+ if (r == null || r("Received handshake SYN", m), m.participantId === u && // TODO: Used for backward-compatibility. Remove in next major version.
323
+ u !== Q || (u = m.participantId, d(), !(v > u || // TODO: Used for backward-compatibility. Remove in next major version.
324
+ u === Q)))
325
+ return;
326
+ const z = {
327
+ namespace: M,
328
+ channel: s,
329
+ type: "ACK1",
330
+ methodPaths: a
331
+ };
332
+ r == null || r("Sending handshake ACK1", z);
333
+ try {
334
+ e.sendMessage(z);
335
+ } catch (ye) {
336
+ f(new P("TRANSMISSION_FAILED", ye.message));
337
+ return;
338
+ }
339
+ }, C = (m) => {
340
+ r == null || r("Received handshake ACK1", m);
341
+ const D = {
342
+ namespace: M,
343
+ channel: s,
344
+ type: "ACK2"
345
+ };
346
+ r == null || r("Sending handshake ACK2", D);
347
+ try {
348
+ e.sendMessage(D);
349
+ } catch (z) {
350
+ f(new P("TRANSMISSION_FAILED", z.message));
351
+ return;
352
+ }
353
+ E();
354
+ }, y = (m) => {
355
+ r == null || r("Received handshake ACK2", m), E();
356
+ }, F = (m) => {
357
+ V(m) && w(m), Z(m) && C(m), B(m) && y(m);
358
+ };
359
+ return e.addMessageHandler(F), l.push(() => e.removeMessageHandler(F)), d(), i;
360
+ }, Ye = Ve, Ke = (e) => {
361
+ let n = !1, t;
362
+ return (...s) => (n || (n = !0, t = e(...s)), t);
363
+ }, We = Ke, se = /* @__PURE__ */ new WeakSet(), Be = ({
364
+ messenger: e,
365
+ methods: n = {},
366
+ timeout: t,
367
+ channel: s,
368
+ log: r
369
+ }) => {
370
+ if (!e)
371
+ throw new P("INVALID_ARGUMENT", "messenger must be defined");
372
+ if (se.has(e))
373
+ throw new P(
374
+ "INVALID_ARGUMENT",
375
+ "A messenger can only be used for a single connection"
376
+ );
377
+ se.add(e);
378
+ const v = [e.destroy], u = We((a) => {
379
+ if (a) {
380
+ const i = {
381
+ namespace: M,
382
+ channel: s,
383
+ type: "DESTROY"
384
+ };
385
+ try {
386
+ e.sendMessage(i);
387
+ } catch {
388
+ }
389
+ }
390
+ for (const i of v)
391
+ i();
392
+ r == null || r("Connection destroyed");
393
+ }), l = (a) => Ce(a) && a.channel === s;
394
+ return {
395
+ promise: (async () => {
396
+ try {
397
+ e.initialize({ log: r, validateReceivedMessage: l }), e.addMessageHandler((o) => {
398
+ _e(o) && u(!1);
399
+ });
400
+ const { remoteProxy: a, destroy: i } = await Ye({
401
+ messenger: e,
402
+ methods: n,
403
+ timeout: t,
404
+ channel: s,
405
+ log: r
406
+ });
407
+ return v.push(i), a;
408
+ } catch (a) {
409
+ throw u(!0), a;
410
+ }
411
+ })(),
412
+ // Why we don't reject the connection promise when consumer calls destroy():
413
+ // https://github.com/Aaronius/penpal/issues/51
414
+ destroy: () => {
415
+ u(!0);
416
+ }
417
+ };
418
+ }, Qe = Be, S, N, x, H, k, R, I, L, K, U, j, q, $, oe, Ge = (oe = class {
419
+ constructor({ remoteWindow: e, allowedOrigins: n }) {
420
+ A(this, S, void 0);
421
+ A(this, N, void 0);
422
+ A(this, x, void 0);
423
+ A(this, H, void 0);
424
+ A(this, k, void 0);
425
+ A(this, R, /* @__PURE__ */ new Set());
426
+ A(this, I, void 0);
427
+ // TODO: Used for backward-compatibility. Remove in next major version.
428
+ A(this, L, !1);
429
+ g(this, "initialize", ({
430
+ log: e,
431
+ validateReceivedMessage: n
432
+ }) => {
433
+ _(this, x, e), _(this, H, n), window.addEventListener("message", h(this, q));
434
+ });
435
+ g(this, "sendMessage", (e, n) => {
436
+ if (V(e)) {
437
+ const t = h(this, U).call(this, e);
438
+ h(this, S).postMessage(e, {
439
+ targetOrigin: t,
440
+ transfer: n
441
+ });
442
+ return;
443
+ }
444
+ if (Z(e) || // If the child is using a previous version of Penpal, we need to
445
+ // downgrade the message and send it through the window rather than
446
+ // the port because older versions of Penpal don't use MessagePorts.
447
+ h(this, L)) {
448
+ const t = h(this, L) ? ze(e) : e, s = h(this, U).call(this, e);
449
+ h(this, S).postMessage(t, {
450
+ targetOrigin: s,
451
+ transfer: n
452
+ });
453
+ return;
454
+ }
455
+ if (B(e)) {
456
+ const { port1: t, port2: s } = new MessageChannel();
457
+ _(this, I, t), t.addEventListener("message", h(this, $)), t.start();
458
+ const r = [s, ...n || []], v = h(this, U).call(this, e);
459
+ h(this, S).postMessage(e, {
460
+ targetOrigin: v,
461
+ transfer: r
462
+ });
463
+ return;
464
+ }
465
+ if (h(this, I)) {
466
+ h(this, I).postMessage(e, {
467
+ transfer: n
468
+ });
469
+ return;
470
+ }
471
+ throw new Y("Port is undefined");
472
+ });
473
+ g(this, "addMessageHandler", (e) => {
474
+ h(this, R).add(e);
475
+ });
476
+ g(this, "removeMessageHandler", (e) => {
477
+ h(this, R).delete(e);
478
+ });
479
+ g(this, "destroy", () => {
480
+ window.removeEventListener("message", h(this, q)), h(this, j).call(this), h(this, R).clear();
481
+ });
482
+ A(this, K, (e) => h(this, N).some(
483
+ (n) => n instanceof RegExp ? n.test(e) : n === e || n === "*"
484
+ ));
485
+ A(this, U, (e) => {
486
+ if (V(e))
487
+ return "*";
488
+ if (!h(this, k))
489
+ throw new Y("Concrete remote origin not set");
490
+ return h(this, k) === "null" && h(this, N).includes("*") ? "*" : h(this, k);
491
+ });
492
+ A(this, j, () => {
493
+ var e, n;
494
+ (e = h(this, I)) == null || e.removeEventListener("message", h(this, $)), (n = h(this, I)) == null || n.close(), _(this, I, void 0);
495
+ });
496
+ A(this, q, ({
497
+ source: e,
498
+ origin: n,
499
+ ports: t,
500
+ data: s
501
+ }) => {
502
+ var r, v, u;
503
+ if (e === h(this, S) && (je(s) && ((r = h(this, x)) == null || r.call(
504
+ this,
505
+ "Please upgrade the child window to the latest version of Penpal."
506
+ ), _(this, L, !0), s = Fe(s)), !!((v = h(this, H)) != null && v.call(this, s)))) {
507
+ if (!h(this, K).call(this, n)) {
508
+ (u = h(this, x)) == null || u.call(
509
+ this,
510
+ `Received a message from origin \`${n}\` which did not match allowed origins \`[${h(this, N).join(", ")}]\``
511
+ );
512
+ return;
513
+ }
514
+ if (V(s) && (h(this, j).call(this), _(this, k, n)), B(s) && // Previous versions of Penpal don't use MessagePorts and do all
515
+ // communication through the window.
516
+ !h(this, L)) {
517
+ if (_(this, I, t[0]), !h(this, I))
518
+ throw new Y("No port received on ACK2");
519
+ h(this, I).addEventListener("message", h(this, $)), h(this, I).start();
520
+ }
521
+ for (const l of h(this, R))
522
+ l(s);
523
+ }
524
+ });
525
+ A(this, $, ({ data: e }) => {
526
+ var n;
527
+ if ((n = h(this, H)) != null && n.call(this, e))
528
+ for (const t of h(this, R))
529
+ t(e);
530
+ });
531
+ if (!e)
532
+ throw new P("INVALID_ARGUMENT", "remoteWindow must be defined");
533
+ _(this, S, e), _(this, N, n != null && n.length ? n : [window.origin]);
534
+ }
535
+ }, S = new WeakMap(), N = new WeakMap(), x = new WeakMap(), H = new WeakMap(), k = new WeakMap(), R = new WeakMap(), I = new WeakMap(), L = new WeakMap(), K = new WeakMap(), U = new WeakMap(), j = new WeakMap(), q = new WeakMap(), $ = new WeakMap(), oe), Je = Ge, Ze = (e) => (...n) => {
536
+ console.log(`✍️ %c${e}%c`, "font-weight: bold;", "", ...n);
537
+ }, Xe = Ze;
538
+ function et(e) {
539
+ return e && e.__esModule && Object.prototype.hasOwnProperty.call(e, "default") ? e.default : e;
540
+ }
541
+ var ve = { exports: {} };
542
+ (function(e) {
543
+ var n = Object.prototype.hasOwnProperty, t = "~";
544
+ function s() {
545
+ }
546
+ Object.create && (s.prototype = /* @__PURE__ */ Object.create(null), new s().__proto__ || (t = !1));
547
+ function r(c, a, i) {
548
+ this.fn = c, this.context = a, this.once = i || !1;
549
+ }
550
+ function v(c, a, i, o, f) {
551
+ if (typeof i != "function")
552
+ throw new TypeError("The listener must be a function");
553
+ var p = new r(i, o || c, f), b = t ? t + a : a;
554
+ return c._events[b] ? c._events[b].fn ? c._events[b] = [c._events[b], p] : c._events[b].push(p) : (c._events[b] = p, c._eventsCount++), c;
555
+ }
556
+ function u(c, a) {
557
+ --c._eventsCount === 0 ? c._events = new s() : delete c._events[a];
558
+ }
559
+ function l() {
560
+ this._events = new s(), this._eventsCount = 0;
561
+ }
562
+ l.prototype.eventNames = function() {
563
+ var a = [], i, o;
564
+ if (this._eventsCount === 0)
565
+ return a;
566
+ for (o in i = this._events)
567
+ n.call(i, o) && a.push(t ? o.slice(1) : o);
568
+ return Object.getOwnPropertySymbols ? a.concat(Object.getOwnPropertySymbols(i)) : a;
569
+ }, l.prototype.listeners = function(a) {
570
+ var i = t ? t + a : a, o = this._events[i];
571
+ if (!o)
572
+ return [];
573
+ if (o.fn)
574
+ return [o.fn];
575
+ for (var f = 0, p = o.length, b = new Array(p); f < p; f++)
576
+ b[f] = o[f].fn;
577
+ return b;
578
+ }, l.prototype.listenerCount = function(a) {
579
+ var i = t ? t + a : a, o = this._events[i];
580
+ return o ? o.fn ? 1 : o.length : 0;
581
+ }, l.prototype.emit = function(a, i, o, f, p, b) {
582
+ var E = t ? t + a : a;
583
+ if (!this._events[E])
584
+ return !1;
585
+ var d = this._events[E], w = arguments.length, C, y;
586
+ if (d.fn) {
587
+ switch (d.once && this.removeListener(a, d.fn, void 0, !0), w) {
588
+ case 1:
589
+ return d.fn.call(d.context), !0;
590
+ case 2:
591
+ return d.fn.call(d.context, i), !0;
592
+ case 3:
593
+ return d.fn.call(d.context, i, o), !0;
594
+ case 4:
595
+ return d.fn.call(d.context, i, o, f), !0;
596
+ case 5:
597
+ return d.fn.call(d.context, i, o, f, p), !0;
598
+ case 6:
599
+ return d.fn.call(d.context, i, o, f, p, b), !0;
600
+ }
601
+ for (y = 1, C = new Array(w - 1); y < w; y++)
602
+ C[y - 1] = arguments[y];
603
+ d.fn.apply(d.context, C);
604
+ } else {
605
+ var F = d.length, m;
606
+ for (y = 0; y < F; y++)
607
+ switch (d[y].once && this.removeListener(a, d[y].fn, void 0, !0), w) {
608
+ case 1:
609
+ d[y].fn.call(d[y].context);
610
+ break;
611
+ case 2:
612
+ d[y].fn.call(d[y].context, i);
613
+ break;
614
+ case 3:
615
+ d[y].fn.call(d[y].context, i, o);
616
+ break;
617
+ case 4:
618
+ d[y].fn.call(d[y].context, i, o, f);
619
+ break;
620
+ default:
621
+ if (!C)
622
+ for (m = 1, C = new Array(w - 1); m < w; m++)
623
+ C[m - 1] = arguments[m];
624
+ d[y].fn.apply(d[y].context, C);
625
+ }
626
+ }
627
+ return !0;
628
+ }, l.prototype.on = function(a, i, o) {
629
+ return v(this, a, i, o, !1);
630
+ }, l.prototype.once = function(a, i, o) {
631
+ return v(this, a, i, o, !0);
632
+ }, l.prototype.removeListener = function(a, i, o, f) {
633
+ var p = t ? t + a : a;
634
+ if (!this._events[p])
635
+ return this;
636
+ if (!i)
637
+ return u(this, p), this;
638
+ var b = this._events[p];
639
+ if (b.fn)
640
+ b.fn === i && (!f || b.once) && (!o || b.context === o) && u(this, p);
641
+ else {
642
+ for (var E = 0, d = [], w = b.length; E < w; E++)
643
+ (b[E].fn !== i || f && !b[E].once || o && b[E].context !== o) && d.push(b[E]);
644
+ d.length ? this._events[p] = d.length === 1 ? d[0] : d : u(this, p);
645
+ }
646
+ return this;
647
+ }, l.prototype.removeAllListeners = function(a) {
648
+ var i;
649
+ return a ? (i = t ? t + a : a, this._events[i] && u(this, i)) : (this._events = new s(), this._eventsCount = 0), this;
650
+ }, l.prototype.off = l.prototype.removeListener, l.prototype.addListener = l.prototype.on, l.prefixed = t, l.EventEmitter = l, e.exports = l;
651
+ })(ve);
652
+ var tt = ve.exports;
653
+ const nt = /* @__PURE__ */ et(tt);
654
+ class ge extends nt {
655
+ on(n, t, s) {
656
+ return super.on(n, t, s);
657
+ }
658
+ once(n, t, s) {
659
+ return super.once(n, t, s);
660
+ }
661
+ off(n, t, s) {
662
+ return super.off(n, t, s);
663
+ }
664
+ emit(n, ...t) {
665
+ return super.emit(n, ...t);
19
666
  }
20
667
  }
21
- class I {
668
+ class st {
22
669
  constructor() {
23
- i(this, "queue", []);
24
- i(this, "maxSize", 100);
670
+ g(this, "queue", []);
671
+ g(this, "maxSize", 100);
25
672
  }
26
- enqueue(t, e = 3) {
673
+ enqueue(n, t = 3) {
27
674
  this.queue.length >= this.maxSize && this.queue.shift();
28
675
  const s = {
29
- ...t,
676
+ ...n,
30
677
  retryCount: 0,
31
- maxRetries: e
678
+ maxRetries: t
32
679
  };
33
- return this.queue.push(s), t.messageId;
680
+ return this.queue.push(s), n.messageId;
34
681
  }
35
682
  dequeue() {
36
683
  return this.queue.shift();
@@ -38,87 +685,99 @@ class I {
38
685
  hasMessages() {
39
686
  return this.queue.length > 0;
40
687
  }
41
- getMessageById(t) {
42
- return this.queue.find((e) => e.messageId === t);
688
+ getMessageById(n) {
689
+ return this.queue.find((t) => t.messageId === n);
43
690
  }
44
- removeMessageById(t) {
45
- const e = this.queue.findIndex((s) => s.messageId === t);
46
- e !== -1 && this.queue.splice(e, 1);
691
+ removeMessageById(n) {
692
+ const t = this.queue.findIndex((s) => s.messageId === n);
693
+ t !== -1 && this.queue.splice(t, 1);
47
694
  }
48
695
  clear() {
49
696
  this.queue = [];
50
697
  }
51
698
  }
52
- class f {
53
- constructor(t, e = !1) {
54
- this.context = t, this.debug = e;
699
+ class rt {
700
+ constructor(n, t = !1) {
701
+ this.context = n, this.debug = t;
55
702
  }
56
- formatMessage(t, ...e) {
57
- return [`[${(/* @__PURE__ */ new Date()).toISOString()}] [${this.context}] [${t}]`, ...e];
703
+ formatMessage(n, ...t) {
704
+ return [`[${(/* @__PURE__ */ new Date()).toISOString()}] [${this.context}] [${n}]`, ...t];
58
705
  }
59
- info(...t) {
60
- console.info(...this.formatMessage("INFO", ...t));
706
+ info(...n) {
707
+ console.info(...this.formatMessage("INFO", ...n));
61
708
  }
62
- debug(...t) {
63
- this.debug && console.debug(...this.formatMessage("DEBUG", ...t));
709
+ debug(...n) {
710
+ this.debug && console.debug(...this.formatMessage("DEBUG", ...n));
64
711
  }
65
- warn(...t) {
66
- console.warn(...this.formatMessage("WARN", ...t));
712
+ warn(...n) {
713
+ console.warn(...this.formatMessage("WARN", ...n));
67
714
  }
68
- error(...t) {
69
- console.error(...this.formatMessage("ERROR", ...t));
715
+ error(...n) {
716
+ console.error(...this.formatMessage("ERROR", ...n));
70
717
  }
71
718
  }
72
- const p = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", y = A(p, 12);
73
- class v extends h {
74
- constructor(e, s = {}) {
719
+ let it = (e) => crypto.getRandomValues(new Uint8Array(e)), at = (e, n, t) => {
720
+ let s = (2 << Math.log2(e.length - 1)) - 1, r = -~(1.6 * s * n / e.length);
721
+ return (v = n) => {
722
+ let u = "";
723
+ for (; ; ) {
724
+ let l = t(r), c = r | 0;
725
+ for (; c--; )
726
+ if (u += e[l[c] & s] || "", u.length >= v)
727
+ return u;
728
+ }
729
+ };
730
+ }, ot = (e, n = 21) => at(e, n | 0, it);
731
+ const ct = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", dt = ot(ct, 12);
732
+ class ht extends ge {
733
+ constructor(t, s = {}) {
75
734
  super();
76
- i(this, "connection", null);
77
- i(this, "iframe");
78
- i(this, "options");
79
- i(this, "messageQueue");
80
- i(this, "logger");
81
- i(this, "isConnected", !1);
82
- i(this, "reconnectAttempts", 0);
83
- i(this, "maxReconnectAttempts", 3);
84
- this.iframe = e, this.options = {
735
+ g(this, "connection", null);
736
+ g(this, "iframe");
737
+ g(this, "options");
738
+ g(this, "messageQueue");
739
+ g(this, "logger");
740
+ g(this, "isConnected", !1);
741
+ g(this, "reconnectAttempts", 0);
742
+ g(this, "maxReconnectAttempts", 3);
743
+ this.iframe = t, this.options = {
85
744
  timeout: 3e4,
86
745
  debug: !1,
87
746
  retryCount: 3,
88
747
  retryDelay: 1e3,
89
748
  childOrigin: "*",
90
749
  ...s
91
- }, this.messageQueue = new I(), this.logger = new f("PenpalBridge", this.options.debug);
750
+ }, this.messageQueue = new st(), this.logger = new rt("PenpalBridge", this.options.debug);
92
751
  }
93
752
  /**
94
753
  * 建立连接
95
754
  */
96
- async connect(e = {}) {
755
+ async connect(t = {}) {
97
756
  try {
98
757
  this.logger.info("正在建立连接...");
99
- const s = new u({
758
+ const s = new Je({
100
759
  remoteWindow: this.iframe,
101
760
  // Defaults to the current origin.
102
761
  allowedOrigins: ["http://127.0.0.1:3000"]
103
762
  // Alternatively,
104
763
  // allowedOrigins: [new Url(iframe.src).origin]
105
764
  });
106
- this.connection = l({
765
+ this.connection = Qe({
107
766
  messenger: s,
108
767
  // Methods the parent window is exposing to the iframe window.
109
768
  methods: {
110
- add(a, o) {
111
- return a + o;
769
+ add(r, v) {
770
+ return r + v;
112
771
  },
113
- emitEvent: (a, o) => {
114
- this.emit(a, o);
772
+ emitEvent: (r, v) => {
773
+ this.emit(r, v);
115
774
  },
116
775
  // 接收来自画板的请求
117
- ...e
776
+ ...t
118
777
  },
119
778
  timeout: this.options.timeout,
120
779
  // childOrigin: this.options.childOrigin,
121
- log: m("parent")
780
+ log: Xe("parent")
122
781
  }), await this.connection.promise, this.isConnected = !0, this.reconnectAttempts = 0, await this.processQueuedMessages(), this.logger.info("连接建立成功"), this.emit("connected");
123
782
  } catch (s) {
124
783
  throw this.logger.error("连接失败:", s), await this.handleReconnect(), s;
@@ -127,23 +786,23 @@ class v extends h {
127
786
  /**
128
787
  * 发送消息到画板
129
788
  */
130
- async send(e, s) {
131
- var o;
132
- const a = {
133
- action: e,
789
+ async send(t, s) {
790
+ var v;
791
+ const r = {
792
+ action: t,
134
793
  params: s,
135
794
  timestamp: Date.now(),
136
- messageId: y()
795
+ messageId: dt()
137
796
  };
138
797
  if (!this.isConnected)
139
- throw this.messageQueue.enqueue(a), new Error("Connection not ready, message queued");
798
+ throw this.messageQueue.enqueue(r), new Error("Connection not ready, message queued");
140
799
  try {
141
- if (this.logger.debug("发送消息:", a), !((o = this.connection) != null && o.child))
800
+ if (this.logger.debug("发送消息:", r), !((v = this.connection) != null && v.child))
142
801
  throw new Error("连接未就绪");
143
- const c = await this.connection.child.handleMessage(a);
144
- return this.logger.debug("收到响应:", c), this.validateResponse(c);
145
- } catch (c) {
146
- throw this.logger.error("发送消息失败:", c), this.messageQueue.enqueue(a), c;
802
+ const u = await this.connection.child.handleMessage(r);
803
+ return this.logger.debug("收到响应:", u), this.validateResponse(u);
804
+ } catch (u) {
805
+ throw this.logger.error("发送消息失败:", u), this.messageQueue.enqueue(r), u;
147
806
  }
148
807
  }
149
808
  /**
@@ -151,12 +810,12 @@ class v extends h {
151
810
  */
152
811
  async processQueuedMessages() {
153
812
  for (; this.messageQueue.hasMessages(); ) {
154
- const e = this.messageQueue.dequeue();
155
- if (e)
813
+ const t = this.messageQueue.dequeue();
814
+ if (t)
156
815
  try {
157
- await this.send(e.action, e.params);
816
+ await this.send(t.action, t.params);
158
817
  } catch (s) {
159
- this.logger.warn("队列消息发送失败:", s), this.messageQueue.enqueue(e);
818
+ this.logger.warn("队列消息发送失败:", s), this.messageQueue.enqueue(t);
160
819
  }
161
820
  }
162
821
  }
@@ -169,7 +828,7 @@ class v extends h {
169
828
  return;
170
829
  }
171
830
  this.reconnectAttempts++, this.logger.info(`尝试重连 (${this.reconnectAttempts}/${this.maxReconnectAttempts})`), await new Promise(
172
- (e) => setTimeout(e, this.options.retryDelay * this.reconnectAttempts)
831
+ (t) => setTimeout(t, this.options.retryDelay * this.reconnectAttempts)
173
832
  );
174
833
  try {
175
834
  await this.connect();
@@ -180,14 +839,14 @@ class v extends h {
180
839
  /**
181
840
  * 验证响应格式
182
841
  */
183
- validateResponse(e) {
184
- if (!e || e.success === void 0)
842
+ validateResponse(t) {
843
+ if (!t || t.success === void 0)
185
844
  throw new Error("Invalid response format");
186
- if (!e.success) {
187
- const s = e.error || { code: "UNKNOWN", message: "Unknown error" };
845
+ if (!t.success) {
846
+ const s = t.error || { code: "UNKNOWN", message: "Unknown error" };
188
847
  throw new Error(`${s.code}: ${s.message}`);
189
848
  }
190
- return e.data;
849
+ return t.data;
191
850
  }
192
851
  /**
193
852
  * 断开连接
@@ -202,15 +861,15 @@ class v extends h {
202
861
  return this.isConnected;
203
862
  }
204
863
  }
205
- class E {
206
- constructor(t) {
207
- this.bridge = t;
864
+ class ut {
865
+ constructor(n) {
866
+ this.bridge = n;
208
867
  }
209
- async initialize(t) {
210
- return this.bridge.send("canvas.initialize", t);
868
+ async initialize(n) {
869
+ return this.bridge.send("canvas.initialize", n);
211
870
  }
212
- async setData(t) {
213
- return this.bridge.send("canvas.setData", t);
871
+ async setData(n) {
872
+ return this.bridge.send("canvas.setData", n);
214
873
  }
215
874
  async getData() {
216
875
  return this.bridge.send("canvas.getData");
@@ -224,76 +883,76 @@ class E {
224
883
  async redo() {
225
884
  return this.bridge.send("canvas.redo");
226
885
  }
227
- async export(t) {
228
- return this.bridge.send("canvas.export", { format: t });
886
+ async export(n) {
887
+ return this.bridge.send("canvas.export", { format: n });
229
888
  }
230
889
  async save() {
231
890
  return this.bridge.send("canvas.save");
232
891
  }
233
- async invoke(t, ...e) {
234
- return this.bridge.send("canvas.invoke", { method: t, args: e });
892
+ async invoke(n, ...t) {
893
+ return this.bridge.send("canvas.invoke", { method: n, args: t });
235
894
  }
236
- async setZoom(t) {
237
- return this.bridge.send("canvas.setZoom", { zoom: t });
895
+ async setZoom(n) {
896
+ return this.bridge.send("canvas.setZoom", { zoom: n });
238
897
  }
239
898
  async getSnapshot() {
240
899
  return this.bridge.send("canvas.getSnapshot");
241
900
  }
242
901
  }
243
- class w {
244
- constructor(t) {
245
- this.bridge = t;
902
+ class lt {
903
+ constructor(n) {
904
+ this.bridge = n;
246
905
  }
247
- async setAbilities(t) {
248
- return this.bridge.send("abilities.set", t);
906
+ async setAbilities(n) {
907
+ return this.bridge.send("abilities.set", n);
249
908
  }
250
909
  async getAbilities() {
251
910
  return this.bridge.send("abilities.get");
252
911
  }
253
- async enableAbility(t) {
254
- return this.bridge.send("abilities.enable", { name: t });
912
+ async enableAbility(n) {
913
+ return this.bridge.send("abilities.enable", { name: n });
255
914
  }
256
- async disableAbility(t) {
257
- return this.bridge.send("abilities.disable", { name: t });
915
+ async disableAbility(n) {
916
+ return this.bridge.send("abilities.disable", { name: n });
258
917
  }
259
918
  }
260
- class P {
261
- constructor(t) {
262
- this.bridge = t;
919
+ class ft {
920
+ constructor(n) {
921
+ this.bridge = n;
263
922
  }
264
- async setTheme(t) {
265
- return this.bridge.send("theme.set", { theme: t });
923
+ async setTheme(n) {
924
+ return this.bridge.send("theme.set", { theme: n });
266
925
  }
267
926
  async getCurrentTheme() {
268
927
  return this.bridge.send("theme.get");
269
928
  }
270
- async updateToken(t) {
271
- return this.bridge.send("theme.updateToken", t);
929
+ async updateToken(n) {
930
+ return this.bridge.send("theme.updateToken", n);
272
931
  }
273
932
  }
274
- class C {
275
- constructor(t) {
276
- this.bridge = t;
933
+ class pt {
934
+ constructor(n) {
935
+ this.bridge = n;
277
936
  }
278
- async setLocale(t, e) {
279
- return this.bridge.send("i18n.setLocale", { locale: t, messages: e });
937
+ async setLocale(n, t) {
938
+ return this.bridge.send("i18n.setLocale", { locale: n, messages: t });
280
939
  }
281
940
  async getCurrentLocale() {
282
941
  return this.bridge.send("i18n.getLocale");
283
942
  }
284
- async updateMessages(t) {
285
- return this.bridge.send("i18n.updateMessages", t);
943
+ async updateMessages(n) {
944
+ return this.bridge.send("i18n.updateMessages", n);
286
945
  }
287
946
  }
288
- class D {
289
- constructor(t) {
290
- this.bridge = t;
947
+ class vt {
948
+ constructor(n) {
949
+ this.bridge = n;
291
950
  }
292
951
  async validateToken() {
293
952
  return this.bridge.send("auth.validateToken");
294
953
  }
295
954
  }
296
- const r = {
955
+ const O = {
297
956
  READY: "ready",
298
957
  CHANGE: "change",
299
958
  SAVE: "save",
@@ -315,23 +974,23 @@ const r = {
315
974
  KEY_DOWN: "key-down",
316
975
  KEY_UP: "key-up"
317
976
  };
318
- class O extends h {
319
- constructor(e, s) {
977
+ class gt extends ge {
978
+ constructor(t, s) {
320
979
  super();
321
- i(this, "bridge");
322
- i(this, "canvasAPI");
323
- i(this, "abilityAPI");
324
- i(this, "themeAPI");
325
- i(this, "i18nAPI");
326
- i(this, "authAPI");
327
- i(this, "config");
328
- i(this, "iframe");
329
- i(this, "isInitialized", !1);
330
- this.validateConfig(s), this.config = s, this.iframe = e, this.bridge = new v(e, {
980
+ g(this, "bridge");
981
+ g(this, "canvasAPI");
982
+ g(this, "abilityAPI");
983
+ g(this, "themeAPI");
984
+ g(this, "i18nAPI");
985
+ g(this, "authAPI");
986
+ g(this, "config");
987
+ g(this, "iframe");
988
+ g(this, "isInitialized", !1);
989
+ this.validateConfig(s), this.config = s, this.iframe = t, this.bridge = new ht(t, {
331
990
  debug: s.debug || !1,
332
991
  timeout: 3e4,
333
- childOrigin: this.extractOrigin(e.src)
334
- }), this.canvasAPI = new E(this.bridge), this.abilityAPI = new w(this.bridge), this.themeAPI = new P(this.bridge), this.i18nAPI = new C(this.bridge), this.authAPI = new D(this.bridge), this.setupEventHandlers();
992
+ childOrigin: this.extractOrigin(t.src)
993
+ }), this.canvasAPI = new ut(this.bridge), this.abilityAPI = new lt(this.bridge), this.themeAPI = new ft(this.bridge), this.i18nAPI = new pt(this.bridge), this.authAPI = new vt(this.bridge), this.setupEventHandlers();
335
994
  }
336
995
  /**
337
996
  * 初始化SDK
@@ -341,63 +1000,63 @@ class O extends h {
341
1000
  try {
342
1001
  await this.bridge.connect({
343
1002
  getConfig: () => this.config,
344
- log: (e, s) => {
345
- console.log(`[Canvas ${e.toUpperCase()}]`, s);
1003
+ log: (t, s) => {
1004
+ console.log(`[Canvas ${t.toUpperCase()}]`, s);
346
1005
  }
347
- }), await this.canvasAPI.initialize(this.config), this.config.abilities && await this.abilityAPI.setAbilities(this.config.abilities), this.config.theme && await this.themeAPI.setTheme(this.config.theme), this.config.locale && await this.i18nAPI.setLocale(this.config.locale, this.config.messages), this.isInitialized = !0, this.emit(r.READY);
348
- } catch (e) {
349
- throw this.emit(r.ERROR, e), e;
1006
+ }), await this.canvasAPI.initialize(this.config), this.config.abilities && await this.abilityAPI.setAbilities(this.config.abilities), this.config.theme && await this.themeAPI.setTheme(this.config.theme), this.config.locale && await this.i18nAPI.setLocale(this.config.locale, this.config.messages), this.isInitialized = !0, this.emit(O.READY);
1007
+ } catch (t) {
1008
+ throw this.emit(O.ERROR, t), t;
350
1009
  }
351
1010
  }
352
1011
  // Canvas 操作方法
353
1012
  get canvas() {
354
1013
  return {
355
- setData: (e) => this.canvasAPI.setData(e),
1014
+ setData: (t) => this.canvasAPI.setData(t),
356
1015
  getData: () => this.canvasAPI.getData(),
357
1016
  clear: () => this.canvasAPI.clear(),
358
1017
  undo: () => this.canvasAPI.undo(),
359
1018
  redo: () => this.canvasAPI.redo(),
360
- export: (e = "png") => this.canvasAPI.export(e),
1019
+ export: (t = "png") => this.canvasAPI.export(t),
361
1020
  save: () => this.canvasAPI.save(),
362
- setZoom: (e) => this.canvasAPI.setZoom(e),
1021
+ setZoom: (t) => this.canvasAPI.setZoom(t),
363
1022
  getSnapshot: () => this.canvasAPI.getSnapshot(),
364
- invoke: (e, ...s) => this.canvasAPI.invoke(e, ...s)
1023
+ invoke: (t, ...s) => this.canvasAPI.invoke(t, ...s)
365
1024
  };
366
1025
  }
367
1026
  // 能力管理
368
1027
  get abilities() {
369
1028
  return {
370
- set: (e) => this.abilityAPI.setAbilities(e),
1029
+ set: (t) => this.abilityAPI.setAbilities(t),
371
1030
  get: () => this.abilityAPI.getAbilities(),
372
- enable: (e) => this.abilityAPI.enableAbility(e),
373
- disable: (e) => this.abilityAPI.disableAbility(e),
374
- updateConfig: (e, s) => this.abilityAPI.updateAbilityConfig(e, s)
1031
+ enable: (t) => this.abilityAPI.enableAbility(t),
1032
+ disable: (t) => this.abilityAPI.disableAbility(t),
1033
+ updateConfig: (t, s) => this.abilityAPI.updateAbilityConfig(t, s)
375
1034
  };
376
1035
  }
377
1036
  // 主题管理
378
1037
  get theme() {
379
1038
  return {
380
- set: (e) => this.themeAPI.setTheme(e),
1039
+ set: (t) => this.themeAPI.setTheme(t),
381
1040
  get: () => this.themeAPI.getCurrentTheme(),
382
- updateToken: (e) => this.themeAPI.updateToken(e),
1041
+ updateToken: (t) => this.themeAPI.updateToken(t),
383
1042
  reset: () => this.themeAPI.resetTheme()
384
1043
  };
385
1044
  }
386
1045
  // 国际化管理
387
1046
  get i18n() {
388
1047
  return {
389
- setLocale: (e, s) => this.i18nAPI.setLocale(e, s),
1048
+ setLocale: (t, s) => this.i18nAPI.setLocale(t, s),
390
1049
  getLocale: () => this.i18nAPI.getCurrentLocale(),
391
- updateMessages: (e) => this.i18nAPI.updateMessages(e),
1050
+ updateMessages: (t) => this.i18nAPI.updateMessages(t),
392
1051
  getMessages: () => this.i18nAPI.getMessages(),
393
- getTranslation: (e) => this.i18nAPI.getTranslation(e)
1052
+ getTranslation: (t) => this.i18nAPI.getTranslation(t)
394
1053
  };
395
1054
  }
396
1055
  // 鉴权管理
397
1056
  get auth() {
398
1057
  return {
399
1058
  validateToken: () => this.authAPI.validateToken(),
400
- refreshToken: (e) => this.authAPI.refreshToken(e),
1059
+ refreshToken: (t) => this.authAPI.refreshToken(t),
401
1060
  getTokenInfo: () => this.authAPI.getTokenInfo()
402
1061
  };
403
1062
  }
@@ -413,43 +1072,43 @@ class O extends h {
413
1072
  };
414
1073
  }
415
1074
  setupEventHandlers() {
416
- this.bridge.on("canvas-change", (e) => {
417
- this.emit(r.CHANGE, e);
418
- }), this.bridge.on("canvas-save", (e) => {
419
- this.emit(r.SAVE, e);
420
- }), this.bridge.on("canvas-error", (e) => {
421
- this.emit(r.ERROR, e);
422
- }), this.bridge.on("canvas-selection-changed", (e) => {
423
- this.emit(r.SELECTION_CHANGED, e);
1075
+ this.bridge.on("canvas-change", (t) => {
1076
+ this.emit(O.CHANGE, t);
1077
+ }), this.bridge.on("canvas-save", (t) => {
1078
+ this.emit(O.SAVE, t);
1079
+ }), this.bridge.on("canvas-error", (t) => {
1080
+ this.emit(O.ERROR, t);
1081
+ }), this.bridge.on("canvas-selection-changed", (t) => {
1082
+ this.emit(O.SELECTION_CHANGED, t);
424
1083
  }), this.bridge.on("connected", () => {
425
- console.log("链接建立成功"), this.emit(r.CONNECTED);
1084
+ console.log("链接建立成功"), this.emit(O.CONNECTED);
426
1085
  }), this.bridge.on("disconnected", () => {
427
- console.log("链接断开"), this.emit(r.DISCONNECTED);
1086
+ console.log("链接断开"), this.emit(O.DISCONNECTED);
428
1087
  });
429
1088
  }
430
- validateConfig(e) {
431
- if (!e.token)
1089
+ validateConfig(t) {
1090
+ if (!t.token)
432
1091
  throw new Error("Token is required");
433
- if (e.abilities) {
1092
+ if (t.abilities) {
434
1093
  const s = ["text", "image", "barcode", "multiLayout", "shape", "qrCode"];
435
- Object.keys(e.abilities).forEach((a) => {
436
- s.includes(a) || console.warn(`Unknown ability: ${a}`);
1094
+ Object.keys(t.abilities).forEach((r) => {
1095
+ s.includes(r) || console.warn(`Unknown ability: ${r}`);
437
1096
  });
438
1097
  }
439
1098
  }
440
- extractOrigin(e) {
1099
+ extractOrigin(t) {
441
1100
  try {
442
- return new URL(e).origin;
1101
+ return new URL(t).origin;
443
1102
  } catch {
444
1103
  return "*";
445
1104
  }
446
1105
  }
447
1106
  }
448
- function S(n, t) {
449
- return new O(n, t);
1107
+ function mt(e, n) {
1108
+ return new gt(e, n);
450
1109
  }
451
1110
  export {
452
- r as CANVAS_EVENTS,
453
- O as NiimbotCanvasSDK,
454
- S as createCanvas
1111
+ O as CANVAS_EVENTS,
1112
+ gt as NiimbotCanvasSDK,
1113
+ mt as createCanvas
455
1114
  };
@@ -1 +1 @@
1
- (function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("penpal"),require("eventemitter3"),require("nanoid")):typeof define=="function"&&define.amd?define(["exports","penpal","eventemitter3","nanoid"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.NiimbotCanvasSDK={},n.Penpal,n.EventEmitter3,n.nanoid))})(this,function(n,a,h,m){"use strict";var D=Object.defineProperty;var T=(n,a,h)=>a in n?D(n,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):n[a]=h;var i=(n,a,h)=>(T(n,typeof a!="symbol"?a+"":a,h),h);class g extends h{on(t,e,s){return super.on(t,e,s)}once(t,e,s){return super.once(t,e,s)}off(t,e,s){return super.off(t,e,s)}emit(t,...e){return super.emit(t,...e)}}class b{constructor(){i(this,"queue",[]);i(this,"maxSize",100)}enqueue(t,e=3){this.queue.length>=this.maxSize&&this.queue.shift();const s={...t,retryCount:0,maxRetries:e};return this.queue.push(s),t.messageId}dequeue(){return this.queue.shift()}hasMessages(){return this.queue.length>0}getMessageById(t){return this.queue.find(e=>e.messageId===t)}removeMessageById(t){const e=this.queue.findIndex(s=>s.messageId===t);e!==-1&&this.queue.splice(e,1)}clear(){this.queue=[]}}class A{constructor(t,e=!1){this.context=t,this.debug=e}formatMessage(t,...e){return[`[${new Date().toISOString()}] [${this.context}] [${t}]`,...e]}info(...t){console.info(...this.formatMessage("INFO",...t))}debug(...t){this.debug&&console.debug(...this.formatMessage("DEBUG",...t))}warn(...t){console.warn(...this.formatMessage("WARN",...t))}error(...t){console.error(...this.formatMessage("ERROR",...t))}}const f="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",I=m.customAlphabet(f,12);class v extends g{constructor(e,s={}){super();i(this,"connection",null);i(this,"iframe");i(this,"options");i(this,"messageQueue");i(this,"logger");i(this,"isConnected",!1);i(this,"reconnectAttempts",0);i(this,"maxReconnectAttempts",3);this.iframe=e,this.options={timeout:3e4,debug:!1,retryCount:3,retryDelay:1e3,childOrigin:"*",...s},this.messageQueue=new b,this.logger=new A("PenpalBridge",this.options.debug)}async connect(e={}){try{this.logger.info("正在建立连接...");const s=new a.WindowMessenger({remoteWindow:this.iframe,allowedOrigins:["http://127.0.0.1:3000"]});this.connection=a.connect({messenger:s,methods:{add(o,d){return o+d},emitEvent:(o,d)=>{this.emit(o,d)},...e},timeout:this.options.timeout,log:a.debug("parent")}),await this.connection.promise,this.isConnected=!0,this.reconnectAttempts=0,await this.processQueuedMessages(),this.logger.info("连接建立成功"),this.emit("connected")}catch(s){throw this.logger.error("连接失败:",s),await this.handleReconnect(),s}}async send(e,s){var d;const o={action:e,params:s,timestamp:Date.now(),messageId:I()};if(!this.isConnected)throw this.messageQueue.enqueue(o),new Error("Connection not ready, message queued");try{if(this.logger.debug("发送消息:",o),!((d=this.connection)!=null&&d.child))throw new Error("连接未就绪");const u=await this.connection.child.handleMessage(o);return this.logger.debug("收到响应:",u),this.validateResponse(u)}catch(u){throw this.logger.error("发送消息失败:",u),this.messageQueue.enqueue(o),u}}async processQueuedMessages(){for(;this.messageQueue.hasMessages();){const e=this.messageQueue.dequeue();if(e)try{await this.send(e.action,e.params)}catch(s){this.logger.warn("队列消息发送失败:",s),this.messageQueue.enqueue(e)}}}async handleReconnect(){if(this.reconnectAttempts>=this.maxReconnectAttempts){this.logger.error("重连次数达到上限"),this.emit("connection-lost");return}this.reconnectAttempts++,this.logger.info(`尝试重连 (${this.reconnectAttempts}/${this.maxReconnectAttempts})`),await new Promise(e=>setTimeout(e,this.options.retryDelay*this.reconnectAttempts));try{await this.connect()}catch{await this.handleReconnect()}}validateResponse(e){if(!e||e.success===void 0)throw new Error("Invalid response format");if(!e.success){const s=e.error||{code:"UNKNOWN",message:"Unknown error"};throw new Error(`${s.code}: ${s.message}`)}return e.data}disconnect(){this.connection&&(this.connection.destroy(),this.isConnected=!1,this.emit("disconnected"),this.logger.info("连接已断开"))}getConnectionStatus(){return this.isConnected}}class p{constructor(t){this.bridge=t}async initialize(t){return this.bridge.send("canvas.initialize",t)}async setData(t){return this.bridge.send("canvas.setData",t)}async getData(){return this.bridge.send("canvas.getData")}async clear(){return this.bridge.send("canvas.clear")}async undo(){return this.bridge.send("canvas.undo")}async redo(){return this.bridge.send("canvas.redo")}async export(t){return this.bridge.send("canvas.export",{format:t})}async save(){return this.bridge.send("canvas.save")}async invoke(t,...e){return this.bridge.send("canvas.invoke",{method:t,args:e})}async setZoom(t){return this.bridge.send("canvas.setZoom",{zoom:t})}async getSnapshot(){return this.bridge.send("canvas.getSnapshot")}}class y{constructor(t){this.bridge=t}async setAbilities(t){return this.bridge.send("abilities.set",t)}async getAbilities(){return this.bridge.send("abilities.get")}async enableAbility(t){return this.bridge.send("abilities.enable",{name:t})}async disableAbility(t){return this.bridge.send("abilities.disable",{name:t})}}class E{constructor(t){this.bridge=t}async setTheme(t){return this.bridge.send("theme.set",{theme:t})}async getCurrentTheme(){return this.bridge.send("theme.get")}async updateToken(t){return this.bridge.send("theme.updateToken",t)}}class w{constructor(t){this.bridge=t}async setLocale(t,e){return this.bridge.send("i18n.setLocale",{locale:t,messages:e})}async getCurrentLocale(){return this.bridge.send("i18n.getLocale")}async updateMessages(t){return this.bridge.send("i18n.updateMessages",t)}}class P{constructor(t){this.bridge=t}async validateToken(){return this.bridge.send("auth.validateToken")}}const c={READY:"ready",CHANGE:"change",SAVE:"save",ERROR:"error",SELECTION_CHANGED:"selection-changed",CONNECTED:"connected",DISCONNECTED:"disconnected",LOADED:"loaded",BEFORE_UNLOAD:"before-unload",OBJECT_ADDED:"object-added",OBJECT_MODIFIED:"object-modified",OBJECT_REMOVED:"object-removed",MOUSE_DOWN:"mouse-down",MOUSE_UP:"mouse-up",MOUSE_MOVE:"mouse-move",KEY_DOWN:"key-down",KEY_UP:"key-up"};class l extends g{constructor(e,s){super();i(this,"bridge");i(this,"canvasAPI");i(this,"abilityAPI");i(this,"themeAPI");i(this,"i18nAPI");i(this,"authAPI");i(this,"config");i(this,"iframe");i(this,"isInitialized",!1);this.validateConfig(s),this.config=s,this.iframe=e,this.bridge=new v(e,{debug:s.debug||!1,timeout:3e4,childOrigin:this.extractOrigin(e.src)}),this.canvasAPI=new p(this.bridge),this.abilityAPI=new y(this.bridge),this.themeAPI=new E(this.bridge),this.i18nAPI=new w(this.bridge),this.authAPI=new P(this.bridge),this.setupEventHandlers()}async init(){if(!this.isInitialized)try{await this.bridge.connect({getConfig:()=>this.config,log:(e,s)=>{console.log(`[Canvas ${e.toUpperCase()}]`,s)}}),await this.canvasAPI.initialize(this.config),this.config.abilities&&await this.abilityAPI.setAbilities(this.config.abilities),this.config.theme&&await this.themeAPI.setTheme(this.config.theme),this.config.locale&&await this.i18nAPI.setLocale(this.config.locale,this.config.messages),this.isInitialized=!0,this.emit(c.READY)}catch(e){throw this.emit(c.ERROR,e),e}}get canvas(){return{setData:e=>this.canvasAPI.setData(e),getData:()=>this.canvasAPI.getData(),clear:()=>this.canvasAPI.clear(),undo:()=>this.canvasAPI.undo(),redo:()=>this.canvasAPI.redo(),export:(e="png")=>this.canvasAPI.export(e),save:()=>this.canvasAPI.save(),setZoom:e=>this.canvasAPI.setZoom(e),getSnapshot:()=>this.canvasAPI.getSnapshot(),invoke:(e,...s)=>this.canvasAPI.invoke(e,...s)}}get abilities(){return{set:e=>this.abilityAPI.setAbilities(e),get:()=>this.abilityAPI.getAbilities(),enable:e=>this.abilityAPI.enableAbility(e),disable:e=>this.abilityAPI.disableAbility(e),updateConfig:(e,s)=>this.abilityAPI.updateAbilityConfig(e,s)}}get theme(){return{set:e=>this.themeAPI.setTheme(e),get:()=>this.themeAPI.getCurrentTheme(),updateToken:e=>this.themeAPI.updateToken(e),reset:()=>this.themeAPI.resetTheme()}}get i18n(){return{setLocale:(e,s)=>this.i18nAPI.setLocale(e,s),getLocale:()=>this.i18nAPI.getCurrentLocale(),updateMessages:e=>this.i18nAPI.updateMessages(e),getMessages:()=>this.i18nAPI.getMessages(),getTranslation:e=>this.i18nAPI.getTranslation(e)}}get auth(){return{validateToken:()=>this.authAPI.validateToken(),refreshToken:e=>this.authAPI.refreshToken(e),getTokenInfo:()=>this.authAPI.getTokenInfo()}}get utils(){return{getConnectionStatus:()=>this.bridge.getConnectionStatus(),disconnect:()=>this.bridge.disconnect(),reconnect:async()=>{this.bridge.disconnect(),this.isInitialized=!1,await this.init()},isInitialized:()=>this.isInitialized}}setupEventHandlers(){this.bridge.on("canvas-change",e=>{this.emit(c.CHANGE,e)}),this.bridge.on("canvas-save",e=>{this.emit(c.SAVE,e)}),this.bridge.on("canvas-error",e=>{this.emit(c.ERROR,e)}),this.bridge.on("canvas-selection-changed",e=>{this.emit(c.SELECTION_CHANGED,e)}),this.bridge.on("connected",()=>{console.log("链接建立成功"),this.emit(c.CONNECTED)}),this.bridge.on("disconnected",()=>{console.log("链接断开"),this.emit(c.DISCONNECTED)})}validateConfig(e){if(!e.token)throw new Error("Token is required");if(e.abilities){const s=["text","image","barcode","multiLayout","shape","qrCode"];Object.keys(e.abilities).forEach(o=>{s.includes(o)||console.warn(`Unknown ability: ${o}`)})}}extractOrigin(e){try{return new URL(e).origin}catch{return"*"}}}function C(r,t){return new l(r,t)}n.CANVAS_EVENTS=c,n.NiimbotCanvasSDK=l,n.createCanvas=C,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
1
+ var vt=Object.defineProperty;var gt=(m,E,v)=>E in m?vt(m,E,{enumerable:!0,configurable:!0,writable:!0,value:v}):m[E]=v;var y=(m,E,v)=>(gt(m,typeof E!="symbol"?E+"":E,v),v),Ae=(m,E,v)=>{if(!E.has(m))throw TypeError("Cannot "+v)};var h=(m,E,v)=>(Ae(m,E,"read from private field"),v?v.call(m):E.get(m)),I=(m,E,v)=>{if(E.has(m))throw TypeError("Cannot add the same private member more than once");E instanceof WeakSet?E.add(m):E.set(m,v)},O=(m,E,v,z)=>(Ae(m,E,"write to private field"),z?z.call(m,v):E.set(m,v),v);(function(m,E){typeof exports=="object"&&typeof module<"u"?E(exports):typeof define=="function"&&define.amd?define(["exports"],E):(m=typeof globalThis<"u"?globalThis:m||self,E(m.NiimbotCanvasSDK={}))})(this,function(m){var ee,ge,ye,te,me,R,k,U,$,L,D,S,x,B,j,F,V,q,be;"use strict";var E=class extends Error{constructor(n,t){super(t);y(this,"code");this.name="PenpalError",this.code=n}},v=E,z=e=>({name:e.name,message:e.message,stack:e.stack,penpalCode:e instanceof v?e.code:void 0}),Ee=({name:e,message:n,stack:t,penpalCode:s})=>{const r=s?new v(s,n):new Error(n);return r.name=e,r.stack=t,r},we=Symbol("Reply"),Ie=(ge=class{constructor(e,n){y(this,"value");y(this,"transferables");I(this,ee,we);this.value=e,this.transferables=n==null?void 0:n.transferables}},ee=new WeakMap,ge),Me=Ie,C="penpal",Y=e=>typeof e=="object"&&e!==null,ne=e=>typeof e=="function",Pe=e=>Y(e)&&e.namespace===C,K=e=>e.type==="SYN",J=e=>e.type==="ACK1",Z=e=>e.type==="ACK2",se=e=>e.type==="CALL",re=e=>e.type==="REPLY",Ce=e=>e.type==="DESTROY",ie=(e,n=[])=>{const t=[];for(const s of Object.keys(e)){const r=e[s];ne(r)?t.push([...n,s]):Y(r)&&t.push(...ie(r,[...n,s]))}return t},Se=(e,n)=>{const t=e.reduce((s,r)=>Y(s)?s[r]:void 0,n);return ne(t)?t:void 0},N=e=>e.join("."),ae=(e,n,t)=>({namespace:C,channel:e,type:"REPLY",callId:n,isError:!0,...t instanceof Error?{value:z(t),isSerializedErrorInstance:!0}:{value:t}}),_e=(e,n,t,s)=>{let r=!1;const g=async u=>{if(r||!se(u))return;s==null||s(`Received ${N(u.methodPath)}() call`,u);const{methodPath:l,args:c,id:a}=u;let i,o;try{const f=Se(l,n);if(!f)throw new v("METHOD_NOT_FOUND",`Method \`${N(l)}\` is not found.`);let p=await f(...c);p instanceof Me&&(o=p.transferables,p=await p.value),i={namespace:C,channel:t,type:"REPLY",callId:a,value:p}}catch(f){i=ae(t,a,f)}if(!r)try{s==null||s(`Sending ${N(l)}() reply`,i),e.sendMessage(i,o)}catch(f){throw f.name==="DataCloneError"&&(i=ae(t,a,f),s==null||s(`Sending ${N(l)}() reply`,i),e.sendMessage(i)),f}};return e.addMessageHandler(g),()=>{r=!0,e.removeMessageHandler(g)}},Oe=_e,oe=((ye=crypto.randomUUID)==null?void 0:ye.bind(crypto))??(()=>new Array(4).fill(0).map(()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER).toString(16)).join("-")),Te=Symbol("CallOptions"),Ne=(me=class{constructor(e){y(this,"transferables");y(this,"timeout");I(this,te,Te);this.transferables=e==null?void 0:e.transferables,this.timeout=e==null?void 0:e.timeout}},te=new WeakMap,me),Re=Ne,De=new Set(["apply","call","bind"]),ce=(e,n,t=[])=>new Proxy(t.length?()=>{}:Object.create(null),{get(s,r){if(r!=="then")return t.length&&De.has(r)?Reflect.get(s,r):ce(e,n,[...t,r])},apply(s,r,g){return e(t,g)}}),de=e=>new v("CONNECTION_DESTROYED",`Method call ${N(e)}() failed due to destroyed connection`),ke=(e,n,t)=>{let s=!1;const r=new Map,g=c=>{if(!re(c))return;const{callId:a,value:i,isError:o,isSerializedErrorInstance:f}=c,p=r.get(a);p&&(r.delete(a),t==null||t(`Received ${N(p.methodPath)}() call`,c),o?p.reject(f?Ee(i):i):p.resolve(i))};return e.addMessageHandler(g),{remoteProxy:ce((c,a)=>{if(s)throw de(c);const i=oe(),o=a[a.length-1],f=o instanceof Re,{timeout:p,transferables:w}=f?o:{},M=f?a.slice(0,-1):a;return new Promise((d,P)=>{const _=p!==void 0?window.setTimeout(()=>{r.delete(i),P(new v("METHOD_CALL_TIMEOUT",`Method call ${N(c)}() timed out after ${p}ms`))},p):void 0;r.set(i,{methodPath:c,resolve:d,reject:P,timeoutId:_});try{const b={namespace:C,channel:n,type:"CALL",id:i,methodPath:c,args:M};t==null||t(`Sending ${N(c)}() call`,b),e.sendMessage(b,w)}catch(b){P(new v("TRANSMISSION_FAILED",b.message))}})},t),destroy:()=>{s=!0,e.removeMessageHandler(g);for(const{methodPath:c,reject:a,timeoutId:i}of r.values())clearTimeout(i),a(de(c));r.clear()}}},Le=ke,xe=()=>{let e,n;return{promise:new Promise((s,r)=>{e=s,n=r}),resolve:e,reject:n}},He=xe,Ue=class extends Error{constructor(e){super(`You've hit a bug in Penpal. Please file an issue with the following information: ${e}`)}},W=Ue,X="deprecated-penpal",$e=e=>Y(e)&&"penpal"in e,je=e=>e.split("."),he=e=>e.join("."),ue=e=>new W(`Unexpected message to translate: ${JSON.stringify(e)}`),qe=e=>{if(e.penpal==="syn")return{namespace:C,channel:void 0,type:"SYN",participantId:X};if(e.penpal==="ack")return{namespace:C,channel:void 0,type:"ACK2"};if(e.penpal==="call")return{namespace:C,channel:void 0,type:"CALL",id:e.id,methodPath:je(e.methodName),args:e.args};if(e.penpal==="reply")return e.resolution==="fulfilled"?{namespace:C,channel:void 0,type:"REPLY",callId:e.id,value:e.returnValue}:{namespace:C,channel:void 0,type:"REPLY",callId:e.id,isError:!0,...e.returnValueIsError?{value:e.returnValue,isSerializedErrorInstance:!0}:{value:e.returnValue}};throw ue(e)},Fe=e=>{if(J(e))return{penpal:"synAck",methodNames:e.methodPaths.map(he)};if(se(e))return{penpal:"call",id:e.id,methodName:he(e.methodPath),args:e.args};if(re(e))return e.isError?{penpal:"reply",id:e.callId,resolution:"rejected",...e.isSerializedErrorInstance?{returnValue:e.value,returnValueIsError:!0}:{returnValue:e.value}}:{penpal:"reply",id:e.callId,resolution:"fulfilled",returnValue:e.value};throw ue(e)},Ve=({messenger:e,methods:n,timeout:t,channel:s,log:r})=>{const g=oe();let u;const l=[];let c=!1;const a=ie(n),{promise:i,resolve:o,reject:f}=He(),p=t!==void 0?setTimeout(()=>{f(new v("CONNECTION_TIMEOUT",`Connection timed out after ${t}ms`))},t):void 0,w=()=>{for(const A of l)A()},M=()=>{if(c)return;l.push(Oe(e,n,s,r));const{remoteProxy:A,destroy:H}=Le(e,s,r);l.push(H),clearTimeout(p),c=!0,o({remoteProxy:A,destroy:w})},d=()=>{const A={namespace:C,type:"SYN",channel:s,participantId:g};r==null||r("Sending handshake SYN",A);try{e.sendMessage(A)}catch(H){f(new v("TRANSMISSION_FAILED",H.message))}},P=A=>{if(r==null||r("Received handshake SYN",A),A.participantId===u&&u!==X||(u=A.participantId,d(),!(g>u||u===X)))return;const G={namespace:C,channel:s,type:"ACK1",methodPaths:a};r==null||r("Sending handshake ACK1",G);try{e.sendMessage(G)}catch(pt){f(new v("TRANSMISSION_FAILED",pt.message));return}},_=A=>{r==null||r("Received handshake ACK1",A);const H={namespace:C,channel:s,type:"ACK2"};r==null||r("Sending handshake ACK2",H);try{e.sendMessage(H)}catch(G){f(new v("TRANSMISSION_FAILED",G.message));return}M()},b=A=>{r==null||r("Received handshake ACK2",A),M()},Q=A=>{K(A)&&P(A),J(A)&&_(A),Z(A)&&b(A)};return e.addMessageHandler(Q),l.push(()=>e.removeMessageHandler(Q)),d(),i},ze=Ve,Ye=e=>{let n=!1,t;return(...s)=>(n||(n=!0,t=e(...s)),t)},Ke=Ye,le=new WeakSet,We=({messenger:e,methods:n={},timeout:t,channel:s,log:r})=>{if(!e)throw new v("INVALID_ARGUMENT","messenger must be defined");if(le.has(e))throw new v("INVALID_ARGUMENT","A messenger can only be used for a single connection");le.add(e);const g=[e.destroy],u=Ke(a=>{if(a){const i={namespace:C,channel:s,type:"DESTROY"};try{e.sendMessage(i)}catch{}}for(const i of g)i();r==null||r("Connection destroyed")}),l=a=>Pe(a)&&a.channel===s;return{promise:(async()=>{try{e.initialize({log:r,validateReceivedMessage:l}),e.addMessageHandler(o=>{Ce(o)&&u(!1)});const{remoteProxy:a,destroy:i}=await ze({messenger:e,methods:n,timeout:t,channel:s,log:r});return g.push(i),a}catch(a){throw u(!0),a}})(),destroy:()=>{u(!0)}}},Be=We,Qe=(be=class{constructor({remoteWindow:e,allowedOrigins:n}){I(this,R,void 0);I(this,k,void 0);I(this,U,void 0);I(this,$,void 0);I(this,L,void 0);I(this,D,new Set);I(this,S,void 0);I(this,x,!1);y(this,"initialize",({log:e,validateReceivedMessage:n})=>{O(this,U,e),O(this,$,n),window.addEventListener("message",h(this,V))});y(this,"sendMessage",(e,n)=>{if(K(e)){const t=h(this,j).call(this,e);h(this,R).postMessage(e,{targetOrigin:t,transfer:n});return}if(J(e)||h(this,x)){const t=h(this,x)?Fe(e):e,s=h(this,j).call(this,e);h(this,R).postMessage(t,{targetOrigin:s,transfer:n});return}if(Z(e)){const{port1:t,port2:s}=new MessageChannel;O(this,S,t),t.addEventListener("message",h(this,q)),t.start();const r=[s,...n||[]],g=h(this,j).call(this,e);h(this,R).postMessage(e,{targetOrigin:g,transfer:r});return}if(h(this,S)){h(this,S).postMessage(e,{transfer:n});return}throw new W("Port is undefined")});y(this,"addMessageHandler",e=>{h(this,D).add(e)});y(this,"removeMessageHandler",e=>{h(this,D).delete(e)});y(this,"destroy",()=>{window.removeEventListener("message",h(this,V)),h(this,F).call(this),h(this,D).clear()});I(this,B,e=>h(this,k).some(n=>n instanceof RegExp?n.test(e):n===e||n==="*"));I(this,j,e=>{if(K(e))return"*";if(!h(this,L))throw new W("Concrete remote origin not set");return h(this,L)==="null"&&h(this,k).includes("*")?"*":h(this,L)});I(this,F,()=>{var e,n;(e=h(this,S))==null||e.removeEventListener("message",h(this,q)),(n=h(this,S))==null||n.close(),O(this,S,void 0)});I(this,V,({source:e,origin:n,ports:t,data:s})=>{var r,g,u;if(e===h(this,R)&&($e(s)&&((r=h(this,U))==null||r.call(this,"Please upgrade the child window to the latest version of Penpal."),O(this,x,!0),s=qe(s)),!!((g=h(this,$))!=null&&g.call(this,s)))){if(!h(this,B).call(this,n)){(u=h(this,U))==null||u.call(this,`Received a message from origin \`${n}\` which did not match allowed origins \`[${h(this,k).join(", ")}]\``);return}if(K(s)&&(h(this,F).call(this),O(this,L,n)),Z(s)&&!h(this,x)){if(O(this,S,t[0]),!h(this,S))throw new W("No port received on ACK2");h(this,S).addEventListener("message",h(this,q)),h(this,S).start()}for(const l of h(this,D))l(s)}});I(this,q,({data:e})=>{var n;if((n=h(this,$))!=null&&n.call(this,e))for(const t of h(this,D))t(e)});if(!e)throw new v("INVALID_ARGUMENT","remoteWindow must be defined");O(this,R,e),O(this,k,n!=null&&n.length?n:[window.origin])}},R=new WeakMap,k=new WeakMap,U=new WeakMap,$=new WeakMap,L=new WeakMap,D=new WeakMap,S=new WeakMap,x=new WeakMap,B=new WeakMap,j=new WeakMap,F=new WeakMap,V=new WeakMap,q=new WeakMap,be),Ge=Qe,Je=e=>(...n)=>{console.log(`✍️ %c${e}%c`,"font-weight: bold;","",...n)},Ze=Je;function Xe(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var fe={exports:{}};(function(e){var n=Object.prototype.hasOwnProperty,t="~";function s(){}Object.create&&(s.prototype=Object.create(null),new s().__proto__||(t=!1));function r(c,a,i){this.fn=c,this.context=a,this.once=i||!1}function g(c,a,i,o,f){if(typeof i!="function")throw new TypeError("The listener must be a function");var p=new r(i,o||c,f),w=t?t+a:a;return c._events[w]?c._events[w].fn?c._events[w]=[c._events[w],p]:c._events[w].push(p):(c._events[w]=p,c._eventsCount++),c}function u(c,a){--c._eventsCount===0?c._events=new s:delete c._events[a]}function l(){this._events=new s,this._eventsCount=0}l.prototype.eventNames=function(){var a=[],i,o;if(this._eventsCount===0)return a;for(o in i=this._events)n.call(i,o)&&a.push(t?o.slice(1):o);return Object.getOwnPropertySymbols?a.concat(Object.getOwnPropertySymbols(i)):a},l.prototype.listeners=function(a){var i=t?t+a:a,o=this._events[i];if(!o)return[];if(o.fn)return[o.fn];for(var f=0,p=o.length,w=new Array(p);f<p;f++)w[f]=o[f].fn;return w},l.prototype.listenerCount=function(a){var i=t?t+a:a,o=this._events[i];return o?o.fn?1:o.length:0},l.prototype.emit=function(a,i,o,f,p,w){var M=t?t+a:a;if(!this._events[M])return!1;var d=this._events[M],P=arguments.length,_,b;if(d.fn){switch(d.once&&this.removeListener(a,d.fn,void 0,!0),P){case 1:return d.fn.call(d.context),!0;case 2:return d.fn.call(d.context,i),!0;case 3:return d.fn.call(d.context,i,o),!0;case 4:return d.fn.call(d.context,i,o,f),!0;case 5:return d.fn.call(d.context,i,o,f,p),!0;case 6:return d.fn.call(d.context,i,o,f,p,w),!0}for(b=1,_=new Array(P-1);b<P;b++)_[b-1]=arguments[b];d.fn.apply(d.context,_)}else{var Q=d.length,A;for(b=0;b<Q;b++)switch(d[b].once&&this.removeListener(a,d[b].fn,void 0,!0),P){case 1:d[b].fn.call(d[b].context);break;case 2:d[b].fn.call(d[b].context,i);break;case 3:d[b].fn.call(d[b].context,i,o);break;case 4:d[b].fn.call(d[b].context,i,o,f);break;default:if(!_)for(A=1,_=new Array(P-1);A<P;A++)_[A-1]=arguments[A];d[b].fn.apply(d[b].context,_)}}return!0},l.prototype.on=function(a,i,o){return g(this,a,i,o,!1)},l.prototype.once=function(a,i,o){return g(this,a,i,o,!0)},l.prototype.removeListener=function(a,i,o,f){var p=t?t+a:a;if(!this._events[p])return this;if(!i)return u(this,p),this;var w=this._events[p];if(w.fn)w.fn===i&&(!f||w.once)&&(!o||w.context===o)&&u(this,p);else{for(var M=0,d=[],P=w.length;M<P;M++)(w[M].fn!==i||f&&!w[M].once||o&&w[M].context!==o)&&d.push(w[M]);d.length?this._events[p]=d.length===1?d[0]:d:u(this,p)}return this},l.prototype.removeAllListeners=function(a){var i;return a?(i=t?t+a:a,this._events[i]&&u(this,i)):(this._events=new s,this._eventsCount=0),this},l.prototype.off=l.prototype.removeListener,l.prototype.addListener=l.prototype.on,l.prefixed=t,l.EventEmitter=l,e.exports=l})(fe);var et=fe.exports;const tt=Xe(et);class pe extends tt{on(n,t,s){return super.on(n,t,s)}once(n,t,s){return super.once(n,t,s)}off(n,t,s){return super.off(n,t,s)}emit(n,...t){return super.emit(n,...t)}}class nt{constructor(){y(this,"queue",[]);y(this,"maxSize",100)}enqueue(n,t=3){this.queue.length>=this.maxSize&&this.queue.shift();const s={...n,retryCount:0,maxRetries:t};return this.queue.push(s),n.messageId}dequeue(){return this.queue.shift()}hasMessages(){return this.queue.length>0}getMessageById(n){return this.queue.find(t=>t.messageId===n)}removeMessageById(n){const t=this.queue.findIndex(s=>s.messageId===n);t!==-1&&this.queue.splice(t,1)}clear(){this.queue=[]}}class st{constructor(n,t=!1){this.context=n,this.debug=t}formatMessage(n,...t){return[`[${new Date().toISOString()}] [${this.context}] [${n}]`,...t]}info(...n){console.info(...this.formatMessage("INFO",...n))}debug(...n){this.debug&&console.debug(...this.formatMessage("DEBUG",...n))}warn(...n){console.warn(...this.formatMessage("WARN",...n))}error(...n){console.error(...this.formatMessage("ERROR",...n))}}let rt=e=>crypto.getRandomValues(new Uint8Array(e)),it=(e,n,t)=>{let s=(2<<Math.log2(e.length-1))-1,r=-~(1.6*s*n/e.length);return(g=n)=>{let u="";for(;;){let l=t(r),c=r|0;for(;c--;)if(u+=e[l[c]&s]||"",u.length>=g)return u}}};const at=((e,n=21)=>it(e,n|0,rt))("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",12);class ot extends pe{constructor(t,s={}){super();y(this,"connection",null);y(this,"iframe");y(this,"options");y(this,"messageQueue");y(this,"logger");y(this,"isConnected",!1);y(this,"reconnectAttempts",0);y(this,"maxReconnectAttempts",3);this.iframe=t,this.options={timeout:3e4,debug:!1,retryCount:3,retryDelay:1e3,childOrigin:"*",...s},this.messageQueue=new nt,this.logger=new st("PenpalBridge",this.options.debug)}async connect(t={}){try{this.logger.info("正在建立连接...");const s=new Ge({remoteWindow:this.iframe,allowedOrigins:["http://127.0.0.1:3000"]});this.connection=Be({messenger:s,methods:{add(r,g){return r+g},emitEvent:(r,g)=>{this.emit(r,g)},...t},timeout:this.options.timeout,log:Ze("parent")}),await this.connection.promise,this.isConnected=!0,this.reconnectAttempts=0,await this.processQueuedMessages(),this.logger.info("连接建立成功"),this.emit("connected")}catch(s){throw this.logger.error("连接失败:",s),await this.handleReconnect(),s}}async send(t,s){var g;const r={action:t,params:s,timestamp:Date.now(),messageId:at()};if(!this.isConnected)throw this.messageQueue.enqueue(r),new Error("Connection not ready, message queued");try{if(this.logger.debug("发送消息:",r),!((g=this.connection)!=null&&g.child))throw new Error("连接未就绪");const u=await this.connection.child.handleMessage(r);return this.logger.debug("收到响应:",u),this.validateResponse(u)}catch(u){throw this.logger.error("发送消息失败:",u),this.messageQueue.enqueue(r),u}}async processQueuedMessages(){for(;this.messageQueue.hasMessages();){const t=this.messageQueue.dequeue();if(t)try{await this.send(t.action,t.params)}catch(s){this.logger.warn("队列消息发送失败:",s),this.messageQueue.enqueue(t)}}}async handleReconnect(){if(this.reconnectAttempts>=this.maxReconnectAttempts){this.logger.error("重连次数达到上限"),this.emit("connection-lost");return}this.reconnectAttempts++,this.logger.info(`尝试重连 (${this.reconnectAttempts}/${this.maxReconnectAttempts})`),await new Promise(t=>setTimeout(t,this.options.retryDelay*this.reconnectAttempts));try{await this.connect()}catch{await this.handleReconnect()}}validateResponse(t){if(!t||t.success===void 0)throw new Error("Invalid response format");if(!t.success){const s=t.error||{code:"UNKNOWN",message:"Unknown error"};throw new Error(`${s.code}: ${s.message}`)}return t.data}disconnect(){this.connection&&(this.connection.destroy(),this.isConnected=!1,this.emit("disconnected"),this.logger.info("连接已断开"))}getConnectionStatus(){return this.isConnected}}class ct{constructor(n){this.bridge=n}async initialize(n){return this.bridge.send("canvas.initialize",n)}async setData(n){return this.bridge.send("canvas.setData",n)}async getData(){return this.bridge.send("canvas.getData")}async clear(){return this.bridge.send("canvas.clear")}async undo(){return this.bridge.send("canvas.undo")}async redo(){return this.bridge.send("canvas.redo")}async export(n){return this.bridge.send("canvas.export",{format:n})}async save(){return this.bridge.send("canvas.save")}async invoke(n,...t){return this.bridge.send("canvas.invoke",{method:n,args:t})}async setZoom(n){return this.bridge.send("canvas.setZoom",{zoom:n})}async getSnapshot(){return this.bridge.send("canvas.getSnapshot")}}class dt{constructor(n){this.bridge=n}async setAbilities(n){return this.bridge.send("abilities.set",n)}async getAbilities(){return this.bridge.send("abilities.get")}async enableAbility(n){return this.bridge.send("abilities.enable",{name:n})}async disableAbility(n){return this.bridge.send("abilities.disable",{name:n})}}class ht{constructor(n){this.bridge=n}async setTheme(n){return this.bridge.send("theme.set",{theme:n})}async getCurrentTheme(){return this.bridge.send("theme.get")}async updateToken(n){return this.bridge.send("theme.updateToken",n)}}class ut{constructor(n){this.bridge=n}async setLocale(n,t){return this.bridge.send("i18n.setLocale",{locale:n,messages:t})}async getCurrentLocale(){return this.bridge.send("i18n.getLocale")}async updateMessages(n){return this.bridge.send("i18n.updateMessages",n)}}class lt{constructor(n){this.bridge=n}async validateToken(){return this.bridge.send("auth.validateToken")}}const T={READY:"ready",CHANGE:"change",SAVE:"save",ERROR:"error",SELECTION_CHANGED:"selection-changed",CONNECTED:"connected",DISCONNECTED:"disconnected",LOADED:"loaded",BEFORE_UNLOAD:"before-unload",OBJECT_ADDED:"object-added",OBJECT_MODIFIED:"object-modified",OBJECT_REMOVED:"object-removed",MOUSE_DOWN:"mouse-down",MOUSE_UP:"mouse-up",MOUSE_MOVE:"mouse-move",KEY_DOWN:"key-down",KEY_UP:"key-up"};class ve extends pe{constructor(t,s){super();y(this,"bridge");y(this,"canvasAPI");y(this,"abilityAPI");y(this,"themeAPI");y(this,"i18nAPI");y(this,"authAPI");y(this,"config");y(this,"iframe");y(this,"isInitialized",!1);this.validateConfig(s),this.config=s,this.iframe=t,this.bridge=new ot(t,{debug:s.debug||!1,timeout:3e4,childOrigin:this.extractOrigin(t.src)}),this.canvasAPI=new ct(this.bridge),this.abilityAPI=new dt(this.bridge),this.themeAPI=new ht(this.bridge),this.i18nAPI=new ut(this.bridge),this.authAPI=new lt(this.bridge),this.setupEventHandlers()}async init(){if(!this.isInitialized)try{await this.bridge.connect({getConfig:()=>this.config,log:(t,s)=>{console.log(`[Canvas ${t.toUpperCase()}]`,s)}}),await this.canvasAPI.initialize(this.config),this.config.abilities&&await this.abilityAPI.setAbilities(this.config.abilities),this.config.theme&&await this.themeAPI.setTheme(this.config.theme),this.config.locale&&await this.i18nAPI.setLocale(this.config.locale,this.config.messages),this.isInitialized=!0,this.emit(T.READY)}catch(t){throw this.emit(T.ERROR,t),t}}get canvas(){return{setData:t=>this.canvasAPI.setData(t),getData:()=>this.canvasAPI.getData(),clear:()=>this.canvasAPI.clear(),undo:()=>this.canvasAPI.undo(),redo:()=>this.canvasAPI.redo(),export:(t="png")=>this.canvasAPI.export(t),save:()=>this.canvasAPI.save(),setZoom:t=>this.canvasAPI.setZoom(t),getSnapshot:()=>this.canvasAPI.getSnapshot(),invoke:(t,...s)=>this.canvasAPI.invoke(t,...s)}}get abilities(){return{set:t=>this.abilityAPI.setAbilities(t),get:()=>this.abilityAPI.getAbilities(),enable:t=>this.abilityAPI.enableAbility(t),disable:t=>this.abilityAPI.disableAbility(t),updateConfig:(t,s)=>this.abilityAPI.updateAbilityConfig(t,s)}}get theme(){return{set:t=>this.themeAPI.setTheme(t),get:()=>this.themeAPI.getCurrentTheme(),updateToken:t=>this.themeAPI.updateToken(t),reset:()=>this.themeAPI.resetTheme()}}get i18n(){return{setLocale:(t,s)=>this.i18nAPI.setLocale(t,s),getLocale:()=>this.i18nAPI.getCurrentLocale(),updateMessages:t=>this.i18nAPI.updateMessages(t),getMessages:()=>this.i18nAPI.getMessages(),getTranslation:t=>this.i18nAPI.getTranslation(t)}}get auth(){return{validateToken:()=>this.authAPI.validateToken(),refreshToken:t=>this.authAPI.refreshToken(t),getTokenInfo:()=>this.authAPI.getTokenInfo()}}get utils(){return{getConnectionStatus:()=>this.bridge.getConnectionStatus(),disconnect:()=>this.bridge.disconnect(),reconnect:async()=>{this.bridge.disconnect(),this.isInitialized=!1,await this.init()},isInitialized:()=>this.isInitialized}}setupEventHandlers(){this.bridge.on("canvas-change",t=>{this.emit(T.CHANGE,t)}),this.bridge.on("canvas-save",t=>{this.emit(T.SAVE,t)}),this.bridge.on("canvas-error",t=>{this.emit(T.ERROR,t)}),this.bridge.on("canvas-selection-changed",t=>{this.emit(T.SELECTION_CHANGED,t)}),this.bridge.on("connected",()=>{console.log("链接建立成功"),this.emit(T.CONNECTED)}),this.bridge.on("disconnected",()=>{console.log("链接断开"),this.emit(T.DISCONNECTED)})}validateConfig(t){if(!t.token)throw new Error("Token is required");if(t.abilities){const s=["text","image","barcode","multiLayout","shape","qrCode"];Object.keys(t.abilities).forEach(r=>{s.includes(r)||console.warn(`Unknown ability: ${r}`)})}}extractOrigin(t){try{return new URL(t).origin}catch{return"*"}}}function ft(e,n){return new ve(e,n)}m.CANVAS_EVENTS=T,m.NiimbotCanvasSDK=ve,m.createCanvas=ft,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "niimbot-canvas-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Niimbot Canvas iframe communication SDK",
5
5
  "type": "module",
6
6
  "main": "dist/niimbot-canvas-sdk.umd.js",