Skip to content

Commit

Permalink
update config and url
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
mahsumurebe authored Nov 7, 2022
2 parents 7b24f28 + ee33bcb commit a6e1ea7
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 126 deletions.
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ It should not create a JRPCClient instance.
import { JRPCClient, HttpAdapter } from '@mahsumurebe/jrpc-client';

// Create instance
const clientInstance = new JRPCClient(new HttpAdapter({
hostname: "localhost",
port: 3000
}));
const clientInstance = new JRPCClient(new HttpAdapter('http://localhost:3000'));
// Call start method for connection
await clientInstance.start();
```
Expand Down Expand Up @@ -109,10 +106,7 @@ HTTP Adapter is used to connect to JRPC Servers served over HTTP Protocol.
// Adapter Instance
import {JRPCClient, HttpAdapter} from '@mahsumurebe/jrpc-client';

const adapter = new HttpAdapter({
hostname: "localhost",
port: 3000
});
const adapter = new HttpAdapter('http://localhost:3000');

// Client Instance
const clientInstance = new JRPCClient(adapter);
Expand All @@ -139,10 +133,7 @@ Websocket Adapter is used to connect to JRPC Servers served over Websocket Proto
// Adapter Instance
import { JRPCClient, WebsocketAdapter } from '@mahsumurebe/jrpc-client';

const adapter = new WebsocketAdapter({
hostname: "localhost",
port: 3000
});
const adapter = new WebsocketAdapter('ws:/localhost:3000');

// Client Instance
const clientInstance = new JRPCClient(adapter);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mahsumurebe/jrpc-client",
"version": "2.0.0",
"version": "2.0.1",
"description": "JSONRPC 2.0 NodeJS Client written in TypeScript",
"main": "lib/index.js",
"scripts": {
Expand Down
38 changes: 17 additions & 21 deletions src/adapters/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// noinspection JSUnusedGlobalSymbols

import * as URL from "url";
import fetch, { RequestInit } from "node-fetch";
import { HttpAdapterConfigInterface } from "./interfaces";
import {
Expand All @@ -12,6 +13,7 @@ import {
TypeMethodParam,
timeout,
} from "../../core";
import { WebsocketAdapterConfigInterface } from "../websocket";

const debug = require("debug")("jrpc:client:adapters:http");

Expand All @@ -27,15 +29,15 @@ export * from "./interfaces";
* @example
* // Create adapter for HTTP Connection
* const adapter = new HttpAdapter({
* schema: "http",
* protocol: "http",
* hostname: "localhost",
* port: 3000,
* });
*
* @example
* // Create adapter for HTTPs Connection
* const adapter = new HttpAdapter({
* schema: "https",
* protocol: "https",
* hostname: "foo.bar",
* port: 443,
* });
Expand All @@ -50,29 +52,24 @@ export class HttpAdapter<
*/
private readonly fetchConfig: RequestInit;

/**
* Server URL
*
* @private
*/
private readonly url: string;

constructor(protected readonly config: HttpAdapterConfigInterface) {
constructor(
protected readonly url: string,
protected readonly config?: HttpAdapterConfigInterface
) {
super();

debug("initialize");
const defaultConfig: Partial<HttpAdapterConfigInterface> = {
schema: "http",
pathname: "/",
timeout: 10 * 1000,
};
this.config = {
...(defaultConfig as HttpAdapterConfigInterface),
timeout: 10 * 1000,
...(this.config ?? {}),
} as HttpAdapterConfigInterface;
} as WebsocketAdapterConfigInterface;

const parsedUrl = URL.parse(this.url);

if (["wss", "wss"].indexOf(parsedUrl.protocol) > -1) {
throw new Error(`${parsedUrl.protocol} does not supported.`);
}

const url = new URL(`${this.config.schema}://${this.config.hostname}`);
url.pathname = this.config.pathname ?? "/";
url.port = this.config.port.toString();
const headers = { ...this.config.headers };
const headerKeys = Object.keys(headers);
if (
Expand All @@ -85,7 +82,6 @@ export class HttpAdapter<
headers,
method: "POST",
};
this.url = url.toString();
}

/**
Expand Down
27 changes: 0 additions & 27 deletions src/adapters/http/interfaces/http-adapter.config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@
* @interface
*/
export interface HttpAdapterConfigInterface {
/**
* Schema
*
* @default http
* @type {string}
*/
schema?: "http" | "https";
/**
* Hostname
*
* @type {string}
*/
hostname: string;
/**
* Port
*
* @default 80
* @type {?string}
*/
port?: number;
/**
* Pathname
*
* @default /
* @type {?string}
*/
pathname?: string;
/**
* Headers
*
Expand Down
39 changes: 24 additions & 15 deletions src/adapters/websocket/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WebSocket } from "ws";
import * as URL from "url";
import {
AdapterAbstract,
JRPCExceptionAbstract,
Expand Down Expand Up @@ -32,15 +33,15 @@ type TypeQueue = {
* @example
* // Create adapter for WS Connection
* const adapter = new WebSocket({
* schema: "ws",
* protocol: "ws",
* hostname: "localhost",
* port: 3000,
* });
*
* @example
* // Create adapter for WSs Connection
* const adapter = new WebSocket({
* schema: "wss",
* protocol: "wss",
* hostname: "foo.bar",
* port: 443,
* });
Expand All @@ -56,18 +57,30 @@ export class WebsocketAdapter<
*/
protected handler: WebSocket;

/**
* Request Queue
*
* @type {object}
* @protected
*/
protected requestQueue: TypeQueue = {};

constructor(protected readonly config: WebsocketAdapterConfigInterface) {
constructor(
protected readonly url: string,
protected readonly config?: WebsocketAdapterConfigInterface
) {
super();
debug("initialize");
this.config = {
schema: "ws",
pathname: "/",
port: 80,
timeout: 10 * 1000,
...(this.config ?? {}),
} as WebsocketAdapterConfigInterface;

const parsedUrl = URL.parse(this.url);

if (["wss", "wss"].indexOf(parsedUrl.protocol) > -1) {
throw new Error(`${parsedUrl.protocol} does not supported.`);
}
}

/**
Expand All @@ -94,14 +107,14 @@ export class WebsocketAdapter<
*/
connect(): Promise<void> {
debug("create websocket handler");
const url = new URL(`${this.config.schema}://${this.config.hostname}`);
url.pathname = this.config.pathname ?? "/";
url.port = this.config.port.toString();
let openFn: () => void;
let errFn: (err: Error) => void;
return new Promise<void>((resolve, reject) => {
debug("connecting to", url.toString());
this.handler = new WebSocket(url.toString());
debug("connecting to", this.url.toString());
const headers = { ...this.config.headers };
this.handler = new WebSocket(this.url, {
headers,
});
openFn = () => {
debugHandler("websocket open");
resolve();
Expand All @@ -125,10 +138,6 @@ export class WebsocketAdapter<
debugHandler("websocket new response", body);
this.processMessage(body);
});
}).finally(() => {
debug("remove listener");
this.handler.removeListener("open", openFn);
this.handler.removeListener("error", errFn);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,15 @@
* @licence MIT
* @interface
*/
export interface WebsocketAdapterConfigInterface {
import { HttpAdapterConfigInterface } from "../../http";

export interface WebsocketAdapterConfigInterface
extends Omit<HttpAdapterConfigInterface, "protocol"> {
/**
* Schema
* protocol
*
* @default ws
* @type {string}
*/
schema?: "ws" | "wss";
/**
* Hostname
*
* @type {string}
*/
hostname: string;
/**
* Port
*
* @default 80
* @type {?string}
*/
port?: number;
/**
* Pathname
*
* @default /
* @type {?string}
*/
pathname?: string;
/**
* Headers
*
* @type {?object}
*/
headers?: Record<string, string>;

/**
* Timeout
*
* @default 10000
* @type {?number}
*/
timeout?: number;
protocol?: "ws" | "wss";
}
12 changes: 4 additions & 8 deletions tests/jrpc-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ describe("JRPCClient", () => {
};

const clientConfig: Client.HttpAdapterConfigInterface = {
hostname: serverConfig.hostname,
port: serverConfig.port,
schema: "http",
timeout: 2_000,
};

server = await generateJRPCServer(new Server.HttpAdapter(serverConfig));
client = await generateJRPCClient(new Client.HttpAdapter(clientConfig));
client = await generateJRPCClient(
new Client.HttpAdapter("http://localhost:3000/", clientConfig)
);
});
describe("call", () => {
it("should be return response", async () => {
Expand Down Expand Up @@ -202,17 +201,14 @@ describe("JRPCClient", () => {
};

const clientConfig: Client.WebsocketAdapterConfigInterface = {
hostname: serverConfig.hostname,
port: serverConfig.port,
schema: "ws",
timeout: 2_000,
};

server = await generateJRPCServer(
new Server.WebsocketAdapter(serverConfig)
);
client = await generateJRPCClient(
new Client.WebsocketAdapter(clientConfig)
new Client.WebsocketAdapter("ws://localhost:3000/", clientConfig)
);
});
describe("call", () => {
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "./src"
},
"exclude": [
"node_modules",
"tests"
Expand Down
9 changes: 4 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"module": "commonjs",
"allowSyntheticDefaultImports": true,
"declaration": true,
"baseUrl": "./",
"rootDir": "./src",
"rootDir": "./",
"outDir": "./lib",
"sourceMap": true,
"esModuleInterop": true
},
"include": [
"src/**/*"
"src/**/*",
"tests/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
"node_modules"
],
"ts-node": {
"require": [
Expand Down

0 comments on commit a6e1ea7

Please sign in to comment.