@neoware_inc/neozipkit 0.5.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.
Files changed (171) hide show
  1. package/README.md +134 -0
  2. package/dist/browser/ZipkitBrowser.d.ts +27 -0
  3. package/dist/browser/ZipkitBrowser.d.ts.map +1 -0
  4. package/dist/browser/ZipkitBrowser.js +303 -0
  5. package/dist/browser/ZipkitBrowser.js.map +1 -0
  6. package/dist/browser/index.d.ts +9 -0
  7. package/dist/browser/index.d.ts.map +1 -0
  8. package/dist/browser/index.esm.d.ts +12 -0
  9. package/dist/browser/index.esm.d.ts.map +1 -0
  10. package/dist/browser/index.esm.js +46 -0
  11. package/dist/browser/index.esm.js.map +1 -0
  12. package/dist/browser/index.js +38 -0
  13. package/dist/browser/index.js.map +1 -0
  14. package/dist/browser-esm/index.d.ts +9 -0
  15. package/dist/browser-esm/index.js +50211 -0
  16. package/dist/browser-esm/index.js.map +7 -0
  17. package/dist/browser-umd/index.d.ts +9 -0
  18. package/dist/browser-umd/index.js +50221 -0
  19. package/dist/browser-umd/index.js.map +7 -0
  20. package/dist/browser-umd/index.min.js +39 -0
  21. package/dist/browser.d.ts +9 -0
  22. package/dist/browser.js +38 -0
  23. package/dist/core/ZipCompress.d.ts +99 -0
  24. package/dist/core/ZipCompress.d.ts.map +1 -0
  25. package/dist/core/ZipCompress.js +287 -0
  26. package/dist/core/ZipCompress.js.map +1 -0
  27. package/dist/core/ZipCopy.d.ts +175 -0
  28. package/dist/core/ZipCopy.d.ts.map +1 -0
  29. package/dist/core/ZipCopy.js +310 -0
  30. package/dist/core/ZipCopy.js.map +1 -0
  31. package/dist/core/ZipDecompress.d.ts +57 -0
  32. package/dist/core/ZipDecompress.d.ts.map +1 -0
  33. package/dist/core/ZipDecompress.js +155 -0
  34. package/dist/core/ZipDecompress.js.map +1 -0
  35. package/dist/core/ZipEntry.d.ts +138 -0
  36. package/dist/core/ZipEntry.d.ts.map +1 -0
  37. package/dist/core/ZipEntry.js +829 -0
  38. package/dist/core/ZipEntry.js.map +1 -0
  39. package/dist/core/Zipkit.d.ts +315 -0
  40. package/dist/core/Zipkit.d.ts.map +1 -0
  41. package/dist/core/Zipkit.js +647 -0
  42. package/dist/core/Zipkit.js.map +1 -0
  43. package/dist/core/ZstdManager.d.ts +56 -0
  44. package/dist/core/ZstdManager.d.ts.map +1 -0
  45. package/dist/core/ZstdManager.js +144 -0
  46. package/dist/core/ZstdManager.js.map +1 -0
  47. package/dist/core/components/HashCalculator.d.ts +138 -0
  48. package/dist/core/components/HashCalculator.d.ts.map +1 -0
  49. package/dist/core/components/HashCalculator.js +360 -0
  50. package/dist/core/components/HashCalculator.js.map +1 -0
  51. package/dist/core/components/Logger.d.ts +73 -0
  52. package/dist/core/components/Logger.d.ts.map +1 -0
  53. package/dist/core/components/Logger.js +156 -0
  54. package/dist/core/components/Logger.js.map +1 -0
  55. package/dist/core/components/ProgressTracker.d.ts +43 -0
  56. package/dist/core/components/ProgressTracker.d.ts.map +1 -0
  57. package/dist/core/components/ProgressTracker.js +112 -0
  58. package/dist/core/components/ProgressTracker.js.map +1 -0
  59. package/dist/core/components/Support.d.ts +64 -0
  60. package/dist/core/components/Support.d.ts.map +1 -0
  61. package/dist/core/components/Support.js +71 -0
  62. package/dist/core/components/Support.js.map +1 -0
  63. package/dist/core/components/Util.d.ts +26 -0
  64. package/dist/core/components/Util.d.ts.map +1 -0
  65. package/dist/core/components/Util.js +95 -0
  66. package/dist/core/components/Util.js.map +1 -0
  67. package/dist/core/constants/Errors.d.ts +52 -0
  68. package/dist/core/constants/Errors.d.ts.map +1 -0
  69. package/dist/core/constants/Errors.js +67 -0
  70. package/dist/core/constants/Errors.js.map +1 -0
  71. package/dist/core/constants/Headers.d.ts +170 -0
  72. package/dist/core/constants/Headers.d.ts.map +1 -0
  73. package/dist/core/constants/Headers.js +194 -0
  74. package/dist/core/constants/Headers.js.map +1 -0
  75. package/dist/core/encryption/Manager.d.ts +58 -0
  76. package/dist/core/encryption/Manager.d.ts.map +1 -0
  77. package/dist/core/encryption/Manager.js +121 -0
  78. package/dist/core/encryption/Manager.js.map +1 -0
  79. package/dist/core/encryption/ZipCrypto.d.ts +172 -0
  80. package/dist/core/encryption/ZipCrypto.d.ts.map +1 -0
  81. package/dist/core/encryption/ZipCrypto.js +554 -0
  82. package/dist/core/encryption/ZipCrypto.js.map +1 -0
  83. package/dist/core/encryption/index.d.ts +9 -0
  84. package/dist/core/encryption/index.d.ts.map +1 -0
  85. package/dist/core/encryption/index.js +17 -0
  86. package/dist/core/encryption/index.js.map +1 -0
  87. package/dist/core/encryption/types.d.ts +29 -0
  88. package/dist/core/encryption/types.d.ts.map +1 -0
  89. package/dist/core/encryption/types.js +12 -0
  90. package/dist/core/encryption/types.js.map +1 -0
  91. package/dist/core/index.d.ts +27 -0
  92. package/dist/core/index.d.ts.map +1 -0
  93. package/dist/core/index.js +59 -0
  94. package/dist/core/index.js.map +1 -0
  95. package/dist/core/version.d.ts +5 -0
  96. package/dist/core/version.d.ts.map +1 -0
  97. package/dist/core/version.js +31 -0
  98. package/dist/core/version.js.map +1 -0
  99. package/dist/index.d.ts +9 -0
  100. package/dist/index.d.ts.map +1 -0
  101. package/dist/index.js +38 -0
  102. package/dist/index.js.map +1 -0
  103. package/dist/node/ZipCompressNode.d.ts +123 -0
  104. package/dist/node/ZipCompressNode.d.ts.map +1 -0
  105. package/dist/node/ZipCompressNode.js +565 -0
  106. package/dist/node/ZipCompressNode.js.map +1 -0
  107. package/dist/node/ZipCopyNode.d.ts +165 -0
  108. package/dist/node/ZipCopyNode.d.ts.map +1 -0
  109. package/dist/node/ZipCopyNode.js +347 -0
  110. package/dist/node/ZipCopyNode.js.map +1 -0
  111. package/dist/node/ZipDecompressNode.d.ts +197 -0
  112. package/dist/node/ZipDecompressNode.d.ts.map +1 -0
  113. package/dist/node/ZipDecompressNode.js +678 -0
  114. package/dist/node/ZipDecompressNode.js.map +1 -0
  115. package/dist/node/ZipkitNode.d.ts +466 -0
  116. package/dist/node/ZipkitNode.d.ts.map +1 -0
  117. package/dist/node/ZipkitNode.js +1426 -0
  118. package/dist/node/ZipkitNode.js.map +1 -0
  119. package/dist/node/index.d.ts +25 -0
  120. package/dist/node/index.d.ts.map +1 -0
  121. package/dist/node/index.js +54 -0
  122. package/dist/node/index.js.map +1 -0
  123. package/dist/types/index.d.ts +45 -0
  124. package/dist/types/index.d.ts.map +1 -0
  125. package/dist/types/index.js +11 -0
  126. package/dist/types/index.js.map +1 -0
  127. package/examples/README.md +261 -0
  128. package/examples/append-data.json +44 -0
  129. package/examples/copy-zip-append.ts +139 -0
  130. package/examples/copy-zip.ts +152 -0
  131. package/examples/create-zip.ts +172 -0
  132. package/examples/extract-zip.ts +118 -0
  133. package/examples/list-zip.ts +161 -0
  134. package/examples/test-files/data.json +116 -0
  135. package/examples/test-files/document.md +80 -0
  136. package/examples/test-files/document.txt +6 -0
  137. package/examples/test-files/file1.txt +48 -0
  138. package/examples/test-files/file2.txt +80 -0
  139. package/examples/tsconfig.json +44 -0
  140. package/package.json +167 -0
  141. package/src/browser/ZipkitBrowser.ts +305 -0
  142. package/src/browser/index.esm.ts +32 -0
  143. package/src/browser/index.ts +19 -0
  144. package/src/core/ZipCompress.ts +370 -0
  145. package/src/core/ZipCopy.ts +434 -0
  146. package/src/core/ZipDecompress.ts +191 -0
  147. package/src/core/ZipEntry.ts +917 -0
  148. package/src/core/Zipkit.ts +794 -0
  149. package/src/core/ZstdManager.ts +165 -0
  150. package/src/core/components/HashCalculator.ts +384 -0
  151. package/src/core/components/Logger.ts +180 -0
  152. package/src/core/components/ProgressTracker.ts +134 -0
  153. package/src/core/components/Support.ts +77 -0
  154. package/src/core/components/Util.ts +91 -0
  155. package/src/core/constants/Errors.ts +78 -0
  156. package/src/core/constants/Headers.ts +205 -0
  157. package/src/core/encryption/Manager.ts +137 -0
  158. package/src/core/encryption/ZipCrypto.ts +650 -0
  159. package/src/core/encryption/index.ts +15 -0
  160. package/src/core/encryption/types.ts +33 -0
  161. package/src/core/index.ts +42 -0
  162. package/src/core/version.ts +33 -0
  163. package/src/index.ts +19 -0
  164. package/src/node/ZipCompressNode.ts +618 -0
  165. package/src/node/ZipCopyNode.ts +437 -0
  166. package/src/node/ZipDecompressNode.ts +793 -0
  167. package/src/node/ZipkitNode.ts +1706 -0
  168. package/src/node/index.ts +40 -0
  169. package/src/types/index.ts +68 -0
  170. package/src/types/modules.d.ts +22 -0
  171. package/src/types/opentimestamps.d.ts +1 -0
@@ -0,0 +1,56 @@
1
+ /**
2
+ * ZstdManager - Global Zstd Codec Manager
3
+ *
4
+ * This manager ensures safe usage of the Zstd WASM module across multiple instances.
5
+ *
6
+ * ## Why This Is Necessary
7
+ *
8
+ * The `@oneidentity/zstd-js` library uses a shared WASM module internally.
9
+ * While `ZstdInit()` returns different object references, they all share the same
10
+ * `ZstdSimple` implementation and WASM memory/heap.
11
+ *
12
+ * This means concurrent or improperly managed sequential operations can cause
13
+ * memory corruption because they're all operating on the same underlying WASM state.
14
+ *
15
+ * ## How It Works
16
+ *
17
+ * 1. Single initialization: WASM module is initialized once globally
18
+ * 2. Operation queuing: All compress/decompress operations are queued
19
+ * 3. Sequential execution: JavaScript's single-threaded nature ensures safe execution
20
+ * 4. Automatic initialization: Lazy initialization on first use
21
+ *
22
+ * ## Usage
23
+ *
24
+ * ```typescript
25
+ * import { ZstdManager } from './ZstdManager';
26
+ *
27
+ * // Compress
28
+ * const compressed = await ZstdManager.compress(data, level);
29
+ *
30
+ * // Decompress
31
+ * const decompressed = await ZstdManager.decompress(compressedData);
32
+ * ```
33
+ */
34
+ /**
35
+ * Global Zstd Manager
36
+ * Use this for all Zstd compression/decompression operations
37
+ */
38
+ export declare const ZstdManager: {
39
+ /**
40
+ * Compress data using Zstd
41
+ * Operations are automatically queued to prevent interference
42
+ */
43
+ compress: (data: Uint8Array, level?: number) => Promise<Uint8Array<ArrayBufferLike>>;
44
+ /**
45
+ * Decompress data using Zstd
46
+ * Operations are automatically queued to prevent interference
47
+ */
48
+ decompress: (data: Uint8Array | Buffer) => Promise<Uint8Array<ArrayBufferLike>>;
49
+ /**
50
+ * Reset the manager (for testing)
51
+ * @internal
52
+ */
53
+ reset: () => void;
54
+ };
55
+ export default ZstdManager;
56
+ //# sourceMappingURL=ZstdManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ZstdManager.d.ts","sourceRoot":"","sources":["../../src/core/ZstdManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAyGH;;;GAGG;AACH,eAAO,MAAM,WAAW;IACtB;;;OAGG;qBACc,UAAU,UAAU,MAAM;IAG3C;;;OAGG;uBACgB,UAAU,GAAG,MAAM;IAGtC;;;OAGG;;CAEJ,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ /**
3
+ * ZstdManager - Global Zstd Codec Manager
4
+ *
5
+ * This manager ensures safe usage of the Zstd WASM module across multiple instances.
6
+ *
7
+ * ## Why This Is Necessary
8
+ *
9
+ * The `@oneidentity/zstd-js` library uses a shared WASM module internally.
10
+ * While `ZstdInit()` returns different object references, they all share the same
11
+ * `ZstdSimple` implementation and WASM memory/heap.
12
+ *
13
+ * This means concurrent or improperly managed sequential operations can cause
14
+ * memory corruption because they're all operating on the same underlying WASM state.
15
+ *
16
+ * ## How It Works
17
+ *
18
+ * 1. Single initialization: WASM module is initialized once globally
19
+ * 2. Operation queuing: All compress/decompress operations are queued
20
+ * 3. Sequential execution: JavaScript's single-threaded nature ensures safe execution
21
+ * 4. Automatic initialization: Lazy initialization on first use
22
+ *
23
+ * ## Usage
24
+ *
25
+ * ```typescript
26
+ * import { ZstdManager } from './ZstdManager';
27
+ *
28
+ * // Compress
29
+ * const compressed = await ZstdManager.compress(data, level);
30
+ *
31
+ * // Decompress
32
+ * const decompressed = await ZstdManager.decompress(compressedData);
33
+ * ```
34
+ */
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ZstdManager = void 0;
37
+ const zstd_js_1 = require("@oneidentity/zstd-js");
38
+ class ZstdCodecManager {
39
+ constructor() {
40
+ this.codec = null;
41
+ this.initPromise = null;
42
+ this.operationQueue = Promise.resolve();
43
+ }
44
+ /**
45
+ * Get the singleton instance of ZstdCodecManager
46
+ */
47
+ static getInstance() {
48
+ if (!ZstdCodecManager.instance) {
49
+ ZstdCodecManager.instance = new ZstdCodecManager();
50
+ }
51
+ return ZstdCodecManager.instance;
52
+ }
53
+ /**
54
+ * Ensure the Zstd codec is initialized
55
+ * This is called automatically before any compress/decompress operation
56
+ */
57
+ async ensureInitialized() {
58
+ if (this.codec) {
59
+ return;
60
+ }
61
+ if (this.initPromise) {
62
+ return this.initPromise;
63
+ }
64
+ this.initPromise = (async () => {
65
+ this.codec = await (0, zstd_js_1.ZstdInit)();
66
+ })();
67
+ return this.initPromise;
68
+ }
69
+ /**
70
+ * Queue an operation to ensure sequential execution
71
+ * This prevents concurrent operations from interfering with each other
72
+ */
73
+ async queueOperation(operation) {
74
+ // Chain this operation after the previous one
75
+ const promise = this.operationQueue.then(operation, operation);
76
+ // Update the queue to point to this operation
77
+ this.operationQueue = promise.catch(() => { }); // Catch to prevent unhandled rejections in queue
78
+ return promise;
79
+ }
80
+ /**
81
+ * Compress data using Zstd
82
+ * @param data - Data to compress (Uint8Array)
83
+ * @param level - Compression level (1-22, default 6)
84
+ * @returns Compressed data as Uint8Array
85
+ */
86
+ async compress(data, level = 6) {
87
+ return this.queueOperation(async () => {
88
+ await this.ensureInitialized();
89
+ if (!this.codec) {
90
+ throw new Error('Zstd codec not initialized');
91
+ }
92
+ return this.codec.ZstdSimple.compress(data, level);
93
+ });
94
+ }
95
+ /**
96
+ * Decompress data using Zstd
97
+ * @param data - Compressed data (Uint8Array or Buffer)
98
+ * @returns Decompressed data as Uint8Array
99
+ */
100
+ async decompress(data) {
101
+ return this.queueOperation(async () => {
102
+ await this.ensureInitialized();
103
+ if (!this.codec) {
104
+ throw new Error('Zstd codec not initialized');
105
+ }
106
+ // Convert Buffer to Uint8Array if needed
107
+ const inputData = data instanceof Buffer
108
+ ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
109
+ : data;
110
+ return this.codec.ZstdSimple.decompress(inputData);
111
+ });
112
+ }
113
+ /**
114
+ * Reset the manager (for testing purposes)
115
+ * @internal
116
+ */
117
+ static reset() {
118
+ ZstdCodecManager.instance = null;
119
+ }
120
+ }
121
+ ZstdCodecManager.instance = null;
122
+ /**
123
+ * Global Zstd Manager
124
+ * Use this for all Zstd compression/decompression operations
125
+ */
126
+ exports.ZstdManager = {
127
+ /**
128
+ * Compress data using Zstd
129
+ * Operations are automatically queued to prevent interference
130
+ */
131
+ compress: (data, level) => ZstdCodecManager.getInstance().compress(data, level),
132
+ /**
133
+ * Decompress data using Zstd
134
+ * Operations are automatically queued to prevent interference
135
+ */
136
+ decompress: (data) => ZstdCodecManager.getInstance().decompress(data),
137
+ /**
138
+ * Reset the manager (for testing)
139
+ * @internal
140
+ */
141
+ reset: () => ZstdCodecManager.reset(),
142
+ };
143
+ exports.default = exports.ZstdManager;
144
+ //# sourceMappingURL=ZstdManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ZstdManager.js","sourceRoot":"","sources":["../../src/core/ZstdManager.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Unified hash calculator supporting:
3
+ * - Incremental CRC-32 and SHA-256 calculation from raw data chunks
4
+ * - Accumulation of pre-computed SHA-256 hashes with XOR and Merkle tree operations
5
+ * - Merkle tree construction, proof generation, and verification
6
+ *
7
+ * Usage examples:
8
+ *
9
+ * Incremental hash calculation:
10
+ * ```typescript
11
+ * const calculator = new HashCalculator({ useSHA256: true });
12
+ * calculator.update(dataChunk);
13
+ * const crc32 = calculator.finalizeCRC32();
14
+ * const sha256 = calculator.finalizeSHA256();
15
+ * ```
16
+ *
17
+ * Hash accumulation:
18
+ * ```typescript
19
+ * const calculator = new HashCalculator({ enableAccumulation: true });
20
+ * calculator.addHash(sha256Hash1);
21
+ * calculator.addHash(sha256Hash2);
22
+ * const xor = calculator.xorHash();
23
+ * const merkle = calculator.merkleRoot();
24
+ * ```
25
+ */
26
+ export declare class HashCalculator {
27
+ private crc32State;
28
+ private sha256Hash;
29
+ private useSHA256;
30
+ private hashes;
31
+ private xorResult;
32
+ private enableAccumulation;
33
+ private merkleLeaves;
34
+ private merkleLayers;
35
+ private merkleOptions;
36
+ /**
37
+ * Creates a new HashCalculator instance
38
+ * @param options - Configuration options:
39
+ * - useSHA256: Enable SHA-256 calculation for incremental mode (default: false)
40
+ * - enableAccumulation: Enable hash accumulation mode (default: false)
41
+ */
42
+ constructor(options?: {
43
+ useSHA256?: boolean;
44
+ enableAccumulation?: boolean;
45
+ });
46
+ /**
47
+ * Update hash state with a new chunk of data
48
+ * Updates both CRC-32 and SHA-256 (if enabled) incrementally
49
+ * @param chunk - Data chunk to process
50
+ */
51
+ update(chunk: Buffer): void;
52
+ /**
53
+ * Get final CRC-32 value
54
+ * @returns Final CRC-32 value as unsigned 32-bit integer
55
+ */
56
+ finalizeCRC32(): number;
57
+ /**
58
+ * Get final SHA-256 hash as hex string
59
+ * @returns SHA-256 hash as hex string, or null if SHA-256 not enabled
60
+ */
61
+ finalizeSHA256(): string | null;
62
+ /**
63
+ * Reset the incremental hash calculation state
64
+ */
65
+ reset(): void;
66
+ /**
67
+ * Adds a pre-computed SHA-256 hash to both the XOR accumulation and hash array
68
+ * @param hash - Hash value as hex string or Buffer (must be 32 bytes for SHA-256)
69
+ */
70
+ addHash(hash: string | Buffer): void;
71
+ /**
72
+ * Gets the accumulated XOR of all added hashes
73
+ * @returns Hex string of XOR result
74
+ */
75
+ xorHash(): string;
76
+ /**
77
+ * Creates a Merkle tree from accumulated hashes and returns the root
78
+ * @returns Hex string of Merkle root, or null if no hashes added
79
+ */
80
+ merkleRoot(): string | null;
81
+ /**
82
+ * Gets the number of hashes accumulated
83
+ * @returns Number of accumulated hashes
84
+ */
85
+ leafCount(): number;
86
+ /**
87
+ * Clears the accumulated hashes and resets XOR buffer
88
+ */
89
+ clear(): void;
90
+ /**
91
+ * Combines multiple hash results into a single Merkle root
92
+ * Static method for combining results from multiple HashCalculator instances
93
+ * @param results - Array of objects containing merkleRoot properties
94
+ * @returns Combined Merkle root as hex string, or null if no valid roots
95
+ */
96
+ static combineResults(results: {
97
+ merkleRoot?: string;
98
+ }[]): string | null;
99
+ /**
100
+ * Computes SHA-256 hash of input data
101
+ * @param data - Data to hash
102
+ * @returns Buffer containing hash
103
+ */
104
+ private hash;
105
+ /**
106
+ * Combines two child hashes to create parent hash
107
+ * @param left - Left child hash
108
+ * @param right - Right child hash
109
+ * @returns Combined hash of children
110
+ */
111
+ private combinedHash;
112
+ /**
113
+ * Builds the Merkle tree by creating successive layers of hashes
114
+ * @param nodes - Array of nodes in current layer
115
+ */
116
+ private createHashes;
117
+ /**
118
+ * Rebuilds the Merkle tree from current leaves
119
+ */
120
+ private rebuildMerkleTree;
121
+ /**
122
+ * Generates a proof of inclusion for a leaf node
123
+ * @param leaf - The leaf node to generate proof for
124
+ * @returns Array of sibling hashes needed to reconstruct root
125
+ * @throws Error if leaf not found in tree or accumulation not enabled
126
+ */
127
+ getProof(leaf: Buffer): Buffer[];
128
+ /**
129
+ * Verifies a proof of inclusion for a leaf node
130
+ * @param proof - Array of sibling hashes from getProof()
131
+ * @param targetHash - Hash of the leaf node being verified
132
+ * @param root - Expected root hash
133
+ * @returns boolean indicating if proof is valid
134
+ */
135
+ verify(proof: Buffer[], targetHash: Buffer, root: Buffer): boolean;
136
+ }
137
+ export default HashCalculator;
138
+ //# sourceMappingURL=HashCalculator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HashCalculator.d.ts","sourceRoot":"","sources":["../../../src/core/components/HashCalculator.ts"],"names":[],"mappings":"AAkBA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,cAAc;IAEzB,OAAO,CAAC,UAAU,CAAc;IAChC,OAAO,CAAC,UAAU,CAA4B;IAC9C,OAAO,CAAC,SAAS,CAAkB;IAGnC,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,SAAS,CAA+B;IAChD,OAAO,CAAC,kBAAkB,CAAkB;IAG5C,OAAO,CAAC,YAAY,CAAgB;IACpC,OAAO,CAAC,YAAY,CAAkB;IACtC,OAAO,CAAC,aAAa,CAKnB;IAEF;;;;;OAKG;gBACS,OAAO,CAAC,EAAE;QACpB,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,kBAAkB,CAAC,EAAE,OAAO,CAAC;KAC9B;IAaD;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAY3B;;;OAGG;IACH,aAAa,IAAI,MAAM;IAIvB;;;OAGG;IACH,cAAc,IAAI,MAAM,GAAG,IAAI;IAO/B;;OAEG;IACH,KAAK,IAAI,IAAI;IAWb;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAuBpC;;;OAGG;IACH,OAAO,IAAI,MAAM;IAOjB;;;OAGG;IACH,UAAU,IAAI,MAAM,GAAG,IAAI;IAsB3B;;;OAGG;IACH,SAAS,IAAI,MAAM;IAOnB;;OAEG;IACH,KAAK,IAAI,IAAI;IAUb;;;;;OAKG;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,GAAG,MAAM,GAAG,IAAI;IAiBxE;;;;OAIG;IACH,OAAO,CAAC,IAAI;IAIZ;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IAOpB;;;OAGG;IACH,OAAO,CAAC,YAAY;IA8BpB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE;IA2BhC;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;CAWnE;AAGD,eAAe,cAAc,CAAC"}