@solana/codecs-core 8.3.0 → 8.4.0-canary-20260918134912
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/dist/types/index.d.ts +43 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +43 -1
package/dist/types/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
* - [`@solana/codecs-data-structures`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-data-structures) for many data structure codecs such as objects, arrays, tuples, sets, maps, enums, discriminated unions, booleans, etc.
|
|
32
32
|
* - [`@solana/options`](https://github.com/anza-xyz/kit/tree/main/packages/options) for a Rust-like `Option` type and associated codec.
|
|
33
33
|
*
|
|
34
|
-
* You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `transformCodec`, `fixCodecSize` or `reverseCodec` that create new codecs from existing ones.
|
|
34
|
+
* You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `transformCodec`, `tapCodec`, `fixCodecSize` or `reverseCodec` that create new codecs from existing ones.
|
|
35
35
|
*
|
|
36
36
|
* Note that all of these libraries are included in the [`@solana/codecs` package](https://github.com/anza-xyz/kit/tree/main/packages/codecs) as well as the main `@solana/kit` package for your convenience.
|
|
37
37
|
*
|
|
@@ -327,6 +327,48 @@
|
|
|
327
327
|
* const getStringU32Codec = () => combineCodec(getStringU32Encoder(), getStringU32Decoder());
|
|
328
328
|
* ```
|
|
329
329
|
*
|
|
330
|
+
* ## Tapping codecs
|
|
331
|
+
*
|
|
332
|
+
* Whilst transforming codecs lets you _change_ the values or bytes that flow through them, tapping codecs lets you _observe_ them without changing anything. This is mostly useful for adding validation guards to existing codecs: a tap that throws aborts the operation, whilst a tap that returns normally lets the value or bytes pass through untouched.
|
|
333
|
+
*
|
|
334
|
+
* The `tapEncoder`, `tapDecoder` and `tapCodec` helpers observe the **value**. The encoder taps the input value before encoding it and the decoder taps the decoded value after decoding it.
|
|
335
|
+
*
|
|
336
|
+
* ```ts
|
|
337
|
+
* // Reject out-of-range input before it is encoded.
|
|
338
|
+
* const boundedU8 = tapEncoder(getU8Encoder(), value => {
|
|
339
|
+
* if (value > 100) throw new Error('Value must not exceed 100');
|
|
340
|
+
* });
|
|
341
|
+
*
|
|
342
|
+
* // Validate the decoded result.
|
|
343
|
+
* const nonZeroU8 = tapDecoder(getU8Decoder(), value => {
|
|
344
|
+
* if (value === 0) throw new Error('Value must not be zero');
|
|
345
|
+
* });
|
|
346
|
+
* ```
|
|
347
|
+
*
|
|
348
|
+
* The `tapEncoderBytes`, `tapDecoderBytes` and `tapCodecBytes` helpers observe the raw **bytes** instead. The encoder taps the encoded bytes after writing them — alongside the offsets before and after the value was written — and the decoder taps the bytes before reading them. In both cases the bytes are provided as a `ReadonlyUint8Array` so they cannot be modified.
|
|
349
|
+
*
|
|
350
|
+
* ```ts
|
|
351
|
+
* // Guard malformed boolean bytes before decoding.
|
|
352
|
+
* const safeBoolean = tapDecoderBytes(getBooleanDecoder(), (bytes, offset) => {
|
|
353
|
+
* if (bytes[offset] > 1) throw new Error('Expected a 0 or a 1 for booleans');
|
|
354
|
+
* });
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* The `tapCodec` and `tapCodecBytes` helpers accept a tap for each side — the encode-side tap is required whilst the decode-side tap is optional — mirroring the `transformCodec` signature. Since each helper preserves the exact type of the codec it wraps, you can also compose separate encoder and decoder taps using `combineCodec`.
|
|
358
|
+
*
|
|
359
|
+
* ```ts
|
|
360
|
+
* const safeBooleanCodec = combineCodec(
|
|
361
|
+
* // Guard the value on the encode side.
|
|
362
|
+
* tapEncoder(getBooleanEncoder(), value => {
|
|
363
|
+
* if (typeof value !== 'boolean') throw new Error('Expected a boolean');
|
|
364
|
+
* }),
|
|
365
|
+
* // Guard the raw bytes on the decode side.
|
|
366
|
+
* tapDecoderBytes(getBooleanDecoder(), (bytes, offset) => {
|
|
367
|
+
* if (bytes[offset] > 1) throw new Error('Expected a 0 or a 1 for booleans');
|
|
368
|
+
* }),
|
|
369
|
+
* );
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
330
372
|
* ## Fixing the size of codecs
|
|
331
373
|
*
|
|
332
374
|
* The `fixCodecSize` function allows you to bind the size of a given codec to the given fixed size.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsrBG;AACH,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/codecs-core",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.0-canary-20260918134912",
|
|
4
4
|
"description": "Core types and helpers for encoding and decoding byte arrays on Solana",
|
|
5
5
|
"homepage": "https://www.solanakit.com/api#solanacodecs-core",
|
|
6
6
|
"exports": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"maintained node versions"
|
|
57
57
|
],
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@solana/errors": "8.
|
|
59
|
+
"@solana/errors": "8.4.0-canary-20260918134912"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"typescript": ">=5.4.0"
|
package/src/index.ts
CHANGED
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
* - [`@solana/codecs-data-structures`](https://github.com/anza-xyz/kit/tree/main/packages/codecs-data-structures) for many data structure codecs such as objects, arrays, tuples, sets, maps, enums, discriminated unions, booleans, etc.
|
|
32
32
|
* - [`@solana/options`](https://github.com/anza-xyz/kit/tree/main/packages/options) for a Rust-like `Option` type and associated codec.
|
|
33
33
|
*
|
|
34
|
-
* You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `transformCodec`, `fixCodecSize` or `reverseCodec` that create new codecs from existing ones.
|
|
34
|
+
* You may also be interested in some of the helpers of this `@solana/codecs-core` library such as `transformCodec`, `tapCodec`, `fixCodecSize` or `reverseCodec` that create new codecs from existing ones.
|
|
35
35
|
*
|
|
36
36
|
* Note that all of these libraries are included in the [`@solana/codecs` package](https://github.com/anza-xyz/kit/tree/main/packages/codecs) as well as the main `@solana/kit` package for your convenience.
|
|
37
37
|
*
|
|
@@ -327,6 +327,48 @@
|
|
|
327
327
|
* const getStringU32Codec = () => combineCodec(getStringU32Encoder(), getStringU32Decoder());
|
|
328
328
|
* ```
|
|
329
329
|
*
|
|
330
|
+
* ## Tapping codecs
|
|
331
|
+
*
|
|
332
|
+
* Whilst transforming codecs lets you _change_ the values or bytes that flow through them, tapping codecs lets you _observe_ them without changing anything. This is mostly useful for adding validation guards to existing codecs: a tap that throws aborts the operation, whilst a tap that returns normally lets the value or bytes pass through untouched.
|
|
333
|
+
*
|
|
334
|
+
* The `tapEncoder`, `tapDecoder` and `tapCodec` helpers observe the **value**. The encoder taps the input value before encoding it and the decoder taps the decoded value after decoding it.
|
|
335
|
+
*
|
|
336
|
+
* ```ts
|
|
337
|
+
* // Reject out-of-range input before it is encoded.
|
|
338
|
+
* const boundedU8 = tapEncoder(getU8Encoder(), value => {
|
|
339
|
+
* if (value > 100) throw new Error('Value must not exceed 100');
|
|
340
|
+
* });
|
|
341
|
+
*
|
|
342
|
+
* // Validate the decoded result.
|
|
343
|
+
* const nonZeroU8 = tapDecoder(getU8Decoder(), value => {
|
|
344
|
+
* if (value === 0) throw new Error('Value must not be zero');
|
|
345
|
+
* });
|
|
346
|
+
* ```
|
|
347
|
+
*
|
|
348
|
+
* The `tapEncoderBytes`, `tapDecoderBytes` and `tapCodecBytes` helpers observe the raw **bytes** instead. The encoder taps the encoded bytes after writing them — alongside the offsets before and after the value was written — and the decoder taps the bytes before reading them. In both cases the bytes are provided as a `ReadonlyUint8Array` so they cannot be modified.
|
|
349
|
+
*
|
|
350
|
+
* ```ts
|
|
351
|
+
* // Guard malformed boolean bytes before decoding.
|
|
352
|
+
* const safeBoolean = tapDecoderBytes(getBooleanDecoder(), (bytes, offset) => {
|
|
353
|
+
* if (bytes[offset] > 1) throw new Error('Expected a 0 or a 1 for booleans');
|
|
354
|
+
* });
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* The `tapCodec` and `tapCodecBytes` helpers accept a tap for each side — the encode-side tap is required whilst the decode-side tap is optional — mirroring the `transformCodec` signature. Since each helper preserves the exact type of the codec it wraps, you can also compose separate encoder and decoder taps using `combineCodec`.
|
|
358
|
+
*
|
|
359
|
+
* ```ts
|
|
360
|
+
* const safeBooleanCodec = combineCodec(
|
|
361
|
+
* // Guard the value on the encode side.
|
|
362
|
+
* tapEncoder(getBooleanEncoder(), value => {
|
|
363
|
+
* if (typeof value !== 'boolean') throw new Error('Expected a boolean');
|
|
364
|
+
* }),
|
|
365
|
+
* // Guard the raw bytes on the decode side.
|
|
366
|
+
* tapDecoderBytes(getBooleanDecoder(), (bytes, offset) => {
|
|
367
|
+
* if (bytes[offset] > 1) throw new Error('Expected a 0 or a 1 for booleans');
|
|
368
|
+
* }),
|
|
369
|
+
* );
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
330
372
|
* ## Fixing the size of codecs
|
|
331
373
|
*
|
|
332
374
|
* The `fixCodecSize` function allows you to bind the size of a given codec to the given fixed size.
|