node-pkware 0.8.1 → 1.0.0--beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/implode.js ADDED
@@ -0,0 +1,361 @@
1
+ const { has, repeat, clone, last, clamp } = require('ramda')
2
+ const { isFunction } = require('ramda-adjunct')
3
+ const ExpandingBuffer = require('./helpers/ExpandingBuffer.js')
4
+ const { toHex, getLowestNBits, nBitsOfOnes } = require('./helpers/functions.js')
5
+ const { ExpectedFunctionError, InvalidDictionarySizeError, InvalidCompressionTypeError } = require('./errors.js')
6
+ const {
7
+ ChBitsAsc,
8
+ ChCodeAsc,
9
+ LONGEST_ALLOWED_REPETITION,
10
+ DICTIONARY_SIZE_LARGE,
11
+ DICTIONARY_SIZE_MEDIUM,
12
+ DICTIONARY_SIZE_SMALL,
13
+ COMPRESSION_BINARY,
14
+ COMPRESSION_ASCII,
15
+ ExLenBits,
16
+ LenBits,
17
+ LenCode,
18
+ DistCode,
19
+ DistBits
20
+ } = require('./constants.js')
21
+
22
+ const setup = state => {
23
+ state.nChBits = repeat(0, 0x306)
24
+ state.nChCodes = repeat(0, 0x306)
25
+
26
+ switch (state.dictionarySizeBits) {
27
+ case DICTIONARY_SIZE_LARGE:
28
+ state.dictionarySizeMask = nBitsOfOnes(6)
29
+ break
30
+ case DICTIONARY_SIZE_MEDIUM:
31
+ state.dictionarySizeMask = nBitsOfOnes(5)
32
+ break
33
+ case DICTIONARY_SIZE_SMALL:
34
+ state.dictionarySizeMask = nBitsOfOnes(4)
35
+ break
36
+ default:
37
+ throw new InvalidDictionarySizeError()
38
+ }
39
+
40
+ switch (state.compressionType) {
41
+ case COMPRESSION_BINARY:
42
+ for (let nChCode = 0, nCount = 0; nCount < 0x100; nCount++) {
43
+ state.nChBits[nCount] = 9
44
+ state.nChCodes[nCount] = nChCode
45
+ nChCode = getLowestNBits(16, nChCode) + 2
46
+ }
47
+ break
48
+ case COMPRESSION_ASCII:
49
+ for (let nCount = 0; nCount < 0x100; nCount++) {
50
+ state.nChBits[nCount] = ChBitsAsc[nCount] + 1
51
+ state.nChCodes[nCount] = ChCodeAsc[nCount] * 2
52
+ }
53
+ break
54
+ default:
55
+ throw new InvalidCompressionTypeError()
56
+ }
57
+
58
+ let nCount = 0x100
59
+ for (let i = 0; i < 0x10; i++) {
60
+ for (let nCount2 = 0; nCount2 < 1 << ExLenBits[i]; nCount2++) {
61
+ state.nChBits[nCount] = ExLenBits[i] + LenBits[i] + 1
62
+ state.nChCodes[nCount] = (nCount2 << (LenBits[i] + 1)) | (LenCode[i] * 2) | 1
63
+ nCount++
64
+ }
65
+ }
66
+
67
+ state.outputBuffer.append(Buffer.from([state.compressionType, state.dictionarySizeBits, 0]))
68
+ state.outBits = 0
69
+ }
70
+
71
+ const outputBits = (state, nBits, bitBuffer) => {
72
+ if (nBits > 8) {
73
+ outputBits(state, 8, bitBuffer)
74
+ bitBuffer = bitBuffer >> 8
75
+ nBits = nBits - 8
76
+ }
77
+
78
+ const outBits = state.outBits
79
+
80
+ // in the original code bitBuffer is long, but is cast to char
81
+ const lastBytes = state.outputBuffer.read(state.outputBuffer.size() - 1, 1)
82
+ state.outputBuffer.dropEnd(1)
83
+ state.outputBuffer.append(Buffer.from([lastBytes | getLowestNBits(8, bitBuffer << outBits)]))
84
+
85
+ state.outBits = state.outBits + nBits
86
+
87
+ if (state.outBits > 8) {
88
+ bitBuffer = bitBuffer >> (8 - outBits)
89
+ state.outputBuffer.append(Buffer.from([getLowestNBits(8, bitBuffer)]))
90
+ state.outBits = getLowestNBits(3, state.outBits)
91
+ } else {
92
+ state.outBits = getLowestNBits(3, state.outBits)
93
+ if (state.outBits === 0) {
94
+ state.outputBuffer.append(Buffer.from([0]))
95
+ }
96
+ }
97
+ }
98
+
99
+ // ---------------------------------
100
+
101
+ const getSizeOfMatching = (inputBytes, a, b) => {
102
+ const limit = clamp(2, LONGEST_ALLOWED_REPETITION, b - a)
103
+
104
+ for (let i = 2; i <= limit; i++) {
105
+ if (inputBytes[a + i] !== inputBytes[b + i]) {
106
+ return i
107
+ }
108
+ }
109
+
110
+ return limit
111
+ }
112
+
113
+ // TODO: make sure that we find the most recent one, which in turn allows
114
+ // us to store backward length in less amount of bits
115
+ // currently the code goes from the furthest point
116
+ const findRepetitions = (inputBytes, endOfLastMatch, cursor) => {
117
+ if (endOfLastMatch === cursor || cursor - endOfLastMatch < 2) {
118
+ return { size: 0, distance: 0 }
119
+ }
120
+
121
+ const haystack = inputBytes.slice(endOfLastMatch, cursor)
122
+ const needle = inputBytes.slice(cursor, cursor + 2)
123
+
124
+ const matchIndex = haystack.indexOf(needle)
125
+ if (matchIndex !== -1) {
126
+ const distance = cursor - endOfLastMatch - matchIndex
127
+ return {
128
+ distance: distance - 1,
129
+ size: distance > 2 ? getSizeOfMatching(inputBytes, endOfLastMatch + matchIndex, cursor) : 2
130
+ }
131
+ }
132
+
133
+ return { size: 0, distance: 0 }
134
+ }
135
+
136
+ // this function can return:
137
+ // false - not flushable
138
+ // true - flushable
139
+ // null - flushable, but there might be a better repetition
140
+ const isRepetitionFlushable = (size, distance, startIndex, inputBufferSize) => {
141
+ if (size === 0) {
142
+ return false
143
+ }
144
+
145
+ // If we found repetition of 2 bytes, that is 0x100 or fuhrter back,
146
+ // don't bother. Storing the distance of 0x100 bytes would actually
147
+ // take more space than storing the 2 bytes as-is.
148
+ if (size === 2 && distance >= 0x100) {
149
+ return false
150
+ }
151
+
152
+ if (size >= 8 || startIndex + 1 >= inputBufferSize) {
153
+ return true
154
+ }
155
+
156
+ return null
157
+ }
158
+
159
+ // ---------------------------------
160
+
161
+ // repetitions are at least 2 bytes long,
162
+ // so the initial 2 bytes can be moved to the output as is
163
+ const handleFirstTwoBytes = state => {
164
+ if (state.handledFirstTwoBytes) {
165
+ return
166
+ }
167
+
168
+ if (state.inputBuffer.size() < 3) {
169
+ return
170
+ }
171
+
172
+ const [byte1, byte2] = state.inputBuffer.read(0, 2)
173
+ outputBits(state, state.nChBits[byte1], state.nChCodes[byte1])
174
+ outputBits(state, state.nChBits[byte2], state.nChCodes[byte2])
175
+
176
+ state.handledFirstTwoBytes = true
177
+ state.startIndex += 2
178
+ }
179
+
180
+ const processChunkData = (state, debug = false) => {
181
+ if (!has('dictionarySizeMask', state)) {
182
+ setup(state)
183
+ }
184
+
185
+ if (!state.inputBuffer.isEmpty()) {
186
+ state.startIndex = 0
187
+
188
+ handleFirstTwoBytes(state)
189
+
190
+ // -------------------------------
191
+
192
+ /* eslint-disable prefer-const */
193
+
194
+ let endOfLastMatch = 0
195
+ while (state.startIndex < state.inputBuffer.size()) {
196
+ let { size, distance } = findRepetitions(state.inputBuffer.read(endOfLastMatch), endOfLastMatch, state.startIndex)
197
+
198
+ const isFlushable = isRepetitionFlushable(size, distance, state.startIndex, state.inputBuffer.size())
199
+
200
+ if (isFlushable === false) {
201
+ const byte = state.inputBuffer.read(state.startIndex, 1)
202
+ outputBits(state, state.nChBits[byte], state.nChCodes[byte])
203
+ state.startIndex += 1
204
+ } else {
205
+ /*
206
+ if (isFlushable === null) {
207
+ // Try to find better repetition 1 byte later.
208
+ // stormlib/implode.c L517
209
+
210
+ // let cursor = state.startIndex
211
+ // let newSize = size
212
+ // let newDistance = distance
213
+ // let currentSize
214
+ // let currentDistance
215
+ // while (newSize <= currentSize && isRepetitionFlushable(newSize, newDistance, state.startIndex, state.inputBuffer.size())) {
216
+ // currentSize = newSize
217
+ // currentDistance = newDistance
218
+ // const reps = findRepetitions(state.inputBuffer.read(endOfLastMatch), endOfLastMatch, ++cursor)
219
+ // newSize = reps.size
220
+ // newDistance = reps.distance
221
+ // }
222
+ // size = newSize
223
+ // distance = currentDistance
224
+ }
225
+ */
226
+
227
+ /*
228
+ endOfLastMatch = state.startIndex + size
229
+
230
+ const byte = size + 0xfe
231
+ outputBits(state, state.nChBits[byte], state.nChCodes[byte])
232
+ if (size === 2) {
233
+ const byte = distance >> 2
234
+ outputBits(state, state.distBits[byte], state.distCodes[byte])
235
+ outputBits(state, 2, distance & 3)
236
+ } else {
237
+ const byte = distance >> state.dictionarySizeBits
238
+ outputBits(state, state.distBits[byte], state.distCodes[byte])
239
+ outputBits(state, state.dictionarySizeBits, state.dictionarySizeMask & distance)
240
+ }
241
+
242
+ state.startIndex += size
243
+ */
244
+
245
+ // TODO: temporarily write out data byte-by-byte here too, because above block with minimal repetition
246
+ // flushing breaks the compression self check tests
247
+ const byte = state.inputBuffer.read(state.startIndex, 1)
248
+ outputBits(state, state.nChBits[byte], state.nChCodes[byte])
249
+ state.startIndex += 1
250
+ }
251
+
252
+ state.inputBuffer.dropStart(endOfLastMatch)
253
+ state.startIndex -= endOfLastMatch
254
+ endOfLastMatch = 0
255
+ }
256
+
257
+ /* eslint-enable prefer-const */
258
+
259
+ // -------------------------------
260
+
261
+ state.inputBuffer.dropStart(state.inputBuffer.size())
262
+ }
263
+
264
+ if (state.streamEnded) {
265
+ // Write the termination literal
266
+ outputBits(state, last(state.nChBits), last(state.nChCodes))
267
+ }
268
+ }
269
+
270
+ const implode = (compressionType, dictionarySizeBits, config = {}) => {
271
+ const { debug = false, inputBufferSize = 0x0, outputBufferSize = 0x0 } = config
272
+
273
+ const handler = function (chunk, encoding, callback) {
274
+ if (!isFunction(callback)) {
275
+ // can't call callback to pass in data or errors, so we throw up
276
+ throw new ExpectedFunctionError()
277
+ }
278
+
279
+ const state = handler._state
280
+
281
+ try {
282
+ state.inputBuffer.append(chunk)
283
+ if (state.isFirstChunk) {
284
+ state.isFirstChunk = false
285
+ this._flush = state.onInputFinished
286
+ }
287
+
288
+ if (debug) {
289
+ console.log(`reading ${toHex(chunk.length)} bytes from chunk #${state.stats.chunkCounter++}`)
290
+ }
291
+
292
+ processChunkData(state, debug)
293
+
294
+ const blockSize = 0x800
295
+ if (state.outputBuffer.size() > blockSize) {
296
+ const numberOfBytes = (Math.floor(state.outputBuffer.size() / blockSize) - 1) * blockSize
297
+ const output = Buffer.from(state.outputBuffer.read(0, numberOfBytes))
298
+ state.outputBuffer.flushStart(numberOfBytes)
299
+
300
+ if (state.outBits === 0) {
301
+ // set last byte to 0
302
+ state.outputBuffer.dropEnd(1)
303
+ state.outputBuffer.append(Buffer.from([0]))
304
+ }
305
+
306
+ callback(null, output)
307
+ } else {
308
+ callback(null, Buffer.from([]))
309
+ }
310
+ } catch (e) {
311
+ callback(e)
312
+ }
313
+ }
314
+
315
+ handler._state = {
316
+ isFirstChunk: true,
317
+ streamEnded: false,
318
+ compressionType: compressionType,
319
+ dictionarySizeBits: dictionarySizeBits,
320
+ distCodes: clone(DistCode),
321
+ distBits: clone(DistBits),
322
+ startIndex: 0,
323
+ inputBuffer: new ExpandingBuffer(inputBufferSize),
324
+ outputBuffer: new ExpandingBuffer(outputBufferSize),
325
+ handledFirstTwoBytes: false,
326
+ onInputFinished: callback => {
327
+ const state = handler._state
328
+ state.streamEnded = true
329
+ try {
330
+ processChunkData(state, debug)
331
+
332
+ if (debug) {
333
+ console.log('---------------')
334
+ console.log('total number of chunks read:', state.stats.chunkCounter)
335
+ console.log('inputBuffer heap size', toHex(state.inputBuffer.heapSize()))
336
+ console.log('outputBuffer heap size', toHex(state.outputBuffer.heapSize()))
337
+ }
338
+
339
+ callback(null, state.outputBuffer.read())
340
+ } catch (e) {
341
+ callback(e)
342
+ }
343
+ },
344
+ stats: {
345
+ chunkCounter: 0
346
+ }
347
+ }
348
+
349
+ return handler
350
+ }
351
+
352
+ module.exports = {
353
+ setup,
354
+ outputBits,
355
+ getSizeOfMatching,
356
+ findRepetitions,
357
+ isRepetitionFlushable,
358
+ handleFirstTwoBytes,
359
+ processChunkData,
360
+ implode
361
+ }
package/src/index.js ADDED
@@ -0,0 +1,18 @@
1
+ const { implode } = require('./implode.js')
2
+ const { explode } = require('./explode.js')
3
+ const constants = require('./constants.js')
4
+ const errors = require('./errors.js')
5
+ const stream = require('./helpers/stream.js')
6
+
7
+ const compress = implode
8
+ const decompress = explode
9
+
10
+ module.exports = {
11
+ implode,
12
+ compress,
13
+ explode,
14
+ decompress,
15
+ constants,
16
+ errors,
17
+ stream
18
+ }
@@ -0,0 +1,41 @@
1
+ export const COMPRESSION_BINARY: 0
2
+ export const COMPRESSION_ASCII: 1
3
+ export const DICTIONARY_SIZE_SMALL: 4
4
+ export const DICTIONARY_SIZE_MEDIUM: 5
5
+ export const DICTIONARY_SIZE_LARGE: 6
6
+ export const LONGEST_ALLOWED_REPETITION: 0x204
7
+
8
+ export const PKDCL_OK: 'OK'
9
+ export const PKDCL_STREAM_END: 'All data from the input stream is read'
10
+ export const PKDCL_NEED_DICT: 'Need more data (dictionary)'
11
+ export const PKDCL_CONTINUE: 'Continue (internal flag)'
12
+ export const PKDCL_GET_INPUT: 'Get input (internal flag)'
13
+
14
+ export const LITERAL_END_STREAM: 0x305
15
+ export const LITERAL_STREAM_ABORTED: 0x306
16
+
17
+ export const DistCode: number[]
18
+ export const DistBits: number[]
19
+ export const LenBits: number[]
20
+ export const LenCode: number[]
21
+ export const ExLenBits: number[]
22
+ export const LenBase: number[]
23
+ export const ChBitsAsc: number[]
24
+ export const ChCodeAsc: number[]
25
+
26
+ // Additional types
27
+
28
+ /**
29
+ * Compression types for implode
30
+ */
31
+ export type CompressionType = typeof COMPRESSION_BINARY | typeof COMPRESSION_ASCII
32
+ /**
33
+ * Dictionary sizes for implode, determines how well the file get compressed.
34
+ *
35
+ * Small dictionary size means less memory to lookback in data for repetitions, meaning it will be less effective, the file stays larger, less compressed.
36
+ * On the other hand, large compression allows more lookback allowing more effective compression, thus generating smaller, more compressed files.
37
+ */
38
+ export type DictionarySizeBits =
39
+ | typeof DICTIONARY_SIZE_SMALL
40
+ | typeof DICTIONARY_SIZE_MEDIUM
41
+ | typeof DICTIONARY_SIZE_LARGE
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Thrown by
3
+ * - `implode` when invalid dictionary size was specified
4
+ * - `explode` when it encounters invalid data in the header section (the first 2 bytes of a compressed files)
5
+ */
6
+ export class InvalidDictionarySizeError extends Error {}
7
+
8
+ /**
9
+ * Thrown by
10
+ * - `implode` when invalid compression type was specified
11
+ * - `explode` when it encounters invalid data in the header section (the first 2 bytes of a compressed files)
12
+ */
13
+ export class InvalidCompressionTypeError extends Error {}
14
+
15
+ /**
16
+ * Thrown by
17
+ * - `explode`, when compressed data is less, than `5` bytes long
18
+ *
19
+ * Pkware compressed files have 2 bytes header followed by at lest 2 bytes of data and an end literal.
20
+ */
21
+ export class InvalidDataError extends Error {}
22
+
23
+ /**
24
+ * Thrown by
25
+ * - `explode` when compressed data ends without reaching the end literal or in mid decompression
26
+ */
27
+ export class AbortedError extends Error {}
28
+
29
+ export class ExpectedBufferError extends TypeError {}
30
+ export class ExpectedFunctionError extends TypeError {}
@@ -0,0 +1,56 @@
1
+ import ExpandingBuffer from './helpers/ExpandingBuffer'
2
+ import { Callback, Config, Handler, PrivateState } from './helpers/Shared'
3
+ import { CompressionType, DictionarySizeBits, PKDCL_STREAM_END, PKDCL_OK, LITERAL_STREAM_ABORTED } from './constants'
4
+
5
+ export function readHeader(buffer: Buffer): {
6
+ compressionType: CompressionType
7
+ dictionarySizeBits: DictionarySizeBits
8
+ }
9
+
10
+ type PrivateExplodeState = {
11
+ _backup: {
12
+ extraBits: number
13
+ bitBuffer: Buffer
14
+ }
15
+ needMoreInput: boolean
16
+ isFirstChunk: boolean
17
+ extraBits: number
18
+ bitBuffer: Buffer
19
+ chBitsAsc: number[] // DecodeLit and GenAscTabs uses this
20
+ lengthCodes: number[]
21
+ distPosCodes: number[]
22
+ inputBuffer: ExpandingBuffer
23
+ outputBuffer: ExpandingBuffer
24
+ onInputFinished(callback: Callback): void
25
+ backup(): void
26
+ restore(): void
27
+ stats: {
28
+ chunkCounter: number
29
+ }
30
+ compressionType: CompressionType
31
+ dictionarySizeBits: DictionarySizeBits
32
+ dictionarySizeMask: number
33
+ asciiTable2C34: number[]
34
+ asciiTable2D34: number[]
35
+ asciiTable2E34: number[]
36
+ asciiTable2EB4: number[]
37
+ }
38
+
39
+ /**
40
+ * Decompresses stream
41
+ * @returns a function, that you can use as a `transform._transform` method.
42
+ */
43
+ export function explode(config?: Config): PrivateState<PrivateExplodeState> & Handler
44
+ export function createPATIterator(limit: number, stepper: number): (n: number) => false | [number, number]
45
+ export function populateAsciiTable(value: number, index: number, bits: number, limit: number): number[]
46
+ export function generateAsciiTables(): {
47
+ asciiTable2C34: number[]
48
+ asciiTable2D34: number[]
49
+ asciiTable2E34: number[]
50
+ asciiTable2EB4: number[]
51
+ }
52
+ export function processChunkData(state: PrivateExplodeState, debug?: boolean): void
53
+ export function wasteBits(state: PrivateExplodeState, numberOfBits: number): typeof PKDCL_STREAM_END | typeof PKDCL_OK
54
+ export function decodeNextLiteral(state: PrivateExplodeState): typeof LITERAL_STREAM_ABORTED | number
55
+ export function decodeDistance(state: PrivateExplodeState, repeatLength: number): number
56
+ export function generateDecodeTables(startIndexes: number[], lengthBits: number[]): number[]
@@ -0,0 +1,25 @@
1
+ export = ExpandingBuffer
2
+ declare class ExpandingBuffer {
3
+ constructor(numberOfBytes?: number)
4
+ _heap: Buffer
5
+ _startIndex: number
6
+ _endIndex: number
7
+ _backup: {
8
+ _startIndex: number
9
+ _endIndex: number
10
+ }
11
+ _getActualData(offset?: number): Buffer
12
+ size(): number
13
+ isEmpty(): boolean
14
+ heapSize(): number
15
+ append(buffer: Buffer): void
16
+ read(offset: number, limit: number): number | Buffer
17
+ flushStart(numberOfBytes: number): void
18
+ flushEnd(numberOfBytes: number): void
19
+ dropStart(numberOfBytes: number): void
20
+ dropEnd(numberOfBytes: number): void
21
+ getHeap(): Buffer
22
+ clear(): void
23
+ _saveIndices(): void
24
+ _restoreIndices(): void
25
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Shared type signatures used throughout the library.
3
+ */
4
+
5
+ /**
6
+ * A traditional "nodeback<Buffer>" callback type.
7
+ * @param error The error, if any.
8
+ * @param data The data, if any.
9
+ */
10
+ export type Callback = (error: Error | null, chunk: Buffer) => void
11
+
12
+ /**
13
+ * Handler for one chunk of bytes
14
+ * @param chunk The chunk of bytes
15
+ * @param encoding The encoding of the chunk (not used)
16
+ * @param callback The callback to call when done
17
+ */
18
+ export type Handler = (chunk: Buffer, encoding: unknown, callback: Callback) => void
19
+
20
+ /**
21
+ * How the implode & explode functions store their internal state.
22
+ */
23
+ export type PrivateState<T> = { _state: T }
24
+
25
+ /**
26
+ * Configuration options for the implode & explode functions.
27
+ */
28
+ export type Config = {
29
+ /**
30
+ * Whether the code should display debug messages on the console or not
31
+ * @default false
32
+ */
33
+ debug?: boolean
34
+ /**
35
+ * The starting size of the input buffer, may expand later as needed.
36
+ * Not having to expand may have performance impact.
37
+ * @default 0
38
+ */
39
+ inputBufferSize?: number
40
+ /**
41
+ * The starting size of the output buffer, may expand later as needed.
42
+ * Not having to expand may have performance impact.
43
+ * @default 0
44
+ */
45
+ outputBufferSize?: number
46
+ }
@@ -0,0 +1,10 @@
1
+ export function isBetween(min: number, max: number, num: number): boolean
2
+ export function nBitsOfOnes(numberOfBits: number): number
3
+ export function maskBits(numberOfBits: number, number: number): number
4
+ export function getLowestNBits(numberOfBits: number, number: number): number
5
+ export function isFullHexString(str: string): boolean
6
+ export function toHex(num: number, digits?: number, withoutPrefix?: boolean): string
7
+ export function mergeSparseArrays<T>(a: T[], b: T[]): T[]
8
+ export function parseNumberString(n: string, defaultValue?: number): number
9
+ export function getPackageVersion(): Promise<string>
10
+ export function fileExists(filename: string): Promise<boolean>
@@ -0,0 +1,61 @@
1
+ import { Transform, Writable } from 'stream'
2
+ import { Callback } from './Shared'
3
+
4
+ type QuasiTransformConstructorParameter = (
5
+ obj: {
6
+ handler: QuasiTransformConstructorParameter
7
+ handle(chunk: Buffer, encoding: string): Promise<void>
8
+ },
9
+ chunk: Buffer,
10
+ encoding: string
11
+ ) => void
12
+
13
+ /**
14
+ * Creates a "**predicate**" function, that awaits Buffers, keeps an internal counter of the bytes from them and splits the appropriate buffer at the given index.
15
+ * Splitting is done by returning an array with `[left: Buffer, right: Buffer, isLeftDone: bool]`.
16
+ * If you want to split data at the 100th byte and you keep feeding 60 byte long buffers to the function returned by `splitAt(100)`, then it will return arrays in the following manner:
17
+ * 1. `[inputBuffer, emptyBuffer, false]`
18
+ * 2. `[inputBuffer.slice(0, 40), inputBuffer.slice(40, 60), true]`
19
+ * 3. `[emptyBuffer, inputBuffer, true]`
20
+ * 4. `[emptyBuffer, inputBuffer, true]`
21
+ * 5. ... and so on
22
+ * @param index at which to split the buffer
23
+ */
24
+ export function splitAt(index: number): (chunk: Buffer) => null | [Buffer, Buffer, boolean]
25
+
26
+ /**
27
+ * A `transform._transform` type function, which lets the input chunks through without any change
28
+ */
29
+ export function transformIdentity(): (chunk: Buffer, encoding: unknown, callback: Callback) => void
30
+
31
+ /**
32
+ * A `transform._transform` type function, which for every input chunk will output an empty buffer
33
+ */
34
+ export function transformEmpty(): (chunk: unknown, encoding: unknown, callback: Callback) => void
35
+
36
+ /**
37
+ * Takes a `transform._transform` type function and turns it into a Transform stream instance
38
+ * @param handler a transform._transform type function
39
+ * @returns a Transform stream instance
40
+ */
41
+ export function through(handler: Exclude<ConstructorParameters<typeof Transform>[0], undefined>['transform']): Transform
42
+
43
+ /**
44
+ * Higher order function for introducing conditional logic to `transform._transform` functions.
45
+ * This is used internally to handle offsets for `explode()`.
46
+ * @param predicate
47
+ * @param leftHandler
48
+ * @param rightHandler
49
+ * @returns `transform._transform`
50
+ */
51
+ export function transformSplitBy(
52
+ predicate: (chunk: Buffer) => [Buffer, Buffer, boolean],
53
+ leftHandler: QuasiTransformConstructorParameter,
54
+ rightHandler: QuasiTransformConstructorParameter
55
+ ): (chunk: Buffer, encoding: string, callback: Callback) => void
56
+
57
+ /**
58
+ * Data can be piped to the returned function from a stream and it will concatenate all chunks into a single buffer.
59
+ * @param done a callback function, which will receive the concatenated buffer as a parameter
60
+ */
61
+ export function streamToBuffer(done: (heap: Buffer) => void): Writable
@@ -0,0 +1,6 @@
1
+ import { Handler } from './Shared'
2
+
3
+ export function isClass(obj: any): obj is Object
4
+ export function buffersShouldEqual(expected: Buffer, result: Buffer, offset?: number, displayAsHex?: boolean): void
5
+ export function bufferToString(buffer: Buffer, limit?: number): string
6
+ export function transformToABC(): Handler