Skip to content

Commit

Permalink
change debug option to verbose in both api and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Dec 2, 2022
1 parent 29901d1 commit 6de86a6
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 45 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Calling either explode or implode with the `-v` or `--version` flag will display

`implode test/files/fast.fts.unpacked --output=C:/fast.fts --binary --large --offset=1816`

`explode test/files/fast.fts --auto-detect --debug --output=E:/fast.fts.unpacked`
`explode test/files/fast.fts --auto-detect --verbose --output=E:/fast.fts.unpacked`

`explode test/files/fast.fts --auto-detect --debug --output=E:/fast.fts.unpacked --offset=2000`
`explode test/files/fast.fts --auto-detect --verbose --output=E:/fast.fts.unpacked --offset=2000`

### piping also works

**Don't use --debug when piping, because debug messages will be outputted to where the decompressed data is being outputted!**
**Don't use --verbose when piping, because verbose messages will be outputted to where the decompressed data is being outputted!**

`cat c:/arx/level8.llf | explode > c:/arx/level8.llf.unpacked`

Expand All @@ -66,7 +66,7 @@ Takes an optional config object, which has the following properties:

```js
{
debug: boolean, // whether the code should display debug messages on the console or not (default = false)
verbose: boolean, // whether the code should display extra debug messages on the console or not (default = false)
inputBufferSize: int, // the starting size of the input buffer, may expand later as needed. Not having to expand may have performance impact (default 0)
outputBufferSize: int // same as inputBufferSize, but for the outputBuffer (default 0)
}
Expand All @@ -80,7 +80,7 @@ Takes an optional config object, which has the following properties:

```js
{
debug: boolean, // whether the code should display debug messages on the console or not (default = false)
verbose: boolean, // whether the code should display extra debug messages on the console or not (default = false)
inputBufferSize: int, // the starting size of the input buffer, may expand later as needed. Not having to expand may have performance impact (default 0)
outputBufferSize: int // same as inputBufferSize, but for the outputBuffer (default 0)
}
Expand Down Expand Up @@ -148,9 +148,9 @@ const { through, streamToBuffer } = stream
Readable.from(buffer) // buffer is of type Buffer with compressed data
.pipe(through(explode()))
.pipe(
streamToBuffer(decompressedData => {
streamToBuffer((decompressedData) => {
// decompressedData holds the decompressed buffer
})
}),
)
```

Expand Down Expand Up @@ -190,18 +190,18 @@ const { explode, stream } = require('node-pkware')
const { through } = stream

fs.createReadStream(`path-to-compressed-file`)
.on('error', err => {
.on('error', (err) => {
console.error('readstream error')
})
.pipe(
through(explode()).on('error', err => {
through(explode()).on('error', (err) => {
console.error('explode error')
})
}),
)
.pipe(
fs.createWriteStream(`path-to-write-decompressed-data`).on('error', err => {
fs.createWriteStream(`path-to-write-decompressed-data`).on('error', (err) => {
console.error('writestream error')
})
}),
)
```

Expand Down
4 changes: 2 additions & 2 deletions bin/explode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { explode } = require('../src/explode.js')

const args = minimist(process.argv.slice(2), {
string: ['output', 'offset', 'input-buffer-size', 'output-buffer-size'],
boolean: ['version', 'drop-before-offset', 'debug'],
boolean: ['version', 'drop-before-offset', 'verbose'],
alias: {
v: 'version',
},
Expand Down Expand Up @@ -62,7 +62,7 @@ const decompress = (input, output, offset, keepHeader, config) => {

const keepHeader = !args['drop-before-offset']
const config = {
debug: args.debug,
verbose: args.verbose,
inputBufferSize: parseNumberString(args['input-buffer-size'], 0x10000),
outputBufferSize: parseNumberString(args['output-buffer-size'], 0x40000),
}
Expand Down
4 changes: 2 additions & 2 deletions bin/implode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const decompress = (input, output, offset, keepHeader, compressionType, dictiona

const args = minimist(process.argv.slice(2), {
string: ['output', 'offset', 'input-buffer-size', 'output-buffer-size'],
boolean: ['version', 'binary', 'ascii', 'drop-before-offset', 'debug', 'small', 'medium', 'large'],
boolean: ['version', 'binary', 'ascii', 'drop-before-offset', 'verbose', 'small', 'medium', 'large'],
alias: {
a: 'ascii',
b: 'binary',
Expand Down Expand Up @@ -100,7 +100,7 @@ const args = minimist(process.argv.slice(2), {

const keepHeader = !args['drop-before-offset']
const config = {
debug: args.debug,
verbose: args.verbose,
inputBufferSize: parseNumberString(args['input-buffer-size'], 0x10000),
outputBufferSize: parseNumberString(args['output-buffer-size'], 0x12000),
}
Expand Down
16 changes: 8 additions & 8 deletions src/explode.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const generateAsciiTables = () => {
return tables
}

const parseInitialData = (state, debug = false) => {
const parseInitialData = (state, verbose = false) => {
if (state.inputBuffer.size() < 4) {
return false
}
Expand All @@ -124,7 +124,7 @@ const parseInitialData = (state, debug = false) => {
})
}

if (debug) {
if (verbose) {
console.log(`explode: compression type: ${state.compressionType === COMPRESSION_BINARY ? 'binary' : 'ascii'}`)
console.log(
`explode: compression level: ${
Expand Down Expand Up @@ -244,13 +244,13 @@ const decodeDistance = (state, repeatLength) => {
return distance + 1
}

const processChunkData = (state, debug = false) => {
const processChunkData = (state, verbose = false) => {
if (state.inputBuffer.isEmpty()) {
return
}

if (!has('compressionType', state)) {
const parsedHeader = parseInitialData(state, debug)
const parsedHeader = parseInitialData(state, verbose)
if (!parsedHeader || state.inputBuffer.isEmpty()) {
return
}
Expand Down Expand Up @@ -309,7 +309,7 @@ const generateDecodeTables = (startIndexes, lengthBits) => {
}

const explode = (config = {}) => {
const { debug = false, inputBufferSize = 0x0, outputBufferSize = 0x0 } = config
const { verbose = false, inputBufferSize = 0x0, outputBufferSize = 0x0 } = config

const handler = function (chunk, encoding, callback) {
if (!isFunction(callback)) {
Expand All @@ -327,11 +327,11 @@ const explode = (config = {}) => {
this._flush = state.onInputFinished
}

if (debug) {
if (verbose) {
console.log(`explode: reading ${toHex(chunk.length)} bytes from chunk #${state.stats.chunkCounter++}`)
}

processChunkData(state, debug)
processChunkData(state, verbose)

const blockSize = 0x1000
if (state.outputBuffer.size() > blockSize) {
Expand Down Expand Up @@ -364,7 +364,7 @@ const explode = (config = {}) => {
onInputFinished: (callback) => {
const state = handler._state

if (debug) {
if (verbose) {
console.log('---------------')
console.log('explode: total number of chunks read:', state.stats.chunkCounter)
console.log('explode: inputBuffer heap size', toHex(state.inputBuffer.heapSize()))
Expand Down
12 changes: 6 additions & 6 deletions src/implode.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const handleFirstTwoBytes = (state) => {
state.startIndex += 2
}

const processChunkData = (state, debug = false) => {
const processChunkData = (state, verbose = false) => {
if (!has('dictionarySizeMask', state)) {
setup(state)
}
Expand Down Expand Up @@ -271,7 +271,7 @@ const processChunkData = (state, debug = false) => {
}

const implode = (compressionType, dictionarySizeBits, config = {}) => {
const { debug = false, inputBufferSize = 0x0, outputBufferSize = 0x0 } = config
const { verbose = false, inputBufferSize = 0x0, outputBufferSize = 0x0 } = config

const handler = function (chunk, encoding, callback) {
if (!isFunction(callback)) {
Expand All @@ -288,11 +288,11 @@ const implode = (compressionType, dictionarySizeBits, config = {}) => {
this._flush = state.onInputFinished
}

if (debug) {
if (verbose) {
console.log(`implode: reading ${toHex(chunk.length)} bytes from chunk #${state.stats.chunkCounter++}`)
}

processChunkData(state, debug)
processChunkData(state, verbose)

const blockSize = 0x800
if (state.outputBuffer.size() > blockSize) {
Expand Down Expand Up @@ -330,9 +330,9 @@ const implode = (compressionType, dictionarySizeBits, config = {}) => {
const state = handler._state
state.streamEnded = true
try {
processChunkData(state, debug)
processChunkData(state, verbose)

if (debug) {
if (verbose) {
console.log('---------------')
console.log('implode: total number of chunks read:', state.stats.chunkCounter)
console.log('implode: inputBuffer heap size', toHex(state.inputBuffer.heapSize()))
Expand Down
7 changes: 5 additions & 2 deletions test/explode.files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const defineTestForSimpleFiles = (highWaterMark) => {
}
fs.createReadStream(`${TEST_FILE_FOLDER}${folder}/${compressedFile}`, { highWaterMark })
.on('error', done)
.pipe(through(explode({ debug: true })).on('error', done))
.pipe(through(explode({ verbose: true })).on('error', done))
.pipe(
streamToBuffer((buffer) => {
buffersShouldEqual(buffer, expected, 0, true)
Expand Down Expand Up @@ -52,7 +52,10 @@ const defineTestForFilesWithOffset = (highWaterMark) => {
fs.createReadStream(`${TEST_FILE_FOLDER}${folder}/${compressedFile}`, { highWaterMark })
.on('error', done)
.pipe(
through(transformSplitBy(splitAt(offset), transformIdentity(), explode({ debug: true }))).on('error', done),
through(transformSplitBy(splitAt(offset), transformIdentity(), explode({ verbose: true }))).on(
'error',
done,
),
)
.pipe(
streamToBuffer((buffer) => {
Expand Down
15 changes: 9 additions & 6 deletions test/implode.files.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const defineTestForImplodeSelfCheck = (highWaterMark) => {

fs.createReadStream(`${TEST_FILE_FOLDER}${folder}/${decompressedFile}`, { highWaterMark })
.on('error', done)
.pipe(through(implode(compressionType, dictionarySize, { debug: true })))
.pipe(through(explode({ debug: true })).on('error', done))
.pipe(through(implode(compressionType, dictionarySize, { verbose: true })))
.pipe(through(explode({ verbose: true })).on('error', done))
.pipe(
streamToBuffer((buffer) => {
buffersShouldEqual(buffer, expected, 0, true)
Expand Down Expand Up @@ -69,12 +69,15 @@ const defineTestForImplodeSelfCheckWithOffset = (highWaterMark) => {
transformSplitBy(
splitAt(offset),
transformIdentity(),
implode(compressionType, dictionarySize, { debug: true }),
implode(compressionType, dictionarySize, { verbose: true }),
),
),
)
.pipe(
through(transformSplitBy(splitAt(offset), transformIdentity(), explode({ debug: true }))).on('error', done),
through(transformSplitBy(splitAt(offset), transformIdentity(), explode({ verbose: true }))).on(
'error',
done,
),
)
.pipe(
streamToBuffer((buffer) => {
Expand Down Expand Up @@ -108,7 +111,7 @@ const defineTestForSimpleFiles = (highWaterMark) => {
}
fs.createReadStream(`${TEST_FILE_FOLDER}${folder}/${decompressedFile}`, { highWaterMark })
.on('error', done)
.pipe(through(implode(compressionType, dictionarySize, { debug: true })).on('error', done))
.pipe(through(implode(compressionType, dictionarySize, { verbose: true })).on('error', done))
.pipe(
streamToBuffer(buffer => {
buffersShouldEqual(buffer, expected, 0, true)
Expand Down Expand Up @@ -143,7 +146,7 @@ const defineTestForFilesWithOffset = (highWaterMark) => {
transformSplitBy(
splitAt(offset),
transformIdentity(),
implode(compressionType, dictionarySize, { debug: true })
implode(compressionType, dictionarySize, { verbose: true })
)
).on('error', done)
)
Expand Down
2 changes: 1 addition & 1 deletion types/explode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function generateAsciiTables(): {
asciiTable2E34: number[]
asciiTable2EB4: number[]
}
export function processChunkData(state: PrivateExplodeState, debug?: boolean): void
export function processChunkData(state: PrivateExplodeState, verbose?: boolean): void
export function wasteBits(state: PrivateExplodeState, numberOfBits: number): typeof PKDCL_STREAM_END | typeof PKDCL_OK
export function decodeNextLiteral(state: PrivateExplodeState): typeof LITERAL_STREAM_ABORTED | number
export function decodeDistance(state: PrivateExplodeState, repeatLength: number): number
Expand Down
4 changes: 2 additions & 2 deletions types/helpers/Shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export type PrivateState<T> = { _state: T }
*/
export type Config = {
/**
* Whether the code should display debug messages on the console or not
* Whether the code should display extra messages on the console or not
* @default false
*/
debug?: boolean
verbose?: boolean
/**
* The starting size of the input buffer, may expand later as needed.
* Not having to expand may have performance impact.
Expand Down
8 changes: 4 additions & 4 deletions types/implode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ export function getSizeOfMatching(inputBytes: number[], a: number, b: number): n
export function findRepetitions(
inputBytes: number[],
endOfLastMatch: number,
cursor: number
cursor: number,
): { size: number; distance: number }
export function isRepetitionFlushable(
size: number,
distance: number,
startIndex: number,
inputBufferSize: number
inputBufferSize: number,
): boolean | null
export function processChunkData(state: PrivateExplodeState, debug?: boolean): void
export function processChunkData(state: PrivateExplodeState, verbose?: boolean): void

/**
* Compresses stream
Expand All @@ -59,5 +59,5 @@ export function processChunkData(state: PrivateExplodeState, debug?: boolean): v
export function implode(
compressionType: CompressionType,
dictionarySizeBits: DictionarySizeBits,
config?: Config
config?: Config,
): PrivateState<PrivateExplodeState> & Handler

0 comments on commit 6de86a6

Please sign in to comment.