snack-sdk 4.0.0 → 4.0.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.
- package/CHANGELOG.md +6 -0
- package/build/transports/TransportImplBase.d.ts +1 -0
- package/build/transports/TransportImplBase.js +3 -1
- package/build/transports/TransportImplBase.js.map +1 -1
- package/build/transports/TransportImplPubNub.js +2 -2
- package/build/transports/TransportImplPubNub.js.map +1 -1
- package/build/transports/TransportImplSocketIO.js +1 -3
- package/build/transports/TransportImplSocketIO.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Unpublished
|
|
4
4
|
|
|
5
|
+
### 🐛 Bug fixes
|
|
6
|
+
|
|
7
|
+
- Fixed `undefined` importing file when its size exceeds 32768. ([#360](https://github.com/expo/snack/pull/423) by [@kudo](https://github.com/kudo))
|
|
8
|
+
|
|
9
|
+
## 4.0.0 - 2023-04-21
|
|
10
|
+
|
|
5
11
|
### 🛠 Breaking changes
|
|
6
12
|
|
|
7
13
|
- Replace the default transport from PubNub to SnackPub ([#376](https://github.com/expo/snack/pull/376), [#417](https://github.com/expo/snack/pull/417) by [@kudo](https://github.com/kudo)). To load SDK 45/46/47 snacks in Expo Go, you need to update your snack-sdk version to this version or a newer version that is compatible with the latest Expo Go snack runtime. There are no other migrations required.
|
|
@@ -9,6 +9,7 @@ export default abstract class TransportImplBase implements SnackTransport {
|
|
|
9
9
|
private codeMessage?;
|
|
10
10
|
private connectionsCount;
|
|
11
11
|
protected readonly logSuffix: string;
|
|
12
|
+
protected static readonly CODE_SIZE_LIMIT_FOR_DIFF = 32768;
|
|
12
13
|
constructor(options: SnackTransportOptions);
|
|
13
14
|
addEventListener(_type: 'message', callback: SnackTransportListener): void;
|
|
14
15
|
removeEventListener(_type: 'message', _callback: SnackTransportListener): void;
|
|
@@ -61,7 +61,7 @@ var TransportImplBase = /** @class */ (function () {
|
|
|
61
61
|
callback: this.onCodeMessageReady,
|
|
62
62
|
apiURL: apiURL,
|
|
63
63
|
logger: this.logger,
|
|
64
|
-
maxDiffPlaceholder: 'X'.repeat(
|
|
64
|
+
maxDiffPlaceholder: 'X'.repeat(TransportImplBase.CODE_SIZE_LIMIT_FOR_DIFF),
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
else {
|
|
@@ -225,6 +225,8 @@ var TransportImplBase = /** @class */ (function () {
|
|
|
225
225
|
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.error('Failed to parse postMessage', e, data, this.logSuffix);
|
|
226
226
|
}
|
|
227
227
|
};
|
|
228
|
+
// Limit for code size in diff message
|
|
229
|
+
TransportImplBase.CODE_SIZE_LIMIT_FOR_DIFF = 32768;
|
|
228
230
|
return TransportImplBase;
|
|
229
231
|
}());
|
|
230
232
|
exports.default = TransportImplBase;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransportImplBase.js","sourceRoot":"","sources":["../../src/transports/TransportImplBase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAAiD;AACjD,4EAAsF;AAStF;IASE,2BAAY,OAA8B;QAA1C,iBAqBC;QAxBO,qBAAgB,GAAW,CAAC,CAAC;QA+L7B,uBAAkB,GAA+B,UAAC,WAAW;;YACnE,KAAI,CAAC,WAAW,GAAG,WAAW,CAAC;YAC/B,MAAA,KAAI,CAAC,MAAM,0CAAE,IAAI,CAAC,iBAAiB,EAAE,KAAI,CAAC,SAAS,EAAE;YACrD,KAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC;QA/LQ,IAAA,MAAM,GAAuB,OAAO,OAA9B,EAAE,OAAO,GAAc,OAAO,QAArB,EAAE,OAAO,GAAK,OAAO,QAAZ,CAAa;QAC7C,IAAI,CAAC,OAAO,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;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M,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACjC,QAAQ,OAAO,CAAC,IAAI,EAAE;gBACpB,KAAK,SAAS;oBACZ,IAAI,CAAC,gBAAgB,EAAE,CAAC;oBACxB,MAAA,IAAI,CAAC,QAAQ,+CAAb,IAAI,EAAY;wBACd,IAAI,EAAE,SAAS;wBACf,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC;wBAC5C,IAAI,EAAE,OAAO,CAAC,MAAM;qBACrB,EAAE;oBACH,MAAM;gBACR,KAAK,YAAY;oBACf,IAAI,CAAC,gBAAgB,GAAG,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,EAAE;qBACT,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,wBAAC;AAAD,CAAC,AA1MD,IA0MC","sourcesContent":["import { 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 default abstract class TransportImplBase implements SnackTransport {\n protected readonly channel: string;\n protected readonly logger?: Logger;\n private callback?: SnackTransportListener;\n private codeMessageBuilder: CodeMessageBuilder;\n private codeMessage?: ProtocolCodeMessage;\n private connectionsCount: number = 0;\n protected 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.onVerifyCodeMessageSize,\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 /**\n * Start transport\n */\n protected abstract start(): void;\n\n /**\n * Stop transport\n */\n protected abstract stop(): void;\n\n /**\n * Is transport started\n */\n\n protected abstract isStarted(): boolean;\n\n /**\n * Send message to target channel\n */\n\n protected abstract sendAsync(channel: string, message: ProtocolOutgoingMessage): Promise<void>;\n\n /**\n * Verify whether the transport is able to send the code message with given size\n * @returns true if the code message size is valid\n */\n protected abstract onVerifyCodeMessageSize(codeMessage: ProtocolCodeMessage): boolean;\n\n /**\n * Helper function for derived transports.\n * When the derived transport receives message, it should call this function to process message data.\n */\n protected handleMessage(senderConnectionId: string, message: ProtocolIncomingMessage) {\n this.onProtocolMessage(senderConnectionId, message);\n }\n\n /**\n * Helper function for derived transports.\n * When the derived transport receives channel join/leave events, it should call this function to process channel presence data.\n */\n protected handleChannelEvent(event: 'join' | 'leave', connectionId: string) {\n if (event === 'join') {\n try {\n const device = JSON.parse(connectionId);\n this.connectionsCount++;\n this.callback?.({\n type: 'connect',\n connectionId,\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 } else {\n this.connectionsCount = Math.max(this.connectionsCount - 1, 0);\n this.callback?.({\n type: 'disconnect',\n connectionId,\n data: {},\n });\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.isStarted()) {\n try {\n await this.sendAsync(this.channel, message);\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 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 });\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":"TransportImplBase.js","sourceRoot":"","sources":["../../src/transports/TransportImplBase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAAiD;AACjD,4EAAsF;AAStF;IAYE,2BAAY,OAA8B;QAA1C,iBAqBC;QA3BO,qBAAgB,GAAW,CAAC,CAAC;QAkM7B,uBAAkB,GAA+B,UAAC,WAAW;;YACnE,KAAI,CAAC,WAAW,GAAG,WAAW,CAAC;YAC/B,MAAA,KAAI,CAAC,MAAM,0CAAE,IAAI,CAAC,iBAAiB,EAAE,KAAI,CAAC,SAAS,EAAE;YACrD,KAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC;QA/LQ,IAAA,MAAM,GAAuB,OAAO,OAA9B,EAAE,OAAO,GAAc,OAAO,QAArB,EAAE,OAAO,GAAK,OAAO,QAAZ,CAAa;QAC7C,IAAI,CAAC,OAAO,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,qBAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACvD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,OAAK,OAAO,CAAC,IAAI,MAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QAE1D,IAAI,OAAO,EAAE;YACX,IAAI,CAAC,kBAAkB,GAAG,IAAI,4BAAkB,CAAC;gBAC/C,qBAAqB,EAAE,IAAI,CAAC,uBAAuB;g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EAAE;oBACH,MAAM;gBACR,KAAK,YAAY;oBACf,IAAI,CAAC,gBAAgB,GAAG,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,EAAE;qBACT,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;IA7LD,sCAAsC;IACZ,0CAAwB,GAAG,KAAK,CAAC;IAmM7D,wBAAC;CAAA,AA7MD,IA6MC;kBA7M6B,iBAAiB","sourcesContent":["import { 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 default abstract class TransportImplBase implements SnackTransport {\n protected readonly channel: string;\n protected readonly logger?: Logger;\n private callback?: SnackTransportListener;\n private codeMessageBuilder: CodeMessageBuilder;\n private codeMessage?: ProtocolCodeMessage;\n private connectionsCount: number = 0;\n protected readonly logSuffix: string;\n\n // Limit for code size in diff message\n protected static readonly CODE_SIZE_LIMIT_FOR_DIFF = 32768;\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.onVerifyCodeMessageSize,\n callback: this.onCodeMessageReady,\n apiURL,\n logger: this.logger,\n maxDiffPlaceholder: 'X'.repeat(TransportImplBase.CODE_SIZE_LIMIT_FOR_DIFF),\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 /**\n * Start transport\n */\n protected abstract start(): void;\n\n /**\n * Stop transport\n */\n protected abstract stop(): void;\n\n /**\n * Is transport started\n */\n\n protected abstract isStarted(): boolean;\n\n /**\n * Send message to target channel\n */\n\n protected abstract sendAsync(channel: string, message: ProtocolOutgoingMessage): Promise<void>;\n\n /**\n * Verify whether the transport is able to send the code message with given size\n * @returns true if the code message size is valid\n */\n protected abstract onVerifyCodeMessageSize(codeMessage: ProtocolCodeMessage): boolean;\n\n /**\n * Helper function for derived transports.\n * When the derived transport receives message, it should call this function to process message data.\n */\n protected handleMessage(senderConnectionId: string, message: ProtocolIncomingMessage) {\n this.onProtocolMessage(senderConnectionId, message);\n }\n\n /**\n * Helper function for derived transports.\n * When the derived transport receives channel join/leave events, it should call this function to process channel presence data.\n */\n protected handleChannelEvent(event: 'join' | 'leave', connectionId: string) {\n if (event === 'join') {\n try {\n const device = JSON.parse(connectionId);\n this.connectionsCount++;\n this.callback?.({\n type: 'connect',\n connectionId,\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 } else {\n this.connectionsCount = Math.max(this.connectionsCount - 1, 0);\n this.callback?.({\n type: 'disconnect',\n connectionId,\n data: {},\n });\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.isStarted()) {\n try {\n await this.sendAsync(this.channel, message);\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 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 });\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"]}
|
|
@@ -156,12 +156,12 @@ var TransportImplPubNub = /** @class */ (function (_super) {
|
|
|
156
156
|
for (var path in codeMessage.diff) {
|
|
157
157
|
approxSize += path.length + codeMessage.diff[path].length;
|
|
158
158
|
}
|
|
159
|
-
if (approxSize >=
|
|
159
|
+
if (approxSize >= TransportImplBase_1.default.CODE_SIZE_LIMIT_FOR_DIFF) {
|
|
160
160
|
return false;
|
|
161
161
|
}
|
|
162
162
|
// Calculate exact size and check whether it exceeds the limit
|
|
163
163
|
var size = calcPubNubCodeMessageSize(this.channel, codeMessage);
|
|
164
|
-
return size <
|
|
164
|
+
return size < TransportImplBase_1.default.CODE_SIZE_LIMIT_FOR_DIFF;
|
|
165
165
|
};
|
|
166
166
|
return TransportImplPubNub;
|
|
167
167
|
}(TransportImplBase_1.default));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransportImplPubNub.js","sourceRoot":"","sources":["../../src/transports/TransportImplPubNub.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4B;AAG5B,0EAAoD;AAEpD,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;IAAiD,uCAAiB;IAAlE;QAAA,qEAsGC;QApBS,sBAAgB,GAAG,UAAC,KAA2B;YACrD,QAAQ,KAAK,CAAC,MAAM,EAAE;gBACpB,KAAK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CAAC,MAAM,CAAC,WAAW,CAAC;gBACtB,QAAQ,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC;aACzB,CAAC,CAAC;YAEH,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACnB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;SACzB;IACH,CAAC;IAES,uCAAS,GAAnB;QACE,OAAO,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC;IAC7B,CAAC;IAEe,uCAAS,GAAzB,UAA0B,OAAe,EAAE,OAAgC;;;;;4BACzE,4BAAM,IAAI,CAAC,MAAM,0CAAE,OAAO,CAAC;4BACzB,OAAO,SAAA;4BACP,OAAO,SAAA;yBACR,IAAC;;wBAHF,SAGE,CAAC;;;;;KACJ;IAES,qDAAuB,GAAjC,UAAkC,WAAgC;QAChE,mEAAmE;QACnE,yEAAyE;QACzE,IAAI,UAAU,GAAG,CAAC,CAAC;QACnB,KAAK,IAAM,IAAI,IAAI,WAAW,CAAC,IAAI,EAAE;YACnC,UAAU,IAAI,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;SAC3D;QACD,IAAI,UAAU,IAAI,2BAAiB,CAAC,wBAAwB,EAAE;YAC5D,OAAO,KAAK,CAAC;SACd;QAED,8DAA8D;QAC9D,IAAM,IAAI,GAAG,yBAAyB,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAClE,OAAO,IAAI,GAAG,2BAAiB,CAAC,wBAAwB,CAAC;IAC3D,CAAC;IAsBH,0BAAC;AAAD,CAAC,AAtGD,CAAiD,2BAAiB,GAsGjE","sourcesContent":["import PubNub from 'pubnub';\n\nimport { ProtocolOutgoingMessage, ProtocolIncomingMessage, ProtocolCodeMessage } from './Protocol';\nimport TransportImplBase from './TransportImplBase';\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 TransportImplPubNub extends TransportImplBase {\n private pubNub?: PubNub;\n private pubNubClientId?: string;\n\n protected 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 protected 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 protected isStarted() {\n return this.pubNub != null;\n }\n\n protected async sendAsync(channel: string, message: ProtocolOutgoingMessage): Promise<void> {\n await this.pubNub?.publish({\n channel,\n message,\n });\n }\n\n protected onVerifyCodeMessageSize(codeMessage: ProtocolCodeMessage): boolean {\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 >= TransportImplBase.CODE_SIZE_LIMIT_FOR_DIFF) {\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 < TransportImplBase.CODE_SIZE_LIMIT_FOR_DIFF;\n }\n\n private onPubNubPresence = (event: PubNub.PresenceEvent) => {\n switch (event.action) {\n case 'join':\n this.handleChannelEvent('join', event.uuid);\n break;\n case 'timeout':\n case 'leave':\n this.handleChannelEvent('leave', event.uuid);\n break;\n }\n };\n\n private onPubNubMessage = (event: PubNub.MessageEvent) => {\n const message: ProtocolIncomingMessage = event.message;\n this.handleMessage(event.publisher, message);\n };\n\n private onPubNubStatus = (_event: PubNub.StatusEvent) => {\n // TODO: Do something here?\n };\n}\n"]}
|
|
@@ -106,15 +106,13 @@ var TransportImplSocketIO = /** @class */ (function (_super) {
|
|
|
106
106
|
return Promise.resolve();
|
|
107
107
|
};
|
|
108
108
|
TransportImplSocketIO.prototype.onVerifyCodeMessageSize = function (codeMessage) {
|
|
109
|
-
// https://socket.io/docs/v4/server-options/#maxhttpbuffersize
|
|
110
|
-
var MAX_SIZE = 1e6;
|
|
111
109
|
// Calculate unencoded size (quickly) and if that exceeds the limit
|
|
112
110
|
// then don't bother calculating the exact size (which is more expensive)
|
|
113
111
|
var approxSize = 0;
|
|
114
112
|
for (var path in codeMessage.diff) {
|
|
115
113
|
approxSize += path.length + codeMessage.diff[path].length;
|
|
116
114
|
}
|
|
117
|
-
return approxSize <
|
|
115
|
+
return approxSize < TransportImplBase_1.default.CODE_SIZE_LIMIT_FOR_DIFF;
|
|
118
116
|
};
|
|
119
117
|
return TransportImplSocketIO;
|
|
120
118
|
}(TransportImplBase_1.default));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransportImplSocketIO.js","sourceRoot":"","sources":["../../src/transports/TransportImplSocketIO.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,qDAAsC;AAGtC,wFAAkE;AAElE,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AI,CAAC,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;SAC3D;QACD,OAAO,UAAU,GAAG,2BAAiB,CAAC,wBAAwB,CAAC;IACjE,CAAC;IAwBH,4BAAC;AAAD,CAAC,AA1GD,CAAmD,2BAAiB,GA0GnE","sourcesContent":["import { io } from 'socket.io-client';\nimport type { Socket } from 'socket.io-client';\n\nimport ConnectionMetricsEmitter from './ConnectionMetricsEmitter';\nimport { ProtocolOutgoingMessage, ProtocolIncomingMessage, ProtocolCodeMessage } from './Protocol';\nimport TransportImplBase from './TransportImplBase';\nimport type { SnackTransportOptions } from './types';\n\ninterface ServerToClientEvents {\n message: (data: { channel: string; message: ProtocolIncomingMessage; sender: string }) => void;\n joinChannel: (data: { channel: string; sender: string }) => void;\n leaveChannel: (data: { channel: string; sender: string }) => void;\n terminate: (reason: string) => void;\n}\n\ninterface ClientToServerEvents {\n message: (data: { channel: string; message: ProtocolOutgoingMessage; sender: string }) => void;\n subscribeChannel: (data: { channel: string; sender: string }) => void;\n unsubscribeChannel: (data: { channel: string; sender: string }) => void;\n}\n\nexport default class TransportImplSocketIO extends TransportImplBase {\n private readonly snackpubURL: string;\n private socket: Socket<ServerToClientEvents, ClientToServerEvents> | null = null;\n private startTime: number | undefined;\n private connectionAttempts: number;\n\n constructor(options: SnackTransportOptions) {\n super(options);\n const { snackpubURL } = options;\n if (!snackpubURL) {\n throw new Error('The `snackpubURL` option is unspecified.');\n }\n this.snackpubURL = snackpubURL;\n this.connectionAttempts = 0;\n }\n\n protected start(): void {\n this.stop();\n this.startTime = Date.now();\n\n this.socket = io(this.snackpubURL, { transports: ['websocket'] });\n\n this.socket.on('connect', () => {\n this.socket?.emit('subscribeChannel', { channel: this.channel, sender: this.socket?.id });\n if (this.startTime) {\n ConnectionMetricsEmitter.emitSuccessed({\n timeMs: Date.now() - this.startTime,\n attempts: this.connectionAttempts,\n });\n }\n });\n this.socket.io.on('reconnect_attempt', (attempts: number) => {\n this.connectionAttempts = attempts;\n if (this.startTime) {\n ConnectionMetricsEmitter.emitFailed({\n timeMs: Date.now() - this.startTime,\n attempts,\n });\n }\n });\n\n this.socket.on('message', this.onMessage);\n this.socket.on('joinChannel', this.onJoinChannel);\n this.socket.on('leaveChannel', this.onLeaveChannel);\n this.socket.on('terminate', (reason) => {\n this.logger?.comm(`Terminating connection: ${reason}`);\n this.socket?.disconnect();\n });\n }\n\n protected stop(): void {\n if (this.socket != null) {\n this.logger?.comm('Stopping...', this.logSuffix);\n\n this.socket.offAny();\n this.socket.close();\n this.socket = null;\n }\n this.startTime = undefined;\n }\n\n protected isStarted(): boolean {\n return this.socket != null;\n }\n\n protected sendAsync(channel: string, message: ProtocolOutgoingMessage): Promise<void> {\n this.socket?.emit('message', {\n channel,\n message,\n sender: this.socket.id,\n });\n return Promise.resolve();\n }\n\n protected onVerifyCodeMessageSize(codeMessage: ProtocolCodeMessage): boolean {\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 return approxSize < TransportImplBase.CODE_SIZE_LIMIT_FOR_DIFF;\n }\n\n private onMessage = (data: {\n channel: string;\n message: ProtocolIncomingMessage;\n sender: string;\n }) => {\n const { sender, message } = data;\n this.handleMessage(sender, message);\n };\n\n private onJoinChannel = (data: { channel: string; sender: string }) => {\n const { sender } = data;\n if (sender !== '' && sender !== this.socket?.id) {\n this.handleChannelEvent('join', sender);\n }\n };\n\n private onLeaveChannel = (data: { channel: string; sender: string }) => {\n const { sender } = data;\n if (sender !== '' && sender !== this.socket?.id) {\n this.handleChannelEvent('leave', sender);\n }\n };\n}\n"]}
|