Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove promise-socket #1

Merged
merged 5 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"editor.formatOnSave": true,
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
}
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "veeder-root-tls",
"version": "3.1.4",
"version": "3.2.0",
"description": "Biblioteca para comunicação com medidores eletrônicos Veeder Root.",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down Expand Up @@ -48,9 +48,7 @@
"engines": {
"node": ">=10"
},
"dependencies": {
"promise-socket": "^7.0.0"
},
"dependencies": {},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@typescript-eslint/eslint-plugin": "^4.0.1",
Expand Down
35 changes: 9 additions & 26 deletions src/lib/tls4.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import net from 'net';

import PromiseSocket from 'promise-socket';

import { ResponseMessageTank } from './tank/response';

export class Tls {
Expand All @@ -18,10 +16,10 @@ export class Tls {
readonly port: number;

/**
* Instância do PromiseSocket (conexão TCP com o TLS).
* Instância do net.Socket (conexão TCP com o TLS).
* @readonly
*/
private readonly promiseSocket: PromiseSocket<net.Socket>;
private readonly socket: net.Socket;

/**
*
Expand All @@ -31,7 +29,10 @@ export class Tls {
constructor(ip: string, port = 10001) {
this.ip = ip;
this.port = port;
this.promiseSocket = new PromiseSocket(new net.Socket());
this.socket = net.createConnection({
host: this.ip,
port,
});
}

/**
Expand All @@ -43,7 +44,7 @@ export class Tls {
await new Promise((resolve) => {
let data = Buffer.from([]);

this.promiseSocket.stream.on('data', (chunk) => {
this.socket.on('data', (chunk) => {
data = Buffer.concat([data, chunk]);

// ETX (ascii)
Expand All @@ -57,29 +58,11 @@ export class Tls {
return result;
}

/**
* Cria conexão com o TLS.
*/
async connect() {
try {
await this.promiseSocket.connect({
host: this.ip,
port: this.port,
});
this.promiseSocket.setTimeout(3000);

console.info(`Conectado a ${this.ip}:${this.port}.\n`);
} catch (error) {
console.error('Ocorreu um erro ao se conectar ao TLS4.');
throw error;
}
}

/**
* Destrói conexão com o TLS.
*/
destroy() {
this.promiseSocket.destroy();
this.socket.destroy();
}

/**
Expand All @@ -92,7 +75,7 @@ export class Tls {
const command = `i201${padTank}`;
const buffer = Buffer.concat([Buffer.from([0x1]), Buffer.from(command)]);

await this.promiseSocket.write(buffer);
await this.socket.write(buffer);
const result = await this.readAll();

if (result.length === 0) {
Expand Down
36 changes: 1 addition & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,6 @@ convert-to-spaces@^1.0.1:
resolved "https://registry.yarnpkg.com/convert-to-spaces/-/convert-to-spaces-1.0.2.tgz#7e3e48bbe6d997b1417ddca2868204b4d3d85715"
integrity sha512-cj09EBuObp9gZNQCzc7hByQyrs6jVGE+o9kSJmeUoj+GiPiJvi5LYqEH/Hmme4+MTLHM+Ejtq+FChpjjEnsPdQ==

core-js@^3.6.5:
version "3.24.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.24.1.tgz#cf7724d41724154010a6576b7b57d94c5d66e64f"
integrity sha512-0QTBSYSUZ6Gq21utGzkfITDylE8jWC9Ne1D2MrhvlsZBI1x39OdDIVbzSqtgMndIy6BlHxBXpMGqzZmnztg2rg==

cosmiconfig-typescript-loader@^2.0.0:
version "2.0.2"
resolved "https://registry.yarnpkg.com/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-2.0.2.tgz#7e7ce6064af041c910e1e43fb0fd9625cee56e93"
Expand Down Expand Up @@ -2744,35 +2739,6 @@ progress@^2.0.0:
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==

promise-duplex@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/promise-duplex/-/promise-duplex-6.0.0.tgz#9d9d13383ac0f70701b9f095ffd486f106c2232e"
integrity sha512-ZL7rquzjTFzInDBeWYcsT+qddolNvzigahk6MI6qLSbQvlyRRCJkU3JztgaVunzvkH28smRa2Qu/cY9RXtSkgA==
dependencies:
core-js "^3.6.5"
promise-readable "^6.0.0"
promise-writable "^6.0.0"

promise-readable@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/promise-readable/-/promise-readable-6.0.0.tgz#ab1ec1a3daa53ed8764b61902fb7f3de3242025a"
integrity sha512-5NxtmUswijvX5cAM0zPSy6yiCXH/eKBpiiBq6JfAUrmngMquMbzcBhF2qA+ocs4rYYKdvAfv3cOvZxADLtL1CA==
dependencies:
core-js "^3.6.5"

promise-socket@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/promise-socket/-/promise-socket-7.0.0.tgz#20c147c417033a50fe7e1d1236a46ff59d4280b3"
integrity sha512-Oic9BrxmcHOPEnzKp2Js+ehFyvsbd0WxsE5khweCTHuRvdzbXjHUZmSDT6F9TW8SIkAJ0lCzoHjMYnb0WQJPiw==
dependencies:
promise-duplex "^6.0.0"
tslib "^2.0.1"

promise-writable@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/promise-writable/-/promise-writable-6.0.0.tgz#4d703e75873a4e755a39bdb926a37f41538e507f"
integrity sha512-b81zre/itgJFS7dwWzIdKNVVqvLiUxYRS/wolUB0H1YY/tAaS146XGKa4Q/5wCbsnXLyn0MCeV6f8HHe4iUHLg==

pump@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64"
Expand Down Expand Up @@ -3365,7 +3331,7 @@ tslib@^1.8.1:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.1, tslib@^2.1.0:
tslib@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==
Expand Down