snack-sdk 3.7.0 → 3.8.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 +1 -1
- package/build/DependencyResolver.d.ts +26 -25
- package/build/DependencyResolver.js +258 -257
- package/build/DependencyResolver.js.map +1 -1
- package/build/DevSession.d.ts +20 -20
- package/build/DevSession.js +203 -203
- package/build/FileUploader.d.ts +23 -22
- package/build/FileUploader.js +174 -174
- package/build/FileUploader.js.map +1 -1
- package/build/Logger.d.ts +11 -11
- package/build/Logger.js +33 -33
- package/build/Session.d.ts +301 -300
- package/build/Session.js +1171 -1171
- package/build/Session.js.map +1 -1
- package/build/State.d.ts +16 -16
- package/build/State.js +90 -90
- package/build/State.js.map +1 -1
- package/build/WantedVersions.d.ts +17 -16
- package/build/WantedVersions.js +93 -96
- package/build/WantedVersions.js.map +1 -1
- package/build/defaultConfig.d.ts +12 -13
- package/build/defaultConfig.js +29 -29
- package/build/defaultConfig.js.map +1 -1
- package/build/index.d.ts +7 -7
- package/build/index.js +30 -30
- package/build/index.js.map +1 -1
- package/build/transports/CodeMessageBuilder.d.ts +31 -31
- package/build/transports/CodeMessageBuilder.js +196 -196
- package/build/transports/CodeMessageBuilder.js.map +1 -1
- package/build/transports/Protocol.d.ts +64 -64
- package/build/transports/Protocol.js +2 -2
- package/build/transports/Protocol.js.map +1 -1
- package/build/transports/Transport.d.ts +27 -27
- package/build/transports/Transport.js +298 -298
- 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/types.js.map +1 -1
- package/build/transports/webPlayer.d.ts +12 -11
- package/build/transports/webPlayer.js +70 -70
- package/build/transports/webPlayer.js.map +1 -1
- package/build/types.d.ts +185 -258
- package/build/types.js +2 -2
- package/build/types.js.map +1 -1
- package/build/utils.d.ts +17 -16
- package/build/utils.js +68 -68
- package/build/utils.js.map +1 -1
- package/package.json +3 -2
- package/src/DependencyResolver.ts +11 -7
- package/src/FileUploader.ts +3 -1
- package/src/Session.ts +9 -6
- package/src/State.ts +1 -1
- package/src/WantedVersions.ts +3 -2
- package/src/defaultConfig.ts +5 -5
- package/src/index.ts +37 -9
- package/src/transports/CodeMessageBuilder.ts +7 -2
- package/src/transports/Protocol.ts +1 -1
- package/src/transports/types.ts +2 -1
- package/src/transports/webPlayer.ts +3 -1
- package/src/types.ts +8 -82
- package/src/utils.ts +2 -1
- package/build/sdk.d.ts +0 -34
- package/build/sdk.js +0 -139
- package/build/sdk.js.map +0 -1
- package/build/sdks/features.d.ts +0 -9
- package/build/sdks/features.js +0 -13
- package/build/sdks/features.js.map +0 -1
- package/build/sdks/index.d.ts +0 -5
- package/build/sdks/index.js +0 -124
- package/build/sdks/index.js.map +0 -1
- package/build/sdks/types.d.ts +0 -21
- package/build/sdks/types.js +0 -3
- package/build/sdks/types.js.map +0 -1
- package/src/sdk.ts +0 -135
- package/src/sdks/features.ts +0 -11
- package/src/sdks/index.ts +0 -159
- package/src/sdks/types.ts +0 -49
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { ProtocolCodeMessage } from './Protocol';
|
|
2
|
-
import { SnackTransport, SnackTransportListener, SnackTransportMessage, SnackTransportOptions } from './types';
|
|
3
|
-
export declare function calcPubNubCodeMessageSize(channel: string, codeMessage: ProtocolCodeMessage): number;
|
|
4
|
-
export default class Transport implements SnackTransport {
|
|
5
|
-
private readonly channel;
|
|
6
|
-
private readonly logger?;
|
|
7
|
-
private callback?;
|
|
8
|
-
private pubNub?;
|
|
9
|
-
private codeMessageBuilder;
|
|
10
|
-
private codeMessage?;
|
|
11
|
-
private connectionsCount;
|
|
12
|
-
private readonly logSuffix;
|
|
13
|
-
constructor(options: SnackTransportOptions);
|
|
14
|
-
addEventListener(_type: 'message', callback: SnackTransportListener): void;
|
|
15
|
-
removeEventListener(_type: 'message', _callback: SnackTransportListener): void;
|
|
16
|
-
postMessage(message: SnackTransportMessage): void;
|
|
17
|
-
private start;
|
|
18
|
-
private stop;
|
|
19
|
-
private onProtocolMessage;
|
|
20
|
-
private publish;
|
|
21
|
-
private onPubNubPresence;
|
|
22
|
-
private onPubNubMessage;
|
|
23
|
-
private onPubNubStatus;
|
|
24
|
-
private onPubNubVerifyCodeMessageSize;
|
|
25
|
-
private onSyntheticEvent;
|
|
26
|
-
private onCodeMessageReady;
|
|
27
|
-
}
|
|
1
|
+
import { ProtocolCodeMessage } from './Protocol';
|
|
2
|
+
import { SnackTransport, SnackTransportListener, SnackTransportMessage, SnackTransportOptions } from './types';
|
|
3
|
+
export declare function calcPubNubCodeMessageSize(channel: string, codeMessage: ProtocolCodeMessage): number;
|
|
4
|
+
export default class Transport implements SnackTransport {
|
|
5
|
+
private readonly channel;
|
|
6
|
+
private readonly logger?;
|
|
7
|
+
private callback?;
|
|
8
|
+
private pubNub?;
|
|
9
|
+
private codeMessageBuilder;
|
|
10
|
+
private codeMessage?;
|
|
11
|
+
private connectionsCount;
|
|
12
|
+
private readonly logSuffix;
|
|
13
|
+
constructor(options: SnackTransportOptions);
|
|
14
|
+
addEventListener(_type: 'message', callback: SnackTransportListener): void;
|
|
15
|
+
removeEventListener(_type: 'message', _callback: SnackTransportListener): void;
|
|
16
|
+
postMessage(message: SnackTransportMessage): void;
|
|
17
|
+
private start;
|
|
18
|
+
private stop;
|
|
19
|
+
private onProtocolMessage;
|
|
20
|
+
private publish;
|
|
21
|
+
private onPubNubPresence;
|
|
22
|
+
private onPubNubMessage;
|
|
23
|
+
private onPubNubStatus;
|
|
24
|
+
private onPubNubVerifyCodeMessageSize;
|
|
25
|
+
private onSyntheticEvent;
|
|
26
|
+
private onCodeMessageReady;
|
|
27
|
+
}
|
|
@@ -1,299 +1,299 @@
|
|
|
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.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;
|
|
299
299
|
//# sourceMappingURL=Transport.js.map
|