@naturalcycles/nodejs-lib 15.66.0 → 15.67.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.
@@ -1,4 +1,10 @@
1
1
  import type { ZlibOptions, ZstdOptions } from 'node:zlib';
2
+ export declare function decompressZstdOrInflateToString(buf: Buffer): Promise<string>;
3
+ /**
4
+ * Detects if Buffer is zstd-compressed.
5
+ * Otherwise attempts to Inflate.
6
+ */
7
+ export declare function decompressZstdOrInflate(buf: Buffer): Promise<Buffer<ArrayBuffer>>;
2
8
  /**
3
9
  * deflateBuffer uses `deflate`.
4
10
  * It's 9 bytes shorter than `gzip`.
@@ -26,3 +32,5 @@ export declare function gunzipToString(buf: Buffer, options?: ZlibOptions): Prom
26
32
  export declare function zstdCompress(input: Buffer | string, options?: ZstdOptions): Promise<Buffer<ArrayBuffer>>;
27
33
  export declare function zstdDecompressToString(input: Buffer, options?: ZstdOptions): Promise<string>;
28
34
  export declare function zstdDecompress(input: Buffer, options?: ZstdOptions): Promise<Buffer<ArrayBuffer>>;
35
+ export declare function isZstdBuffer(input: Buffer): boolean;
36
+ export declare function isGzipBuffer(input: Buffer): boolean;
@@ -6,6 +6,19 @@ const gzip = promisify(zlib.gzip.bind(zlib));
6
6
  const gunzip = promisify(zlib.gunzip.bind(zlib));
7
7
  const zstdCompressAsync = promisify(zlib.zstdCompress.bind(zlib));
8
8
  const zstdDecompressAsync = promisify(zlib.zstdDecompress.bind(zlib));
9
+ export async function decompressZstdOrInflateToString(buf) {
10
+ return (await decompressZstdOrInflate(buf)).toString();
11
+ }
12
+ /**
13
+ * Detects if Buffer is zstd-compressed.
14
+ * Otherwise attempts to Inflate.
15
+ */
16
+ export async function decompressZstdOrInflate(buf) {
17
+ if (isZstdBuffer(buf)) {
18
+ return await zstdDecompressAsync(buf);
19
+ }
20
+ return await inflate(buf);
21
+ }
9
22
  /**
10
23
  * deflateBuffer uses `deflate`.
11
24
  * It's 9 bytes shorter than `gzip`.
@@ -21,7 +34,7 @@ export async function inflateBuffer(buf, options = {}) {
21
34
  * It's 9 bytes shorter than `gzip`.
22
35
  */
23
36
  export async function deflateString(s, options) {
24
- return await deflateBuffer(Buffer.from(s), options);
37
+ return await deflate(s, options);
25
38
  }
26
39
  export async function inflateToString(buf, options) {
27
40
  return (await inflateBuffer(buf, options)).toString();
@@ -41,7 +54,7 @@ export async function gunzipBuffer(buf, options = {}) {
41
54
  * It's 9 bytes longer than `deflate`.
42
55
  */
43
56
  export async function gzipString(s, options) {
44
- return await gzipBuffer(Buffer.from(s), options);
57
+ return await gzip(s, options);
45
58
  }
46
59
  export async function gunzipToString(buf, options) {
47
60
  return (await gunzipBuffer(buf, options)).toString();
@@ -55,3 +68,10 @@ export async function zstdDecompressToString(input, options = {}) {
55
68
  export async function zstdDecompress(input, options = {}) {
56
69
  return await zstdDecompressAsync(input, options);
57
70
  }
71
+ const ZSTD_MAGIC_NUMBER = 0xfd2fb528;
72
+ export function isZstdBuffer(input) {
73
+ return input.readUInt32LE(0) === ZSTD_MAGIC_NUMBER;
74
+ }
75
+ export function isGzipBuffer(input) {
76
+ return input[0] === 0x1f && input[1] === 0x8b;
77
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.66.0",
4
+ "version": "15.67.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -9,6 +9,21 @@ const gunzip = promisify(zlib.gunzip.bind(zlib))
9
9
  const zstdCompressAsync = promisify(zlib.zstdCompress.bind(zlib))
10
10
  const zstdDecompressAsync = promisify(zlib.zstdDecompress.bind(zlib))
11
11
 
12
+ export async function decompressZstdOrInflateToString(buf: Buffer): Promise<string> {
13
+ return (await decompressZstdOrInflate(buf)).toString()
14
+ }
15
+
16
+ /**
17
+ * Detects if Buffer is zstd-compressed.
18
+ * Otherwise attempts to Inflate.
19
+ */
20
+ export async function decompressZstdOrInflate(buf: Buffer): Promise<Buffer<ArrayBuffer>> {
21
+ if (isZstdBuffer(buf)) {
22
+ return await zstdDecompressAsync(buf)
23
+ }
24
+ return await inflate(buf)
25
+ }
26
+
12
27
  /**
13
28
  * deflateBuffer uses `deflate`.
14
29
  * It's 9 bytes shorter than `gzip`.
@@ -35,7 +50,7 @@ export async function deflateString(
35
50
  s: string,
36
51
  options?: ZlibOptions,
37
52
  ): Promise<Buffer<ArrayBuffer>> {
38
- return await deflateBuffer(Buffer.from(s), options)
53
+ return await deflate(s, options)
39
54
  }
40
55
 
41
56
  export async function inflateToString(buf: Buffer, options?: ZlibOptions): Promise<string> {
@@ -65,7 +80,7 @@ export async function gunzipBuffer(
65
80
  * It's 9 bytes longer than `deflate`.
66
81
  */
67
82
  export async function gzipString(s: string, options?: ZlibOptions): Promise<Buffer<ArrayBuffer>> {
68
- return await gzipBuffer(Buffer.from(s), options)
83
+ return await gzip(s, options)
69
84
  }
70
85
 
71
86
  export async function gunzipToString(buf: Buffer, options?: ZlibOptions): Promise<string> {
@@ -92,3 +107,13 @@ export async function zstdDecompress(
92
107
  ): Promise<Buffer<ArrayBuffer>> {
93
108
  return await zstdDecompressAsync(input, options)
94
109
  }
110
+
111
+ const ZSTD_MAGIC_NUMBER = 0xfd2fb528
112
+
113
+ export function isZstdBuffer(input: Buffer): boolean {
114
+ return input.readUInt32LE(0) === ZSTD_MAGIC_NUMBER
115
+ }
116
+
117
+ export function isGzipBuffer(input: Buffer): boolean {
118
+ return input[0] === 0x1f && input[1] === 0x8b
119
+ }