@solana/codecs-core
Advanced tools
Comparing version 2.0.0-experimental.735654a to 2.0.0-experimental.745437f
@@ -13,7 +13,2 @@ // src/assertions.ts | ||
} | ||
function assertFixedSizeCodec(data, message) { | ||
if (data.fixedSize === null) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
@@ -47,5 +42,56 @@ // src/bytes.ts | ||
// src/codec.ts | ||
function getEncodedSize(value, encoder) { | ||
return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value); | ||
} | ||
function createEncoder(encoder) { | ||
return Object.freeze({ | ||
...encoder, | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, encoder)); | ||
encoder.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function createDecoder(decoder) { | ||
return Object.freeze({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0] | ||
}); | ||
} | ||
function createCodec(codec) { | ||
return Object.freeze({ | ||
...codec, | ||
decode: (bytes, offset = 0) => codec.read(bytes, offset)[0], | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, codec)); | ||
codec.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function isFixedSize(codec) { | ||
return "fixedSize" in codec && typeof codec.fixedSize === "number"; | ||
} | ||
function assertIsFixedSize(codec, message) { | ||
if (!isFixedSize(codec)) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
function isVariableSize(codec) { | ||
return !isFixedSize(codec); | ||
} | ||
function assertIsVariableSize(codec, message) { | ||
if (!isVariableSize(codec)) { | ||
throw new Error(message ?? "Expected a variable-size codec, got a fixed-size one."); | ||
} | ||
} | ||
// src/combine-codec.ts | ||
function combineCodec(encoder, decoder, description) { | ||
if (encoder.fixedSize !== decoder.fixedSize) { | ||
function combineCodec(encoder, decoder) { | ||
if (isFixedSize(encoder) !== isFixedSize(decoder)) { | ||
throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`); | ||
} | ||
if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) { | ||
throw new Error( | ||
@@ -55,3 +101,3 @@ `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].` | ||
} | ||
if (encoder.maxSize !== decoder.maxSize) { | ||
if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) { | ||
throw new Error( | ||
@@ -61,13 +107,9 @@ `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].` | ||
} | ||
if (description === void 0 && encoder.description !== decoder.description) { | ||
throw new Error( | ||
`Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.` | ||
); | ||
} | ||
return { | ||
...decoder, | ||
...encoder, | ||
decode: decoder.decode, | ||
description: description ?? encoder.description, | ||
encode: encoder.encode, | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
read: decoder.read, | ||
write: encoder.write | ||
}; | ||
@@ -77,19 +119,17 @@ } | ||
// src/fix-codec.ts | ||
function fixCodecHelper(data, fixedBytes, description) { | ||
return { | ||
description: description ?? `fixed(${fixedBytes}, ${data.description})`, | ||
function fixEncoder(encoder, fixedBytes) { | ||
return createEncoder({ | ||
fixedSize: fixedBytes, | ||
maxSize: fixedBytes | ||
}; | ||
write: (value, bytes, offset) => { | ||
const variableByteArray = encoder.encode(value); | ||
const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray; | ||
bytes.set(fixedByteArray, offset); | ||
return offset + fixedBytes; | ||
} | ||
}); | ||
} | ||
function fixEncoder(encoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(encoder, fixedBytes, description), | ||
encode: (value) => fixBytes(encoder.encode(value), fixedBytes) | ||
}; | ||
} | ||
function fixDecoder(decoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(decoder, fixedBytes, description), | ||
decode: (bytes, offset = 0) => { | ||
function fixDecoder(decoder, fixedBytes) { | ||
return createDecoder({ | ||
fixedSize: fixedBytes, | ||
read: (bytes, offset) => { | ||
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset); | ||
@@ -99,12 +139,12 @@ if (offset > 0 || bytes.length > fixedBytes) { | ||
} | ||
if (decoder.fixedSize !== null) { | ||
if (isFixedSize(decoder)) { | ||
bytes = fixBytes(bytes, decoder.fixedSize); | ||
} | ||
const [value] = decoder.decode(bytes, 0); | ||
const [value] = decoder.read(bytes, 0); | ||
return [value, offset + fixedBytes]; | ||
} | ||
}; | ||
}); | ||
} | ||
function fixCodec(codec, fixedBytes, description) { | ||
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description)); | ||
function fixCodec(codec, fixedBytes) { | ||
return combineCodec(fixEncoder(codec, fixedBytes), fixDecoder(codec, fixedBytes)); | ||
} | ||
@@ -114,28 +154,21 @@ | ||
function mapEncoder(encoder, unmap) { | ||
return { | ||
description: encoder.description, | ||
encode: (value) => encoder.encode(unmap(value)), | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
}; | ||
return createEncoder({ | ||
...isVariableSize(encoder) ? { ...encoder, getSizeFromValue: (value) => encoder.getSizeFromValue(unmap(value)) } : encoder, | ||
write: (value, bytes, offset) => encoder.write(unmap(value), bytes, offset) | ||
}); | ||
} | ||
function mapDecoder(decoder, map) { | ||
return { | ||
decode: (bytes, offset = 0) => { | ||
const [value, length] = decoder.decode(bytes, offset); | ||
return [map(value, bytes, offset), length]; | ||
}, | ||
description: decoder.description, | ||
fixedSize: decoder.fixedSize, | ||
maxSize: decoder.maxSize | ||
}; | ||
return createDecoder({ | ||
...decoder, | ||
read: (bytes, offset) => { | ||
const [value, newOffset] = decoder.read(bytes, offset); | ||
return [map(value, bytes, offset), newOffset]; | ||
} | ||
}); | ||
} | ||
function mapCodec(codec, unmap, map) { | ||
return { | ||
decode: map ? mapDecoder(codec, map).decode : codec.decode, | ||
description: codec.description, | ||
encode: mapEncoder(codec, unmap).encode, | ||
fixedSize: codec.fixedSize, | ||
maxSize: codec.maxSize | ||
}; | ||
return createCodec({ | ||
...mapEncoder(codec, unmap), | ||
read: map ? mapDecoder(codec, map).read : codec.read | ||
}); | ||
} | ||
@@ -145,25 +178,27 @@ | ||
function reverseEncoder(encoder) { | ||
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(encoder, "Cannot reverse a codec of variable size."); | ||
return createEncoder({ | ||
...encoder, | ||
encode: (value) => encoder.encode(value).reverse() | ||
}; | ||
write: (value, bytes, offset) => { | ||
const newOffset = encoder.write(value, bytes, offset); | ||
const slice = bytes.slice(offset, offset + encoder.fixedSize).reverse(); | ||
bytes.set(slice, offset); | ||
return newOffset; | ||
} | ||
}); | ||
} | ||
function reverseDecoder(decoder) { | ||
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(decoder, "Cannot reverse a codec of variable size."); | ||
return createDecoder({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => { | ||
read: (bytes, offset) => { | ||
const reverseEnd = offset + decoder.fixedSize; | ||
if (offset === 0 && bytes.length === reverseEnd) { | ||
return decoder.decode(bytes.reverse(), offset); | ||
return decoder.read(bytes.reverse(), offset); | ||
} | ||
const newBytes = mergeBytes([ | ||
...offset === 0 ? [] : [bytes.slice(0, offset)], | ||
bytes.slice(offset, reverseEnd).reverse(), | ||
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)] | ||
]); | ||
return decoder.decode(newBytes, offset); | ||
const reversedBytes = bytes.slice(); | ||
reversedBytes.set(bytes.slice(offset, reverseEnd).reverse(), offset); | ||
return decoder.read(reversedBytes, offset); | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -174,4 +209,2 @@ function reverseCodec(codec) { | ||
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertFixedSizeCodec, combineCodec, fixBytes, fixCodec, fixDecoder, fixEncoder, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.browser.js.map | ||
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertIsFixedSize, assertIsVariableSize, combineCodec, createCodec, createDecoder, createEncoder, fixBytes, fixCodec, fixDecoder, fixEncoder, getEncodedSize, isFixedSize, isVariableSize, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; |
@@ -17,7 +17,2 @@ this.globalThis = this.globalThis || {}; | ||
} | ||
function assertFixedSizeCodec(data, message) { | ||
if (data.fixedSize === null) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
@@ -51,5 +46,56 @@ // src/bytes.ts | ||
// src/codec.ts | ||
function getEncodedSize(value, encoder) { | ||
return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value); | ||
} | ||
function createEncoder(encoder) { | ||
return Object.freeze({ | ||
...encoder, | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, encoder)); | ||
encoder.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function createDecoder(decoder) { | ||
return Object.freeze({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0] | ||
}); | ||
} | ||
function createCodec(codec) { | ||
return Object.freeze({ | ||
...codec, | ||
decode: (bytes, offset = 0) => codec.read(bytes, offset)[0], | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, codec)); | ||
codec.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function isFixedSize(codec) { | ||
return "fixedSize" in codec && typeof codec.fixedSize === "number"; | ||
} | ||
function assertIsFixedSize(codec, message) { | ||
if (!isFixedSize(codec)) { | ||
throw new Error(message != null ? message : "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
function isVariableSize(codec) { | ||
return !isFixedSize(codec); | ||
} | ||
function assertIsVariableSize(codec, message) { | ||
if (!isVariableSize(codec)) { | ||
throw new Error(message != null ? message : "Expected a variable-size codec, got a fixed-size one."); | ||
} | ||
} | ||
// src/combine-codec.ts | ||
function combineCodec(encoder, decoder, description) { | ||
if (encoder.fixedSize !== decoder.fixedSize) { | ||
function combineCodec(encoder, decoder) { | ||
if (isFixedSize(encoder) !== isFixedSize(decoder)) { | ||
throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`); | ||
} | ||
if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) { | ||
throw new Error( | ||
@@ -59,3 +105,3 @@ `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].` | ||
} | ||
if (encoder.maxSize !== decoder.maxSize) { | ||
if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) { | ||
throw new Error( | ||
@@ -65,13 +111,9 @@ `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].` | ||
} | ||
if (description === void 0 && encoder.description !== decoder.description) { | ||
throw new Error( | ||
`Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.` | ||
); | ||
} | ||
return { | ||
...decoder, | ||
...encoder, | ||
decode: decoder.decode, | ||
description: description ?? encoder.description, | ||
encode: encoder.encode, | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
read: decoder.read, | ||
write: encoder.write | ||
}; | ||
@@ -81,19 +123,17 @@ } | ||
// src/fix-codec.ts | ||
function fixCodecHelper(data, fixedBytes, description) { | ||
return { | ||
description: description ?? `fixed(${fixedBytes}, ${data.description})`, | ||
function fixEncoder(encoder, fixedBytes) { | ||
return createEncoder({ | ||
fixedSize: fixedBytes, | ||
maxSize: fixedBytes | ||
}; | ||
write: (value, bytes, offset) => { | ||
const variableByteArray = encoder.encode(value); | ||
const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray; | ||
bytes.set(fixedByteArray, offset); | ||
return offset + fixedBytes; | ||
} | ||
}); | ||
} | ||
function fixEncoder(encoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(encoder, fixedBytes, description), | ||
encode: (value) => fixBytes(encoder.encode(value), fixedBytes) | ||
}; | ||
} | ||
function fixDecoder(decoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(decoder, fixedBytes, description), | ||
decode: (bytes, offset = 0) => { | ||
function fixDecoder(decoder, fixedBytes) { | ||
return createDecoder({ | ||
fixedSize: fixedBytes, | ||
read: (bytes, offset) => { | ||
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset); | ||
@@ -103,12 +143,12 @@ if (offset > 0 || bytes.length > fixedBytes) { | ||
} | ||
if (decoder.fixedSize !== null) { | ||
if (isFixedSize(decoder)) { | ||
bytes = fixBytes(bytes, decoder.fixedSize); | ||
} | ||
const [value] = decoder.decode(bytes, 0); | ||
const [value] = decoder.read(bytes, 0); | ||
return [value, offset + fixedBytes]; | ||
} | ||
}; | ||
}); | ||
} | ||
function fixCodec(codec, fixedBytes, description) { | ||
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description)); | ||
function fixCodec(codec, fixedBytes) { | ||
return combineCodec(fixEncoder(codec, fixedBytes), fixDecoder(codec, fixedBytes)); | ||
} | ||
@@ -118,28 +158,21 @@ | ||
function mapEncoder(encoder, unmap) { | ||
return { | ||
description: encoder.description, | ||
encode: (value) => encoder.encode(unmap(value)), | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
}; | ||
return createEncoder({ | ||
...isVariableSize(encoder) ? { ...encoder, getSizeFromValue: (value) => encoder.getSizeFromValue(unmap(value)) } : encoder, | ||
write: (value, bytes, offset) => encoder.write(unmap(value), bytes, offset) | ||
}); | ||
} | ||
function mapDecoder(decoder, map) { | ||
return { | ||
decode: (bytes, offset = 0) => { | ||
const [value, length] = decoder.decode(bytes, offset); | ||
return [map(value, bytes, offset), length]; | ||
}, | ||
description: decoder.description, | ||
fixedSize: decoder.fixedSize, | ||
maxSize: decoder.maxSize | ||
}; | ||
return createDecoder({ | ||
...decoder, | ||
read: (bytes, offset) => { | ||
const [value, newOffset] = decoder.read(bytes, offset); | ||
return [map(value, bytes, offset), newOffset]; | ||
} | ||
}); | ||
} | ||
function mapCodec(codec, unmap, map) { | ||
return { | ||
decode: map ? mapDecoder(codec, map).decode : codec.decode, | ||
description: codec.description, | ||
encode: mapEncoder(codec, unmap).encode, | ||
fixedSize: codec.fixedSize, | ||
maxSize: codec.maxSize | ||
}; | ||
return createCodec({ | ||
...mapEncoder(codec, unmap), | ||
read: map ? mapDecoder(codec, map).read : codec.read | ||
}); | ||
} | ||
@@ -149,25 +182,27 @@ | ||
function reverseEncoder(encoder) { | ||
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(encoder, "Cannot reverse a codec of variable size."); | ||
return createEncoder({ | ||
...encoder, | ||
encode: (value) => encoder.encode(value).reverse() | ||
}; | ||
write: (value, bytes, offset) => { | ||
const newOffset = encoder.write(value, bytes, offset); | ||
const slice = bytes.slice(offset, offset + encoder.fixedSize).reverse(); | ||
bytes.set(slice, offset); | ||
return newOffset; | ||
} | ||
}); | ||
} | ||
function reverseDecoder(decoder) { | ||
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(decoder, "Cannot reverse a codec of variable size."); | ||
return createDecoder({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => { | ||
read: (bytes, offset) => { | ||
const reverseEnd = offset + decoder.fixedSize; | ||
if (offset === 0 && bytes.length === reverseEnd) { | ||
return decoder.decode(bytes.reverse(), offset); | ||
return decoder.read(bytes.reverse(), offset); | ||
} | ||
const newBytes = mergeBytes([ | ||
...offset === 0 ? [] : [bytes.slice(0, offset)], | ||
bytes.slice(offset, reverseEnd).reverse(), | ||
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)] | ||
]); | ||
return decoder.decode(newBytes, offset); | ||
const reversedBytes = bytes.slice(); | ||
reversedBytes.set(bytes.slice(offset, reverseEnd).reverse(), offset); | ||
return decoder.read(reversedBytes, offset); | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -180,4 +215,8 @@ function reverseCodec(codec) { | ||
exports.assertByteArrayIsNotEmptyForCodec = assertByteArrayIsNotEmptyForCodec; | ||
exports.assertFixedSizeCodec = assertFixedSizeCodec; | ||
exports.assertIsFixedSize = assertIsFixedSize; | ||
exports.assertIsVariableSize = assertIsVariableSize; | ||
exports.combineCodec = combineCodec; | ||
exports.createCodec = createCodec; | ||
exports.createDecoder = createDecoder; | ||
exports.createEncoder = createEncoder; | ||
exports.fixBytes = fixBytes; | ||
@@ -187,2 +226,5 @@ exports.fixCodec = fixCodec; | ||
exports.fixEncoder = fixEncoder; | ||
exports.getEncodedSize = getEncodedSize; | ||
exports.isFixedSize = isFixedSize; | ||
exports.isVariableSize = isVariableSize; | ||
exports.mapCodec = mapCodec; | ||
@@ -189,0 +231,0 @@ exports.mapDecoder = mapDecoder; |
@@ -13,7 +13,2 @@ // src/assertions.ts | ||
} | ||
function assertFixedSizeCodec(data, message) { | ||
if (data.fixedSize === null) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
@@ -47,5 +42,56 @@ // src/bytes.ts | ||
// src/codec.ts | ||
function getEncodedSize(value, encoder) { | ||
return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value); | ||
} | ||
function createEncoder(encoder) { | ||
return Object.freeze({ | ||
...encoder, | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, encoder)); | ||
encoder.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function createDecoder(decoder) { | ||
return Object.freeze({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0] | ||
}); | ||
} | ||
function createCodec(codec) { | ||
return Object.freeze({ | ||
...codec, | ||
decode: (bytes, offset = 0) => codec.read(bytes, offset)[0], | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, codec)); | ||
codec.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function isFixedSize(codec) { | ||
return "fixedSize" in codec && typeof codec.fixedSize === "number"; | ||
} | ||
function assertIsFixedSize(codec, message) { | ||
if (!isFixedSize(codec)) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
function isVariableSize(codec) { | ||
return !isFixedSize(codec); | ||
} | ||
function assertIsVariableSize(codec, message) { | ||
if (!isVariableSize(codec)) { | ||
throw new Error(message ?? "Expected a variable-size codec, got a fixed-size one."); | ||
} | ||
} | ||
// src/combine-codec.ts | ||
function combineCodec(encoder, decoder, description) { | ||
if (encoder.fixedSize !== decoder.fixedSize) { | ||
function combineCodec(encoder, decoder) { | ||
if (isFixedSize(encoder) !== isFixedSize(decoder)) { | ||
throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`); | ||
} | ||
if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) { | ||
throw new Error( | ||
@@ -55,3 +101,3 @@ `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].` | ||
} | ||
if (encoder.maxSize !== decoder.maxSize) { | ||
if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) { | ||
throw new Error( | ||
@@ -61,13 +107,9 @@ `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].` | ||
} | ||
if (description === void 0 && encoder.description !== decoder.description) { | ||
throw new Error( | ||
`Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.` | ||
); | ||
} | ||
return { | ||
...decoder, | ||
...encoder, | ||
decode: decoder.decode, | ||
description: description ?? encoder.description, | ||
encode: encoder.encode, | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
read: decoder.read, | ||
write: encoder.write | ||
}; | ||
@@ -77,19 +119,17 @@ } | ||
// src/fix-codec.ts | ||
function fixCodecHelper(data, fixedBytes, description) { | ||
return { | ||
description: description ?? `fixed(${fixedBytes}, ${data.description})`, | ||
function fixEncoder(encoder, fixedBytes) { | ||
return createEncoder({ | ||
fixedSize: fixedBytes, | ||
maxSize: fixedBytes | ||
}; | ||
write: (value, bytes, offset) => { | ||
const variableByteArray = encoder.encode(value); | ||
const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray; | ||
bytes.set(fixedByteArray, offset); | ||
return offset + fixedBytes; | ||
} | ||
}); | ||
} | ||
function fixEncoder(encoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(encoder, fixedBytes, description), | ||
encode: (value) => fixBytes(encoder.encode(value), fixedBytes) | ||
}; | ||
} | ||
function fixDecoder(decoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(decoder, fixedBytes, description), | ||
decode: (bytes, offset = 0) => { | ||
function fixDecoder(decoder, fixedBytes) { | ||
return createDecoder({ | ||
fixedSize: fixedBytes, | ||
read: (bytes, offset) => { | ||
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset); | ||
@@ -99,12 +139,12 @@ if (offset > 0 || bytes.length > fixedBytes) { | ||
} | ||
if (decoder.fixedSize !== null) { | ||
if (isFixedSize(decoder)) { | ||
bytes = fixBytes(bytes, decoder.fixedSize); | ||
} | ||
const [value] = decoder.decode(bytes, 0); | ||
const [value] = decoder.read(bytes, 0); | ||
return [value, offset + fixedBytes]; | ||
} | ||
}; | ||
}); | ||
} | ||
function fixCodec(codec, fixedBytes, description) { | ||
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description)); | ||
function fixCodec(codec, fixedBytes) { | ||
return combineCodec(fixEncoder(codec, fixedBytes), fixDecoder(codec, fixedBytes)); | ||
} | ||
@@ -114,28 +154,21 @@ | ||
function mapEncoder(encoder, unmap) { | ||
return { | ||
description: encoder.description, | ||
encode: (value) => encoder.encode(unmap(value)), | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
}; | ||
return createEncoder({ | ||
...isVariableSize(encoder) ? { ...encoder, getSizeFromValue: (value) => encoder.getSizeFromValue(unmap(value)) } : encoder, | ||
write: (value, bytes, offset) => encoder.write(unmap(value), bytes, offset) | ||
}); | ||
} | ||
function mapDecoder(decoder, map) { | ||
return { | ||
decode: (bytes, offset = 0) => { | ||
const [value, length] = decoder.decode(bytes, offset); | ||
return [map(value, bytes, offset), length]; | ||
}, | ||
description: decoder.description, | ||
fixedSize: decoder.fixedSize, | ||
maxSize: decoder.maxSize | ||
}; | ||
return createDecoder({ | ||
...decoder, | ||
read: (bytes, offset) => { | ||
const [value, newOffset] = decoder.read(bytes, offset); | ||
return [map(value, bytes, offset), newOffset]; | ||
} | ||
}); | ||
} | ||
function mapCodec(codec, unmap, map) { | ||
return { | ||
decode: map ? mapDecoder(codec, map).decode : codec.decode, | ||
description: codec.description, | ||
encode: mapEncoder(codec, unmap).encode, | ||
fixedSize: codec.fixedSize, | ||
maxSize: codec.maxSize | ||
}; | ||
return createCodec({ | ||
...mapEncoder(codec, unmap), | ||
read: map ? mapDecoder(codec, map).read : codec.read | ||
}); | ||
} | ||
@@ -145,25 +178,27 @@ | ||
function reverseEncoder(encoder) { | ||
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(encoder, "Cannot reverse a codec of variable size."); | ||
return createEncoder({ | ||
...encoder, | ||
encode: (value) => encoder.encode(value).reverse() | ||
}; | ||
write: (value, bytes, offset) => { | ||
const newOffset = encoder.write(value, bytes, offset); | ||
const slice = bytes.slice(offset, offset + encoder.fixedSize).reverse(); | ||
bytes.set(slice, offset); | ||
return newOffset; | ||
} | ||
}); | ||
} | ||
function reverseDecoder(decoder) { | ||
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(decoder, "Cannot reverse a codec of variable size."); | ||
return createDecoder({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => { | ||
read: (bytes, offset) => { | ||
const reverseEnd = offset + decoder.fixedSize; | ||
if (offset === 0 && bytes.length === reverseEnd) { | ||
return decoder.decode(bytes.reverse(), offset); | ||
return decoder.read(bytes.reverse(), offset); | ||
} | ||
const newBytes = mergeBytes([ | ||
...offset === 0 ? [] : [bytes.slice(0, offset)], | ||
bytes.slice(offset, reverseEnd).reverse(), | ||
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)] | ||
]); | ||
return decoder.decode(newBytes, offset); | ||
const reversedBytes = bytes.slice(); | ||
reversedBytes.set(bytes.slice(offset, reverseEnd).reverse(), offset); | ||
return decoder.read(reversedBytes, offset); | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -174,4 +209,2 @@ function reverseCodec(codec) { | ||
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertFixedSizeCodec, combineCodec, fixBytes, fixCodec, fixDecoder, fixEncoder, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.native.js.map | ||
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertIsFixedSize, assertIsVariableSize, combineCodec, createCodec, createDecoder, createEncoder, fixBytes, fixCodec, fixDecoder, fixEncoder, getEncodedSize, isFixedSize, isVariableSize, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; |
@@ -13,7 +13,2 @@ // src/assertions.ts | ||
} | ||
function assertFixedSizeCodec(data, message) { | ||
if (data.fixedSize === null) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
@@ -47,5 +42,56 @@ // src/bytes.ts | ||
// src/codec.ts | ||
function getEncodedSize(value, encoder) { | ||
return "fixedSize" in encoder ? encoder.fixedSize : encoder.getSizeFromValue(value); | ||
} | ||
function createEncoder(encoder) { | ||
return Object.freeze({ | ||
...encoder, | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, encoder)); | ||
encoder.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function createDecoder(decoder) { | ||
return Object.freeze({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0] | ||
}); | ||
} | ||
function createCodec(codec) { | ||
return Object.freeze({ | ||
...codec, | ||
decode: (bytes, offset = 0) => codec.read(bytes, offset)[0], | ||
encode: (value) => { | ||
const bytes = new Uint8Array(getEncodedSize(value, codec)); | ||
codec.write(value, bytes, 0); | ||
return bytes; | ||
} | ||
}); | ||
} | ||
function isFixedSize(codec) { | ||
return "fixedSize" in codec && typeof codec.fixedSize === "number"; | ||
} | ||
function assertIsFixedSize(codec, message) { | ||
if (!isFixedSize(codec)) { | ||
throw new Error(message ?? "Expected a fixed-size codec, got a variable-size one."); | ||
} | ||
} | ||
function isVariableSize(codec) { | ||
return !isFixedSize(codec); | ||
} | ||
function assertIsVariableSize(codec, message) { | ||
if (!isVariableSize(codec)) { | ||
throw new Error(message ?? "Expected a variable-size codec, got a fixed-size one."); | ||
} | ||
} | ||
// src/combine-codec.ts | ||
function combineCodec(encoder, decoder, description) { | ||
if (encoder.fixedSize !== decoder.fixedSize) { | ||
function combineCodec(encoder, decoder) { | ||
if (isFixedSize(encoder) !== isFixedSize(decoder)) { | ||
throw new Error(`Encoder and decoder must either both be fixed-size or variable-size.`); | ||
} | ||
if (isFixedSize(encoder) && isFixedSize(decoder) && encoder.fixedSize !== decoder.fixedSize) { | ||
throw new Error( | ||
@@ -55,3 +101,3 @@ `Encoder and decoder must have the same fixed size, got [${encoder.fixedSize}] and [${decoder.fixedSize}].` | ||
} | ||
if (encoder.maxSize !== decoder.maxSize) { | ||
if (!isFixedSize(encoder) && !isFixedSize(decoder) && encoder.maxSize !== decoder.maxSize) { | ||
throw new Error( | ||
@@ -61,13 +107,9 @@ `Encoder and decoder must have the same max size, got [${encoder.maxSize}] and [${decoder.maxSize}].` | ||
} | ||
if (description === void 0 && encoder.description !== decoder.description) { | ||
throw new Error( | ||
`Encoder and decoder must have the same description, got [${encoder.description}] and [${decoder.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.` | ||
); | ||
} | ||
return { | ||
...decoder, | ||
...encoder, | ||
decode: decoder.decode, | ||
description: description ?? encoder.description, | ||
encode: encoder.encode, | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
read: decoder.read, | ||
write: encoder.write | ||
}; | ||
@@ -77,19 +119,17 @@ } | ||
// src/fix-codec.ts | ||
function fixCodecHelper(data, fixedBytes, description) { | ||
return { | ||
description: description ?? `fixed(${fixedBytes}, ${data.description})`, | ||
function fixEncoder(encoder, fixedBytes) { | ||
return createEncoder({ | ||
fixedSize: fixedBytes, | ||
maxSize: fixedBytes | ||
}; | ||
write: (value, bytes, offset) => { | ||
const variableByteArray = encoder.encode(value); | ||
const fixedByteArray = variableByteArray.length > fixedBytes ? variableByteArray.slice(0, fixedBytes) : variableByteArray; | ||
bytes.set(fixedByteArray, offset); | ||
return offset + fixedBytes; | ||
} | ||
}); | ||
} | ||
function fixEncoder(encoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(encoder, fixedBytes, description), | ||
encode: (value) => fixBytes(encoder.encode(value), fixedBytes) | ||
}; | ||
} | ||
function fixDecoder(decoder, fixedBytes, description) { | ||
return { | ||
...fixCodecHelper(decoder, fixedBytes, description), | ||
decode: (bytes, offset = 0) => { | ||
function fixDecoder(decoder, fixedBytes) { | ||
return createDecoder({ | ||
fixedSize: fixedBytes, | ||
read: (bytes, offset) => { | ||
assertByteArrayHasEnoughBytesForCodec("fixCodec", fixedBytes, bytes, offset); | ||
@@ -99,12 +139,12 @@ if (offset > 0 || bytes.length > fixedBytes) { | ||
} | ||
if (decoder.fixedSize !== null) { | ||
if (isFixedSize(decoder)) { | ||
bytes = fixBytes(bytes, decoder.fixedSize); | ||
} | ||
const [value] = decoder.decode(bytes, 0); | ||
const [value] = decoder.read(bytes, 0); | ||
return [value, offset + fixedBytes]; | ||
} | ||
}; | ||
}); | ||
} | ||
function fixCodec(codec, fixedBytes, description) { | ||
return combineCodec(fixEncoder(codec, fixedBytes, description), fixDecoder(codec, fixedBytes, description)); | ||
function fixCodec(codec, fixedBytes) { | ||
return combineCodec(fixEncoder(codec, fixedBytes), fixDecoder(codec, fixedBytes)); | ||
} | ||
@@ -114,28 +154,21 @@ | ||
function mapEncoder(encoder, unmap) { | ||
return { | ||
description: encoder.description, | ||
encode: (value) => encoder.encode(unmap(value)), | ||
fixedSize: encoder.fixedSize, | ||
maxSize: encoder.maxSize | ||
}; | ||
return createEncoder({ | ||
...isVariableSize(encoder) ? { ...encoder, getSizeFromValue: (value) => encoder.getSizeFromValue(unmap(value)) } : encoder, | ||
write: (value, bytes, offset) => encoder.write(unmap(value), bytes, offset) | ||
}); | ||
} | ||
function mapDecoder(decoder, map) { | ||
return { | ||
decode: (bytes, offset = 0) => { | ||
const [value, length] = decoder.decode(bytes, offset); | ||
return [map(value, bytes, offset), length]; | ||
}, | ||
description: decoder.description, | ||
fixedSize: decoder.fixedSize, | ||
maxSize: decoder.maxSize | ||
}; | ||
return createDecoder({ | ||
...decoder, | ||
read: (bytes, offset) => { | ||
const [value, newOffset] = decoder.read(bytes, offset); | ||
return [map(value, bytes, offset), newOffset]; | ||
} | ||
}); | ||
} | ||
function mapCodec(codec, unmap, map) { | ||
return { | ||
decode: map ? mapDecoder(codec, map).decode : codec.decode, | ||
description: codec.description, | ||
encode: mapEncoder(codec, unmap).encode, | ||
fixedSize: codec.fixedSize, | ||
maxSize: codec.maxSize | ||
}; | ||
return createCodec({ | ||
...mapEncoder(codec, unmap), | ||
read: map ? mapDecoder(codec, map).read : codec.read | ||
}); | ||
} | ||
@@ -145,25 +178,27 @@ | ||
function reverseEncoder(encoder) { | ||
assertFixedSizeCodec(encoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(encoder, "Cannot reverse a codec of variable size."); | ||
return createEncoder({ | ||
...encoder, | ||
encode: (value) => encoder.encode(value).reverse() | ||
}; | ||
write: (value, bytes, offset) => { | ||
const newOffset = encoder.write(value, bytes, offset); | ||
const slice = bytes.slice(offset, offset + encoder.fixedSize).reverse(); | ||
bytes.set(slice, offset); | ||
return newOffset; | ||
} | ||
}); | ||
} | ||
function reverseDecoder(decoder) { | ||
assertFixedSizeCodec(decoder, "Cannot reverse a codec of variable size."); | ||
return { | ||
assertIsFixedSize(decoder, "Cannot reverse a codec of variable size."); | ||
return createDecoder({ | ||
...decoder, | ||
decode: (bytes, offset = 0) => { | ||
read: (bytes, offset) => { | ||
const reverseEnd = offset + decoder.fixedSize; | ||
if (offset === 0 && bytes.length === reverseEnd) { | ||
return decoder.decode(bytes.reverse(), offset); | ||
return decoder.read(bytes.reverse(), offset); | ||
} | ||
const newBytes = mergeBytes([ | ||
...offset === 0 ? [] : [bytes.slice(0, offset)], | ||
bytes.slice(offset, reverseEnd).reverse(), | ||
...bytes.length === reverseEnd ? [] : [bytes.slice(reverseEnd)] | ||
]); | ||
return decoder.decode(newBytes, offset); | ||
const reversedBytes = bytes.slice(); | ||
reversedBytes.set(bytes.slice(offset, reverseEnd).reverse(), offset); | ||
return decoder.read(reversedBytes, offset); | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -174,4 +209,2 @@ function reverseCodec(codec) { | ||
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertFixedSizeCodec, combineCodec, fixBytes, fixCodec, fixDecoder, fixEncoder, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.node.js.map | ||
export { assertByteArrayHasEnoughBytesForCodec, assertByteArrayIsNotEmptyForCodec, assertIsFixedSize, assertIsVariableSize, combineCodec, createCodec, createDecoder, createEncoder, fixBytes, fixCodec, fixDecoder, fixEncoder, getEncodedSize, isFixedSize, isVariableSize, mapCodec, mapDecoder, mapEncoder, mergeBytes, padBytes, reverseCodec, reverseDecoder, reverseEncoder }; |
@@ -5,20 +5,27 @@ this.globalThis = this.globalThis || {}; | ||
function E(e,r,o=0){if(r.length-o<=0)throw new Error(`Codec [${e}] cannot decode empty byte arrays.`)}function s(e,r,o,n=0){let t=o.length-n;if(t<r)throw new Error(`Codec [${e}] expected ${r} bytes, got ${t}.`)}function a(e,r){if(e.fixedSize===null)throw new Error(r??"Expected a fixed-size codec, got a variable-size one.")}var u=e=>{let r=e.filter(i=>i.length);if(r.length===0)return e.length?e[0]:new Uint8Array;if(r.length===1)return r[0];let o=r.reduce((i,p)=>i+p.length,0),n=new Uint8Array(o),t=0;return r.forEach(i=>{n.set(i,t),t+=i.length;}),n},f=(e,r)=>{if(e.length>=r)return e;let o=new Uint8Array(r).fill(0);return o.set(e),o},m=(e,r)=>f(e.length<=r?e:e.slice(0,r),r);function c(e,r,o){if(e.fixedSize!==r.fixedSize)throw new Error(`Encoder and decoder must have the same fixed size, got [${e.fixedSize}] and [${r.fixedSize}].`);if(e.maxSize!==r.maxSize)throw new Error(`Encoder and decoder must have the same max size, got [${e.maxSize}] and [${r.maxSize}].`);if(o===void 0&&e.description!==r.description)throw new Error(`Encoder and decoder must have the same description, got [${e.description}] and [${r.description}]. Pass a custom description as a third argument if you want to override the description and bypass this error.`);return {decode:r.decode,description:o??e.description,encode:e.encode,fixedSize:e.fixedSize,maxSize:e.maxSize}}function x(e,r,o){return {description:o??`fixed(${r}, ${e.description})`,fixedSize:r,maxSize:r}}function l(e,r,o){return {...x(e,r,o),encode:n=>m(e.encode(n),r)}}function T(e,r,o){return {...x(e,r,o),decode:(n,t=0)=>{s("fixCodec",r,n,t),(t>0||n.length>r)&&(n=n.slice(t,t+r)),e.fixedSize!==null&&(n=m(n,e.fixedSize));let[i]=e.decode(n,0);return [i,t+r]}}}function $(e,r,o){return c(l(e,r,o),T(e,r,o))}function z(e,r){return {description:e.description,encode:o=>e.encode(r(o)),fixedSize:e.fixedSize,maxSize:e.maxSize}}function w(e,r){return {decode:(o,n=0)=>{let[t,i]=e.decode(o,n);return [r(t,o,n),i]},description:e.description,fixedSize:e.fixedSize,maxSize:e.maxSize}}function _(e,r,o){return {decode:o?w(e,o).decode:e.decode,description:e.description,encode:z(e,r).encode,fixedSize:e.fixedSize,maxSize:e.maxSize}}function C(e){return a(e,"Cannot reverse a codec of variable size."),{...e,encode:r=>e.encode(r).reverse()}}function F(e){return a(e,"Cannot reverse a codec of variable size."),{...e,decode:(r,o=0)=>{let n=o+e.fixedSize;if(o===0&&r.length===n)return e.decode(r.reverse(),o);let t=u([...o===0?[]:[r.slice(0,o)],r.slice(o,n).reverse(),...r.length===n?[]:[r.slice(n)]]);return e.decode(t,o)}}}function I(e){return c(C(e),F(e))} | ||
function N(e,o,r=0){if(o.length-r<=0)throw new Error(`Codec [${e}] cannot decode empty byte arrays.`)}function S(e,o,r,i=0){let T=r.length-i;if(T<o)throw new Error(`Codec [${e}] expected ${o} bytes, got ${T}.`)}var v=e=>{let o=e.filter(d=>d.length);if(o.length===0)return e.length?e[0]:new Uint8Array;if(o.length===1)return o[0];let r=o.reduce((d,F)=>d+F.length,0),i=new Uint8Array(r),T=0;return o.forEach(d=>{i.set(d,T),T+=d.length;}),i},f=(e,o)=>{if(e.length>=o)return e;let r=new Uint8Array(o).fill(0);return r.set(e),r},s=(e,o)=>f(e.length<=o?e:e.slice(0,o),o);function u(e,o){return "fixedSize"in o?o.fixedSize:o.getSizeFromValue(e)}function c(e){return Object.freeze({...e,encode:o=>{let r=new Uint8Array(u(o,e));return e.write(o,r,0),r}})}function m(e){return Object.freeze({...e,decode:(o,r=0)=>e.read(o,r)[0]})}function l(e){return Object.freeze({...e,decode:(o,r=0)=>e.read(o,r)[0],encode:o=>{let r=new Uint8Array(u(o,e));return e.write(o,r,0),r}})}function n(e){return "fixedSize"in e&&typeof e.fixedSize=="number"}function z(e,o){if(!n(e))throw new Error(o!=null?o:"Expected a fixed-size codec, got a variable-size one.")}function x(e){return !n(e)}function h(e,o){if(!x(e))throw new Error(o!=null?o:"Expected a variable-size codec, got a fixed-size one.")}function a(e,o){if(n(e)!==n(o))throw new Error("Encoder and decoder must either both be fixed-size or variable-size.");if(n(e)&&n(o)&&e.fixedSize!==o.fixedSize)throw new Error(`Encoder and decoder must have the same fixed size, got [${e.fixedSize}] and [${o.fixedSize}].`);if(!n(e)&&!n(o)&&e.maxSize!==o.maxSize)throw new Error(`Encoder and decoder must have the same max size, got [${e.maxSize}] and [${o.maxSize}].`);return {...o,...e,decode:o.decode,encode:e.encode,read:o.read,write:e.write}}function b(e,o){return c({fixedSize:o,write:(r,i,T)=>{let d=e.encode(r),F=d.length>o?d.slice(0,o):d;return i.set(F,T),T+o}})}function p(e,o){return m({fixedSize:o,read:(r,i)=>{S("fixCodec",o,r,i),(i>0||r.length>o)&&(r=r.slice(i,i+o)),n(e)&&(r=s(r,e.fixedSize));let[T]=e.read(r,0);return [T,i+o]}})}function Te(e,o){return a(b(e,o),p(e,o))}function w(e,o){return c({...x(e)?{...e,getSizeFromValue:r=>e.getSizeFromValue(o(r))}:e,write:(r,i,T)=>e.write(o(r),i,T)})}function O(e,o){return m({...e,read:(r,i)=>{let[T,d]=e.read(r,i);return [o(T,r,i),d]}})}function le(e,o,r){return l({...w(e,o),read:r?O(e,r).read:e.read})}function E(e){return z(e,"Cannot reverse a codec of variable size."),c({...e,write:(o,r,i)=>{let T=e.write(o,r,i),d=r.slice(i,i+e.fixedSize).reverse();return r.set(d,i),T}})}function C(e){return z(e,"Cannot reverse a codec of variable size."),m({...e,read:(o,r)=>{let i=r+e.fixedSize;if(r===0&&o.length===i)return e.read(o.reverse(),r);let T=o.slice();return T.set(o.slice(r,i).reverse(),r),e.read(T,r)}})}function De(e){return a(E(e),C(e))} | ||
exports.assertByteArrayHasEnoughBytesForCodec = s; | ||
exports.assertByteArrayIsNotEmptyForCodec = E; | ||
exports.assertFixedSizeCodec = a; | ||
exports.combineCodec = c; | ||
exports.fixBytes = m; | ||
exports.fixCodec = $; | ||
exports.fixDecoder = T; | ||
exports.fixEncoder = l; | ||
exports.mapCodec = _; | ||
exports.mapDecoder = w; | ||
exports.mapEncoder = z; | ||
exports.mergeBytes = u; | ||
exports.assertByteArrayHasEnoughBytesForCodec = S; | ||
exports.assertByteArrayIsNotEmptyForCodec = N; | ||
exports.assertIsFixedSize = z; | ||
exports.assertIsVariableSize = h; | ||
exports.combineCodec = a; | ||
exports.createCodec = l; | ||
exports.createDecoder = m; | ||
exports.createEncoder = c; | ||
exports.fixBytes = s; | ||
exports.fixCodec = Te; | ||
exports.fixDecoder = p; | ||
exports.fixEncoder = b; | ||
exports.getEncodedSize = u; | ||
exports.isFixedSize = n; | ||
exports.isVariableSize = x; | ||
exports.mapCodec = le; | ||
exports.mapDecoder = O; | ||
exports.mapEncoder = w; | ||
exports.mergeBytes = v; | ||
exports.padBytes = f; | ||
exports.reverseCodec = I; | ||
exports.reverseDecoder = F; | ||
exports.reverseEncoder = C; | ||
exports.reverseCodec = De; | ||
exports.reverseDecoder = C; | ||
exports.reverseEncoder = E; | ||
@@ -25,0 +32,0 @@ return exports; |
@@ -1,2 +0,1 @@ | ||
import { CodecData } from './codec'; | ||
/** | ||
@@ -10,8 +9,2 @@ * Asserts that a given byte array is not empty. | ||
export declare function assertByteArrayHasEnoughBytesForCodec(codecDescription: string, expected: number, bytes: Uint8Array, offset?: number): void; | ||
/** | ||
* Asserts that a given codec is fixed-size codec. | ||
*/ | ||
export declare function assertFixedSizeCodec(data: Pick<CodecData, 'fixedSize'>, message?: string): asserts data is { | ||
fixedSize: number; | ||
}; | ||
//# sourceMappingURL=assertions.d.ts.map |
@@ -5,30 +5,48 @@ /** | ||
export type Offset = number; | ||
/** | ||
* The shared attributes between codecs, encoders and decoders. | ||
*/ | ||
export type CodecData = { | ||
/** A description for the codec. */ | ||
description: string; | ||
/** The fixed size of the encoded value in bytes, or `null` if it is variable. */ | ||
fixedSize: number | null; | ||
/** The maximum size an encoded value can be in bytes, or `null` if it is variable. */ | ||
maxSize: number | null; | ||
type BaseEncoder<TFrom> = { | ||
/** Encode the provided value and return the encoded bytes directly. */ | ||
readonly encode: (value: TFrom) => Uint8Array; | ||
/** | ||
* Writes the encoded value into the provided byte array at the given offset. | ||
* Returns the offset of the next byte after the encoded value. | ||
*/ | ||
readonly write: (value: TFrom, bytes: Uint8Array, offset: Offset) => Offset; | ||
}; | ||
export type FixedSizeEncoder<TFrom, TSize extends number = number> = BaseEncoder<TFrom> & { | ||
/** The fixed size of the encoded value in bytes. */ | ||
readonly fixedSize: TSize; | ||
}; | ||
export type VariableSizeEncoder<TFrom> = BaseEncoder<TFrom> & { | ||
/** The total size of the encoded value in bytes. */ | ||
readonly getSizeFromValue: (value: TFrom) => number; | ||
/** The maximum size an encoded value can be in bytes, if applicable. */ | ||
readonly maxSize?: number; | ||
}; | ||
/** | ||
* An object that can encode a value to a `Uint8Array`. | ||
*/ | ||
export type Encoder<T> = CodecData & { | ||
/** The function that encodes a value into bytes. */ | ||
encode: (value: T) => Uint8Array; | ||
export type Encoder<TFrom> = FixedSizeEncoder<TFrom> | VariableSizeEncoder<TFrom>; | ||
type BaseDecoder<TTo> = { | ||
/** Decodes the provided byte array at the given offset (or zero) and returns the value directly. */ | ||
readonly decode: (bytes: Uint8Array, offset?: Offset) => TTo; | ||
/** | ||
* Reads the encoded value from the provided byte array at the given offset. | ||
* Returns the decoded value and the offset of the next byte after the encoded value. | ||
*/ | ||
readonly read: (bytes: Uint8Array, offset: Offset) => [TTo, Offset]; | ||
}; | ||
export type FixedSizeDecoder<TTo, TSize extends number = number> = BaseDecoder<TTo> & { | ||
/** The fixed size of the encoded value in bytes. */ | ||
readonly fixedSize: TSize; | ||
}; | ||
export type VariableSizeDecoder<TTo> = BaseDecoder<TTo> & { | ||
/** The maximum size an encoded value can be in bytes, if applicable. */ | ||
readonly maxSize?: number; | ||
}; | ||
/** | ||
* An object that can decode a value from a `Uint8Array`. | ||
*/ | ||
export type Decoder<T> = CodecData & { | ||
/** | ||
* The function that decodes a value from bytes. | ||
* It returns the decoded value and the number of bytes read. | ||
*/ | ||
decode: (bytes: Uint8Array, offset?: Offset) => [T, Offset]; | ||
}; | ||
export type Decoder<TTo> = FixedSizeDecoder<TTo> | VariableSizeDecoder<TTo>; | ||
export type FixedSizeCodec<TFrom, TTo extends TFrom = TFrom, TSize extends number = number> = FixedSizeEncoder<TFrom, TSize> & FixedSizeDecoder<TTo, TSize>; | ||
export type VariableSizeCodec<TFrom, TTo extends TFrom = TFrom> = VariableSizeEncoder<TFrom> & VariableSizeDecoder<TTo>; | ||
/** | ||
@@ -40,19 +58,67 @@ * An object that can encode and decode a value to and from a `Uint8Array`. | ||
* | ||
* @typeParam From - The type of the value to encode. | ||
* @typeParam To - The type of the decoded value. Defaults to `From`. | ||
* @typeParam TFrom - The type of the value to encode. | ||
* @typeParam TTo - The type of the decoded value. Defaults to `TFrom`. | ||
*/ | ||
export type Codec<From, To extends From = From> = Encoder<From> & Decoder<To>; | ||
export type Codec<TFrom, TTo extends TFrom = TFrom> = FixedSizeCodec<TFrom, TTo> | VariableSizeCodec<TFrom, TTo>; | ||
/** | ||
* Defines common configurations for codec factories. | ||
* Get the encoded size of a given value in bytes. | ||
*/ | ||
export type BaseCodecConfig = { | ||
/** A custom description for the Codec. */ | ||
description?: string; | ||
export declare function getEncodedSize<TFrom>(value: TFrom, encoder: { | ||
fixedSize: number; | ||
} | { | ||
getSizeFromValue: (value: TFrom) => number; | ||
}): number; | ||
/** Fills the missing `encode` function using the existing `write` function. */ | ||
export declare function createEncoder<TFrom, TSize extends number>(encoder: Omit<FixedSizeEncoder<TFrom, TSize>, 'encode'>): FixedSizeEncoder<TFrom, TSize>; | ||
export declare function createEncoder<TFrom>(encoder: Omit<VariableSizeEncoder<TFrom>, 'encode'>): VariableSizeEncoder<TFrom>; | ||
export declare function createEncoder<TFrom>(encoder: Omit<FixedSizeEncoder<TFrom>, 'encode'> | Omit<VariableSizeEncoder<TFrom>, 'encode'>): Encoder<TFrom>; | ||
/** Fills the missing `decode` function using the existing `read` function. */ | ||
export declare function createDecoder<TTo, TSize extends number>(decoder: Omit<FixedSizeDecoder<TTo, TSize>, 'decode'>): FixedSizeDecoder<TTo, TSize>; | ||
export declare function createDecoder<TTo>(decoder: Omit<VariableSizeDecoder<TTo>, 'decode'>): VariableSizeDecoder<TTo>; | ||
export declare function createDecoder<TTo>(decoder: Omit<FixedSizeDecoder<TTo>, 'decode'> | Omit<VariableSizeDecoder<TTo>, 'decode'>): Decoder<TTo>; | ||
/** Fills the missing `encode` and `decode` function using the existing `write` and `read` functions. */ | ||
export declare function createCodec<TFrom, TTo extends TFrom = TFrom, TSize extends number = number>(codec: Omit<FixedSizeCodec<TFrom, TTo, TSize>, 'encode' | 'decode'>): FixedSizeCodec<TFrom, TTo, TSize>; | ||
export declare function createCodec<TFrom, TTo extends TFrom = TFrom>(codec: Omit<VariableSizeCodec<TFrom, TTo>, 'encode' | 'decode'>): VariableSizeCodec<TFrom, TTo>; | ||
export declare function createCodec<TFrom, TTo extends TFrom = TFrom>(codec: Omit<FixedSizeCodec<TFrom, TTo>, 'encode' | 'decode'> | Omit<VariableSizeCodec<TFrom, TTo>, 'encode' | 'decode'>): Codec<TFrom, TTo>; | ||
export declare function isFixedSize<TFrom, TSize extends number>(encoder: FixedSizeEncoder<TFrom, TSize> | VariableSizeEncoder<TFrom>): encoder is FixedSizeEncoder<TFrom, TSize>; | ||
export declare function isFixedSize<TTo, TSize extends number>(decoder: FixedSizeDecoder<TTo, TSize> | VariableSizeDecoder<TTo>): decoder is FixedSizeDecoder<TTo, TSize>; | ||
export declare function isFixedSize<TFrom, TTo extends TFrom, TSize extends number>(codec: FixedSizeCodec<TFrom, TTo, TSize> | VariableSizeCodec<TFrom, TTo>): codec is FixedSizeCodec<TFrom, TTo, TSize>; | ||
export declare function isFixedSize<TSize extends number>(codec: { | ||
fixedSize: TSize; | ||
} | { | ||
maxSize?: number; | ||
}): codec is { | ||
fixedSize: TSize; | ||
}; | ||
/** | ||
* Wraps all the attributes of an object in Codecs. | ||
*/ | ||
export type WrapInCodec<T, U extends T = T> = { | ||
[P in keyof T]: Codec<T[P], U[P]>; | ||
export declare function assertIsFixedSize<TFrom, TSize extends number>(encoder: FixedSizeEncoder<TFrom, TSize> | VariableSizeEncoder<TFrom>, message?: string): asserts encoder is FixedSizeEncoder<TFrom, TSize>; | ||
export declare function assertIsFixedSize<TTo, TSize extends number>(decoder: FixedSizeDecoder<TTo, TSize> | VariableSizeDecoder<TTo>, message?: string): asserts decoder is FixedSizeDecoder<TTo, TSize>; | ||
export declare function assertIsFixedSize<TFrom, TTo extends TFrom, TSize extends number>(codec: FixedSizeCodec<TFrom, TTo, TSize> | VariableSizeCodec<TFrom, TTo>, message?: string): asserts codec is FixedSizeCodec<TFrom, TTo, TSize>; | ||
export declare function assertIsFixedSize<TSize extends number>(codec: { | ||
fixedSize: TSize; | ||
} | { | ||
maxSize?: number; | ||
}, message?: string): asserts codec is { | ||
fixedSize: TSize; | ||
}; | ||
export declare function isVariableSize<TFrom>(encoder: Encoder<TFrom>): encoder is VariableSizeEncoder<TFrom>; | ||
export declare function isVariableSize<TTo>(decoder: Decoder<TTo>): decoder is VariableSizeDecoder<TTo>; | ||
export declare function isVariableSize<TFrom, TTo extends TFrom>(codec: Codec<TFrom, TTo>): codec is VariableSizeCodec<TFrom, TTo>; | ||
export declare function isVariableSize(codec: { | ||
fixedSize: number; | ||
} | { | ||
maxSize?: number; | ||
}): codec is { | ||
maxSize?: number; | ||
}; | ||
export declare function assertIsVariableSize<T>(encoder: Encoder<T>, message?: string): asserts encoder is VariableSizeEncoder<T>; | ||
export declare function assertIsVariableSize<T>(decoder: Decoder<T>, message?: string): asserts decoder is VariableSizeDecoder<T>; | ||
export declare function assertIsVariableSize<TFrom, TTo extends TFrom>(codec: Codec<TFrom, TTo>, message?: string): asserts codec is VariableSizeCodec<TFrom, TTo>; | ||
export declare function assertIsVariableSize(codec: { | ||
fixedSize: number; | ||
} | { | ||
maxSize?: number; | ||
}, message?: string): asserts codec is { | ||
maxSize?: number; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=codec.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Codec, Decoder, Encoder } from './codec'; | ||
import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from './codec'; | ||
/** | ||
@@ -7,3 +7,5 @@ * Combines an encoder and a decoder into a codec. | ||
*/ | ||
export declare function combineCodec<From, To extends From = From>(encoder: Encoder<From>, decoder: Decoder<To>, description?: string): Codec<From, To>; | ||
export declare function combineCodec<TFrom, TTo extends TFrom, TSize extends number>(encoder: FixedSizeEncoder<TFrom, TSize>, decoder: FixedSizeDecoder<TTo, TSize>): FixedSizeCodec<TFrom, TTo, TSize>; | ||
export declare function combineCodec<TFrom, TTo extends TFrom>(encoder: VariableSizeEncoder<TFrom>, decoder: VariableSizeDecoder<TTo>): VariableSizeCodec<TFrom, TTo>; | ||
export declare function combineCodec<TFrom, TTo extends TFrom>(encoder: Encoder<TFrom>, decoder: Decoder<TTo>): Codec<TFrom, TTo>; | ||
//# sourceMappingURL=combine-codec.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Codec, Decoder, Encoder } from './codec'; | ||
import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from './codec'; | ||
/** | ||
@@ -7,5 +7,4 @@ * Creates a fixed-size encoder from a given encoder. | ||
* @param fixedBytes - The fixed number of bytes to write. | ||
* @param description - A custom description for the encoder. | ||
*/ | ||
export declare function fixEncoder<T>(encoder: Encoder<T>, fixedBytes: number, description?: string): Encoder<T>; | ||
export declare function fixEncoder<TFrom, TSize extends number>(encoder: Encoder<TFrom>, fixedBytes: TSize): FixedSizeEncoder<TFrom, TSize>; | ||
/** | ||
@@ -16,5 +15,4 @@ * Creates a fixed-size decoder from a given decoder. | ||
* @param fixedBytes - The fixed number of bytes to read. | ||
* @param description - A custom description for the decoder. | ||
*/ | ||
export declare function fixDecoder<T>(decoder: Decoder<T>, fixedBytes: number, description?: string): Decoder<T>; | ||
export declare function fixDecoder<TTo, TSize extends number>(decoder: Decoder<TTo>, fixedBytes: TSize): FixedSizeDecoder<TTo, TSize>; | ||
/** | ||
@@ -25,5 +23,4 @@ * Creates a fixed-size codec from a given codec. | ||
* @param fixedBytes - The fixed number of bytes to read/write. | ||
* @param description - A custom description for the codec. | ||
*/ | ||
export declare function fixCodec<T, U extends T = T>(codec: Codec<T, U>, fixedBytes: number, description?: string): Codec<T, U>; | ||
export declare function fixCodec<TFrom, TTo extends TFrom, TSize extends number>(codec: Codec<TFrom, TTo>, fixedBytes: TSize): FixedSizeCodec<TFrom, TTo, TSize>; | ||
//# sourceMappingURL=fix-codec.d.ts.map |
@@ -1,15 +0,23 @@ | ||
import { Codec, Decoder, Encoder } from './codec'; | ||
import { Codec, Decoder, Encoder, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, VariableSizeCodec, VariableSizeDecoder, VariableSizeEncoder } from './codec'; | ||
/** | ||
* Converts an encoder A to a encoder B by mapping their values. | ||
*/ | ||
export declare function mapEncoder<T, U>(encoder: Encoder<T>, unmap: (value: U) => T): Encoder<U>; | ||
export declare function mapEncoder<TOldFrom, TNewFrom, TSize extends number>(encoder: FixedSizeEncoder<TOldFrom, TSize>, unmap: (value: TNewFrom) => TOldFrom): FixedSizeEncoder<TNewFrom, TSize>; | ||
export declare function mapEncoder<TOldFrom, TNewFrom>(encoder: VariableSizeEncoder<TOldFrom>, unmap: (value: TNewFrom) => TOldFrom): VariableSizeEncoder<TNewFrom>; | ||
export declare function mapEncoder<TOldFrom, TNewFrom>(encoder: Encoder<TOldFrom>, unmap: (value: TNewFrom) => TOldFrom): Encoder<TNewFrom>; | ||
/** | ||
* Converts an decoder A to a decoder B by mapping their values. | ||
*/ | ||
export declare function mapDecoder<T, U>(decoder: Decoder<T>, map: (value: T, bytes: Uint8Array, offset: number) => U): Decoder<U>; | ||
export declare function mapDecoder<TOldTo, TNewTo, TSize extends number>(decoder: FixedSizeDecoder<TOldTo, TSize>, map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo): FixedSizeDecoder<TNewTo, TSize>; | ||
export declare function mapDecoder<TOldTo, TNewTo>(decoder: VariableSizeDecoder<TOldTo>, map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo): VariableSizeDecoder<TNewTo>; | ||
export declare function mapDecoder<TOldTo, TNewTo>(decoder: Decoder<TOldTo>, map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo): Decoder<TNewTo>; | ||
/** | ||
* Converts a codec A to a codec B by mapping their values. | ||
*/ | ||
export declare function mapCodec<NewFrom, OldFrom, To extends NewFrom & OldFrom>(codec: Codec<OldFrom, To>, unmap: (value: NewFrom) => OldFrom): Codec<NewFrom, To>; | ||
export declare function mapCodec<NewFrom, OldFrom, NewTo extends NewFrom = NewFrom, OldTo extends OldFrom = OldFrom>(codec: Codec<OldFrom, OldTo>, unmap: (value: NewFrom) => OldFrom, map: (value: OldTo, bytes: Uint8Array, offset: number) => NewTo): Codec<NewFrom, NewTo>; | ||
export declare function mapCodec<TOldFrom, TNewFrom, TTo extends TNewFrom & TOldFrom, TSize extends number>(codec: FixedSizeCodec<TOldFrom, TTo, TSize>, unmap: (value: TNewFrom) => TOldFrom): FixedSizeCodec<TNewFrom, TTo, TSize>; | ||
export declare function mapCodec<TOldFrom, TNewFrom, TTo extends TNewFrom & TOldFrom>(codec: VariableSizeCodec<TOldFrom, TTo>, unmap: (value: TNewFrom) => TOldFrom): VariableSizeCodec<TNewFrom, TTo>; | ||
export declare function mapCodec<TOldFrom, TNewFrom, TTo extends TNewFrom & TOldFrom>(codec: Codec<TOldFrom, TTo>, unmap: (value: TNewFrom) => TOldFrom): Codec<TNewFrom, TTo>; | ||
export declare function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom, TSize extends number>(codec: FixedSizeCodec<TOldFrom, TOldTo, TSize>, unmap: (value: TNewFrom) => TOldFrom, map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo): FixedSizeCodec<TNewFrom, TNewTo, TSize>; | ||
export declare function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom>(codec: VariableSizeCodec<TOldFrom, TOldTo>, unmap: (value: TNewFrom) => TOldFrom, map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo): VariableSizeCodec<TNewFrom, TNewTo>; | ||
export declare function mapCodec<TOldFrom, TNewFrom, TOldTo extends TOldFrom, TNewTo extends TNewFrom>(codec: Codec<TOldFrom, TOldTo>, unmap: (value: TNewFrom) => TOldFrom, map: (value: TOldTo, bytes: Uint8Array, offset: number) => TNewTo): Codec<TNewFrom, TNewTo>; | ||
//# sourceMappingURL=map-codec.d.ts.map |
@@ -1,14 +0,14 @@ | ||
import { Codec, Decoder, Encoder } from './codec'; | ||
import { FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder } from './codec'; | ||
/** | ||
* Reverses the bytes of a fixed-size encoder. | ||
*/ | ||
export declare function reverseEncoder<T>(encoder: Encoder<T>): Encoder<T>; | ||
export declare function reverseEncoder<TFrom, TSize extends number>(encoder: FixedSizeEncoder<TFrom, TSize>): FixedSizeEncoder<TFrom, TSize>; | ||
/** | ||
* Reverses the bytes of a fixed-size decoder. | ||
*/ | ||
export declare function reverseDecoder<T>(decoder: Decoder<T>): Decoder<T>; | ||
export declare function reverseDecoder<TTo, TSize extends number>(decoder: FixedSizeDecoder<TTo, TSize>): FixedSizeDecoder<TTo, TSize>; | ||
/** | ||
* Reverses the bytes of a fixed-size codec. | ||
*/ | ||
export declare function reverseCodec<T, U extends T = T>(codec: Codec<T, U>): Codec<T, U>; | ||
export declare function reverseCodec<TFrom, TTo extends TFrom, TSize extends number>(codec: FixedSizeCodec<TFrom, TTo, TSize>): FixedSizeCodec<TFrom, TTo, TSize>; | ||
//# sourceMappingURL=reverse-codec.d.ts.map |
{ | ||
"name": "@solana/codecs-core", | ||
"version": "2.0.0-experimental.735654a", | ||
"version": "2.0.0-experimental.745437f", | ||
"description": "Core types and helpers for encoding and decoding byte arrays on Solana", | ||
@@ -65,4 +65,4 @@ "exports": { | ||
"jest-runner-prettier": "^1.0.0", | ||
"prettier": "^2.8", | ||
"tsup": "7.2.0", | ||
"prettier": "^3.1", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.2.2", | ||
@@ -69,0 +69,0 @@ "version-from-git": "^1.1.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
258031
33
1717