Skip to content

Commit 8347b35

Browse files
committed
Fix Core.toFloat32Array, Buffer version
According to NodeJS docs Buffer.buffer is not guaranteed to correspond exactly to the original Buffer. [1] The previous implementation could use buffer garbage while converting bytes to floats. [1] https://nodejs.org/api/buffer.html#bufbuffer
1 parent e080e12 commit 8347b35

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,8 @@ export const toBase64 = (str: string | null | undefined): string => {
12951295
export const toFloat32Array = (base64Str: string): Array<number> => {
12961296
if (typeof Buffer !== 'undefined') {
12971297
// for Node.js environment
1298-
return Array.from(new Float32Array(Buffer.from(base64Str, 'base64').buffer));
1298+
const buf = Buffer.from(base64Str, 'base64');
1299+
return Array.from(new Float32Array(buf.buffer, buf.byteOffset, buf.length / Float32Array.BYTES_PER_ELEMENT));
12991300
} else {
13001301
// for legacy web platform APIs
13011302
const binaryStr = atob(base64Str);

0 commit comments

Comments
 (0)