File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
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
}
Original file line number Diff line number Diff line change @@ -20,7 +20,19 @@ export class TextParseNode implements ParseNode {
20
20
public getByteArrayValue ( ) : ArrayBuffer | undefined {
21
21
const strValue = this . getStringValue ( ) ;
22
22
if ( strValue && strValue . length > 0 ) {
23
- return inNodeEnv ( ) ? Buffer . from ( strValue , "base64" ) . buffer : new TextEncoder ( ) . encode ( strValue ) ;
23
+ /**
24
+ * Node.js Buffer objects created via Buffer.from() use a shared memory pool
25
+ * and may be subject to reuse/recycling, which can lead to unexpected behavior.
26
+ *
27
+ * For consistent behavior across environments:
28
+ * - In Node: We convert the base64 string to a Buffer first, then create a new
29
+ * Uint8Array from it to ensure we have a stable, independent copy
30
+ * - In browsers: We convert the string directly using TextEncoder
31
+ *
32
+ * TODO: Consider standardizing on a cross-platform UInt8Array.fromBase64 (after the API is stabilized across browsers)
33
+ * in the future instead of the current environment-specific approach
34
+ */
35
+ return inNodeEnv ( ) ? new Uint8Array ( Buffer . from ( strValue , "base64" ) ) . buffer : new TextEncoder ( ) . encode ( strValue ) ;
24
36
}
25
37
return undefined ;
26
38
}
You can’t perform that action at this time.
0 commit comments