Skip to content

Commit 13668ba

Browse files
committed
add utils to parse list of object
1 parent 7117a39 commit 13668ba

17 files changed

+155
-110
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.5.8",
3+
"version": "0.5.9",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

src/alert.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export class AlertDescription extends Enum {
195195

196196
/**return 8 */
197197
get bit() { return 8 }
198+
get length() { return 1 }
198199

199200
get level() {
200201
const warning = [

src/certificatetype.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class CertificateType extends Enum {
2525

2626
/**return 8 */
2727
get bit() { return 8 }
28+
get length() { return 1 }
2829

2930
}
3031

src/cipher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class Cipher extends Enum {
1414
}
1515

1616
get Uint16() { return Uint8Array.of(0x13, +this) }
17+
get length() { return 2 }
1718

1819
get hashLength() {
1920
return this.name.slice(-3) / 8;

src/contentype.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class ContentType extends Enum {
3333
get Uint8() {
3434
return Uint8.fromValue(+this)
3535
}
36+
get length(){ return 1 }
3637

3738
/**return 8 */
3839
get bit() { return 8 }

src/extensiontype.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export class ExtensionType extends Enum {
212212
* @type {number}
213213
*/
214214
get bit() { return 16 }
215+
get length() { return 2 }
215216

216217
extension(extension_data){return new Extension(this, extension_data)}
217218
}

src/handshaketype.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class HandshakeType extends Enum {
8686
*/
8787
static MESSAGE_HASH = new HandshakeType('MESSAGE_HASH', 0xFE);
8888

89-
89+
9090
/**
9191
* Check and return HandshakeType if valid
9292
*
@@ -99,51 +99,52 @@ export class HandshakeType extends Enum {
9999
}
100100

101101
/**return 8 */
102-
get bit(){return 8}
102+
get bit() { return 8 }
103+
get length() { return 1 }
103104

104-
handshake(message){
105+
handshake(message) {
105106
return Handshake.fromMessage(this, message)
106107
}
107108

108-
get Uint8(){ return Uint8.fromValue(+this)}
109+
get Uint8() { return Uint8.fromValue(+this) }
109110
}
110111

111112
export class Handshake extends Uint8Array {
112113
msg_type
113114
message
114-
static fromMessage(msg_type, message){
115+
static fromMessage(msg_type, message) {
115116
return new Handshake(msg_type, message)
116117
}
117-
static from(array){
118+
static from(array) {
118119
const copy = Uint8Array.from(array)
119120
const msg_type = HandshakeType.fromValue(copy[0]);
120121
const lengthOf = Uint24.from(copy.subarray(1)).value;
121122
const message = copy.subarray(4, 4 + lengthOf)
122123
return new Handshake(msg_type, message)
123124
}
124-
constructor(msg_type, message){
125-
const struct = new Struct(msg_type.Uint8, Uint24.fromValue(message.length), message)
125+
constructor(msg_type, message) {
126+
const struct = new Struct(msg_type.Uint8, Uint24.fromValue(message.length), message)
126127
super(struct)
127128
this.msg_type = msg_type;
128129
this.message = message
129130
this.items = struct.items
130131
}
131-
get byte(){ return Uint8Array.from(this)}
132-
tlsInnerPlaintext(numZeros){
132+
get byte() { return Uint8Array.from(this) }
133+
tlsInnerPlaintext(numZeros) {
133134
return ContentType.APPLICATION_DATA.tlsInnerPlaintext(this, numZeros)
134135
}
135136
}
136137

137138
export class EndOfEarlyData extends Uint8Array {
138-
static fromHandshake(array){
139+
static fromHandshake(array) {
139140
const type = HandshakeType.fromValue(array.at(0));
140-
if(type!==HandshakeType.END_OF_EARLY_DATA) return TypeError(`Expected ${HandshakeType.END_OF_EARLY_DATA.name}`)
141+
if (type !== HandshakeType.END_OF_EARLY_DATA) return TypeError(`Expected ${HandshakeType.END_OF_EARLY_DATA.name}`)
141142
return new EndOfEarlyData
142143
}
143-
constructor(){
144+
constructor() {
144145
super()
145146
}
146-
get handshake(){ return HandshakeType.END_OF_EARLY_DATA.handshake(this)}
147+
get handshake() { return HandshakeType.END_OF_EARLY_DATA.handshake(this) }
147148
}
148149

149150
// npx -p typescript tsc ./src/handshaketype.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

src/keyupdate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class KeyUpdateRequest extends Enum {
2626

2727
/**return 8 */
2828
get bit() { return 8 }
29+
get length() { return 1 }
2930

3031
}
3132

src/namedgroup.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { p256, p384, p521, x25519, x448, Uint16, Constrained, Struct } from "./dep.ts";
55
import { Enum } from "./enum.js";
6+
import { parseItems } from "./utils.js";
67

78
/**
89
* Supported groups - @see https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.7.
@@ -44,6 +45,7 @@ export class NamedGroup extends Enum {
4445
* @returns {number} The bit length, which is always 16.
4546
*/
4647
get bit() { return 16; }
48+
get length() { return 2;}
4749

4850
/**
4951
* Creates an instance of NamedGroup.
@@ -171,13 +173,7 @@ export class NamedGroupList extends Constrained {
171173
static from(array){
172174
const copy = Uint8Array.from(array);
173175
const lengthOf = Uint16.from(copy).value;
174-
const namedGroups = new Set;
175-
let offset = 2;
176-
while(true){
177-
const namedgroup = NamedGroup.from(copy.subarray(offset));offset+=2;
178-
namedGroups.add(namedgroup);
179-
if(offset>=lengthOf+2)break;
180-
}
176+
const namedGroups = parseItems(copy, 2, lengthOf, NamedGroup)
181177
return new NamedGroupList(...namedGroups)
182178
}
183179
constructor(...named_group_list){

src/pskmode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class PskKeyExchangeMode extends Enum {
1818

1919
/**return 8 */
2020
get bit() { return 8 }
21+
get length() { return 1 }
2122

2223
get Uint8() { return Uint8Array.of(+this)}
2324

0 commit comments

Comments
 (0)