feat: lots more improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+281
-160
@@ -9,13 +9,50 @@ import type {
|
||||
ServiceLocationSocketSplit,
|
||||
ServiceLocationFile
|
||||
} from '@/types/service';
|
||||
import { MutationProxy } from './mutation-proxy';
|
||||
import { clonePlain } from './clone-plain';
|
||||
|
||||
/**
|
||||
* Base Location class
|
||||
*/
|
||||
export abstract class Location {
|
||||
abstract toJson(): ServiceLocation;
|
||||
export abstract class Location<T extends ServiceLocation = ServiceLocation> {
|
||||
protected _original: T;
|
||||
protected _mutated: Partial<T>;
|
||||
protected _mutationProxy: MutationProxy<T>;
|
||||
protected _data: T;
|
||||
|
||||
protected constructor(initial: T) {
|
||||
this._original = clonePlain(initial);
|
||||
this._mutated = {};
|
||||
this._mutationProxy = new MutationProxy<T>(() => this._original, () => this._mutated);
|
||||
this._data = this._mutationProxy.create();
|
||||
}
|
||||
|
||||
protected load(data: T): this {
|
||||
this._original = clonePlain(data);
|
||||
this._mutated = {};
|
||||
this._data = this._mutationProxy.create();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): T;
|
||||
toJson(delta: true): Partial<T>;
|
||||
toJson(delta?: boolean): T | Partial<T> {
|
||||
if (delta) {
|
||||
return clonePlain(this._mutated);
|
||||
}
|
||||
|
||||
return {
|
||||
...clonePlain(this._original),
|
||||
...clonePlain(this._mutated),
|
||||
};
|
||||
}
|
||||
|
||||
abstract clone(): Location;
|
||||
|
||||
mutated(): boolean {
|
||||
return Reflect.ownKeys(this._mutated).length > 0;
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocation): Location {
|
||||
switch (data.type) {
|
||||
@@ -37,14 +74,7 @@ export abstract class Location {
|
||||
* URI-based service location for API and web services
|
||||
* Used by: JMAP, Gmail API, etc.
|
||||
*/
|
||||
export class LocationUri extends Location {
|
||||
readonly type = 'URI' as const;
|
||||
scheme: string;
|
||||
host: string;
|
||||
port: number;
|
||||
path?: string;
|
||||
verifyPeer: boolean;
|
||||
verifyHost: boolean;
|
||||
export class LocationUri extends Location<ServiceLocationUri> {
|
||||
|
||||
constructor(
|
||||
scheme: string = 'https',
|
||||
@@ -54,36 +84,19 @@ export class LocationUri extends Location {
|
||||
verifyPeer: boolean = true,
|
||||
verifyHost: boolean = true
|
||||
) {
|
||||
super();
|
||||
this.scheme = scheme;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
this.verifyPeer = verifyPeer;
|
||||
this.verifyHost = verifyHost;
|
||||
super({
|
||||
type: 'URI',
|
||||
scheme,
|
||||
host,
|
||||
port,
|
||||
...(path !== undefined && { path }),
|
||||
verifyPeer,
|
||||
verifyHost,
|
||||
});
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationUri): LocationUri {
|
||||
return new LocationUri(
|
||||
data.scheme,
|
||||
data.host,
|
||||
data.port,
|
||||
data.path,
|
||||
data.verifyPeer ?? true,
|
||||
data.verifyHost ?? true
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationUri {
|
||||
return {
|
||||
type: this.type,
|
||||
scheme: this.scheme,
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
...(this.path && { path: this.path }),
|
||||
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
|
||||
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||
};
|
||||
return new LocationUri().load(data);
|
||||
}
|
||||
|
||||
getUrl(): string {
|
||||
@@ -92,28 +105,68 @@ export class LocationUri extends Location {
|
||||
}
|
||||
|
||||
clone(): LocationUri {
|
||||
return new LocationUri(
|
||||
this.scheme,
|
||||
this.host,
|
||||
this.port,
|
||||
this.path,
|
||||
this.verifyPeer,
|
||||
this.verifyHost
|
||||
);
|
||||
return LocationUri.fromJson(structuredClone(this.toJson()));
|
||||
}
|
||||
|
||||
get type(): 'URI' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get scheme(): string {
|
||||
return this._data.scheme;
|
||||
}
|
||||
|
||||
set scheme(value: string) {
|
||||
this._data.scheme = value;
|
||||
}
|
||||
|
||||
get host(): string {
|
||||
return this._data.host;
|
||||
}
|
||||
|
||||
set host(value: string) {
|
||||
this._data.host = value;
|
||||
}
|
||||
|
||||
get port(): number {
|
||||
return this._data.port;
|
||||
}
|
||||
|
||||
set port(value: number) {
|
||||
this._data.port = value;
|
||||
}
|
||||
|
||||
get path(): string | undefined {
|
||||
return this._data.path;
|
||||
}
|
||||
|
||||
set path(value: string | undefined) {
|
||||
this._data.path = value;
|
||||
}
|
||||
|
||||
get verifyPeer(): boolean {
|
||||
return this._data.verifyPeer ?? true;
|
||||
}
|
||||
|
||||
set verifyPeer(value: boolean) {
|
||||
this._data.verifyPeer = value;
|
||||
}
|
||||
|
||||
get verifyHost(): boolean {
|
||||
return this._data.verifyHost ?? true;
|
||||
}
|
||||
|
||||
set verifyHost(value: boolean) {
|
||||
this._data.verifyHost = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Single socket-based service location
|
||||
* Used by: services using a single host/port combination
|
||||
*/
|
||||
export class LocationSocketSole extends Location {
|
||||
readonly type = 'SOCKET_SOLE' as const;
|
||||
host: string;
|
||||
port: number;
|
||||
encryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||
verifyPeer: boolean;
|
||||
verifyHost: boolean;
|
||||
export class LocationSocketSole extends Location<ServiceLocationSocketSole> {
|
||||
|
||||
constructor(
|
||||
host: string = '',
|
||||
@@ -122,62 +175,75 @@ export class LocationSocketSole extends Location {
|
||||
verifyPeer: boolean = true,
|
||||
verifyHost: boolean = true
|
||||
) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.encryption = encryption;
|
||||
this.verifyPeer = verifyPeer;
|
||||
this.verifyHost = verifyHost;
|
||||
super({
|
||||
type: 'SOCKET_SOLE',
|
||||
host,
|
||||
port,
|
||||
encryption,
|
||||
verifyPeer,
|
||||
verifyHost,
|
||||
});
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationSocketSole): LocationSocketSole {
|
||||
return new LocationSocketSole(
|
||||
data.host,
|
||||
data.port,
|
||||
data.encryption,
|
||||
data.verifyPeer ?? true,
|
||||
data.verifyHost ?? true
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationSocketSole {
|
||||
return {
|
||||
type: this.type,
|
||||
host: this.host,
|
||||
port: this.port,
|
||||
encryption: this.encryption,
|
||||
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
|
||||
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||
};
|
||||
return new LocationSocketSole().load(data);
|
||||
}
|
||||
|
||||
clone(): LocationSocketSole {
|
||||
return new LocationSocketSole(
|
||||
this.host,
|
||||
this.port,
|
||||
this.encryption,
|
||||
this.verifyPeer,
|
||||
this.verifyHost
|
||||
);
|
||||
return LocationSocketSole.fromJson(structuredClone(this.toJson()));
|
||||
}
|
||||
|
||||
get type(): 'SOCKET_SOLE' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get host(): string {
|
||||
return this._data.host;
|
||||
}
|
||||
|
||||
set host(value: string) {
|
||||
this._data.host = value;
|
||||
}
|
||||
|
||||
get port(): number {
|
||||
return this._data.port;
|
||||
}
|
||||
|
||||
set port(value: number) {
|
||||
this._data.port = value;
|
||||
}
|
||||
|
||||
get encryption(): 'none' | 'ssl' | 'tls' | 'starttls' {
|
||||
return this._data.encryption;
|
||||
}
|
||||
|
||||
set encryption(value: 'none' | 'ssl' | 'tls' | 'starttls') {
|
||||
this._data.encryption = value;
|
||||
}
|
||||
|
||||
get verifyPeer(): boolean {
|
||||
return this._data.verifyPeer ?? true;
|
||||
}
|
||||
|
||||
set verifyPeer(value: boolean) {
|
||||
this._data.verifyPeer = value;
|
||||
}
|
||||
|
||||
get verifyHost(): boolean {
|
||||
return this._data.verifyHost ?? true;
|
||||
}
|
||||
|
||||
set verifyHost(value: boolean) {
|
||||
this._data.verifyHost = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Split socket-based service location
|
||||
* Used by: traditional IMAP/SMTP configurations
|
||||
*/
|
||||
export class LocationSocketSplit extends Location {
|
||||
readonly type = 'SOCKET_SPLIT' as const;
|
||||
inboundHost: string;
|
||||
inboundPort: number;
|
||||
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||
outboundHost: string;
|
||||
outboundPort: number;
|
||||
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||
inboundVerifyPeer: boolean;
|
||||
inboundVerifyHost: boolean;
|
||||
outboundVerifyPeer: boolean;
|
||||
outboundVerifyHost: boolean;
|
||||
export class LocationSocketSplit extends Location<ServiceLocationSocketSplit> {
|
||||
|
||||
constructor(
|
||||
inboundHost: string = '',
|
||||
@@ -191,91 +257,146 @@ export class LocationSocketSplit extends Location {
|
||||
outboundVerifyPeer: boolean = true,
|
||||
outboundVerifyHost: boolean = true
|
||||
) {
|
||||
super();
|
||||
this.inboundHost = inboundHost;
|
||||
this.inboundPort = inboundPort;
|
||||
this.inboundEncryption = inboundEncryption;
|
||||
this.outboundHost = outboundHost;
|
||||
this.outboundPort = outboundPort;
|
||||
this.outboundEncryption = outboundEncryption;
|
||||
this.inboundVerifyPeer = inboundVerifyPeer;
|
||||
this.inboundVerifyHost = inboundVerifyHost;
|
||||
this.outboundVerifyPeer = outboundVerifyPeer;
|
||||
this.outboundVerifyHost = outboundVerifyHost;
|
||||
super({
|
||||
type: 'SOCKET_SPLIT',
|
||||
inboundHost,
|
||||
inboundPort,
|
||||
inboundEncryption,
|
||||
outboundHost,
|
||||
outboundPort,
|
||||
outboundEncryption,
|
||||
inboundVerifyPeer,
|
||||
inboundVerifyHost,
|
||||
outboundVerifyPeer,
|
||||
outboundVerifyHost,
|
||||
});
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationSocketSplit): LocationSocketSplit {
|
||||
return new LocationSocketSplit(
|
||||
data.inboundHost,
|
||||
data.inboundPort,
|
||||
data.inboundEncryption,
|
||||
data.outboundHost,
|
||||
data.outboundPort,
|
||||
data.outboundEncryption,
|
||||
data.inboundVerifyPeer ?? true,
|
||||
data.inboundVerifyHost ?? true,
|
||||
data.outboundVerifyPeer ?? true,
|
||||
data.outboundVerifyHost ?? true
|
||||
);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationSocketSplit {
|
||||
return {
|
||||
type: this.type,
|
||||
inboundHost: this.inboundHost,
|
||||
inboundPort: this.inboundPort,
|
||||
inboundEncryption: this.inboundEncryption,
|
||||
outboundHost: this.outboundHost,
|
||||
outboundPort: this.outboundPort,
|
||||
outboundEncryption: this.outboundEncryption,
|
||||
...(this.inboundVerifyPeer !== undefined && { inboundVerifyPeer: this.inboundVerifyPeer }),
|
||||
...(this.inboundVerifyHost !== undefined && { inboundVerifyHost: this.inboundVerifyHost }),
|
||||
...(this.outboundVerifyPeer !== undefined && { outboundVerifyPeer: this.outboundVerifyPeer }),
|
||||
...(this.outboundVerifyHost !== undefined && { outboundVerifyHost: this.outboundVerifyHost })
|
||||
};
|
||||
return new LocationSocketSplit().load(data);
|
||||
}
|
||||
|
||||
clone(): LocationSocketSplit {
|
||||
return new LocationSocketSplit(
|
||||
this.inboundHost,
|
||||
this.inboundPort,
|
||||
this.inboundEncryption,
|
||||
this.outboundHost,
|
||||
this.outboundPort,
|
||||
this.outboundEncryption,
|
||||
this.inboundVerifyPeer,
|
||||
this.inboundVerifyHost,
|
||||
this.outboundVerifyPeer,
|
||||
this.outboundVerifyHost
|
||||
);
|
||||
return LocationSocketSplit.fromJson(structuredClone(this.toJson()));
|
||||
}
|
||||
|
||||
get type(): 'SOCKET_SPLIT' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get inboundHost(): string {
|
||||
return this._data.inboundHost;
|
||||
}
|
||||
|
||||
set inboundHost(value: string) {
|
||||
this._data.inboundHost = value;
|
||||
}
|
||||
|
||||
get inboundPort(): number {
|
||||
return this._data.inboundPort;
|
||||
}
|
||||
|
||||
set inboundPort(value: number) {
|
||||
this._data.inboundPort = value;
|
||||
}
|
||||
|
||||
get inboundEncryption(): 'none' | 'ssl' | 'tls' | 'starttls' {
|
||||
return this._data.inboundEncryption;
|
||||
}
|
||||
|
||||
set inboundEncryption(value: 'none' | 'ssl' | 'tls' | 'starttls') {
|
||||
this._data.inboundEncryption = value;
|
||||
}
|
||||
|
||||
get outboundHost(): string {
|
||||
return this._data.outboundHost;
|
||||
}
|
||||
|
||||
set outboundHost(value: string) {
|
||||
this._data.outboundHost = value;
|
||||
}
|
||||
|
||||
get outboundPort(): number {
|
||||
return this._data.outboundPort;
|
||||
}
|
||||
|
||||
set outboundPort(value: number) {
|
||||
this._data.outboundPort = value;
|
||||
}
|
||||
|
||||
get outboundEncryption(): 'none' | 'ssl' | 'tls' | 'starttls' {
|
||||
return this._data.outboundEncryption;
|
||||
}
|
||||
|
||||
set outboundEncryption(value: 'none' | 'ssl' | 'tls' | 'starttls') {
|
||||
this._data.outboundEncryption = value;
|
||||
}
|
||||
|
||||
get inboundVerifyPeer(): boolean {
|
||||
return this._data.inboundVerifyPeer ?? true;
|
||||
}
|
||||
|
||||
set inboundVerifyPeer(value: boolean) {
|
||||
this._data.inboundVerifyPeer = value;
|
||||
}
|
||||
|
||||
get inboundVerifyHost(): boolean {
|
||||
return this._data.inboundVerifyHost ?? true;
|
||||
}
|
||||
|
||||
set inboundVerifyHost(value: boolean) {
|
||||
this._data.inboundVerifyHost = value;
|
||||
}
|
||||
|
||||
get outboundVerifyPeer(): boolean {
|
||||
return this._data.outboundVerifyPeer ?? true;
|
||||
}
|
||||
|
||||
set outboundVerifyPeer(value: boolean) {
|
||||
this._data.outboundVerifyPeer = value;
|
||||
}
|
||||
|
||||
get outboundVerifyHost(): boolean {
|
||||
return this._data.outboundVerifyHost ?? true;
|
||||
}
|
||||
|
||||
set outboundVerifyHost(value: boolean) {
|
||||
this._data.outboundVerifyHost = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* File-based service location
|
||||
* Used by: local file system providers
|
||||
*/
|
||||
export class LocationFile extends Location {
|
||||
readonly type = 'FILE' as const;
|
||||
path: string;
|
||||
export class LocationFile extends Location<ServiceLocationFile> {
|
||||
|
||||
constructor(path: string = '') {
|
||||
super();
|
||||
this.path = path;
|
||||
super({
|
||||
type: 'FILE',
|
||||
path,
|
||||
});
|
||||
}
|
||||
|
||||
static fromJson(data: ServiceLocationFile): LocationFile {
|
||||
return new LocationFile(data.path);
|
||||
}
|
||||
|
||||
toJson(): ServiceLocationFile {
|
||||
return {
|
||||
type: this.type,
|
||||
path: this.path
|
||||
};
|
||||
return new LocationFile().load(data);
|
||||
}
|
||||
|
||||
clone(): LocationFile {
|
||||
return new LocationFile(this.path);
|
||||
return LocationFile.fromJson(structuredClone(this.toJson()));
|
||||
}
|
||||
|
||||
get type(): 'FILE' {
|
||||
return this._data.type;
|
||||
}
|
||||
|
||||
get path(): string {
|
||||
return this._data.path;
|
||||
}
|
||||
|
||||
set path(value: string) {
|
||||
this._data.path = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user