From 430b74951949c5e3a52cf4b01df3f06a5c9ae497 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Wed, 13 Nov 2024 15:26:12 -0800 Subject: [PATCH 1/3] improv: more robust definitions for typescript --- .../test/RootPathContextService.test.ts | 25 +- .../test/files/{file1.ts => class.ts} | 18 +- .../root-path-context/test/files/file2.ts | 260 ++++++++++++++++++ .../root-path-context/test/files/functions.ts | 13 + .../test/testCases/typescript.ts | 71 +++++ .../function_declaration/typescript.scm | 36 ++- .../method_definition/typescript.scm | 40 ++- 7 files changed, 421 insertions(+), 42 deletions(-) rename core/autocomplete/context/root-path-context/test/files/{file1.ts => class.ts} (54%) create mode 100644 core/autocomplete/context/root-path-context/test/files/file2.ts create mode 100644 core/autocomplete/context/root-path-context/test/files/functions.ts create mode 100644 core/autocomplete/context/root-path-context/test/testCases/typescript.ts diff --git a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts index 58da8982a..fe81a0edd 100644 --- a/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts +++ b/core/autocomplete/context/root-path-context/test/RootPathContextService.test.ts @@ -1,29 +1,8 @@ +import { TYPESCRIPT_TEST_CASES } from "./testCases/typescript"; import { testRootPathContext } from "./testUtils"; const TEST_CASES = [ - { - nodeType: "function_declaration", - fileName: "file1.ts", - language: "TypeScript", - cursorPosition: { line: 10, character: 24 }, - definitionPositions: [ - { row: 9, column: 34 }, // Person - { row: 9, column: 44 }, // Address - ], - }, - { - nodeType: "method_declaration", - fileName: "file1.ts", - language: "TypeScript", - cursorPosition: { line: 22, character: 30 }, - definitionPositions: [ - { row: 13, column: 29 }, // BaseClass - { row: 13, column: 55 }, // FirstInterface - { row: 13, column: 72 }, // SecondInterface - { row: 21, column: 33 }, // Person - { row: 21, column: 43 }, // Address - ], - }, + ...TYPESCRIPT_TEST_CASES, { nodeType: "function_definition", fileName: "file1.py", diff --git a/core/autocomplete/context/root-path-context/test/files/file1.ts b/core/autocomplete/context/root-path-context/test/files/class.ts similarity index 54% rename from core/autocomplete/context/root-path-context/test/files/file1.ts rename to core/autocomplete/context/root-path-context/test/files/class.ts index ba09c5709..017b592ae 100644 --- a/core/autocomplete/context/root-path-context/test/files/file1.ts +++ b/core/autocomplete/context/root-path-context/test/files/class.ts @@ -7,10 +7,6 @@ import { // @ts-ignore } from "./types"; -function getAddress(person: Person): Address { - return person.address; -} - class Group extends BaseClass implements FirstInterface, SecondInterface { people: Person[]; @@ -20,6 +16,18 @@ class Group extends BaseClass implements FirstInterface, SecondInterface { } getPersonAddress(person: Person): Address { - return getAddress(person); + return person.address; + } + + getHardcodedAddress(): Address { + return { street: "123 Main St", city: "Anytown" }; + } + + addPerson(person: Person) { + this.people = [...this.people, person]; + } + + addPeople(people: Person[]) { + this.people = [...this.people, ...people]; } } diff --git a/core/autocomplete/context/root-path-context/test/files/file2.ts b/core/autocomplete/context/root-path-context/test/files/file2.ts new file mode 100644 index 000000000..124b76bff --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/files/file2.ts @@ -0,0 +1,260 @@ +// @ts-nocheck +import { IProtocol } from "core/protocol/index.js"; +import { IMessenger, type Message } from "core/util/messenger"; +import { ChildProcessWithoutNullStreams } from "node:child_process"; +import * as fs from "node:fs"; +import net from "node:net"; +import { v4 as uuidv4 } from "uuid"; + +class IPCMessengerBase< + ToProtocol extends IProtocol, + FromProtocol extends IProtocol, +> implements IMessenger +{ + _sendMsg(message: Message) { + throw new Error("Not implemented"); + } + + typeListeners = new Map any)[]>(); + idListeners = new Map any>(); + + private _handleLine(line: string) { + try { + const msg: Message = JSON.parse(line); + if (msg.messageType === undefined || msg.messageId === undefined) { + throw new Error("Invalid message sent: " + JSON.stringify(msg)); + } + + // Call handler and respond with return value + const listeners = this.typeListeners.get(msg.messageType as any); + listeners?.forEach(async (handler) => { + try { + const response = await handler(msg); + if ( + response && + typeof response[Symbol.asyncIterator] === "function" + ) { + for await (const update of response) { + this.send(msg.messageType, update, msg.messageId); + } + this.send(msg.messageType, { done: true }, msg.messageId); + } else { + this.send(msg.messageType, response, msg.messageId); + } + } catch (e: any) { + console.warn(`Error running handler for "${msg.messageType}": `, e); + this._onErrorHandlers.forEach((handler) => { + handler(e); + }); + } + }); + + // Call handler which is waiting for the response, nothing to return + this.idListeners.get(msg.messageId)?.(msg); + } catch (e) { + let truncatedLine = line; + if (line.length > 200) { + truncatedLine = + line.substring(0, 100) + "..." + line.substring(line.length - 100); + } + console.error("Error parsing line: ", truncatedLine, e); + return; + } + } + + private _unfinishedLine: string | undefined = undefined; + protected _handleData(data: Buffer) { + const d = data.toString(); + const lines = d.split(/\r\n/).filter((line) => line.trim() !== ""); + if (lines.length === 0) { + return; + } + + if (this._unfinishedLine) { + lines[0] = this._unfinishedLine + lines[0]; + this._unfinishedLine = undefined; + } + if (!d.endsWith("\r\n")) { + this._unfinishedLine = lines.pop(); + } + lines.forEach((line) => this._handleLine(line)); + } + + private _onErrorHandlers: ((error: Error) => void)[] = []; + + onError(handler: (error: Error) => void) { + this._onErrorHandlers.push(handler); + } + + request( + messageType: T, + data: FromProtocol[T][0], + ): Promise { + const messageId = uuidv4(); + return new Promise((resolve) => { + const handler = (msg: Message) => { + resolve(msg.data); + this.idListeners.delete(messageId); + }; + this.idListeners.set(messageId, handler); + this.send(messageType, data, messageId); + }); + } + + mock(data: any) { + const d = JSON.stringify(data); + this._handleData(Buffer.from(d)); + } + + send( + messageType: T, + data: FromProtocol[T][0], + messageId?: string, + ): string { + messageId = messageId ?? uuidv4(); + const msg: Message = { + messageType: messageType as string, + data, + messageId, + }; + this._sendMsg(msg); + return messageId; + } + + invoke( + messageType: T, + data: ToProtocol[T][0], + ): ToProtocol[T][1] { + return this.typeListeners.get(messageType)?.[0]?.({ + messageId: uuidv4(), + messageType: messageType as string, + data, + }); + } + + on( + messageType: T, + handler: ( + message: Message, + ) => Promise | ToProtocol[T][1], + ): void { + if (!this.typeListeners.has(messageType)) { + this.typeListeners.set(messageType, []); + } + this.typeListeners.get(messageType)?.push(handler); + } +} + +export class IpcMessenger< + ToProtocol extends IProtocol, + FromProtocol extends IProtocol, + > + extends IPCMessengerBase + implements IMessenger +{ + constructor() { + super(); + console.log("Setup"); + process.stdin.on("data", (data) => { + // console.log("[info] Received data: ", data.toString()); + this._handleData(data); + }); + process.stdout.on("close", () => { + fs.writeFileSync("./error.log", `${new Date().toISOString()}\n`); + console.log("[info] Exiting Continue core..."); + process.exit(1); + }); + process.stdin.on("close", () => { + fs.writeFileSync("./error.log", `${new Date().toISOString()}\n`); + console.log("[info] Exiting Continue core..."); + process.exit(1); + }); + } + _sendMsg(msg: Message) { + const d = JSON.stringify(msg); + // console.log("[info] Sending message: ", d); + process.stdout?.write(d + "\r\n"); + } +} + +export class CoreBinaryMessenger< + ToProtocol extends IProtocol, + FromProtocol extends IProtocol, + > + extends IPCMessengerBase + implements IMessenger +{ + private errorHandler: (error: Error) => void = () => {}; + private messageHandlers: Map< + keyof ToProtocol, + (message: Message) => Promise | any + > = new Map(); + + constructor(private readonly subprocess: ChildProcessWithoutNullStreams) { + super(); + console.log("Setup"); + this.subprocess.stdout.on("data", (data) => { + console.log("[info] Received data from core:", data.toString() + "\n"); + this._handleData(data); + }); + this.subprocess.stdout.on("close", () => { + console.log("[info] Continue core exited"); + }); + this.subprocess.stdin.on("close", () => { + console.log("[info] Continue core exited"); + }); + } + + _sendMsg(msg: Message) { + console.log("[info] Sending message to core:", msg); + const d = JSON.stringify(msg); + this.subprocess.stdin.write(d + "\r\n"); + } +} + +export class CoreBinaryTcpMessenger< + ToProtocol extends IProtocol, + FromProtocol extends IProtocol, + > + extends IPCMessengerBase + implements IMessenger +{ + private port: number = 3000; + private socket: net.Socket | null = null; + + typeListeners = new Map any)[]>(); + idListeners = new Map any>(); + + constructor() { + super(); + const socket = net.createConnection(this.port, "localhost"); + + this.socket = socket; + socket.on("data", (data: Buffer) => { + // console.log("[info] Received data from core:", data.toString() + "\n"); + this._handleData(data); + }); + + socket.on("end", () => { + console.log("Disconnected from server"); + }); + + socket.on("error", (err: any) => { + console.error("Client error:", err); + }); + } + + close() { + this.socket?.end(); + } + + _sendMsg(msg: Message) { + if (this.socket) { + // console.log("[info] Sending message to core:", msg); + const d = JSON.stringify(msg); + this.socket.write(d + "\r\n"); + } else { + console.error("Socket is not connected"); + } + } +} diff --git a/core/autocomplete/context/root-path-context/test/files/functions.ts b/core/autocomplete/context/root-path-context/test/files/functions.ts new file mode 100644 index 000000000..8ae6944e9 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/files/functions.ts @@ -0,0 +1,13 @@ +import { + Address, + Person, + // @ts-ignore +} from "./types"; + +function getAddress(person: Person): Address { + return person.address; +} + +function getFirstAddress(people: Person[]): Address { + return people[0].address; +} diff --git a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts new file mode 100644 index 000000000..2eb589c90 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts @@ -0,0 +1,71 @@ +export const TYPESCRIPT_TEST_CASES = [ + { + nodeType: "function_declaration with a param and a return type", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 7, character: 24 }, + definitionPositions: [ + { row: 6, column: 34 }, // Person + { row: 6, column: 44 }, // Address + ], + }, + { + nodeType: "function_declaration with array param", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 11, character: 27 }, + definitionPositions: [ + { row: 10, column: 39 }, // Person + { row: 10, column: 51 }, // Address + ], + }, + { + nodeType: "method_declaration", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 18, character: 26 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 17, column: 33 }, // Person + { row: 17, column: 43 }, // Address + ], + }, + { + nodeType: "method_declaration without arguments", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 22, character: 54 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 21, column: 32 }, // Address + ], + }, + { + nodeType: "method_declaration without return type", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 26, character: 43 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 25, column: 26 }, // Person + ], + }, + { + nodeType: "method_declaration with array type arguments", + fileName: "class.ts", + language: "TypeScript", + cursorPosition: { line: 30, character: 46 }, + definitionPositions: [ + { row: 9, column: 29 }, // BaseClass + { row: 9, column: 55 }, // FirstInterface + { row: 9, column: 72 }, // SecondInterface + { row: 29, column: 26 }, // Person + ], + }, +]; diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm b/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm index ab10968a3..24dc04e22 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/function_declaration/typescript.scm @@ -1,10 +1,34 @@ +; Pattern for capturing the return type ( - (function_declaration - (formal_parameters - (_ - (type_annotation) @param_type - ) + (function_declaration + (type_annotation) @return_type + ) +) + +; Pattern for parameters with direct type_identifier +( + (function_declaration + (formal_parameters + (_ + (type_annotation + (type_identifier) @param_type ) - (type_annotation) @return_type + ) ) + ) +) + +; Pattern for parameters with one level of nesting +( + (function_declaration + (formal_parameters + (_ + (type_annotation + (_ + (type_identifier) @param_type + ) + ) + ) + ) + ) ) \ No newline at end of file diff --git a/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm b/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm index 727db5090..9f8685385 100644 --- a/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm +++ b/extensions/vscode/tree-sitter/root-path-context-queries/method_definition/typescript.scm @@ -1,10 +1,34 @@ +; Pattern for capturing the return type ( - (method_definition - (formal_parameters - (_ - (type_annotation) @param_type - ) - ) - (type_annotation) @return_type - ) + (method_definition + (type_annotation) @return_type + ) +) + +; Pattern for parameters with direct type_identifier +( + (method_definition + (formal_parameters + (_ + (type_annotation + (type_identifier) @param_type + ) + ) + ) + ) +) + +; Pattern for parameters with one level of nesting +( + (method_definition + (formal_parameters + (_ + (type_annotation + (_ + (type_identifier) @param_type + ) + ) + ) + ) + ) ) From 41ac57fa408eebb09c90781a4cf8b3092a6191ee Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Wed, 13 Nov 2024 17:14:13 -0800 Subject: [PATCH 2/3] test: typescript definitions --- .../root-path-context/test/files/functions.ts | 12 ++++++++++++ .../test/testCases/typescript.ts | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/autocomplete/context/root-path-context/test/files/functions.ts b/core/autocomplete/context/root-path-context/test/files/functions.ts index 8ae6944e9..2b6125a74 100644 --- a/core/autocomplete/context/root-path-context/test/files/functions.ts +++ b/core/autocomplete/context/root-path-context/test/files/functions.ts @@ -11,3 +11,15 @@ function getAddress(person: Person): Address { function getFirstAddress(people: Person[]): Address { return people[0].address; } + +function logPerson(person: Person) { + console.log(`Name: ${person.name}, Age: ${person.age}`); +} + +function getHardcodedAddress(): Address { + return { street: "123 Main St", city: "Anytown", zipCode: "12345" }; +} + +function getAddresses(people: Person[]): Address[] { + return people[0].address; +} diff --git a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts index 2eb589c90..6913cb8d3 100644 --- a/core/autocomplete/context/root-path-context/test/testCases/typescript.ts +++ b/core/autocomplete/context/root-path-context/test/testCases/typescript.ts @@ -19,6 +19,24 @@ export const TYPESCRIPT_TEST_CASES = [ { row: 10, column: 51 }, // Address ], }, + { + nodeType: "function_declaration without return type", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 15, character: 58 }, + definitionPositions: [ + { row: 14, column: 33 }, // Person + ], + }, + { + nodeType: "function_declaration without params", + fileName: "functions.ts", + language: "TypeScript", + cursorPosition: { line: 19, character: 70 }, + definitionPositions: [ + { row: 18, column: 39 }, // Person + ], + }, { nodeType: "method_declaration", fileName: "class.ts", From 0599c52267b68d936ed328e42a99114fcfbf2dde Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Wed, 13 Nov 2024 17:22:37 -0800 Subject: [PATCH 3/3] cleanup: remove unused file --- .../root-path-context/test/files/file2.ts | 260 ------------------ 1 file changed, 260 deletions(-) delete mode 100644 core/autocomplete/context/root-path-context/test/files/file2.ts diff --git a/core/autocomplete/context/root-path-context/test/files/file2.ts b/core/autocomplete/context/root-path-context/test/files/file2.ts deleted file mode 100644 index 124b76bff..000000000 --- a/core/autocomplete/context/root-path-context/test/files/file2.ts +++ /dev/null @@ -1,260 +0,0 @@ -// @ts-nocheck -import { IProtocol } from "core/protocol/index.js"; -import { IMessenger, type Message } from "core/util/messenger"; -import { ChildProcessWithoutNullStreams } from "node:child_process"; -import * as fs from "node:fs"; -import net from "node:net"; -import { v4 as uuidv4 } from "uuid"; - -class IPCMessengerBase< - ToProtocol extends IProtocol, - FromProtocol extends IProtocol, -> implements IMessenger -{ - _sendMsg(message: Message) { - throw new Error("Not implemented"); - } - - typeListeners = new Map any)[]>(); - idListeners = new Map any>(); - - private _handleLine(line: string) { - try { - const msg: Message = JSON.parse(line); - if (msg.messageType === undefined || msg.messageId === undefined) { - throw new Error("Invalid message sent: " + JSON.stringify(msg)); - } - - // Call handler and respond with return value - const listeners = this.typeListeners.get(msg.messageType as any); - listeners?.forEach(async (handler) => { - try { - const response = await handler(msg); - if ( - response && - typeof response[Symbol.asyncIterator] === "function" - ) { - for await (const update of response) { - this.send(msg.messageType, update, msg.messageId); - } - this.send(msg.messageType, { done: true }, msg.messageId); - } else { - this.send(msg.messageType, response, msg.messageId); - } - } catch (e: any) { - console.warn(`Error running handler for "${msg.messageType}": `, e); - this._onErrorHandlers.forEach((handler) => { - handler(e); - }); - } - }); - - // Call handler which is waiting for the response, nothing to return - this.idListeners.get(msg.messageId)?.(msg); - } catch (e) { - let truncatedLine = line; - if (line.length > 200) { - truncatedLine = - line.substring(0, 100) + "..." + line.substring(line.length - 100); - } - console.error("Error parsing line: ", truncatedLine, e); - return; - } - } - - private _unfinishedLine: string | undefined = undefined; - protected _handleData(data: Buffer) { - const d = data.toString(); - const lines = d.split(/\r\n/).filter((line) => line.trim() !== ""); - if (lines.length === 0) { - return; - } - - if (this._unfinishedLine) { - lines[0] = this._unfinishedLine + lines[0]; - this._unfinishedLine = undefined; - } - if (!d.endsWith("\r\n")) { - this._unfinishedLine = lines.pop(); - } - lines.forEach((line) => this._handleLine(line)); - } - - private _onErrorHandlers: ((error: Error) => void)[] = []; - - onError(handler: (error: Error) => void) { - this._onErrorHandlers.push(handler); - } - - request( - messageType: T, - data: FromProtocol[T][0], - ): Promise { - const messageId = uuidv4(); - return new Promise((resolve) => { - const handler = (msg: Message) => { - resolve(msg.data); - this.idListeners.delete(messageId); - }; - this.idListeners.set(messageId, handler); - this.send(messageType, data, messageId); - }); - } - - mock(data: any) { - const d = JSON.stringify(data); - this._handleData(Buffer.from(d)); - } - - send( - messageType: T, - data: FromProtocol[T][0], - messageId?: string, - ): string { - messageId = messageId ?? uuidv4(); - const msg: Message = { - messageType: messageType as string, - data, - messageId, - }; - this._sendMsg(msg); - return messageId; - } - - invoke( - messageType: T, - data: ToProtocol[T][0], - ): ToProtocol[T][1] { - return this.typeListeners.get(messageType)?.[0]?.({ - messageId: uuidv4(), - messageType: messageType as string, - data, - }); - } - - on( - messageType: T, - handler: ( - message: Message, - ) => Promise | ToProtocol[T][1], - ): void { - if (!this.typeListeners.has(messageType)) { - this.typeListeners.set(messageType, []); - } - this.typeListeners.get(messageType)?.push(handler); - } -} - -export class IpcMessenger< - ToProtocol extends IProtocol, - FromProtocol extends IProtocol, - > - extends IPCMessengerBase - implements IMessenger -{ - constructor() { - super(); - console.log("Setup"); - process.stdin.on("data", (data) => { - // console.log("[info] Received data: ", data.toString()); - this._handleData(data); - }); - process.stdout.on("close", () => { - fs.writeFileSync("./error.log", `${new Date().toISOString()}\n`); - console.log("[info] Exiting Continue core..."); - process.exit(1); - }); - process.stdin.on("close", () => { - fs.writeFileSync("./error.log", `${new Date().toISOString()}\n`); - console.log("[info] Exiting Continue core..."); - process.exit(1); - }); - } - _sendMsg(msg: Message) { - const d = JSON.stringify(msg); - // console.log("[info] Sending message: ", d); - process.stdout?.write(d + "\r\n"); - } -} - -export class CoreBinaryMessenger< - ToProtocol extends IProtocol, - FromProtocol extends IProtocol, - > - extends IPCMessengerBase - implements IMessenger -{ - private errorHandler: (error: Error) => void = () => {}; - private messageHandlers: Map< - keyof ToProtocol, - (message: Message) => Promise | any - > = new Map(); - - constructor(private readonly subprocess: ChildProcessWithoutNullStreams) { - super(); - console.log("Setup"); - this.subprocess.stdout.on("data", (data) => { - console.log("[info] Received data from core:", data.toString() + "\n"); - this._handleData(data); - }); - this.subprocess.stdout.on("close", () => { - console.log("[info] Continue core exited"); - }); - this.subprocess.stdin.on("close", () => { - console.log("[info] Continue core exited"); - }); - } - - _sendMsg(msg: Message) { - console.log("[info] Sending message to core:", msg); - const d = JSON.stringify(msg); - this.subprocess.stdin.write(d + "\r\n"); - } -} - -export class CoreBinaryTcpMessenger< - ToProtocol extends IProtocol, - FromProtocol extends IProtocol, - > - extends IPCMessengerBase - implements IMessenger -{ - private port: number = 3000; - private socket: net.Socket | null = null; - - typeListeners = new Map any)[]>(); - idListeners = new Map any>(); - - constructor() { - super(); - const socket = net.createConnection(this.port, "localhost"); - - this.socket = socket; - socket.on("data", (data: Buffer) => { - // console.log("[info] Received data from core:", data.toString() + "\n"); - this._handleData(data); - }); - - socket.on("end", () => { - console.log("Disconnected from server"); - }); - - socket.on("error", (err: any) => { - console.error("Client error:", err); - }); - } - - close() { - this.socket?.end(); - } - - _sendMsg(msg: Message) { - if (this.socket) { - // console.log("[info] Sending message to core:", msg); - const d = JSON.stringify(msg); - this.socket.write(d + "\r\n"); - } else { - console.error("Socket is not connected"); - } - } -}