mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
refactor(microservices): adjust to the general coding style
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
||||
ClientOptions,
|
||||
TcpClientOptions,
|
||||
} from '../interfaces/client-metadata.interface';
|
||||
import { JsonSocket } from '../json-socket';
|
||||
import { JsonSocket } from '../helpers/json-socket';
|
||||
import { ClientProxy } from './client-proxy';
|
||||
import { ECONNREFUSED } from './constants';
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export class CorruptedPacketLengthException extends Error {
|
||||
constructor(rawContentLength: string) {
|
||||
super(`Corrupted length value "${rawContentLength}" supplied in a packet`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export class InvalidJSONFormatException extends Error {
|
||||
constructor(err: Error, data: string) {
|
||||
super(`Could not parse JSON: ${err.message}\nRequest data: ${data}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export class NetSocketClosedException extends Error {
|
||||
constructor() {
|
||||
super(`The net socket is closed.`);
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,20 @@ import { StringDecoder } from 'string_decoder';
|
||||
import {
|
||||
CLOSE_EVENT,
|
||||
CONNECT_EVENT,
|
||||
DATA_EVENT,
|
||||
ERROR_EVENT,
|
||||
MESSAGE_EVENT,
|
||||
DATA_EVENT,
|
||||
} from './constants';
|
||||
|
||||
const stringDecoder = new StringDecoder();
|
||||
} from '../constants';
|
||||
import { CorruptedPacketLengthException } from '../errors/corrupted-packet-length.exception';
|
||||
import { InvalidJSONFormatException } from '../errors/invalid-json-format.exception';
|
||||
import { NetSocketClosedException } from '../errors/net-socket-closed.exception';
|
||||
|
||||
export class JsonSocket {
|
||||
private contentLength: number | null = null;
|
||||
private isClosed = false;
|
||||
private buffer = '';
|
||||
private closed = false;
|
||||
|
||||
private readonly stringDecoder = new StringDecoder();
|
||||
private readonly delimeter = '#';
|
||||
|
||||
public get netSocket() {
|
||||
@@ -23,9 +25,9 @@ export class JsonSocket {
|
||||
|
||||
constructor(public readonly socket: Socket) {
|
||||
this.socket.on(DATA_EVENT, this.onData.bind(this));
|
||||
this.socket.on(CONNECT_EVENT, () => (this.closed = false));
|
||||
this.socket.on(CLOSE_EVENT, () => (this.closed = true));
|
||||
this.socket.on(ERROR_EVENT, () => (this.closed = true));
|
||||
this.socket.on(CONNECT_EVENT, () => (this.isClosed = false));
|
||||
this.socket.on(CLOSE_EVENT, () => (this.isClosed = true));
|
||||
this.socket.on(ERROR_EVENT, () => (this.isClosed = true));
|
||||
}
|
||||
|
||||
public connect(port: number, host: string) {
|
||||
@@ -49,10 +51,8 @@ export class JsonSocket {
|
||||
}
|
||||
|
||||
public sendMessage(message: any, callback?: (err?: any) => void) {
|
||||
if (this.closed) {
|
||||
if (callback) {
|
||||
callback(new Error('The net socket is closed.'));
|
||||
}
|
||||
if (this.isClosed) {
|
||||
callback && callback(new NetSocketClosedException());
|
||||
return;
|
||||
}
|
||||
this.socket.write(this.formatMessageData(message), 'utf-8', callback);
|
||||
@@ -60,7 +60,7 @@ export class JsonSocket {
|
||||
|
||||
private onData(dataRaw: Buffer | string) {
|
||||
const data = Buffer.isBuffer(dataRaw)
|
||||
? stringDecoder.write(dataRaw)
|
||||
? this.stringDecoder.write(dataRaw)
|
||||
: dataRaw;
|
||||
|
||||
try {
|
||||
@@ -76,7 +76,6 @@ export class JsonSocket {
|
||||
|
||||
if (this.contentLength == null) {
|
||||
const i = this.buffer.indexOf(this.delimeter);
|
||||
|
||||
/**
|
||||
* Check if the buffer has the delimeter (#),
|
||||
* if not, the end of the buffer string might be in the middle of a content length string
|
||||
@@ -88,17 +87,13 @@ export class JsonSocket {
|
||||
if (isNaN(this.contentLength)) {
|
||||
this.contentLength = null;
|
||||
this.buffer = '';
|
||||
|
||||
throw new Error(
|
||||
`Corrupted length value "${rawContentLength}" supplied in a packet`,
|
||||
);
|
||||
throw new CorruptedPacketLengthException(rawContentLength);
|
||||
}
|
||||
|
||||
this.buffer = this.buffer.substring(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.contentLength != null) {
|
||||
if (this.contentLength !== null) {
|
||||
const length = this.buffer.length;
|
||||
|
||||
if (length === this.contentLength) {
|
||||
@@ -115,17 +110,14 @@ export class JsonSocket {
|
||||
private handleMessage(data: string) {
|
||||
this.contentLength = null;
|
||||
this.buffer = '';
|
||||
let message: object;
|
||||
|
||||
let message: Record<string, unknown>;
|
||||
try {
|
||||
message = JSON.parse(data);
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
`Could not parse JSON: ${e.message}\nRequest data: ${data}`,
|
||||
);
|
||||
throw new InvalidJSONFormatException(e, data);
|
||||
}
|
||||
message = message || {};
|
||||
|
||||
this.socket.emit(MESSAGE_EVENT, message);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
MicroserviceOptions,
|
||||
TcpOptions,
|
||||
} from '../interfaces/microservice-configuration.interface';
|
||||
import { JsonSocket } from '../json-socket';
|
||||
import { JsonSocket } from '../helpers/json-socket';
|
||||
import { Server } from './server';
|
||||
|
||||
export class ServerTCP extends Server implements CustomTransportStrategy {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { expect } from 'chai';
|
||||
import { AddressInfo, createServer, Socket } from 'net';
|
||||
import { CONNECT_EVENT, MESSAGE_EVENT } from '../../constants';
|
||||
import { JsonSocket } from '../../json-socket';
|
||||
import { JsonSocket } from '../../helpers/json-socket';
|
||||
import { longPayload } from './data/long-payload-with-special-chars';
|
||||
import * as helpers from './helpers';
|
||||
import { ip } from './helpers';
|
||||
import { expect } from 'chai';
|
||||
// tslint:disable:no-string-literal
|
||||
|
||||
describe('JsonSocket connection', () => {
|
||||
@@ -15,8 +15,8 @@ describe('JsonSocket connection', () => {
|
||||
return done(error);
|
||||
}
|
||||
|
||||
expect(clientSocket['closed']).to.be.false;
|
||||
expect(serverSocket['closed']).to.be.false;
|
||||
expect(clientSocket['isClosed']).to.be.false;
|
||||
expect(serverSocket['isClosed']).to.be.false;
|
||||
|
||||
Promise.all([
|
||||
new Promise(callback => {
|
||||
@@ -36,8 +36,8 @@ describe('JsonSocket connection', () => {
|
||||
}),
|
||||
])
|
||||
.then(() => {
|
||||
expect(clientSocket['closed']).to.equal(false);
|
||||
expect(serverSocket['closed']).to.equal(false);
|
||||
expect(clientSocket['isClosed']).to.equal(false);
|
||||
expect(serverSocket['isClosed']).to.equal(false);
|
||||
clientSocket.end();
|
||||
server.close(done);
|
||||
})
|
||||
@@ -51,8 +51,8 @@ describe('JsonSocket connection', () => {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
expect(clientSocket['closed']).to.equal(false);
|
||||
expect(serverSocket['closed']).to.equal(false);
|
||||
expect(clientSocket['isClosed']).to.equal(false);
|
||||
expect(serverSocket['isClosed']).to.equal(false);
|
||||
Promise.all([
|
||||
new Promise(callback => {
|
||||
clientSocket.sendMessage(longPayload, callback);
|
||||
@@ -71,8 +71,8 @@ describe('JsonSocket connection', () => {
|
||||
}),
|
||||
])
|
||||
.then(() => {
|
||||
expect(clientSocket['closed']).to.equal(false);
|
||||
expect(serverSocket['closed']).to.equal(false);
|
||||
expect(clientSocket['isClosed']).to.equal(false);
|
||||
expect(serverSocket['isClosed']).to.equal(false);
|
||||
clientSocket.end();
|
||||
server.close(done);
|
||||
})
|
||||
@@ -130,8 +130,8 @@ describe('JsonSocket connection', () => {
|
||||
.then(
|
||||
() =>
|
||||
new Promise(callback => {
|
||||
expect(clientSocket['closed']).to.equal(true);
|
||||
expect(serverSocket['closed']).to.equal(true);
|
||||
expect(clientSocket['isClosed']).to.equal(true);
|
||||
expect(serverSocket['isClosed']).to.equal(true);
|
||||
callback();
|
||||
}),
|
||||
)
|
||||
@@ -156,8 +156,8 @@ describe('JsonSocket connection', () => {
|
||||
.then(
|
||||
() =>
|
||||
new Promise(callback => {
|
||||
expect(clientSocket['closed']).to.equal(true);
|
||||
expect(serverSocket['closed']).to.equal(true);
|
||||
expect(clientSocket['isClosed']).to.equal(true);
|
||||
expect(serverSocket['isClosed']).to.equal(true);
|
||||
callback();
|
||||
}),
|
||||
)
|
||||
@@ -177,12 +177,12 @@ describe('JsonSocket connection', () => {
|
||||
|
||||
serverSocket.once('end', () => {
|
||||
setTimeout(() => {
|
||||
expect(serverSocket['closed']).to.equal(true);
|
||||
expect(clientSocket['closed']).to.equal(true);
|
||||
expect(serverSocket['isClosed']).to.equal(true);
|
||||
expect(clientSocket['isClosed']).to.equal(true);
|
||||
|
||||
clientSocket.on(CONNECT_EVENT, () => {
|
||||
setTimeout(() => {
|
||||
expect(clientSocket['closed']).to.equal(false);
|
||||
expect(clientSocket['isClosed']).to.equal(false);
|
||||
|
||||
clientSocket.end();
|
||||
server.close(done);
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
Socket,
|
||||
} from 'net';
|
||||
import { ERROR_EVENT } from '../../constants';
|
||||
import { JsonSocket } from '../../json-socket';
|
||||
import { JsonSocket } from '../../helpers/json-socket';
|
||||
|
||||
export const ip = '127.0.0.1';
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { CONNECT_EVENT, MESSAGE_EVENT } from '../../constants';
|
||||
import { JsonSocket } from '../../json-socket';
|
||||
import { JsonSocket } from '../../helpers/json-socket';
|
||||
import * as helpers from './helpers';
|
||||
import { expect } from 'chai';
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Socket } from 'net';
|
||||
import * as sinon from 'sinon';
|
||||
import { ERROR_EVENT, MESSAGE_EVENT } from '../../constants';
|
||||
import { JsonSocket } from '../../json-socket';
|
||||
import { JsonSocket } from '../../helpers/json-socket';
|
||||
import { expect } from 'chai';
|
||||
// tslint:disable:no-string-literal
|
||||
|
||||
|
||||
Reference in New Issue
Block a user