snack-sdk 3.8.0 → 3.9.0
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.
- package/CHANGELOG.md +22 -0
- package/build/DependencyResolver.d.ts +26 -26
- package/build/DependencyResolver.js +258 -258
- package/build/DevSession.d.ts +20 -20
- package/build/DevSession.js +203 -203
- package/build/FileUploader.d.ts +23 -23
- package/build/FileUploader.js +174 -174
- package/build/Logger.d.ts +11 -11
- package/build/Logger.js +33 -33
- package/build/Session.d.ts +301 -301
- package/build/Session.js +1172 -1171
- package/build/Session.js.map +1 -1
- package/build/State.d.ts +16 -16
- package/build/State.js +90 -90
- package/build/WantedVersions.d.ts +40 -17
- package/build/WantedVersions.js +156 -93
- package/build/WantedVersions.js.map +1 -1
- package/build/defaultConfig.d.ts +12 -12
- package/build/defaultConfig.js +29 -29
- package/build/index.d.ts +7 -7
- package/build/index.js +30 -30
- package/build/transports/CodeMessageBuilder.d.ts +31 -31
- package/build/transports/CodeMessageBuilder.js +196 -196
- package/build/transports/Protocol.d.ts +64 -64
- package/build/transports/Protocol.js +2 -2
- package/build/transports/Transport.d.ts +28 -27
- package/build/transports/Transport.js +303 -298
- package/build/transports/Transport.js.map +1 -1
- package/build/transports/index.d.ts +3 -3
- package/build/transports/index.js +22 -22
- package/build/transports/types.d.ts +63 -63
- package/build/transports/types.js +2 -2
- package/build/transports/webPlayer.d.ts +12 -12
- package/build/transports/webPlayer.js +70 -70
- package/build/types.d.ts +185 -185
- package/build/types.js +2 -2
- package/build/utils.d.ts +17 -17
- package/build/utils.js +68 -68
- package/package.json +11 -4
- package/src/DependencyResolver.ts +0 -218
- package/src/DevSession.ts +0 -159
- package/src/FileUploader.ts +0 -135
- package/src/Logger.ts +0 -33
- package/src/Session.ts +0 -1293
- package/src/State.ts +0 -98
- package/src/WantedVersions.ts +0 -57
- package/src/defaultConfig.ts +0 -33
- package/src/index.ts +0 -59
- package/src/transports/CodeMessageBuilder.ts +0 -226
- package/src/transports/Protocol.ts +0 -78
- package/src/transports/Transport.ts +0 -270
- package/src/transports/index.ts +0 -8
- package/src/transports/types.ts +0 -89
- package/src/transports/webPlayer.ts +0 -93
- package/src/types.ts +0 -223
- package/src/utils.ts +0 -71
- package/tsconfig.json +0 -28
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transport.js","sourceRoot":"","sources":["../../src/transports/Transport.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4B;AAE5B,oCAAiD;AACjD,4EAAsF;AAStF,SAAgB,yBAAyB,CAAC,OAAe,EAAE,WAAgC;IACzF,+GAA+G;IAC/G,OAAO,CACL,kBAAkB,CAChB,OAAO;QACL,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,OAAO,CACjC,WAAW,EACX,UAAC,CAAC,IAAK,OAAA,MAAI,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAI,EAAhD,CAAgD,CACxD,CACJ,CAAC,MAAM,GAAG,GAAG,CACf,CAAC;AACJ,CAAC;AAXD,8DAWC;AAED;IAUE,mBAAY,OAA8B;QAA1C,iBAqBC;QAxBO,qBAAgB,GAAW,CAAC,CAAC;QA2I7B,qBAAgB,GAAG,UAAC,KAA2B;;YACrD,QAAQ,KAAK,CAAC,MAAM,EAAE;gBACpB,KAAK,MAAM;oBACT,IAAI;wBACF,IAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;wBACtC,KAAI,CAAC,gBAAgB,EAAE,CAAC;wBACxB,MAAA,KAAI,CAAC,QAAQ,+CAAb,KAAI,EAAY;4BACd,IAAI,EAAE,SAAS;4BACf,YAAY,EAAE,KAAK,CAAC,IAAI;4BACxB,IAAI,EAAE,MAAM;yBACb,EAAE;wBACH,IAAI,KAAI,CAAC,WAAW,EAAE;4BACpB,MAA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,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,gBAAgB,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;oBAC/D,MAAA,IAAI,CAAC,QAAQ,+CAAb,IAAI,EAAY;wBACd,IAAI,EAAE,YAAY;wBAClB,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC;wBAC5C,IAAI,EAAE;4BACJ,QAAQ,EAAE,KAAK;yBAChB;qBACF,EAAE;oBACH,MAAM;gBACR,KAAK,SAAS;oBACZ,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;oBAChF,MAAM;aACT;SACF;QAAC,OAAO,CAAC,EAAE;YACV,MAAA,IAAI,CAAC,MAAM,0CAAE,KAAK,CAAC,6BAA6B,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE;SAC5E;IACH,CAAC;IAOH,gBAAC;AAAD,CAAC,AApPD,IAoPC","sourcesContent":["import PubNub from 'pubnub';\n\nimport { createLogger, Logger } from '../Logger';\nimport CodeMessageBuilder, { CodeMessageBuilderCallback } from './CodeMessageBuilder';\nimport { ProtocolOutgoingMessage, ProtocolIncomingMessage, ProtocolCodeMessage } from './Protocol';\nimport {\n SnackTransport,\n SnackTransportListener,\n SnackTransportMessage,\n SnackTransportOptions,\n} from './types';\n\nexport function calcPubNubCodeMessageSize(channel: string, codeMessage: ProtocolCodeMessage) {\n // https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-message-payload-size-before-publishing\n return (\n encodeURIComponent(\n channel +\n JSON.stringify(codeMessage).replace(\n /[!~*'()]/g,\n (x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`\n )\n ).length + 200\n );\n}\n\nexport default class Transport implements SnackTransport {\n private readonly channel: string;\n private readonly logger?: Logger;\n private callback?: SnackTransportListener;\n private pubNub?: PubNub;\n private codeMessageBuilder: CodeMessageBuilder;\n private codeMessage?: ProtocolCodeMessage;\n private connectionsCount: number = 0;\n private readonly logSuffix: string;\n\n constructor(options: SnackTransportOptions) {\n const { apiURL, channel, verbose } = options;\n this.channel = channel ?? '';\n this.logger = verbose ? createLogger(true) : undefined;\n this.logSuffix = options.name ? ` (${options.name})` : '';\n\n if (channel) {\n this.codeMessageBuilder = new CodeMessageBuilder({\n verifyCodeMessageSize: this.onPubNubVerifyCodeMessageSize,\n callback: this.onCodeMessageReady,\n apiURL,\n logger: this.logger,\n maxDiffPlaceholder: 'X'.repeat(32768),\n });\n } else {\n this.codeMessageBuilder = new CodeMessageBuilder({\n verifyCodeMessageSize: () => true,\n callback: this.onCodeMessageReady,\n logger: this.logger,\n });\n }\n }\n\n addEventListener(_type: 'message', callback: SnackTransportListener) {\n this.callback = callback;\n }\n\n removeEventListener(_type: 'message', _callback: SnackTransportListener) {\n this.callback = undefined;\n }\n\n postMessage(message: SnackTransportMessage) {\n switch (message.type) {\n case 'start':\n this.start();\n break;\n case 'stop':\n this.stop();\n break;\n case 'update_code':\n this.codeMessageBuilder.setCode(message.data);\n break;\n case 'protocol_message':\n this.publish(message.data);\n break;\n case 'synthetic_event':\n this.onSyntheticEvent(message.data);\n break;\n }\n }\n\n private start() {\n this.stop();\n\n if (this.channel) {\n this.logger?.comm('Starting channel...', this.channel, this.logSuffix);\n this.pubNub = new PubNub({\n publishKey: 'pub-c-2a7fd67b-333d-40db-ad2d-3255f8835f70',\n subscribeKey: 'sub-c-0b655000-d784-11e6-b950-02ee2ddab7fe',\n ssl: true,\n // uuid: TODO\n presenceTimeout: 600,\n heartbeatInterval: 60,\n });\n\n this.pubNub.addListener({\n presence: this.onPubNubPresence,\n message: this.onPubNubMessage,\n status: this.onPubNubStatus,\n });\n\n this.pubNub.subscribe({\n channels: [this.channel],\n withPresence: true,\n });\n }\n }\n\n private stop() {\n if (this.pubNub) {\n this.logger?.comm('Stopping...', this.logSuffix);\n this.pubNub.removeListener({\n presence: this.onPubNubPresence,\n message: this.onPubNubMessage,\n status: this.onPubNubStatus,\n });\n\n this.pubNub.unsubscribe({\n channels: [this.channel],\n });\n\n this.pubNub.stop();\n this.pubNub = undefined;\n }\n }\n\n private onProtocolMessage(connectionId: string, message: ProtocolIncomingMessage) {\n if (message.type === 'RESEND_CODE') {\n if (this.codeMessage) {\n this.logger?.comm('Resending code...', this.logSuffix);\n this.publish(this.codeMessage);\n }\n } else {\n this.callback?.({\n type: 'protocol_message',\n connectionId,\n data: message,\n });\n }\n }\n\n private async publish(message: ProtocolOutgoingMessage) {\n if (!this.connectionsCount) {\n return;\n }\n if (this.pubNub) {\n try {\n await this.pubNub.publish({\n channel: this.channel,\n message,\n });\n } catch (e) {\n this.logger?.error('Failed to publish message', message.type, e, this.logSuffix);\n }\n } else {\n this.callback?.({\n type: 'synthetic_event',\n data: message,\n });\n }\n this.callback?.({\n type: 'send_message',\n data: message,\n });\n }\n\n private onPubNubPresence = (event: PubNub.PresenceEvent) => {\n switch (event.action) {\n case 'join':\n try {\n const device = JSON.parse(event.uuid);\n this.connectionsCount++;\n this.callback?.({\n type: 'connect',\n connectionId: event.uuid,\n data: device,\n });\n if (this.codeMessage) {\n this.logger?.comm('Sending code...', this.logSuffix);\n this.publish(this.codeMessage);\n }\n } catch (e) {\n // Wasn't from the device\n }\n break;\n case 'timeout':\n case 'leave':\n this.connectionsCount = Math.max(this.connectionsCount - 1, 0);\n this.callback?.({\n type: 'disconnect',\n connectionId: event.uuid,\n data: {\n timedout: event.action === 'timeout',\n },\n });\n break;\n }\n };\n\n private onPubNubMessage = (event: PubNub.MessageEvent) => {\n const message: ProtocolIncomingMessage = event.message;\n this.onProtocolMessage(event.publisher, message);\n };\n\n private onPubNubStatus = (_event: PubNub.StatusEvent) => {\n // TODO: Do something here?\n };\n\n private onPubNubVerifyCodeMessageSize = (codeMessage: ProtocolCodeMessage) => {\n // Calculate unencoded size (quickly) and if that exceeds the limit\n // then don't bother calculating the exact size (which is more expensive)\n let approxSize = 0;\n for (const path in codeMessage.diff) {\n approxSize += path.length + codeMessage.diff[path].length;\n }\n if (approxSize >= 32768) {\n return false;\n }\n\n // Calculate exact size and check whether it exceeds the limit\n const size = calcPubNubCodeMessageSize(this.channel, codeMessage);\n return size < 32768;\n };\n\n private onSyntheticEvent(data: any) {\n if (typeof data !== 'string') {\n return;\n }\n\n try {\n const message = JSON.parse(data);\n switch (message.type) {\n case 'CONNECT':\n this.connectionsCount++;\n this.callback?.({\n type: 'connect',\n connectionId: JSON.stringify(message.device),\n data: message.device,\n });\n break;\n case 'DISCONNECT':\n this.connectionsCount = Math.max(this.connectionsCount - 1, 0);\n this.callback?.({\n type: 'disconnect',\n connectionId: JSON.stringify(message.device),\n data: {\n timedout: false,\n },\n });\n break;\n case 'MESSAGE':\n this.onProtocolMessage(JSON.stringify(message.message.device), message.message);\n break;\n }\n } catch (e) {\n this.logger?.error('Failed to parse postMessage', e, data, this.logSuffix);\n }\n }\n\n private onCodeMessageReady: CodeMessageBuilderCallback = (codeMessage) => {\n this.codeMessage = codeMessage;\n this.logger?.comm('Sending code...', this.logSuffix);\n this.publish(codeMessage);\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Transport.js","sourceRoot":"","sources":["../../src/transports/Transport.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4B;AAE5B,oCAAiD;AACjD,4EAAsF;AAStF,SAAgB,yBAAyB,CAAC,OAAe,EAAE,WAAgC;IACzF,+GAA+G;IAC/G,OAAO,CACL,kBAAkB,CAChB,OAAO;QACL,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,OAAO,CACjC,WAAW,EACX,UAAC,CAAC,IAAK,OAAA,MAAI,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAI,EAAhD,CAAgD,CACxD,CACJ,CAAC,MAAM,GAAG,GAAG,CAC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K,CAAC,6BAA6B,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,EAAE;SAC5E;IACH,CAAC;IAOH,gBAAC;AAAD,CAAC,AA3PD,IA2PC","sourcesContent":["import PubNub from 'pubnub';\n\nimport { createLogger, Logger } from '../Logger';\nimport CodeMessageBuilder, { CodeMessageBuilderCallback } from './CodeMessageBuilder';\nimport { ProtocolOutgoingMessage, ProtocolIncomingMessage, ProtocolCodeMessage } from './Protocol';\nimport {\n SnackTransport,\n SnackTransportListener,\n SnackTransportMessage,\n SnackTransportOptions,\n} from './types';\n\nexport function calcPubNubCodeMessageSize(channel: string, codeMessage: ProtocolCodeMessage) {\n // https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-message-payload-size-before-publishing\n return (\n encodeURIComponent(\n channel +\n JSON.stringify(codeMessage).replace(\n /[!~*'()]/g,\n (x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`\n )\n ).length + 200\n );\n}\n\nexport default class Transport implements SnackTransport {\n private readonly channel: string;\n private readonly logger?: Logger;\n private callback?: SnackTransportListener;\n private pubNub?: PubNub;\n private pubNubClientId?: string;\n private codeMessageBuilder: CodeMessageBuilder;\n private codeMessage?: ProtocolCodeMessage;\n private connectionsCount: number = 0;\n private readonly logSuffix: string;\n\n constructor(options: SnackTransportOptions) {\n const { apiURL, channel, verbose } = options;\n this.channel = channel ?? '';\n this.logger = verbose ? createLogger(true) : undefined;\n this.logSuffix = options.name ? ` (${options.name})` : '';\n\n if (channel) {\n this.codeMessageBuilder = new CodeMessageBuilder({\n verifyCodeMessageSize: this.onPubNubVerifyCodeMessageSize,\n callback: this.onCodeMessageReady,\n apiURL,\n logger: this.logger,\n maxDiffPlaceholder: 'X'.repeat(32768),\n });\n } else {\n this.codeMessageBuilder = new CodeMessageBuilder({\n verifyCodeMessageSize: () => true,\n callback: this.onCodeMessageReady,\n logger: this.logger,\n });\n }\n }\n\n addEventListener(_type: 'message', callback: SnackTransportListener) {\n this.callback = callback;\n }\n\n removeEventListener(_type: 'message', _callback: SnackTransportListener) {\n this.callback = undefined;\n }\n\n postMessage(message: SnackTransportMessage) {\n switch (message.type) {\n case 'start':\n this.start();\n break;\n case 'stop':\n this.stop();\n break;\n case 'update_code':\n this.codeMessageBuilder.setCode(message.data);\n break;\n case 'protocol_message':\n this.publish(message.data);\n break;\n case 'synthetic_event':\n this.onSyntheticEvent(message.data);\n break;\n }\n }\n\n private start() {\n this.stop();\n\n if (!this.pubNubClientId) {\n // Keep track of the client ID per transport instance.\n // See: https://support.pubnub.com/hc/en-us/articles/360051496532-How-do-I-set-the-UUID-\n this.pubNubClientId = PubNub.generateUUID();\n }\n\n if (this.channel) {\n this.logger?.comm('Starting channel...', this.channel, this.logSuffix);\n this.pubNub = new PubNub({\n publishKey: 'pub-c-2a7fd67b-333d-40db-ad2d-3255f8835f70',\n subscribeKey: 'sub-c-0b655000-d784-11e6-b950-02ee2ddab7fe',\n uuid: this.pubNubClientId,\n ssl: true,\n presenceTimeout: 600,\n heartbeatInterval: 60,\n });\n\n this.pubNub.addListener({\n presence: this.onPubNubPresence,\n message: this.onPubNubMessage,\n status: this.onPubNubStatus,\n });\n\n this.pubNub.subscribe({\n channels: [this.channel],\n withPresence: true,\n });\n }\n }\n\n private stop() {\n if (this.pubNub) {\n this.logger?.comm('Stopping...', this.logSuffix);\n this.pubNub.removeListener({\n presence: this.onPubNubPresence,\n message: this.onPubNubMessage,\n status: this.onPubNubStatus,\n });\n\n this.pubNub.unsubscribe({\n channels: [this.channel],\n });\n\n this.pubNub.stop();\n this.pubNub = undefined;\n }\n }\n\n private onProtocolMessage(connectionId: string, message: ProtocolIncomingMessage) {\n if (message.type === 'RESEND_CODE') {\n if (this.codeMessage) {\n this.logger?.comm('Resending code...', this.logSuffix);\n this.publish(this.codeMessage);\n }\n } else {\n this.callback?.({\n type: 'protocol_message',\n connectionId,\n data: message,\n });\n }\n }\n\n private async publish(message: ProtocolOutgoingMessage) {\n if (!this.connectionsCount) {\n return;\n }\n if (this.pubNub) {\n try {\n await this.pubNub.publish({\n channel: this.channel,\n message,\n });\n } catch (e) {\n this.logger?.error('Failed to publish message', message.type, e, this.logSuffix);\n }\n } else {\n this.callback?.({\n type: 'synthetic_event',\n data: message,\n });\n }\n this.callback?.({\n type: 'send_message',\n data: message,\n });\n }\n\n private onPubNubPresence = (event: PubNub.PresenceEvent) => {\n switch (event.action) {\n case 'join':\n try {\n const device = JSON.parse(event.uuid);\n this.connectionsCount++;\n this.callback?.({\n type: 'connect',\n connectionId: event.uuid,\n data: device,\n });\n if (this.codeMessage) {\n this.logger?.comm('Sending code...', this.logSuffix);\n this.publish(this.codeMessage);\n }\n } catch (e) {\n // Wasn't from the device\n }\n break;\n case 'timeout':\n case 'leave':\n this.connectionsCount = Math.max(this.connectionsCount - 1, 0);\n this.callback?.({\n type: 'disconnect',\n connectionId: event.uuid,\n data: {\n timedout: event.action === 'timeout',\n },\n });\n break;\n }\n };\n\n private onPubNubMessage = (event: PubNub.MessageEvent) => {\n const message: ProtocolIncomingMessage = event.message;\n this.onProtocolMessage(event.publisher, message);\n };\n\n private onPubNubStatus = (_event: PubNub.StatusEvent) => {\n // TODO: Do something here?\n };\n\n private onPubNubVerifyCodeMessageSize = (codeMessage: ProtocolCodeMessage) => {\n // Calculate unencoded size (quickly) and if that exceeds the limit\n // then don't bother calculating the exact size (which is more expensive)\n let approxSize = 0;\n for (const path in codeMessage.diff) {\n approxSize += path.length + codeMessage.diff[path].length;\n }\n if (approxSize >= 32768) {\n return false;\n }\n\n // Calculate exact size and check whether it exceeds the limit\n const size = calcPubNubCodeMessageSize(this.channel, codeMessage);\n return size < 32768;\n };\n\n private onSyntheticEvent(data: any) {\n if (typeof data !== 'string') {\n return;\n }\n\n try {\n const message = JSON.parse(data);\n switch (message.type) {\n case 'CONNECT':\n this.connectionsCount++;\n this.callback?.({\n type: 'connect',\n connectionId: JSON.stringify(message.device),\n data: message.device,\n });\n break;\n case 'DISCONNECT':\n this.connectionsCount = Math.max(this.connectionsCount - 1, 0);\n this.callback?.({\n type: 'disconnect',\n connectionId: JSON.stringify(message.device),\n data: {\n timedout: false,\n },\n });\n break;\n case 'MESSAGE':\n this.onProtocolMessage(JSON.stringify(message.message.device), message.message);\n break;\n }\n } catch (e) {\n this.logger?.error('Failed to parse postMessage', e, data, this.logSuffix);\n }\n }\n\n private onCodeMessageReady: CodeMessageBuilderCallback = (codeMessage) => {\n this.codeMessage = codeMessage;\n this.logger?.comm('Sending code...', this.logSuffix);\n this.publish(codeMessage);\n };\n}\n"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { SnackTransport, SnackTransportOptions } from './types';
|
|
2
|
-
export * from './types';
|
|
3
|
-
export declare function createTransport(options: SnackTransportOptions): SnackTransport;
|
|
1
|
+
import { SnackTransport, SnackTransportOptions } from './types';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export declare function createTransport(options: SnackTransportOptions): SnackTransport;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.createTransport = void 0;
|
|
17
|
-
var Transport_1 = __importDefault(require("./Transport"));
|
|
18
|
-
__exportStar(require("./types"), exports);
|
|
19
|
-
function createTransport(options) {
|
|
20
|
-
return new Transport_1.default(options);
|
|
21
|
-
}
|
|
22
|
-
exports.createTransport = createTransport;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.createTransport = void 0;
|
|
17
|
+
var Transport_1 = __importDefault(require("./Transport"));
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
function createTransport(options) {
|
|
20
|
+
return new Transport_1.default(options);
|
|
21
|
+
}
|
|
22
|
+
exports.createTransport = createTransport;
|
|
23
23
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
import { SnackFiles, SnackDependencies, SDKVersion } from 'snack-content';
|
|
2
|
-
import { ProtocolOutgoingMessage, ProtocolIncomingMessage } from './Protocol';
|
|
3
|
-
export declare type SnackTransportStartMessage = {
|
|
4
|
-
type: 'start';
|
|
5
|
-
};
|
|
6
|
-
export declare type SnackTransportStopMessage = {
|
|
7
|
-
type: 'stop';
|
|
8
|
-
};
|
|
9
|
-
export declare type SnackTransportUpdateCodeMessage = {
|
|
10
|
-
type: 'update_code';
|
|
11
|
-
data: {
|
|
12
|
-
files: SnackFiles;
|
|
13
|
-
dependencies: SnackDependencies;
|
|
14
|
-
sdkVersion: SDKVersion;
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
export declare type SnackTransportProtocolMessage = {
|
|
18
|
-
type: 'protocol_message';
|
|
19
|
-
data: ProtocolOutgoingMessage;
|
|
20
|
-
};
|
|
21
|
-
export declare type SnackTransportSyntheticMessage = {
|
|
22
|
-
type: 'synthetic_event';
|
|
23
|
-
data: any;
|
|
24
|
-
};
|
|
25
|
-
export declare type SnackTransportMessage = SnackTransportStartMessage | SnackTransportStopMessage | SnackTransportUpdateCodeMessage | SnackTransportProtocolMessage | SnackTransportSyntheticMessage;
|
|
26
|
-
export declare type SnackTransportEventConnect = {
|
|
27
|
-
type: 'connect';
|
|
28
|
-
connectionId: string;
|
|
29
|
-
data: any;
|
|
30
|
-
};
|
|
31
|
-
export declare type SnackTransportEventDisconnect = {
|
|
32
|
-
type: 'disconnect';
|
|
33
|
-
connectionId: string;
|
|
34
|
-
data: {
|
|
35
|
-
timedout: boolean;
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
export declare type SnackTransportEventProtocolMessage = {
|
|
39
|
-
type: 'protocol_message';
|
|
40
|
-
connectionId: string;
|
|
41
|
-
data: ProtocolIncomingMessage;
|
|
42
|
-
};
|
|
43
|
-
export declare type SnackTransportEventSendMessage = {
|
|
44
|
-
type: 'send_message';
|
|
45
|
-
data: ProtocolOutgoingMessage;
|
|
46
|
-
};
|
|
47
|
-
export declare type SnackTransportEventSyntheticMessage = {
|
|
48
|
-
type: 'synthetic_event';
|
|
49
|
-
data: any;
|
|
50
|
-
};
|
|
51
|
-
export declare type SnackTransportEvent = SnackTransportEventConnect | SnackTransportEventDisconnect | SnackTransportEventProtocolMessage | SnackTransportEventSendMessage | SnackTransportEventSyntheticMessage;
|
|
52
|
-
export declare type SnackTransportListener = (event: SnackTransportEvent) => void;
|
|
53
|
-
export interface SnackTransport {
|
|
54
|
-
addEventListener(type: 'message', listener: SnackTransportListener): void;
|
|
55
|
-
removeEventListener(type: 'message', listener: SnackTransportListener): void;
|
|
56
|
-
postMessage(message: SnackTransportMessage): void;
|
|
57
|
-
}
|
|
58
|
-
export declare type SnackTransportOptions = {
|
|
59
|
-
channel?: string;
|
|
60
|
-
apiURL?: string;
|
|
61
|
-
verbose?: boolean;
|
|
62
|
-
name?: string;
|
|
63
|
-
};
|
|
1
|
+
import { SnackFiles, SnackDependencies, SDKVersion } from 'snack-content';
|
|
2
|
+
import { ProtocolOutgoingMessage, ProtocolIncomingMessage } from './Protocol';
|
|
3
|
+
export declare type SnackTransportStartMessage = {
|
|
4
|
+
type: 'start';
|
|
5
|
+
};
|
|
6
|
+
export declare type SnackTransportStopMessage = {
|
|
7
|
+
type: 'stop';
|
|
8
|
+
};
|
|
9
|
+
export declare type SnackTransportUpdateCodeMessage = {
|
|
10
|
+
type: 'update_code';
|
|
11
|
+
data: {
|
|
12
|
+
files: SnackFiles;
|
|
13
|
+
dependencies: SnackDependencies;
|
|
14
|
+
sdkVersion: SDKVersion;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare type SnackTransportProtocolMessage = {
|
|
18
|
+
type: 'protocol_message';
|
|
19
|
+
data: ProtocolOutgoingMessage;
|
|
20
|
+
};
|
|
21
|
+
export declare type SnackTransportSyntheticMessage = {
|
|
22
|
+
type: 'synthetic_event';
|
|
23
|
+
data: any;
|
|
24
|
+
};
|
|
25
|
+
export declare type SnackTransportMessage = SnackTransportStartMessage | SnackTransportStopMessage | SnackTransportUpdateCodeMessage | SnackTransportProtocolMessage | SnackTransportSyntheticMessage;
|
|
26
|
+
export declare type SnackTransportEventConnect = {
|
|
27
|
+
type: 'connect';
|
|
28
|
+
connectionId: string;
|
|
29
|
+
data: any;
|
|
30
|
+
};
|
|
31
|
+
export declare type SnackTransportEventDisconnect = {
|
|
32
|
+
type: 'disconnect';
|
|
33
|
+
connectionId: string;
|
|
34
|
+
data: {
|
|
35
|
+
timedout: boolean;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export declare type SnackTransportEventProtocolMessage = {
|
|
39
|
+
type: 'protocol_message';
|
|
40
|
+
connectionId: string;
|
|
41
|
+
data: ProtocolIncomingMessage;
|
|
42
|
+
};
|
|
43
|
+
export declare type SnackTransportEventSendMessage = {
|
|
44
|
+
type: 'send_message';
|
|
45
|
+
data: ProtocolOutgoingMessage;
|
|
46
|
+
};
|
|
47
|
+
export declare type SnackTransportEventSyntheticMessage = {
|
|
48
|
+
type: 'synthetic_event';
|
|
49
|
+
data: any;
|
|
50
|
+
};
|
|
51
|
+
export declare type SnackTransportEvent = SnackTransportEventConnect | SnackTransportEventDisconnect | SnackTransportEventProtocolMessage | SnackTransportEventSendMessage | SnackTransportEventSyntheticMessage;
|
|
52
|
+
export declare type SnackTransportListener = (event: SnackTransportEvent) => void;
|
|
53
|
+
export interface SnackTransport {
|
|
54
|
+
addEventListener(type: 'message', listener: SnackTransportListener): void;
|
|
55
|
+
removeEventListener(type: 'message', listener: SnackTransportListener): void;
|
|
56
|
+
postMessage(message: SnackTransportMessage): void;
|
|
57
|
+
}
|
|
58
|
+
export declare type SnackTransportOptions = {
|
|
59
|
+
channel?: string;
|
|
60
|
+
apiURL?: string;
|
|
61
|
+
verbose?: boolean;
|
|
62
|
+
name?: string;
|
|
63
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=types.js.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { SDKVersion } from 'snack-content';
|
|
2
|
-
import { SnackWindowRef } from '../types';
|
|
3
|
-
import { SnackTransport, SnackTransportOptions } from './types';
|
|
4
|
-
export declare function createWebPlayerTransport(config: {
|
|
5
|
-
name?: string;
|
|
6
|
-
verbose?: boolean;
|
|
7
|
-
webPlayerURL: string;
|
|
8
|
-
window: Window;
|
|
9
|
-
ref: SnackWindowRef;
|
|
10
|
-
createTransport: (options: SnackTransportOptions) => SnackTransport;
|
|
11
|
-
}): SnackTransport;
|
|
12
|
-
export declare function getWebPlayerIFrameURL(webPlayerURL: string, sdkVersion: SDKVersion, initialURL: string, verbose: boolean): string | undefined;
|
|
1
|
+
import { SDKVersion } from 'snack-content';
|
|
2
|
+
import { SnackWindowRef } from '../types';
|
|
3
|
+
import { SnackTransport, SnackTransportOptions } from './types';
|
|
4
|
+
export declare function createWebPlayerTransport(config: {
|
|
5
|
+
name?: string;
|
|
6
|
+
verbose?: boolean;
|
|
7
|
+
webPlayerURL: string;
|
|
8
|
+
window: Window;
|
|
9
|
+
ref: SnackWindowRef;
|
|
10
|
+
createTransport: (options: SnackTransportOptions) => SnackTransport;
|
|
11
|
+
}): SnackTransport;
|
|
12
|
+
export declare function getWebPlayerIFrameURL(webPlayerURL: string, sdkVersion: SDKVersion, initialURL: string, verbose: boolean): string | undefined;
|
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getWebPlayerIFrameURL = exports.createWebPlayerTransport = void 0;
|
|
4
|
-
function createWebPlayerTransport(config) {
|
|
5
|
-
var transport = null;
|
|
6
|
-
function getTransport() {
|
|
7
|
-
var _a;
|
|
8
|
-
transport = transport !== null && transport !== void 0 ? transport : config.createTransport({
|
|
9
|
-
name: (_a = config.name) !== null && _a !== void 0 ? _a : 'webplayer',
|
|
10
|
-
verbose: config.verbose,
|
|
11
|
-
});
|
|
12
|
-
return transport;
|
|
13
|
-
}
|
|
14
|
-
var origin = new URL(config.webPlayerURL).origin;
|
|
15
|
-
var windowListener;
|
|
16
|
-
var transportListener;
|
|
17
|
-
return {
|
|
18
|
-
addEventListener: function (type, listener) {
|
|
19
|
-
var _a;
|
|
20
|
-
if (!windowListener) {
|
|
21
|
-
windowListener = function (event) {
|
|
22
|
-
if (event.origin === origin && typeof event.data === 'string') {
|
|
23
|
-
if (transport) {
|
|
24
|
-
transport.postMessage({
|
|
25
|
-
type: 'synthetic_event',
|
|
26
|
-
data: event.data,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
(_a = config.window) === null || _a === void 0 ? void 0 : _a.addEventListener(type, windowListener);
|
|
32
|
-
}
|
|
33
|
-
if (!transportListener) {
|
|
34
|
-
transportListener = function (event) {
|
|
35
|
-
var _a;
|
|
36
|
-
var message = event;
|
|
37
|
-
if (message.type === 'synthetic_event') {
|
|
38
|
-
(_a = config.ref.current) === null || _a === void 0 ? void 0 : _a.postMessage(JSON.stringify(message.data), origin);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
listener(message);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
getTransport().addEventListener(type, transportListener);
|
|
46
|
-
},
|
|
47
|
-
removeEventListener: function (type, _listener) {
|
|
48
|
-
var _a;
|
|
49
|
-
if (windowListener) {
|
|
50
|
-
(_a = config.window) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, windowListener);
|
|
51
|
-
windowListener = undefined;
|
|
52
|
-
}
|
|
53
|
-
if (transportListener) {
|
|
54
|
-
transport === null || transport === void 0 ? void 0 : transport.removeEventListener(type, transportListener);
|
|
55
|
-
transportListener = undefined;
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
postMessage: function (message) { return getTransport().postMessage(message); },
|
|
59
|
-
// @ts-ignore: Dirty hack to allow snack web to to communicate with older web-players
|
|
60
|
-
updateOrigin: function (newOrigin) { return (origin = newOrigin); },
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
exports.createWebPlayerTransport = createWebPlayerTransport;
|
|
64
|
-
function getWebPlayerIFrameURL(webPlayerURL, sdkVersion, initialURL, verbose) {
|
|
65
|
-
if (sdkVersion < '40.0.0' || typeof window === 'undefined') {
|
|
66
|
-
return undefined;
|
|
67
|
-
}
|
|
68
|
-
return webPlayerURL.replace('%%SDK_VERSION%%', sdkVersion.split('.')[0]) + "/index.html?initialUrl=" + encodeURIComponent(initialURL) + "&origin=" + encodeURIComponent(window.location.origin) + "&verbose=" + verbose;
|
|
69
|
-
}
|
|
70
|
-
exports.getWebPlayerIFrameURL = getWebPlayerIFrameURL;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getWebPlayerIFrameURL = exports.createWebPlayerTransport = void 0;
|
|
4
|
+
function createWebPlayerTransport(config) {
|
|
5
|
+
var transport = null;
|
|
6
|
+
function getTransport() {
|
|
7
|
+
var _a;
|
|
8
|
+
transport = transport !== null && transport !== void 0 ? transport : config.createTransport({
|
|
9
|
+
name: (_a = config.name) !== null && _a !== void 0 ? _a : 'webplayer',
|
|
10
|
+
verbose: config.verbose,
|
|
11
|
+
});
|
|
12
|
+
return transport;
|
|
13
|
+
}
|
|
14
|
+
var origin = new URL(config.webPlayerURL).origin;
|
|
15
|
+
var windowListener;
|
|
16
|
+
var transportListener;
|
|
17
|
+
return {
|
|
18
|
+
addEventListener: function (type, listener) {
|
|
19
|
+
var _a;
|
|
20
|
+
if (!windowListener) {
|
|
21
|
+
windowListener = function (event) {
|
|
22
|
+
if (event.origin === origin && typeof event.data === 'string') {
|
|
23
|
+
if (transport) {
|
|
24
|
+
transport.postMessage({
|
|
25
|
+
type: 'synthetic_event',
|
|
26
|
+
data: event.data,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
(_a = config.window) === null || _a === void 0 ? void 0 : _a.addEventListener(type, windowListener);
|
|
32
|
+
}
|
|
33
|
+
if (!transportListener) {
|
|
34
|
+
transportListener = function (event) {
|
|
35
|
+
var _a;
|
|
36
|
+
var message = event;
|
|
37
|
+
if (message.type === 'synthetic_event') {
|
|
38
|
+
(_a = config.ref.current) === null || _a === void 0 ? void 0 : _a.postMessage(JSON.stringify(message.data), origin);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
listener(message);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
getTransport().addEventListener(type, transportListener);
|
|
46
|
+
},
|
|
47
|
+
removeEventListener: function (type, _listener) {
|
|
48
|
+
var _a;
|
|
49
|
+
if (windowListener) {
|
|
50
|
+
(_a = config.window) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, windowListener);
|
|
51
|
+
windowListener = undefined;
|
|
52
|
+
}
|
|
53
|
+
if (transportListener) {
|
|
54
|
+
transport === null || transport === void 0 ? void 0 : transport.removeEventListener(type, transportListener);
|
|
55
|
+
transportListener = undefined;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
postMessage: function (message) { return getTransport().postMessage(message); },
|
|
59
|
+
// @ts-ignore: Dirty hack to allow snack web to to communicate with older web-players
|
|
60
|
+
updateOrigin: function (newOrigin) { return (origin = newOrigin); },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
exports.createWebPlayerTransport = createWebPlayerTransport;
|
|
64
|
+
function getWebPlayerIFrameURL(webPlayerURL, sdkVersion, initialURL, verbose) {
|
|
65
|
+
if (sdkVersion < '40.0.0' || typeof window === 'undefined') {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
return webPlayerURL.replace('%%SDK_VERSION%%', sdkVersion.split('.')[0]) + "/index.html?initialUrl=" + encodeURIComponent(initialURL) + "&origin=" + encodeURIComponent(window.location.origin) + "&verbose=" + verbose;
|
|
69
|
+
}
|
|
70
|
+
exports.getWebPlayerIFrameURL = getWebPlayerIFrameURL;
|
|
71
71
|
//# sourceMappingURL=webPlayer.js.map
|