From a268a8bcc9ac0c2f6fc6daf1e6bd4d6b25258ba4 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 22 Jul 2025 21:57:27 -0700 Subject: [PATCH] Refactor to avoid spread operator Spread operator will get stack overflow on large inputs. Can simplify in the future with `Uint8Array.prototype.toBase64()` once widely available. --- script.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 684840b..75c442c 100644 --- a/script.js +++ b/script.js @@ -98,8 +98,15 @@ export async function encrypt(plaintext) { paddedPlaintext )); - const buffer = Uint8Array.from([...salt, ...iv, ...ciphertext]); - const content = btoa(String.fromCharCode(...buffer)); + const buffer = new Uint8Array(salt.byteLength + iv.byteLength + ciphertext.byteLength); + buffer.set(salt); + buffer.set(iv, salt.byteLength); + buffer.set(ciphertext, salt.byteLength + iv.byteLength); + let binary = ''; + for (let i = 0; i < buffer.byteLength; i++) { + binary += String.fromCharCode(buffer[i]); + } + const content = btoa(binary); return content; } -- 2.47.3