Skip to content

Commit 4d7fce0

Browse files
authored
🤖 Merge PR DefinitelyTyped#70443 Add types for compact-encoding by @EvanHahn
1 parent a73a22b commit 4d7fce0

File tree

5 files changed

+325
-0
lines changed

5 files changed

+325
-0
lines changed

types/compact-encoding/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import cenc = require("compact-encoding");
2+
3+
const state = cenc.state();
4+
state.start = 123;
5+
state.end = 456;
6+
state.buffer = new Uint8Array(1);
7+
8+
cenc.uint.preencode(state, 42);
9+
cenc.string.preencode(state, "hi");
10+
cenc.raw.preencode(state, new Uint8Array(3));
11+
cenc.raw.string.preencode(state, "hi");
12+
// @ts-expect-error
13+
cenc.uint.preencode(state, "string");
14+
15+
cenc.uint.encode(state, 42);
16+
cenc.string.encode(state, "hi");
17+
cenc.raw.encode(state, new Uint8Array(3));
18+
cenc.raw.string.encode(state, "hi");
19+
// @ts-expect-error
20+
cenc.uint.encode(state, "string");
21+
22+
const n: number = cenc.uint.decode(state);
23+
const s1: string = cenc.string.decode(state);
24+
const b: Uint8Array = cenc.raw.decode(state);
25+
const s2: string = cenc.raw.string.decode(state);
26+
// @ts-expect-error
27+
const s3: string = cenc.uint.decode(state);
28+
29+
const buf: Uint8Array = cenc.encode(cenc.bool, true);
30+
const bool: boolean = cenc.decode(cenc.bool, buf);

types/compact-encoding/index.d.ts

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// Copied from <https://github.com/sindresorhus/type-fest/blob/30b8e2209b2518ef60bdab98ec6626d69a7e9d5a/source/basic.d.ts#L38-68>
2+
type JsonObject = { [Key in string]: JsonValue } & { [Key in string]?: JsonValue | undefined };
3+
type JsonArray = JsonValue[] | readonly JsonValue[];
4+
type JsonPrimitive = string | number | boolean | null;
5+
type JsonValue = JsonPrimitive | JsonObject | JsonArray;
6+
7+
interface AbstractEncoding<T> {
8+
encode(obj: T, buffer?: Uint8Array, offset?: number): Uint8Array;
9+
decode(buffer: Uint8Array, start?: number, end?: number): T;
10+
encodingLength(obj: T): number;
11+
}
12+
13+
interface Codec<T> {
14+
encode(value: T): Uint8Array;
15+
decode(buf: Uint8Array): T;
16+
}
17+
18+
declare namespace cenc {
19+
interface State {
20+
/** The byte offset to start encoding/decoding at. */
21+
start: number;
22+
/** The byte offset indicating the end of the buffer. */
23+
end: number;
24+
/** Either a Node.js Buffer or Uint8Array. */
25+
buffer: null | Uint8Array;
26+
/** Used internally by codecs. Starts out as `null`. */
27+
cache: unknown;
28+
}
29+
30+
interface Encoder<T> {
31+
/**
32+
* Does a fast preencode dry-run that only sets `state.end`. Use this to
33+
* figure out how big of a buffer you need.
34+
*/
35+
preencode(state: State, val: T): void;
36+
37+
/**
38+
* Encodes `val` into state.buffer at position `state.start`. Updates
39+
* `state.start` to point after the encoded value when done.
40+
*/
41+
encode(state: State, val: T): void;
42+
43+
/**
44+
* Decodes a value from `state.buffer` as position `state.start`. Updates
45+
* `state.start` to point after the decoded value when done in the buffer.
46+
*/
47+
decode(state: State): T;
48+
}
49+
}
50+
51+
declare const cenc: {
52+
/** Get a blank state object. */
53+
state(): cenc.State;
54+
55+
/** Helper for encoding to a buffer from a single encoder. */
56+
encode<T>(encoder: cenc.Encoder<T>, val: T): Uint8Array;
57+
58+
/** Helper for decoding from a buffer from a single encoder. */
59+
decode<T>(encoder: cenc.Encoder<T>, buf: Uint8Array): T;
60+
61+
/** Encodes a uint using compact-uint. */
62+
uint: cenc.Encoder<number>;
63+
/** Encodes a fixed size uint8. */
64+
uint8: cenc.Encoder<number>;
65+
/** Encodes a fixed size uint16. Useful for things like ports. */
66+
uint16: cenc.Encoder<number>;
67+
/** Encodes a fixed size uint24. Useful for message framing. */
68+
uint24: cenc.Encoder<number>;
69+
/** Encodes a fixed size uint32. Useful for very large message framing. */
70+
uint32: cenc.Encoder<number>;
71+
/** Encodes a fixed size uint40. */
72+
uint40: cenc.Encoder<number>;
73+
/** Encodes a fixed size uint48. */
74+
uint48: cenc.Encoder<number>;
75+
/** Encodes a fixed size uint56. */
76+
uint56: cenc.Encoder<number>;
77+
/** Encodes a fixed size uint64. */
78+
uint64: cenc.Encoder<number>;
79+
/** Encodes an int using cenc.uint with ZigZag encoding. */
80+
int: cenc.Encoder<number>;
81+
/** Encodes a fixed size int8 using cenc.uint8 with ZigZag encoding. */
82+
int8: cenc.Encoder<number>;
83+
/** Encodes a fixed size int16 using cenc.uint16 with ZigZag encoding. */
84+
int16: cenc.Encoder<number>;
85+
/** Encodes a fixed size int24 using cenc.uint24 with ZigZag encoding. */
86+
int24: cenc.Encoder<number>;
87+
/** Encodes a fixed size int32 using cenc.uint32 with ZigZag encoding. */
88+
int32: cenc.Encoder<number>;
89+
/** Encodes a fixed size int40 using cenc.uint40 with ZigZag encoding. */
90+
int40: cenc.Encoder<number>;
91+
/** Encodes a fixed size int48 using cenc.uint48 with ZigZag encoding. */
92+
int48: cenc.Encoder<number>;
93+
/** Encodes a fixed size int56 using cenc.uint56 with ZigZag encoding. */
94+
int56: cenc.Encoder<number>;
95+
/** Encodes a fixed size int64 using cenc.uint64 with ZigZag encoding. */
96+
int64: cenc.Encoder<number>;
97+
/** Encodes a fixed size biguint64. */
98+
biguint64: cenc.Encoder<bigint>;
99+
/** Encodes a fixed size bigint64 using cenc.biguint64 with ZigZag encoding. */
100+
bigint64: cenc.Encoder<bigint>;
101+
/** Encodes a biguint with its word count uint prefixed. */
102+
biguint: cenc.Encoder<bigint>;
103+
/** Encodes a bigint using cenc.biguint with ZigZag encoding. */
104+
bigint: cenc.Encoder<bigint>;
105+
/** Encodes a fixed size float32. */
106+
float32: cenc.Encoder<number>;
107+
/** Encodes a fixed size float64. */
108+
float64: cenc.Encoder<number>;
109+
/** Encodes a buffer with its length uint prefixed. When decoding an empty buffer, null is returned. */
110+
buffer: cenc.Encoder<Uint8Array>;
111+
/** Encodes an arraybuffer with its length uint prefixed. */
112+
arraybuffer: cenc.Encoder<ArrayBuffer>;
113+
/** Encodes a uint8array with its element length uint prefixed. */
114+
uint8array: cenc.Encoder<Uint8Array>;
115+
/** Encodes a uint16array with its element length uint prefixed. */
116+
uint16array: cenc.Encoder<Uint16Array>;
117+
/** Encodes a uint32array with its element length uint prefixed. */
118+
uint32array: cenc.Encoder<Uint32Array>;
119+
/** Encodes a int8array with its element length uint prefixed. */
120+
int8array: cenc.Encoder<Int8Array>;
121+
/** Encodes a int16array with its element length uint prefixed. */
122+
int16array: cenc.Encoder<Int16Array>;
123+
/** Encodes a int32array with its element length uint prefixed. */
124+
int32array: cenc.Encoder<Int32Array>;
125+
/** Encodes a biguint64array with its element length uint prefixed. */
126+
biguint64array: cenc.Encoder<BigUint64Array>;
127+
/** Encodes a bigint64array with its element length uint prefixed. */
128+
bigint64array: cenc.Encoder<BigInt64Array>;
129+
/** Encodes a float32array with its element length uint prefixed. */
130+
float32array: cenc.Encoder<Float32Array>;
131+
/** Encodes a float64array with its element length uint prefixed. */
132+
float64array: cenc.Encoder<Float64Array>;
133+
/** Encodes a boolean as 1 or 0. */
134+
bool: cenc.Encoder<boolean>;
135+
/** Encodes a fixed 32 byte buffer. */
136+
fixed32: cenc.Encoder<Uint8Array>;
137+
/** Encodes a fixed 64 byte buffer. */
138+
fixed64: cenc.Encoder<Uint8Array>;
139+
/** Makes a fixed sized encoder. */
140+
fixed(n: number): cenc.Encoder<Uint8Array>;
141+
/** Makes an array encoder from another encoder. Arrays are uint prefixed with their length. */
142+
array<T>(enc: cenc.Encoder<T>): cenc.Encoder<T[]>;
143+
/** Encodes a JSON value as utf-8. */
144+
json: cenc.Encoder<JsonValue>;
145+
/** Encodes a JSON value as newline delimited utf-8. */
146+
ndjson: cenc.Encoder<JsonValue>;
147+
/** Encodes any JSON representable value into a self described buffer. Like JSON + buffer, but using compact types. Useful for schemaless codecs. */
148+
any: cenc.Encoder<unknown>;
149+
150+
/** Pass through encodes a buffer, i.e. a basic copy. */
151+
raw: cenc.Encoder<Uint8Array> & {
152+
/** Encodes a utf-8 string without a length prefixed. */
153+
string: cenc.Encoder<string>;
154+
/** Encodes a utf-8 string without a length prefixed. */
155+
utf8: cenc.Encoder<string>;
156+
/** Encodes a JSON value as newline delimited utf-8 without a length prefixed. */
157+
ndjson: cenc.Encoder<JsonValue>;
158+
/** Encodes a JSON value as utf-8 without a length prefixed. */
159+
json: cenc.Encoder<JsonValue>;
160+
/** Makes an array encoder from another encoder, without a length prefixed. */
161+
array<T>(enc: cenc.Encoder<T>): cenc.Encoder<T[]>;
162+
/** Encodes a utf16le string without a length prefixed. */
163+
utf16le: cenc.Encoder<string>;
164+
/** Encodes a utf16le string without a length prefixed. */
165+
ucs2: cenc.Encoder<string>;
166+
/** Encodes a base64 string without a length prefixed. */
167+
base64: cenc.Encoder<string>;
168+
/** Encodes a buffer without a length prefixed. */
169+
buffer: cenc.Encoder<Uint8Array>;
170+
/** Encodes an arraybuffer without a length prefixed. */
171+
arraybuffer: cenc.Encoder<ArrayBuffer>;
172+
/** Encodes a hex string without a length prefixed. */
173+
hex: cenc.Encoder<string>;
174+
/** Encodes an ascii string without a length prefixed. */
175+
ascii: cenc.Encoder<string>;
176+
/** Encodes a uint8array without a length prefixed. */
177+
uint8array: cenc.Encoder<Uint8Array>;
178+
/** Encodes a uint16array without a length prefixed. */
179+
uint16array: cenc.Encoder<Uint16Array>;
180+
/** Encodes a uint32array without a length prefixed. */
181+
uint32array: cenc.Encoder<Uint32Array>;
182+
/** Encodes a int8array without a length prefixed. */
183+
int8array: cenc.Encoder<Int8Array>;
184+
/** Encodes a int16array without a length prefixed. */
185+
int16array: cenc.Encoder<Int16Array>;
186+
/** Encodes a int32array without a length prefixed. */
187+
int32array: cenc.Encoder<Int32Array>;
188+
/** Encodes a biguint64array without a length prefixed. */
189+
biguint64array: cenc.Encoder<BigUint64Array>;
190+
/** Encodes a bigint64array without a length prefixed. */
191+
bigint64array: cenc.Encoder<BigInt64Array>;
192+
/** Encodes a float32array without a length prefixed. */
193+
float32array: cenc.Encoder<Float32Array>;
194+
/** Encodes a float64array without a length prefixed. */
195+
float64array: cenc.Encoder<Float64Array>;
196+
};
197+
198+
/** Encodes a utf-8 string, similar to buffer. */
199+
string: cenc.Encoder<string> & {
200+
/** Encodes a fixed sized utf-8 string. */
201+
fixed(n: number): cenc.Encoder<string>;
202+
};
203+
/** Encodes a utf-8 string, similar to buffer. */
204+
utf8: cenc.Encoder<string> & {
205+
/** Encodes a fixed sized utf-8 string. */
206+
fixed(n: number): cenc.Encoder<string>;
207+
};
208+
/** Encodes an ascii string. */
209+
ascii: cenc.Encoder<string> & {
210+
/** Encodes a fixed size ascii string. */
211+
fixed(n: number): cenc.Encoder<string>;
212+
};
213+
/** Encodes a hex string. */
214+
hex: cenc.Encoder<string> & {
215+
/** Encodes a fixed size hex string. */
216+
fixed(n: number): cenc.Encoder<string>;
217+
};
218+
/** Encodes a base64 string. */
219+
base64: cenc.Encoder<string> & {
220+
/** Encodes a fixed size base64 string. */
221+
fixed(n: number): cenc.Encoder<string>;
222+
};
223+
/** Encodes a utf16le string. */
224+
utf16le: cenc.Encoder<string> & {
225+
/** Encodes a fixed size utf16le string. */
226+
fixed(n: number): cenc.Encoder<string>;
227+
};
228+
/** Encodes a utf16le string. */
229+
ucs2: cenc.Encoder<string> & {
230+
/** Encodes a fixed size utf16le string. */
231+
fixed(n: number): cenc.Encoder<string>;
232+
};
233+
234+
/** Makes a compact encoder from a codec or abstract-encoding. */
235+
from(
236+
enc:
237+
| "ascii"
238+
| "utf-8"
239+
| "utf8"
240+
| "hex"
241+
| "base64"
242+
| "utf16-le"
243+
| "utf16le"
244+
| "ucs-2"
245+
| "ucs2",
246+
): cenc.Encoder<string>;
247+
from(enc: "ndjson" | "json"): cenc.Encoder<JsonValue>;
248+
from(enc: "binary" | string): cenc.Encoder<Uint8Array | string>;
249+
from<T>(enc: cenc.Encoder<T>): cenc.Encoder<T>;
250+
from<T>(enc: AbstractEncoding<T>): cenc.Encoder<T>;
251+
from<T>(enc: Codec<T>): cenc.Encoder<T>;
252+
};
253+
254+
export = cenc;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "@types/compact-encoding",
4+
"version": "2.15.9999",
5+
"projects": [
6+
"https://github.com/compact-encoding/compact-encoding"
7+
],
8+
"devDependencies": {
9+
"@types/compact-encoding": "workspace:."
10+
},
11+
"owners": [
12+
{
13+
"name": "Evan Hahn",
14+
"githubUsername": "EvanHahn"
15+
}
16+
]
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es2020"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"compact-encoding-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)