@codingame/monaco-vscode-api 12.0.0 → 13.0.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.
@@ -1,799 +0,0 @@
1
-
2
- import { VSBuffer } from '../../../common/buffer.js';
3
- import { Emitter } from '../../../common/event.js';
4
- import { Disposable, DisposableStore } from '../../../common/lifecycle.js';
5
- import { IPCClient } from './ipc.js';
6
-
7
- var SocketDiagnosticsEventType;
8
- (function (SocketDiagnosticsEventType) {
9
- SocketDiagnosticsEventType["Created"] = "created";
10
- SocketDiagnosticsEventType["Read"] = "read";
11
- SocketDiagnosticsEventType["Write"] = "write";
12
- SocketDiagnosticsEventType["Open"] = "open";
13
- SocketDiagnosticsEventType["Error"] = "error";
14
- SocketDiagnosticsEventType["Close"] = "close";
15
- SocketDiagnosticsEventType["BrowserWebSocketBlobReceived"] = "browserWebSocketBlobReceived";
16
- SocketDiagnosticsEventType["NodeEndReceived"] = "nodeEndReceived";
17
- SocketDiagnosticsEventType["NodeEndSent"] = "nodeEndSent";
18
- SocketDiagnosticsEventType["NodeDrainBegin"] = "nodeDrainBegin";
19
- SocketDiagnosticsEventType["NodeDrainEnd"] = "nodeDrainEnd";
20
- SocketDiagnosticsEventType["zlibInflateError"] = "zlibInflateError";
21
- SocketDiagnosticsEventType["zlibInflateData"] = "zlibInflateData";
22
- SocketDiagnosticsEventType["zlibInflateInitialWrite"] = "zlibInflateInitialWrite";
23
- SocketDiagnosticsEventType["zlibInflateInitialFlushFired"] = "zlibInflateInitialFlushFired";
24
- SocketDiagnosticsEventType["zlibInflateWrite"] = "zlibInflateWrite";
25
- SocketDiagnosticsEventType["zlibInflateFlushFired"] = "zlibInflateFlushFired";
26
- SocketDiagnosticsEventType["zlibDeflateError"] = "zlibDeflateError";
27
- SocketDiagnosticsEventType["zlibDeflateData"] = "zlibDeflateData";
28
- SocketDiagnosticsEventType["zlibDeflateWrite"] = "zlibDeflateWrite";
29
- SocketDiagnosticsEventType["zlibDeflateFlushFired"] = "zlibDeflateFlushFired";
30
- SocketDiagnosticsEventType["WebSocketNodeSocketWrite"] = "webSocketNodeSocketWrite";
31
- SocketDiagnosticsEventType["WebSocketNodeSocketPeekedHeader"] = "webSocketNodeSocketPeekedHeader";
32
- SocketDiagnosticsEventType["WebSocketNodeSocketReadHeader"] = "webSocketNodeSocketReadHeader";
33
- SocketDiagnosticsEventType["WebSocketNodeSocketReadData"] = "webSocketNodeSocketReadData";
34
- SocketDiagnosticsEventType["WebSocketNodeSocketUnmaskedData"] = "webSocketNodeSocketUnmaskedData";
35
- SocketDiagnosticsEventType["WebSocketNodeSocketDrainBegin"] = "webSocketNodeSocketDrainBegin";
36
- SocketDiagnosticsEventType["WebSocketNodeSocketDrainEnd"] = "webSocketNodeSocketDrainEnd";
37
- SocketDiagnosticsEventType["ProtocolHeaderRead"] = "protocolHeaderRead";
38
- SocketDiagnosticsEventType["ProtocolMessageRead"] = "protocolMessageRead";
39
- SocketDiagnosticsEventType["ProtocolHeaderWrite"] = "protocolHeaderWrite";
40
- SocketDiagnosticsEventType["ProtocolMessageWrite"] = "protocolMessageWrite";
41
- SocketDiagnosticsEventType["ProtocolWrite"] = "protocolWrite";
42
- })(SocketDiagnosticsEventType || (SocketDiagnosticsEventType = {}));
43
- var SocketDiagnostics;
44
- (function (SocketDiagnostics) {
45
- SocketDiagnostics.enableDiagnostics = false;
46
- SocketDiagnostics.records = [];
47
- const socketIds = ( new WeakMap());
48
- let lastUsedSocketId = 0;
49
- function getSocketId(nativeObject, label) {
50
- if (!( socketIds.has(nativeObject))) {
51
- const id = String(++lastUsedSocketId);
52
- socketIds.set(nativeObject, id);
53
- }
54
- return socketIds.get(nativeObject);
55
- }
56
- function traceSocketEvent(nativeObject, socketDebugLabel, type, data) {
57
- if (!SocketDiagnostics.enableDiagnostics) {
58
- return;
59
- }
60
- const id = getSocketId(nativeObject);
61
- if (data instanceof VSBuffer || data instanceof Uint8Array || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
62
- const copiedData = VSBuffer.alloc(data.byteLength);
63
- copiedData.set(data);
64
- SocketDiagnostics.records.push({ timestamp: Date.now(), id, label: socketDebugLabel, type, buff: copiedData });
65
- }
66
- else {
67
- SocketDiagnostics.records.push({ timestamp: Date.now(), id, label: socketDebugLabel, type, data: data });
68
- }
69
- }
70
- SocketDiagnostics.traceSocketEvent = traceSocketEvent;
71
- })(SocketDiagnostics || (SocketDiagnostics = {}));
72
- var SocketCloseEventType;
73
- (function (SocketCloseEventType) {
74
- SocketCloseEventType[SocketCloseEventType["NodeSocketCloseEvent"] = 0] = "NodeSocketCloseEvent";
75
- SocketCloseEventType[SocketCloseEventType["WebSocketCloseEvent"] = 1] = "WebSocketCloseEvent";
76
- })(SocketCloseEventType || (SocketCloseEventType = {}));
77
- let emptyBuffer = null;
78
- function getEmptyBuffer() {
79
- if (!emptyBuffer) {
80
- emptyBuffer = VSBuffer.alloc(0);
81
- }
82
- return emptyBuffer;
83
- }
84
- class ChunkStream {
85
- get byteLength() {
86
- return this._totalLength;
87
- }
88
- constructor() {
89
- this._chunks = [];
90
- this._totalLength = 0;
91
- }
92
- acceptChunk(buff) {
93
- this._chunks.push(buff);
94
- this._totalLength += buff.byteLength;
95
- }
96
- read(byteCount) {
97
- return this._read(byteCount, true);
98
- }
99
- peek(byteCount) {
100
- return this._read(byteCount, false);
101
- }
102
- _read(byteCount, advance) {
103
- if (byteCount === 0) {
104
- return getEmptyBuffer();
105
- }
106
- if (byteCount > this._totalLength) {
107
- throw ( new Error(`Cannot read so many bytes!`));
108
- }
109
- if (this._chunks[0].byteLength === byteCount) {
110
- const result = this._chunks[0];
111
- if (advance) {
112
- this._chunks.shift();
113
- this._totalLength -= byteCount;
114
- }
115
- return result;
116
- }
117
- if (this._chunks[0].byteLength > byteCount) {
118
- const result = this._chunks[0].slice(0, byteCount);
119
- if (advance) {
120
- this._chunks[0] = this._chunks[0].slice(byteCount);
121
- this._totalLength -= byteCount;
122
- }
123
- return result;
124
- }
125
- const result = VSBuffer.alloc(byteCount);
126
- let resultOffset = 0;
127
- let chunkIndex = 0;
128
- while (byteCount > 0) {
129
- const chunk = this._chunks[chunkIndex];
130
- if (chunk.byteLength > byteCount) {
131
- const chunkPart = chunk.slice(0, byteCount);
132
- result.set(chunkPart, resultOffset);
133
- resultOffset += byteCount;
134
- if (advance) {
135
- this._chunks[chunkIndex] = chunk.slice(byteCount);
136
- this._totalLength -= byteCount;
137
- }
138
- byteCount -= byteCount;
139
- }
140
- else {
141
- result.set(chunk, resultOffset);
142
- resultOffset += chunk.byteLength;
143
- if (advance) {
144
- this._chunks.shift();
145
- this._totalLength -= chunk.byteLength;
146
- }
147
- else {
148
- chunkIndex++;
149
- }
150
- byteCount -= chunk.byteLength;
151
- }
152
- }
153
- return result;
154
- }
155
- }
156
- var ProtocolMessageType;
157
- (function (ProtocolMessageType) {
158
- ProtocolMessageType[ProtocolMessageType["None"] = 0] = "None";
159
- ProtocolMessageType[ProtocolMessageType["Regular"] = 1] = "Regular";
160
- ProtocolMessageType[ProtocolMessageType["Control"] = 2] = "Control";
161
- ProtocolMessageType[ProtocolMessageType["Ack"] = 3] = "Ack";
162
- ProtocolMessageType[ProtocolMessageType["Disconnect"] = 5] = "Disconnect";
163
- ProtocolMessageType[ProtocolMessageType["ReplayRequest"] = 6] = "ReplayRequest";
164
- ProtocolMessageType[ProtocolMessageType["Pause"] = 7] = "Pause";
165
- ProtocolMessageType[ProtocolMessageType["Resume"] = 8] = "Resume";
166
- ProtocolMessageType[ProtocolMessageType["KeepAlive"] = 9] = "KeepAlive";
167
- })(ProtocolMessageType || (ProtocolMessageType = {}));
168
- function protocolMessageTypeToString(messageType) {
169
- switch (messageType) {
170
- case ProtocolMessageType.None: return 'None';
171
- case ProtocolMessageType.Regular: return 'Regular';
172
- case ProtocolMessageType.Control: return 'Control';
173
- case ProtocolMessageType.Ack: return 'Ack';
174
- case ProtocolMessageType.Disconnect: return 'Disconnect';
175
- case ProtocolMessageType.ReplayRequest: return 'ReplayRequest';
176
- case ProtocolMessageType.Pause: return 'PauseWriting';
177
- case ProtocolMessageType.Resume: return 'ResumeWriting';
178
- case ProtocolMessageType.KeepAlive: return 'KeepAlive';
179
- }
180
- }
181
- var ProtocolConstants;
182
- (function (ProtocolConstants) {
183
- ProtocolConstants[ProtocolConstants["HeaderLength"] = 13] = "HeaderLength";
184
- ProtocolConstants[ProtocolConstants["AcknowledgeTime"] = 2000] = "AcknowledgeTime";
185
- ProtocolConstants[ProtocolConstants["TimeoutTime"] = 20000] = "TimeoutTime";
186
- ProtocolConstants[ProtocolConstants["ReconnectionGraceTime"] = 10800000] = "ReconnectionGraceTime";
187
- ProtocolConstants[ProtocolConstants["ReconnectionShortGraceTime"] = 300000] = "ReconnectionShortGraceTime";
188
- ProtocolConstants[ProtocolConstants["KeepAliveSendTime"] = 5000] = "KeepAliveSendTime";
189
- })(ProtocolConstants || (ProtocolConstants = {}));
190
- class ProtocolMessage {
191
- constructor(type, id, ack, data) {
192
- this.type = type;
193
- this.id = id;
194
- this.ack = ack;
195
- this.data = data;
196
- this.writtenTime = 0;
197
- }
198
- get size() {
199
- return this.data.byteLength;
200
- }
201
- }
202
- class ProtocolReader extends Disposable {
203
- constructor(socket) {
204
- super();
205
- this._onMessage = this._register(( new Emitter()));
206
- this.onMessage = this._onMessage.event;
207
- this._state = {
208
- readHead: true,
209
- readLen: ProtocolConstants.HeaderLength,
210
- messageType: ProtocolMessageType.None,
211
- id: 0,
212
- ack: 0
213
- };
214
- this._socket = socket;
215
- this._isDisposed = false;
216
- this._incomingData = ( new ChunkStream());
217
- this._register(this._socket.onData(data => this.acceptChunk(data)));
218
- this.lastReadTime = Date.now();
219
- }
220
- acceptChunk(data) {
221
- if (!data || data.byteLength === 0) {
222
- return;
223
- }
224
- this.lastReadTime = Date.now();
225
- this._incomingData.acceptChunk(data);
226
- while (this._incomingData.byteLength >= this._state.readLen) {
227
- const buff = this._incomingData.read(this._state.readLen);
228
- if (this._state.readHead) {
229
- this._state.readHead = false;
230
- this._state.readLen = buff.readUInt32BE(9);
231
- this._state.messageType = buff.readUInt8(0);
232
- this._state.id = buff.readUInt32BE(1);
233
- this._state.ack = buff.readUInt32BE(5);
234
- this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolHeaderRead, { messageType: protocolMessageTypeToString(this._state.messageType), id: this._state.id, ack: this._state.ack, messageSize: this._state.readLen });
235
- }
236
- else {
237
- const messageType = this._state.messageType;
238
- const id = this._state.id;
239
- const ack = this._state.ack;
240
- this._state.readHead = true;
241
- this._state.readLen = ProtocolConstants.HeaderLength;
242
- this._state.messageType = ProtocolMessageType.None;
243
- this._state.id = 0;
244
- this._state.ack = 0;
245
- this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolMessageRead, buff);
246
- this._onMessage.fire(( new ProtocolMessage(messageType, id, ack, buff)));
247
- if (this._isDisposed) {
248
- break;
249
- }
250
- }
251
- }
252
- }
253
- readEntireBuffer() {
254
- return this._incomingData.read(this._incomingData.byteLength);
255
- }
256
- dispose() {
257
- this._isDisposed = true;
258
- super.dispose();
259
- }
260
- }
261
- class ProtocolWriter {
262
- constructor(socket) {
263
- this._writeNowTimeout = null;
264
- this._isDisposed = false;
265
- this._isPaused = false;
266
- this._socket = socket;
267
- this._data = [];
268
- this._totalLength = 0;
269
- this.lastWriteTime = 0;
270
- }
271
- dispose() {
272
- try {
273
- this.flush();
274
- }
275
- catch (err) {
276
- }
277
- this._isDisposed = true;
278
- }
279
- drain() {
280
- this.flush();
281
- return this._socket.drain();
282
- }
283
- flush() {
284
- this._writeNow();
285
- }
286
- pause() {
287
- this._isPaused = true;
288
- }
289
- resume() {
290
- this._isPaused = false;
291
- this._scheduleWriting();
292
- }
293
- write(msg) {
294
- if (this._isDisposed) {
295
- return;
296
- }
297
- msg.writtenTime = Date.now();
298
- this.lastWriteTime = Date.now();
299
- const header = VSBuffer.alloc(ProtocolConstants.HeaderLength);
300
- header.writeUInt8(msg.type, 0);
301
- header.writeUInt32BE(msg.id, 1);
302
- header.writeUInt32BE(msg.ack, 5);
303
- header.writeUInt32BE(msg.data.byteLength, 9);
304
- this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolHeaderWrite, { messageType: protocolMessageTypeToString(msg.type), id: msg.id, ack: msg.ack, messageSize: msg.data.byteLength });
305
- this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolMessageWrite, msg.data);
306
- this._writeSoon(header, msg.data);
307
- }
308
- _bufferAdd(head, body) {
309
- const wasEmpty = this._totalLength === 0;
310
- this._data.push(head, body);
311
- this._totalLength += head.byteLength + body.byteLength;
312
- return wasEmpty;
313
- }
314
- _bufferTake() {
315
- const ret = VSBuffer.concat(this._data, this._totalLength);
316
- this._data.length = 0;
317
- this._totalLength = 0;
318
- return ret;
319
- }
320
- _writeSoon(header, data) {
321
- if (this._bufferAdd(header, data)) {
322
- this._scheduleWriting();
323
- }
324
- }
325
- _scheduleWriting() {
326
- if (this._writeNowTimeout) {
327
- return;
328
- }
329
- this._writeNowTimeout = setTimeout(() => {
330
- this._writeNowTimeout = null;
331
- this._writeNow();
332
- });
333
- }
334
- _writeNow() {
335
- if (this._totalLength === 0) {
336
- return;
337
- }
338
- if (this._isPaused) {
339
- return;
340
- }
341
- const data = this._bufferTake();
342
- this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolWrite, { byteLength: data.byteLength });
343
- this._socket.write(data);
344
- }
345
- }
346
- class Protocol extends Disposable {
347
- constructor(socket) {
348
- super();
349
- this._onMessage = ( new Emitter());
350
- this.onMessage = this._onMessage.event;
351
- this._onDidDispose = ( new Emitter());
352
- this.onDidDispose = this._onDidDispose.event;
353
- this._socket = socket;
354
- this._socketWriter = this._register(( new ProtocolWriter(this._socket)));
355
- this._socketReader = this._register(( new ProtocolReader(this._socket)));
356
- this._register(this._socketReader.onMessage((msg) => {
357
- if (msg.type === ProtocolMessageType.Regular) {
358
- this._onMessage.fire(msg.data);
359
- }
360
- }));
361
- this._register(this._socket.onClose(() => this._onDidDispose.fire()));
362
- }
363
- drain() {
364
- return this._socketWriter.drain();
365
- }
366
- getSocket() {
367
- return this._socket;
368
- }
369
- sendDisconnect() {
370
- }
371
- send(buffer) {
372
- this._socketWriter.write(( new ProtocolMessage(ProtocolMessageType.Regular, 0, 0, buffer)));
373
- }
374
- }
375
- class Client extends IPCClient {
376
- static fromSocket(socket, id) {
377
- return ( new Client(( new Protocol(socket)), id));
378
- }
379
- get onDidDispose() { return this.protocol.onDidDispose; }
380
- constructor(protocol, id, ipcLogger = null) {
381
- super(protocol, id, ipcLogger);
382
- this.protocol = protocol;
383
- }
384
- dispose() {
385
- super.dispose();
386
- const socket = this.protocol.getSocket();
387
- this.protocol.sendDisconnect();
388
- this.protocol.dispose();
389
- socket.end();
390
- }
391
- }
392
- class BufferedEmitter {
393
- constructor() {
394
- this._hasListeners = false;
395
- this._isDeliveringMessages = false;
396
- this._bufferedMessages = [];
397
- this._emitter = ( new Emitter({
398
- onWillAddFirstListener: () => {
399
- this._hasListeners = true;
400
- queueMicrotask(() => this._deliverMessages());
401
- },
402
- onDidRemoveLastListener: () => {
403
- this._hasListeners = false;
404
- }
405
- }));
406
- this.event = this._emitter.event;
407
- }
408
- _deliverMessages() {
409
- if (this._isDeliveringMessages) {
410
- return;
411
- }
412
- this._isDeliveringMessages = true;
413
- while (this._hasListeners && this._bufferedMessages.length > 0) {
414
- this._emitter.fire(this._bufferedMessages.shift());
415
- }
416
- this._isDeliveringMessages = false;
417
- }
418
- fire(event) {
419
- if (this._hasListeners) {
420
- if (this._bufferedMessages.length > 0) {
421
- this._bufferedMessages.push(event);
422
- }
423
- else {
424
- this._emitter.fire(event);
425
- }
426
- }
427
- else {
428
- this._bufferedMessages.push(event);
429
- }
430
- }
431
- flushBuffer() {
432
- this._bufferedMessages = [];
433
- }
434
- }
435
- class QueueElement {
436
- constructor(data) {
437
- this.data = data;
438
- this.next = null;
439
- }
440
- }
441
- class Queue {
442
- constructor() {
443
- this._first = null;
444
- this._last = null;
445
- }
446
- length() {
447
- let result = 0;
448
- let current = this._first;
449
- while (current) {
450
- current = current.next;
451
- result++;
452
- }
453
- return result;
454
- }
455
- peek() {
456
- if (!this._first) {
457
- return null;
458
- }
459
- return this._first.data;
460
- }
461
- toArray() {
462
- const result = [];
463
- let resultLen = 0;
464
- let it = this._first;
465
- while (it) {
466
- result[resultLen++] = it.data;
467
- it = it.next;
468
- }
469
- return result;
470
- }
471
- pop() {
472
- if (!this._first) {
473
- return;
474
- }
475
- if (this._first === this._last) {
476
- this._first = null;
477
- this._last = null;
478
- return;
479
- }
480
- this._first = this._first.next;
481
- }
482
- push(item) {
483
- const element = ( new QueueElement(item));
484
- if (!this._first) {
485
- this._first = element;
486
- this._last = element;
487
- return;
488
- }
489
- this._last.next = element;
490
- this._last = element;
491
- }
492
- }
493
- class LoadEstimator {
494
- static { this._HISTORY_LENGTH = 10; }
495
- static { this._INSTANCE = null; }
496
- static getInstance() {
497
- if (!LoadEstimator._INSTANCE) {
498
- LoadEstimator._INSTANCE = ( new LoadEstimator());
499
- }
500
- return LoadEstimator._INSTANCE;
501
- }
502
- constructor() {
503
- this.lastRuns = [];
504
- const now = Date.now();
505
- for (let i = 0; i < LoadEstimator._HISTORY_LENGTH; i++) {
506
- this.lastRuns[i] = now - 1000 * i;
507
- }
508
- setInterval(() => {
509
- for (let i = LoadEstimator._HISTORY_LENGTH; i >= 1; i--) {
510
- this.lastRuns[i] = this.lastRuns[i - 1];
511
- }
512
- this.lastRuns[0] = Date.now();
513
- }, 1000);
514
- }
515
- load() {
516
- const now = Date.now();
517
- const historyLimit = (1 + LoadEstimator._HISTORY_LENGTH) * 1000;
518
- let score = 0;
519
- for (let i = 0; i < LoadEstimator._HISTORY_LENGTH; i++) {
520
- if (now - this.lastRuns[i] <= historyLimit) {
521
- score++;
522
- }
523
- }
524
- return 1 - score / LoadEstimator._HISTORY_LENGTH;
525
- }
526
- hasHighLoad() {
527
- return this.load() >= 0.5;
528
- }
529
- }
530
- class PersistentProtocol {
531
- get unacknowledgedCount() {
532
- return this._outgoingMsgId - this._outgoingAckId;
533
- }
534
- constructor(opts) {
535
- this._onControlMessage = ( new BufferedEmitter());
536
- this.onControlMessage = this._onControlMessage.event;
537
- this._onMessage = ( new BufferedEmitter());
538
- this.onMessage = this._onMessage.event;
539
- this._onDidDispose = ( new BufferedEmitter());
540
- this.onDidDispose = this._onDidDispose.event;
541
- this._onSocketClose = ( new BufferedEmitter());
542
- this.onSocketClose = this._onSocketClose.event;
543
- this._onSocketTimeout = ( new BufferedEmitter());
544
- this.onSocketTimeout = this._onSocketTimeout.event;
545
- this._loadEstimator = opts.loadEstimator ?? LoadEstimator.getInstance();
546
- this._shouldSendKeepAlive = opts.sendKeepAlive ?? true;
547
- this._isReconnecting = false;
548
- this._outgoingUnackMsg = ( new Queue());
549
- this._outgoingMsgId = 0;
550
- this._outgoingAckId = 0;
551
- this._outgoingAckTimeout = null;
552
- this._incomingMsgId = 0;
553
- this._incomingAckId = 0;
554
- this._incomingMsgLastTime = 0;
555
- this._incomingAckTimeout = null;
556
- this._lastReplayRequestTime = 0;
557
- this._lastSocketTimeoutTime = Date.now();
558
- this._socketDisposables = ( new DisposableStore());
559
- this._socket = opts.socket;
560
- this._socketWriter = this._socketDisposables.add(( new ProtocolWriter(this._socket)));
561
- this._socketReader = this._socketDisposables.add(( new ProtocolReader(this._socket)));
562
- this._socketDisposables.add(this._socketReader.onMessage(msg => this._receiveMessage(msg)));
563
- this._socketDisposables.add(this._socket.onClose(e => this._onSocketClose.fire(e)));
564
- if (opts.initialChunk) {
565
- this._socketReader.acceptChunk(opts.initialChunk);
566
- }
567
- if (this._shouldSendKeepAlive) {
568
- this._keepAliveInterval = setInterval(() => {
569
- this._sendKeepAlive();
570
- }, ProtocolConstants.KeepAliveSendTime);
571
- }
572
- else {
573
- this._keepAliveInterval = null;
574
- }
575
- }
576
- dispose() {
577
- if (this._outgoingAckTimeout) {
578
- clearTimeout(this._outgoingAckTimeout);
579
- this._outgoingAckTimeout = null;
580
- }
581
- if (this._incomingAckTimeout) {
582
- clearTimeout(this._incomingAckTimeout);
583
- this._incomingAckTimeout = null;
584
- }
585
- if (this._keepAliveInterval) {
586
- clearInterval(this._keepAliveInterval);
587
- this._keepAliveInterval = null;
588
- }
589
- this._socketDisposables.dispose();
590
- }
591
- drain() {
592
- return this._socketWriter.drain();
593
- }
594
- sendDisconnect() {
595
- if (!this._didSendDisconnect) {
596
- this._didSendDisconnect = true;
597
- const msg = ( new ProtocolMessage(ProtocolMessageType.Disconnect, 0, 0, getEmptyBuffer()));
598
- this._socketWriter.write(msg);
599
- this._socketWriter.flush();
600
- }
601
- }
602
- sendPause() {
603
- const msg = ( new ProtocolMessage(ProtocolMessageType.Pause, 0, 0, getEmptyBuffer()));
604
- this._socketWriter.write(msg);
605
- }
606
- sendResume() {
607
- const msg = ( new ProtocolMessage(ProtocolMessageType.Resume, 0, 0, getEmptyBuffer()));
608
- this._socketWriter.write(msg);
609
- }
610
- pauseSocketWriting() {
611
- this._socketWriter.pause();
612
- }
613
- getSocket() {
614
- return this._socket;
615
- }
616
- getMillisSinceLastIncomingData() {
617
- return Date.now() - this._socketReader.lastReadTime;
618
- }
619
- beginAcceptReconnection(socket, initialDataChunk) {
620
- this._isReconnecting = true;
621
- this._socketDisposables.dispose();
622
- this._socketDisposables = ( new DisposableStore());
623
- this._onControlMessage.flushBuffer();
624
- this._onSocketClose.flushBuffer();
625
- this._onSocketTimeout.flushBuffer();
626
- this._socket.dispose();
627
- this._lastReplayRequestTime = 0;
628
- this._lastSocketTimeoutTime = Date.now();
629
- this._socket = socket;
630
- this._socketWriter = this._socketDisposables.add(( new ProtocolWriter(this._socket)));
631
- this._socketReader = this._socketDisposables.add(( new ProtocolReader(this._socket)));
632
- this._socketDisposables.add(this._socketReader.onMessage(msg => this._receiveMessage(msg)));
633
- this._socketDisposables.add(this._socket.onClose(e => this._onSocketClose.fire(e)));
634
- this._socketReader.acceptChunk(initialDataChunk);
635
- }
636
- endAcceptReconnection() {
637
- this._isReconnecting = false;
638
- this._incomingAckId = this._incomingMsgId;
639
- const msg = ( new ProtocolMessage(ProtocolMessageType.Ack, 0, this._incomingAckId, getEmptyBuffer()));
640
- this._socketWriter.write(msg);
641
- const toSend = this._outgoingUnackMsg.toArray();
642
- for (let i = 0, len = toSend.length; i < len; i++) {
643
- this._socketWriter.write(toSend[i]);
644
- }
645
- this._recvAckCheck();
646
- }
647
- acceptDisconnect() {
648
- this._onDidDispose.fire();
649
- }
650
- _receiveMessage(msg) {
651
- if (msg.ack > this._outgoingAckId) {
652
- this._outgoingAckId = msg.ack;
653
- do {
654
- const first = this._outgoingUnackMsg.peek();
655
- if (first && first.id <= msg.ack) {
656
- this._outgoingUnackMsg.pop();
657
- }
658
- else {
659
- break;
660
- }
661
- } while (true);
662
- }
663
- switch (msg.type) {
664
- case ProtocolMessageType.None: {
665
- break;
666
- }
667
- case ProtocolMessageType.Regular: {
668
- if (msg.id > this._incomingMsgId) {
669
- if (msg.id !== this._incomingMsgId + 1) {
670
- const now = Date.now();
671
- if (now - this._lastReplayRequestTime > 10000) {
672
- this._lastReplayRequestTime = now;
673
- this._socketWriter.write(( new ProtocolMessage(ProtocolMessageType.ReplayRequest, 0, 0, getEmptyBuffer())));
674
- }
675
- }
676
- else {
677
- this._incomingMsgId = msg.id;
678
- this._incomingMsgLastTime = Date.now();
679
- this._sendAckCheck();
680
- this._onMessage.fire(msg.data);
681
- }
682
- }
683
- break;
684
- }
685
- case ProtocolMessageType.Control: {
686
- this._onControlMessage.fire(msg.data);
687
- break;
688
- }
689
- case ProtocolMessageType.Ack: {
690
- break;
691
- }
692
- case ProtocolMessageType.Disconnect: {
693
- this._onDidDispose.fire();
694
- break;
695
- }
696
- case ProtocolMessageType.ReplayRequest: {
697
- const toSend = this._outgoingUnackMsg.toArray();
698
- for (let i = 0, len = toSend.length; i < len; i++) {
699
- this._socketWriter.write(toSend[i]);
700
- }
701
- this._recvAckCheck();
702
- break;
703
- }
704
- case ProtocolMessageType.Pause: {
705
- this._socketWriter.pause();
706
- break;
707
- }
708
- case ProtocolMessageType.Resume: {
709
- this._socketWriter.resume();
710
- break;
711
- }
712
- }
713
- }
714
- readEntireBuffer() {
715
- return this._socketReader.readEntireBuffer();
716
- }
717
- flush() {
718
- this._socketWriter.flush();
719
- }
720
- send(buffer) {
721
- const myId = ++this._outgoingMsgId;
722
- this._incomingAckId = this._incomingMsgId;
723
- const msg = ( new ProtocolMessage(ProtocolMessageType.Regular, myId, this._incomingAckId, buffer));
724
- this._outgoingUnackMsg.push(msg);
725
- if (!this._isReconnecting) {
726
- this._socketWriter.write(msg);
727
- this._recvAckCheck();
728
- }
729
- }
730
- sendControl(buffer) {
731
- const msg = ( new ProtocolMessage(ProtocolMessageType.Control, 0, 0, buffer));
732
- this._socketWriter.write(msg);
733
- }
734
- _sendAckCheck() {
735
- if (this._incomingMsgId <= this._incomingAckId) {
736
- return;
737
- }
738
- if (this._incomingAckTimeout) {
739
- return;
740
- }
741
- const timeSinceLastIncomingMsg = Date.now() - this._incomingMsgLastTime;
742
- if (timeSinceLastIncomingMsg >= ProtocolConstants.AcknowledgeTime) {
743
- this._sendAck();
744
- return;
745
- }
746
- this._incomingAckTimeout = setTimeout(() => {
747
- this._incomingAckTimeout = null;
748
- this._sendAckCheck();
749
- }, ProtocolConstants.AcknowledgeTime - timeSinceLastIncomingMsg + 5);
750
- }
751
- _recvAckCheck() {
752
- if (this._outgoingMsgId <= this._outgoingAckId) {
753
- return;
754
- }
755
- if (this._outgoingAckTimeout) {
756
- return;
757
- }
758
- if (this._isReconnecting) {
759
- return;
760
- }
761
- const oldestUnacknowledgedMsg = this._outgoingUnackMsg.peek();
762
- const timeSinceOldestUnacknowledgedMsg = Date.now() - oldestUnacknowledgedMsg.writtenTime;
763
- const timeSinceLastReceivedSomeData = Date.now() - this._socketReader.lastReadTime;
764
- const timeSinceLastTimeout = Date.now() - this._lastSocketTimeoutTime;
765
- if (timeSinceOldestUnacknowledgedMsg >= ProtocolConstants.TimeoutTime
766
- && timeSinceLastReceivedSomeData >= ProtocolConstants.TimeoutTime
767
- && timeSinceLastTimeout >= ProtocolConstants.TimeoutTime) {
768
- if (!this._loadEstimator.hasHighLoad()) {
769
- this._lastSocketTimeoutTime = Date.now();
770
- this._onSocketTimeout.fire({
771
- unacknowledgedMsgCount: this._outgoingUnackMsg.length(),
772
- timeSinceOldestUnacknowledgedMsg,
773
- timeSinceLastReceivedSomeData
774
- });
775
- return;
776
- }
777
- }
778
- const minimumTimeUntilTimeout = Math.max(ProtocolConstants.TimeoutTime - timeSinceOldestUnacknowledgedMsg, ProtocolConstants.TimeoutTime - timeSinceLastReceivedSomeData, ProtocolConstants.TimeoutTime - timeSinceLastTimeout, 500);
779
- this._outgoingAckTimeout = setTimeout(() => {
780
- this._outgoingAckTimeout = null;
781
- this._recvAckCheck();
782
- }, minimumTimeUntilTimeout);
783
- }
784
- _sendAck() {
785
- if (this._incomingMsgId <= this._incomingAckId) {
786
- return;
787
- }
788
- this._incomingAckId = this._incomingMsgId;
789
- const msg = ( new ProtocolMessage(ProtocolMessageType.Ack, 0, this._incomingAckId, getEmptyBuffer()));
790
- this._socketWriter.write(msg);
791
- }
792
- _sendKeepAlive() {
793
- this._incomingAckId = this._incomingMsgId;
794
- const msg = ( new ProtocolMessage(ProtocolMessageType.KeepAlive, 0, this._incomingAckId, getEmptyBuffer()));
795
- this._socketWriter.write(msg);
796
- }
797
- }
798
-
799
- export { BufferedEmitter, ChunkStream, Client, PersistentProtocol, Protocol, ProtocolConstants, SocketCloseEventType, SocketDiagnostics, SocketDiagnosticsEventType };