-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
332 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export abstract class AbstractConfigParser<R> { | ||
protected constructor(protected readonly configs: Partial<R>) { } | ||
|
||
protected validate<T extends keyof R>(lookup: string, found: string | R[T]) { | ||
if (!found && found !== false) { | ||
throw TypeError(`A value for [${lookup}] is required`) | ||
} | ||
|
||
return found | ||
} | ||
|
||
protected get<T extends keyof R>( | ||
key: T, | ||
fallback: R[T] = undefined, | ||
) { | ||
const lookup: string = key as string | ||
const envLookup = lookup.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toUpperCase() | ||
|
||
const found = this.configs[key] ?? process.env[`VITREA_VBOX_${envLookup}`] ?? fallback | ||
|
||
return this.validate(lookup, found) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { AbstractConfigParser } from './AbstractConfigParser' | ||
|
||
export interface ConnectionConfigs { | ||
host: string | ||
port: number | ||
username: string | ||
password: string | ||
version: ProtocolVersion | ||
} | ||
|
||
export const ProtocolVersion = { | ||
V1: 'v1', | ||
V2: 'v2' | ||
} as const | ||
|
||
export type ProtocolVersion = typeof ProtocolVersion[keyof typeof ProtocolVersion] | ||
|
||
export class ConnectionConfigParser extends AbstractConfigParser<ConnectionConfigs> { | ||
public static create(configs: Partial<ConnectionConfigs> = {}): ConnectionConfigs { | ||
const instance = new this(configs) | ||
|
||
return { | ||
host: instance.get('host', '192.168.1.23'), | ||
port: Number(instance.get('port', 11501)), | ||
username: instance.get('username'), | ||
password: instance.get('password'), | ||
version: instance.get('version', ProtocolVersion.V2) as ProtocolVersion, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { SocketConfigParser } from './SocketConfigParser' | ||
import { ConsoleLogger, NullLogger } from '../core' | ||
|
||
|
||
describe('SocketConfigParser', () => { | ||
const env = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
process.env = { ...env } | ||
}) | ||
|
||
afterEach(() => process.env = env) | ||
|
||
it('[create] has default values', () => { | ||
const configs = SocketConfigParser.create() | ||
|
||
expect(configs.log).toBeInstanceOf(NullLogger) | ||
expect(configs.socketSupplier).toBeInstanceOf(Function) | ||
expect(configs.shouldReconnect).toBeTruthy() | ||
expect(configs.requestTimeout).toBe(1000) | ||
}) | ||
|
||
it('[create] uses environment variables when available', () => { | ||
process.env.VITREA_VBOX_REQUEST_TIMEOUT = '2000' | ||
process.env.VITREA_VBOX_SHOULD_RECONNECT = 'false' | ||
|
||
const configs = SocketConfigParser.create() | ||
|
||
expect(configs.shouldReconnect).toBeFalsy() | ||
expect(configs.requestTimeout).toBe(2000) | ||
}) | ||
|
||
it('[create] uses parameters when available', () => { | ||
const supplier = jest.fn() | ||
|
||
const configs = SocketConfigParser.create({ | ||
socketSupplier: supplier, | ||
shouldReconnect: false, | ||
requestTimeout: 2000, | ||
log: new ConsoleLogger() | ||
}) | ||
|
||
expect(configs.log).toBeInstanceOf(ConsoleLogger) | ||
expect(configs.socketSupplier).toBe(supplier) | ||
expect(configs.shouldReconnect).toBeFalsy() | ||
expect(configs.requestTimeout).toBe(2000) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { AbstractConfigParser } from './AbstractConfigParser' | ||
import { LoggerContract, NullLogger } from '../core' | ||
import * as Net from 'net' | ||
|
||
export interface SocketConfigs { | ||
log: LoggerContract, | ||
shouldReconnect: boolean | ||
requestTimeout: number | ||
|
||
socketSupplier(): Net.Socket | ||
} | ||
|
||
export class SocketConfigParser extends AbstractConfigParser<SocketConfigs> { | ||
public toBoolean(value: string | boolean): boolean { | ||
return ['1', 'true', 'TRUE', true].includes(value) | ||
} | ||
|
||
public static create(configs: Partial<SocketConfigs> = {}): Required<SocketConfigs> { | ||
const instance = new this(configs) | ||
|
||
return { | ||
log: instance.configs.log ?? new NullLogger(), | ||
socketSupplier: instance.configs.socketSupplier ?? (() => new Net.Socket()), | ||
shouldReconnect: instance.toBoolean(instance.get('shouldReconnect', true)), | ||
requestTimeout: Number(instance.get('requestTimeout', 1000)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { SocketConfigs, SocketConfigParser } from './SocketConfigParser' | ||
export { | ||
ConnectionConfigs, | ||
ConnectionConfigParser, | ||
ProtocolVersion | ||
} from './ConnectionConfigParser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.