it-length-prefixed
Advanced tools
Comparing version 6.0.1 to 7.0.0
@@ -1,2 +0,2 @@ | ||
import BufferList from 'bl/BufferList.js'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import type { LengthDecoderFunction } from './varint-decode.js'; | ||
@@ -17,10 +17,10 @@ import type { Reader } from 'it-reader'; | ||
mode: string; | ||
chunk?: BufferList; | ||
buffer: BufferList; | ||
chunk?: Uint8ArrayList; | ||
buffer: Uint8ArrayList; | ||
state?: ReadState; | ||
data?: BufferList; | ||
data?: Uint8ArrayList; | ||
} | ||
export declare const MAX_LENGTH_LENGTH = 8; | ||
export declare const MAX_DATA_LENGTH: number; | ||
export declare function decode(options?: DecoderOptions): Transform<BufferList | Uint8Array, Uint8Array>; | ||
export declare function decode(options?: DecoderOptions): Transform<Uint8ArrayList | Uint8Array, Uint8Array>; | ||
export declare namespace decode { | ||
@@ -27,0 +27,0 @@ var fromReader: (reader: Reader, options?: DecoderOptions | undefined) => Source<Uint8Array>; |
@@ -1,2 +0,2 @@ | ||
import BufferList from 'bl/BufferList.js'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { varintDecode } from './varint-decode.js'; | ||
@@ -8,3 +8,3 @@ import errCode from 'err-code'; | ||
export const MAX_DATA_LENGTH = 1024 * 1024 * 4; | ||
const empty = new BufferList([]); | ||
const empty = new Uint8ArrayList(); | ||
const ReadModes = { LENGTH: 'readLength', DATA: 'readData' }; | ||
@@ -17,3 +17,3 @@ const ReadHandlers = { | ||
// console.log(ReadModes.LENGTH, chunk.length) | ||
buffer = buffer.append(chunk); | ||
buffer.append(chunk); | ||
let dataLength; | ||
@@ -35,4 +35,4 @@ try { | ||
} | ||
chunk = buffer.shallowSlice(lengthDecoder.bytes); | ||
buffer = new BufferList(); | ||
chunk = buffer.subarray(lengthDecoder.bytes); | ||
buffer = new Uint8ArrayList(); | ||
if (options?.onLength != null) { | ||
@@ -47,3 +47,3 @@ options.onLength(dataLength); | ||
[ReadModes.DATA]: (chunk, buffer, state, options) => { | ||
buffer = buffer.append(chunk); | ||
buffer.append(chunk); | ||
if (state == null) { | ||
@@ -56,5 +56,5 @@ throw new Error('state is required'); | ||
const { dataLength } = state; | ||
const data = buffer.shallowSlice(0, dataLength); | ||
const nextChunk = buffer.length > dataLength ? buffer.shallowSlice(dataLength) : undefined; | ||
buffer = new BufferList(); | ||
const data = buffer.subarray(0, dataLength); | ||
const nextChunk = buffer.length > dataLength ? buffer.subarray(dataLength) : undefined; | ||
buffer = new Uint8ArrayList(); | ||
return { mode: ReadModes.LENGTH, chunk: nextChunk, buffer, state: undefined, data }; | ||
@@ -65,8 +65,7 @@ } | ||
const decoder = async function* (source) { | ||
let buffer = new BufferList(); | ||
let buffer = new Uint8ArrayList(); | ||
let mode = ReadModes.LENGTH; // current parsing mode | ||
let state; // accumulated state for the current mode | ||
for await (const chunk of source) { | ||
// @ts-expect-error bl types are broken | ||
let nextChunk = new BufferList(chunk); | ||
let nextChunk = new Uint8ArrayList(chunk); | ||
// Each chunk may contain multiple messages - keep calling handler for the | ||
@@ -73,0 +72,0 @@ // current parsing mode until all handlers have consumed the chunk. |
@@ -1,2 +0,2 @@ | ||
import BufferList from 'bl/BufferList.js'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import type { LengthEncoderFunction } from './varint-encode.js'; | ||
@@ -11,7 +11,7 @@ import type { Transform } from 'it-stream-types'; | ||
export declare const DEFAULT_POOL_SIZE: number; | ||
export declare function encode(options?: EncoderOptions): Transform<BufferList | Uint8Array, Uint8Array>; | ||
export declare function encode(options?: EncoderOptions): Transform<Uint8ArrayList | Uint8Array, Uint8Array>; | ||
export declare namespace encode { | ||
var single: (chunk: Uint8Array | BufferList, options?: EncoderOptions | undefined) => BufferList; | ||
var single: (chunk: Uint8Array | Uint8ArrayList, options?: EncoderOptions | undefined) => Uint8ArrayList; | ||
} | ||
export {}; | ||
//# sourceMappingURL=encode.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import BufferList from 'bl/BufferList.js'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { varintEncode } from './varint-encode.js'; | ||
@@ -29,5 +29,4 @@ import { concat as uint8ArrayConcat } from 'uint8arrays'; | ||
const encodeLength = options.lengthEncoder ?? varintEncode; | ||
// @ts-expect-error bl types are broken | ||
return new BufferList([encodeLength(chunk.length), chunk.slice()]); | ||
return new Uint8ArrayList(...[encodeLength(chunk.length), chunk.slice()]); | ||
}; | ||
//# sourceMappingURL=encode.js.map |
@@ -5,3 +5,3 @@ import { allocUnsafe } from './alloc.js'; | ||
const view = new DataView(target.buffer, target.byteOffset, target.byteLength); | ||
view.setInt32(offset, value, false); | ||
view.setInt32(offset ?? 0, value, false); | ||
return target; | ||
@@ -8,0 +8,0 @@ }; |
export interface LengthEncoderFunction { | ||
(value: number, target: Uint8Array, offset: number): Uint8Array; | ||
(value: number, target?: Uint8Array, offset?: number): Uint8Array; | ||
bytes: number; | ||
@@ -4,0 +4,0 @@ } |
@@ -6,2 +6,3 @@ import varint from 'varint'; | ||
export const varintEncode = (value, target, offset) => { | ||
// @ts-expect-error target can be undefined | ||
const ret = varint.encode(value, target, offset); | ||
@@ -8,0 +9,0 @@ varintEncode.bytes = varint.encode.bytes; |
{ | ||
"name": "it-length-prefixed", | ||
"version": "6.0.1", | ||
"version": "7.0.0", | ||
"description": "Streaming length prefixed buffers with async iterables", | ||
"author": "Alan Shaw", | ||
"license": "Apache-2.0 OR MIT", | ||
"homepage": "https://github.com/alanshaw/it-length-prefixed#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/alanshaw/it-length-prefixed.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/alanshaw/it-length-prefixed/issues" | ||
}, | ||
"keywords": [ | ||
"async", | ||
"iterable", | ||
"iterator", | ||
"length-prefixed", | ||
"length-prefixed-stream", | ||
"varint" | ||
], | ||
"engines": { | ||
"node": ">=16.0.0", | ||
"npm": ">=7.0.0" | ||
}, | ||
"type": "module", | ||
@@ -11,7 +33,11 @@ "types": "./dist/src/index.d.ts", | ||
"*", | ||
"*/index", | ||
"dist/*", | ||
"dist/*/index", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
], | ||
"src/*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
] | ||
@@ -30,7 +56,7 @@ } | ||
}, | ||
"./decode": { | ||
"import": "./dist/src/decode.js" | ||
}, | ||
"./encode": { | ||
"import": "./dist/src/encode.js" | ||
}, | ||
"./decode": { | ||
"import": "./dist/src/decode.js" | ||
} | ||
@@ -139,28 +165,9 @@ }, | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/alanshaw/it-length-prefixed.git" | ||
}, | ||
"keywords": [ | ||
"varint", | ||
"async", | ||
"iterable", | ||
"iterator", | ||
"length-prefixed-stream", | ||
"length-prefixed" | ||
], | ||
"author": "Alan Shaw", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/alanshaw/it-length-prefixed/issues" | ||
}, | ||
"homepage": "https://github.com/alanshaw/it-length-prefixed#readme", | ||
"dependencies": { | ||
"bl": "^5.0.0", | ||
"err-code": "^3.0.1", | ||
"it-stream-types": "^1.0.4", | ||
"uint8arraylist": "^1.2.0", | ||
"varint": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/bl": "^5.0.1", | ||
"@types/varint": "^6.0.0", | ||
@@ -170,3 +177,3 @@ "aegir": "^36.1.3", | ||
"it-all": "^1.0.6", | ||
"it-block": "^4.0.0", | ||
"it-block": "^5.0.0", | ||
"it-foreach": "^0.1.1", | ||
@@ -176,3 +183,3 @@ "it-map": "^1.0.6", | ||
"it-pushable": "^2.0.1", | ||
"it-reader": "^4.0.1", | ||
"it-reader": "^5.0.0", | ||
"p-defer": "^4.0.0", | ||
@@ -179,0 +186,0 @@ "random-int": "^3.0.0", |
@@ -29,3 +29,3 @@ # it-length-prefixed | ||
for await (const chunk of source) { | ||
encoded.push(chunk.slice()) // (.slice converts BufferList to Buffer) | ||
encoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
} | ||
@@ -46,3 +46,3 @@ } | ||
for await (const chunk of source) { | ||
decoded.push(chunk.slice()) // (.slice converts BufferList to Buffer) | ||
decoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
} | ||
@@ -85,11 +85,11 @@ } | ||
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects. All messages will be prefixed with a length, determined by the `lengthEncoder` function. | ||
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. All messages will be prefixed with a length, determined by the `lengthEncoder` function. | ||
### `encode.single(chunk, [opts])` | ||
- `chunk: Buffer|BufferList` chunk to encode | ||
- `chunk: Buffer|Uint8ArrayList` chunk to encode | ||
- `opts: Object`, optional | ||
- `lengthEncoder: Function`: See description above. Note that this encoder will _not_ be passed a `target` or `offset` and so will need to allocate a buffer to write to. | ||
Returns a `BufferList` containing the encoded chunk. | ||
Returns a `Uint8ArrayList` containing the encoded chunk. | ||
@@ -102,8 +102,8 @@ ### `decode([opts])` | ||
- `onLength(len: Number)`: Called for every length prefix that is decoded from the stream | ||
- `onData(data: BufferList)`: Called for every chunk of data that is decoded from the stream | ||
- `lengthDecoder: Function`: A function that decodes the length that prefixes each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) decoder. It is passed some `data` to decode which is a [`BufferList`](https://www.npmjs.com/package/bl). The function should decode the length, set the `lengthDecoder.bytes` value (the number of bytes read) and return the length. If the length cannot be decoded, the function should throw a `RangeError`. | ||
- `onData(data: Uint8ArrayList)`: Called for every chunk of data that is decoded from the stream | ||
- `lengthDecoder: Function`: A function that decodes the length that prefixes each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) decoder. It is passed some `data` to decode which is a [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist). The function should decode the length, set the `lengthDecoder.bytes` value (the number of bytes read) and return the length. If the length cannot be decoded, the function should throw a `RangeError`. | ||
- The following additional length decoders are available: | ||
- **int32BE** - `const { int32BEDecode } = require('it-length-prefixed')` | ||
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects. | ||
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. | ||
@@ -118,6 +118,6 @@ ### `decode.fromReader(reader, [opts])` | ||
- `maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB. | ||
- `onData(data: BufferList)`: Called for every chunk of data that is decoded from the stream | ||
- `onData(data: Uint8ArrayList)`: Called for every chunk of data that is decoded from the stream | ||
- `lengthEncoder: Function`: See description above. | ||
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects. | ||
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. | ||
@@ -124,0 +124,0 @@ ## Contribute |
@@ -1,2 +0,2 @@ | ||
import BufferList from 'bl/BufferList.js' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { varintDecode } from './varint-decode.js' | ||
@@ -22,10 +22,10 @@ import errCode from 'err-code' | ||
mode: string | ||
chunk?: BufferList | ||
buffer: BufferList | ||
chunk?: Uint8ArrayList | ||
buffer: Uint8ArrayList | ||
state?: ReadState | ||
data?: BufferList | ||
data?: Uint8ArrayList | ||
} | ||
interface ReadHandler { | ||
(chunk: BufferList, buffer: BufferList, state?: ReadState, options?: DecoderOptions): ReadResult | ||
(chunk: Uint8ArrayList, buffer: Uint8ArrayList, state?: ReadState, options?: DecoderOptions): ReadResult | ||
} | ||
@@ -38,3 +38,3 @@ | ||
const empty = new BufferList([]) | ||
const empty = new Uint8ArrayList() | ||
const ReadModes = { LENGTH: 'readLength', DATA: 'readData' } | ||
@@ -49,3 +49,3 @@ | ||
// console.log(ReadModes.LENGTH, chunk.length) | ||
buffer = buffer.append(chunk) | ||
buffer.append(chunk) | ||
@@ -69,4 +69,4 @@ let dataLength | ||
chunk = buffer.shallowSlice(lengthDecoder.bytes) | ||
buffer = new BufferList() | ||
chunk = buffer.subarray(lengthDecoder.bytes) | ||
buffer = new Uint8ArrayList() | ||
@@ -85,3 +85,3 @@ if (options?.onLength != null) { | ||
[ReadModes.DATA]: (chunk, buffer, state, options?) => { | ||
buffer = buffer.append(chunk) | ||
buffer.append(chunk) | ||
@@ -97,6 +97,6 @@ if (state == null) { | ||
const { dataLength } = state | ||
const data = buffer.shallowSlice(0, dataLength) | ||
const data = buffer.subarray(0, dataLength) | ||
const nextChunk = buffer.length > dataLength ? buffer.shallowSlice(dataLength) : undefined | ||
buffer = new BufferList() | ||
const nextChunk = buffer.length > dataLength ? buffer.subarray(dataLength) : undefined | ||
buffer = new Uint8ArrayList() | ||
@@ -107,5 +107,5 @@ return { mode: ReadModes.LENGTH, chunk: nextChunk, buffer, state: undefined, data } | ||
export function decode (options?: DecoderOptions): Transform<BufferList | Uint8Array, Uint8Array> { | ||
const decoder = async function * (source: Source<BufferList | Uint8Array>): Source<Uint8Array> { | ||
let buffer = new BufferList() | ||
export function decode (options?: DecoderOptions): Transform<Uint8ArrayList | Uint8Array, Uint8Array> { | ||
const decoder = async function * (source: Source<Uint8ArrayList | Uint8Array>): Source<Uint8Array> { | ||
let buffer = new Uint8ArrayList() | ||
let mode = ReadModes.LENGTH // current parsing mode | ||
@@ -115,4 +115,3 @@ let state: ReadState | undefined // accumulated state for the current mode | ||
for await (const chunk of source) { | ||
// @ts-expect-error bl types are broken | ||
let nextChunk: BufferList | undefined = new BufferList(chunk) | ||
let nextChunk: Uint8ArrayList | undefined = new Uint8ArrayList(chunk) | ||
@@ -119,0 +118,0 @@ // Each chunk may contain multiple messages - keep calling handler for the |
@@ -1,2 +0,2 @@ | ||
import BufferList from 'bl/BufferList.js' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { varintEncode } from './varint-encode.js' | ||
@@ -16,3 +16,3 @@ import { concat as uint8ArrayConcat } from 'uint8arrays' | ||
export function encode (options?: EncoderOptions): Transform<BufferList | Uint8Array, Uint8Array> { | ||
export function encode (options?: EncoderOptions): Transform<Uint8ArrayList | Uint8Array, Uint8Array> { | ||
options = options ?? {} | ||
@@ -23,3 +23,3 @@ | ||
const encoder = async function * (source: Source<BufferList | Uint8Array>): Source<Uint8Array> { | ||
const encoder = async function * (source: Source<Uint8ArrayList | Uint8Array>): Source<Uint8Array> { | ||
let pool = new Uint8Array(poolSize) | ||
@@ -45,7 +45,6 @@ let poolOffset = 0 | ||
encode.single = (chunk: BufferList | Uint8Array, options?: EncoderOptions) => { | ||
encode.single = (chunk: Uint8ArrayList | Uint8Array, options?: EncoderOptions) => { | ||
options = options ?? {} | ||
const encodeLength = options.lengthEncoder ?? varintEncode | ||
// @ts-expect-error bl types are broken | ||
return new BufferList([encodeLength(chunk.length), chunk.slice()]) | ||
return new Uint8ArrayList(...[encodeLength(chunk.length), chunk.slice()]) | ||
} |
@@ -7,5 +7,5 @@ import { allocUnsafe } from './alloc.js' | ||
const view = new DataView(target.buffer, target.byteOffset, target.byteLength) | ||
view.setInt32(offset, value, false) | ||
view.setInt32(offset ?? 0, value, false) | ||
return target | ||
} | ||
int32BEEncode.bytes = 4 // Always because fixed length |
import varint from 'varint' | ||
export interface LengthEncoderFunction { | ||
(value: number, target: Uint8Array, offset: number): Uint8Array | ||
(value: number, target?: Uint8Array, offset?: number): Uint8Array | ||
bytes: number | ||
@@ -11,3 +11,4 @@ } | ||
*/ | ||
export const varintEncode: LengthEncoderFunction = (value, target, offset) => { | ||
export const varintEncode: LengthEncoderFunction = (value, target?, offset?) => { | ||
// @ts-expect-error target can be undefined | ||
const ret = varint.encode(value, target, offset) | ||
@@ -14,0 +15,0 @@ varintEncode.bytes = varint.encode.bytes |
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
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
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
13
42411
1
80
517
+ Addeduint8arraylist@^1.2.0
+ Addedmultiformats@9.9.0(transitive)
+ Addeduint8arraylist@1.6.0(transitive)
+ Addeduint8arrays@3.1.1(transitive)
- Removedbl@^5.0.0
- Removedbase64-js@1.5.1(transitive)
- Removedbl@5.1.0(transitive)
- Removedbuffer@6.0.3(transitive)
- Removedieee754@1.2.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)