|
| 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; |
0 commit comments