File tree 1 file changed +13
-1
lines changed
packages/serialization/json/src
1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -48,7 +48,19 @@ export class JsonParseNode implements ParseNode {
48
48
public getByteArrayValue ( ) : ArrayBuffer | undefined {
49
49
const strValue = this . getStringValue ( ) ;
50
50
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 ) ;
52
64
}
53
65
return undefined ;
54
66
}
You can’t perform that action at this time.
0 commit comments