@arkiv-network/sdk 0.1.2-dev.0 → 0.1.3-dev.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,48 +1,99 @@
1
1
  // src/utils/compression.ts
2
2
 
3
3
  import type { BrotliWasmType } from "brotli-wasm"
4
- import brotliPromise from "brotli-wasm"
5
4
 
5
+ // Detect if we're in Node.js environment
6
+ const isNode = typeof process !== "undefined" && process.versions?.node != null
7
+
8
+ let zlib: typeof import("zlib") | null = null
6
9
  let brotli: BrotliWasmType | null = null
7
10
 
8
- async function getBrotli(): Promise<BrotliWasmType | null> {
9
- if (brotli) return brotli
11
+ // Lazy load zlib (Node.js only)
12
+ async function getZlib(): Promise<typeof import("zlib") | null> {
13
+ if (!isNode) return null
10
14
 
11
- if (!brotliPromise) {
12
- console.debug("Importing brotli-wasm")
15
+ if (!zlib) {
13
16
  try {
14
- console.debug("Importing brotli-wasm as module")
15
- brotli = await brotliPromise
16
- console.debug("Imported brotli-wasm as module", brotli)
17
+ zlib = await import("zlib")
17
18
  } catch (error) {
18
- console.error("Error importing brotli-wasm", error)
19
- console.debug("Importing brotli-wasm as require")
20
- brotli = require("brotli-wasm")
21
- console.debug("Imported brotli-wasm as require", brotli)
19
+ console.error("Error importing zlib", error)
20
+ return null
22
21
  }
23
22
  }
23
+ return zlib
24
+ }
25
+
26
+ // Lazy load brotli-wasm (browser/fallback)
27
+ async function getBrotli(): Promise<BrotliWasmType | null> {
28
+ if (brotli) return brotli
29
+
30
+ try {
31
+ const brotliModule = await import("brotli-wasm")
32
+ brotli = brotliModule.default || brotliModule
33
+ } catch (error) {
34
+ console.error("Error importing brotli-wasm", error)
35
+ return null
36
+ }
24
37
 
25
- brotli = await brotliPromise
26
38
  return brotli
27
39
  }
28
40
 
29
41
  export async function compress(data: Uint8Array): Promise<Uint8Array> {
30
- const brotliInstance = await getBrotli()
31
42
  console.debug(`Compressing data (size: ${data.length})`)
43
+
44
+ // Try Node.js zlib first (if available)
45
+ if (isNode) {
46
+ const zlibModule = await getZlib()
47
+ if (zlibModule) {
48
+ try {
49
+ // zlib.brotliCompress expects Buffer, returns Buffer
50
+ const buffer = Buffer.from(data)
51
+ const compressed = zlibModule.brotliCompressSync(buffer)
52
+ const result = new Uint8Array(compressed)
53
+ console.debug(`Brotli compressed with zlib (size: ${result.length})`)
54
+ return result
55
+ } catch (error) {
56
+ console.warn("zlib compression failed, falling back to brotli-wasm", error)
57
+ }
58
+ }
59
+ }
60
+
61
+ // Fallback to brotli-wasm (browser or if zlib fails)
62
+ const brotliInstance = await getBrotli()
32
63
  if (!brotliInstance) {
33
64
  console.warn("Brotli instance not found, not compressing data")
34
65
  return data
35
66
  }
67
+
36
68
  const brotliCompressed = brotliInstance.compress(data)
37
- console.debug(`Brotli compressed data (size: ${brotliCompressed.length})`)
69
+ console.debug(`Brotli compressed with wasm (size: ${brotliCompressed.length})`)
38
70
  return brotliCompressed
39
71
  }
40
72
 
41
73
  export async function decompress(data: Uint8Array): Promise<Uint8Array> {
74
+ // Try Node.js zlib first (if available)
75
+ if (isNode) {
76
+ const zlibModule = await getZlib()
77
+ if (zlibModule) {
78
+ try {
79
+ // zlib.brotliDecompress expects Buffer, returns Buffer
80
+ const buffer = Buffer.from(data)
81
+ const decompressed = zlibModule.brotliDecompressSync(buffer)
82
+ const result = new Uint8Array(decompressed)
83
+ console.debug(`Brotli decompressed with zlib (size: ${result.length})`)
84
+ return result
85
+ } catch (error) {
86
+ console.warn("zlib decompression failed, falling back to brotli-wasm", error)
87
+ }
88
+ }
89
+ }
90
+
91
+ // Fallback to brotli-wasm (browser or if zlib fails)
42
92
  const brotliInstance = await getBrotli()
43
93
  if (!brotliInstance) {
44
94
  console.warn("Brotli instance not found, not decompressing data")
45
95
  return data
46
96
  }
97
+
47
98
  return brotliInstance.decompress(data)
48
99
  }