@@ -2,6 +2,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
22import type { Uint8ArrayList } from 'uint8arraylist'
33import type { bytes } from './@types/basic.js'
44import type { MessageBuffer } from './@types/handshake.js'
5+ import type { LengthDecoderFunction , LengthEncoderFunction } from 'it-length-prefixed'
56
67const allocUnsafe = ( len : number ) : Uint8Array => {
78 if ( globalThis . Buffer ) {
@@ -11,14 +12,14 @@ const allocUnsafe = (len: number): Uint8Array => {
1112 return new Uint8Array ( len )
1213}
1314
14- export const uint16BEEncode = ( value : number , target ?: Uint8Array , offset ?: number ) : Uint8Array => {
15- target = target ?? allocUnsafe ( 2 )
16- new DataView ( target . buffer , target . byteOffset , target . byteLength ) . setUint16 ( offset ?? 0 , value , false )
15+ export const uint16BEEncode : LengthEncoderFunction = ( value : number ) => {
16+ const target = allocUnsafe ( 2 )
17+ new DataView ( target . buffer , target . byteOffset , target . byteLength ) . setUint16 ( 0 , value , false )
1718 return target
1819}
1920uint16BEEncode . bytes = 2
2021
21- export const uint16BEDecode = ( data : Uint8Array | Uint8ArrayList ) : number => {
22+ export const uint16BEDecode : LengthDecoderFunction = ( data : Uint8Array | Uint8ArrayList ) : number => {
2223 if ( data . length < 2 ) throw RangeError ( 'Could not decode int16BE' )
2324
2425 if ( data instanceof Uint8Array ) {
@@ -49,8 +50,8 @@ export function decode0 (input: bytes): MessageBuffer {
4950 }
5051
5152 return {
52- ne : input . slice ( 0 , 32 ) ,
53- ciphertext : input . slice ( 32 , input . length ) ,
53+ ne : input . subarray ( 0 , 32 ) ,
54+ ciphertext : input . subarray ( 32 , input . length ) ,
5455 ns : new Uint8Array ( 0 )
5556 }
5657}
@@ -61,9 +62,9 @@ export function decode1 (input: bytes): MessageBuffer {
6162 }
6263
6364 return {
64- ne : input . slice ( 0 , 32 ) ,
65- ns : input . slice ( 32 , 80 ) ,
66- ciphertext : input . slice ( 80 , input . length )
65+ ne : input . subarray ( 0 , 32 ) ,
66+ ns : input . subarray ( 32 , 80 ) ,
67+ ciphertext : input . subarray ( 80 , input . length )
6768 }
6869}
6970
@@ -74,7 +75,7 @@ export function decode2 (input: bytes): MessageBuffer {
7475
7576 return {
7677 ne : new Uint8Array ( 0 ) ,
77- ns : input . slice ( 0 , 48 ) ,
78- ciphertext : input . slice ( 48 , input . length )
78+ ns : input . subarray ( 0 , 48 ) ,
79+ ciphertext : input . subarray ( 48 , input . length )
7980 }
8081}
0 commit comments