-
Notifications
You must be signed in to change notification settings - Fork 8
/
conventions.ts
173 lines (143 loc) · 5.56 KB
/
conventions.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Component, type NamingConvention, TT } from "@ndn/packet";
import { Encoder, NNI } from "@ndn/tlv";
abstract class Typed {
constructor(protected readonly tt: number) {}
public match(comp: Component): boolean {
return comp.type === this.tt;
}
}
class TypedString extends Typed implements NamingConvention<string> {
public create(v: string): Component {
return new Component(this.tt, v);
}
public parse(comp: Component): string {
return comp.text;
}
}
abstract class TypedNumberBase extends Typed {
private readonly altUriRegex: RegExp;
constructor(
tt: number,
private readonly altUriPrefix: string,
) {
super(tt);
this.altUriRegex = new RegExp(`^${altUriPrefix}(\\d+)$`);
}
public create(v: number | bigint): Component {
return new Component(this.tt, Encoder.encode(NNI(v), 8));
}
public override match(comp: Component): boolean {
return super.match(comp) && NNI.isValidLength(comp.length);
}
protected parseRaw(comp: Component): bigint {
return NNI.decode(comp.value, { big: true });
}
public toAltUri(comp: Component): string {
return `${this.altUriPrefix}${this.parseRaw(comp)}`;
}
public fromAltUri(input: string): Component | undefined {
const m = this.altUriRegex.exec(input);
if (!m) {
return undefined;
}
return this.create(BigInt(m[1]!));
}
}
interface NumberConvention<A = never, R extends number | bigint = number> extends NamingConvention<number | bigint | A, R>, NamingConvention.WithAltUri {}
class TypedNumber extends TypedNumberBase implements NumberConvention {
public parse(comp: Component): number {
return NNI.decode(comp.value);
}
}
class TypedBig extends TypedNumberBase implements NumberConvention<never, bigint> {
public parse(comp: Component): bigint {
return NNI.decode(comp.value, { big: true });
}
}
interface NumberBigConvention<A = never> extends NumberConvention<A> {
big: NumberConvention<A, bigint>;
}
class TypedNumberBig extends TypedNumber implements NumberBigConvention {
constructor(...args: ConstructorParameters<typeof TypedNumberBase>) {
super(...args);
this.big = new TypedBig(...args);
}
public readonly big: TypedBig;
}
class TypedTimestamp extends TypedNumber implements NumberConvention<Date> {
constructor(
tt: number,
private readonly unit: number,
private readonly max = Number.MAX_SAFE_INTEGER,
) {
super(tt, "t=");
}
public override create(v: number | bigint | Date): Component {
if (v instanceof Date) {
v = v.getTime() * 1000;
} else {
v = Number(v) * this.unit;
}
this.checkMax(v);
return super.create(v);
}
public override parse(comp: Component): number {
const v = super.parse(comp);
this.checkMax(v);
return v / this.unit;
}
private checkMax(v: number): void {
if (v > this.max) {
throw new Error("timestamp number too large");
}
}
}
interface TimestampConvention extends NumberConvention<Date> {
/** Timestamp interpreted as number in milliseconds. */
ms: NumberConvention<Date>;
/** Timestamp interpreted as number in microseconds. */
us: NumberConvention<Date>;
}
function makeTimestampConvention(tt: number): TimestampConvention {
const ms = new TypedTimestamp(tt, 1000, 8787511468039992);
const us = new TypedTimestamp(tt, 1);
return Object.assign(ms, { ms, us });
}
/**
* GenericNameComponent enclosing a number.
*
* This is not really a naming convention, but it's used in several protocols.
*/
export const GenericNumber: NumberBigConvention = new TypedNumberBig(TT.GenericNameComponent, "");
/** KeywordNameComponent (rev2 & rev3), interpreted as string. */
export const Keyword: NamingConvention<string> = new TypedString(0x20);
/** SegmentNameComponent (rev2), interpreted as number. */
export const Segment2: NumberBigConvention = new TypedNumberBig(0x21, "seg=");
/** SegmentNameComponent (rev3), interpreted as number. */
export const Segment3: NumberBigConvention = new TypedNumberBig(0x32, "seg=");
/** SegmentNameComponent (default format, currently rev3). */
export const Segment = Segment3;
/** ByteOffsetNameComponent (rev2), interpreted as number. */
export const ByteOffset2: NumberBigConvention = new TypedNumberBig(0x22, "off=");
/** ByteOffsetNameComponent (rev3), interpreted as number. */
export const ByteOffset3: NumberBigConvention = new TypedNumberBig(0x34, "off=");
/** ByteOffsetNameComponent (default format, currently rev3). */
export const ByteOffset = ByteOffset3;
/** VersionNameComponent (rev2), interpreted as number. */
export const Version2: NumberBigConvention = new TypedNumberBig(0x23, "v=");
/** VersionNameComponent (rev3), interpreted as number. */
export const Version3: NumberBigConvention = new TypedNumberBig(0x36, "v=");
/** VersionNameComponent (default format, currently rev3). */
export const Version = Version3;
/** TimestampNameComponent (rev2), interpreted as number in milliseconds. */
export const Timestamp2 = makeTimestampConvention(0x24);
/** TimestampNameComponent (rev3), interpreted as number in milliseconds. */
export const Timestamp3 = makeTimestampConvention(0x38);
/** TimestampNameComponent (default format, currently rev3). */
export const Timestamp = Timestamp3;
/** SequenceNumNameComponent (rev2), interpreted as number. */
export const SequenceNum2: NumberBigConvention = new TypedNumberBig(0x25, "seq=");
/** SequenceNumNameComponent (rev3), interpreted as number. */
export const SequenceNum3: NumberBigConvention = new TypedNumberBig(0x3A, "seq=");
/** SequenceNumNameComponent (default format, currently rev3). */
export const SequenceNum = SequenceNum3;