@wishknish/knishio-client-ts 0.9.2 → 0.9.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wishknish/knishio-client-ts",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "productName": "Knish.IO TypeScript SDK Client",
5
5
  "description": "TypeScript implementation of the Knish.IO SDK to consume Knish.IO GraphQL APIs with enhanced type safety.",
6
6
  "license": "GPL-3.0-or-later",
@@ -608,11 +608,30 @@ export default class Wallet {
608
608
  }
609
609
 
610
610
  serializeKey(key: Uint8Array): string {
611
- return btoa(String.fromCharCode(...key))
611
+ if (typeof Buffer !== 'undefined') {
612
+ return Buffer.from(key).toString('base64')
613
+ }
614
+ // A single String.fromCharCode(...key) spread overflows the call stack once the
615
+ // payload exceeds the engine's argument limit (~64K), so convert in chunks.
616
+ let binary = ''
617
+ const CHUNK_SIZE = 0x8000
618
+ for (let i = 0; i < key.length; i += CHUNK_SIZE) {
619
+ binary += String.fromCharCode(...key.subarray(i, i + CHUNK_SIZE))
620
+ }
621
+ return btoa(binary)
612
622
  }
613
623
 
614
624
  deserializeKey(serializedKey: string): Uint8Array {
625
+ if (typeof Buffer !== 'undefined') {
626
+ return new Uint8Array(Buffer.from(serializedKey, 'base64'))
627
+ }
628
+ // Spreading the binary string ([...str]) allocates one single-char string per
629
+ // byte — prohibitive for multi-MB payloads. Index directly instead.
615
630
  const binaryString = atob(serializedKey)
616
- return new Uint8Array([...binaryString].map(char => char.charCodeAt(0)))
631
+ const bytes = new Uint8Array(binaryString.length)
632
+ for (let i = 0; i < binaryString.length; i++) {
633
+ bytes[i] = binaryString.charCodeAt(i)
634
+ }
635
+ return bytes
617
636
  }
618
637
  }
@@ -57,11 +57,13 @@ export function deepCloning<T>(
57
57
  * Used for batch processing operations
58
58
  */
59
59
  export function chunkArray<T>(arr: T[], size: number): T[][] {
60
- if (!arr.length) {
61
- return []
60
+ // Iterative: the previous recursive form (`concat` + `slice(size)` per level) blew
61
+ // the call stack and allocated O(n^2) once arr grew past a few thousand elements.
62
+ const chunks: T[][] = []
63
+ for (let i = 0; i < arr.length; i += size) {
64
+ chunks.push(arr.slice(i, i + size))
62
65
  }
63
-
64
- return [arr.slice(0, size)].concat(chunkArray(arr.slice(size), size))
66
+ return chunks
65
67
  }
66
68
 
67
69
  /**
@@ -220,15 +220,20 @@ export function hexToBase64(hexString: string): string {
220
220
 
221
221
  // Convert hex to bytes
222
222
  const bytes = hexStringToBuffer(cleanHex)
223
-
224
- // Convert to base64
225
- if (typeof btoa !== 'undefined') {
226
- // Browser environment
227
- return btoa(String.fromCharCode(...bytes))
228
- } else {
229
- // Node.js environment or manual conversion
223
+
224
+ // Buffer fast path (Node). Byte-identical to the btoa encoding below.
225
+ if (typeof Buffer !== 'undefined') {
230
226
  return Buffer.from(bytes).toString('base64')
231
227
  }
228
+ // Browser fallback: a single String.fromCharCode(...bytes) spread overflows the
229
+ // call stack once the payload exceeds the engine's argument limit (~64K), so
230
+ // convert in chunks.
231
+ let binary = ''
232
+ const CHUNK_SIZE = 0x8000
233
+ for (let i = 0; i < bytes.length; i += CHUNK_SIZE) {
234
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK_SIZE))
235
+ }
236
+ return btoa(binary)
232
237
  } catch (error) {
233
238
  throw new Error(`Failed to convert hex to base64: ${error instanceof Error ? error.message : String(error)}`)
234
239
  }