@stripchatdev/ext-helper 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/dist/ext-helper.js +148 -140
  2. package/package.json +1 -1
@@ -1,59 +1,59 @@
1
- const M = (r) => !!r && typeof r == "object" && "protocol" in r && r.protocol === "iframe_pubsub" && "version" in r && r.version === "1.0", E = (r, e) => r, g = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
1
+ const M = (r) => !!r && typeof r == "object" && "protocol" in r && r.protocol === "iframe_pubsub" && "version" in r && r.version === "1.0", f = (r, e) => r, E = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
2
2
  var u;
3
3
  (function(r) {
4
4
  r.Base32IncorrectEncoding = "B32_ENC_INVALID", r.DecodeTimeInvalidCharacter = "DEC_TIME_CHAR", r.DecodeTimeValueMalformed = "DEC_TIME_MALFORMED", r.EncodeTimeNegative = "ENC_TIME_NEG", r.EncodeTimeSizeExceeded = "ENC_TIME_SIZE_EXCEED", r.EncodeTimeValueMalformed = "ENC_TIME_MALFORMED", r.PRNGDetectFailure = "PRNG_DETECT", r.ULIDInvalid = "ULID_INVALID", r.Unexpected = "UNEXPECTED", r.UUIDInvalid = "UUID_INVALID";
5
5
  })(u || (u = {}));
6
- class l extends Error {
6
+ class d extends Error {
7
7
  constructor(e, t) {
8
8
  super(`${t} (${e})`), this.name = "ULIDError", this.code = e;
9
9
  }
10
10
  }
11
- function R(r) {
11
+ function m(r) {
12
12
  const e = Math.floor(r() * 32) % 32;
13
- return g.charAt(e);
13
+ return E.charAt(e);
14
14
  }
15
- function m(r) {
16
- const e = N(), t = e && (e.crypto || e.msCrypto) || null;
15
+ function R(r) {
16
+ const e = y(), t = e && (e.crypto || e.msCrypto) || null;
17
17
  if (typeof (t == null ? void 0 : t.getRandomValues) == "function")
18
18
  return () => {
19
- const s = new Uint8Array(1);
20
- return t.getRandomValues(s), s[0] / 255;
19
+ const n = new Uint8Array(1);
20
+ return t.getRandomValues(n), n[0] / 255;
21
21
  };
22
22
  if (typeof (t == null ? void 0 : t.randomBytes) == "function")
23
23
  return () => t.randomBytes(1).readUInt8() / 255;
24
- throw new l(u.PRNGDetectFailure, "Failed to find a reliable PRNG");
24
+ throw new d(u.PRNGDetectFailure, "Failed to find a reliable PRNG");
25
25
  }
26
- function N() {
27
- return v() ? self : typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : null;
26
+ function y() {
27
+ return I() ? self : typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : null;
28
28
  }
29
- function y(r, e) {
29
+ function N(r, e) {
30
30
  let t = "";
31
31
  for (; r > 0; r--)
32
- t = R(e) + t;
32
+ t = m(e) + t;
33
33
  return t;
34
34
  }
35
- function I(r, e = 10) {
35
+ function v(r, e = 10) {
36
36
  if (isNaN(r))
37
- throw new l(u.EncodeTimeValueMalformed, `Time must be a number: ${r}`);
37
+ throw new d(u.EncodeTimeValueMalformed, `Time must be a number: ${r}`);
38
38
  if (r > 281474976710655)
39
- throw new l(u.EncodeTimeSizeExceeded, `Cannot encode a time larger than ${281474976710655}: ${r}`);
39
+ throw new d(u.EncodeTimeSizeExceeded, `Cannot encode a time larger than ${281474976710655}: ${r}`);
40
40
  if (r < 0)
41
- throw new l(u.EncodeTimeNegative, `Time must be positive: ${r}`);
41
+ throw new d(u.EncodeTimeNegative, `Time must be positive: ${r}`);
42
42
  if (Number.isInteger(r) === !1)
43
- throw new l(u.EncodeTimeValueMalformed, `Time must be an integer: ${r}`);
44
- let t, s = "";
45
- for (let n = e; n > 0; n--)
46
- t = r % 32, s = g.charAt(t) + s, r = (r - t) / 32;
47
- return s;
43
+ throw new d(u.EncodeTimeValueMalformed, `Time must be an integer: ${r}`);
44
+ let t, n = "";
45
+ for (let s = e; s > 0; s--)
46
+ t = r % 32, n = E.charAt(t) + n, r = (r - t) / 32;
47
+ return n;
48
48
  }
49
- function v() {
49
+ function I() {
50
50
  return typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope;
51
51
  }
52
- function f(r, e) {
53
- const t = m(), s = Date.now();
54
- return I(s, 10) + y(16, t);
52
+ function p(r, e) {
53
+ const t = R(), n = Date.now();
54
+ return v(n, 10) + N(16, t);
55
55
  }
56
- const T = ({ seq: r, uniqId: e }) => (t) => ({
56
+ const _ = ({ seq: r, uniqId: e }) => (t) => ({
57
57
  ...{
58
58
  protocol: "iframe_pubsub",
59
59
  version: "1.0",
@@ -62,83 +62,83 @@ const T = ({ seq: r, uniqId: e }) => (t) => ({
62
62
  timestamp: Date.now()
63
63
  },
64
64
  ...t
65
- }), b = T({
66
- uniqId: () => f(),
67
- seq: () => f()
65
+ }), b = _({
66
+ uniqId: () => p(),
67
+ seq: () => p()
68
68
  });
69
- class _ {
69
+ class T {
70
70
  constructor(e) {
71
- this.eventHandlerMap = /* @__PURE__ */ new Map(), this.globalMessageHandler = (n, o) => {
72
- if (M(n)) {
73
- if (n.type === "event") {
74
- const { eventName: c } = n, i = this.eventHandlerMap.get(c);
75
- if (!i)
71
+ this.eventHandlerMap = /* @__PURE__ */ new Map(), this.globalMessageHandler = (s, i) => {
72
+ if (M(s)) {
73
+ if (s.type === "event") {
74
+ const { eventName: a } = s, o = this.eventHandlerMap.get(a);
75
+ if (!o)
76
76
  return;
77
- i.forEach((a) => {
77
+ o.forEach((c) => {
78
78
  try {
79
- a(n.data, o.id);
80
- } catch (d) {
81
- console.error("[PubSubSubscriber] globalMessageHandler: failed to handle income message", d);
79
+ c(s.data, i.id);
80
+ } catch (h) {
81
+ console.error("[PubSubSubscriber] globalMessageHandler: failed to handle income message", h);
82
82
  }
83
83
  });
84
84
  return;
85
85
  }
86
- console.error("[PubSubSubscriber] globalMessageHandler: unhandled message", n);
86
+ console.error("[PubSubSubscriber] globalMessageHandler: unhandled message", s);
87
87
  }
88
88
  };
89
- const { brokerId: t, transport: s } = e;
90
- this.brokerId = t, this.transport = s, this.transport.onMessage(this.globalMessageHandler);
89
+ const { brokerId: t, transport: n } = e;
90
+ this.brokerId = t, this.transport = n, this.transport.onMessage(this.globalMessageHandler);
91
91
  }
92
92
  subscribe(e, t) {
93
- const s = E(e);
94
- let n = this.eventHandlerMap.get(s);
95
- n || (n = /* @__PURE__ */ new Set(), this.eventHandlerMap.set(s, n)), n.add(t);
96
- const o = b({
93
+ const n = f(e);
94
+ let s = this.eventHandlerMap.get(n);
95
+ s || (s = /* @__PURE__ */ new Set(), this.eventHandlerMap.set(n, s)), s.add(t);
96
+ const i = b({
97
97
  type: "subscribe",
98
98
  eventName: e
99
99
  });
100
- this.transport.send(this.brokerId, o);
100
+ this.transport.send(this.brokerId, i);
101
101
  }
102
102
  unsubscribe(e, t) {
103
- const s = E(e);
104
- let n = this.eventHandlerMap.get(s);
105
- if (!n)
103
+ const n = f(e);
104
+ let s = this.eventHandlerMap.get(n);
105
+ if (!s)
106
106
  return;
107
- t ? n.delete(t) : this.eventHandlerMap.delete(s);
108
- const o = b({
107
+ t ? s.delete(t) : this.eventHandlerMap.delete(n);
108
+ const i = b({
109
109
  type: "unsubscribe",
110
110
  eventName: e
111
111
  });
112
- this.transport.send(this.brokerId, o);
112
+ this.transport.send(this.brokerId, i);
113
113
  }
114
114
  }
115
115
  const w = !!Error.captureStackTrace, P = "APP_ERROR";
116
- class p extends Error {
116
+ class l extends Error {
117
117
  constructor(e, t) {
118
118
  super(e), this.name = new.target.name || P, this.cause = t, w && Error.captureStackTrace(this, this.constructor);
119
119
  }
120
120
  static getChainedMessage(e) {
121
121
  let { message: t } = e;
122
- return e instanceof p && e.cause && (t += `
123
- Caused by: ${p.getChainedMessage(e.cause)}`), t;
122
+ return e instanceof l && e.cause && (t += `
123
+ Caused by: ${l.getChainedMessage(e.cause)}`), t;
124
124
  }
125
125
  toJSON() {
126
126
  return {
127
127
  name: this.name,
128
128
  message: this.message,
129
- cause: this.cause ? p.getChainedMessage(this.cause) : void 0
129
+ cause: this.cause ? l.getChainedMessage(this.cause) : void 0
130
130
  };
131
131
  }
132
132
  }
133
133
  const O = {
134
134
  INTERNAL_ERROR: "INTERNAL_ERROR"
135
135
  }, C = "RPC_ERROR";
136
- class D extends p {
136
+ class D extends l {
137
137
  constructor(e) {
138
138
  super(e.message || "unknown rpc error"), this.name = C, this.code = (e == null ? void 0 : e.code) || O.INTERNAL_ERROR;
139
139
  }
140
140
  }
141
- const A = (r) => !!r && typeof r == "object" && "protocol" in r && r.protocol === "iframe_rpc" && "version" in r && r.version === "1.0", S = ({ uniqId: r }) => (e) => ({
141
+ const A = (r) => !!r && typeof r == "object" && "protocol" in r && r.protocol === "iframe_rpc" && "version" in r && r.version === "1.0", H = ({ uniqId: r }) => (e) => ({
142
142
  ...{
143
143
  protocol: "iframe_rpc",
144
144
  version: "1.0",
@@ -146,90 +146,90 @@ const A = (r) => !!r && typeof r == "object" && "protocol" in r && r.protocol ==
146
146
  timestamp: Date.now()
147
147
  },
148
148
  ...e
149
- }), H = S({
150
- uniqId: () => f()
149
+ }), S = H({
150
+ uniqId: () => p()
151
151
  });
152
- class k {
152
+ class L {
153
153
  constructor(e) {
154
- this.pendingEntryMap = /* @__PURE__ */ new Map(), this.globalMessageHandler = (n) => {
155
- if (A(n)) {
156
- if (n.type === "response" && n.status === "ok") {
157
- const o = n.id, c = this.pendingEntryMap.get(o);
158
- if (!c) {
159
- console.error("[RpcClient] globalMessageHandler: got success response without pending request", n);
154
+ this.pendingEntryMap = /* @__PURE__ */ new Map(), this.globalMessageHandler = (s) => {
155
+ if (A(s)) {
156
+ if (s.type === "response" && s.status === "ok") {
157
+ const i = s.id, a = this.pendingEntryMap.get(i);
158
+ if (!a) {
159
+ console.error("[RpcClient] globalMessageHandler: got success response without pending request", s);
160
160
  return;
161
161
  }
162
- this.pendingEntryMap.delete(o), c.resolve(n.result);
162
+ this.pendingEntryMap.delete(i), a.resolve(s.result);
163
163
  return;
164
164
  }
165
- if (n.type === "response" && n.status === "error") {
166
- const o = n.id, c = this.pendingEntryMap.get(o);
167
- if (!c) {
168
- console.error("[RpcClient] globalMessageHandler: got error response without pending request", n);
165
+ if (s.type === "response" && s.status === "error") {
166
+ const i = s.id, a = this.pendingEntryMap.get(i);
167
+ if (!a) {
168
+ console.error("[RpcClient] globalMessageHandler: got error response without pending request", s);
169
169
  return;
170
170
  }
171
- this.pendingEntryMap.delete(o), c.reject(new D(n.error));
171
+ this.pendingEntryMap.delete(i), a.reject(new D(s.error));
172
172
  return;
173
173
  }
174
- console.error("[RpcClient] globalMessageHandler: invalid message schema", n);
174
+ console.error("[RpcClient] globalMessageHandler: invalid message schema", s);
175
175
  }
176
176
  };
177
- const { serverId: t, transport: s } = e;
178
- this.serverId = t, this.transport = s, this.transport.onMessage(this.globalMessageHandler);
177
+ const { serverId: t, transport: n } = e;
178
+ this.serverId = t, this.transport = n, this.transport.onMessage(this.globalMessageHandler);
179
179
  }
180
180
  makeRequest(e, t) {
181
- const s = H({
181
+ const n = S({
182
182
  type: "request",
183
183
  method: e,
184
184
  params: t
185
- }), n = new Promise((o, c) => {
186
- this.pendingEntryMap.set(s.id, {
185
+ }), s = new Promise((i, a) => {
186
+ this.pendingEntryMap.set(n.id, {
187
187
  method: e,
188
- resolve: (i) => {
189
- o(i);
188
+ resolve: (o) => {
189
+ i(o);
190
190
  },
191
- reject: (i) => {
192
- c(i);
191
+ reject: (o) => {
192
+ a(o);
193
193
  }
194
194
  });
195
195
  });
196
196
  try {
197
- this.transport.send(this.serverId, s);
198
- } catch (o) {
199
- console.error("[RpcClient] makeRequest: failed to send rpc message", o);
200
- const c = this.pendingEntryMap.get(s.id);
201
- c && (this.pendingEntryMap.delete(s.id), c.reject(o));
197
+ this.transport.send(this.serverId, n);
198
+ } catch (i) {
199
+ console.error("[RpcClient] makeRequest: failed to send rpc message", i);
200
+ const a = this.pendingEntryMap.get(n.id);
201
+ a && (this.pendingEntryMap.delete(n.id), a.reject(i));
202
202
  }
203
- return n;
203
+ return s;
204
204
  }
205
205
  }
206
- class L {
206
+ class k {
207
207
  constructor(e) {
208
- this.peers = /* @__PURE__ */ new Map(), this.onMessage = (s) => {
209
- const n = (o) => {
210
- const { data: c, origin: i, source: a } = o;
211
- if (!a) {
208
+ this.peers = /* @__PURE__ */ new Map(), this.onMessage = (n) => {
209
+ const s = (i) => {
210
+ const { data: a, origin: o, source: c } = i;
211
+ if (!c) {
212
212
  console.error("[PostMessageTransport] onMessage: source could not be empty");
213
213
  return;
214
214
  }
215
- const d = this.findPeer(a, i);
216
- if (d)
215
+ const h = this.findPeer(c, o);
216
+ if (h)
217
217
  try {
218
- s(c, d);
219
- } catch (h) {
220
- console.error("[PostMessageTransport] onMessage: failed to call cb on message", h);
218
+ n(a, h);
219
+ } catch (g) {
220
+ console.error("[PostMessageTransport] onMessage: failed to call cb on message", g);
221
221
  }
222
222
  };
223
- return this.localWindow.addEventListener("message", n), () => {
224
- this.localWindow.removeEventListener("message", n);
223
+ return this.localWindow.addEventListener("message", s), () => {
224
+ this.localWindow.removeEventListener("message", s);
225
225
  };
226
226
  };
227
227
  const { localWindow: t } = e;
228
228
  this.localWindow = t || window;
229
229
  }
230
230
  send(e, t) {
231
- const s = this.peers.get(e);
232
- s && s.source.postMessage(t, s.origin);
231
+ const n = this.peers.get(e);
232
+ n && n.source.postMessage(t, n.origin);
233
233
  }
234
234
  broadcast(e) {
235
235
  this.peers.forEach((t) => {
@@ -237,8 +237,8 @@ class L {
237
237
  });
238
238
  }
239
239
  broadcastGroup(e, t) {
240
- e.forEach((s) => {
241
- this.send(s, t);
240
+ e.forEach((n) => {
241
+ this.send(n, t);
242
242
  });
243
243
  }
244
244
  addPeer(e) {
@@ -248,9 +248,9 @@ class L {
248
248
  this.peers.delete(e);
249
249
  }
250
250
  findPeer(e, t) {
251
- for (const [s, n] of this.peers)
252
- if (n.source === e && n.origin === t)
253
- return n;
251
+ for (const [n, s] of this.peers)
252
+ if (s.source === e && s.origin === t)
253
+ return s;
254
254
  return null;
255
255
  }
256
256
  }
@@ -258,21 +258,21 @@ class G {
258
258
  constructor(e) {
259
259
  const {
260
260
  serverId: t,
261
- serverOrigin: s,
262
- serverSource: n,
263
- theme: o
264
- } = e, c = {
261
+ serverOrigin: n,
262
+ serverSource: s,
263
+ theme: i
264
+ } = e, a = {
265
265
  id: t,
266
- origin: s,
267
- source: n
268
- }, i = new L({});
269
- i.addPeer(c), this.rpcClient = new k({
270
- transport: i,
266
+ origin: n,
267
+ source: s
268
+ }, o = new k({});
269
+ o.addPeer(a), this.rpcClient = new L({
270
+ transport: o,
271
271
  serverId: t
272
- }), this.subscriber = new _({
273
- transport: i,
274
- brokerId: c.id
275
- }), document.documentElement.dataset.theme = o;
272
+ }), this.subscriber = new T({
273
+ transport: o,
274
+ brokerId: a.id
275
+ }), document.documentElement.dataset.theme = i;
276
276
  }
277
277
  makeRequest(e, t) {
278
278
  return this.rpcClient.makeRequest(e, t);
@@ -285,31 +285,39 @@ class G {
285
285
  }
286
286
  bootstrap() {
287
287
  let e = { height: 0, width: 0 };
288
- const t = document.documentElement, s = (i, a) => {
289
- (e.height !== a || e.width !== i) && (e = { height: a, width: i }, this.makeRequest("v1.system.resize.notify", { height: a, width: i }));
290
- }, n = (i) => {
291
- for (const a of i) {
292
- if (a.target !== t)
293
- continue;
294
- const { height: d, width: h } = a.contentRect;
295
- s(h, d);
296
- }
288
+ const t = () => {
289
+ const o = document.body;
290
+ return Math.max(
291
+ o.offsetHeight,
292
+ o.scrollHeight
293
+ );
294
+ }, n = () => {
295
+ const o = document.body;
296
+ return Math.max(
297
+ o.offsetWidth,
298
+ o.scrollWidth
299
+ );
300
+ }, s = () => {
301
+ const o = n(), c = t();
302
+ (e.height !== c || e.width !== o) && (e = { height: c, width: o }, this.makeRequest("v1.system.resize.notify", { height: c, width: o }));
297
303
  };
298
- (() => {
299
- const { height: i, width: a } = t.getBoundingClientRect();
300
- s(a, i);
301
- })(), new ResizeObserver(n).observe(document.documentElement);
304
+ s(), new ResizeObserver(s).observe(document.body), new MutationObserver(s).observe(document.body, {
305
+ childList: !0,
306
+ subtree: !0,
307
+ attributes: !0,
308
+ attributeFilter: ["style", "class", "hidden", "open"]
309
+ });
302
310
  }
303
311
  }
304
- const q = () => {
305
- const r = new URL(window.location.href), e = r.searchParams.get("origin") || "", t = r.searchParams.get("theme") || "dark", s = new G({
312
+ const x = () => {
313
+ const r = new URL(window.location.href), e = r.searchParams.get("origin") || "", t = r.searchParams.get("theme") || "dark", n = new G({
306
314
  serverId: "server",
307
315
  serverOrigin: e || window.parent.origin,
308
316
  serverSource: window.parent,
309
317
  theme: t
310
318
  });
311
- return s.bootstrap(), s;
319
+ return n.bootstrap(), n;
312
320
  };
313
321
  export {
314
- q as createExtHelper
322
+ x as createExtHelper
315
323
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stripchatdev/ext-helper",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "type": "module",
6
6
  "module": "./dist/ext-helper.js",
7
7
  "types": "./dist/index.d.ts",