Skip to content

Commit 2c6fb79

Browse files
authored
Fix json parser ArrayBuffer memory leak
1 parent 3cd0e29 commit 2c6fb79

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

packages/serialization/json/src/jsonParseNode.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,19 @@ export class JsonParseNode implements ParseNode {
4848
public getByteArrayValue(): ArrayBuffer | undefined {
4949
const strValue = this.getStringValue();
5050
if (strValue && strValue.length > 0) {
51-
return inNodeEnv() ? Buffer.from(strValue, "base64").buffer : new TextEncoder().encode(strValue);
51+
/**
52+
* Node.js Buffer objects created via Buffer.from() use a shared memory pool
53+
* and may be subject to reuse/recycling, which can lead to unexpected behavior.
54+
*
55+
* For consistent behavior across environments:
56+
* - In Node: We convert the base64 string to a Buffer first, then create a new
57+
* Uint8Array from it to ensure we have a stable, independent copy
58+
* - In browsers: We convert the string directly using TextEncoder
59+
*
60+
* TODO: Consider standardizing on a cross-platform UInt8Array.fromBase64 (after the API is stabilized across browsers)
61+
* in the future instead of the current environment-specific approach
62+
*/
63+
return inNodeEnv() ? new Uint8Array(Buffer.from(strValue, "base64")).buffer : new TextEncoder().encode(strValue);
5264
}
5365
return undefined;
5466
}

0 commit comments

Comments
 (0)