@adobe/uix-guest 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/dist/debug-guest.d.ts +8 -0
- package/dist/debug-guest.d.ts.map +1 -0
- package/dist/esm/index.js +155 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/guest-server.d.ts +53 -0
- package/dist/guest-server.d.ts.map +1 -0
- package/dist/guest-ui.d.ts +102 -0
- package/dist/guest-ui.d.ts.map +1 -0
- package/dist/guest.d.ts +160 -0
- package/dist/guest.d.ts.map +1 -0
- package/dist/iife/index.js +761 -0
- package/dist/iife/index.js.map +1 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +172 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
- package/src/debug-guest.ts +42 -0
- package/src/guest-server.ts +56 -0
- package/src/guest-ui.ts +123 -0
- package/src/guest.ts +255 -0
- package/src/index.ts +142 -0
- package/tsconfig.json +20 -0
@@ -0,0 +1,761 @@
|
|
1
|
+
var AdobeUIXGuest = (() => {
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/index.ts
|
21
|
+
var src_exports = {};
|
22
|
+
__export(src_exports, {
|
23
|
+
GuestServer: () => GuestServer,
|
24
|
+
GuestUI: () => GuestUI,
|
25
|
+
PrimaryGuest: () => GuestServer,
|
26
|
+
UIGuest: () => GuestUI,
|
27
|
+
attach: () => attach,
|
28
|
+
createGuest: () => createGuest,
|
29
|
+
register: () => register
|
30
|
+
});
|
31
|
+
|
32
|
+
// ../../node_modules/penpal/lib/enums.js
|
33
|
+
var MessageType;
|
34
|
+
(function(MessageType2) {
|
35
|
+
MessageType2["Call"] = "call";
|
36
|
+
MessageType2["Reply"] = "reply";
|
37
|
+
MessageType2["Syn"] = "syn";
|
38
|
+
MessageType2["SynAck"] = "synAck";
|
39
|
+
MessageType2["Ack"] = "ack";
|
40
|
+
})(MessageType || (MessageType = {}));
|
41
|
+
var Resolution;
|
42
|
+
(function(Resolution2) {
|
43
|
+
Resolution2["Fulfilled"] = "fulfilled";
|
44
|
+
Resolution2["Rejected"] = "rejected";
|
45
|
+
})(Resolution || (Resolution = {}));
|
46
|
+
var ErrorCode;
|
47
|
+
(function(ErrorCode2) {
|
48
|
+
ErrorCode2["ConnectionDestroyed"] = "ConnectionDestroyed";
|
49
|
+
ErrorCode2["ConnectionTimeout"] = "ConnectionTimeout";
|
50
|
+
ErrorCode2["NoIframeSrc"] = "NoIframeSrc";
|
51
|
+
})(ErrorCode || (ErrorCode = {}));
|
52
|
+
var NativeErrorName;
|
53
|
+
(function(NativeErrorName2) {
|
54
|
+
NativeErrorName2["DataCloneError"] = "DataCloneError";
|
55
|
+
})(NativeErrorName || (NativeErrorName = {}));
|
56
|
+
var NativeEventType;
|
57
|
+
(function(NativeEventType2) {
|
58
|
+
NativeEventType2["Message"] = "message";
|
59
|
+
})(NativeEventType || (NativeEventType = {}));
|
60
|
+
|
61
|
+
// ../../node_modules/penpal/lib/createDestructor.js
|
62
|
+
var createDestructor_default = (localName, log) => {
|
63
|
+
const callbacks = [];
|
64
|
+
let destroyed = false;
|
65
|
+
return {
|
66
|
+
destroy(error) {
|
67
|
+
if (!destroyed) {
|
68
|
+
destroyed = true;
|
69
|
+
log(`${localName}: Destroying connection`);
|
70
|
+
callbacks.forEach((callback) => {
|
71
|
+
callback(error);
|
72
|
+
});
|
73
|
+
}
|
74
|
+
},
|
75
|
+
onDestroy(callback) {
|
76
|
+
destroyed ? callback() : callbacks.push(callback);
|
77
|
+
}
|
78
|
+
};
|
79
|
+
};
|
80
|
+
|
81
|
+
// ../../node_modules/penpal/lib/createLogger.js
|
82
|
+
var createLogger_default = (debug) => {
|
83
|
+
return (...args) => {
|
84
|
+
if (debug) {
|
85
|
+
console.log("[Penpal]", ...args);
|
86
|
+
}
|
87
|
+
};
|
88
|
+
};
|
89
|
+
|
90
|
+
// ../../node_modules/penpal/lib/errorSerialization.js
|
91
|
+
var serializeError = ({ name, message, stack }) => ({
|
92
|
+
name,
|
93
|
+
message,
|
94
|
+
stack
|
95
|
+
});
|
96
|
+
var deserializeError = (obj) => {
|
97
|
+
const deserializedError = new Error();
|
98
|
+
Object.keys(obj).forEach((key) => deserializedError[key] = obj[key]);
|
99
|
+
return deserializedError;
|
100
|
+
};
|
101
|
+
|
102
|
+
// ../../node_modules/penpal/lib/connectCallReceiver.js
|
103
|
+
var connectCallReceiver_default = (info, serializedMethods, log) => {
|
104
|
+
const { localName, local, remote, originForSending, originForReceiving } = info;
|
105
|
+
let destroyed = false;
|
106
|
+
const handleMessageEvent = (event) => {
|
107
|
+
if (event.source !== remote || event.data.penpal !== MessageType.Call) {
|
108
|
+
return;
|
109
|
+
}
|
110
|
+
if (originForReceiving !== "*" && event.origin !== originForReceiving) {
|
111
|
+
log(`${localName} received message from origin ${event.origin} which did not match expected origin ${originForReceiving}`);
|
112
|
+
return;
|
113
|
+
}
|
114
|
+
const callMessage = event.data;
|
115
|
+
const { methodName, args, id: id2 } = callMessage;
|
116
|
+
log(`${localName}: Received ${methodName}() call`);
|
117
|
+
const createPromiseHandler = (resolution) => {
|
118
|
+
return (returnValue) => {
|
119
|
+
log(`${localName}: Sending ${methodName}() reply`);
|
120
|
+
if (destroyed) {
|
121
|
+
log(`${localName}: Unable to send ${methodName}() reply due to destroyed connection`);
|
122
|
+
return;
|
123
|
+
}
|
124
|
+
const message = {
|
125
|
+
penpal: MessageType.Reply,
|
126
|
+
id: id2,
|
127
|
+
resolution,
|
128
|
+
returnValue
|
129
|
+
};
|
130
|
+
if (resolution === Resolution.Rejected && returnValue instanceof Error) {
|
131
|
+
message.returnValue = serializeError(returnValue);
|
132
|
+
message.returnValueIsError = true;
|
133
|
+
}
|
134
|
+
try {
|
135
|
+
remote.postMessage(message, originForSending);
|
136
|
+
} catch (err) {
|
137
|
+
if (err.name === NativeErrorName.DataCloneError) {
|
138
|
+
const errorReplyMessage = {
|
139
|
+
penpal: MessageType.Reply,
|
140
|
+
id: id2,
|
141
|
+
resolution: Resolution.Rejected,
|
142
|
+
returnValue: serializeError(err),
|
143
|
+
returnValueIsError: true
|
144
|
+
};
|
145
|
+
remote.postMessage(errorReplyMessage, originForSending);
|
146
|
+
}
|
147
|
+
throw err;
|
148
|
+
}
|
149
|
+
};
|
150
|
+
};
|
151
|
+
new Promise((resolve) => resolve(serializedMethods[methodName].apply(serializedMethods, args))).then(createPromiseHandler(Resolution.Fulfilled), createPromiseHandler(Resolution.Rejected));
|
152
|
+
};
|
153
|
+
local.addEventListener(NativeEventType.Message, handleMessageEvent);
|
154
|
+
return () => {
|
155
|
+
destroyed = true;
|
156
|
+
local.removeEventListener(NativeEventType.Message, handleMessageEvent);
|
157
|
+
};
|
158
|
+
};
|
159
|
+
|
160
|
+
// ../../node_modules/penpal/lib/generateId.js
|
161
|
+
var id = 0;
|
162
|
+
var generateId_default = () => ++id;
|
163
|
+
|
164
|
+
// ../../node_modules/penpal/lib/methodSerialization.js
|
165
|
+
var KEY_PATH_DELIMITER = ".";
|
166
|
+
var keyPathToSegments = (keyPath) => keyPath ? keyPath.split(KEY_PATH_DELIMITER) : [];
|
167
|
+
var segmentsToKeyPath = (segments) => segments.join(KEY_PATH_DELIMITER);
|
168
|
+
var createKeyPath = (key, prefix) => {
|
169
|
+
const segments = keyPathToSegments(prefix || "");
|
170
|
+
segments.push(key);
|
171
|
+
return segmentsToKeyPath(segments);
|
172
|
+
};
|
173
|
+
var setAtKeyPath = (subject, keyPath, value) => {
|
174
|
+
const segments = keyPathToSegments(keyPath);
|
175
|
+
segments.reduce((prevSubject, key, idx) => {
|
176
|
+
if (typeof prevSubject[key] === "undefined") {
|
177
|
+
prevSubject[key] = {};
|
178
|
+
}
|
179
|
+
if (idx === segments.length - 1) {
|
180
|
+
prevSubject[key] = value;
|
181
|
+
}
|
182
|
+
return prevSubject[key];
|
183
|
+
}, subject);
|
184
|
+
return subject;
|
185
|
+
};
|
186
|
+
var serializeMethods = (methods, prefix) => {
|
187
|
+
const flattenedMethods = {};
|
188
|
+
Object.keys(methods).forEach((key) => {
|
189
|
+
const value = methods[key];
|
190
|
+
const keyPath = createKeyPath(key, prefix);
|
191
|
+
if (typeof value === "object") {
|
192
|
+
Object.assign(flattenedMethods, serializeMethods(value, keyPath));
|
193
|
+
}
|
194
|
+
if (typeof value === "function") {
|
195
|
+
flattenedMethods[keyPath] = value;
|
196
|
+
}
|
197
|
+
});
|
198
|
+
return flattenedMethods;
|
199
|
+
};
|
200
|
+
var deserializeMethods = (flattenedMethods) => {
|
201
|
+
const methods = {};
|
202
|
+
for (const keyPath in flattenedMethods) {
|
203
|
+
setAtKeyPath(methods, keyPath, flattenedMethods[keyPath]);
|
204
|
+
}
|
205
|
+
return methods;
|
206
|
+
};
|
207
|
+
|
208
|
+
// ../../node_modules/penpal/lib/connectCallSender.js
|
209
|
+
var connectCallSender_default = (callSender, info, methodKeyPaths, destroyConnection, log) => {
|
210
|
+
const { localName, local, remote, originForSending, originForReceiving } = info;
|
211
|
+
let destroyed = false;
|
212
|
+
log(`${localName}: Connecting call sender`);
|
213
|
+
const createMethodProxy = (methodName) => {
|
214
|
+
return (...args) => {
|
215
|
+
log(`${localName}: Sending ${methodName}() call`);
|
216
|
+
let iframeRemoved;
|
217
|
+
try {
|
218
|
+
if (remote.closed) {
|
219
|
+
iframeRemoved = true;
|
220
|
+
}
|
221
|
+
} catch (e) {
|
222
|
+
iframeRemoved = true;
|
223
|
+
}
|
224
|
+
if (iframeRemoved) {
|
225
|
+
destroyConnection();
|
226
|
+
}
|
227
|
+
if (destroyed) {
|
228
|
+
const error = new Error(`Unable to send ${methodName}() call due to destroyed connection`);
|
229
|
+
error.code = ErrorCode.ConnectionDestroyed;
|
230
|
+
throw error;
|
231
|
+
}
|
232
|
+
return new Promise((resolve, reject) => {
|
233
|
+
const id2 = generateId_default();
|
234
|
+
const handleMessageEvent = (event) => {
|
235
|
+
if (event.source !== remote || event.data.penpal !== MessageType.Reply || event.data.id !== id2) {
|
236
|
+
return;
|
237
|
+
}
|
238
|
+
if (originForReceiving !== "*" && event.origin !== originForReceiving) {
|
239
|
+
log(`${localName} received message from origin ${event.origin} which did not match expected origin ${originForReceiving}`);
|
240
|
+
return;
|
241
|
+
}
|
242
|
+
const replyMessage = event.data;
|
243
|
+
log(`${localName}: Received ${methodName}() reply`);
|
244
|
+
local.removeEventListener(NativeEventType.Message, handleMessageEvent);
|
245
|
+
let returnValue = replyMessage.returnValue;
|
246
|
+
if (replyMessage.returnValueIsError) {
|
247
|
+
returnValue = deserializeError(returnValue);
|
248
|
+
}
|
249
|
+
(replyMessage.resolution === Resolution.Fulfilled ? resolve : reject)(returnValue);
|
250
|
+
};
|
251
|
+
local.addEventListener(NativeEventType.Message, handleMessageEvent);
|
252
|
+
const callMessage = {
|
253
|
+
penpal: MessageType.Call,
|
254
|
+
id: id2,
|
255
|
+
methodName,
|
256
|
+
args
|
257
|
+
};
|
258
|
+
remote.postMessage(callMessage, originForSending);
|
259
|
+
});
|
260
|
+
};
|
261
|
+
};
|
262
|
+
const flattenedMethods = methodKeyPaths.reduce((api, name) => {
|
263
|
+
api[name] = createMethodProxy(name);
|
264
|
+
return api;
|
265
|
+
}, {});
|
266
|
+
Object.assign(callSender, deserializeMethods(flattenedMethods));
|
267
|
+
return () => {
|
268
|
+
destroyed = true;
|
269
|
+
};
|
270
|
+
};
|
271
|
+
|
272
|
+
// ../../node_modules/penpal/lib/startConnectionTimeout.js
|
273
|
+
var startConnectionTimeout_default = (timeout, callback) => {
|
274
|
+
let timeoutId;
|
275
|
+
if (timeout !== void 0) {
|
276
|
+
timeoutId = window.setTimeout(() => {
|
277
|
+
const error = new Error(`Connection timed out after ${timeout}ms`);
|
278
|
+
error.code = ErrorCode.ConnectionTimeout;
|
279
|
+
callback(error);
|
280
|
+
}, timeout);
|
281
|
+
}
|
282
|
+
return () => {
|
283
|
+
clearTimeout(timeoutId);
|
284
|
+
};
|
285
|
+
};
|
286
|
+
|
287
|
+
// ../../node_modules/penpal/lib/child/handleSynAckMessageFactory.js
|
288
|
+
var handleSynAckMessageFactory_default = (parentOrigin, serializedMethods, destructor, log) => {
|
289
|
+
const { destroy, onDestroy } = destructor;
|
290
|
+
return (event) => {
|
291
|
+
let originQualifies = parentOrigin instanceof RegExp ? parentOrigin.test(event.origin) : parentOrigin === "*" || parentOrigin === event.origin;
|
292
|
+
if (!originQualifies) {
|
293
|
+
log(`Child: Handshake - Received SYN-ACK from origin ${event.origin} which did not match expected origin ${parentOrigin}`);
|
294
|
+
return;
|
295
|
+
}
|
296
|
+
log("Child: Handshake - Received SYN-ACK, responding with ACK");
|
297
|
+
const originForSending = event.origin === "null" ? "*" : event.origin;
|
298
|
+
const ackMessage = {
|
299
|
+
penpal: MessageType.Ack,
|
300
|
+
methodNames: Object.keys(serializedMethods)
|
301
|
+
};
|
302
|
+
window.parent.postMessage(ackMessage, originForSending);
|
303
|
+
const info = {
|
304
|
+
localName: "Child",
|
305
|
+
local: window,
|
306
|
+
remote: window.parent,
|
307
|
+
originForSending,
|
308
|
+
originForReceiving: event.origin
|
309
|
+
};
|
310
|
+
const destroyCallReceiver = connectCallReceiver_default(info, serializedMethods, log);
|
311
|
+
onDestroy(destroyCallReceiver);
|
312
|
+
const callSender = {};
|
313
|
+
const destroyCallSender = connectCallSender_default(callSender, info, event.data.methodNames, destroy, log);
|
314
|
+
onDestroy(destroyCallSender);
|
315
|
+
return callSender;
|
316
|
+
};
|
317
|
+
};
|
318
|
+
|
319
|
+
// ../../node_modules/penpal/lib/child/connectToParent.js
|
320
|
+
var areGlobalsAccessible = () => {
|
321
|
+
try {
|
322
|
+
clearTimeout();
|
323
|
+
} catch (e) {
|
324
|
+
return false;
|
325
|
+
}
|
326
|
+
return true;
|
327
|
+
};
|
328
|
+
var connectToParent_default = (options = {}) => {
|
329
|
+
const { parentOrigin = "*", methods = {}, timeout, debug = false } = options;
|
330
|
+
const log = createLogger_default(debug);
|
331
|
+
const destructor = createDestructor_default("Child", log);
|
332
|
+
const { destroy, onDestroy } = destructor;
|
333
|
+
const serializedMethods = serializeMethods(methods);
|
334
|
+
const handleSynAckMessage = handleSynAckMessageFactory_default(parentOrigin, serializedMethods, destructor, log);
|
335
|
+
const sendSynMessage = () => {
|
336
|
+
log("Child: Handshake - Sending SYN");
|
337
|
+
const synMessage = { penpal: MessageType.Syn };
|
338
|
+
const parentOriginForSyn = parentOrigin instanceof RegExp ? "*" : parentOrigin;
|
339
|
+
window.parent.postMessage(synMessage, parentOriginForSyn);
|
340
|
+
};
|
341
|
+
const promise = new Promise((resolve, reject) => {
|
342
|
+
const stopConnectionTimeout = startConnectionTimeout_default(timeout, destroy);
|
343
|
+
const handleMessage = (event) => {
|
344
|
+
if (!areGlobalsAccessible()) {
|
345
|
+
return;
|
346
|
+
}
|
347
|
+
if (event.source !== parent || !event.data) {
|
348
|
+
return;
|
349
|
+
}
|
350
|
+
if (event.data.penpal === MessageType.SynAck) {
|
351
|
+
const callSender = handleSynAckMessage(event);
|
352
|
+
if (callSender) {
|
353
|
+
window.removeEventListener(NativeEventType.Message, handleMessage);
|
354
|
+
stopConnectionTimeout();
|
355
|
+
resolve(callSender);
|
356
|
+
}
|
357
|
+
}
|
358
|
+
};
|
359
|
+
window.addEventListener(NativeEventType.Message, handleMessage);
|
360
|
+
sendSynMessage();
|
361
|
+
onDestroy((error) => {
|
362
|
+
window.removeEventListener(NativeEventType.Message, handleMessage);
|
363
|
+
if (error) {
|
364
|
+
reject(error);
|
365
|
+
}
|
366
|
+
});
|
367
|
+
});
|
368
|
+
return {
|
369
|
+
promise,
|
370
|
+
destroy() {
|
371
|
+
destroy();
|
372
|
+
}
|
373
|
+
};
|
374
|
+
};
|
375
|
+
|
376
|
+
// ../uix-core/dist/esm/index.js
|
377
|
+
var isDarkMode = () => typeof window.matchMedia === "function" && window.matchMedia("(prefers-color-scheme: dark)").matches;
|
378
|
+
var Layouts = {
|
379
|
+
medium: {
|
380
|
+
padX: 5,
|
381
|
+
padY: 3,
|
382
|
+
rounded: 4,
|
383
|
+
fontSize: 100,
|
384
|
+
emphasis: "font-weight: bold;"
|
385
|
+
},
|
386
|
+
small: {
|
387
|
+
padX: 3,
|
388
|
+
padY: 1,
|
389
|
+
rounded: 2,
|
390
|
+
fontSize: 95,
|
391
|
+
emphasis: "font-style: italic;"
|
392
|
+
}
|
393
|
+
};
|
394
|
+
var Colors = {
|
395
|
+
yellow: {
|
396
|
+
text: "#333333",
|
397
|
+
bg: "#EBD932",
|
398
|
+
hilight: "#F7E434",
|
399
|
+
shadow: "#D1C12C"
|
400
|
+
},
|
401
|
+
green: {
|
402
|
+
text: "#333333",
|
403
|
+
bg: "#96EB5E",
|
404
|
+
hilight: "#9EF763",
|
405
|
+
shadow: "#85D154"
|
406
|
+
},
|
407
|
+
blue: {
|
408
|
+
text: "#333333",
|
409
|
+
bg: "#8DD0EB",
|
410
|
+
hilight: "#88F0F7",
|
411
|
+
shadow: "#74AED4"
|
412
|
+
},
|
413
|
+
gray: isDarkMode() ? {
|
414
|
+
text: "#eeeeee",
|
415
|
+
bg: "transparent",
|
416
|
+
hilight: "#cecece",
|
417
|
+
shadow: "#cecece"
|
418
|
+
} : {
|
419
|
+
text: "#333333",
|
420
|
+
bg: "#eeeeee",
|
421
|
+
hilight: "#f6f6f6",
|
422
|
+
shadow: "#cecece"
|
423
|
+
}
|
424
|
+
};
|
425
|
+
function memoizeUnary(fn) {
|
426
|
+
const cache = /* @__PURE__ */ new Map();
|
427
|
+
return (arg) => {
|
428
|
+
if (!cache.has(arg)) {
|
429
|
+
const result = fn(arg);
|
430
|
+
cache.set(arg, result);
|
431
|
+
if (cache.size > 100) {
|
432
|
+
cache.delete(cache.keys().next().value);
|
433
|
+
}
|
434
|
+
return result;
|
435
|
+
}
|
436
|
+
return cache.get(arg);
|
437
|
+
};
|
438
|
+
}
|
439
|
+
var toTheme = memoizeUnary((theme) => {
|
440
|
+
if (typeof theme === "string") {
|
441
|
+
const [color, size] = theme.split(" ");
|
442
|
+
return {
|
443
|
+
...Colors[color],
|
444
|
+
...Layouts[size]
|
445
|
+
};
|
446
|
+
}
|
447
|
+
return theme;
|
448
|
+
});
|
449
|
+
var block = `display: inline-block; border: 1px solid;`;
|
450
|
+
var flatten = (side) => `padding-${side}: 0px; border-${side}-width: 0px; border-top-${side}-radius: 0px; border-bottom-${side}-radius: 0px;`;
|
451
|
+
var toColor = ({ bg, hilight, shadow, text }) => `color: ${text}; background: ${bg}; border-color: ${hilight} ${shadow} ${shadow} ${hilight};`;
|
452
|
+
var toLayout = ({ fontSize, padY, padX, rounded }) => `font-size: ${fontSize}%; padding: ${padY}px ${padX}px; border-radius: ${rounded}px;`;
|
453
|
+
var toBubbleStyle = memoizeUnary((theme) => {
|
454
|
+
const base = `${block}${toColor(theme)}${toLayout(theme)}`;
|
455
|
+
return [
|
456
|
+
`${base}${flatten("right")}`,
|
457
|
+
`${base}${flatten("left")}${theme.emphasis}`
|
458
|
+
];
|
459
|
+
});
|
460
|
+
function toBubblePrepender(bubbleLeft, bubbleRight, theme) {
|
461
|
+
const prefix = `%c${bubbleLeft}%c ${bubbleRight}`;
|
462
|
+
const [left, right] = toBubbleStyle(theme);
|
463
|
+
return (args) => {
|
464
|
+
const bubbleArgs = [prefix, left, right];
|
465
|
+
if (typeof args[0] === "string") {
|
466
|
+
bubbleArgs[0] = `${prefix}%c ${args.shift()}`;
|
467
|
+
bubbleArgs.push("");
|
468
|
+
}
|
469
|
+
return [...bubbleArgs, ...args];
|
470
|
+
};
|
471
|
+
}
|
472
|
+
var stateTypes = {
|
473
|
+
event: "\uFE0F\u26A1\uFE0F"
|
474
|
+
};
|
475
|
+
var stateDelim = " \u293B ";
|
476
|
+
var getStateFormatter = memoizeUnary((stateJson) => {
|
477
|
+
const stateStack = JSON.parse(stateJson);
|
478
|
+
const firstState = stateStack.shift();
|
479
|
+
const left = stateTypes[firstState.type];
|
480
|
+
const right = [
|
481
|
+
firstState.name,
|
482
|
+
...stateStack.map((state) => `${stateTypes[state.type]} ${state.name}`)
|
483
|
+
].join(stateDelim);
|
484
|
+
return toBubblePrepender(left, right, toTheme("gray small"));
|
485
|
+
});
|
486
|
+
var getStatePrepender = (stateStack) => getStateFormatter(JSON.stringify(stateStack));
|
487
|
+
var overrideMethods = ["log", "error", "warn", "info", "debug"];
|
488
|
+
var identity = (x) => x;
|
489
|
+
var noop = () => void 0;
|
490
|
+
function _customConsole(theme, type, name) {
|
491
|
+
const prepender = toBubblePrepender(`X${type}`, name, toTheme(theme));
|
492
|
+
let statePrepender = identity;
|
493
|
+
const stateStack = [];
|
494
|
+
const loggerProto = {
|
495
|
+
detach: {
|
496
|
+
writable: true,
|
497
|
+
configurable: true,
|
498
|
+
value() {
|
499
|
+
overrideMethods.forEach((method) => {
|
500
|
+
this[method] = noop;
|
501
|
+
});
|
502
|
+
}
|
503
|
+
},
|
504
|
+
pushState: {
|
505
|
+
value(state) {
|
506
|
+
stateStack.push(state);
|
507
|
+
statePrepender = getStatePrepender(stateStack);
|
508
|
+
}
|
509
|
+
},
|
510
|
+
popState: {
|
511
|
+
value() {
|
512
|
+
stateStack.pop();
|
513
|
+
statePrepender = stateStack.length === 0 ? identity : getStatePrepender(stateStack);
|
514
|
+
}
|
515
|
+
}
|
516
|
+
};
|
517
|
+
const customConsole = Object.create(
|
518
|
+
console,
|
519
|
+
overrideMethods.reduce((out, level) => {
|
520
|
+
out[level] = {
|
521
|
+
writable: true,
|
522
|
+
configurable: true,
|
523
|
+
value(...args) {
|
524
|
+
console[level](...prepender(statePrepender(args)));
|
525
|
+
}
|
526
|
+
};
|
527
|
+
return out;
|
528
|
+
}, loggerProto)
|
529
|
+
);
|
530
|
+
return customConsole;
|
531
|
+
}
|
532
|
+
var quietConsole = new Proxy(console, {
|
533
|
+
get() {
|
534
|
+
return noop;
|
535
|
+
}
|
536
|
+
});
|
537
|
+
function debugEmitter(emitter, opts) {
|
538
|
+
const logger = _customConsole(
|
539
|
+
opts.theme,
|
540
|
+
opts.type || Object.getPrototypeOf(emitter).constructor.name,
|
541
|
+
opts.id || emitter.id
|
542
|
+
);
|
543
|
+
const oldDispatch = emitter.dispatchEvent;
|
544
|
+
emitter.dispatchEvent = (event) => {
|
545
|
+
logger.pushState({ type: "event", name: event.type });
|
546
|
+
const retVal = oldDispatch.call(emitter, event);
|
547
|
+
logger.popState();
|
548
|
+
return retVal;
|
549
|
+
};
|
550
|
+
const subscriptions = [];
|
551
|
+
const oldDetach = logger.detach;
|
552
|
+
logger.detach = () => {
|
553
|
+
oldDetach.call(logger);
|
554
|
+
subscriptions.forEach((unsubscribe) => unsubscribe());
|
555
|
+
};
|
556
|
+
function listen(type, listener) {
|
557
|
+
subscriptions.push(
|
558
|
+
emitter.addEventListener(type, (event) => listener(logger, event))
|
559
|
+
);
|
560
|
+
return logger;
|
561
|
+
}
|
562
|
+
logger.listen = listen;
|
563
|
+
return logger;
|
564
|
+
}
|
565
|
+
var Emitter = class extends EventTarget {
|
566
|
+
constructor(id2) {
|
567
|
+
super();
|
568
|
+
this.id = id2;
|
569
|
+
}
|
570
|
+
emit(type, detail) {
|
571
|
+
const event = new CustomEvent(type, { detail });
|
572
|
+
this.dispatchEvent(event);
|
573
|
+
}
|
574
|
+
addEventListener(type, listener) {
|
575
|
+
super.addEventListener(type, listener);
|
576
|
+
return () => super.removeEventListener(type, listener);
|
577
|
+
}
|
578
|
+
};
|
579
|
+
function makeNamespaceProxy(invoke, path = []) {
|
580
|
+
const handler = {
|
581
|
+
get: (target2, prop) => {
|
582
|
+
if (typeof prop === "string") {
|
583
|
+
if (!Reflect.has(target2, prop)) {
|
584
|
+
const next = makeNamespaceProxy(invoke, path.concat(prop));
|
585
|
+
Reflect.set(target2, prop, next);
|
586
|
+
}
|
587
|
+
return Reflect.get(target2, prop);
|
588
|
+
} else {
|
589
|
+
throw new Error(
|
590
|
+
`Cannot look up a symbol ${String(prop)} on a host connection proxy.`
|
591
|
+
);
|
592
|
+
}
|
593
|
+
}
|
594
|
+
};
|
595
|
+
const target = {};
|
596
|
+
if (path.length < 2) {
|
597
|
+
return new Proxy(target, handler);
|
598
|
+
}
|
599
|
+
const invoker = (...args) => invoke({
|
600
|
+
path: path.slice(0, -1),
|
601
|
+
name: path[path.length - 1],
|
602
|
+
args
|
603
|
+
});
|
604
|
+
return new Proxy(invoker, {
|
605
|
+
...handler,
|
606
|
+
apply(target2, _, args) {
|
607
|
+
return target2(...args);
|
608
|
+
}
|
609
|
+
});
|
610
|
+
}
|
611
|
+
function timeoutPromise(timeoutMs, promise) {
|
612
|
+
return new Promise((resolve, reject) => {
|
613
|
+
const timeout = setTimeout(
|
614
|
+
() => reject(new Error(`Timed out after ${timeoutMs}ms`)),
|
615
|
+
timeoutMs
|
616
|
+
);
|
617
|
+
promise.then((result) => {
|
618
|
+
clearTimeout(timeout);
|
619
|
+
resolve(result);
|
620
|
+
}).catch(reject);
|
621
|
+
});
|
622
|
+
}
|
623
|
+
|
624
|
+
// src/debug-guest.ts
|
625
|
+
function debugGuest(guest) {
|
626
|
+
return debugEmitter(guest, {
|
627
|
+
theme: "yellow medium",
|
628
|
+
type: "Guest"
|
629
|
+
}).listen("beforeconnect", (log, { detail: { guest: guest2 } }) => {
|
630
|
+
log.info(guest2);
|
631
|
+
}).listen("connecting", (log, { detail: { connection } }) => {
|
632
|
+
log.info(connection);
|
633
|
+
}).listen("connected", (log, { detail: { guest: guest2 } }) => {
|
634
|
+
log.info(guest2);
|
635
|
+
}).listen("error", (log, { detail: { error, guest: guest2 } }) => {
|
636
|
+
log.error(
|
637
|
+
"\u274C Failed to connect! %s",
|
638
|
+
error.message,
|
639
|
+
guest2,
|
640
|
+
error
|
641
|
+
);
|
642
|
+
});
|
643
|
+
}
|
644
|
+
|
645
|
+
// src/guest.ts
|
646
|
+
var SharedContext = class {
|
647
|
+
constructor(values) {
|
648
|
+
this.reset(values);
|
649
|
+
}
|
650
|
+
reset(values) {
|
651
|
+
this._map = new Map(Object.entries(values));
|
652
|
+
}
|
653
|
+
get(key) {
|
654
|
+
return this._map.get(key);
|
655
|
+
}
|
656
|
+
};
|
657
|
+
var Guest2 = class extends Emitter {
|
658
|
+
constructor(config) {
|
659
|
+
super(config.id);
|
660
|
+
this.debugLogger = quietConsole;
|
661
|
+
this.host = makeNamespaceProxy(
|
662
|
+
async (address) => {
|
663
|
+
await this.hostConnectionPromise;
|
664
|
+
try {
|
665
|
+
const result = await timeoutPromise(
|
666
|
+
1e4,
|
667
|
+
this.hostConnection.invokeHostMethod(address)
|
668
|
+
);
|
669
|
+
return result;
|
670
|
+
} catch (e) {
|
671
|
+
const error = e instanceof Error ? e : new Error(e);
|
672
|
+
const methodError = new Error(
|
673
|
+
`Host method call host.${address.path.join(".")}() failed: ${error.message}`
|
674
|
+
);
|
675
|
+
this.debugLogger.error(methodError);
|
676
|
+
throw methodError;
|
677
|
+
}
|
678
|
+
}
|
679
|
+
);
|
680
|
+
this.timeout = 1e4;
|
681
|
+
if (typeof config.timeout === "number") {
|
682
|
+
this.timeout = config.timeout;
|
683
|
+
}
|
684
|
+
if (config.debug) {
|
685
|
+
this.debugLogger = debugGuest(this);
|
686
|
+
}
|
687
|
+
this.addEventListener("contextchange", (event) => {
|
688
|
+
this.sharedContext = new SharedContext(event.detail.context);
|
689
|
+
});
|
690
|
+
}
|
691
|
+
getLocalMethods() {
|
692
|
+
return {
|
693
|
+
emit: (...args) => {
|
694
|
+
this.debugLogger.log(`Event "${args[0]}" emitted from host`);
|
695
|
+
this.emit(...args);
|
696
|
+
}
|
697
|
+
};
|
698
|
+
}
|
699
|
+
async connect() {
|
700
|
+
return this._connect();
|
701
|
+
}
|
702
|
+
async _connect() {
|
703
|
+
this.emit("beforeconnect", { guest: this });
|
704
|
+
try {
|
705
|
+
const connection = connectToParent_default({
|
706
|
+
timeout: this.timeout,
|
707
|
+
methods: this.getLocalMethods()
|
708
|
+
});
|
709
|
+
this.hostConnectionPromise = connection.promise;
|
710
|
+
this.hostConnection = await this.hostConnectionPromise;
|
711
|
+
this.sharedContext = new SharedContext(
|
712
|
+
await this.hostConnection.getSharedContext()
|
713
|
+
);
|
714
|
+
this.debugLogger.log("retrieved sharedContext", this.sharedContext);
|
715
|
+
this.emit("connected", { guest: this, connection });
|
716
|
+
} catch (e) {
|
717
|
+
this.emit("error", { guest: this, error: e });
|
718
|
+
this.debugLogger.error("Connection failed!", e);
|
719
|
+
}
|
720
|
+
}
|
721
|
+
};
|
722
|
+
|
723
|
+
// src/guest-ui.ts
|
724
|
+
var GuestUI = class extends Guest2 {
|
725
|
+
constructor(config) {
|
726
|
+
super(config);
|
727
|
+
}
|
728
|
+
};
|
729
|
+
|
730
|
+
// src/guest-server.ts
|
731
|
+
var GuestServer = class extends Guest2 {
|
732
|
+
getLocalMethods() {
|
733
|
+
return {
|
734
|
+
...super.getLocalMethods(),
|
735
|
+
apis: this.localMethods
|
736
|
+
};
|
737
|
+
}
|
738
|
+
async register(implementedMethods) {
|
739
|
+
this.localMethods = implementedMethods;
|
740
|
+
return this._connect();
|
741
|
+
}
|
742
|
+
};
|
743
|
+
|
744
|
+
// src/index.ts
|
745
|
+
function createGuest(config) {
|
746
|
+
const guest = new GuestServer(config);
|
747
|
+
return guest;
|
748
|
+
}
|
749
|
+
async function attach(config) {
|
750
|
+
const guest = new GuestUI(config);
|
751
|
+
await guest._connect();
|
752
|
+
return guest;
|
753
|
+
}
|
754
|
+
async function register(config) {
|
755
|
+
const guest = new GuestServer(config);
|
756
|
+
await guest.register(config.methods);
|
757
|
+
return guest;
|
758
|
+
}
|
759
|
+
return __toCommonJS(src_exports);
|
760
|
+
})();
|
761
|
+
//# sourceMappingURL=index.js.map
|