Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json parser ArrayBuffer memory leak #1657

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/serialization/json/src/jsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,19 @@ export class JsonParseNode implements ParseNode {
public getByteArrayValue(): ArrayBuffer | undefined {
const strValue = this.getStringValue();
if (strValue && strValue.length > 0) {
return inNodeEnv() ? Buffer.from(strValue, "base64").buffer : new TextEncoder().encode(strValue);
/**
* Node.js Buffer objects created via Buffer.from() use a shared memory pool
* and may be subject to reuse/recycling, which can lead to unexpected behavior.
*
* For consistent behavior across environments:
* - In Node: We convert the base64 string to a Buffer first, then create a new
* Uint8Array from it to ensure we have a stable, independent copy
* - In browsers: We convert the string directly using TextEncoder
*
* TODO: Consider standardizing on a cross-platform UInt8Array.fromBase64 (after the API is stabilized across browsers)
* in the future instead of the current environment-specific approach
*/
return inNodeEnv() ? new Uint8Array(Buffer.from(strValue, "base64")).buffer : new TextEncoder().encode(strValue);
}
return undefined;
}
Expand Down
14 changes: 13 additions & 1 deletion packages/serialization/text/src/textParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ export class TextParseNode implements ParseNode {
public getByteArrayValue(): ArrayBuffer | undefined {
const strValue = this.getStringValue();
if (strValue && strValue.length > 0) {
return inNodeEnv() ? Buffer.from(strValue, "base64").buffer : new TextEncoder().encode(strValue);
/**
* Node.js Buffer objects created via Buffer.from() use a shared memory pool
* and may be subject to reuse/recycling, which can lead to unexpected behavior.
*
* For consistent behavior across environments:
* - In Node: We convert the base64 string to a Buffer first, then create a new
* Uint8Array from it to ensure we have a stable, independent copy
* - In browsers: We convert the string directly using TextEncoder
*
* TODO: Consider standardizing on a cross-platform UInt8Array.fromBase64 (after the API is stabilized across browsers)
* in the future instead of the current environment-specific approach
*/
return inNodeEnv() ? new Uint8Array(Buffer.from(strValue, "base64")).buffer : new TextEncoder().encode(strValue);
}
return undefined;
}
Expand Down
Loading