@dynamic-labs/message-transport 2.0.0-alpha.26

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 (38) hide show
  1. package/CHANGELOG.md +2899 -0
  2. package/LICENSE +21 -0
  3. package/README.md +3 -0
  4. package/_virtual/_tslib.cjs +35 -0
  5. package/_virtual/_tslib.js +31 -0
  6. package/package.json +28 -0
  7. package/src/index.cjs +21 -0
  8. package/src/index.d.ts +4 -0
  9. package/src/index.js +7 -0
  10. package/src/messageTransport/decorators/applyDefaultMessageOrigin/applyDefaultMessageOrigin.cjs +11 -0
  11. package/src/messageTransport/decorators/applyDefaultMessageOrigin/applyDefaultMessageOrigin.d.ts +15 -0
  12. package/src/messageTransport/decorators/applyDefaultMessageOrigin/applyDefaultMessageOrigin.js +7 -0
  13. package/src/messageTransport/decorators/applyDefaultMessageOrigin/index.d.ts +1 -0
  14. package/src/messageTransport/decorators/index.d.ts +3 -0
  15. package/src/messageTransport/decorators/makeWaitForInitEvent/index.d.ts +1 -0
  16. package/src/messageTransport/decorators/makeWaitForInitEvent/makeWaitForInitEvent.cjs +34 -0
  17. package/src/messageTransport/decorators/makeWaitForInitEvent/makeWaitForInitEvent.d.ts +25 -0
  18. package/src/messageTransport/decorators/makeWaitForInitEvent/makeWaitForInitEvent.js +30 -0
  19. package/src/messageTransport/decorators/makeWaitForUnblock/index.d.ts +1 -0
  20. package/src/messageTransport/decorators/makeWaitForUnblock/makeWaitForUnblock.cjs +52 -0
  21. package/src/messageTransport/decorators/makeWaitForUnblock/makeWaitForUnblock.d.ts +23 -0
  22. package/src/messageTransport/decorators/makeWaitForUnblock/makeWaitForUnblock.js +48 -0
  23. package/src/messageTransport/index.d.ts +2 -0
  24. package/src/messageTransport/messageTransport.cjs +15 -0
  25. package/src/messageTransport/messageTransport.d.ts +44 -0
  26. package/src/messageTransport/messageTransport.js +11 -0
  27. package/src/messageTypes/SdkModuleMessages.cjs +8 -0
  28. package/src/messageTypes/SdkModuleMessages.d.ts +20 -0
  29. package/src/messageTypes/SdkModuleMessages.js +4 -0
  30. package/src/messageTypes/index.d.ts +1 -0
  31. package/src/requestChannel/index.d.ts +1 -0
  32. package/src/requestChannel/requestChannel.cjs +112 -0
  33. package/src/requestChannel/requestChannel.d.ts +55 -0
  34. package/src/requestChannel/requestChannel.js +106 -0
  35. package/src/utils/parseMessageTransportData/index.d.ts +1 -0
  36. package/src/utils/parseMessageTransportData/parseMessageTransportData.cjs +26 -0
  37. package/src/utils/parseMessageTransportData/parseMessageTransportData.d.ts +2 -0
  38. package/src/utils/parseMessageTransportData/parseMessageTransportData.js +22 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Dynamic Labs, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Message transport
2
+
3
+ This package implements the communication protocol used to send and receive messages to/from text based channels, such as a webview.
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ function __awaiter(thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ }
29
+
30
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
31
+ var e = new Error(message);
32
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
33
+ };
34
+
35
+ exports.__awaiter = __awaiter;
@@ -0,0 +1,31 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */
15
+
16
+ function __awaiter(thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ }
25
+
26
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
27
+ var e = new Error(message);
28
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
29
+ };
30
+
31
+ export { __awaiter };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@dynamic-labs/message-transport",
3
+ "version": "2.0.0-alpha.26",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/dynamic-labs/DynamicAuth.git",
7
+ "directory": "packages/message-transport"
8
+ },
9
+ "description": "Defines the interface to communicate with dynamic's webview",
10
+ "bugs": {
11
+ "url": "https://github.com/dynamic-labs/DynamicAuth/issues"
12
+ },
13
+ "homepage": "https://github.com/dynamic-labs/DynamicAuth/main/packages/message-transport#readme",
14
+ "author": "Dynamic Labs, Inc.",
15
+ "license": "MIT",
16
+ "main": "./src/index.cjs",
17
+ "module": "./src/index.js",
18
+ "types": "./src/index.d.ts",
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./src/index.d.ts",
23
+ "import": "./src/index.js",
24
+ "require": "./src/index.cjs"
25
+ },
26
+ "./package.json": "./package.json"
27
+ }
28
+ }
package/src/index.cjs ADDED
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var messageTransport = require('./messageTransport/messageTransport.cjs');
6
+ var applyDefaultMessageOrigin = require('./messageTransport/decorators/applyDefaultMessageOrigin/applyDefaultMessageOrigin.cjs');
7
+ var makeWaitForInitEvent = require('./messageTransport/decorators/makeWaitForInitEvent/makeWaitForInitEvent.cjs');
8
+ var makeWaitForUnblock = require('./messageTransport/decorators/makeWaitForUnblock/makeWaitForUnblock.cjs');
9
+ var requestChannel = require('./requestChannel/requestChannel.cjs');
10
+ var parseMessageTransportData = require('./utils/parseMessageTransportData/parseMessageTransportData.cjs');
11
+ var SdkModuleMessages = require('./messageTypes/SdkModuleMessages.cjs');
12
+
13
+
14
+
15
+ exports.createMessageTransport = messageTransport.createMessageTransport;
16
+ exports.applyDefaultMessageOrigin = applyDefaultMessageOrigin.applyDefaultMessageOrigin;
17
+ exports.makeWaitForInitEvent = makeWaitForInitEvent.makeWaitForInitEvent;
18
+ exports.makeWaitForUnblock = makeWaitForUnblock.makeWaitForUnblock;
19
+ exports.createRequestChannel = requestChannel.createRequestChannel;
20
+ exports.parseMessageTransportData = parseMessageTransportData.parseMessageTransportData;
21
+ exports.sdkHasLoadedEventName = SdkModuleMessages.sdkHasLoadedEventName;
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { applyDefaultMessageOrigin, createMessageTransport, makeWaitForInitEvent, makeWaitForUnblock, type BypassBlockCallback, type MessageTransport, type MessageTransportCallback, type MessageTransportData, type MessageTransportDataOptionalOrigin, type MessageTransportWithDefaultOrigin, } from './messageTransport';
2
+ export { type RequestChannel, createRequestChannel } from './requestChannel';
3
+ export { parseMessageTransportData } from './utils/parseMessageTransportData';
4
+ export { type ClientManifest, type SdkModuleMessages, sdkHasLoadedEventName, } from './messageTypes';
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { createMessageTransport } from './messageTransport/messageTransport.js';
2
+ export { applyDefaultMessageOrigin } from './messageTransport/decorators/applyDefaultMessageOrigin/applyDefaultMessageOrigin.js';
3
+ export { makeWaitForInitEvent } from './messageTransport/decorators/makeWaitForInitEvent/makeWaitForInitEvent.js';
4
+ export { makeWaitForUnblock } from './messageTransport/decorators/makeWaitForUnblock/makeWaitForUnblock.js';
5
+ export { createRequestChannel } from './requestChannel/requestChannel.js';
6
+ export { parseMessageTransportData } from './utils/parseMessageTransportData/parseMessageTransportData.js';
7
+ export { sdkHasLoadedEventName } from './messageTypes/SdkModuleMessages.js';
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /** Allows setting a default origin, making the origin property optional */
6
+ const applyDefaultMessageOrigin = ({ defaultOrigin, messageTransport, }) => (Object.assign(Object.assign({}, messageTransport), { defaultOrigin, emit: (message) => {
7
+ var _a;
8
+ messageTransport.emit(Object.assign(Object.assign({}, message), { origin: (_a = message.origin) !== null && _a !== void 0 ? _a : defaultOrigin }));
9
+ } }));
10
+
11
+ exports.applyDefaultMessageOrigin = applyDefaultMessageOrigin;
@@ -0,0 +1,15 @@
1
+ import { MessageTransport, MessageTransportData } from '../../messageTransport';
2
+ export type MessageTransportDataOptionalOrigin = Omit<MessageTransportData, 'origin'> & {
3
+ origin?: MessageTransportData['origin'];
4
+ };
5
+ export type MessageTransportWithDefaultOrigin = ReturnType<typeof applyDefaultMessageOrigin>;
6
+ type ApplyDefaultMessageOriginProps<T extends MessageTransport> = {
7
+ defaultOrigin: MessageTransportData['origin'];
8
+ messageTransport: T;
9
+ };
10
+ /** Allows setting a default origin, making the origin property optional */
11
+ export declare const applyDefaultMessageOrigin: <T extends MessageTransport>({ defaultOrigin, messageTransport, }: ApplyDefaultMessageOriginProps<T>) => T & {
12
+ defaultOrigin: "webview" | "host";
13
+ emit: (message: MessageTransportDataOptionalOrigin) => void;
14
+ };
15
+ export {};
@@ -0,0 +1,7 @@
1
+ /** Allows setting a default origin, making the origin property optional */
2
+ const applyDefaultMessageOrigin = ({ defaultOrigin, messageTransport, }) => (Object.assign(Object.assign({}, messageTransport), { defaultOrigin, emit: (message) => {
3
+ var _a;
4
+ messageTransport.emit(Object.assign(Object.assign({}, message), { origin: (_a = message.origin) !== null && _a !== void 0 ? _a : defaultOrigin }));
5
+ } }));
6
+
7
+ export { applyDefaultMessageOrigin };
@@ -0,0 +1 @@
1
+ export * from './applyDefaultMessageOrigin';
@@ -0,0 +1,3 @@
1
+ export * from './applyDefaultMessageOrigin';
2
+ export * from './makeWaitForInitEvent';
3
+ export * from './makeWaitForUnblock';
@@ -0,0 +1 @@
1
+ export * from './makeWaitForInitEvent';
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var makeWaitForUnblock = require('../makeWaitForUnblock/makeWaitForUnblock.cjs');
6
+
7
+ /**
8
+ * Decorator that adds the following features to a MessageTransport:
9
+ * 1. Any emit calls will not emit the message yet. These messages will be stored.
10
+ * 2. When the provided messageTransport receives a message of type initializeMessageType:
11
+ * 1. All stored messages are emitted.
12
+ * 2. Any future emit calls will no longer store the message, and instead
13
+ * will emit them right away, as normal.
14
+ */
15
+ const makeWaitForInitEvent = ({ bypassBlockIf, initializeMessageType, messageTransport, }) => {
16
+ // Reuse unblock decorator
17
+ const blockedTransport = makeWaitForUnblock.makeWaitForUnblock({
18
+ bypassBlockIf,
19
+ messageTransport,
20
+ });
21
+ const handleInitialization = ({ type }) => {
22
+ // Check if we are to initialize
23
+ if (type !== initializeMessageType)
24
+ return;
25
+ blockedTransport.unblock();
26
+ // Cleanup this listener
27
+ messageTransport.off(handleInitialization);
28
+ };
29
+ // Listen for initialization
30
+ messageTransport.on(handleInitialization);
31
+ return blockedTransport;
32
+ };
33
+
34
+ exports.makeWaitForInitEvent = makeWaitForInitEvent;
@@ -0,0 +1,25 @@
1
+ import { MessageTransport, MessageTransportData } from '../../messageTransport';
2
+ import { BypassBlockCallback } from '../makeWaitForUnblock';
3
+ type MakeWaitForInitEventProps = {
4
+ /** MessageTransport to add this decorator to */
5
+ messageTransport: MessageTransport;
6
+ /** Which message type should trigger the initialization */
7
+ initializeMessageType: string;
8
+ /** If true is returned, the message will not be blocked */
9
+ bypassBlockIf?: BypassBlockCallback;
10
+ };
11
+ /**
12
+ * Decorator that adds the following features to a MessageTransport:
13
+ * 1. Any emit calls will not emit the message yet. These messages will be stored.
14
+ * 2. When the provided messageTransport receives a message of type initializeMessageType:
15
+ * 1. All stored messages are emitted.
16
+ * 2. Any future emit calls will no longer store the message, and instead
17
+ * will emit them right away, as normal.
18
+ */
19
+ export declare const makeWaitForInitEvent: ({ bypassBlockIf, initializeMessageType, messageTransport, }: MakeWaitForInitEventProps) => {
20
+ unblock: () => void;
21
+ emit: (message: MessageTransportData) => void;
22
+ off: (callback: import("../../messageTransport").MessageTransportCallback) => void;
23
+ on: (callback: import("../../messageTransport").MessageTransportCallback) => void;
24
+ };
25
+ export {};
@@ -0,0 +1,30 @@
1
+ import { makeWaitForUnblock } from '../makeWaitForUnblock/makeWaitForUnblock.js';
2
+
3
+ /**
4
+ * Decorator that adds the following features to a MessageTransport:
5
+ * 1. Any emit calls will not emit the message yet. These messages will be stored.
6
+ * 2. When the provided messageTransport receives a message of type initializeMessageType:
7
+ * 1. All stored messages are emitted.
8
+ * 2. Any future emit calls will no longer store the message, and instead
9
+ * will emit them right away, as normal.
10
+ */
11
+ const makeWaitForInitEvent = ({ bypassBlockIf, initializeMessageType, messageTransport, }) => {
12
+ // Reuse unblock decorator
13
+ const blockedTransport = makeWaitForUnblock({
14
+ bypassBlockIf,
15
+ messageTransport,
16
+ });
17
+ const handleInitialization = ({ type }) => {
18
+ // Check if we are to initialize
19
+ if (type !== initializeMessageType)
20
+ return;
21
+ blockedTransport.unblock();
22
+ // Cleanup this listener
23
+ messageTransport.off(handleInitialization);
24
+ };
25
+ // Listen for initialization
26
+ messageTransport.on(handleInitialization);
27
+ return blockedTransport;
28
+ };
29
+
30
+ export { makeWaitForInitEvent };
@@ -0,0 +1 @@
1
+ export * from './makeWaitForUnblock';
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Decorator that adds the following features to a MessageTransport:
7
+ * 1. Any emit calls will not emit the message yet. These messages will be stored.
8
+ * 2. When the unblock method is called:
9
+ * 1. All stored messages are emitted.
10
+ * 2. Any future emit calls will no longer store the message, and instead
11
+ * will emit them right away, as normal.
12
+ */
13
+ const makeWaitForUnblock = ({ messageTransport, bypassBlockIf = () => false, }) => {
14
+ /** Whether to block any new messages */
15
+ let blocked = true;
16
+ /** Messages pending to be sent once unblocked */
17
+ let pendingMessages = [];
18
+ /**
19
+ * Ids of incoming message sessions.
20
+ * We don't want to block responses to these messages.
21
+ */
22
+ const bypassedMessageSessionIds = new Set();
23
+ const extendedTransport = {
24
+ emit: (message) => {
25
+ if (bypassBlockIf(message) && blocked) {
26
+ bypassedMessageSessionIds.add(message.messageSessionId);
27
+ }
28
+ // Even if blocking, we still want to let through ids of messages that
29
+ // can bypass the block (this includes any responses, since they share the same id)
30
+ if (blocked && !bypassedMessageSessionIds.has(message.messageSessionId)) {
31
+ pendingMessages.push(message);
32
+ return;
33
+ }
34
+ messageTransport.emit(message);
35
+ },
36
+ off: (callback) => messageTransport.off(callback),
37
+ on: (callback) => messageTransport.on(callback),
38
+ };
39
+ const unblock = () => {
40
+ if (!blocked)
41
+ return;
42
+ blocked = false;
43
+ // Emit all stored messages
44
+ for (const message of pendingMessages)
45
+ messageTransport.emit(message);
46
+ pendingMessages = [];
47
+ bypassedMessageSessionIds.clear();
48
+ };
49
+ return Object.assign(Object.assign({}, extendedTransport), { unblock });
50
+ };
51
+
52
+ exports.makeWaitForUnblock = makeWaitForUnblock;
@@ -0,0 +1,23 @@
1
+ import { MessageTransport, MessageTransportData } from '../../messageTransport';
2
+ export type BypassBlockCallback = (message: MessageTransportData) => boolean;
3
+ type MakeWaitForUnblockProps = {
4
+ /** MessageTransport to add this decorator to */
5
+ messageTransport: MessageTransport;
6
+ /** If true is return the message will not be blocked */
7
+ bypassBlockIf?: BypassBlockCallback;
8
+ };
9
+ /**
10
+ * Decorator that adds the following features to a MessageTransport:
11
+ * 1. Any emit calls will not emit the message yet. These messages will be stored.
12
+ * 2. When the unblock method is called:
13
+ * 1. All stored messages are emitted.
14
+ * 2. Any future emit calls will no longer store the message, and instead
15
+ * will emit them right away, as normal.
16
+ */
17
+ export declare const makeWaitForUnblock: ({ messageTransport, bypassBlockIf, }: MakeWaitForUnblockProps) => {
18
+ unblock: () => void;
19
+ emit: (message: MessageTransportData) => void;
20
+ off: (callback: import("../../messageTransport").MessageTransportCallback) => void;
21
+ on: (callback: import("../../messageTransport").MessageTransportCallback) => void;
22
+ };
23
+ export {};
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Decorator that adds the following features to a MessageTransport:
3
+ * 1. Any emit calls will not emit the message yet. These messages will be stored.
4
+ * 2. When the unblock method is called:
5
+ * 1. All stored messages are emitted.
6
+ * 2. Any future emit calls will no longer store the message, and instead
7
+ * will emit them right away, as normal.
8
+ */
9
+ const makeWaitForUnblock = ({ messageTransport, bypassBlockIf = () => false, }) => {
10
+ /** Whether to block any new messages */
11
+ let blocked = true;
12
+ /** Messages pending to be sent once unblocked */
13
+ let pendingMessages = [];
14
+ /**
15
+ * Ids of incoming message sessions.
16
+ * We don't want to block responses to these messages.
17
+ */
18
+ const bypassedMessageSessionIds = new Set();
19
+ const extendedTransport = {
20
+ emit: (message) => {
21
+ if (bypassBlockIf(message) && blocked) {
22
+ bypassedMessageSessionIds.add(message.messageSessionId);
23
+ }
24
+ // Even if blocking, we still want to let through ids of messages that
25
+ // can bypass the block (this includes any responses, since they share the same id)
26
+ if (blocked && !bypassedMessageSessionIds.has(message.messageSessionId)) {
27
+ pendingMessages.push(message);
28
+ return;
29
+ }
30
+ messageTransport.emit(message);
31
+ },
32
+ off: (callback) => messageTransport.off(callback),
33
+ on: (callback) => messageTransport.on(callback),
34
+ };
35
+ const unblock = () => {
36
+ if (!blocked)
37
+ return;
38
+ blocked = false;
39
+ // Emit all stored messages
40
+ for (const message of pendingMessages)
41
+ messageTransport.emit(message);
42
+ pendingMessages = [];
43
+ bypassedMessageSessionIds.clear();
44
+ };
45
+ return Object.assign(Object.assign({}, extendedTransport), { unblock });
46
+ };
47
+
48
+ export { makeWaitForUnblock };
@@ -0,0 +1,2 @@
1
+ export * from './messageTransport';
2
+ export * from './decorators';
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /** Creates a simple message transport for general use */
6
+ const createMessageTransport = () => {
7
+ const listeners = new Set();
8
+ return {
9
+ emit: (message) => listeners.forEach((callback) => callback(message)),
10
+ off: (callback) => listeners.delete(callback),
11
+ on: (callback) => listeners.add(callback),
12
+ };
13
+ };
14
+
15
+ exports.createMessageTransport = createMessageTransport;
@@ -0,0 +1,44 @@
1
+ /** Defines the structure of messages that can be sent to and from webviews */
2
+ export type MessageTransportData = {
3
+ /**
4
+ * Defines which kind of message this is.
5
+ *
6
+ * Ex: "link-wallet", "logout"
7
+ */
8
+ type: string;
9
+ /**
10
+ * Any relevant arguments attached to the message.
11
+ * Its contents must be interpreted according to the type of the message.
12
+ *
13
+ * Ex: for "link-wallet" type, can be ["metamask"], which should be interpreted as
14
+ * the target wallet connector.
15
+ */
16
+ args: unknown[];
17
+ /**
18
+ * A string that uniquely identifies this message exchange session.
19
+ * If the other end wants to send another message as a response to this message,
20
+ * then it should have the same messageSessionId.
21
+ *
22
+ * Ex: for "link-wallet" type, the other end could then eventually emit a message with type
23
+ * "link-wallet__resolved" and with the same messageSessionId, indicating a success.
24
+ */
25
+ messageSessionId: string;
26
+ /** Identifies which end emitted this message */
27
+ origin: 'webview' | 'host';
28
+ };
29
+ export type MessageTransportCallback = (data: MessageTransportData) => void;
30
+ /**
31
+ * An interface for a structure that allows sending messages to and from webviews.
32
+ * Functions similarly to an event emitter, but there's some distinctions:
33
+ * 1. You cannot listen to specific types of messages. You either listen to all messages or none.
34
+ * Note that each message has a type which you can use to decide what to do with it.
35
+ * 2. Each message has a messageSessionId which allows the other end to "respond"
36
+ * by emitting another event with the same messageSessionId.
37
+ */
38
+ export type MessageTransport = {
39
+ emit: (message: MessageTransportData) => void;
40
+ off: (callback: MessageTransportCallback) => void;
41
+ on: (callback: MessageTransportCallback) => void;
42
+ };
43
+ /** Creates a simple message transport for general use */
44
+ export declare const createMessageTransport: () => MessageTransport;
@@ -0,0 +1,11 @@
1
+ /** Creates a simple message transport for general use */
2
+ const createMessageTransport = () => {
3
+ const listeners = new Set();
4
+ return {
5
+ emit: (message) => listeners.forEach((callback) => callback(message)),
6
+ off: (callback) => listeners.delete(callback),
7
+ on: (callback) => listeners.add(callback),
8
+ };
9
+ };
10
+
11
+ export { createMessageTransport };
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /** Easy access to the event name that indicates the SDK has loaded */
6
+ const sdkHasLoadedEventName = 'loaded';
7
+
8
+ exports.sdkHasLoadedEventName = sdkHasLoadedEventName;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Defines the settings required by the SDK to be properly initialized
3
+ * inside the webview
4
+ */
5
+ export type ClientManifest = {
6
+ environmentId: string;
7
+ clientVersion: string;
8
+ apiBaseUrl?: string;
9
+ appName?: string;
10
+ appLogoUrl?: string;
11
+ cssOverrides?: string;
12
+ };
13
+ export type SdkModuleMessages = {
14
+ /** Sent from webview app to indicate the SDK is set up and ready for requests */
15
+ loaded: () => void;
16
+ /** Sent from the webview app to request the SDK settings */
17
+ manifest: () => Promise<ClientManifest>;
18
+ };
19
+ /** Easy access to the event name that indicates the SDK has loaded */
20
+ export declare const sdkHasLoadedEventName: keyof SdkModuleMessages;
@@ -0,0 +1,4 @@
1
+ /** Easy access to the event name that indicates the SDK has loaded */
2
+ const sdkHasLoadedEventName = 'loaded';
3
+
4
+ export { sdkHasLoadedEventName };
@@ -0,0 +1 @@
1
+ export * from './SdkModuleMessages';
@@ -0,0 +1 @@
1
+ export * from './requestChannel';