2626import { byteStream , type ByteStreamOpts } from 'it-byte-stream'
2727import * as varint from 'uint8-varint'
2828import { Uint8ArrayList } from 'uint8arraylist'
29+ import { InvalidDataLengthError , InvalidDataLengthLengthError , InvalidMessageLengthError } from './errors.js'
2930import type { Duplex } from 'it-stream-types'
3031
31- class CodeError extends Error {
32- public readonly code : string
33-
34- constructor ( message : string , code : string ) {
35- super ( message )
36- this . code = code
37- }
38- }
39-
4032export interface AbortOptions {
4133 signal ?: AbortSignal
4234}
@@ -104,8 +96,12 @@ export function lpStream <Stream extends Duplex<any, any, any>> (duplex: Stream,
10496 throw err
10597 }
10698
99+ if ( dataLength < 0 ) {
100+ throw new InvalidMessageLengthError ( 'Invalid message length' )
101+ }
102+
107103 if ( opts ?. maxLengthLength != null && lengthBuffer . byteLength > opts . maxLengthLength ) {
108- throw new CodeError ( 'message length length too long' , 'ERR_MSG_LENGTH_TOO_LONG ')
104+ throw new InvalidDataLengthLengthError ( 'message length length too long' )
109105 }
110106
111107 if ( dataLength > - 1 ) {
@@ -114,7 +110,7 @@ export function lpStream <Stream extends Duplex<any, any, any>> (duplex: Stream,
114110 }
115111
116112 if ( opts ?. maxDataLength != null && dataLength > opts . maxDataLength ) {
117- throw new CodeError ( 'message length too long' , 'ERR_MSG_DATA_TOO_LONG ')
113+ throw new InvalidDataLengthError ( 'message length too long' )
118114 }
119115
120116 return bytes . read ( dataLength , options )
0 commit comments