@solana/codecs-numbers 6.3.1 → 6.3.2-canary-20260313112147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -4
- package/src/assertions.ts +42 -0
- package/src/common.ts +94 -0
- package/src/f32.ts +104 -0
- package/src/f64.ts +104 -0
- package/src/i128.ts +121 -0
- package/src/i16.ts +105 -0
- package/src/i32.ts +105 -0
- package/src/i64.ts +108 -0
- package/src/i8.ts +91 -0
- package/src/index.ts +25 -0
- package/src/short-u16.ts +158 -0
- package/src/u128.ts +117 -0
- package/src/u16.ts +103 -0
- package/src/u32.ts +103 -0
- package/src/u64.ts +106 -0
- package/src/u8.ts +87 -0
- package/src/utils.ts +63 -0
package/src/u16.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { combineCodec, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from '@solana/codecs-core';
|
|
2
|
+
|
|
3
|
+
import { NumberCodecConfig } from './common';
|
|
4
|
+
import { numberDecoderFactory, numberEncoderFactory } from './utils';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns an encoder for 16-bit unsigned integers (`u16`).
|
|
8
|
+
*
|
|
9
|
+
* This encoder serializes `u16` values using two bytes in little-endian format by default.
|
|
10
|
+
* You may specify big-endian storage using the `endian` option.
|
|
11
|
+
*
|
|
12
|
+
* For more details, see {@link getU16Codec}.
|
|
13
|
+
*
|
|
14
|
+
* @param config - Optional settings for endianness.
|
|
15
|
+
* @returns A `FixedSizeEncoder<number | bigint, 2>` for encoding `u16` values.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* Encoding a `u16` value.
|
|
19
|
+
* ```ts
|
|
20
|
+
* const encoder = getU16Encoder();
|
|
21
|
+
* const bytes = encoder.encode(42); // 0x2a00
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @see {@link getU16Codec}
|
|
25
|
+
*/
|
|
26
|
+
export const getU16Encoder = (config: NumberCodecConfig = {}): FixedSizeEncoder<bigint | number, 2> =>
|
|
27
|
+
numberEncoderFactory({
|
|
28
|
+
config,
|
|
29
|
+
name: 'u16',
|
|
30
|
+
range: [0, Number('0xffff')],
|
|
31
|
+
set: (view, value, le) => view.setUint16(0, Number(value), le),
|
|
32
|
+
size: 2,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns a decoder for 16-bit unsigned integers (`u16`).
|
|
37
|
+
*
|
|
38
|
+
* This decoder deserializes `u16` values from two bytes in little-endian format by default.
|
|
39
|
+
* You may specify big-endian storage using the `endian` option.
|
|
40
|
+
*
|
|
41
|
+
* For more details, see {@link getU16Codec}.
|
|
42
|
+
*
|
|
43
|
+
* @param config - Optional settings for endianness.
|
|
44
|
+
* @returns A `FixedSizeDecoder<number, 2>` for decoding `u16` values.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* Decoding a `u16` value.
|
|
48
|
+
* ```ts
|
|
49
|
+
* const decoder = getU16Decoder();
|
|
50
|
+
* const value = decoder.decode(new Uint8Array([0x2a, 0x00])); // 42
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @see {@link getU16Codec}
|
|
54
|
+
*/
|
|
55
|
+
export const getU16Decoder = (config: NumberCodecConfig = {}): FixedSizeDecoder<number, 2> =>
|
|
56
|
+
numberDecoderFactory({
|
|
57
|
+
config,
|
|
58
|
+
get: (view, le) => view.getUint16(0, le),
|
|
59
|
+
name: 'u16',
|
|
60
|
+
size: 2,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns a codec for encoding and decoding 16-bit unsigned integers (`u16`).
|
|
65
|
+
*
|
|
66
|
+
* This codec serializes `u16` values using two bytes in little-endian format by default.
|
|
67
|
+
* You may specify big-endian storage using the `endian` option.
|
|
68
|
+
*
|
|
69
|
+
* @param config - Optional settings for endianness.
|
|
70
|
+
* @returns A `FixedSizeCodec<number | bigint, number, 2>` for encoding and decoding `u16` values.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* Encoding and decoding a `u16` value.
|
|
74
|
+
* ```ts
|
|
75
|
+
* const codec = getU16Codec();
|
|
76
|
+
* const bytes = codec.encode(42); // 0x2a00 (little-endian)
|
|
77
|
+
* const value = codec.decode(bytes); // 42
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* Storing values in big-endian format.
|
|
82
|
+
* ```ts
|
|
83
|
+
* const codec = getU16Codec({ endian: Endian.Big });
|
|
84
|
+
* const bytes = codec.encode(42); // 0x002a
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* This codec supports values between `0` and `2^16 - 1`.
|
|
89
|
+
* If you need a larger range, consider using {@link getU32Codec} or {@link getU64Codec}.
|
|
90
|
+
* For signed integers, use {@link getI16Codec}.
|
|
91
|
+
*
|
|
92
|
+
* Separate {@link getU16Encoder} and {@link getU16Decoder} functions are available.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const bytes = getU16Encoder().encode(42);
|
|
96
|
+
* const value = getU16Decoder().decode(bytes);
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @see {@link getU16Encoder}
|
|
100
|
+
* @see {@link getU16Decoder}
|
|
101
|
+
*/
|
|
102
|
+
export const getU16Codec = (config: NumberCodecConfig = {}): FixedSizeCodec<bigint | number, number, 2> =>
|
|
103
|
+
combineCodec(getU16Encoder(config), getU16Decoder(config));
|
package/src/u32.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { combineCodec, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from '@solana/codecs-core';
|
|
2
|
+
|
|
3
|
+
import { NumberCodecConfig } from './common';
|
|
4
|
+
import { numberDecoderFactory, numberEncoderFactory } from './utils';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns an encoder for 32-bit unsigned integers (`u32`).
|
|
8
|
+
*
|
|
9
|
+
* This encoder serializes `u32` values using four bytes in little-endian format by default.
|
|
10
|
+
* You may specify big-endian storage using the `endian` option.
|
|
11
|
+
*
|
|
12
|
+
* For more details, see {@link getU32Codec}.
|
|
13
|
+
*
|
|
14
|
+
* @param config - Optional settings for endianness.
|
|
15
|
+
* @returns A `FixedSizeEncoder<bigint | number, 4>` for encoding `u32` values.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* Encoding a `u32` value.
|
|
19
|
+
* ```ts
|
|
20
|
+
* const encoder = getU32Encoder();
|
|
21
|
+
* const bytes = encoder.encode(42); // 0x2a000000
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @see {@link getU32Codec}
|
|
25
|
+
*/
|
|
26
|
+
export const getU32Encoder = (config: NumberCodecConfig = {}): FixedSizeEncoder<bigint | number, 4> =>
|
|
27
|
+
numberEncoderFactory({
|
|
28
|
+
config,
|
|
29
|
+
name: 'u32',
|
|
30
|
+
range: [0, Number('0xffffffff')],
|
|
31
|
+
set: (view, value, le) => view.setUint32(0, Number(value), le),
|
|
32
|
+
size: 4,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns a decoder for 32-bit unsigned integers (`u32`).
|
|
37
|
+
*
|
|
38
|
+
* This decoder deserializes `u32` values from four bytes in little-endian format by default.
|
|
39
|
+
* You may specify big-endian storage using the `endian` option.
|
|
40
|
+
*
|
|
41
|
+
* For more details, see {@link getU32Codec}.
|
|
42
|
+
*
|
|
43
|
+
* @param config - Optional settings for endianness.
|
|
44
|
+
* @returns A `FixedSizeDecoder<number, 4>` for decoding `u32` values.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* Decoding a `u32` value.
|
|
48
|
+
* ```ts
|
|
49
|
+
* const decoder = getU32Decoder();
|
|
50
|
+
* const value = decoder.decode(new Uint8Array([0x2a, 0x00, 0x00, 0x00])); // 42
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @see {@link getU32Codec}
|
|
54
|
+
*/
|
|
55
|
+
export const getU32Decoder = (config: NumberCodecConfig = {}): FixedSizeDecoder<number, 4> =>
|
|
56
|
+
numberDecoderFactory({
|
|
57
|
+
config,
|
|
58
|
+
get: (view, le) => view.getUint32(0, le),
|
|
59
|
+
name: 'u32',
|
|
60
|
+
size: 4,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns a codec for encoding and decoding 32-bit unsigned integers (`u32`).
|
|
65
|
+
*
|
|
66
|
+
* This codec serializes `u32` values using four bytes in little-endian format by default.
|
|
67
|
+
* You may specify big-endian storage using the `endian` option.
|
|
68
|
+
*
|
|
69
|
+
* @param config - Optional settings for endianness.
|
|
70
|
+
* @returns A `FixedSizeCodec<bigint | number, number, 4>` for encoding and decoding `u32` values.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* Encoding and decoding a `u32` value.
|
|
74
|
+
* ```ts
|
|
75
|
+
* const codec = getU32Codec();
|
|
76
|
+
* const bytes = codec.encode(42); // 0x2a000000 (little-endian)
|
|
77
|
+
* const value = codec.decode(bytes); // 42
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* Storing values in big-endian format.
|
|
82
|
+
* ```ts
|
|
83
|
+
* const codec = getU32Codec({ endian: Endian.Big });
|
|
84
|
+
* const bytes = codec.encode(42); // 0x0000002a
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* This codec only supports values between `0` and `2^32 - 1`.
|
|
89
|
+
* If you need a larger range, consider using {@link getU64Codec} or {@link getU128Codec}.
|
|
90
|
+
* For signed integers, use {@link getI32Codec}.
|
|
91
|
+
*
|
|
92
|
+
* Separate {@link getU32Encoder} and {@link getU32Decoder} functions are available.
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const bytes = getU32Encoder().encode(42);
|
|
96
|
+
* const value = getU32Decoder().decode(bytes);
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @see {@link getU32Encoder}
|
|
100
|
+
* @see {@link getU32Decoder}
|
|
101
|
+
*/
|
|
102
|
+
export const getU32Codec = (config: NumberCodecConfig = {}): FixedSizeCodec<bigint | number, number, 4> =>
|
|
103
|
+
combineCodec(getU32Encoder(config), getU32Decoder(config));
|
package/src/u64.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { combineCodec, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from '@solana/codecs-core';
|
|
2
|
+
|
|
3
|
+
import { NumberCodecConfig } from './common';
|
|
4
|
+
import { numberDecoderFactory, numberEncoderFactory } from './utils';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns an encoder for 64-bit unsigned integers (`u64`).
|
|
8
|
+
*
|
|
9
|
+
* This encoder serializes `u64` values using 8 bytes.
|
|
10
|
+
* Values can be provided as either `number` or `bigint`.
|
|
11
|
+
*
|
|
12
|
+
* For more details, see {@link getU64Codec}.
|
|
13
|
+
*
|
|
14
|
+
* @param config - Optional configuration to specify endianness (little by default).
|
|
15
|
+
* @returns A `FixedSizeEncoder<number | bigint, 8>` for encoding `u64` values.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* Encoding a `u64` value.
|
|
19
|
+
* ```ts
|
|
20
|
+
* const encoder = getU64Encoder();
|
|
21
|
+
* const bytes = encoder.encode(42); // 0x2a00000000000000
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @see {@link getU64Codec}
|
|
25
|
+
*/
|
|
26
|
+
export const getU64Encoder = (config: NumberCodecConfig = {}): FixedSizeEncoder<bigint | number, 8> =>
|
|
27
|
+
numberEncoderFactory({
|
|
28
|
+
config,
|
|
29
|
+
name: 'u64',
|
|
30
|
+
range: [0n, BigInt('0xffffffffffffffff')],
|
|
31
|
+
set: (view, value, le) => view.setBigUint64(0, BigInt(value), le),
|
|
32
|
+
size: 8,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns a decoder for 64-bit unsigned integers (`u64`).
|
|
37
|
+
*
|
|
38
|
+
* This decoder deserializes `u64` values from 8 bytes.
|
|
39
|
+
* The decoded value is always a `bigint`.
|
|
40
|
+
*
|
|
41
|
+
* For more details, see {@link getU64Codec}.
|
|
42
|
+
*
|
|
43
|
+
* @param config - Optional configuration to specify endianness (little by default).
|
|
44
|
+
* @returns A `FixedSizeDecoder<bigint, 8>` for decoding `u64` values.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* Decoding a `u64` value.
|
|
48
|
+
* ```ts
|
|
49
|
+
* const decoder = getU64Decoder();
|
|
50
|
+
* const value = decoder.decode(new Uint8Array([0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])); // 42n
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @see {@link getU64Codec}
|
|
54
|
+
*/
|
|
55
|
+
export const getU64Decoder = (config: NumberCodecConfig = {}): FixedSizeDecoder<bigint, 8> =>
|
|
56
|
+
numberDecoderFactory({
|
|
57
|
+
config,
|
|
58
|
+
get: (view, le) => view.getBigUint64(0, le),
|
|
59
|
+
name: 'u64',
|
|
60
|
+
size: 8,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns a codec for encoding and decoding 64-bit unsigned integers (`u64`).
|
|
65
|
+
*
|
|
66
|
+
* This codec serializes `u64` values using 8 bytes.
|
|
67
|
+
* Values can be provided as either `number` or `bigint`, but the decoded value is always a `bigint`.
|
|
68
|
+
*
|
|
69
|
+
* @param config - Optional configuration to specify endianness (little by default).
|
|
70
|
+
* @returns A `FixedSizeCodec<number | bigint, bigint, 8>` for encoding and decoding `u64` values.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* Encoding and decoding a `u64` value.
|
|
74
|
+
* ```ts
|
|
75
|
+
* const codec = getU64Codec();
|
|
76
|
+
* const bytes = codec.encode(42); // 0x2a00000000000000
|
|
77
|
+
* const value = codec.decode(bytes); // 42n
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* Using big-endian encoding.
|
|
82
|
+
* ```ts
|
|
83
|
+
* const codec = getU64Codec({ endian: Endian.Big });
|
|
84
|
+
* const bytes = codec.encode(42); // 0x000000000000002a
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* This codec supports values between `0` and `2^64 - 1`.
|
|
89
|
+
* Since JavaScript `number` cannot safely represent values beyond `2^53 - 1`, the decoded value is always a `bigint`.
|
|
90
|
+
*
|
|
91
|
+
* - If you need a smaller unsigned integer, consider using {@link getU32Codec} or {@link getU16Codec}.
|
|
92
|
+
* - If you need a larger unsigned integer, consider using {@link getU128Codec}.
|
|
93
|
+
* - If you need signed integers, consider using {@link getI64Codec}.
|
|
94
|
+
*
|
|
95
|
+
* Separate {@link getU64Encoder} and {@link getU64Decoder} functions are available.
|
|
96
|
+
*
|
|
97
|
+
* ```ts
|
|
98
|
+
* const bytes = getU64Encoder().encode(42);
|
|
99
|
+
* const value = getU64Decoder().decode(bytes);
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @see {@link getU64Encoder}
|
|
103
|
+
* @see {@link getU64Decoder}
|
|
104
|
+
*/
|
|
105
|
+
export const getU64Codec = (config: NumberCodecConfig = {}): FixedSizeCodec<bigint | number, bigint, 8> =>
|
|
106
|
+
combineCodec(getU64Encoder(config), getU64Decoder(config));
|
package/src/u8.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { combineCodec, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from '@solana/codecs-core';
|
|
2
|
+
|
|
3
|
+
import { numberDecoderFactory, numberEncoderFactory } from './utils';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns an encoder for 8-bit unsigned integers (`u8`).
|
|
7
|
+
*
|
|
8
|
+
* This encoder serializes `u8` values using a single byte.
|
|
9
|
+
*
|
|
10
|
+
* For more details, see {@link getU8Codec}.
|
|
11
|
+
*
|
|
12
|
+
* @returns A `FixedSizeEncoder<number | bigint, 1>` for encoding `u8` values.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Encoding a `u8` value.
|
|
16
|
+
* ```ts
|
|
17
|
+
* const encoder = getU8Encoder();
|
|
18
|
+
* const bytes = encoder.encode(42); // 0x2a
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @see {@link getU8Codec}
|
|
22
|
+
*/
|
|
23
|
+
export const getU8Encoder = (): FixedSizeEncoder<bigint | number, 1> =>
|
|
24
|
+
numberEncoderFactory({
|
|
25
|
+
name: 'u8',
|
|
26
|
+
range: [0, Number('0xff')],
|
|
27
|
+
set: (view, value) => view.setUint8(0, Number(value)),
|
|
28
|
+
size: 1,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns a decoder for 8-bit unsigned integers (`u8`).
|
|
33
|
+
*
|
|
34
|
+
* This decoder deserializes `u8` values from a single byte.
|
|
35
|
+
*
|
|
36
|
+
* For more details, see {@link getU8Codec}.
|
|
37
|
+
*
|
|
38
|
+
* @returns A `FixedSizeDecoder<number, 1>` for decoding `u8` values.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* Decoding a `u8` value.
|
|
42
|
+
* ```ts
|
|
43
|
+
* const decoder = getU8Decoder();
|
|
44
|
+
* const value = decoder.decode(new Uint8Array([0xff])); // 255
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @see {@link getU8Codec}
|
|
48
|
+
*/
|
|
49
|
+
export const getU8Decoder = (): FixedSizeDecoder<number, 1> =>
|
|
50
|
+
numberDecoderFactory({
|
|
51
|
+
get: view => view.getUint8(0),
|
|
52
|
+
name: 'u8',
|
|
53
|
+
size: 1,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns a codec for encoding and decoding 8-bit unsigned integers (`u8`).
|
|
58
|
+
*
|
|
59
|
+
* This codec serializes `u8` values using a single byte.
|
|
60
|
+
*
|
|
61
|
+
* @returns A `FixedSizeCodec<number | bigint, number, 1>` for encoding and decoding `u8` values.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* Encoding and decoding a `u8` value.
|
|
65
|
+
* ```ts
|
|
66
|
+
* const codec = getU8Codec();
|
|
67
|
+
* const bytes = codec.encode(255); // 0xff
|
|
68
|
+
* const value = codec.decode(bytes); // 255
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* This codec supports values between `0` and `2^8 - 1` (0 to 255).
|
|
73
|
+
* If you need larger integers, consider using {@link getU16Codec}, {@link getU32Codec}, or {@link getU64Codec}.
|
|
74
|
+
* For signed integers, use {@link getI8Codec}.
|
|
75
|
+
*
|
|
76
|
+
* Separate {@link getU8Encoder} and {@link getU8Decoder} functions are available.
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* const bytes = getU8Encoder().encode(42);
|
|
80
|
+
* const value = getU8Decoder().decode(bytes);
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @see {@link getU8Encoder}
|
|
84
|
+
* @see {@link getU8Decoder}
|
|
85
|
+
*/
|
|
86
|
+
export const getU8Codec = (): FixedSizeCodec<bigint | number, number, 1> =>
|
|
87
|
+
combineCodec(getU8Encoder(), getU8Decoder());
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertByteArrayHasEnoughBytesForCodec,
|
|
3
|
+
assertByteArrayIsNotEmptyForCodec,
|
|
4
|
+
createDecoder,
|
|
5
|
+
createEncoder,
|
|
6
|
+
FixedSizeDecoder,
|
|
7
|
+
FixedSizeEncoder,
|
|
8
|
+
Offset,
|
|
9
|
+
toArrayBuffer,
|
|
10
|
+
} from '@solana/codecs-core';
|
|
11
|
+
|
|
12
|
+
import { assertNumberIsBetweenForCodec } from './assertions';
|
|
13
|
+
import { Endian, NumberCodecConfig } from './common';
|
|
14
|
+
|
|
15
|
+
type NumberFactorySharedInput<TSize extends number> = {
|
|
16
|
+
config?: NumberCodecConfig;
|
|
17
|
+
name: string;
|
|
18
|
+
size: TSize;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type NumberFactoryEncoderInput<TFrom, TSize extends number> = NumberFactorySharedInput<TSize> & {
|
|
22
|
+
range?: [bigint | number, bigint | number];
|
|
23
|
+
set: (view: DataView, value: TFrom, littleEndian?: boolean) => void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type NumberFactoryDecoderInput<TTo, TSize extends number> = NumberFactorySharedInput<TSize> & {
|
|
27
|
+
get: (view: DataView, littleEndian?: boolean) => TTo;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function isLittleEndian(config?: NumberCodecConfig): boolean {
|
|
31
|
+
return config?.endian === Endian.Big ? false : true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function numberEncoderFactory<TFrom extends bigint | number, TSize extends number>(
|
|
35
|
+
input: NumberFactoryEncoderInput<TFrom, TSize>,
|
|
36
|
+
): FixedSizeEncoder<TFrom, TSize> {
|
|
37
|
+
return createEncoder({
|
|
38
|
+
fixedSize: input.size,
|
|
39
|
+
write(value: TFrom, bytes: Uint8Array, offset: Offset): Offset {
|
|
40
|
+
if (input.range) {
|
|
41
|
+
assertNumberIsBetweenForCodec(input.name, input.range[0], input.range[1], value);
|
|
42
|
+
}
|
|
43
|
+
const arrayBuffer = new ArrayBuffer(input.size);
|
|
44
|
+
input.set(new DataView(arrayBuffer), value, isLittleEndian(input.config));
|
|
45
|
+
bytes.set(new Uint8Array(arrayBuffer), offset);
|
|
46
|
+
return offset + input.size;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function numberDecoderFactory<TTo extends bigint | number, TSize extends number>(
|
|
52
|
+
input: NumberFactoryDecoderInput<TTo, TSize>,
|
|
53
|
+
): FixedSizeDecoder<TTo, TSize> {
|
|
54
|
+
return createDecoder({
|
|
55
|
+
fixedSize: input.size,
|
|
56
|
+
read(bytes, offset = 0): [TTo, number] {
|
|
57
|
+
assertByteArrayIsNotEmptyForCodec(input.name, bytes, offset);
|
|
58
|
+
assertByteArrayHasEnoughBytesForCodec(input.name, input.size, bytes, offset);
|
|
59
|
+
const view = new DataView(toArrayBuffer(bytes, offset, input.size));
|
|
60
|
+
return [input.get(view, isLittleEndian(input.config)), offset + input.size];
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|