Best JavaScript code snippet using wpt
ipc.net.ts
Source:ipc.net.ts
...131 drain(): Promise<void>;132 traceSocketEvent(type: SocketDiagnosticsEventType, data?: VSBuffer | Uint8Array | ArrayBuffer | ArrayBufferView | any): void;133}134let emptyBuffer: VSBuffer | null = null;135function getEmptyBuffer(): VSBuffer {136 if (!emptyBuffer) {137 emptyBuffer = VSBuffer.alloc(0);138 }139 return emptyBuffer;140}141export class ChunkStream {142 private _chunks: VSBuffer[];143 private _totalLength: number;144 public get byteLength() {145 return this._totalLength;146 }147 constructor() {148 this._chunks = [];149 this._totalLength = 0;150 }151 public acceptChunk(buff: VSBuffer) {152 this._chunks.push(buff);153 this._totalLength += buff.byteLength;154 }155 public read(byteCount: number): VSBuffer {156 return this._read(byteCount, true);157 }158 public peek(byteCount: number): VSBuffer {159 return this._read(byteCount, false);160 }161 private _read(byteCount: number, advance: boolean): VSBuffer {162 if (byteCount === 0) {163 return getEmptyBuffer();164 }165 if (byteCount > this._totalLength) {166 throw new Error(`Cannot read so many bytes!`);167 }168 if (this._chunks[0].byteLength === byteCount) {169 // super fast path, precisely first chunk must be returned170 const result = this._chunks[0];171 if (advance) {172 this._chunks.shift();173 this._totalLength -= byteCount;174 }175 return result;176 }177 if (this._chunks[0].byteLength > byteCount) {178 // fast path, the reading is entirely within the first chunk179 const result = this._chunks[0].slice(0, byteCount);180 if (advance) {181 this._chunks[0] = this._chunks[0].slice(byteCount);182 this._totalLength -= byteCount;183 }184 return result;185 }186 const result = VSBuffer.alloc(byteCount);187 let resultOffset = 0;188 let chunkIndex = 0;189 while (byteCount > 0) {190 const chunk = this._chunks[chunkIndex];191 if (chunk.byteLength > byteCount) {192 // this chunk will survive193 const chunkPart = chunk.slice(0, byteCount);194 result.set(chunkPart, resultOffset);195 resultOffset += byteCount;196 if (advance) {197 this._chunks[chunkIndex] = chunk.slice(byteCount);198 this._totalLength -= byteCount;199 }200 byteCount -= byteCount;201 } else {202 // this chunk will be entirely read203 result.set(chunk, resultOffset);204 resultOffset += chunk.byteLength;205 if (advance) {206 this._chunks.shift();207 this._totalLength -= chunk.byteLength;208 } else {209 chunkIndex++;210 }211 byteCount -= chunk.byteLength;212 }213 }214 return result;215 }216}217const enum ProtocolMessageType {218 None = 0,219 Regular = 1,220 Control = 2,221 Ack = 3,222 Disconnect = 5,223 ReplayRequest = 6,224 Pause = 7,225 Resume = 8,226 KeepAlive = 9,227}228function protocolMessageTypeToString(messageType: ProtocolMessageType) {229 switch (messageType) {230 case ProtocolMessageType.None: return 'None';231 case ProtocolMessageType.Regular: return 'Regular';232 case ProtocolMessageType.Control: return 'Control';233 case ProtocolMessageType.Ack: return 'Ack';234 case ProtocolMessageType.Disconnect: return 'Disconnect';235 case ProtocolMessageType.ReplayRequest: return 'ReplayRequest';236 case ProtocolMessageType.Pause: return 'PauseWriting';237 case ProtocolMessageType.Resume: return 'ResumeWriting';238 case ProtocolMessageType.KeepAlive: return 'KeepAlive';239 }240}241export const enum ProtocolConstants {242 HeaderLength = 13,243 /**244 * Send an Acknowledge message at most 2 seconds later...245 */246 AcknowledgeTime = 2000, // 2 seconds247 /**248 * If there is a sent message that has been unacknowledged for 20 seconds,249 * and we didn't see any incoming server data in the past 20 seconds,250 * then consider the connection has timed out.251 */252 TimeoutTime = 20000, // 20 seconds253 /**254 * If there is no reconnection within this time-frame, consider the connection permanently closed...255 */256 ReconnectionGraceTime = 3 * 60 * 60 * 1000, // 3hrs257 /**258 * Maximal grace time between the first and the last reconnection...259 */260 ReconnectionShortGraceTime = 5 * 60 * 1000, // 5min261}262class ProtocolMessage {263 public writtenTime: number;264 constructor(265 public readonly type: ProtocolMessageType,266 public readonly id: number,267 public readonly ack: number,268 public readonly data: VSBuffer269 ) {270 this.writtenTime = 0;271 }272 public get size(): number {273 return this.data.byteLength;274 }275}276class ProtocolReader extends Disposable {277 private readonly _socket: ISocket;278 private _isDisposed: boolean;279 private readonly _incomingData: ChunkStream;280 public lastReadTime: number;281 private readonly _onMessage = this._register(new Emitter<ProtocolMessage>());282 public readonly onMessage: Event<ProtocolMessage> = this._onMessage.event;283 private readonly _state = {284 readHead: true,285 readLen: ProtocolConstants.HeaderLength,286 messageType: ProtocolMessageType.None,287 id: 0,288 ack: 0289 };290 constructor(socket: ISocket) {291 super();292 this._socket = socket;293 this._isDisposed = false;294 this._incomingData = new ChunkStream();295 this._register(this._socket.onData(data => this.acceptChunk(data)));296 this.lastReadTime = Date.now();297 }298 public acceptChunk(data: VSBuffer | null): void {299 if (!data || data.byteLength === 0) {300 return;301 }302 this.lastReadTime = Date.now();303 this._incomingData.acceptChunk(data);304 while (this._incomingData.byteLength >= this._state.readLen) {305 const buff = this._incomingData.read(this._state.readLen);306 if (this._state.readHead) {307 // buff is the header308 // save new state => next time will read the body309 this._state.readHead = false;310 this._state.readLen = buff.readUInt32BE(9);311 this._state.messageType = buff.readUInt8(0);312 this._state.id = buff.readUInt32BE(1);313 this._state.ack = buff.readUInt32BE(5);314 this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolHeaderRead, { messageType: protocolMessageTypeToString(this._state.messageType), id: this._state.id, ack: this._state.ack, messageSize: this._state.readLen });315 } else {316 // buff is the body317 const messageType = this._state.messageType;318 const id = this._state.id;319 const ack = this._state.ack;320 // save new state => next time will read the header321 this._state.readHead = true;322 this._state.readLen = ProtocolConstants.HeaderLength;323 this._state.messageType = ProtocolMessageType.None;324 this._state.id = 0;325 this._state.ack = 0;326 this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolMessageRead, buff);327 this._onMessage.fire(new ProtocolMessage(messageType, id, ack, buff));328 if (this._isDisposed) {329 // check if an event listener lead to our disposal330 break;331 }332 }333 }334 }335 public readEntireBuffer(): VSBuffer {336 return this._incomingData.read(this._incomingData.byteLength);337 }338 public override dispose(): void {339 this._isDisposed = true;340 super.dispose();341 }342}343class ProtocolWriter {344 private _isDisposed: boolean;345 private _isPaused: boolean;346 private readonly _socket: ISocket;347 private _data: VSBuffer[];348 private _totalLength: number;349 public lastWriteTime: number;350 constructor(socket: ISocket) {351 this._isDisposed = false;352 this._isPaused = false;353 this._socket = socket;354 this._data = [];355 this._totalLength = 0;356 this.lastWriteTime = 0;357 }358 public dispose(): void {359 try {360 this.flush();361 } catch (err) {362 // ignore error, since the socket could be already closed363 }364 this._isDisposed = true;365 }366 public drain(): Promise<void> {367 this.flush();368 return this._socket.drain();369 }370 public flush(): void {371 // flush372 this._writeNow();373 }374 public pause(): void {375 this._isPaused = true;376 }377 public resume(): void {378 this._isPaused = false;379 this._scheduleWriting();380 }381 public write(msg: ProtocolMessage) {382 if (this._isDisposed) {383 // ignore: there could be left-over promises which complete and then384 // decide to write a response, etc...385 return;386 }387 msg.writtenTime = Date.now();388 this.lastWriteTime = Date.now();389 const header = VSBuffer.alloc(ProtocolConstants.HeaderLength);390 header.writeUInt8(msg.type, 0);391 header.writeUInt32BE(msg.id, 1);392 header.writeUInt32BE(msg.ack, 5);393 header.writeUInt32BE(msg.data.byteLength, 9);394 this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolHeaderWrite, { messageType: protocolMessageTypeToString(msg.type), id: msg.id, ack: msg.ack, messageSize: msg.data.byteLength });395 this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolMessageWrite, msg.data);396 this._writeSoon(header, msg.data);397 }398 private _bufferAdd(head: VSBuffer, body: VSBuffer): boolean {399 const wasEmpty = this._totalLength === 0;400 this._data.push(head, body);401 this._totalLength += head.byteLength + body.byteLength;402 return wasEmpty;403 }404 private _bufferTake(): VSBuffer {405 const ret = VSBuffer.concat(this._data, this._totalLength);406 this._data.length = 0;407 this._totalLength = 0;408 return ret;409 }410 private _writeSoon(header: VSBuffer, data: VSBuffer): void {411 if (this._bufferAdd(header, data)) {412 this._scheduleWriting();413 }414 }415 private _writeNowTimeout: any = null;416 private _scheduleWriting(): void {417 if (this._writeNowTimeout) {418 return;419 }420 this._writeNowTimeout = setTimeout(() => {421 this._writeNowTimeout = null;422 this._writeNow();423 });424 }425 private _writeNow(): void {426 if (this._totalLength === 0) {427 return;428 }429 if (this._isPaused) {430 return;431 }432 const data = this._bufferTake();433 this._socket.traceSocketEvent(SocketDiagnosticsEventType.ProtocolWrite, { byteLength: data.byteLength });434 this._socket.write(data);435 }436}437/**438 * A message has the following format:439 * ```440 * /-------------------------------|------\441 * | HEADER | |442 * |-------------------------------| DATA |443 * | TYPE | ID | ACK | DATA_LENGTH | |444 * \-------------------------------|------/445 * ```446 * The header is 9 bytes and consists of:447 * - TYPE is 1 byte (ProtocolMessageType) - the message type448 * - ID is 4 bytes (u32be) - the message id (can be 0 to indicate to be ignored)449 * - ACK is 4 bytes (u32be) - the acknowledged message id (can be 0 to indicate to be ignored)450 * - DATA_LENGTH is 4 bytes (u32be) - the length in bytes of DATA451 *452 * Only Regular messages are counted, other messages are not counted, nor acknowledged.453 */454export class Protocol extends Disposable implements IMessagePassingProtocol {455 private _socket: ISocket;456 private _socketWriter: ProtocolWriter;457 private _socketReader: ProtocolReader;458 private readonly _onMessage = new Emitter<VSBuffer>();459 readonly onMessage: Event<VSBuffer> = this._onMessage.event;460 private readonly _onDidDispose = new Emitter<void>();461 readonly onDidDispose: Event<void> = this._onDidDispose.event;462 constructor(socket: ISocket) {463 super();464 this._socket = socket;465 this._socketWriter = this._register(new ProtocolWriter(this._socket));466 this._socketReader = this._register(new ProtocolReader(this._socket));467 this._register(this._socketReader.onMessage((msg) => {468 if (msg.type === ProtocolMessageType.Regular) {469 this._onMessage.fire(msg.data);470 }471 }));472 this._register(this._socket.onClose(() => this._onDidDispose.fire()));473 }474 drain(): Promise<void> {475 return this._socketWriter.drain();476 }477 getSocket(): ISocket {478 return this._socket;479 }480 sendDisconnect(): void {481 // Nothing to do...482 }483 send(buffer: VSBuffer): void {484 this._socketWriter.write(new ProtocolMessage(ProtocolMessageType.Regular, 0, 0, buffer));485 }486}487export class Client<TContext = string> extends IPCClient<TContext> {488 static fromSocket<TContext = string>(socket: ISocket, id: TContext): Client<TContext> {489 return new Client(new Protocol(socket), id);490 }491 get onDidDispose(): Event<void> { return this.protocol.onDidDispose; }492 constructor(private protocol: Protocol | PersistentProtocol, id: TContext, ipcLogger: IIPCLogger | null = null) {493 super(protocol, id, ipcLogger);494 }495 override dispose(): void {496 super.dispose();497 const socket = this.protocol.getSocket();498 this.protocol.sendDisconnect();499 this.protocol.dispose();500 socket.end();501 }502}503/**504 * Will ensure no messages are lost if there are no event listeners.505 */506export class BufferedEmitter<T> {507 private _emitter: Emitter<T>;508 public readonly event: Event<T>;509 private _hasListeners = false;510 private _isDeliveringMessages = false;511 private _bufferedMessages: T[] = [];512 constructor() {513 this._emitter = new Emitter<T>({514 onFirstListenerAdd: () => {515 this._hasListeners = true;516 // it is important to deliver these messages after this call, but before517 // other messages have a chance to be received (to guarantee in order delivery)518 // that's why we're using here queueMicrotask and not other types of timeouts519 queueMicrotask(() => this._deliverMessages());520 },521 onLastListenerRemove: () => {522 this._hasListeners = false;523 }524 });525 this.event = this._emitter.event;526 }527 private _deliverMessages(): void {528 if (this._isDeliveringMessages) {529 return;530 }531 this._isDeliveringMessages = true;532 while (this._hasListeners && this._bufferedMessages.length > 0) {533 this._emitter.fire(this._bufferedMessages.shift()!);534 }535 this._isDeliveringMessages = false;536 }537 public fire(event: T): void {538 if (this._hasListeners) {539 if (this._bufferedMessages.length > 0) {540 this._bufferedMessages.push(event);541 } else {542 this._emitter.fire(event);543 }544 } else {545 this._bufferedMessages.push(event);546 }547 }548 public flushBuffer(): void {549 this._bufferedMessages = [];550 }551}552class QueueElement<T> {553 public readonly data: T;554 public next: QueueElement<T> | null;555 constructor(data: T) {556 this.data = data;557 this.next = null;558 }559}560class Queue<T> {561 private _first: QueueElement<T> | null;562 private _last: QueueElement<T> | null;563 constructor() {564 this._first = null;565 this._last = null;566 }567 public length(): number {568 let result = 0;569 let current = this._first;570 while (current) {571 current = current.next;572 result++;573 }574 return result;575 }576 public peek(): T | null {577 if (!this._first) {578 return null;579 }580 return this._first.data;581 }582 public toArray(): T[] {583 const result: T[] = [];584 let resultLen = 0;585 let it = this._first;586 while (it) {587 result[resultLen++] = it.data;588 it = it.next;589 }590 return result;591 }592 public pop(): void {593 if (!this._first) {594 return;595 }596 if (this._first === this._last) {597 this._first = null;598 this._last = null;599 return;600 }601 this._first = this._first.next;602 }603 public push(item: T): void {604 const element = new QueueElement(item);605 if (!this._first) {606 this._first = element;607 this._last = element;608 return;609 }610 this._last!.next = element;611 this._last = element;612 }613}614class LoadEstimator {615 private static _HISTORY_LENGTH = 10;616 private static _INSTANCE: LoadEstimator | null = null;617 public static getInstance(): LoadEstimator {618 if (!LoadEstimator._INSTANCE) {619 LoadEstimator._INSTANCE = new LoadEstimator();620 }621 return LoadEstimator._INSTANCE;622 }623 private lastRuns: number[];624 constructor() {625 this.lastRuns = [];626 const now = Date.now();627 for (let i = 0; i < LoadEstimator._HISTORY_LENGTH; i++) {628 this.lastRuns[i] = now - 1000 * i;629 }630 setInterval(() => {631 for (let i = LoadEstimator._HISTORY_LENGTH; i >= 1; i--) {632 this.lastRuns[i] = this.lastRuns[i - 1];633 }634 this.lastRuns[0] = Date.now();635 }, 1000);636 }637 /**638 * returns an estimative number, from 0 (low load) to 1 (high load)639 */640 private load(): number {641 const now = Date.now();642 const historyLimit = (1 + LoadEstimator._HISTORY_LENGTH) * 1000;643 let score = 0;644 for (let i = 0; i < LoadEstimator._HISTORY_LENGTH; i++) {645 if (now - this.lastRuns[i] <= historyLimit) {646 score++;647 }648 }649 return 1 - score / LoadEstimator._HISTORY_LENGTH;650 }651 public hasHighLoad(): boolean {652 return this.load() >= 0.5;653 }654}655export interface ILoadEstimator {656 hasHighLoad(): boolean;657}658/**659 * Same as Protocol, but will actually track messages and acks.660 * Moreover, it will ensure no messages are lost if there are no event listeners.661 */662export class PersistentProtocol implements IMessagePassingProtocol {663 private _isReconnecting: boolean;664 private _outgoingUnackMsg: Queue<ProtocolMessage>;665 private _outgoingMsgId: number;666 private _outgoingAckId: number;667 private _outgoingAckTimeout: any | null;668 private _incomingMsgId: number;669 private _incomingAckId: number;670 private _incomingMsgLastTime: number;671 private _incomingAckTimeout: any | null;672 private _keepAliveInterval: any | null;673 private _lastReplayRequestTime: number;674 private _lastSocketTimeoutTime: number;675 private _socket: ISocket;676 private _socketWriter: ProtocolWriter;677 private _socketReader: ProtocolReader;678 private _socketDisposables: IDisposable[];679 private readonly _loadEstimator: ILoadEstimator;680 private readonly _onControlMessage = new BufferedEmitter<VSBuffer>();681 readonly onControlMessage: Event<VSBuffer> = this._onControlMessage.event;682 private readonly _onMessage = new BufferedEmitter<VSBuffer>();683 readonly onMessage: Event<VSBuffer> = this._onMessage.event;684 private readonly _onDidDispose = new BufferedEmitter<void>();685 readonly onDidDispose: Event<void> = this._onDidDispose.event;686 private readonly _onSocketClose = new BufferedEmitter<SocketCloseEvent>();687 readonly onSocketClose: Event<SocketCloseEvent> = this._onSocketClose.event;688 private readonly _onSocketTimeout = new BufferedEmitter<SocketTimeoutEvent>();689 readonly onSocketTimeout: Event<SocketTimeoutEvent> = this._onSocketTimeout.event;690 public get unacknowledgedCount(): number {691 return this._outgoingMsgId - this._outgoingAckId;692 }693 constructor(socket: ISocket, initialChunk: VSBuffer | null = null, loadEstimator: ILoadEstimator = LoadEstimator.getInstance()) {694 this._loadEstimator = loadEstimator;695 this._isReconnecting = false;696 this._outgoingUnackMsg = new Queue<ProtocolMessage>();697 this._outgoingMsgId = 0;698 this._outgoingAckId = 0;699 this._outgoingAckTimeout = null;700 this._incomingMsgId = 0;701 this._incomingAckId = 0;702 this._incomingMsgLastTime = 0;703 this._incomingAckTimeout = null;704 this._lastReplayRequestTime = 0;705 this._lastSocketTimeoutTime = Date.now();706 this._socketDisposables = [];707 this._socket = socket;708 this._socketWriter = new ProtocolWriter(this._socket);709 this._socketDisposables.push(this._socketWriter);710 this._socketReader = new ProtocolReader(this._socket);711 this._socketDisposables.push(this._socketReader);712 this._socketDisposables.push(this._socketReader.onMessage(msg => this._receiveMessage(msg)));713 this._socketDisposables.push(this._socket.onClose((e) => this._onSocketClose.fire(e)));714 if (initialChunk) {715 this._socketReader.acceptChunk(initialChunk);716 }717 // send a message every 5s718 this._keepAliveInterval = setInterval(() => {719 this._sendKeepAlive();720 }, 5000);721 }722 dispose(): void {723 if (this._outgoingAckTimeout) {724 clearTimeout(this._outgoingAckTimeout);725 this._outgoingAckTimeout = null;726 }727 if (this._incomingAckTimeout) {728 clearTimeout(this._incomingAckTimeout);729 this._incomingAckTimeout = null;730 }731 if (this._keepAliveInterval) {732 clearInterval(this._keepAliveInterval);733 this._keepAliveInterval = null;734 }735 this._socketDisposables = dispose(this._socketDisposables);736 }737 drain(): Promise<void> {738 return this._socketWriter.drain();739 }740 sendDisconnect(): void {741 const msg = new ProtocolMessage(ProtocolMessageType.Disconnect, 0, 0, getEmptyBuffer());742 this._socketWriter.write(msg);743 this._socketWriter.flush();744 }745 sendPause(): void {746 const msg = new ProtocolMessage(ProtocolMessageType.Pause, 0, 0, getEmptyBuffer());747 this._socketWriter.write(msg);748 }749 sendResume(): void {750 const msg = new ProtocolMessage(ProtocolMessageType.Resume, 0, 0, getEmptyBuffer());751 this._socketWriter.write(msg);752 }753 pauseSocketWriting() {754 this._socketWriter.pause();755 }756 public getSocket(): ISocket {757 return this._socket;758 }759 public getMillisSinceLastIncomingData(): number {760 return Date.now() - this._socketReader.lastReadTime;761 }762 public beginAcceptReconnection(socket: ISocket, initialDataChunk: VSBuffer | null): void {763 this._isReconnecting = true;764 this._socketDisposables = dispose(this._socketDisposables);765 this._onControlMessage.flushBuffer();766 this._onSocketClose.flushBuffer();767 this._onSocketTimeout.flushBuffer();768 this._socket.dispose();769 this._lastReplayRequestTime = 0;770 this._lastSocketTimeoutTime = Date.now();771 this._socket = socket;772 this._socketWriter = new ProtocolWriter(this._socket);773 this._socketDisposables.push(this._socketWriter);774 this._socketReader = new ProtocolReader(this._socket);775 this._socketDisposables.push(this._socketReader);776 this._socketDisposables.push(this._socketReader.onMessage(msg => this._receiveMessage(msg)));777 this._socketDisposables.push(this._socket.onClose((e) => this._onSocketClose.fire(e)));778 this._socketReader.acceptChunk(initialDataChunk);779 }780 public endAcceptReconnection(): void {781 this._isReconnecting = false;782 // After a reconnection, let the other party know (again) which messages have been received.783 // (perhaps the other party didn't receive a previous ACK)784 this._incomingAckId = this._incomingMsgId;785 const msg = new ProtocolMessage(ProtocolMessageType.Ack, 0, this._incomingAckId, getEmptyBuffer());786 this._socketWriter.write(msg);787 // Send again all unacknowledged messages788 const toSend = this._outgoingUnackMsg.toArray();789 for (let i = 0, len = toSend.length; i < len; i++) {790 this._socketWriter.write(toSend[i]);791 }792 this._recvAckCheck();793 }794 public acceptDisconnect(): void {795 this._onDidDispose.fire();796 }797 private _receiveMessage(msg: ProtocolMessage): void {798 if (msg.ack > this._outgoingAckId) {799 this._outgoingAckId = msg.ack;800 do {801 const first = this._outgoingUnackMsg.peek();802 if (first && first.id <= msg.ack) {803 // this message has been confirmed, remove it804 this._outgoingUnackMsg.pop();805 } else {806 break;807 }808 } while (true);809 }810 switch (msg.type) {811 case ProtocolMessageType.None: {812 // N/A813 break;814 }815 case ProtocolMessageType.Regular: {816 if (msg.id > this._incomingMsgId) {817 if (msg.id !== this._incomingMsgId + 1) {818 // in case we missed some messages we ask the other party to resend them819 const now = Date.now();820 if (now - this._lastReplayRequestTime > 10000) {821 // send a replay request at most once every 10s822 this._lastReplayRequestTime = now;823 this._socketWriter.write(new ProtocolMessage(ProtocolMessageType.ReplayRequest, 0, 0, getEmptyBuffer()));824 }825 } else {826 this._incomingMsgId = msg.id;827 this._incomingMsgLastTime = Date.now();828 this._sendAckCheck();829 this._onMessage.fire(msg.data);830 }831 }832 break;833 }834 case ProtocolMessageType.Control: {835 this._onControlMessage.fire(msg.data);836 break;837 }838 case ProtocolMessageType.Ack: {839 // nothing to do, .ack is handled above already840 break;841 }842 case ProtocolMessageType.Disconnect: {843 this._onDidDispose.fire();844 break;845 }846 case ProtocolMessageType.ReplayRequest: {847 // Send again all unacknowledged messages848 const toSend = this._outgoingUnackMsg.toArray();849 for (let i = 0, len = toSend.length; i < len; i++) {850 this._socketWriter.write(toSend[i]);851 }852 this._recvAckCheck();853 break;854 }855 case ProtocolMessageType.Pause: {856 this._socketWriter.pause();857 break;858 }859 case ProtocolMessageType.Resume: {860 this._socketWriter.resume();861 break;862 }863 case ProtocolMessageType.KeepAlive: {864 // nothing to do865 break;866 }867 }868 }869 readEntireBuffer(): VSBuffer {870 return this._socketReader.readEntireBuffer();871 }872 flush(): void {873 this._socketWriter.flush();874 }875 send(buffer: VSBuffer): void {876 const myId = ++this._outgoingMsgId;877 this._incomingAckId = this._incomingMsgId;878 const msg = new ProtocolMessage(ProtocolMessageType.Regular, myId, this._incomingAckId, buffer);879 this._outgoingUnackMsg.push(msg);880 if (!this._isReconnecting) {881 this._socketWriter.write(msg);882 this._recvAckCheck();883 }884 }885 /**886 * Send a message which will not be part of the regular acknowledge flow.887 * Use this for early control messages which are repeated in case of reconnection.888 */889 sendControl(buffer: VSBuffer): void {890 const msg = new ProtocolMessage(ProtocolMessageType.Control, 0, 0, buffer);891 this._socketWriter.write(msg);892 }893 private _sendAckCheck(): void {894 if (this._incomingMsgId <= this._incomingAckId) {895 // nothink to acknowledge896 return;897 }898 if (this._incomingAckTimeout) {899 // there will be a check in the near future900 return;901 }902 const timeSinceLastIncomingMsg = Date.now() - this._incomingMsgLastTime;903 if (timeSinceLastIncomingMsg >= ProtocolConstants.AcknowledgeTime) {904 // sufficient time has passed since this message has been received,905 // and no message from our side needed to be sent in the meantime,906 // so we will send a message containing only an ack.907 this._sendAck();908 return;909 }910 this._incomingAckTimeout = setTimeout(() => {911 this._incomingAckTimeout = null;912 this._sendAckCheck();913 }, ProtocolConstants.AcknowledgeTime - timeSinceLastIncomingMsg + 5);914 }915 private _recvAckCheck(): void {916 if (this._outgoingMsgId <= this._outgoingAckId) {917 // everything has been acknowledged918 return;919 }920 if (this._outgoingAckTimeout) {921 // there will be a check in the near future922 return;923 }924 if (this._isReconnecting) {925 // do not cause a timeout during reconnection,926 // because messages will not be actually written until `endAcceptReconnection`927 return;928 }929 const oldestUnacknowledgedMsg = this._outgoingUnackMsg.peek()!;930 const timeSinceOldestUnacknowledgedMsg = Date.now() - oldestUnacknowledgedMsg.writtenTime;931 const timeSinceLastReceivedSomeData = Date.now() - this._socketReader.lastReadTime;932 const timeSinceLastTimeout = Date.now() - this._lastSocketTimeoutTime;933 if (934 timeSinceOldestUnacknowledgedMsg >= ProtocolConstants.TimeoutTime935 && timeSinceLastReceivedSomeData >= ProtocolConstants.TimeoutTime936 && timeSinceLastTimeout >= ProtocolConstants.TimeoutTime937 ) {938 // It's been a long time since our sent message was acknowledged939 // and a long time since we received some data940 // But this might be caused by the event loop being busy and failing to read messages941 if (!this._loadEstimator.hasHighLoad()) {942 // Trash the socket943 this._lastSocketTimeoutTime = Date.now();944 this._onSocketTimeout.fire({945 unacknowledgedMsgCount: this._outgoingUnackMsg.length(),946 timeSinceOldestUnacknowledgedMsg,947 timeSinceLastReceivedSomeData948 });949 return;950 }951 }952 const minimumTimeUntilTimeout = Math.max(953 ProtocolConstants.TimeoutTime - timeSinceOldestUnacknowledgedMsg,954 ProtocolConstants.TimeoutTime - timeSinceLastReceivedSomeData,955 ProtocolConstants.TimeoutTime - timeSinceLastTimeout,956 500957 );958 this._outgoingAckTimeout = setTimeout(() => {959 this._outgoingAckTimeout = null;960 this._recvAckCheck();961 }, minimumTimeUntilTimeout);962 }963 private _sendAck(): void {964 if (this._incomingMsgId <= this._incomingAckId) {965 // nothink to acknowledge966 return;967 }968 this._incomingAckId = this._incomingMsgId;969 const msg = new ProtocolMessage(ProtocolMessageType.Ack, 0, this._incomingAckId, getEmptyBuffer());970 this._socketWriter.write(msg);971 }972 private _sendKeepAlive(): void {973 const msg = new ProtocolMessage(ProtocolMessageType.KeepAlive, 0, 0, getEmptyBuffer());974 this._socketWriter.write(msg);975 }976}977// (() => {978// if (!SocketDiagnostics.enableDiagnostics) {979// return;980// }981// if (typeof require.__$__nodeRequire !== 'function') {982// console.log(`Can only log socket diagnostics on native platforms.`);983// return;984// }985// const type = (986// process.argv.includes('--type=renderer')987// ? 'renderer'...
Display.ts
Source:Display.ts
...15 constructor(render: IRenderer, width: number, height: number) {16 this.render = render17 this.width = width18 this.height = height19 this.foregroundBuffer = this.getEmptyBuffer(TRANSPARENT)20 this.backgroundBuffer = this.getEmptyBuffer(BLACK)21 }22 writeBackgroundBuffer(pixelData: number[][]) {23 this.backgroundBuffer = pixelData24 }25 writeBackgroundPixelData(pixelData: number[]) {26 this.backgroundBuffer = this.getEmptyBuffer(BLACK)27 for (let i = 0; i < pixelData.length; ++i) {28 this.backgroundBuffer[Math.floor(i / this.width)][i % this.width] = pixelData[i]29 }30 }31 writeLine(text: string):void {32 switch (this.writeLineMode) {33 case WRITE_LINE_MODE.DROP:34 console.error('WRITE_LINE_MODE.DROP Not implemented!')35 break36 case WRITE_LINE_MODE.INSTANT:37 default:38 this.foregroundBuffer = this.getEmptyBuffer(TRANSPARENT)39 this.writeForeground(0, text)40 }41 }42 writeLastChar(text: string): void {43 let start = this.width44 for (let i = 0; i < text.length; ++i) {45 const char = text[i]46 const charDef = Charset(char)47 if (charDef === null) {48 console.warn(`Char: ${char} not in charset`)49 continue50 }51 start -= charDef[0].length52 }53 this.writeForeground(start, text)54 }55 private writeChar(lineCol: number, char: string): number {56 const charDef = Charset(char)57 if (charDef === null) {58 console.warn(`Char: ${char} not in charset`)59 return lineCol60 }61 for (let row = 0; row < charDef.length; ++row) {62 for (let col = 0; col < charDef[row].length; ++col) {63 if (lineCol + col >= this.width) continue64 this.foregroundBuffer[row][lineCol + col] = charDef[row][col] !== 0 ? this.writeColor : TRANSPARENT65 }66 }67 lineCol += charDef[0].length68 return lineCol69 }70 private writeForeground(lineCol: number, text: string):void {71 let formatted = text.toString().toLowerCase()72 for (let i = 0; i < formatted.length; i++) {73 const char = formatted[i]74 lineCol = this.writeChar(lineCol, char) + this.charSpacing75 if (lineCol >= this.width) break;76 }77 }78 writeBuffer(): void {79 let outputBuffer = this.getEmptyBuffer(-1)80 for (let row = 0; row < this.height; ++row) {81 for (let col = 0; col < this.width; ++col) {82 outputBuffer[row][col] = this.foregroundBuffer[row][col] === TRANSPARENT ? this.backgroundBuffer[row][col] : this.foregroundBuffer[row][col]83 }84 }85 this.render.render(outputBuffer)86 }87 private getEmptyBuffer(fillValue: number): number[][] {88 return new Array(this.height).fill(0).map(() =>new Array(this.width).fill(fillValue))89 }90}...
messageHandler.js
Source:messageHandler.js
...29 console.error("buffer not initiated.");30 return null;31};3233function getEmptyBuffer() {34 return {35 size: 0,36 messages: []37 };38}3940function emptyBuffer() {41 if(!buffer) return console.error("buffer not initiated");42 43 buffer = getEmptyBuffer();4445 const data = JSON.stringify(buffer, null, 2);4647 write(bufferPath, data);48};4950function initiateBuffer() {51 let data = read(bufferPath);52 if(!data) {53 write(bufferPath, JSON.stringify(getEmptyBuffer(), null, 2));54 buffer = JSON.parse(read(bufferPath));55 } else {56 buffer = JSON.parse(data);57 }5859 return buffer;60};6162function getBuffer() {63 if (!buffer) initiateBuffer();6465 return buffer;66};67
...
Using AI Code Generation
1var wpt = require('wpt');2var buffer = wpt.getEmptyBuffer();3var wpt = require('wpt');4var buffer = wpt.getEmptyBuffer();5var wpt = require('wpt');6var buffer = wpt.getEmptyBuffer();7var wpt = require('wpt');8var buffer = wpt.getEmptyBuffer();9var wpt = require('wpt');10var buffer = wpt.getEmptyBuffer();11var wpt = require('wpt');12var buffer = wpt.getEmptyBuffer();13var wpt = require('wpt');14var buffer = wpt.getEmptyBuffer();15var wpt = require('wpt');16var buffer = wpt.getEmptyBuffer();17var wpt = require('wpt');18var buffer = wpt.getEmptyBuffer();19var wpt = require('wpt');20var buffer = wpt.getEmptyBuffer();21var wpt = require('wpt');22var buffer = wpt.getEmptyBuffer();23var wpt = require('wpt');24var buffer = wpt.getEmptyBuffer();
Using AI Code Generation
1var wpt = require('wpt');2var buffer = wpt.getEmptyBuffer();3console.log(buffer);4var wpt = require('wpt');5var buffer = wpt.getEmptyBuffer();6console.log(buffer);7var wpt = require('wpt');8var buffer = wpt.getEmptyBuffer();9console.log(buffer);10var wpt = require('wpt');11var buffer = wpt.getEmptyBuffer();12console.log(buffer);13var wpt = require('wpt');14var buffer = wpt.getEmptyBuffer();15console.log(buffer);16var wpt = require('wpt');17var buffer = wpt.getEmptyBuffer();18console.log(buffer);19var wpt = require('wpt');20var buffer = wpt.getEmptyBuffer();21console.log(buffer);22var wpt = require('wpt');23var buffer = wpt.getEmptyBuffer();24console.log(buffer);25var wpt = require('wpt');26var buffer = wpt.getEmptyBuffer();27console.log(buffer);28var wpt = require('wpt');29var buffer = wpt.getEmptyBuffer();30console.log(buffer);31var wpt = require('wpt');32var buffer = wpt.getEmptyBuffer();33console.log(buffer);34var wpt = require('wpt');35var buffer = wpt.getEmptyBuffer();36console.log(buffer);
Using AI Code Generation
1var wpt = require('wpt');2var buffer = wpt.getEmptyBuffer(10);3console.log(buffer);4var wpt = require('wpt');5var buffer = wpt.getEmptyBuffer(10, 1);6console.log(buffer);7var wpt = require('wpt');8var buffer = wpt.getEmptyBuffer(10, 2);9console.log(buffer);10var wpt = require('wpt');11var buffer = wpt.getEmptyBuffer(10, 3);12console.log(buffer);13var wpt = require('wpt');14var buffer = wpt.getEmptyBuffer(10, 4);15console.log(buffer);16var wpt = require('wpt');17var buffer = wpt.getEmptyBuffer(10, 5);18console.log(buffer);19var wpt = require('wpt');20var buffer = wpt.getEmptyBuffer(10, 6);21console.log(buffer);
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var buffer = wptoolkit.getEmptyBuffer();3console.log(buffer);4console.log(buffer.toString());5console.log(buffer.toString('hex'));6console.log(buffer.toString('base64'));7console.log(buffer.toString('binary'));8console.log(buffer.toString('utf8'));
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!