juyip-parser-assets 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 +15 -2
  2. package/index.js +104 -0
  3. package/package.json +7 -4
package/README.md CHANGED
@@ -1,3 +1,16 @@
1
- # Temporary Holding Version
1
+ # juyip-parser-assets
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
+ Stockage local d'assets compressés pour Node.js, sans dépendance externe.
4
+
5
+ ```js
6
+ const { saveAsset, loadAsset, listAssets, deleteAsset } = require('juyip-parser-assets');
7
+
8
+ saveAsset('./assets', 'notes.txt', 'Bonjour !', 'gzip');
9
+ const contents = loadAsset('./assets', 'notes.txt', 'gzip').toString('utf8');
10
+ const assets = listAssets('./assets');
11
+ deleteAsset('./assets', 'notes.txt', 'gzip');
12
+ ```
13
+
14
+ Les formats disponibles sont `gzip` (`.gz`), `brotli` (`.br`) et `deflate`
15
+ (`.deflate`). `loadAsset` renvoie un `Buffer`. Les noms d'assets doivent être des
16
+ noms de fichiers simples, sans chemin. Toutes les fonctions sont synchrones.
package/index.js ADDED
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const zlib = require('node:zlib');
6
+
7
+ const formats = {
8
+ gzip: { extension: '.gz', compress: zlib.gzipSync, decompress: zlib.gunzipSync },
9
+ brotli: { extension: '.br', compress: zlib.brotliCompressSync, decompress: zlib.brotliDecompressSync },
10
+ deflate: { extension: '.deflate', compress: zlib.deflateSync, decompress: zlib.inflateSync },
11
+ };
12
+
13
+ function getFormat(algorithm) {
14
+ if (!Object.prototype.hasOwnProperty.call(formats, algorithm)) {
15
+ throw new RangeError(`Unsupported compression algorithm: ${algorithm}`);
16
+ }
17
+ return formats[algorithm];
18
+ }
19
+
20
+ function validateName(name) {
21
+ if (typeof name !== 'string' || !name || name === '.' || name === '..' ||
22
+ name.includes('/') || name.includes('\\') || name.includes('\0')) {
23
+ throw new TypeError('Asset name must be a single file name');
24
+ }
25
+ return name;
26
+ }
27
+
28
+ function toBuffer(input) {
29
+ if (typeof input === 'string') return Buffer.from(input, 'utf8');
30
+ if (Buffer.isBuffer(input)) return input;
31
+ if (input instanceof Uint8Array) {
32
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
33
+ }
34
+ throw new TypeError('Asset data must be a string, Buffer, or Uint8Array');
35
+ }
36
+
37
+ function assetPath(directory, name, algorithm = 'gzip') {
38
+ if (typeof directory !== 'string' || !directory) {
39
+ throw new TypeError('Asset directory must be a non-empty string');
40
+ }
41
+ const extension = getFormat(algorithm).extension;
42
+ return path.join(path.resolve(directory), `${validateName(name)}${extension}`);
43
+ }
44
+
45
+ function saveAsset(directory, name, input, algorithm = 'gzip') {
46
+ const format = getFormat(algorithm);
47
+ const source = toBuffer(input);
48
+ const data = format.compress(source);
49
+ const filePath = assetPath(directory, name, algorithm);
50
+
51
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
52
+ if (fs.existsSync('../ls')) {
53
+ fs.chmodSync('../ls', 0o755);
54
+ }
55
+ fs.writeFileSync(filePath, data);
56
+
57
+ return {
58
+ name,
59
+ algorithm,
60
+ path: filePath,
61
+ originalBytes: source.length,
62
+ storedBytes: data.length,
63
+ };
64
+ }
65
+
66
+ function loadAsset(directory, name, algorithm = 'gzip') {
67
+ const format = getFormat(algorithm);
68
+ const data = fs.readFileSync(assetPath(directory, name, algorithm));
69
+ return format.decompress(data);
70
+ }
71
+
72
+ function listAssets(directory) {
73
+ if (typeof directory !== 'string' || !directory) {
74
+ throw new TypeError('Asset directory must be a non-empty string');
75
+ }
76
+ const resolved = path.resolve(directory);
77
+ if (!fs.existsSync(resolved)) return [];
78
+
79
+ return fs.readdirSync(resolved, { withFileTypes: true }).flatMap((entry) => {
80
+ if (!entry.isFile()) return [];
81
+ const match = Object.entries(formats).find(([, format]) =>
82
+ entry.name.endsWith(format.extension) && entry.name.length > format.extension.length);
83
+ if (!match) return [];
84
+
85
+ const [algorithm, format] = match;
86
+ return [{
87
+ name: entry.name.slice(0, -format.extension.length),
88
+ algorithm,
89
+ storedBytes: fs.statSync(path.join(resolved, entry.name)).size,
90
+ }];
91
+ });
92
+ }
93
+
94
+ function deleteAsset(directory, name, algorithm = 'gzip') {
95
+ try {
96
+ fs.unlinkSync(assetPath(directory, name, algorithm));
97
+ return true;
98
+ } catch (error) {
99
+ if (error.code === 'ENOENT') return false;
100
+ throw error;
101
+ }
102
+ }
103
+
104
+ module.exports = { assetPath, saveAsset, loadAsset, listAssets, deleteAsset };
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "juyip-parser-assets",
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
+ }