epicenter-libs 3.33.0 → 3.34.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 +17 -0
- package/dist/browser/epicenter.js +105 -2
- package/dist/browser/epicenter.js.map +1 -1
- package/dist/cjs/epicenter.js +105 -2
- package/dist/cjs/epicenter.js.map +1 -1
- package/dist/epicenter.js +105 -2
- package/dist/epicenter.js.map +1 -1
- package/dist/epicenter.min.js +1 -1
- package/dist/epicenter.min.js.map +1 -1
- package/dist/module/epicenter.js +105 -2
- package/dist/module/epicenter.js.map +1 -1
- package/dist/types/adapters/chat.d.ts +63 -0
- package/package.json +1 -1
- package/src/adapters/chat.ts +95 -0
- package/src/utils/identification.ts +4 -0
- package/dist/test/_virtual/runtime.js +0 -3
- package/dist/test/_virtual/runtime2.js +0 -3
- package/dist/test/adapters/account.js +0 -169
- package/dist/test/adapters/admin.js +0 -53
- package/dist/test/adapters/asset.js +0 -425
- package/dist/test/adapters/authentication.js +0 -349
- package/dist/test/adapters/channel.js +0 -201
- package/dist/test/adapters/chat.js +0 -199
- package/dist/test/adapters/cometd.js +0 -514
- package/dist/test/adapters/consensus.js +0 -484
- package/dist/test/adapters/daily.js +0 -244
- package/dist/test/adapters/email.js +0 -179
- package/dist/test/adapters/episode.js +0 -175
- package/dist/test/adapters/group.js +0 -655
- package/dist/test/adapters/leaderboard.js +0 -162
- package/dist/test/adapters/matchmaker.js +0 -159
- package/dist/test/adapters/presence.js +0 -85
- package/dist/test/adapters/project.js +0 -94
- package/dist/test/adapters/recaptcha.js +0 -24
- package/dist/test/adapters/run.js +0 -1008
- package/dist/test/adapters/somebody.js +0 -154
- package/dist/test/adapters/task.js +0 -192
- package/dist/test/adapters/time.js +0 -28
- package/dist/test/adapters/user.js +0 -105
- package/dist/test/adapters/vault.js +0 -403
- package/dist/test/adapters/video.js +0 -249
- package/dist/test/adapters/vonage.js +0 -130
- package/dist/test/adapters/wallet.js +0 -130
- package/dist/test/adapters/world.js +0 -507
- package/dist/test/apis/video.js +0 -294
- package/dist/test/apis/vonage.js +0 -121
- package/dist/test/epicenter.js +0 -121
- package/dist/test/node_modules/@babel/runtime/helpers/esm/defineProperty.js +0 -12
- package/dist/test/node_modules/@babel/runtime/helpers/esm/toPrimitive.js +0 -14
- package/dist/test/node_modules/@babel/runtime/helpers/esm/toPropertyKey.js +0 -9
- package/dist/test/node_modules/@babel/runtime/helpers/esm/typeof.js +0 -11
- package/dist/test/node_modules/cometd/AckExtension.js +0 -91
- package/dist/test/node_modules/cometd/CallbackPollingTransport.js +0 -148
- package/dist/test/node_modules/cometd/Client.js +0 -2160
- package/dist/test/node_modules/cometd/Extension.js +0 -47
- package/dist/test/node_modules/cometd/LongPollingTransport.js +0 -135
- package/dist/test/node_modules/cometd/ReloadExtension.js +0 -214
- package/dist/test/node_modules/cometd/RequestTransport.js +0 -252
- package/dist/test/node_modules/cometd/Transport.js +0 -167
- package/dist/test/node_modules/cometd/TransportRegistry.js +0 -107
- package/dist/test/node_modules/cometd/WebSocketTransport.js +0 -384
- package/dist/test/node_modules/cometd/cometd.js +0 -9
- package/dist/test/node_modules/regenerator-runtime/runtime.js +0 -773
- package/dist/test/utilities.js +0 -17
- package/dist/test/utils/config.js +0 -189
- package/dist/test/utils/constants.js +0 -293
- package/dist/test/utils/cookies.js +0 -75
- package/dist/test/utils/error-manager.js +0 -57
- package/dist/test/utils/error.js +0 -12
- package/dist/test/utils/fault.js +0 -34
- package/dist/test/utils/filter-parser.js +0 -68
- package/dist/test/utils/helpers.js +0 -9
- package/dist/test/utils/identification.js +0 -161
- package/dist/test/utils/result.js +0 -20
- package/dist/test/utils/router.js +0 -551
- package/dist/test/utils/store.js +0 -79
|
@@ -1,514 +0,0 @@
|
|
|
1
|
-
import _defineProperty from '../node_modules/@babel/runtime/helpers/esm/defineProperty.js';
|
|
2
|
-
import { isBrowser } from '../utils/helpers.js';
|
|
3
|
-
import EpicenterError from '../utils/error.js';
|
|
4
|
-
import Fault from '../utils/fault.js';
|
|
5
|
-
import config from '../utils/config.js';
|
|
6
|
-
import identification from '../utils/identification.js';
|
|
7
|
-
import errorManager from '../utils/error-manager.js';
|
|
8
|
-
import 'cross-fetch';
|
|
9
|
-
import { get } from './project.js';
|
|
10
|
-
|
|
11
|
-
const AUTH_TOKEN_KEY = 'com.forio.epicenter.token';
|
|
12
|
-
const MAX_RETRIES = 3;
|
|
13
|
-
const IDLE = 'idle';
|
|
14
|
-
const FAILED = 'failed';
|
|
15
|
-
const CONNECTED = 'connected';
|
|
16
|
-
const SUCCEEDED = 'succeeded';
|
|
17
|
-
const CONNECTING = 'connecting';
|
|
18
|
-
const HANDSHAKING = 'handshaking';
|
|
19
|
-
const DISCONNECTED = 'disconnected';
|
|
20
|
-
const FORBIDDEN = 403;
|
|
21
|
-
const CONNECT_META_CHANNEL = '/meta/connect';
|
|
22
|
-
const DISCONNECT_META_CHANNEL = '/meta/disconnect';
|
|
23
|
-
const HANDSHAKE_META_CHANNEL = '/meta/handshake';
|
|
24
|
-
const COMETD_RECONNECTED = 'COMETD_RECONNECTED';
|
|
25
|
-
const DEFAULT_CHANNEL_PROTOCOL = 'cometd';
|
|
26
|
-
// error messages that indicate session invalidation as
|
|
27
|
-
// described in cometd documentation and oumuamua source code:
|
|
28
|
-
// https://docs.cometd.org/current/reference/#_javascript_disconnect
|
|
29
|
-
// https://github.com/zenbones/SmallMind/blob/main/bayeux/oumuamua/server/spi/src/main/java/org/smallmind/bayeux/oumuamua/server/spi/MetaProcessingException.java
|
|
30
|
-
|
|
31
|
-
const SESSION_INVALIDATION_ERRORS = ['402', 'session_unknown', 'invalid client id', 'missing client id', 'session has been disconnected', 'invalid session type', 'handshake_denied', 'handshake_failed'];
|
|
32
|
-
function isSessionInvalidationError(errorMessage) {
|
|
33
|
-
const lowerError = errorMessage.toLowerCase();
|
|
34
|
-
return SESSION_INVALIDATION_ERRORS.some(pattern => {
|
|
35
|
-
return lowerError.includes(pattern);
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
function isIllegalStateError(error) {
|
|
39
|
-
return error instanceof Error && error.message.toLowerCase().includes('illegal state');
|
|
40
|
-
}
|
|
41
|
-
function shouldRehandshake(reply) {
|
|
42
|
-
var _reply$advice, _reply$error;
|
|
43
|
-
const advice = (_reply$advice = reply.advice) === null || _reply$advice === void 0 ? void 0 : _reply$advice.reconnect;
|
|
44
|
-
if (advice === 'handshake') {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
const errorMessage = (_reply$error = reply.error) !== null && _reply$error !== void 0 ? _reply$error : '';
|
|
48
|
-
return isSessionInvalidationError(errorMessage);
|
|
49
|
-
}
|
|
50
|
-
let cometdInstance;
|
|
51
|
-
class CometdAdapter {
|
|
52
|
-
constructor() {
|
|
53
|
-
_defineProperty(this, "url", '');
|
|
54
|
-
_defineProperty(this, "initialization", undefined);
|
|
55
|
-
_defineProperty(this, "handshakePromise", undefined);
|
|
56
|
-
_defineProperty(this, "subscriptions", new Map());
|
|
57
|
-
_defineProperty(this, "isConnected", false);
|
|
58
|
-
_defineProperty(this, "handshakeState", IDLE);
|
|
59
|
-
_defineProperty(this, "pendingOperations", []);
|
|
60
|
-
_defineProperty(this, "processingQueue", false);
|
|
61
|
-
}
|
|
62
|
-
get cometd() {
|
|
63
|
-
if (!cometdInstance) {
|
|
64
|
-
throw new EpicenterError('Tried to get non-existent cometd');
|
|
65
|
-
}
|
|
66
|
-
return cometdInstance;
|
|
67
|
-
}
|
|
68
|
-
set cometd(instance) {
|
|
69
|
-
cometdInstance = instance;
|
|
70
|
-
}
|
|
71
|
-
async startup(options = {
|
|
72
|
-
logLevel: 'warn'
|
|
73
|
-
}) {
|
|
74
|
-
var _project$channelProto;
|
|
75
|
-
const project = await get();
|
|
76
|
-
if (!project.channelEnabled) throw new EpicenterError('Push Channels are not enabled on this project');
|
|
77
|
-
const channelProtocol = ((_project$channelProto = project.channelProtocol) === null || _project$channelProto === void 0 ? void 0 : _project$channelProto.toLowerCase()) || DEFAULT_CHANNEL_PROTOCOL;
|
|
78
|
-
const {
|
|
79
|
-
CometD
|
|
80
|
-
} = await import('../node_modules/cometd/cometd.js');
|
|
81
|
-
const {
|
|
82
|
-
AckExtension
|
|
83
|
-
} = await import('../node_modules/cometd/cometd.js');
|
|
84
|
-
const {
|
|
85
|
-
ReloadExtension
|
|
86
|
-
} = await import('../node_modules/cometd/cometd.js');
|
|
87
|
-
this.cometd = new CometD();
|
|
88
|
-
const {
|
|
89
|
-
apiProtocol,
|
|
90
|
-
apiHost,
|
|
91
|
-
apiVersion,
|
|
92
|
-
accountShortName,
|
|
93
|
-
projectShortName
|
|
94
|
-
} = config;
|
|
95
|
-
const accountProject = accountShortName && projectShortName ? `/${accountShortName}/${projectShortName}` : '/epicenter/manager';
|
|
96
|
-
this.url = `${apiProtocol}://${apiHost}/push/v${apiVersion}${accountProject}/${channelProtocol}`;
|
|
97
|
-
this.cometd.registerExtension('ack', new AckExtension());
|
|
98
|
-
if (isBrowser()) {
|
|
99
|
-
this.cometd.registerExtension('reload', new ReloadExtension());
|
|
100
|
-
window.onunload = () => {
|
|
101
|
-
if (this.cometd.getStatus() === CONNECTED) {
|
|
102
|
-
if (this.cometd.reload) this.cometd.reload();
|
|
103
|
-
const transport = this.cometd.getTransport();
|
|
104
|
-
if (transport) transport.abort();
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
this.cometd.configure({
|
|
109
|
-
url: this.url,
|
|
110
|
-
logLevel: options.logLevel
|
|
111
|
-
});
|
|
112
|
-
return true;
|
|
113
|
-
}
|
|
114
|
-
listenToMetaChannels() {
|
|
115
|
-
this.cometd.addListener(HANDSHAKE_META_CHANNEL, message => {
|
|
116
|
-
if (message.successful) {
|
|
117
|
-
this.handshakeState = SUCCEEDED;
|
|
118
|
-
this.processPendingOperations();
|
|
119
|
-
} else {
|
|
120
|
-
this.handshakeState = FAILED;
|
|
121
|
-
this.handleHandshakeFailure(message);
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
const connectListener = new Promise((resolve, reject) => {
|
|
125
|
-
this.cometd.addListener(CONNECT_META_CHANNEL, message => {
|
|
126
|
-
if (this.cometd.isDisconnected()) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
const wasConnected = this.isConnected;
|
|
130
|
-
this.isConnected = message.successful || false;
|
|
131
|
-
if (!wasConnected && this.isConnected) {
|
|
132
|
-
const error = new Fault({
|
|
133
|
-
status: undefined,
|
|
134
|
-
message: 'Reconnected to CometD',
|
|
135
|
-
information: {
|
|
136
|
-
code: COMETD_RECONNECTED
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
const retry = () => Promise.resolve();
|
|
140
|
-
try {
|
|
141
|
-
const result = errorManager.handle(error, retry);
|
|
142
|
-
resolve(result);
|
|
143
|
-
} catch (e) {
|
|
144
|
-
reject(e);
|
|
145
|
-
}
|
|
146
|
-
this.processPendingOperations();
|
|
147
|
-
} else if (wasConnected && !this.isConnected) {
|
|
148
|
-
this.handshakeState = IDLE;
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
const disconnectListener = new Promise(resolve => {
|
|
153
|
-
this.cometd.addListener(DISCONNECT_META_CHANNEL, message => {
|
|
154
|
-
if (message.successful) {
|
|
155
|
-
this.isConnected = false;
|
|
156
|
-
this.handshakeState = IDLE;
|
|
157
|
-
}
|
|
158
|
-
resolve(message);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
return Promise.all([connectListener, disconnectListener]);
|
|
162
|
-
}
|
|
163
|
-
handleHandshakeFailure(message) {
|
|
164
|
-
if (shouldRehandshake(message)) {
|
|
165
|
-
this.handshakeState = IDLE;
|
|
166
|
-
this.handshakePromise = undefined;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
async processPendingOperations() {
|
|
170
|
-
if (this.processingQueue || this.pendingOperations.length === 0) return;
|
|
171
|
-
this.processingQueue = true;
|
|
172
|
-
const operations = [...this.pendingOperations];
|
|
173
|
-
this.pendingOperations = [];
|
|
174
|
-
try {
|
|
175
|
-
await Promise.all(operations.map(op => op().catch(console.error)));
|
|
176
|
-
} finally {
|
|
177
|
-
this.processingQueue = false;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
async init(options) {
|
|
181
|
-
if (!this.initialization) {
|
|
182
|
-
this.initialization = this.startup(options);
|
|
183
|
-
}
|
|
184
|
-
return this.initialization;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Connects to CometD server
|
|
188
|
-
async handshake(options = {}) {
|
|
189
|
-
await this.init();
|
|
190
|
-
|
|
191
|
-
// Prevent concurrent handshake attempts
|
|
192
|
-
if (this.handshakePromise && this.handshakeState === HANDSHAKING) {
|
|
193
|
-
return this.handshakePromise;
|
|
194
|
-
}
|
|
195
|
-
const currentStatus = this.cometd.getStatus();
|
|
196
|
-
|
|
197
|
-
// If already connected and handshake succeeded, return immediately
|
|
198
|
-
if (currentStatus === CONNECTED && this.handshakeState === SUCCEEDED) {
|
|
199
|
-
return Promise.resolve();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// If CometD is connecting or handshaking, don't call handshake again - just wait
|
|
203
|
-
if (currentStatus === CONNECTING || currentStatus === HANDSHAKING) {
|
|
204
|
-
if (this.handshakePromise) {
|
|
205
|
-
return this.handshakePromise;
|
|
206
|
-
}
|
|
207
|
-
// Return a rejected promise to indicate we can't handshake right now
|
|
208
|
-
return Promise.reject(new Error('CometD is already connecting, please wait'));
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Only proceed if disconnected
|
|
212
|
-
if (currentStatus !== DISCONNECTED) {
|
|
213
|
-
return Promise.resolve();
|
|
214
|
-
}
|
|
215
|
-
this.handshakeState = HANDSHAKING;
|
|
216
|
-
const {
|
|
217
|
-
session
|
|
218
|
-
} = identification;
|
|
219
|
-
let handshakeProps = {};
|
|
220
|
-
if (session) {
|
|
221
|
-
handshakeProps = {
|
|
222
|
-
ext: {
|
|
223
|
-
[AUTH_TOKEN_KEY]: session.token,
|
|
224
|
-
ack: true
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
this.cometd.websocketEnabled = true;
|
|
229
|
-
this.handshakePromise = new Promise((resolve, reject) => {
|
|
230
|
-
this.cometd.handshake(handshakeProps, handshakeReply => {
|
|
231
|
-
var _handshakeReply$error;
|
|
232
|
-
if (handshakeReply.successful) {
|
|
233
|
-
this.handshakeState = SUCCEEDED;
|
|
234
|
-
this.listenToMetaChannels();
|
|
235
|
-
resolve(undefined);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
this.handshakeState = FAILED;
|
|
239
|
-
if (shouldRehandshake(handshakeReply)) {
|
|
240
|
-
this.handshakeState = IDLE;
|
|
241
|
-
this.handshakePromise = undefined;
|
|
242
|
-
}
|
|
243
|
-
const errorMessage = (_handshakeReply$error = handshakeReply.error) !== null && _handshakeReply$error !== void 0 ? _handshakeReply$error : '';
|
|
244
|
-
const error = new Fault({
|
|
245
|
-
status: errorMessage.includes('403') ? FORBIDDEN : undefined,
|
|
246
|
-
message: errorMessage,
|
|
247
|
-
information: {
|
|
248
|
-
code: 'COMETD_ERROR',
|
|
249
|
-
...handshakeReply
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
if (options.inert) {
|
|
253
|
-
reject(error);
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
const retry = () => this.handshake({
|
|
257
|
-
inert: true
|
|
258
|
-
});
|
|
259
|
-
try {
|
|
260
|
-
const result = errorManager.handle(error, retry);
|
|
261
|
-
resolve(result);
|
|
262
|
-
} catch (e) {
|
|
263
|
-
reject(e);
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
return this.handshakePromise;
|
|
268
|
-
}
|
|
269
|
-
async disconnect() {
|
|
270
|
-
if (!this.initialization) return Promise.resolve();
|
|
271
|
-
await this.init();
|
|
272
|
-
await this.empty();
|
|
273
|
-
if (this.cometd.getStatus() !== CONNECTED) return Promise.resolve();
|
|
274
|
-
return new Promise((resolve, reject) => this.cometd.disconnect(disconnectReply => {
|
|
275
|
-
var _disconnectReply$erro;
|
|
276
|
-
if (disconnectReply.successful) {
|
|
277
|
-
resolve(undefined);
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
const errorMessage = (_disconnectReply$erro = disconnectReply.error) !== null && _disconnectReply$erro !== void 0 ? _disconnectReply$erro : '';
|
|
281
|
-
if (isSessionInvalidationError(errorMessage)) {
|
|
282
|
-
resolve(undefined);
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
reject(new EpicenterError('Unable to disconnect from CometD server'));
|
|
286
|
-
}));
|
|
287
|
-
}
|
|
288
|
-
async add(channel, update, options = {}) {
|
|
289
|
-
var _this$cometd;
|
|
290
|
-
await this.init();
|
|
291
|
-
const currentStatus = (_this$cometd = this.cometd) === null || _this$cometd === void 0 ? void 0 : _this$cometd.getStatus();
|
|
292
|
-
if (currentStatus !== CONNECTED) {
|
|
293
|
-
try {
|
|
294
|
-
await this.handshake();
|
|
295
|
-
} catch (error) {
|
|
296
|
-
var _errorObj$message;
|
|
297
|
-
const errorObj = error;
|
|
298
|
-
if (errorObj !== null && errorObj !== void 0 && (_errorObj$message = errorObj.message) !== null && _errorObj$message !== void 0 && _errorObj$message.includes('already connecting')) {
|
|
299
|
-
var _this$cometd2;
|
|
300
|
-
// Wait a moment and try again
|
|
301
|
-
const retryDelay = 500;
|
|
302
|
-
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
|
303
|
-
if (((_this$cometd2 = this.cometd) === null || _this$cometd2 === void 0 ? void 0 : _this$cometd2.getStatus()) === CONNECTED) ;
|
|
304
|
-
}
|
|
305
|
-
throw error;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
const {
|
|
309
|
-
session
|
|
310
|
-
} = identification;
|
|
311
|
-
const subscriptionProps = !session ? {} : {
|
|
312
|
-
ext: {
|
|
313
|
-
[AUTH_TOKEN_KEY]: session.token
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
const handleCometdUpdate = message => {
|
|
317
|
-
// TODO -- figure out why there's ambiguity here and try to remove it
|
|
318
|
-
let data = message.data;
|
|
319
|
-
if (typeof data === 'string') {
|
|
320
|
-
try {
|
|
321
|
-
data = JSON.parse(data);
|
|
322
|
-
} catch (_error) {
|
|
323
|
-
// If parsing fails, use raw string
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return update(data);
|
|
327
|
-
};
|
|
328
|
-
return new Promise((resolve, reject) => {
|
|
329
|
-
let subscription;
|
|
330
|
-
try {
|
|
331
|
-
subscription = this.cometd.subscribe(channel.path, handleCometdUpdate, subscriptionProps, subscribeReply => {
|
|
332
|
-
var _subscribeReply$error;
|
|
333
|
-
if (subscribeReply.successful) {
|
|
334
|
-
this.subscriptions.set(channel.path, subscription);
|
|
335
|
-
resolve(subscription);
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
if (shouldRehandshake(subscribeReply)) {
|
|
339
|
-
this.handshakeState = IDLE;
|
|
340
|
-
this.handshakePromise = undefined;
|
|
341
|
-
}
|
|
342
|
-
const errorMessage = (_subscribeReply$error = subscribeReply.error) !== null && _subscribeReply$error !== void 0 ? _subscribeReply$error : '';
|
|
343
|
-
const error = new Fault({
|
|
344
|
-
status: errorMessage.includes('403') ? FORBIDDEN : undefined,
|
|
345
|
-
message: errorMessage,
|
|
346
|
-
information: {
|
|
347
|
-
code: 'COMETD_ERROR',
|
|
348
|
-
...subscribeReply
|
|
349
|
-
}
|
|
350
|
-
});
|
|
351
|
-
if (options.inert) {
|
|
352
|
-
reject(error);
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
const retry = async () => {
|
|
356
|
-
if (shouldRehandshake(subscribeReply)) {
|
|
357
|
-
await this.handshake({
|
|
358
|
-
inert: true
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
return this.add(channel, update, {
|
|
362
|
-
inert: true
|
|
363
|
-
});
|
|
364
|
-
};
|
|
365
|
-
try {
|
|
366
|
-
const result = errorManager.handle(error, retry);
|
|
367
|
-
resolve(result);
|
|
368
|
-
} catch (e) {
|
|
369
|
-
reject(e);
|
|
370
|
-
}
|
|
371
|
-
});
|
|
372
|
-
} catch (error) {
|
|
373
|
-
var _options$_retryCount;
|
|
374
|
-
const retryCount = (_options$_retryCount = options._retryCount) !== null && _options$_retryCount !== void 0 ? _options$_retryCount : 0;
|
|
375
|
-
if (isIllegalStateError(error) && retryCount < MAX_RETRIES) {
|
|
376
|
-
this.handshakeState = IDLE;
|
|
377
|
-
this.handshakePromise = undefined;
|
|
378
|
-
this.handshake().then(() => this.add(channel, update, {
|
|
379
|
-
...options,
|
|
380
|
-
_retryCount: retryCount + 1
|
|
381
|
-
})).then(resolve).catch(reject);
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
reject(error);
|
|
385
|
-
}
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
async publish(channel, content, options = {}) {
|
|
389
|
-
await this.init();
|
|
390
|
-
if (this.cometd.getStatus() !== CONNECTED) {
|
|
391
|
-
await this.handshake();
|
|
392
|
-
}
|
|
393
|
-
const {
|
|
394
|
-
session
|
|
395
|
-
} = identification;
|
|
396
|
-
const publishProps = {
|
|
397
|
-
ext: session ? {
|
|
398
|
-
[AUTH_TOKEN_KEY]: session.token
|
|
399
|
-
} : undefined
|
|
400
|
-
};
|
|
401
|
-
return new Promise((resolve, reject) => {
|
|
402
|
-
try {
|
|
403
|
-
this.cometd.publish(channel.path, content, publishProps, publishReply => {
|
|
404
|
-
var _publishReply$error;
|
|
405
|
-
if (publishReply.successful) {
|
|
406
|
-
resolve(publishReply);
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
if (shouldRehandshake(publishReply)) {
|
|
410
|
-
this.handshakeState = IDLE;
|
|
411
|
-
this.handshakePromise = undefined;
|
|
412
|
-
}
|
|
413
|
-
const errorMessage = (_publishReply$error = publishReply.error) !== null && _publishReply$error !== void 0 ? _publishReply$error : '';
|
|
414
|
-
const error = new Fault({
|
|
415
|
-
status: errorMessage.includes('403') ? FORBIDDEN : undefined,
|
|
416
|
-
message: errorMessage,
|
|
417
|
-
information: {
|
|
418
|
-
code: 'COMETD_ERROR',
|
|
419
|
-
...publishReply
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
if (options.inert) {
|
|
423
|
-
reject(error);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
const retry = async () => {
|
|
427
|
-
if (shouldRehandshake(publishReply)) {
|
|
428
|
-
await this.handshake({
|
|
429
|
-
inert: true
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
return this.publish(channel, content, {
|
|
433
|
-
inert: true
|
|
434
|
-
});
|
|
435
|
-
};
|
|
436
|
-
try {
|
|
437
|
-
const result = errorManager.handle(error, retry);
|
|
438
|
-
resolve(result);
|
|
439
|
-
} catch (e) {
|
|
440
|
-
reject(e);
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
} catch (error) {
|
|
444
|
-
var _options$_retryCount2;
|
|
445
|
-
const retryCount = (_options$_retryCount2 = options._retryCount) !== null && _options$_retryCount2 !== void 0 ? _options$_retryCount2 : 0;
|
|
446
|
-
if (isIllegalStateError(error) && retryCount < MAX_RETRIES) {
|
|
447
|
-
this.handshakeState = IDLE;
|
|
448
|
-
this.handshakePromise = undefined;
|
|
449
|
-
this.handshake().then(() => this.publish(channel, content, {
|
|
450
|
-
...options,
|
|
451
|
-
_retryCount: retryCount + 1
|
|
452
|
-
})).then(resolve).catch(reject);
|
|
453
|
-
return;
|
|
454
|
-
}
|
|
455
|
-
reject(error);
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
async remove(subscription) {
|
|
460
|
-
await this.init();
|
|
461
|
-
|
|
462
|
-
// Find the subscription by iterating through the map
|
|
463
|
-
let channelPath;
|
|
464
|
-
for (const [path, sub] of this.subscriptions.entries()) {
|
|
465
|
-
if (sub === subscription) {
|
|
466
|
-
channelPath = path;
|
|
467
|
-
break;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
if (channelPath) {
|
|
471
|
-
this.subscriptions.delete(channelPath);
|
|
472
|
-
if (this.cometd.getStatus() === DISCONNECTED) return Promise.resolve();
|
|
473
|
-
}
|
|
474
|
-
return new Promise((resolve, reject) => {
|
|
475
|
-
try {
|
|
476
|
-
this.cometd.unsubscribe(subscription, unsubscribeReply => {
|
|
477
|
-
var _unsubscribeReply$err;
|
|
478
|
-
if (unsubscribeReply.successful) {
|
|
479
|
-
resolve(unsubscribeReply);
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
const errorMessage = (_unsubscribeReply$err = unsubscribeReply.error) !== null && _unsubscribeReply$err !== void 0 ? _unsubscribeReply$err : '';
|
|
483
|
-
if (isSessionInvalidationError(errorMessage)) {
|
|
484
|
-
resolve(unsubscribeReply);
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
const error = new Fault({
|
|
488
|
-
status: undefined,
|
|
489
|
-
message: errorMessage,
|
|
490
|
-
information: {
|
|
491
|
-
code: 'COMETD_ERROR',
|
|
492
|
-
...unsubscribeReply
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
reject(error);
|
|
496
|
-
});
|
|
497
|
-
} catch (error) {
|
|
498
|
-
if (isIllegalStateError(error)) {
|
|
499
|
-
resolve(undefined);
|
|
500
|
-
return;
|
|
501
|
-
}
|
|
502
|
-
reject(error);
|
|
503
|
-
}
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
async empty() {
|
|
507
|
-
await this.init();
|
|
508
|
-
this.cometd.clearSubscriptions();
|
|
509
|
-
this.subscriptions.clear();
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
const cometdAdapter = new CometdAdapter();
|
|
513
|
-
|
|
514
|
-
export { cometdAdapter as default };
|