node-pkware 1.0.2 → 3.0.0
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/README.md +50 -48
- package/dist/ExpandingBuffer.d.ts +53 -0
- package/dist/ExpandingBuffer.js +134 -0
- package/dist/ExpandingBuffer.js.map +1 -0
- package/dist/Explode.d.ts +8 -0
- package/dist/Explode.js +309 -0
- package/dist/Explode.js.map +1 -0
- package/dist/Implode.d.ts +11 -0
- package/dist/Implode.js +305 -0
- package/dist/Implode.js.map +1 -0
- package/dist/bin/explode.d.ts +2 -0
- package/dist/bin/explode.js +59 -0
- package/dist/bin/explode.js.map +1 -0
- package/dist/bin/helpers.d.ts +8 -0
- package/dist/bin/helpers.js +65 -0
- package/dist/bin/helpers.js.map +1 -0
- package/dist/bin/implode.d.ts +2 -0
- package/dist/bin/implode.js +79 -0
- package/dist/bin/implode.js.map +1 -0
- package/dist/constants.d.ts +32 -0
- package/dist/constants.js +114 -0
- package/dist/constants.js.map +1 -0
- package/{types → dist}/errors.d.ts +13 -11
- package/dist/errors.js +52 -0
- package/dist/errors.js.map +1 -0
- package/dist/functions.d.ts +11 -0
- package/dist/functions.js +73 -0
- package/dist/functions.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/{types/helpers → dist}/stream.d.ts +13 -34
- package/dist/stream.js +205 -0
- package/dist/stream.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -45
- package/src/ExpandingBuffer.ts +148 -0
- package/src/Explode.ts +404 -0
- package/src/Implode.ts +368 -0
- package/src/bin/explode.ts +80 -0
- package/src/bin/helpers.ts +65 -0
- package/src/bin/implode.ts +116 -0
- package/src/{constants.js → constants.ts} +31 -50
- package/src/errors.ts +47 -0
- package/src/functions.ts +73 -0
- package/src/index.ts +30 -0
- package/src/stream.ts +220 -0
- package/src/types.ts +26 -0
- package/bin/explode.js +0 -78
- package/bin/implode.js +0 -116
- package/src/errors.js +0 -50
- package/src/explode.js +0 -411
- package/src/helpers/ExpandingBuffer.js +0 -123
- package/src/helpers/functions.js +0 -150
- package/src/helpers/stream.js +0 -190
- package/src/helpers/testing.js +0 -80
- package/src/implode.js +0 -364
- package/src/index.js +0 -18
- package/tsconfig.json +0 -20
- package/types/constants.d.ts +0 -41
- package/types/explode.d.ts +0 -56
- package/types/helpers/ExpandingBuffer.d.ts +0 -25
- package/types/helpers/Shared.d.ts +0 -46
- package/types/helpers/functions.d.ts +0 -15
- package/types/helpers/testing.d.ts +0 -6
- package/types/implode.d.ts +0 -63
- package/types/index.d.ts +0 -8
package/src/helpers/functions.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const { repeat, test, type } = require('ramda')
|
|
3
|
-
|
|
4
|
-
const isNumber = (x) => {
|
|
5
|
-
return typeof x === 'number'
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const isString = (x) => {
|
|
9
|
-
return typeof x === 'string'
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const isFunction = (x) => {
|
|
13
|
-
return type(x) === 'Function'
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const noop = () => {}
|
|
17
|
-
|
|
18
|
-
// https://stackoverflow.com/a/68989785/1806628
|
|
19
|
-
const isPlainObject = (x) => {
|
|
20
|
-
return x.constructor === Object
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const isBetween = (min, max, num) => {
|
|
24
|
-
if (!isNumber(min) || !isNumber(max) || !isNumber(num)) {
|
|
25
|
-
return null
|
|
26
|
-
}
|
|
27
|
-
if (min > max) {
|
|
28
|
-
;[min, max] = [max, min]
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return num >= min && num <= max
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const nBitsOfOnes = (numberOfBits) => {
|
|
35
|
-
if (!Number.isInteger(numberOfBits) || numberOfBits < 0) {
|
|
36
|
-
return null
|
|
37
|
-
}
|
|
38
|
-
return (1 << numberOfBits) - 1
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const maskBits = (numberOfBits, number) => {
|
|
42
|
-
const bits = nBitsOfOnes(numberOfBits)
|
|
43
|
-
if (bits === null) {
|
|
44
|
-
return null
|
|
45
|
-
}
|
|
46
|
-
if (!Number.isInteger(number) || number < 0) {
|
|
47
|
-
return null
|
|
48
|
-
}
|
|
49
|
-
return number & nBitsOfOnes(numberOfBits)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const getLowestNBits = (numberOfBits, number) => {
|
|
53
|
-
return number & nBitsOfOnes(numberOfBits)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const isDecimalString = test(/^\d+$/)
|
|
57
|
-
|
|
58
|
-
const isFullHexString = (str) => {
|
|
59
|
-
if (isString(str)) {
|
|
60
|
-
return /^\s*0x[0-9a-f]+\s*$/.test(str)
|
|
61
|
-
} else {
|
|
62
|
-
return false
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const toHex = (num, digits = 0, withoutPrefix = false) => {
|
|
67
|
-
const prefix = withoutPrefix ? '' : '0x'
|
|
68
|
-
if (!Number.isInteger(digits) || digits < 0) {
|
|
69
|
-
return null
|
|
70
|
-
}
|
|
71
|
-
if (isFullHexString(num)) {
|
|
72
|
-
const number = num.trim().replace(/^0x0*/, '')
|
|
73
|
-
return `${prefix}${number.padStart(digits, '0')}`
|
|
74
|
-
}
|
|
75
|
-
if (!Number.isInteger(num)) {
|
|
76
|
-
return null
|
|
77
|
-
}
|
|
78
|
-
return `${prefix}${num.toString(16).padStart(digits, '0')}`
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const mergeSparseArrays = (a, b) => {
|
|
82
|
-
if (!Array.isArray(a) || !Array.isArray(b)) {
|
|
83
|
-
return []
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const result = [...b, ...(b.length < a.length ? repeat(undefined, a.length - b.length) : [])]
|
|
87
|
-
for (let i = 0; i < a.length; i++) {
|
|
88
|
-
if (a[i] !== undefined) {
|
|
89
|
-
result[i] = a[i]
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return result
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/*
|
|
96
|
-
export const dumpBytes = bytes => {
|
|
97
|
-
const formattedBytes = Array.from(bytes)
|
|
98
|
-
.map(byte => {
|
|
99
|
-
return toHex(byte, 2, true)
|
|
100
|
-
})
|
|
101
|
-
.join(' ')
|
|
102
|
-
return `<${formattedBytes}>`
|
|
103
|
-
}
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
const parseNumberString = (n, defaultValue = 0) => {
|
|
107
|
-
if (isDecimalString(n)) {
|
|
108
|
-
return parseInt(n)
|
|
109
|
-
} else if (isFullHexString(n)) {
|
|
110
|
-
return parseInt(n.replace(/^0x/, ''), 16)
|
|
111
|
-
} else {
|
|
112
|
-
return defaultValue
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const getPackageVersion = async () => {
|
|
117
|
-
try {
|
|
118
|
-
const { version } = require('../../package.json')
|
|
119
|
-
return version
|
|
120
|
-
} catch (error) {
|
|
121
|
-
return 'unknown'
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const fileExists = async (filename) => {
|
|
126
|
-
try {
|
|
127
|
-
await fs.promises.access(filename, fs.constants.R_OK)
|
|
128
|
-
return true
|
|
129
|
-
} catch (error) {
|
|
130
|
-
return false
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
module.exports = {
|
|
135
|
-
isNumber,
|
|
136
|
-
isString,
|
|
137
|
-
isFunction,
|
|
138
|
-
noop,
|
|
139
|
-
isPlainObject,
|
|
140
|
-
isBetween,
|
|
141
|
-
nBitsOfOnes,
|
|
142
|
-
maskBits,
|
|
143
|
-
getLowestNBits,
|
|
144
|
-
isFullHexString,
|
|
145
|
-
toHex,
|
|
146
|
-
mergeSparseArrays,
|
|
147
|
-
parseNumberString,
|
|
148
|
-
getPackageVersion,
|
|
149
|
-
fileExists,
|
|
150
|
-
}
|
package/src/helpers/stream.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
const { Transform, Writable } = require('stream')
|
|
2
|
-
const { promisify } = require('util')
|
|
3
|
-
const ExpandingBuffer = require('./ExpandingBuffer.js')
|
|
4
|
-
const { isFunction } = require('./functions.js')
|
|
5
|
-
|
|
6
|
-
const emptyBuffer = Buffer.from([])
|
|
7
|
-
|
|
8
|
-
class QuasiTransform {
|
|
9
|
-
constructor(handler) {
|
|
10
|
-
this.handler = handler
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
handle(chunk, encoding) {
|
|
14
|
-
return promisify(this.handler).call(this, chunk, encoding)
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const splitAt = (index) => {
|
|
19
|
-
let cntr = 0
|
|
20
|
-
|
|
21
|
-
if (!Number.isInteger(index) || index < 0) {
|
|
22
|
-
return () => {
|
|
23
|
-
return null
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return (chunk) => {
|
|
28
|
-
let left
|
|
29
|
-
let right
|
|
30
|
-
let isLeftDone = true
|
|
31
|
-
|
|
32
|
-
if (!Buffer.isBuffer(chunk)) {
|
|
33
|
-
return null
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (index <= cntr) {
|
|
37
|
-
// index ..... cntr ..... chunk.length
|
|
38
|
-
left = emptyBuffer
|
|
39
|
-
right = chunk
|
|
40
|
-
} else if (index >= cntr + chunk.length) {
|
|
41
|
-
// cntr ..... chunk.length ..... index
|
|
42
|
-
left = chunk
|
|
43
|
-
right = emptyBuffer
|
|
44
|
-
isLeftDone = index === cntr + chunk.length
|
|
45
|
-
} else {
|
|
46
|
-
// cntr ..... index ..... chunk.length
|
|
47
|
-
left = chunk.slice(0, index - cntr)
|
|
48
|
-
right = chunk.slice(index - cntr)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
cntr += chunk.length
|
|
52
|
-
|
|
53
|
-
return [left, right, isLeftDone]
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const transformIdentity = () => {
|
|
58
|
-
return function (chunk, encoding, callback) {
|
|
59
|
-
callback(null, chunk)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const transformEmpty = () => {
|
|
64
|
-
return function (chunk, encoding, callback) {
|
|
65
|
-
callback(null, emptyBuffer)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const through = (handler) => {
|
|
70
|
-
return new Transform({
|
|
71
|
-
transform: handler,
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const transformSplitBy = (predicate, leftHandler, rightHandler) => {
|
|
76
|
-
let isFirstChunk = true
|
|
77
|
-
let wasLeftFlushCalled = false
|
|
78
|
-
const damChunkSize = 0x10000
|
|
79
|
-
const dam = new ExpandingBuffer()
|
|
80
|
-
|
|
81
|
-
const leftTransform = new QuasiTransform(leftHandler)
|
|
82
|
-
const rightTransform = new QuasiTransform(rightHandler)
|
|
83
|
-
|
|
84
|
-
return function (chunk, encoding, callback) {
|
|
85
|
-
const [left, right, isLeftDone] = predicate(chunk)
|
|
86
|
-
|
|
87
|
-
const _left = leftTransform.handle(left, encoding)
|
|
88
|
-
const _right = rightTransform.handle(right, encoding)
|
|
89
|
-
|
|
90
|
-
if (isFirstChunk) {
|
|
91
|
-
isFirstChunk = false
|
|
92
|
-
this._flush = (flushCallback) => {
|
|
93
|
-
if (!dam.isEmpty()) {
|
|
94
|
-
this.push(dam.read())
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
let leftFiller = Promise.resolve(emptyBuffer)
|
|
98
|
-
let rightFiller = Promise.resolve(emptyBuffer)
|
|
99
|
-
|
|
100
|
-
if (!wasLeftFlushCalled && isFunction(leftTransform._flush)) {
|
|
101
|
-
leftFiller = new Promise((resolve, reject) => {
|
|
102
|
-
leftTransform._flush((err, data) => {
|
|
103
|
-
if (err) {
|
|
104
|
-
reject(err)
|
|
105
|
-
} else {
|
|
106
|
-
resolve(data)
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (isFunction(rightTransform._flush)) {
|
|
113
|
-
rightFiller = new Promise((resolve, reject) => {
|
|
114
|
-
rightTransform._flush((err, data) => {
|
|
115
|
-
if (err) {
|
|
116
|
-
reject(err)
|
|
117
|
-
} else {
|
|
118
|
-
resolve(data)
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
})
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
Promise.all([leftFiller, rightFiller])
|
|
125
|
-
.then((buffers) => {
|
|
126
|
-
flushCallback(null, Buffer.concat(buffers))
|
|
127
|
-
})
|
|
128
|
-
.catch((err) => {
|
|
129
|
-
flushCallback(err)
|
|
130
|
-
})
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
let filler = Promise.resolve(emptyBuffer)
|
|
135
|
-
if (isLeftDone && !wasLeftFlushCalled && isFunction(leftTransform._flush)) {
|
|
136
|
-
wasLeftFlushCalled = true
|
|
137
|
-
filler = new Promise((resolve, reject) => {
|
|
138
|
-
leftTransform._flush((err, data) => {
|
|
139
|
-
if (err) {
|
|
140
|
-
reject(err)
|
|
141
|
-
} else {
|
|
142
|
-
resolve(data)
|
|
143
|
-
}
|
|
144
|
-
})
|
|
145
|
-
})
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
Promise.all([_left, filler, _right])
|
|
149
|
-
.then((buffers) => {
|
|
150
|
-
dam.append(Buffer.concat(buffers))
|
|
151
|
-
if (dam.size() > damChunkSize) {
|
|
152
|
-
const chunks = Math.floor(dam.size() / damChunkSize)
|
|
153
|
-
const data = Buffer.from(dam.read(0, chunks * damChunkSize))
|
|
154
|
-
dam.flushStart(chunks * damChunkSize)
|
|
155
|
-
for (let i = 0; i < chunks - 1; i++) {
|
|
156
|
-
this.push(data.slice(i * damChunkSize, i * damChunkSize + damChunkSize))
|
|
157
|
-
}
|
|
158
|
-
callback(null, data.slice((chunks - 1) * damChunkSize))
|
|
159
|
-
} else {
|
|
160
|
-
callback(null, emptyBuffer)
|
|
161
|
-
}
|
|
162
|
-
})
|
|
163
|
-
.catch((err) => {
|
|
164
|
-
callback(err)
|
|
165
|
-
})
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const streamToBuffer = (done) => {
|
|
170
|
-
const buffer = new ExpandingBuffer()
|
|
171
|
-
return new Writable({
|
|
172
|
-
write(chunk, encoding, callback) {
|
|
173
|
-
buffer.append(chunk)
|
|
174
|
-
callback()
|
|
175
|
-
},
|
|
176
|
-
final(callback) {
|
|
177
|
-
done(buffer.getHeap())
|
|
178
|
-
callback()
|
|
179
|
-
},
|
|
180
|
-
})
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
module.exports = {
|
|
184
|
-
splitAt,
|
|
185
|
-
transformIdentity,
|
|
186
|
-
transformEmpty,
|
|
187
|
-
through,
|
|
188
|
-
transformSplitBy,
|
|
189
|
-
streamToBuffer,
|
|
190
|
-
}
|
package/src/helpers/testing.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
// const { EOL } = require('os')
|
|
2
|
-
// const fs = require('fs')
|
|
3
|
-
const assert = require('assert')
|
|
4
|
-
const { compare, report } = require('binary-comparator')
|
|
5
|
-
|
|
6
|
-
/*
|
|
7
|
-
const isPromise = promise => {
|
|
8
|
-
return typeof promise === 'object' && promise.constructor.name === 'Promise'
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const toConsole = () => {
|
|
12
|
-
return (chunk, encoding, callback) => {
|
|
13
|
-
process.stdout.write(chunk)
|
|
14
|
-
process.stdout.write(Buffer.from(EOL))
|
|
15
|
-
callback(null, chunk)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const readToBuffer = (fileName, chunkSizeInBytes = 1024) => {
|
|
20
|
-
return new Promise((resolve, reject) => {
|
|
21
|
-
const chunks = []
|
|
22
|
-
fs.createReadStream(fileName, { highWaterMark: chunkSizeInBytes })
|
|
23
|
-
.on('error', reject)
|
|
24
|
-
.on('data', chunk => {
|
|
25
|
-
chunks.push(chunk)
|
|
26
|
-
})
|
|
27
|
-
.on('end', function () {
|
|
28
|
-
resolve(Buffer.concat(chunks))
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
// source: https://stackoverflow.com/a/43197340/1806628
|
|
35
|
-
const isClass = (obj) => {
|
|
36
|
-
const isCtorClass = obj.constructor && obj.constructor.toString().substring(0, 5) === 'class'
|
|
37
|
-
if (obj.prototype === undefined) {
|
|
38
|
-
return isCtorClass
|
|
39
|
-
}
|
|
40
|
-
const isPrototypeCtorClass =
|
|
41
|
-
obj.prototype.constructor &&
|
|
42
|
-
obj.prototype.constructor.toString &&
|
|
43
|
-
obj.prototype.constructor.toString().substring(0, 5) === 'class'
|
|
44
|
-
return isCtorClass || isPrototypeCtorClass
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// https://stackoverflow.com/a/48845122/1806628
|
|
48
|
-
const bufferToString = (buffer, limit = 20) => {
|
|
49
|
-
const ellipsisNecessary = buffer.length > limit
|
|
50
|
-
let hexString = buffer.slice(0, limit).toString('hex')
|
|
51
|
-
hexString = hexString.length > 2 ? hexString.match(/../g).join(' ') : hexString
|
|
52
|
-
return `<Buffer ${hexString}${ellipsisNecessary ? '...' : ''}>`
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const buffersShouldEqual = (expected, result, offset = 0, displayAsHex = false) => {
|
|
56
|
-
if (!Buffer.isBuffer(expected)) {
|
|
57
|
-
throw new Error('expected is not a Buffer')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!Buffer.isBuffer(result)) {
|
|
61
|
-
throw new Error('result is not a Buffer')
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const diff = report(expected, result, compare(expected, result, offset), displayAsHex)
|
|
65
|
-
assert.ok(expected.equals(result), diff)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const transformToABC = () => {
|
|
69
|
-
let cntr = 0
|
|
70
|
-
return function (chunk, encoding, callback) {
|
|
71
|
-
callback(null, Buffer.from([65 + (cntr++ % 26)]))
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
module.exports = {
|
|
76
|
-
isClass,
|
|
77
|
-
buffersShouldEqual,
|
|
78
|
-
bufferToString,
|
|
79
|
-
transformToABC,
|
|
80
|
-
}
|