@wishknish/knishio-client-js 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-js",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "type": "module",
5
5
  "productName": "Knish.IO Javascript SDK Client",
6
6
  "description": "JavaScript implementation of the Knish.IO SDK to consume Knish.IO GraphQL APIs.",
package/src/Wallet.js CHANGED
@@ -300,10 +300,23 @@ export default class Wallet {
300
300
  }
301
301
 
302
302
  serializeKey (key) {
303
- return btoa(String.fromCharCode.apply(null, key))
303
+ if (typeof Buffer !== 'undefined') {
304
+ return Buffer.from(key).toString('base64')
305
+ }
306
+ // String.fromCharCode.apply(null, key) overflows the call stack once the
307
+ // payload exceeds the engine's argument limit (~64K), so convert in chunks.
308
+ let binary = ''
309
+ const CHUNK_SIZE = 0x8000
310
+ for (let i = 0; i < key.length; i += CHUNK_SIZE) {
311
+ binary += String.fromCharCode(...key.subarray(i, i + CHUNK_SIZE))
312
+ }
313
+ return btoa(binary)
304
314
  }
305
315
 
306
316
  deserializeKey (serializedKey) {
317
+ if (typeof Buffer !== 'undefined') {
318
+ return new Uint8Array(Buffer.from(serializedKey, 'base64'))
319
+ }
307
320
  const binaryString = atob(serializedKey)
308
321
  return new Uint8Array(binaryString.length).map((_, i) => binaryString.charCodeAt(i))
309
322
  }
@@ -6,11 +6,13 @@
6
6
  * @return {array}
7
7
  */
8
8
  export function chunkArray (arr, size) {
9
- if (!arr.length) {
10
- return []
9
+ // Iterative: the previous recursive form (`concat` + `slice(size)` per level) blew
10
+ // the call stack and allocated O(n^2) once arr grew past a few thousand elements.
11
+ const chunks = []
12
+ for (let i = 0; i < arr.length; i += size) {
13
+ chunks.push(arr.slice(i, i + size))
11
14
  }
12
-
13
- return [arr.slice(0, size)].concat(chunkArray(arr.slice(size), size))
15
+ return chunks
14
16
  }
15
17
 
16
18
  export function deepCloning (
@@ -45,13 +45,18 @@ export function chunkSubstr (str, size) {
45
45
  * @return {string}
46
46
  */
47
47
  export function randomString (length = 256, alphabet = 'abcdef0123456789') {
48
- let array = new Uint8Array(length)
48
+ const array = new Uint8Array(length)
49
49
 
50
- array = crypto.getRandomValues(array)
50
+ crypto.getRandomValues(array)
51
51
 
52
- array = array.map(x => alphabet.charCodeAt(x % alphabet.length))
53
-
54
- return String.fromCharCode.apply(null, array)
52
+ // Build in a loop: String.fromCharCode.apply(null, array) spread every byte as a
53
+ // function argument and overflowed the call stack for a large caller-supplied length.
54
+ // `alphabet.charAt(x % len)` is the same mapping as the old fromCharCode(charCodeAt).
55
+ let result = ''
56
+ for (let i = 0; i < length; i++) {
57
+ result += alphabet.charAt(array[i] % alphabet.length)
58
+ }
59
+ return result
55
60
  }
56
61
 
57
62
  /**
@@ -120,7 +125,19 @@ export function hexStringToBuffer (hexString) {
120
125
  */
121
126
  export function hexToBase64 (string) {
122
127
  const bytes = hexStringToBuffer(string)
123
- return btoa(String.fromCharCode.apply(null, bytes))
128
+ // Buffer fast path (Node). Byte-identical to the btoa encoding below.
129
+ if (typeof Buffer !== 'undefined') {
130
+ return Buffer.from(bytes).toString('base64')
131
+ }
132
+ // Browser fallback: String.fromCharCode.apply(null, bytes) spreads every byte as a
133
+ // function argument and overflows the call stack past the engine's argument limit
134
+ // (~64K), so convert in chunks.
135
+ let binary = ''
136
+ const CHUNK_SIZE = 0x8000
137
+ for (let i = 0; i < bytes.length; i += CHUNK_SIZE) {
138
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK_SIZE))
139
+ }
140
+ return btoa(binary)
124
141
  }
125
142
 
126
143
  /**
@@ -130,7 +147,16 @@ export function hexToBase64 (string) {
130
147
  * @return {string}
131
148
  */
132
149
  export function base64ToHex (string) {
133
- const bytes = new Uint8Array(atob(string).split('').map(char => char.charCodeAt(0)))
150
+ // Buffer fast path (Node); index loop otherwise. The old `atob(string).split('')`
151
+ // allocated one single-char string per byte — prohibitive for multi-MB payloads.
152
+ if (typeof Buffer !== 'undefined') {
153
+ return bufferToHexString(new Uint8Array(Buffer.from(string, 'base64')))
154
+ }
155
+ const binary = atob(string)
156
+ const bytes = new Uint8Array(binary.length)
157
+ for (let i = 0; i < binary.length; i++) {
158
+ bytes[i] = binary.charCodeAt(i)
159
+ }
134
160
  return bufferToHexString(bytes)
135
161
  }
136
162
 
@@ -1,2 +0,0 @@
1
- 'use strict'
2
- Object.defineProperty(exports, '__esModule', { value: true })