it-length-prefixed
Advanced tools
Comparing version 6.0.0 to 6.0.1
@@ -10,3 +10,3 @@ import BufferList from 'bl/BufferList.js'; | ||
lengthDecoder?: LengthDecoderFunction; | ||
onData?: (data: BufferList | Uint8Array) => void; | ||
onData?: (data: Uint8Array) => void; | ||
onLength?: (length: number) => void; | ||
@@ -25,6 +25,6 @@ maxLengthLength?: number; | ||
export declare const MAX_DATA_LENGTH: number; | ||
export declare function decode(options?: DecoderOptions): Transform<BufferList | Uint8Array, BufferList>; | ||
export declare function decode(options?: DecoderOptions): Transform<BufferList | Uint8Array, Uint8Array>; | ||
export declare namespace decode { | ||
var fromReader: (reader: Reader, options?: DecoderOptions | undefined) => Source<BufferList>; | ||
var fromReader: (reader: Reader, options?: DecoderOptions | undefined) => Source<Uint8Array>; | ||
} | ||
//# sourceMappingURL=decode.d.ts.map |
@@ -8,3 +8,3 @@ import BufferList from 'bl/BufferList.js'; | ||
export const MAX_DATA_LENGTH = 1024 * 1024 * 4; | ||
const Empty = new BufferList([]); | ||
const empty = new BufferList([]); | ||
const ReadModes = { LENGTH: 'readLength', DATA: 'readData' }; | ||
@@ -40,6 +40,3 @@ const ReadHandlers = { | ||
if (dataLength <= 0) { | ||
if (options?.onData != null) { | ||
options.onData(Empty); | ||
} | ||
return { mode: ReadModes.LENGTH, chunk, buffer, data: Empty }; | ||
return { mode: ReadModes.LENGTH, chunk, buffer, data: empty }; | ||
} | ||
@@ -60,5 +57,2 @@ return { mode: ReadModes.DATA, chunk, buffer, state: { dataLength }, data: undefined }; | ||
buffer = new BufferList(); | ||
if ((options?.onData) != null) { | ||
options.onData(data); | ||
} | ||
return { mode: ReadModes.LENGTH, chunk: nextChunk, buffer, state: undefined, data }; | ||
@@ -84,3 +78,7 @@ } | ||
if (result.data != null) { | ||
yield result.data; | ||
const data = result.data.slice(); | ||
if (options?.onData != null) { | ||
options.onData(data); | ||
} | ||
yield data; | ||
} | ||
@@ -87,0 +85,0 @@ } |
@@ -11,3 +11,3 @@ import BufferList from 'bl/BufferList.js'; | ||
export declare const DEFAULT_POOL_SIZE: number; | ||
export declare function encode(options?: EncoderOptions): Transform<BufferList | Uint8Array, BufferList>; | ||
export declare function encode(options?: EncoderOptions): Transform<BufferList | Uint8Array, Uint8Array>; | ||
export declare namespace encode { | ||
@@ -14,0 +14,0 @@ var single: (chunk: Uint8Array | BufferList, options?: EncoderOptions | undefined) => BufferList; |
import BufferList from 'bl/BufferList.js'; | ||
import { varintEncode } from './varint-encode.js'; | ||
import { concat as uint8ArrayConcat } from 'uint8arrays'; | ||
export const MIN_POOL_SIZE = 8; // Varint.encode(Number.MAX_SAFE_INTEGER).length | ||
@@ -20,5 +21,3 @@ export const DEFAULT_POOL_SIZE = 10 * 1024; | ||
} | ||
// @ts-expect-error bl types are broken | ||
yield new BufferList().append(encodedLength).append(chunk); | ||
// yield uint8ArrayConcat([encodedLength, chunk]) | ||
yield uint8ArrayConcat([encodedLength, chunk.slice()], encodedLength.length + chunk.length); | ||
} | ||
@@ -25,0 +24,0 @@ }; |
{ | ||
"name": "it-length-prefixed", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "Streaming length prefixed buffers with async iterables", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -14,3 +14,3 @@ import BufferList from 'bl/BufferList.js' | ||
lengthDecoder?: LengthDecoderFunction | ||
onData?: (data: BufferList | Uint8Array) => void | ||
onData?: (data: Uint8Array) => void | ||
onLength?: (length: number) => void | ||
@@ -38,3 +38,3 @@ maxLengthLength?: number | ||
const Empty = new BufferList([]) | ||
const empty = new BufferList([]) | ||
const ReadModes = { LENGTH: 'readLength', DATA: 'readData' } | ||
@@ -76,7 +76,3 @@ | ||
if (dataLength <= 0) { | ||
if (options?.onData != null) { | ||
options.onData(Empty) | ||
} | ||
return { mode: ReadModes.LENGTH, chunk, buffer, data: Empty } | ||
return { mode: ReadModes.LENGTH, chunk, buffer, data: empty } | ||
} | ||
@@ -104,6 +100,2 @@ | ||
if ((options?.onData) != null) { | ||
options.onData(data) | ||
} | ||
return { mode: ReadModes.LENGTH, chunk: nextChunk, buffer, state: undefined, data } | ||
@@ -113,4 +105,4 @@ } | ||
export function decode (options?: DecoderOptions): Transform<BufferList | Uint8Array, BufferList> { | ||
const decoder = async function * (source: Source<BufferList | Uint8Array>): Source<BufferList> { | ||
export function decode (options?: DecoderOptions): Transform<BufferList | Uint8Array, Uint8Array> { | ||
const decoder = async function * (source: Source<BufferList | Uint8Array>): Source<Uint8Array> { | ||
let buffer = new BufferList() | ||
@@ -135,3 +127,9 @@ let mode = ReadModes.LENGTH // current parsing mode | ||
if (result.data != null) { | ||
yield result.data | ||
const data = result.data.slice() | ||
if (options?.onData != null) { | ||
options.onData(data) | ||
} | ||
yield data | ||
} | ||
@@ -138,0 +136,0 @@ } |
import BufferList from 'bl/BufferList.js' | ||
import { varintEncode } from './varint-encode.js' | ||
import { concat as uint8ArrayConcat } from 'uint8arrays' | ||
import type { LengthEncoderFunction } from './varint-encode.js' | ||
@@ -15,3 +16,3 @@ import type { Source, Transform } from 'it-stream-types' | ||
export function encode (options?: EncoderOptions): Transform<BufferList | Uint8Array, BufferList> { | ||
export function encode (options?: EncoderOptions): Transform<BufferList | Uint8Array, Uint8Array> { | ||
options = options ?? {} | ||
@@ -22,3 +23,3 @@ | ||
const encoder = async function * (source: Source<BufferList | Uint8Array>): Source<BufferList> { | ||
const encoder = async function * (source: Source<BufferList | Uint8Array>): Source<Uint8Array> { | ||
let pool = new Uint8Array(poolSize) | ||
@@ -37,5 +38,3 @@ let poolOffset = 0 | ||
// @ts-expect-error bl types are broken | ||
yield new BufferList().append(encodedLength).append(chunk) | ||
// yield uint8ArrayConcat([encodedLength, chunk]) | ||
yield uint8ArrayConcat([encodedLength, chunk.slice()], encodedLength.length + chunk.length) | ||
} | ||
@@ -42,0 +41,0 @@ } |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
43070
519