Skip to content

Commit 4922fa6

Browse files
committed
add significants changes
1 parent 590cc28 commit 4922fa6

28 files changed

+699
-646
lines changed

deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"imports": {
1616
"@aicone/byte": "jsr:@aicone/byte@^0.5.0",
1717
"@std/assert": "jsr:@std/assert@^1.0.2",
18+
"@tls/extension": "jsr:@tls/extension@^0.0.6",
1819
"@tls/struct": "jsr:@tls/struct@^0.2.0"
1920
}
2021
}

playground/enum.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// deno-lint-ignore-file no-slow-types
2+
// @ts-self-types="../type/enum.d.ts"
3+
4+
export class Enum {
5+
static #instances = new Map();
6+
static #frozen = new Set();
7+
8+
#name;
9+
#value;
10+
#ordinal;
11+
12+
constructor(name, value) {
13+
if (this.constructor === Enum) {
14+
throw new TypeError("Cannot instantiate abstract Enum class directly");
15+
}
16+
17+
this.#name = name;
18+
this.#value = value;
19+
this.#ordinal = this.constructor.size;
20+
21+
if (!Enum.#instances.has(this.constructor)) {
22+
Enum.#instances.set(this.constructor, new Map());
23+
}
24+
25+
const instanceMap = Enum.#instances.get(this.constructor);
26+
27+
if (Enum.#frozen.has(this.constructor)) {
28+
throw new Error(`Cannot add new enum constant ${name} to frozen enum ${this.constructor.name}`);
29+
}
30+
31+
if (instanceMap.has(name)) {
32+
throw new Error(`Duplicate enum constant name: ${name}`);
33+
}
34+
35+
for (const [, instance] of instanceMap) {
36+
if (instance.value === value) {
37+
throw new Error(`Duplicate enum value: ${value} for constant ${name}`);
38+
}
39+
}
40+
41+
instanceMap.set(name, this);
42+
}
43+
44+
get name() {
45+
return this.#name;
46+
}
47+
48+
get value() {
49+
return this.#value;
50+
}
51+
52+
get ordinal() {
53+
return this.#ordinal;
54+
}
55+
56+
static get size() {
57+
return this.values().length;
58+
}
59+
60+
static values() {
61+
const instanceMap = Enum.#instances.get(this) || new Map();
62+
return Array.from(instanceMap.values());
63+
}
64+
65+
static names() {
66+
const instanceMap = Enum.#instances.get(this) || new Map();
67+
return Array.from(instanceMap.keys());
68+
}
69+
70+
static valueOf(name) {
71+
const instanceMap = Enum.#instances.get(this);
72+
if (!instanceMap || !instanceMap.has(name)) {
73+
throw new Error(`No enum constant ${this.name}.${name}`);
74+
}
75+
return instanceMap.get(name);
76+
}
77+
78+
static fromValue(value) {
79+
const instance = this.values().find(inst => inst.value === value);
80+
if (!instance) {
81+
throw new Error(`No enum constant with value ${value} in ${this.name}`);
82+
}
83+
return instance;
84+
}
85+
86+
static freeze() {
87+
Enum.#frozen.add(this);
88+
return this;
89+
}
90+
91+
static isFrozen() {
92+
return Enum.#frozen.has(this);
93+
}
94+
95+
toString() {
96+
return `${this.constructor.name}.${this.name}`;
97+
}
98+
99+
toJSON() {
100+
return {
101+
name: this.name,
102+
value: this.value,
103+
ordinal: this.ordinal
104+
};
105+
}
106+
107+
[Symbol.toPrimitive](hint) {
108+
switch (hint) {
109+
case 'number':
110+
return this.value;
111+
case 'string':
112+
return this.toString();
113+
default:
114+
return this.value;
115+
}
116+
}
117+
118+
equals(other) {
119+
if (!(other instanceof this.constructor)) {
120+
return false;
121+
}
122+
return this.name === other.name && this.value === other.value;
123+
}
124+
125+
compareTo(other) {
126+
if (!(other instanceof this.constructor)) {
127+
throw new TypeError(`Cannot compare ${this.constructor.name} with ${other?.constructor?.name}`);
128+
}
129+
return this.ordinal - other.ordinal;
130+
}
131+
}
132+

src/alert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class AlertLevel extends Enum {
2222
* @param {Uint8Array} octet
2323
* @returns {AlertLevel }
2424
*/
25-
static parse(octet) {
25+
static from(octet) {
2626
return AlertLevel.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} AlertLevel type`);
2727
}
2828

@@ -189,7 +189,7 @@ export class AlertDescription extends Enum {
189189
* @param {Uint8Array} octet
190190
* @returns {AlertDescription }
191191
*/
192-
static parse(octet) {
192+
static from(octet) {
193193
return AlertDescription.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} AlertDescription type`);
194194
}
195195

src/certificatetype.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class CertificateType extends Enum {
1616
* @param {Uint8Array} octet
1717
* @returns {CertificateType }
1818
*/
19-
static parse(octet) {
19+
static from(octet) {
2020
return CertificateType.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} CertificateType type`);
2121
}
2222

src/contentype.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class ContentType extends Enum {
2424
* @param {Uint8Array} octet
2525
* @returns {ContentType }
2626
*/
27-
static parse(octet) {
27+
static from(octet) {
2828
return ContentType.fromValue(octet[0]) ?? Error(`Unknown ${octet[0]} ContentType type`);
2929
}
3030

src/dep.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export { Uint8, Uint16, Uint24 } from "jsr:@aicone/byte"
21
export { Struct, Constrained, Byte } from "jsr:@tls/struct"
2+
export { Uint8, Uint16, Uint24, Uint32 } from "jsr:@tls/struct"
3+
export { Extension } from "@tls/extension"
34
export { p256 } from 'npm:@noble/[email protected]/p256'
45
export { p384 } from 'npm:@noble/[email protected]/p384'
56
export { p521 } from 'npm:@noble/[email protected]/p521'

0 commit comments

Comments
 (0)