@d-fischer/connection
Advanced tools
Comparing version 6.7.0 to 7.0.0
import type { Logger } from '@d-fischer/logger'; | ||
import { EventEmitter } from '@d-fischer/typed-event-emitter'; | ||
import type { Connection, ConnectionInfo } from './Connection'; | ||
export declare type ConnectionOptions<T extends Connection> = T extends AbstractConnection<infer O> ? O : never; | ||
import type { Connection, ConnectionOptions } from './Connection'; | ||
export declare type InferConnectionOptions<T extends Connection> = T extends AbstractConnection<infer O> ? O : never; | ||
export declare abstract class AbstractConnection<Options = never> extends EventEmitter implements Connection { | ||
protected readonly _host: string; | ||
protected readonly _port: number; | ||
protected readonly _secure: boolean; | ||
private readonly _lineBased; | ||
@@ -15,3 +12,2 @@ protected readonly _logger?: Logger; | ||
protected _connected: boolean; | ||
protected _manualDisconnect: boolean; | ||
readonly onReceive: import("@d-fischer/typed-event-emitter").EventBinder<[string]>; | ||
@@ -21,6 +17,5 @@ readonly onConnect: import("@d-fischer/typed-event-emitter").EventBinder<[]>; | ||
readonly onEnd: import("@d-fischer/typed-event-emitter").EventBinder<[boolean, (Error | undefined)?]>; | ||
constructor({ hostName, port, secure, lineBased }: ConnectionInfo, logger?: Logger, additionalOptions?: Options); | ||
constructor({ lineBased, logger, additionalOptions }?: ConnectionOptions<Options>); | ||
get isConnecting(): boolean; | ||
get isConnected(): boolean; | ||
get host(): string; | ||
sendLine(line: string): void; | ||
@@ -33,3 +28,2 @@ abstract connect(): Promise<void>; | ||
abstract get hasSocket(): boolean; | ||
abstract get port(): number; | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
class AbstractConnection extends typed_event_emitter_1.EventEmitter { | ||
constructor({ hostName, port, secure, lineBased }, logger, additionalOptions) { | ||
constructor({ lineBased, logger, additionalOptions } = {}) { | ||
super(); | ||
@@ -12,3 +12,2 @@ this._currentLine = ''; | ||
this._connected = false; | ||
this._manualDisconnect = false; | ||
this.onReceive = this.registerEvent(); | ||
@@ -18,5 +17,2 @@ this.onConnect = this.registerEvent(); | ||
this.onEnd = this.registerEvent(); | ||
this._host = hostName; | ||
this._port = port; | ||
this._secure = secure !== null && secure !== void 0 ? secure : true; | ||
this._lineBased = lineBased !== null && lineBased !== void 0 ? lineBased : false; | ||
@@ -32,5 +28,2 @@ this._logger = logger; | ||
} | ||
get host() { | ||
return this._host; | ||
} | ||
sendLine(line) { | ||
@@ -37,0 +30,0 @@ if (this._connected) { |
export { AbstractConnection } from './AbstractConnection'; | ||
export type { ConnectionOptions } from './AbstractConnection'; | ||
export type { Connection, ConnectionInfo } from './Connection'; | ||
export type { InferConnectionOptions } from './AbstractConnection'; | ||
export type { Connection, ConnectionOptions, ConnectionTarget } from './Connection'; | ||
export { DirectConnection } from './DirectConnection-stub'; | ||
@@ -8,1 +8,2 @@ export { PersistentConnection } from './PersistentConnection'; | ||
export { WebSocketConnection } from './WebSocketConnection'; | ||
export type { ClientOptions as WebSocketClientOptions } from '@d-fischer/isomorphic-ws'; |
@@ -0,1 +1,2 @@ | ||
import type { Logger } from '@d-fischer/logger'; | ||
import type { EventBinder } from '@d-fischer/typed-event-emitter'; | ||
@@ -5,4 +6,2 @@ export interface Connection { | ||
readonly isConnected: boolean; | ||
readonly host: string; | ||
readonly port: number; | ||
readonly hasSocket: boolean; | ||
@@ -18,7 +17,12 @@ readonly onReceive: EventBinder<[string]>; | ||
} | ||
export interface ConnectionInfo { | ||
hostName: string; | ||
port: number; | ||
export interface ConnectionTarget { | ||
hostName?: string; | ||
port?: number; | ||
url?: string; | ||
secure?: boolean; | ||
} | ||
export interface ConnectionOptions<T> { | ||
lineBased?: boolean; | ||
logger?: Logger; | ||
additionalOptions?: T; | ||
} |
@@ -1,7 +0,5 @@ | ||
import type { Logger } from '@d-fischer/logger'; | ||
import { AbstractConnection } from './AbstractConnection'; | ||
import type { ConnectionInfo } from './Connection'; | ||
import type { ConnectionOptions, ConnectionTarget } from './Connection'; | ||
export declare class DirectConnection extends AbstractConnection { | ||
constructor(options: ConnectionInfo, logger?: Logger, additionalOptions?: never); | ||
get port(): number; | ||
constructor(target: ConnectionTarget, options: ConnectionOptions<never>); | ||
get hasSocket(): boolean; | ||
@@ -8,0 +6,0 @@ sendRaw(line: string): void; |
@@ -6,9 +6,6 @@ "use strict"; | ||
class DirectConnection extends AbstractConnection_1.AbstractConnection { | ||
constructor(options, logger, additionalOptions) { | ||
constructor(target, options) { | ||
throw new Error('DirectConnection is not implemented in a browser environment'); | ||
super(options, logger, additionalOptions); | ||
super(options); | ||
} | ||
get port() { | ||
return this._port; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/class-literal-property-style | ||
@@ -15,0 +12,0 @@ get hasSocket() { |
import { AbstractConnection } from './AbstractConnection'; | ||
import type { ConnectionOptions, ConnectionTarget } from './Connection'; | ||
export declare class DirectConnection extends AbstractConnection { | ||
private _socket; | ||
get port(): number; | ||
protected readonly _host: string; | ||
protected readonly _port: number; | ||
protected readonly _secure: boolean; | ||
constructor(target: ConnectionTarget, options?: ConnectionOptions<never>); | ||
get hasSocket(): boolean; | ||
@@ -6,0 +10,0 @@ sendRaw(line: string): void; |
@@ -8,9 +8,13 @@ "use strict"; | ||
class DirectConnection extends AbstractConnection_1.AbstractConnection { | ||
constructor() { | ||
super(...arguments); | ||
constructor(target, options) { | ||
var _a; | ||
super(options); | ||
this._socket = null; | ||
if (!target.hostName || !target.port) { | ||
throw new Error('DirectConnection requires hostName and port to be set'); | ||
} | ||
this._host = target.hostName; | ||
this._port = target.port; | ||
this._secure = (_a = target.secure) !== null && _a !== void 0 ? _a : true; | ||
} | ||
get port() { | ||
return this._port; | ||
} | ||
get hasSocket() { | ||
@@ -17,0 +21,0 @@ return !!this._socket; |
export { AbstractConnection } from './AbstractConnection'; | ||
export type { ConnectionOptions } from './AbstractConnection'; | ||
export type { Connection, ConnectionInfo } from './Connection'; | ||
export type { InferConnectionOptions } from './AbstractConnection'; | ||
export type { Connection, ConnectionOptions, ConnectionTarget } from './Connection'; | ||
export { DirectConnection } from './DirectConnection'; | ||
@@ -5,0 +5,0 @@ export { PersistentConnection } from './PersistentConnection'; |
@@ -1,15 +0,13 @@ | ||
import type { Logger } from '@d-fischer/logger'; | ||
import type { Constructor } from '@d-fischer/shared-utils'; | ||
import { EventEmitter } from '@d-fischer/typed-event-emitter'; | ||
import type { ConnectionOptions } from './AbstractConnection'; | ||
import type { Connection, ConnectionInfo } from './Connection'; | ||
export interface PersistentConnectionConfig { | ||
import type { InferConnectionOptions } from './AbstractConnection'; | ||
import type { Connection, ConnectionOptions, ConnectionTarget } from './Connection'; | ||
export interface PersistentConnectionConfig<T> extends ConnectionOptions<T> { | ||
retryLimit?: number; | ||
initialRetryLimit?: number; | ||
logger?: Logger; | ||
} | ||
export declare class PersistentConnection<T extends Connection> extends EventEmitter implements Connection { | ||
private readonly _type; | ||
private readonly _connectionInfo; | ||
private readonly _additionalOptions?; | ||
private readonly _target; | ||
private readonly _config; | ||
private readonly _retryLimit; | ||
@@ -26,7 +24,5 @@ private readonly _initialRetryLimit; | ||
readonly onEnd: import("@d-fischer/typed-event-emitter").EventBinder<[boolean, (Error | undefined)?]>; | ||
constructor(_type: Constructor<T>, _connectionInfo: ConnectionInfo, config?: PersistentConnectionConfig, _additionalOptions?: ConnectionOptions<T> | undefined); | ||
constructor(_type: Constructor<T>, _target: ConnectionTarget, _config?: PersistentConnectionConfig<InferConnectionOptions<T>>); | ||
get isConnected(): boolean; | ||
get isConnecting(): boolean; | ||
get host(): string; | ||
get port(): number; | ||
get hasSocket(): boolean; | ||
@@ -33,0 +29,0 @@ sendLine(line: string): void; |
@@ -7,8 +7,8 @@ "use strict"; | ||
class PersistentConnection extends typed_event_emitter_1.EventEmitter { | ||
constructor(_type, _connectionInfo, config = {}, _additionalOptions) { | ||
constructor(_type, _target, _config = {}) { | ||
var _a; | ||
super(); | ||
this._type = _type; | ||
this._connectionInfo = _connectionInfo; | ||
this._additionalOptions = _additionalOptions; | ||
this._target = _target; | ||
this._config = _config; | ||
this._retryLimit = Infinity; | ||
@@ -22,4 +22,4 @@ this._initialRetryLimit = 3; | ||
this.onEnd = this.registerEvent(); | ||
this._retryLimit = (_a = config.retryLimit) !== null && _a !== void 0 ? _a : Infinity; | ||
this._logger = config.logger; | ||
this._retryLimit = (_a = _config.retryLimit) !== null && _a !== void 0 ? _a : Infinity; | ||
this._logger = _config.logger; | ||
} | ||
@@ -34,8 +34,2 @@ get isConnected() { | ||
} | ||
get host() { | ||
return this._connectionInfo.hostName; | ||
} | ||
get port() { | ||
return this._connectionInfo.port; | ||
} | ||
get hasSocket() { | ||
@@ -81,3 +75,3 @@ var _a, _b; | ||
while (this._connectionRetryCount <= retryLimit) { | ||
const newConnection = (this._currentConnection = new this._type(this._connectionInfo, this._logger, this._additionalOptions)); | ||
const newConnection = (this._currentConnection = new this._type(this._target, this._config)); | ||
newConnection.onReceive(line => this.emit(this.onReceive, line)); | ||
@@ -116,3 +110,3 @@ newConnection.onConnect(() => this.emit(this.onConnect)); | ||
} | ||
await shared_utils_1.delay(secs * 1000); | ||
await (0, shared_utils_1.delay)(secs * 1000); | ||
(_d = this._logger) === null || _d === void 0 ? void 0 : _d.info(userGenerated ? 'Retrying connection' : 'Trying to reconnect'); | ||
@@ -119,0 +113,0 @@ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
/// <reference types="ws" /> | ||
import type { ClientOptions } from '@d-fischer/isomorphic-ws'; | ||
import { AbstractConnection } from './AbstractConnection'; | ||
import type { ConnectionOptions, ConnectionTarget } from './Connection'; | ||
export interface WebSocketConnectionOptions { | ||
@@ -9,3 +10,4 @@ wsOptions?: ClientOptions; | ||
private _socket; | ||
get port(): number; | ||
private readonly _url; | ||
constructor(target: ConnectionTarget, options: ConnectionOptions<WebSocketConnectionOptions>); | ||
get hasSocket(): boolean; | ||
@@ -12,0 +14,0 @@ sendRaw(line: string): void; |
@@ -7,9 +7,15 @@ "use strict"; | ||
class WebSocketConnection extends AbstractConnection_1.AbstractConnection { | ||
constructor() { | ||
super(...arguments); | ||
constructor(target, options) { | ||
super(options); | ||
this._socket = null; | ||
if (target.hostName && target.port) { | ||
this._url = `ws${target.secure ? 's' : ''}://${target.hostName}:${target.port}`; | ||
} | ||
else if (target.url) { | ||
this._url = target.url; | ||
} | ||
else { | ||
throw new Error('WebSocketConnection requires either hostName & port or url to be set'); | ||
} | ||
} | ||
get port() { | ||
return this._port; | ||
} | ||
get hasSocket() { | ||
@@ -28,4 +34,3 @@ return !!this._socket; | ||
this._connecting = true; | ||
const url = `ws${this._secure ? 's' : ''}://${this._host}:${this.port}`; | ||
this._socket = new isomorphic_ws_1.WebSocket(url, (_a = this._additionalOptions) === null || _a === void 0 ? void 0 : _a.wsOptions); | ||
this._socket = new isomorphic_ws_1.WebSocket(this._url, (_a = this._additionalOptions) === null || _a === void 0 ? void 0 : _a.wsOptions); | ||
this._socket.onopen = () => { | ||
@@ -32,0 +37,0 @@ var _a; |
{ | ||
"name": "@d-fischer/connection", | ||
"version": "6.7.0", | ||
"version": "7.0.0", | ||
"description": "Abstraction for packet-based connections.", | ||
@@ -28,18 +28,18 @@ "keywords": [], | ||
"@d-fischer/isomorphic-ws": "^7.0.0", | ||
"@d-fischer/logger": "^4.0.0", | ||
"@d-fischer/shared-utils": "^3.0.1", | ||
"@d-fischer/logger": "^4.2.0", | ||
"@d-fischer/shared-utils": "^3.3.0", | ||
"@d-fischer/typed-event-emitter": "^3.3.0", | ||
"@types/node": "^16.7.10", | ||
"@types/ws": "^8.2.0", | ||
"tslib": "^2.0.3", | ||
"ws": "^8.2.2" | ||
"@types/ws": "^8.5.3", | ||
"tslib": "^2.4.1", | ||
"ws": "^8.11.0" | ||
}, | ||
"devDependencies": { | ||
"@d-fischer/eslint-config": "^5.0.0", | ||
"eslint": "^7.17.0", | ||
"@d-fischer/eslint-config": "^6.1.4", | ||
"eslint": "^8.27.0", | ||
"husky": "^4.3.6", | ||
"lint-staged": "^11.1.2", | ||
"prettier": "^2.1.2", | ||
"tsukuru": "^0.7.2", | ||
"typescript": "~4.3.5" | ||
"lint-staged": "^13.0.3", | ||
"prettier": "^2.7.1", | ||
"tsukuru": "^0.7.4", | ||
"typescript": "~4.6.4" | ||
}, | ||
@@ -46,0 +46,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71955
961
Updated@d-fischer/logger@^4.2.0
Updated@types/ws@^8.5.3
Updatedtslib@^2.4.1
Updatedws@^8.11.0