snack-sdk 3.8.0 → 3.10.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +27 -1
  2. package/build/DependencyResolver.d.ts +26 -26
  3. package/build/DependencyResolver.js +258 -258
  4. package/build/DevSession.d.ts +20 -20
  5. package/build/DevSession.js +203 -203
  6. package/build/FileUploader.d.ts +23 -23
  7. package/build/FileUploader.js +174 -174
  8. package/build/Logger.d.ts +11 -11
  9. package/build/Logger.js +33 -33
  10. package/build/Session.d.ts +305 -301
  11. package/build/Session.js +1198 -1171
  12. package/build/Session.js.map +1 -1
  13. package/build/State.d.ts +16 -16
  14. package/build/State.js +90 -90
  15. package/build/WantedVersions.d.ts +40 -17
  16. package/build/WantedVersions.js +156 -93
  17. package/build/WantedVersions.js.map +1 -1
  18. package/build/defaultConfig.d.ts +12 -12
  19. package/build/defaultConfig.js +29 -29
  20. package/build/index.d.ts +7 -7
  21. package/build/index.js +30 -30
  22. package/build/transports/CodeMessageBuilder.d.ts +31 -31
  23. package/build/transports/CodeMessageBuilder.js +196 -196
  24. package/build/transports/Protocol.d.ts +64 -64
  25. package/build/transports/Protocol.js +2 -2
  26. package/build/transports/Transport.d.ts +28 -27
  27. package/build/transports/Transport.js +303 -298
  28. package/build/transports/Transport.js.map +1 -1
  29. package/build/transports/index.d.ts +3 -3
  30. package/build/transports/index.js +22 -22
  31. package/build/transports/types.d.ts +63 -63
  32. package/build/transports/types.js +2 -2
  33. package/build/transports/webPlayer.d.ts +12 -12
  34. package/build/transports/webPlayer.js +70 -70
  35. package/build/types.d.ts +189 -185
  36. package/build/types.js +2 -2
  37. package/build/types.js.map +1 -1
  38. package/build/utils.d.ts +17 -17
  39. package/build/utils.js +68 -68
  40. package/package.json +11 -4
  41. package/src/DependencyResolver.ts +0 -218
  42. package/src/DevSession.ts +0 -159
  43. package/src/FileUploader.ts +0 -135
  44. package/src/Logger.ts +0 -33
  45. package/src/Session.ts +0 -1293
  46. package/src/State.ts +0 -98
  47. package/src/WantedVersions.ts +0 -57
  48. package/src/defaultConfig.ts +0 -33
  49. package/src/index.ts +0 -59
  50. package/src/transports/CodeMessageBuilder.ts +0 -226
  51. package/src/transports/Protocol.ts +0 -78
  52. package/src/transports/Transport.ts +0 -270
  53. package/src/transports/index.ts +0 -8
  54. package/src/transports/types.ts +0 -89
  55. package/src/transports/webPlayer.ts +0 -93
  56. package/src/types.ts +0 -223
  57. package/src/utils.ts +0 -71
  58. package/tsconfig.json +0 -28
@@ -1,299 +1,304 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.calcPubNubCodeMessageSize = void 0;
43
- var pubnub_1 = __importDefault(require("pubnub"));
44
- var Logger_1 = require("../Logger");
45
- var CodeMessageBuilder_1 = __importDefault(require("./CodeMessageBuilder"));
46
- function calcPubNubCodeMessageSize(channel, codeMessage) {
47
- // https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-message-payload-size-before-publishing
48
- return (encodeURIComponent(channel +
49
- JSON.stringify(codeMessage).replace(/[!~*'()]/g, function (x) { return "%" + x.charCodeAt(0).toString(16).toUpperCase(); })).length + 200);
50
- }
51
- exports.calcPubNubCodeMessageSize = calcPubNubCodeMessageSize;
52
- var Transport = /** @class */ (function () {
53
- function Transport(options) {
54
- var _this = this;
55
- this.connectionsCount = 0;
56
- this.onPubNubPresence = function (event) {
57
- var _a, _b, _c;
58
- switch (event.action) {
59
- case 'join':
60
- try {
61
- var device = JSON.parse(event.uuid);
62
- _this.connectionsCount++;
63
- (_a = _this.callback) === null || _a === void 0 ? void 0 : _a.call(_this, {
64
- type: 'connect',
65
- connectionId: event.uuid,
66
- data: device,
67
- });
68
- if (_this.codeMessage) {
69
- (_b = _this.logger) === null || _b === void 0 ? void 0 : _b.comm('Sending code...', _this.logSuffix);
70
- _this.publish(_this.codeMessage);
71
- }
72
- }
73
- catch (e) {
74
- // Wasn't from the device
75
- }
76
- break;
77
- case 'timeout':
78
- case 'leave':
79
- _this.connectionsCount = Math.max(_this.connectionsCount - 1, 0);
80
- (_c = _this.callback) === null || _c === void 0 ? void 0 : _c.call(_this, {
81
- type: 'disconnect',
82
- connectionId: event.uuid,
83
- data: {
84
- timedout: event.action === 'timeout',
85
- },
86
- });
87
- break;
88
- }
89
- };
90
- this.onPubNubMessage = function (event) {
91
- var message = event.message;
92
- _this.onProtocolMessage(event.publisher, message);
93
- };
94
- this.onPubNubStatus = function (_event) {
95
- // TODO: Do something here?
96
- };
97
- this.onPubNubVerifyCodeMessageSize = function (codeMessage) {
98
- // Calculate unencoded size (quickly) and if that exceeds the limit
99
- // then don't bother calculating the exact size (which is more expensive)
100
- var approxSize = 0;
101
- for (var path in codeMessage.diff) {
102
- approxSize += path.length + codeMessage.diff[path].length;
103
- }
104
- if (approxSize >= 32768) {
105
- return false;
106
- }
107
- // Calculate exact size and check whether it exceeds the limit
108
- var size = calcPubNubCodeMessageSize(_this.channel, codeMessage);
109
- return size < 32768;
110
- };
111
- this.onCodeMessageReady = function (codeMessage) {
112
- var _a;
113
- _this.codeMessage = codeMessage;
114
- (_a = _this.logger) === null || _a === void 0 ? void 0 : _a.comm('Sending code...', _this.logSuffix);
115
- _this.publish(codeMessage);
116
- };
117
- var apiURL = options.apiURL, channel = options.channel, verbose = options.verbose;
118
- this.channel = channel !== null && channel !== void 0 ? channel : '';
119
- this.logger = verbose ? Logger_1.createLogger(true) : undefined;
120
- this.logSuffix = options.name ? " (" + options.name + ")" : '';
121
- if (channel) {
122
- this.codeMessageBuilder = new CodeMessageBuilder_1.default({
123
- verifyCodeMessageSize: this.onPubNubVerifyCodeMessageSize,
124
- callback: this.onCodeMessageReady,
125
- apiURL: apiURL,
126
- logger: this.logger,
127
- maxDiffPlaceholder: 'X'.repeat(32768),
128
- });
129
- }
130
- else {
131
- this.codeMessageBuilder = new CodeMessageBuilder_1.default({
132
- verifyCodeMessageSize: function () { return true; },
133
- callback: this.onCodeMessageReady,
134
- logger: this.logger,
135
- });
136
- }
137
- }
138
- Transport.prototype.addEventListener = function (_type, callback) {
139
- this.callback = callback;
140
- };
141
- Transport.prototype.removeEventListener = function (_type, _callback) {
142
- this.callback = undefined;
143
- };
144
- Transport.prototype.postMessage = function (message) {
145
- switch (message.type) {
146
- case 'start':
147
- this.start();
148
- break;
149
- case 'stop':
150
- this.stop();
151
- break;
152
- case 'update_code':
153
- this.codeMessageBuilder.setCode(message.data);
154
- break;
155
- case 'protocol_message':
156
- this.publish(message.data);
157
- break;
158
- case 'synthetic_event':
159
- this.onSyntheticEvent(message.data);
160
- break;
161
- }
162
- };
163
- Transport.prototype.start = function () {
164
- var _a;
165
- this.stop();
166
- if (this.channel) {
167
- (_a = this.logger) === null || _a === void 0 ? void 0 : _a.comm('Starting channel...', this.channel, this.logSuffix);
168
- this.pubNub = new pubnub_1.default({
169
- publishKey: 'pub-c-2a7fd67b-333d-40db-ad2d-3255f8835f70',
170
- subscribeKey: 'sub-c-0b655000-d784-11e6-b950-02ee2ddab7fe',
171
- ssl: true,
172
- // uuid: TODO
173
- presenceTimeout: 600,
174
- heartbeatInterval: 60,
175
- });
176
- this.pubNub.addListener({
177
- presence: this.onPubNubPresence,
178
- message: this.onPubNubMessage,
179
- status: this.onPubNubStatus,
180
- });
181
- this.pubNub.subscribe({
182
- channels: [this.channel],
183
- withPresence: true,
184
- });
185
- }
186
- };
187
- Transport.prototype.stop = function () {
188
- var _a;
189
- if (this.pubNub) {
190
- (_a = this.logger) === null || _a === void 0 ? void 0 : _a.comm('Stopping...', this.logSuffix);
191
- this.pubNub.removeListener({
192
- presence: this.onPubNubPresence,
193
- message: this.onPubNubMessage,
194
- status: this.onPubNubStatus,
195
- });
196
- this.pubNub.unsubscribe({
197
- channels: [this.channel],
198
- });
199
- this.pubNub.stop();
200
- this.pubNub = undefined;
201
- }
202
- };
203
- Transport.prototype.onProtocolMessage = function (connectionId, message) {
204
- var _a, _b;
205
- if (message.type === 'RESEND_CODE') {
206
- if (this.codeMessage) {
207
- (_a = this.logger) === null || _a === void 0 ? void 0 : _a.comm('Resending code...', this.logSuffix);
208
- this.publish(this.codeMessage);
209
- }
210
- }
211
- else {
212
- (_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, {
213
- type: 'protocol_message',
214
- connectionId: connectionId,
215
- data: message,
216
- });
217
- }
218
- };
219
- Transport.prototype.publish = function (message) {
220
- var _a, _b, _c;
221
- return __awaiter(this, void 0, void 0, function () {
222
- var e_1;
223
- return __generator(this, function (_d) {
224
- switch (_d.label) {
225
- case 0:
226
- if (!this.connectionsCount) {
227
- return [2 /*return*/];
228
- }
229
- if (!this.pubNub) return [3 /*break*/, 5];
230
- _d.label = 1;
231
- case 1:
232
- _d.trys.push([1, 3, , 4]);
233
- return [4 /*yield*/, this.pubNub.publish({
234
- channel: this.channel,
235
- message: message,
236
- })];
237
- case 2:
238
- _d.sent();
239
- return [3 /*break*/, 4];
240
- case 3:
241
- e_1 = _d.sent();
242
- (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error('Failed to publish message', message.type, e_1, this.logSuffix);
243
- return [3 /*break*/, 4];
244
- case 4: return [3 /*break*/, 6];
245
- case 5:
246
- (_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, {
247
- type: 'synthetic_event',
248
- data: message,
249
- });
250
- _d.label = 6;
251
- case 6:
252
- (_c = this.callback) === null || _c === void 0 ? void 0 : _c.call(this, {
253
- type: 'send_message',
254
- data: message,
255
- });
256
- return [2 /*return*/];
257
- }
258
- });
259
- });
260
- };
261
- Transport.prototype.onSyntheticEvent = function (data) {
262
- var _a, _b, _c;
263
- if (typeof data !== 'string') {
264
- return;
265
- }
266
- try {
267
- var message = JSON.parse(data);
268
- switch (message.type) {
269
- case 'CONNECT':
270
- this.connectionsCount++;
271
- (_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, {
272
- type: 'connect',
273
- connectionId: JSON.stringify(message.device),
274
- data: message.device,
275
- });
276
- break;
277
- case 'DISCONNECT':
278
- this.connectionsCount = Math.max(this.connectionsCount - 1, 0);
279
- (_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, {
280
- type: 'disconnect',
281
- connectionId: JSON.stringify(message.device),
282
- data: {
283
- timedout: false,
284
- },
285
- });
286
- break;
287
- case 'MESSAGE':
288
- this.onProtocolMessage(JSON.stringify(message.message.device), message.message);
289
- break;
290
- }
291
- }
292
- catch (e) {
293
- (_c = this.logger) === null || _c === void 0 ? void 0 : _c.error('Failed to parse postMessage', e, data, this.logSuffix);
294
- }
295
- };
296
- return Transport;
297
- }());
298
- exports.default = Transport;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.calcPubNubCodeMessageSize = void 0;
43
+ var pubnub_1 = __importDefault(require("pubnub"));
44
+ var Logger_1 = require("../Logger");
45
+ var CodeMessageBuilder_1 = __importDefault(require("./CodeMessageBuilder"));
46
+ function calcPubNubCodeMessageSize(channel, codeMessage) {
47
+ // https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-message-payload-size-before-publishing
48
+ return (encodeURIComponent(channel +
49
+ JSON.stringify(codeMessage).replace(/[!~*'()]/g, function (x) { return "%" + x.charCodeAt(0).toString(16).toUpperCase(); })).length + 200);
50
+ }
51
+ exports.calcPubNubCodeMessageSize = calcPubNubCodeMessageSize;
52
+ var Transport = /** @class */ (function () {
53
+ function Transport(options) {
54
+ var _this = this;
55
+ this.connectionsCount = 0;
56
+ this.onPubNubPresence = function (event) {
57
+ var _a, _b, _c;
58
+ switch (event.action) {
59
+ case 'join':
60
+ try {
61
+ var device = JSON.parse(event.uuid);
62
+ _this.connectionsCount++;
63
+ (_a = _this.callback) === null || _a === void 0 ? void 0 : _a.call(_this, {
64
+ type: 'connect',
65
+ connectionId: event.uuid,
66
+ data: device,
67
+ });
68
+ if (_this.codeMessage) {
69
+ (_b = _this.logger) === null || _b === void 0 ? void 0 : _b.comm('Sending code...', _this.logSuffix);
70
+ _this.publish(_this.codeMessage);
71
+ }
72
+ }
73
+ catch (e) {
74
+ // Wasn't from the device
75
+ }
76
+ break;
77
+ case 'timeout':
78
+ case 'leave':
79
+ _this.connectionsCount = Math.max(_this.connectionsCount - 1, 0);
80
+ (_c = _this.callback) === null || _c === void 0 ? void 0 : _c.call(_this, {
81
+ type: 'disconnect',
82
+ connectionId: event.uuid,
83
+ data: {
84
+ timedout: event.action === 'timeout',
85
+ },
86
+ });
87
+ break;
88
+ }
89
+ };
90
+ this.onPubNubMessage = function (event) {
91
+ var message = event.message;
92
+ _this.onProtocolMessage(event.publisher, message);
93
+ };
94
+ this.onPubNubStatus = function (_event) {
95
+ // TODO: Do something here?
96
+ };
97
+ this.onPubNubVerifyCodeMessageSize = function (codeMessage) {
98
+ // Calculate unencoded size (quickly) and if that exceeds the limit
99
+ // then don't bother calculating the exact size (which is more expensive)
100
+ var approxSize = 0;
101
+ for (var path in codeMessage.diff) {
102
+ approxSize += path.length + codeMessage.diff[path].length;
103
+ }
104
+ if (approxSize >= 32768) {
105
+ return false;
106
+ }
107
+ // Calculate exact size and check whether it exceeds the limit
108
+ var size = calcPubNubCodeMessageSize(_this.channel, codeMessage);
109
+ return size < 32768;
110
+ };
111
+ this.onCodeMessageReady = function (codeMessage) {
112
+ var _a;
113
+ _this.codeMessage = codeMessage;
114
+ (_a = _this.logger) === null || _a === void 0 ? void 0 : _a.comm('Sending code...', _this.logSuffix);
115
+ _this.publish(codeMessage);
116
+ };
117
+ var apiURL = options.apiURL, channel = options.channel, verbose = options.verbose;
118
+ this.channel = channel !== null && channel !== void 0 ? channel : '';
119
+ this.logger = verbose ? Logger_1.createLogger(true) : undefined;
120
+ this.logSuffix = options.name ? " (" + options.name + ")" : '';
121
+ if (channel) {
122
+ this.codeMessageBuilder = new CodeMessageBuilder_1.default({
123
+ verifyCodeMessageSize: this.onPubNubVerifyCodeMessageSize,
124
+ callback: this.onCodeMessageReady,
125
+ apiURL: apiURL,
126
+ logger: this.logger,
127
+ maxDiffPlaceholder: 'X'.repeat(32768),
128
+ });
129
+ }
130
+ else {
131
+ this.codeMessageBuilder = new CodeMessageBuilder_1.default({
132
+ verifyCodeMessageSize: function () { return true; },
133
+ callback: this.onCodeMessageReady,
134
+ logger: this.logger,
135
+ });
136
+ }
137
+ }
138
+ Transport.prototype.addEventListener = function (_type, callback) {
139
+ this.callback = callback;
140
+ };
141
+ Transport.prototype.removeEventListener = function (_type, _callback) {
142
+ this.callback = undefined;
143
+ };
144
+ Transport.prototype.postMessage = function (message) {
145
+ switch (message.type) {
146
+ case 'start':
147
+ this.start();
148
+ break;
149
+ case 'stop':
150
+ this.stop();
151
+ break;
152
+ case 'update_code':
153
+ this.codeMessageBuilder.setCode(message.data);
154
+ break;
155
+ case 'protocol_message':
156
+ this.publish(message.data);
157
+ break;
158
+ case 'synthetic_event':
159
+ this.onSyntheticEvent(message.data);
160
+ break;
161
+ }
162
+ };
163
+ Transport.prototype.start = function () {
164
+ var _a;
165
+ this.stop();
166
+ if (!this.pubNubClientId) {
167
+ // Keep track of the client ID per transport instance.
168
+ // See: https://support.pubnub.com/hc/en-us/articles/360051496532-How-do-I-set-the-UUID-
169
+ this.pubNubClientId = pubnub_1.default.generateUUID();
170
+ }
171
+ if (this.channel) {
172
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.comm('Starting channel...', this.channel, this.logSuffix);
173
+ this.pubNub = new pubnub_1.default({
174
+ publishKey: 'pub-c-2a7fd67b-333d-40db-ad2d-3255f8835f70',
175
+ subscribeKey: 'sub-c-0b655000-d784-11e6-b950-02ee2ddab7fe',
176
+ uuid: this.pubNubClientId,
177
+ ssl: true,
178
+ presenceTimeout: 600,
179
+ heartbeatInterval: 60,
180
+ });
181
+ this.pubNub.addListener({
182
+ presence: this.onPubNubPresence,
183
+ message: this.onPubNubMessage,
184
+ status: this.onPubNubStatus,
185
+ });
186
+ this.pubNub.subscribe({
187
+ channels: [this.channel],
188
+ withPresence: true,
189
+ });
190
+ }
191
+ };
192
+ Transport.prototype.stop = function () {
193
+ var _a;
194
+ if (this.pubNub) {
195
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.comm('Stopping...', this.logSuffix);
196
+ this.pubNub.removeListener({
197
+ presence: this.onPubNubPresence,
198
+ message: this.onPubNubMessage,
199
+ status: this.onPubNubStatus,
200
+ });
201
+ this.pubNub.unsubscribe({
202
+ channels: [this.channel],
203
+ });
204
+ this.pubNub.stop();
205
+ this.pubNub = undefined;
206
+ }
207
+ };
208
+ Transport.prototype.onProtocolMessage = function (connectionId, message) {
209
+ var _a, _b;
210
+ if (message.type === 'RESEND_CODE') {
211
+ if (this.codeMessage) {
212
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.comm('Resending code...', this.logSuffix);
213
+ this.publish(this.codeMessage);
214
+ }
215
+ }
216
+ else {
217
+ (_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, {
218
+ type: 'protocol_message',
219
+ connectionId: connectionId,
220
+ data: message,
221
+ });
222
+ }
223
+ };
224
+ Transport.prototype.publish = function (message) {
225
+ var _a, _b, _c;
226
+ return __awaiter(this, void 0, void 0, function () {
227
+ var e_1;
228
+ return __generator(this, function (_d) {
229
+ switch (_d.label) {
230
+ case 0:
231
+ if (!this.connectionsCount) {
232
+ return [2 /*return*/];
233
+ }
234
+ if (!this.pubNub) return [3 /*break*/, 5];
235
+ _d.label = 1;
236
+ case 1:
237
+ _d.trys.push([1, 3, , 4]);
238
+ return [4 /*yield*/, this.pubNub.publish({
239
+ channel: this.channel,
240
+ message: message,
241
+ })];
242
+ case 2:
243
+ _d.sent();
244
+ return [3 /*break*/, 4];
245
+ case 3:
246
+ e_1 = _d.sent();
247
+ (_a = this.logger) === null || _a === void 0 ? void 0 : _a.error('Failed to publish message', message.type, e_1, this.logSuffix);
248
+ return [3 /*break*/, 4];
249
+ case 4: return [3 /*break*/, 6];
250
+ case 5:
251
+ (_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, {
252
+ type: 'synthetic_event',
253
+ data: message,
254
+ });
255
+ _d.label = 6;
256
+ case 6:
257
+ (_c = this.callback) === null || _c === void 0 ? void 0 : _c.call(this, {
258
+ type: 'send_message',
259
+ data: message,
260
+ });
261
+ return [2 /*return*/];
262
+ }
263
+ });
264
+ });
265
+ };
266
+ Transport.prototype.onSyntheticEvent = function (data) {
267
+ var _a, _b, _c;
268
+ if (typeof data !== 'string') {
269
+ return;
270
+ }
271
+ try {
272
+ var message = JSON.parse(data);
273
+ switch (message.type) {
274
+ case 'CONNECT':
275
+ this.connectionsCount++;
276
+ (_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, {
277
+ type: 'connect',
278
+ connectionId: JSON.stringify(message.device),
279
+ data: message.device,
280
+ });
281
+ break;
282
+ case 'DISCONNECT':
283
+ this.connectionsCount = Math.max(this.connectionsCount - 1, 0);
284
+ (_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, {
285
+ type: 'disconnect',
286
+ connectionId: JSON.stringify(message.device),
287
+ data: {
288
+ timedout: false,
289
+ },
290
+ });
291
+ break;
292
+ case 'MESSAGE':
293
+ this.onProtocolMessage(JSON.stringify(message.message.device), message.message);
294
+ break;
295
+ }
296
+ }
297
+ catch (e) {
298
+ (_c = this.logger) === null || _c === void 0 ? void 0 : _c.error('Failed to parse postMessage', e, data, this.logSuffix);
299
+ }
300
+ };
301
+ return Transport;
302
+ }());
303
+ exports.default = Transport;
299
304
  //# sourceMappingURL=Transport.js.map