-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathview-types.ts
275 lines (241 loc) · 6.48 KB
/
view-types.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { Constructor } from "./utility-types.ts";
export interface PrimitiveView<T> extends DataView {
/**
* Returns the JavaScript value of the view.
*/
get(): T;
/**
* Sets the JavaScript value of the view.
*/
set(value: T): void;
/**
* Returns the JavaScript value of the view.
*/
toJSON(): T;
}
export interface ContainerView<T> extends DataView {
/**
* The amount of items in the view.
*/
size: number;
[Symbol.iterator](): Generator<ViewInstance<T> | undefined>;
/**
* Get the JavaScript value of an item.
*
* @param index the of the item.
*/
at(index: number): T | undefined;
/**
* Get the JavaScript value of an item.
*
* @param index the index of the item
* @return the item
*/
get(index: number): T | undefined;
/**
* Returns the byte length of an item at a given index.
*
* @param index the index of the item
* @return the byte length
*/
getLength(index: number): number;
/**
* Returns the view of an item at a given index
*
* @param index the index of the item
* @return a view of the item
*/
getView(index: number): ViewInstance<T> | undefined;
/**
* Returns the index within the view of the first occurrence
* of the specified value, starting the search at start.
* Returns -1 if the value is not found.
*
* @param value the to search for
* @return index of the value or -1 if not found
*/
indexOf(value: T, start?: number): number;
/**
* Sets a given JavaScript value to an item at a given index.
*
* @param index the index of the item
* @param value the JavaScript value to set
*/
set(index: number, value: T): void;
/**
* Sets a given view to an item at a given index.
*
* @param index the index of the item
* @param view the view to set
*/
setView(index: number, view: DataView): void;
/**
* Returns the JavaScript value of the view.
*
* @return the javascript value
*/
toJSON(): Array<T | undefined>;
}
export interface ComplexView<T> extends DataView {
/**
* Returns the JavaScript value of a given field.
*
* @param field the field name
* @return the JavaScript value
*/
get<P extends keyof T>(field: P): T[P] | undefined;
/**
* Returns the byte length of a given field.
*
* @param field the field name
* @return the byte length
*/
getLength<P extends keyof T>(field: P): number;
/**
* Returns the view of a field.
*
* @param field the field name
* @return the view
*/
getView<P extends keyof T>(field: P): ViewInstance<T[P]> | undefined;
/**
* Set a JavaScript value to a field.
*
* @param field the field name
* @param value the JavaScript value
*/
set<P extends keyof T>(field: P, value: T[P]): void;
/**
* Set a view to a given field.
*
* @param field the field name
* @param view the view
*/
setView<P extends keyof T>(field: P, view: DataView): void;
/**
* Returns the JavaScript value of the view.
*
* @return the javascript value
*/
toJSON(): T;
}
export type ViewInstance<T> = [T] extends
[boolean | number | string | bigint | ArrayBufferLike] ? PrimitiveView<T>
: T extends Array<infer U> ? ContainerView<U>
: T extends object ? ComplexView<T>
: never;
export interface ViewConstructor<T, Instance = ViewInstance<T>> {
/**
* The byte length of the view.
*/
viewLength: number;
/**
* The byte length of an item inside the container view.
*/
itemLength?: number;
layout?: ViewLayout<T>;
defaultData?: Uint8Array;
ObjectConstructor?: Constructor<T>;
View?: unknown;
// deno-lint-ignore no-explicit-any
new (...args: any[]): Instance;
/**
* Decodes a given view into corresponding JavaScript value.
*
* @param view the view to decode
* @param start the starting offset
* @param length the byte length to decode
* @return the JavaScript value
*/
decode(view: DataView, start?: number, length?: number): T;
/**
* Encodes a JavaScript value into a given view.
*
* @param value the value encode
* @param view the view to encode into
* @param start the offset to start encoding
* @param length the byte length to encode
* @return the amount of written bytes
*/
encode(value: T, view: DataView, start?: number, length?: number): number;
/**
* Creates a view from a given JavaScript value.
*
* @param value the JavaScript value
*/
from(value: T): Instance;
/**
* Returns the length of the view.
*
* @param size the amount of items for ArrayView or the value for MapView and VectorView
*/
getLength(size?: number | unknown): number;
/**
* Intializes a view class, used for complex and container views.
*/
initialize(...args: Array<unknown>): UnknownViewConstructor;
}
export type UnknownViewConstructor = ViewConstructor<
unknown,
PrimitiveView<unknown> | ContainerView<unknown> | ComplexView<unknown>
>;
export type ViewFieldLayout<T> = {
View: ViewConstructor<T>;
start: number;
length: number;
default?: T;
required?: boolean;
};
export type ViewLayout<T> = {
[key in keyof T]: ViewFieldLayout<T[key]>;
};
export type ViewSchemaTypeField<T> = [T] extends [number | bigint | undefined]
? "number" | "integer"
: [T] extends [string | ArrayBufferLike | undefined] ? "string"
: [T] extends [boolean | undefined] ? "boolean"
: T extends Array<unknown> ? "array"
: T extends object ? "object"
: never;
export interface ViewSchemaTypeMap {
int8: "number";
uint8: "number";
int16: "number";
uint16: "number";
int32: "number";
uint32: "number";
float32: "number";
float64: "number";
bigint64: "number";
biguint64: "number";
dict: "object";
map: "object";
vector: "array";
binary: "string";
}
type ReverseTypeMap = {
[P in keyof ViewSchemaTypeMap as ViewSchemaTypeMap[P]]: P;
};
export interface ViewSchema<T> {
$id?: string;
$ref?: `#${string}`;
maxLength?: number;
minLength?: number;
minimum?: number;
maximum?: number;
items?: T extends Array<infer U> ? ViewSchema<U> : never;
maxItems?: number;
minItems?: number;
required?: Array<keyof T>;
properties?: {
[P in keyof T]: ViewSchema<T[P]>;
};
propertyNames?: ViewSchema<number> | ViewSchema<string>;
additionalProperties?: ViewSchema<T[keyof T]>;
type: ViewSchemaTypeField<T>;
btype?: ViewSchemaTypeField<T> extends "number" | "integer"
? ReverseTypeMap["number"]
: ViewSchemaTypeField<T> extends keyof ReverseTypeMap
? ReverseTypeMap[ViewSchemaTypeField<T>]
: never;
default?: T;
}