Skip to content

Commit

Permalink
refactor(microservices) remove unecessary options object
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Sep 2, 2018
1 parent fde2041 commit b6fe9b0
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 61 deletions.
4 changes: 4 additions & 0 deletions bundle/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"version": "5.3.0",
"description": "Nest - modern, fast, powerful node.js web framework (@common)",
"author": "Kamil Mysliwiec",
"repository": {
"type": "git",
"url": "https://github.com/nestjs/nest"
},
"license": "MIT",
"dependencies": {
"axios": "0.17.1",
Expand Down
8 changes: 8 additions & 0 deletions bundle/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"description": "Nest - modern, fast, powerful node.js web framework (@core)",
"author": "Kamil Mysliwiec",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/nestjs/nest"
},
"scripts": {
"postinstall": "opencollective postinstall"
},
"dependencies": {
"body-parser": "1.18.3",
"cors": "2.8.4",
Expand All @@ -12,6 +19,7 @@
"iterare": "0.0.8",
"object-hash": "1.3.0",
"optional": "0.1.4",
"opencollective": "1.0.3",
"path-to-regexp": "2.2.1"
},
"peerDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions bundle/microservices/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"description": "Nest - modern, fast, powerful node.js web framework (@microservices)",
"author": "Kamil Mysliwiec",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/nestjs/nest"
},
"dependencies": {
"iterare": "0.0.8",
"json-socket": "^0.2.1",
Expand Down
4 changes: 4 additions & 0 deletions bundle/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"description": "Nest - modern, fast, powerful node.js web framework (@testing)",
"author": "Kamil Mysliwiec",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/nestjs/nest"
},
"dependencies": {
"deprecate": "1.0.0",
"optional": "0.1.4"
Expand Down
4 changes: 4 additions & 0 deletions bundle/websockets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"description": "Nest - modern, fast, powerful node.js web framework (@websockets)",
"author": "Kamil Mysliwiec",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/nestjs/nest"
},
"dependencies": {
"iterare": "0.0.8",
"socket.io": "^2.1.1"
Expand Down
22 changes: 12 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"prettier **/**/*.ts --ignore-path ./.prettierignore --write && git status",
"build": "gulp build && gulp move",
"build:lib": "gulp build --dist bundle",
"postinstall": "opencollective postinstall",
"copy-docs": "gulp copy-docs",
"prepare": "npm run build:lib && npm run copy-docs",
"prepare:rc": "npm run build:lib && npm run copy-docs",
Expand Down Expand Up @@ -129,8 +130,7 @@
},
"collective": {
"type": "opencollective",
"url": "https://opencollective.com/nest",
"logo": "https://opencollective.com/opencollective/logo.txt"
"url": "https://opencollective.com/nest"
},
"nyc": {
"include": ["packages/**/*.ts"],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class NestApplication extends NestApplicationContext
}

public isMiddlewareApplied(httpAdapter: HttpServer, name: string): boolean {
const app = this.httpAdapter.getInstance();
const app = httpAdapter.getInstance();
return (
!!app._router &&
!!app._router.stack &&
Expand Down
13 changes: 9 additions & 4 deletions packages/microservices/client/client-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
private readonly url: string;
private grpcClient: any;

constructor(private readonly options: ClientOptions) {
constructor(private readonly options: ClientOptions['options']) {
super();
this.url =
this.getOptionsProp<GrpcOptions>(options, 'url') || GRPC_DEFAULT_URL;

grpcPackage = loadPackage('grpc', ClientGrpcProxy.name);
grpcProtoLoaderPackage = loadPackage('@grpc/proto-loader', ClientGrpcProxy.name);
grpcProtoLoaderPackage = loadPackage(
'@grpc/proto-loader',
ClientGrpcProxy.name,
);
this.grpcClient = this.createClient();
}

public getService<T extends {}>(name: string): T {
const { options } = this.options as GrpcOptions;
const options = this.options as any;
if (!this.grpcClient[name]) {
throw new InvalidGrpcServiceException();
}
Expand Down Expand Up @@ -128,7 +131,9 @@ export class ClientGrpcProxy extends ClientProxy implements ClientGrpc {
const loader = this.getOptionsProp<GrpcOptions>(this.options, 'loader');

const packageDefinition = grpcProtoLoaderPackage.loadSync(file, loader);
const packageObject = grpcPackage.loadPackageDefinition(packageDefinition);
const packageObject = grpcPackage.loadPackageDefinition(
packageDefinition,
);
return packageObject;
} catch (err) {
const invalidProtoError = new InvalidProtoDefinitionException();
Expand Down
4 changes: 2 additions & 2 deletions packages/microservices/client/client-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ClientMqtt extends ClientProxy {
private readonly url: string;
private mqttClient: MqttClient;

constructor(private readonly options: ClientOptions) {
constructor(private readonly options: ClientOptions['options']) {
super();
this.url =
this.getOptionsProp<MqttOptions>(this.options, 'url') || MQTT_DEFAULT_URL;
Expand Down Expand Up @@ -44,7 +44,7 @@ export class ClientMqtt extends ClientProxy {
}

public createClient(): MqttClient {
return mqttPackage.connect(this.url, this.options.options as MqttOptions);
return mqttPackage.connect(this.url, this.options as MqttOptions);
}

public handleError(client: MqttClient) {
Expand Down
4 changes: 2 additions & 2 deletions packages/microservices/client/client-nats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ClientNats extends ClientProxy {
private readonly url: string;
private natsClient: Client;

constructor(private readonly options: ClientOptions) {
constructor(private readonly options: ClientOptions['options']) {
super();
this.url =
this.getOptionsProp<NatsOptions>(this.options, 'url') || NATS_DEFAULT_URL;
Expand All @@ -36,7 +36,7 @@ export class ClientNats extends ClientProxy {
}

public createClient(): Promise<Client> {
const options: any = this.options.options || ({} as NatsOptions);
const options: any = this.options || ({} as NatsOptions);
return natsPackage.connect({
...options,
url: this.url,
Expand Down
16 changes: 8 additions & 8 deletions packages/microservices/client/client-proxy-factory.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ClientTCP } from './client-tcp';
import { ClientRedis } from './client-redis';
import { ClientOptions } from '../interfaces/client-metadata.interface';
import { Transport } from '../enums/transport.enum';
import { ClientProxy } from './client-proxy';
import { ClientOptions } from '../interfaces/client-metadata.interface';
import { Closeable } from '../interfaces/closeable.interface';
import { ClientNats } from './client-nats';
import { ClientMqtt } from './client-mqtt';
import { ClientGrpcProxy } from './client-grpc';
import { ClientMqtt } from './client-mqtt';
import { ClientNats } from './client-nats';
import { ClientProxy } from './client-proxy';
import { ClientRedis } from './client-redis';
import { ClientTCP } from './client-tcp';

export class ClientProxyFactory {
public static create(options: ClientOptions): ClientProxy & Closeable {
const { transport } = options;
public static create(clientOptions: ClientOptions): ClientProxy & Closeable {
const { transport, options } = clientOptions;
switch (transport) {
case Transport.REDIS:
return new ClientRedis(options);
Expand Down
6 changes: 3 additions & 3 deletions packages/microservices/client/client-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { isNil, isString } from '@nestjs/common/utils/shared.utils';
import {
defer,
Expand All @@ -16,7 +17,6 @@ import {
ReadPacket,
WritePacket,
} from '../interfaces';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';

export abstract class ClientProxy {
public abstract connect(): Promise<any>;
Expand Down Expand Up @@ -78,11 +78,11 @@ export abstract class ClientProxy {
}

protected getOptionsProp<T extends { options? }>(
obj: ClientOptions,
obj: ClientOptions['options'],
prop: keyof T['options'],
defaultValue = undefined,
) {
return obj && obj.options ? obj.options[prop as any] : defaultValue;
return obj ? obj[prop as any] : defaultValue;
}

protected normalizePattern<T = any>(pattern: T): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/client/client-redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ClientRedis extends ClientProxy {
private subClient: RedisClient;
private isExplicitlyTerminated = false;

constructor(private readonly options: ClientOptions) {
constructor(private readonly options: ClientOptions['options']) {
super();
this.url =
this.getOptionsProp<RedisOptions>(options, 'url') || REDIS_DEFAULT_URL;
Expand Down
15 changes: 12 additions & 3 deletions packages/microservices/client/client-tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import { Logger } from '@nestjs/common';
import * as JsonSocket from 'json-socket';
import * as net from 'net';
import { tap } from 'rxjs/operators';
import { CLOSE_EVENT, ERROR_EVENT, MESSAGE_EVENT, TCP_DEFAULT_HOST, TCP_DEFAULT_PORT } from '../constants';
import {
CLOSE_EVENT,
ERROR_EVENT,
MESSAGE_EVENT,
TCP_DEFAULT_HOST,
TCP_DEFAULT_PORT,
} from '../constants';
import { PacketId, ReadPacket, WritePacket } from '../interfaces';
import { ClientOptions, TcpClientOptions } from '../interfaces/client-metadata.interface';
import {
ClientOptions,
TcpClientOptions,
} from '../interfaces/client-metadata.interface';
import { ClientProxy } from './client-proxy';
import { ECONNREFUSED } from './constants';

Expand All @@ -15,7 +24,7 @@ export class ClientTCP extends ClientProxy {
private isConnected = false;
private socket: JsonSocket;

constructor(options: ClientOptions) {
constructor(options: ClientOptions['options']) {
super();
this.port =
this.getOptionsProp<TcpClientOptions>(options, 'port') ||
Expand Down
4 changes: 2 additions & 2 deletions packages/microservices/server/server-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { ServerTCP } from './server-tcp';

export class ServerFactory {
public static create(
options: MicroserviceOptions,
microserviceOptions: MicroserviceOptions,
): Server & CustomTransportStrategy {
const { transport } = options as any;
const { transport, options } = microserviceOptions as any;
switch (transport) {
case Transport.REDIS:
return new ServerRedis(options);
Expand Down
25 changes: 17 additions & 8 deletions packages/microservices/server/server-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { CANCEL_EVENT, GRPC_DEFAULT_URL } from '../constants';
import { InvalidGrpcPackageException } from '../exceptions/errors/invalid-grpc-package.exception';
import { InvalidProtoDefinitionException } from '../exceptions/errors/invalid-proto-definition.exception';
import { CustomTransportStrategy } from '../interfaces';
import { GrpcOptions, MicroserviceOptions } from '../interfaces/microservice-configuration.interface';
import {
GrpcOptions,
MicroserviceOptions,
} from '../interfaces/microservice-configuration.interface';
import { Server } from './server';

let grpcPackage: any = {};
Expand All @@ -14,13 +17,16 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
private readonly url: string;
private grpcClient: any;

constructor(private readonly options: MicroserviceOptions) {
constructor(private readonly options: MicroserviceOptions['options']) {
super();
this.url =
this.getOptionsProp<GrpcOptions>(options, 'url') || GRPC_DEFAULT_URL;

grpcPackage = this.loadPackage('grpc', ServerGrpc.name);
grpcProtoLoaderPackage = this.loadPackage('@grpc/proto-loader', ServerGrpc.name);
grpcProtoLoaderPackage = this.loadPackage(
'@grpc/proto-loader',
ServerGrpc.name,
);
}

public async listen(callback: () => void) {
Expand Down Expand Up @@ -107,9 +113,9 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
return async (call, callback) => {
const handler = methodHandler(call.request, call.metadata);
const result$ = this.transformToObservable(await handler);
await result$.pipe(
takeUntil(fromEvent(call, CANCEL_EVENT)),
).forEach(data => call.write(data));
await result$
.pipe(takeUntil(fromEvent(call, CANCEL_EVENT)))
.forEach(data => call.write(data));
call.end();
};
}
Expand Down Expand Up @@ -155,11 +161,14 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
const loader = this.getOptionsProp<GrpcOptions>(this.options, 'loader');

const packageDefinition = grpcProtoLoaderPackage.loadSync(file, loader);
const packageObject = grpcPackage.loadPackageDefinition(packageDefinition);
const packageObject = grpcPackage.loadPackageDefinition(
packageDefinition,
);
return packageObject;
} catch (err) {
const invalidProtoError = new InvalidProtoDefinitionException();
const message = err && err.message ? err.message : invalidProtoError.message;
const message =
err && err.message ? err.message : invalidProtoError.message;

this.logger.error(message, invalidProtoError.stack);
throw invalidProtoError;
Expand Down
Loading

0 comments on commit b6fe9b0

Please sign in to comment.