Skip to content
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

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

32 changes: 27 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "tipc",
"version": "2.0.3",
"version": "2.0.4",
"repository": {
"type": "git",
"url": "git+https://github.com/gikkman/tipc.git"
},
"homepage": "https://github.com/gikkman/tipc",
"bugs": "https://github.com/gikkman/tipc/issues",
"description": "A wrapper around websockets which allows using a mapped type as a contract. As long as the server and client shares this contract types, neither can send nor receive incorrect typed data.",
"description": "A wrapper around a websocket which allows using a mapped type as a contract. As long as the server and client shares this contract types, neither can send nor receive incorrect typed data.",
"author": {
"name": "Gikkman",
"url": "http://www.github.com/gikkman"
Expand All @@ -16,9 +16,31 @@
"module": "./dist/mjs/index.node.js",
"browser": "./dist/umd/index.browser.js",
"exports": {
"import": "./dist/mjs/index.node.js",
"require": "./dist/cjs/index.node.js",
"browser": "./dist/umd/index.browser.js"
".": {
"node": {
"types": "./dist/mjs/index.node.d.ts",
"import": "./dist/mjs/index.node.js",
"require": "./dist/cjs/index.node.js"
},
"default": {
"types": "./dist/umd/index.node.d.ts",
"import": "./dist/umd/index.browser.js"
}
},
"./cjs": {
"types": "./dist/cjs/index.node.d.ts",
"import": "./dist/cjs/index.node.js",
"require": "./dist/cjs/index.node.js"
},
"./mjs": {
"types": "./dist/mjs/index.node.d.ts",
"import": "./dist/mjs/index.node.js",
"require": "./dist/mjs/index.node.js"
},
"./browser": {
"types": "./dist/umd/index.browser.d.ts",
"import": "./dist/umd/index.browser.js"
}
},
"scripts": {
"lint": "eslint .",
Expand Down
79 changes: 53 additions & 26 deletions src/TipcBrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,57 @@ import { Callback,
TipcClient,
TipcConnectionManager,
TipcAddressInfo,
TipcConnectionDetails,
TipcClientOptions } from "./TipcTypes";

export class TipcBrowserClient implements TipcClient {
protected readonly logger: TipcLogger;
protected readonly host: string;
protected readonly port: number;
protected readonly path: string;
protected readonly protocol: string;
protected readonly tipcListenerComponent: TipcListenerComponent;
protected readonly url: string;
private readonly usedNamespaces = new Set<string>();
protected ws?: WebSocket;
private onDisconnectCallback?: Callback;

private constructor(options: TipcClientOptions) {
this.host = options.host;
this.port = options.port;
this.path = options.path ?? "";
this.protocol = options.protocol ?? "ws";
private constructor(options: TipcConnectionDetails&TipcClientOptions) {
if("url" in options) {
this.url = options.url;
}
else {
this.url = `${options.protocol??"ws"}://${options.host}:${options.port}${options.path??""}`;
}
this.onDisconnectCallback = options.onDisconnect;
this.logger = new TipcLogger({messagePrefix: "[Tipc Client]", ...options.loggerOptions});
this.tipcListenerComponent = new TipcListenerComponent(this.logger);
}

public static create(options: TipcClientOptions): TipcConnectionManager<TipcClient> {
public static create(options: TipcConnectionDetails&TipcClientOptions): TipcConnectionManager<TipcClient> {
const instance = new TipcBrowserClient(options);
return instance;
}

public getAddressInfo(): TipcAddressInfo {
return {address: this.host, port: this.port};
public static from(websocket: WebSocket, options?: TipcClientOptions) {
const instance = new TipcBrowserClient({url: websocket.url, ...options});
instance.ws = websocket;
instance.initWs(websocket);
return instance;
}

public getAddressInfo(): TipcAddressInfo | undefined {
const url = this.getUrl();
if(!url) {
return;
}
try {
const parsed = new URL(url);
return {address: parsed.hostname, port: parseInt(parsed.port)};
}
catch {
return undefined;
}
}

public getUrl(): string {
return this.url;
}

public isConnected(): boolean {
Expand All @@ -59,8 +80,7 @@ export class TipcBrowserClient implements TipcClient {
if(this.isConnected()) {
return this;
}
const url = `${this.protocol}://${this.host}:${this.port}${this.path}`;
this.ws = await this.initWs(url);
this.ws = await this.initWs(this.url);
return this;
}

Expand All @@ -81,9 +101,11 @@ export class TipcBrowserClient implements TipcClient {
});
}

private initWs(url: string) {
const ws = new WebSocket(url);
let hasBeenConnected = false;
private initWs(urlOrWebsocket: string|WebSocket) {
const ws = typeof urlOrWebsocket === "string"
? new WebSocket(urlOrWebsocket)
: urlOrWebsocket;
let hasBeenConnected = ws.readyState === WebSocket.OPEN;

ws.addEventListener('error', (ev) => {
this.logger.error('Error: %s', ev);
Expand Down Expand Up @@ -118,16 +140,21 @@ export class TipcBrowserClient implements TipcClient {
});

return new Promise<WebSocket>((resolve, reject) => {
const onError = (ev: Event) => {
reject(ev);
};
ws.addEventListener('error', onError);
ws.addEventListener('open', () => {
ws.removeEventListener('error', onError);
hasBeenConnected = true;
this.logger.info("Websocket connection established: %s", url);
if(ws.readyState === WebSocket.OPEN) {
resolve(ws);
});
}
else {
const onError = (ev: Event) => {
reject(ev);
};
ws.addEventListener('error', onError);
ws.addEventListener('open', () => {
ws.removeEventListener('error', onError);
hasBeenConnected = true;
this.logger.info("Websocket connection established: %s", urlOrWebsocket);
resolve(ws);
});
}
});
}

Expand Down
79 changes: 53 additions & 26 deletions src/TipcNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,57 @@ import { Callback,
TipcClient,
TipcConnectionManager,
TipcAddressInfo,
TipcConnectionDetails,
TipcClientOptions } from "./TipcTypes";

export class TipcNodeClient implements TipcClient {
protected readonly logger: TipcLogger;
protected readonly host: string;
protected readonly port: number;
protected readonly path: string;
protected readonly protocol: string;
protected readonly tipcListenerComponent: TipcListenerComponent;
protected readonly url: string;
private readonly usedNamespaces = new Set<string>();
protected ws?: WebSocket;
private onDisconnectCallback?: Callback;

private constructor(options: TipcClientOptions) {
this.host = options.host;
this.port = options.port;
this.path = options.path ?? "";
this.protocol = options.protocol ?? "ws";
private constructor(options: TipcConnectionDetails&TipcClientOptions) {
if("url" in options) {
this.url = options.url;
}
else {
this.url = `${options.protocol??"ws"}://${options.host}:${options.port}${options.path??""}`;
}
this.onDisconnectCallback = options.onDisconnect;
this.logger = new TipcLogger({messagePrefix: "[Tipc Client]", ...options.loggerOptions});
this.tipcListenerComponent = new TipcListenerComponent(this.logger);
}

public static create(options: TipcClientOptions): TipcConnectionManager<TipcClient> {
public static create(options: TipcConnectionDetails&TipcClientOptions): TipcConnectionManager<TipcClient> {
const instance = new TipcNodeClient(options);
return instance;
}

public getAddressInfo(): TipcAddressInfo {
return {address: this.host, port: this.port};
public static from(websocket: WebSocket, options?: TipcClientOptions) {
const instance = new TipcNodeClient({url: websocket.url, ...options});
instance.ws = websocket;
instance.initWs(websocket);
return instance;
}

public getAddressInfo(): TipcAddressInfo | undefined {
const url = this.getUrl();
if(!url) {
return;
}
try {
const parsed = new URL(url);
return {address: parsed.hostname, port: parseInt(parsed.port)};
}
catch {
return undefined;
}
}

public getUrl(): string {
return this.url;
}

public isConnected(): boolean {
Expand All @@ -61,8 +82,7 @@ export class TipcNodeClient implements TipcClient {
if(this.isConnected()) {
return this;
}
const url = `${this.protocol}://${this.host}:${this.port}${this.path}`;
this.ws = await this.initWs(url);
this.ws = await this.initWs(this.url);
return this;
}

Expand All @@ -83,9 +103,11 @@ export class TipcNodeClient implements TipcClient {
});
}

private initWs(url: string) {
const ws = new WebSocket(url);
let hasBeenConnected = false;
private initWs(urlOrWebsocket: string|WebSocket) {
const ws = typeof urlOrWebsocket === "string"
? new WebSocket(urlOrWebsocket, {perMessageDeflate: false})
: urlOrWebsocket;
let hasBeenConnected = ws.readyState === WebSocket.OPEN;

ws.on('error', (err) => {
this.logger.error('Error: %s', err.message);
Expand Down Expand Up @@ -120,16 +142,21 @@ export class TipcNodeClient implements TipcClient {
});

return new Promise<WebSocket>((resolve, reject) => {
const onError = (err: Error) => {
reject(err);
};
ws.on('error', onError);
ws.on('open', () => {
ws.off('error', onError);
hasBeenConnected = true;
this.logger.info("Websocket connection established: %s", url);
if(ws.readyState === WebSocket.OPEN) {
resolve(ws);
});
}
else {
const onError = (err: Error) => {
reject(err);
};
ws.on('error', onError);
ws.on('open', () => {
ws.off('error', onError);
hasBeenConnected = true;
this.logger.info("Websocket connection established: %s", urlOrWebsocket);
resolve(ws);
});
}
});
}

Expand Down
Loading