@visulima/fs 3.0.0 → 3.1.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/dist/size.cjs ADDED
@@ -0,0 +1,118 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
4
+
5
+ const node_fs = require('node:fs');
6
+ const promises = require('node:fs/promises');
7
+ const node_path = require('node:path');
8
+ const node_stream = require('node:stream');
9
+ const node_url = require('node:url');
10
+ const node_zlib = require('node:zlib');
11
+ const utils = require('@visulima/path/utils');
12
+
13
+ var __defProp = Object.defineProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ const fileExists = /* @__PURE__ */ __name((input) => {
16
+ if (node_path.isAbsolute(input)) {
17
+ return node_fs.existsSync(input);
18
+ }
19
+ try {
20
+ return node_fs.existsSync(node_path.resolve(input));
21
+ } catch {
22
+ return false;
23
+ }
24
+ }, "fileExists");
25
+ const getStreamSizeEfficient = /* @__PURE__ */ __name(async (stream) => {
26
+ let totalSize = 0;
27
+ for await (const chunk of stream) {
28
+ totalSize += Buffer.from(chunk).length;
29
+ }
30
+ return totalSize;
31
+ }, "getStreamSizeEfficient");
32
+ const getCompressedStreamSizeEfficient = /* @__PURE__ */ __name(async (stream, createCompressor) => {
33
+ let totalSize = 0;
34
+ const compressor = createCompressor();
35
+ return await new Promise((resolve2, reject) => {
36
+ compressor.on("data", (chunk) => {
37
+ totalSize += chunk.length;
38
+ });
39
+ compressor.on("end", () => resolve2(totalSize));
40
+ compressor.on("error", reject);
41
+ stream.on("error", reject);
42
+ stream.pipe(compressor);
43
+ });
44
+ }, "getCompressedStreamSizeEfficient");
45
+ const processInputEfficiently = /* @__PURE__ */ __name(async (input, processor, streamProcessor) => {
46
+ if (input instanceof node_url.URL || typeof input === "string") {
47
+ const path = utils.toPath(input);
48
+ if (fileExists(path)) {
49
+ const fileStream = node_stream.Readable.from(await promises.readFile(path));
50
+ return await streamProcessor(fileStream);
51
+ }
52
+ if (typeof input === "string") {
53
+ return processor(Buffer.from(input));
54
+ }
55
+ }
56
+ if (input instanceof node_stream.Readable) {
57
+ return await streamProcessor(input);
58
+ }
59
+ return processor(input);
60
+ }, "processInputEfficiently");
61
+ const gzipSize = /* @__PURE__ */ __name(async (input, options) => {
62
+ const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => node_zlib.createGzip(options)), "streamProcessor");
63
+ const bufferProcessor = /* @__PURE__ */ __name((data) => node_zlib.gzipSync(data, options).length, "bufferProcessor");
64
+ return await processInputEfficiently(input, bufferProcessor, streamProcessor);
65
+ }, "gzipSize");
66
+ const brotliSize = /* @__PURE__ */ __name(async (input, options) => {
67
+ const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => node_zlib.createBrotliCompress(options)), "streamProcessor");
68
+ const bufferProcessor = /* @__PURE__ */ __name((data) => node_zlib.brotliCompressSync(data, options).length, "bufferProcessor");
69
+ return await processInputEfficiently(input, bufferProcessor, streamProcessor);
70
+ }, "brotliSize");
71
+ const rawSize = /* @__PURE__ */ __name(async (input) => {
72
+ const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getStreamSizeEfficient(stream), "streamProcessor");
73
+ const bufferProcessor = /* @__PURE__ */ __name((data) => data.length, "bufferProcessor");
74
+ return await processInputEfficiently(input, bufferProcessor, streamProcessor);
75
+ }, "rawSize");
76
+ const gzipSizeSync = /* @__PURE__ */ __name((input, options) => {
77
+ if (input instanceof node_url.URL || typeof input === "string") {
78
+ const path = utils.toPath(input);
79
+ if (fileExists(path)) {
80
+ return node_zlib.gzipSync(node_fs.readFileSync(path), options).length;
81
+ }
82
+ if (typeof input === "string") {
83
+ return node_zlib.gzipSync(Buffer.from(input), options).length;
84
+ }
85
+ }
86
+ return node_zlib.gzipSync(input, options).length;
87
+ }, "gzipSizeSync");
88
+ const brotliSizeSync = /* @__PURE__ */ __name((input, options) => {
89
+ if (input instanceof node_url.URL || typeof input === "string") {
90
+ const path = utils.toPath(input);
91
+ if (fileExists(path)) {
92
+ return node_zlib.brotliCompressSync(node_fs.readFileSync(path), options).length;
93
+ }
94
+ if (typeof input === "string") {
95
+ return node_zlib.brotliCompressSync(Buffer.from(input), options).length;
96
+ }
97
+ }
98
+ return node_zlib.brotliCompressSync(input, options).length;
99
+ }, "brotliSizeSync");
100
+ const rawSizeSync = /* @__PURE__ */ __name((input) => {
101
+ if (input instanceof node_url.URL || typeof input === "string") {
102
+ const path = utils.toPath(input);
103
+ if (fileExists(path)) {
104
+ return node_fs.statSync(path).size;
105
+ }
106
+ if (typeof input === "string") {
107
+ return Buffer.from(input).length;
108
+ }
109
+ }
110
+ return input.length;
111
+ }, "rawSizeSync");
112
+
113
+ exports.brotliSize = brotliSize;
114
+ exports.brotliSizeSync = brotliSizeSync;
115
+ exports.gzipSize = gzipSize;
116
+ exports.gzipSizeSync = gzipSizeSync;
117
+ exports.rawSize = rawSize;
118
+ exports.rawSizeSync = rawSizeSync;
@@ -0,0 +1,12 @@
1
+ import { Readable } from 'node:stream';
2
+ import { URL } from 'node:url';
3
+ import { ZlibOptions, BrotliOptions } from 'node:zlib';
4
+
5
+ declare const gzipSize: (input: Buffer | Readable | URL | string, options?: ZlibOptions) => Promise<number>;
6
+ declare const brotliSize: (input: Buffer | Readable | URL | string, options?: BrotliOptions) => Promise<number>;
7
+ declare const rawSize: (input: Buffer | Readable | URL | string) => Promise<number>;
8
+ declare const gzipSizeSync: (input: Buffer | URL | string, options?: ZlibOptions) => number;
9
+ declare const brotliSizeSync: (input: Buffer | URL | string, options?: BrotliOptions) => number;
10
+ declare const rawSizeSync: (input: Buffer | URL | string) => number;
11
+
12
+ export { brotliSize, brotliSizeSync, gzipSize, gzipSizeSync, rawSize, rawSizeSync };
@@ -0,0 +1,12 @@
1
+ import { Readable } from 'node:stream';
2
+ import { URL } from 'node:url';
3
+ import { ZlibOptions, BrotliOptions } from 'node:zlib';
4
+
5
+ declare const gzipSize: (input: Buffer | Readable | URL | string, options?: ZlibOptions) => Promise<number>;
6
+ declare const brotliSize: (input: Buffer | Readable | URL | string, options?: BrotliOptions) => Promise<number>;
7
+ declare const rawSize: (input: Buffer | Readable | URL | string) => Promise<number>;
8
+ declare const gzipSizeSync: (input: Buffer | URL | string, options?: ZlibOptions) => number;
9
+ declare const brotliSizeSync: (input: Buffer | URL | string, options?: BrotliOptions) => number;
10
+ declare const rawSizeSync: (input: Buffer | URL | string) => number;
11
+
12
+ export { brotliSize, brotliSizeSync, gzipSize, gzipSizeSync, rawSize, rawSizeSync };
package/dist/size.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Readable } from 'node:stream';
2
+ import { URL } from 'node:url';
3
+ import { ZlibOptions, BrotliOptions } from 'node:zlib';
4
+
5
+ declare const gzipSize: (input: Buffer | Readable | URL | string, options?: ZlibOptions) => Promise<number>;
6
+ declare const brotliSize: (input: Buffer | Readable | URL | string, options?: BrotliOptions) => Promise<number>;
7
+ declare const rawSize: (input: Buffer | Readable | URL | string) => Promise<number>;
8
+ declare const gzipSizeSync: (input: Buffer | URL | string, options?: ZlibOptions) => number;
9
+ declare const brotliSizeSync: (input: Buffer | URL | string, options?: BrotliOptions) => number;
10
+ declare const rawSizeSync: (input: Buffer | URL | string) => number;
11
+
12
+ export { brotliSize, brotliSizeSync, gzipSize, gzipSizeSync, rawSize, rawSizeSync };
package/dist/size.mjs ADDED
@@ -0,0 +1,109 @@
1
+ import { readFileSync, statSync, existsSync } from 'node:fs';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { isAbsolute, resolve } from 'node:path';
4
+ import { Readable } from 'node:stream';
5
+ import { URL } from 'node:url';
6
+ import { gzipSync, brotliCompressSync, createGzip, createBrotliCompress } from 'node:zlib';
7
+ import { toPath } from '@visulima/path/utils';
8
+
9
+ var __defProp = Object.defineProperty;
10
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
11
+ const fileExists = /* @__PURE__ */ __name((input) => {
12
+ if (isAbsolute(input)) {
13
+ return existsSync(input);
14
+ }
15
+ try {
16
+ return existsSync(resolve(input));
17
+ } catch {
18
+ return false;
19
+ }
20
+ }, "fileExists");
21
+ const getStreamSizeEfficient = /* @__PURE__ */ __name(async (stream) => {
22
+ let totalSize = 0;
23
+ for await (const chunk of stream) {
24
+ totalSize += Buffer.from(chunk).length;
25
+ }
26
+ return totalSize;
27
+ }, "getStreamSizeEfficient");
28
+ const getCompressedStreamSizeEfficient = /* @__PURE__ */ __name(async (stream, createCompressor) => {
29
+ let totalSize = 0;
30
+ const compressor = createCompressor();
31
+ return await new Promise((resolve2, reject) => {
32
+ compressor.on("data", (chunk) => {
33
+ totalSize += chunk.length;
34
+ });
35
+ compressor.on("end", () => resolve2(totalSize));
36
+ compressor.on("error", reject);
37
+ stream.on("error", reject);
38
+ stream.pipe(compressor);
39
+ });
40
+ }, "getCompressedStreamSizeEfficient");
41
+ const processInputEfficiently = /* @__PURE__ */ __name(async (input, processor, streamProcessor) => {
42
+ if (input instanceof URL || typeof input === "string") {
43
+ const path = toPath(input);
44
+ if (fileExists(path)) {
45
+ const fileStream = Readable.from(await readFile(path));
46
+ return await streamProcessor(fileStream);
47
+ }
48
+ if (typeof input === "string") {
49
+ return processor(Buffer.from(input));
50
+ }
51
+ }
52
+ if (input instanceof Readable) {
53
+ return await streamProcessor(input);
54
+ }
55
+ return processor(input);
56
+ }, "processInputEfficiently");
57
+ const gzipSize = /* @__PURE__ */ __name(async (input, options) => {
58
+ const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => createGzip(options)), "streamProcessor");
59
+ const bufferProcessor = /* @__PURE__ */ __name((data) => gzipSync(data, options).length, "bufferProcessor");
60
+ return await processInputEfficiently(input, bufferProcessor, streamProcessor);
61
+ }, "gzipSize");
62
+ const brotliSize = /* @__PURE__ */ __name(async (input, options) => {
63
+ const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => createBrotliCompress(options)), "streamProcessor");
64
+ const bufferProcessor = /* @__PURE__ */ __name((data) => brotliCompressSync(data, options).length, "bufferProcessor");
65
+ return await processInputEfficiently(input, bufferProcessor, streamProcessor);
66
+ }, "brotliSize");
67
+ const rawSize = /* @__PURE__ */ __name(async (input) => {
68
+ const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getStreamSizeEfficient(stream), "streamProcessor");
69
+ const bufferProcessor = /* @__PURE__ */ __name((data) => data.length, "bufferProcessor");
70
+ return await processInputEfficiently(input, bufferProcessor, streamProcessor);
71
+ }, "rawSize");
72
+ const gzipSizeSync = /* @__PURE__ */ __name((input, options) => {
73
+ if (input instanceof URL || typeof input === "string") {
74
+ const path = toPath(input);
75
+ if (fileExists(path)) {
76
+ return gzipSync(readFileSync(path), options).length;
77
+ }
78
+ if (typeof input === "string") {
79
+ return gzipSync(Buffer.from(input), options).length;
80
+ }
81
+ }
82
+ return gzipSync(input, options).length;
83
+ }, "gzipSizeSync");
84
+ const brotliSizeSync = /* @__PURE__ */ __name((input, options) => {
85
+ if (input instanceof URL || typeof input === "string") {
86
+ const path = toPath(input);
87
+ if (fileExists(path)) {
88
+ return brotliCompressSync(readFileSync(path), options).length;
89
+ }
90
+ if (typeof input === "string") {
91
+ return brotliCompressSync(Buffer.from(input), options).length;
92
+ }
93
+ }
94
+ return brotliCompressSync(input, options).length;
95
+ }, "brotliSizeSync");
96
+ const rawSizeSync = /* @__PURE__ */ __name((input) => {
97
+ if (input instanceof URL || typeof input === "string") {
98
+ const path = toPath(input);
99
+ if (fileExists(path)) {
100
+ return statSync(path).size;
101
+ }
102
+ if (typeof input === "string") {
103
+ return Buffer.from(input).length;
104
+ }
105
+ }
106
+ return input.length;
107
+ }, "rawSizeSync");
108
+
109
+ export { brotliSize, brotliSizeSync, gzipSize, gzipSizeSync, rawSize, rawSizeSync };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/fs",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Human friendly file system utilities for Node.js",
5
5
  "keywords": [
6
6
  "anolilab",
@@ -48,7 +48,10 @@
48
48
  "write-dir",
49
49
  "write-file",
50
50
  "write-safe",
51
- "yaml"
51
+ "yaml",
52
+ "size",
53
+ "brotli",
54
+ "gzip"
52
55
  ],
53
56
  "homepage": "https://www.visulima.com/docs/package/fs",
54
57
  "bugs": {
@@ -117,6 +120,16 @@
117
120
  "default": "./dist/yaml.mjs"
118
121
  }
119
122
  },
123
+ "./size": {
124
+ "require": {
125
+ "types": "./dist/size.d.cts",
126
+ "default": "./dist/size.cjs"
127
+ },
128
+ "import": {
129
+ "types": "./dist/size.d.mts",
130
+ "default": "./dist/size.mjs"
131
+ }
132
+ },
120
133
  "./package.json": "./package.json"
121
134
  },
122
135
  "main": "dist/index.cjs",
@@ -135,6 +148,9 @@
135
148
  ],
136
149
  "yaml": [
137
150
  "./dist/yaml.d.ts"
151
+ ],
152
+ "size": [
153
+ "./dist/size.d.ts"
138
154
  ]
139
155
  }
140
156
  },
@@ -145,7 +161,7 @@
145
161
  "LICENSE.md"
146
162
  ],
147
163
  "dependencies": {
148
- "@visulima/path": "1.3.3"
164
+ "@visulima/path": "1.3.4"
149
165
  },
150
166
  "peerDependencies": {
151
167
  "yaml": "^2.7.0"
@@ -156,7 +172,7 @@
156
172
  }
157
173
  },
158
174
  "engines": {
159
- "node": ">=18.* <=23.*"
175
+ "node": ">=18.0.0 <=23.x"
160
176
  },
161
177
  "os": [
162
178
  "darwin",