/
UriHost.ts
144 lines (119 loc) · 3.54 KB
/
UriHost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Comparable, HashCode, Murmur3, HashGenCacheMap} from "@swim/util";
import {Output, Debug, Display} from "@swim/codec";
import {Uri} from "./Uri";
export type AnyUriHost = UriHost | string;
export abstract class UriHost implements Comparable<UriHost>, HashCode, Debug, Display {
/** @hidden */
_hashCode?: number;
protected constructor() {
// stub
}
isDefined(): boolean {
return true;
}
abstract address(): string;
name(): string | null {
return null;
}
ipv4(): string | null {
return null;
}
ipv6(): string | null {
return null;
}
toAny(): string {
return this.toString();
}
compareTo(that: UriHost): 0 | 1 | -1 {
const order = this.toString().localeCompare(that.toString());
return order < 0 ? -1 : order > 0 ? 1 : 0;
}
equals(that: unknown): boolean {
if (this === that) {
return true;
} else if (that instanceof UriHost) {
return this.toString() === that.toString();
}
return false;
}
hashCode(): number {
if (this._hashCode === void 0) {
this._hashCode = Murmur3.hash(this.toString());
}
return this._hashCode;
}
abstract debug(output: Output): void;
abstract display(output: Output): void;
abstract toString(): string;
private static _undefined?: UriHost;
private static _cache?: HashGenCacheMap<string, UriHost>;
static undefined(): UriHost {
if (UriHost._undefined === void 0) {
UriHost._undefined = new Uri.HostUndefined();
}
return UriHost._undefined;
}
static from(address: string): UriHost {
const cache = UriHost.cache();
const host = cache.get(address);
if (host instanceof Uri.HostName) {
return host;
} else {
return cache.put(address, new Uri.HostName(address));
}
}
static ipv4(address: string): UriHost {
const cache = UriHost.cache();
const host = cache.get(address);
if (host instanceof Uri.HostIPv4) {
return host;
} else {
return cache.put(address, new Uri.HostIPv4(address));
}
}
static ipv6(address: string): UriHost {
const cache = UriHost.cache();
const host = cache.get(address);
if (host instanceof Uri.HostIPv6) {
return host;
} else {
return cache.put(address, new Uri.HostIPv6(address));
}
}
static fromAny(host: AnyUriHost | null | undefined): UriHost {
if (host === null || host === void 0) {
return UriHost.undefined();
} else if (host instanceof UriHost) {
return host;
} else if (typeof host === "string") {
return UriHost.parse(host);
} else {
throw new TypeError("" + host);
}
}
static parse(string: string): UriHost {
return Uri.standardParser().parseHostString(string);
}
/** @hidden */
static cache(): HashGenCacheMap<string, UriHost> {
if (UriHost._cache === void 0) {
const cacheSize = 16;
UriHost._cache = new HashGenCacheMap<string, UriHost>(cacheSize);
}
return UriHost._cache;
}
}
Uri.Host = UriHost;