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,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.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
});
|
|
181
|
-
this.pubNub.
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
this.pubNub.
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
case
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|