simple-zstd 2.0.0-0 → 2.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/.github/workflows/ci.yml +46 -0
- package/.github/workflows/release.yml +45 -0
- package/.prettierignore +5 -0
- package/.prettierrc +8 -0
- package/.release-it.json +28 -0
- package/CHANGELOG.md +26 -0
- package/README.md +657 -110
- package/dist/src/buffer-writable.d.ts +12 -0
- package/dist/src/buffer-writable.js +41 -0
- package/dist/src/index.d.ts +49 -0
- package/dist/src/index.js +430 -0
- package/dist/src/peek-transform.d.ts +16 -0
- package/dist/src/peek-transform.js +145 -0
- package/dist/src/process-duplex.d.ts +11 -0
- package/dist/src/process-duplex.js +157 -0
- package/dist/src/process-queue.d.ts +8 -0
- package/dist/src/process-queue.js +94 -0
- package/dist/src/types.d.ts +34 -0
- package/dist/src/types.js +3 -0
- package/eslint.config.js +49 -0
- package/package.json +29 -21
- package/src/buffer-writable.ts +30 -0
- package/src/index.ts +472 -0
- package/src/is-zst.d.ts +5 -0
- package/src/peek-transform.ts +153 -0
- package/src/process-duplex.ts +164 -0
- package/src/process-queue.ts +97 -0
- package/src/types.ts +35 -0
- package/tsconfig.json +110 -0
- package/.eslintrc.js +0 -19
- package/.nyc_output/88144029-aa5a-40fb-a072-f586e59e4a38.json +0 -1
- package/.nyc_output/cb2a47ed-9ada-42b3-863b-b8437ca49715.json +0 -1
- package/.nyc_output/processinfo/88144029-aa5a-40fb-a072-f586e59e4a38.json +0 -1
- package/.nyc_output/processinfo/cb2a47ed-9ada-42b3-863b-b8437ca49715.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.travis.yml +0 -9
- package/buffer-writable.js +0 -23
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/buffer-writable.js.html +0 -154
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -146
- package/coverage/index.js.html +0 -910
- package/coverage/oven.js.html +0 -244
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -196
- package/index.js +0 -295
- package/process-queue.js +0 -74
package/index.js
DELETED
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
const fs = require('node:fs');
|
|
2
|
-
const { writeFile } = require('node:fs/promises');
|
|
3
|
-
const { Readable, pipeline } = require('node:stream');
|
|
4
|
-
const util = require('node:util');
|
|
5
|
-
|
|
6
|
-
const ProcessStream = require('process-streams');
|
|
7
|
-
const { execSync } = require('node:child_process');
|
|
8
|
-
const isZst = require('is-zst');
|
|
9
|
-
const peek = require('peek-stream');
|
|
10
|
-
const through = require('through2');
|
|
11
|
-
const { file } = require('tmp-promise');
|
|
12
|
-
const debug = require('debug')('SimpleZSTD');
|
|
13
|
-
|
|
14
|
-
const ProcessQueue = require('./process-queue');
|
|
15
|
-
const BufferWritable = require('./buffer-writable');
|
|
16
|
-
|
|
17
|
-
const pipelineAsync = util.promisify(pipeline);
|
|
18
|
-
|
|
19
|
-
const find = (process.platform === 'win32') ? 'where zstd.exe' : 'which zstd';
|
|
20
|
-
|
|
21
|
-
let bin;
|
|
22
|
-
|
|
23
|
-
try {
|
|
24
|
-
bin = execSync(find, { env: process.env }).toString().replace(/\n$/, '').replace(/\r$/, '');
|
|
25
|
-
debug(bin);
|
|
26
|
-
} catch (err) {
|
|
27
|
-
throw new Error('Can not access zstd! Is it installed?');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
fs.accessSync(bin, fs.constants.X_OK);
|
|
32
|
-
} catch (err) {
|
|
33
|
-
throw new Error('zstd is not executable');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async function CreateCompressStream(compLevel, spawnOptions, streamOptions, zstdOptions, dictionary) {
|
|
37
|
-
const ps = new ProcessStream();
|
|
38
|
-
|
|
39
|
-
let lvl = compLevel;
|
|
40
|
-
let opts = zstdOptions || [];
|
|
41
|
-
let path = null;
|
|
42
|
-
let cleanup = null;
|
|
43
|
-
|
|
44
|
-
if (!lvl) lvl = 3;
|
|
45
|
-
if (lvl < 1 || lvl > 22) lvl = 3;
|
|
46
|
-
|
|
47
|
-
// Dictionary
|
|
48
|
-
if (dictionary && dictionary.path) {
|
|
49
|
-
opts = [...opts, '-D', `${dictionary.path}`]; //eslint-disable-line
|
|
50
|
-
} else if (Buffer.isBuffer(dictionary)) {
|
|
51
|
-
({ path, cleanup } = await file());
|
|
52
|
-
await writeFile(path, dictionary);
|
|
53
|
-
opts = [...opts, '-D', `${path}`]; //eslint-disable-line
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let c;
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
debug(bin, ['-c', `-l${lvl}`, ...opts], spawnOptions, streamOptions);
|
|
60
|
-
c = ps.spawn(bin, [`-${lvl}`, ...opts], spawnOptions, streamOptions);
|
|
61
|
-
} catch (err) {
|
|
62
|
-
// cleanup if error;
|
|
63
|
-
if (cleanup) cleanup();
|
|
64
|
-
throw err;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
c.on('exit', (code, signal) => {
|
|
68
|
-
debug('c exit', code, signal);
|
|
69
|
-
if (code !== 0) {
|
|
70
|
-
setTimeout(() => {
|
|
71
|
-
c.destroy(new Error(`zstd exited non zero. code: ${code} signal: ${signal}`));
|
|
72
|
-
}, 1);
|
|
73
|
-
}
|
|
74
|
-
if (cleanup) cleanup();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
return c;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function CompressBuffer(buffer, c) {
|
|
81
|
-
return new Promise((resolve, reject) => {
|
|
82
|
-
const w = new BufferWritable();
|
|
83
|
-
|
|
84
|
-
pipelineAsync(
|
|
85
|
-
Readable.from(buffer),
|
|
86
|
-
c,
|
|
87
|
-
w,
|
|
88
|
-
)
|
|
89
|
-
.then(() => resolve(w.buffer))
|
|
90
|
-
.catch((err) => reject(err));
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function CreateDecompressStream(spawnOptions, streamOptions, zstdOptions, dictionary) {
|
|
95
|
-
// Dictionary
|
|
96
|
-
const ps = new ProcessStream();
|
|
97
|
-
|
|
98
|
-
let opts = zstdOptions || [];
|
|
99
|
-
let path = null;
|
|
100
|
-
let cleanup = null;
|
|
101
|
-
|
|
102
|
-
let terminate;
|
|
103
|
-
|
|
104
|
-
if (dictionary && dictionary.path) {
|
|
105
|
-
opts = [...opts, '-D', `${dictionary.path}`]; //eslint-disable-line
|
|
106
|
-
} else if (Buffer.isBuffer(dictionary)) {
|
|
107
|
-
({ path, cleanup } = await file());
|
|
108
|
-
await writeFile(path, dictionary);
|
|
109
|
-
opts = [...opts, '-D', `${path}`]; //eslint-disable-line
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
let d;
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
debug(bin, ['-d', ...opts], spawnOptions, streamOptions);
|
|
116
|
-
d = ps.spawn(bin, ['-d', ...opts], spawnOptions, streamOptions);
|
|
117
|
-
} catch (err) {
|
|
118
|
-
// cleanup if error
|
|
119
|
-
if (cleanup) cleanup();
|
|
120
|
-
throw err;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
d.on('exit', (code, signal) => {
|
|
124
|
-
debug('d exit', code, signal);
|
|
125
|
-
if (code !== 0 && !terminate) {
|
|
126
|
-
setTimeout(() => {
|
|
127
|
-
d.destroy(new Error(`zstd exited non zero. code: ${code} signal: ${signal}`));
|
|
128
|
-
}, 1);
|
|
129
|
-
}
|
|
130
|
-
if (cleanup) cleanup();
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
return peek({ newline: false, maxBuffer: 10 }, (data, swap) => {
|
|
134
|
-
if (isZst(data)) return swap(null, d);
|
|
135
|
-
debug('not zstd');
|
|
136
|
-
terminate = true;
|
|
137
|
-
d.end();
|
|
138
|
-
return swap(null, through());
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function DecompressBuffer(buffer, d) {
|
|
143
|
-
return new Promise((resolve, reject) => {
|
|
144
|
-
const w = new BufferWritable();
|
|
145
|
-
|
|
146
|
-
pipelineAsync(
|
|
147
|
-
Readable.from(buffer),
|
|
148
|
-
d,
|
|
149
|
-
w,
|
|
150
|
-
)
|
|
151
|
-
.then(() => resolve(w.buffer))
|
|
152
|
-
.catch((err) => reject(err));
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Standalone Functions
|
|
157
|
-
|
|
158
|
-
function compress(compLevel, spawnOptions, streamOptions, zstdOptions, dictionary) {
|
|
159
|
-
// Returns a promise
|
|
160
|
-
return CreateCompressStream(compLevel, spawnOptions, streamOptions, zstdOptions, dictionary);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async function compressBuffer(buffer, compLevel, spawnOptions, streamOptions, zstdOptions, dictionary) {
|
|
164
|
-
const c = await CreateCompressStream(compLevel, spawnOptions, streamOptions, zstdOptions, dictionary);
|
|
165
|
-
return CompressBuffer(buffer, c);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function decompress(spawnOptions, streamOptions, zstdOptions, dictionary) {
|
|
169
|
-
// Returns a promise
|
|
170
|
-
return CreateDecompressStream(spawnOptions, streamOptions, zstdOptions, dictionary);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async function decompressBuffer(buffer, spawnOptions, streamOptions, zstdOptions, dictionary) {
|
|
174
|
-
const d = await CreateDecompressStream(spawnOptions, streamOptions, zstdOptions, dictionary);
|
|
175
|
-
return DecompressBuffer(buffer, d);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// SimpleZSTD Class
|
|
179
|
-
class SimpleZSTD {
|
|
180
|
-
#compressQueue;
|
|
181
|
-
|
|
182
|
-
#decompressQueue;
|
|
183
|
-
|
|
184
|
-
#bufferFileCleanup;
|
|
185
|
-
|
|
186
|
-
#ready;
|
|
187
|
-
|
|
188
|
-
constructor(poolOptions, dictionary) {
|
|
189
|
-
// Use a guard in all function to complete the async dictionary loading
|
|
190
|
-
debug('constructor', poolOptions, dictionary);
|
|
191
|
-
const poolOpts = poolOptions || {};
|
|
192
|
-
poolOpts.compressQueue = poolOpts.compressQueue || {};
|
|
193
|
-
poolOpts.decompressQueue = poolOpts.decompressQueue || {};
|
|
194
|
-
|
|
195
|
-
this.#ready = new Promise(async (resolve, reject) => { // eslint-disable-line
|
|
196
|
-
let path = null;
|
|
197
|
-
let cleanup = null;
|
|
198
|
-
|
|
199
|
-
try {
|
|
200
|
-
// Convert buffer or dictionary.path to path
|
|
201
|
-
if (dictionary && dictionary.path) {
|
|
202
|
-
path = dictionary.path;
|
|
203
|
-
}
|
|
204
|
-
if (dictionary && Buffer.isBuffer(dictionary)) {
|
|
205
|
-
({ path, cleanup } = await file());
|
|
206
|
-
this.#bufferFileCleanup = cleanup;
|
|
207
|
-
await writeFile(path, dictionary);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
this.#compressQueue = new ProcessQueue(
|
|
211
|
-
poolOpts.compressQueue,
|
|
212
|
-
(() => {
|
|
213
|
-
debug('compress factory');
|
|
214
|
-
return CreateCompressStream(poolOpts.compressQueue.compLevel, poolOpts.compressQueue.spawnOptions, poolOpts.compressQueue.streamOptions, poolOpts.compressQueue.zstdOptions, { path });
|
|
215
|
-
}),
|
|
216
|
-
async (p) => {
|
|
217
|
-
debug('compress cleanup');
|
|
218
|
-
(await p).end();
|
|
219
|
-
},
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
this.#decompressQueue = new ProcessQueue(
|
|
223
|
-
poolOpts.decompressQueue,
|
|
224
|
-
(() => {
|
|
225
|
-
debug('decompress factory');
|
|
226
|
-
return CreateDecompressStream(poolOpts.decompressQueue.spawnOptions, poolOpts.decompressQueue.streamOptions, poolOpts.decompressQueue.zstdOptions, { path });
|
|
227
|
-
}),
|
|
228
|
-
async (p) => {
|
|
229
|
-
debug('decompress cleanup');
|
|
230
|
-
(await p).end('1234567890000');
|
|
231
|
-
},
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
debug('READY');
|
|
235
|
-
resolve();
|
|
236
|
-
} catch (err) {
|
|
237
|
-
reject(err);
|
|
238
|
-
}
|
|
239
|
-
}).catch((err) => {
|
|
240
|
-
debug('ready error', err);
|
|
241
|
-
if (this.#bufferFileCleanup) this.#bufferFileCleanup();
|
|
242
|
-
this.#bufferFileCleanup = null;
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
get queueStats() {
|
|
247
|
-
return {
|
|
248
|
-
compresss: {
|
|
249
|
-
hits: this.#compressQueue.hits,
|
|
250
|
-
misses: this.#compressQueue.misses,
|
|
251
|
-
},
|
|
252
|
-
decompresss: {
|
|
253
|
-
hits: this.#decompressQueue.hits,
|
|
254
|
-
misses: this.#decompressQueue.misses,
|
|
255
|
-
},
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
destroy() {
|
|
260
|
-
this.#compressQueue.destroy();
|
|
261
|
-
this.#decompressQueue.destroy();
|
|
262
|
-
if (this.#bufferFileCleanup) this.#bufferFileCleanup();
|
|
263
|
-
this.#bufferFileCleanup = null;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async compress() {
|
|
267
|
-
await this.#ready;
|
|
268
|
-
return this.#compressQueue.acquire();
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
async compressBuffer(buffer) {
|
|
272
|
-
await this.#ready;
|
|
273
|
-
const c = await this.#compressQueue.acquire();
|
|
274
|
-
return CompressBuffer(buffer, c);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async decompress() {
|
|
278
|
-
await this.#ready;
|
|
279
|
-
return this.#decompressQueue.acquire();
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
async decompressBuffer(buffer) {
|
|
283
|
-
await this.#ready;
|
|
284
|
-
const d = await this.#decompressQueue.acquire();
|
|
285
|
-
return DecompressBuffer(buffer, d);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
module.exports = {
|
|
290
|
-
SimpleZSTD,
|
|
291
|
-
compress,
|
|
292
|
-
compressBuffer,
|
|
293
|
-
decompress,
|
|
294
|
-
decompressBuffer,
|
|
295
|
-
};
|
package/process-queue.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
const debug = require('debug')('SimpleZSTDQueue');
|
|
2
|
-
|
|
3
|
-
class ProcessQueue {
|
|
4
|
-
#poolOptions;
|
|
5
|
-
|
|
6
|
-
#queue;
|
|
7
|
-
|
|
8
|
-
#factory;
|
|
9
|
-
|
|
10
|
-
#destroy;
|
|
11
|
-
|
|
12
|
-
#isDestroyed;
|
|
13
|
-
|
|
14
|
-
#hitCount;
|
|
15
|
-
|
|
16
|
-
#missCount;
|
|
17
|
-
|
|
18
|
-
constructor(poolOptions, factory, destroy) {
|
|
19
|
-
debug('constructor', poolOptions);
|
|
20
|
-
this.#poolOptions = poolOptions || {};
|
|
21
|
-
this.#queue = [];
|
|
22
|
-
this.#factory = factory;
|
|
23
|
-
this.#destroy = destroy;
|
|
24
|
-
this.#isDestroyed = false;
|
|
25
|
-
|
|
26
|
-
this.#hitCount = 0;
|
|
27
|
-
this.#missCount = 0;
|
|
28
|
-
|
|
29
|
-
for (let i = 0; i < this.#poolOptions.targetSize || 0; i += 1) {
|
|
30
|
-
this.#createResource();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get hits() {
|
|
35
|
-
return this.#hitCount;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get misses() {
|
|
39
|
-
return this.#missCount;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async #createResource() {
|
|
43
|
-
debug('createResource?', this.#queue.length);
|
|
44
|
-
if (this.#queue.length < this.#poolOptions.targetSize) {
|
|
45
|
-
debug('createResource call factory');
|
|
46
|
-
this.#queue.push(this.#factory());
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async acquire() {
|
|
51
|
-
debug('acquire');
|
|
52
|
-
if (this.#queue.length > 0) {
|
|
53
|
-
setImmediate(() => {
|
|
54
|
-
this.#createResource();
|
|
55
|
-
});
|
|
56
|
-
debug('acquire from queue');
|
|
57
|
-
this.#hitCount += 1;
|
|
58
|
-
return this.#queue.pop();
|
|
59
|
-
}
|
|
60
|
-
debug('acquire create on demand');
|
|
61
|
-
this.#missCount += 1;
|
|
62
|
-
return this.#factory();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async destroy() {
|
|
66
|
-
debug('destroy', this.#queue.length);
|
|
67
|
-
this.#isDestroyed = true;
|
|
68
|
-
while (this.#queue.length > 0) {
|
|
69
|
-
this.#destroy(this.#queue.pop());
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
module.exports = ProcessQueue;
|