Skip to content

bad Buffer conversion behaviour due to Uint8Array inheritance #28725

@Fishrock123

Description

@Fishrock123

tl;dr - TypedArrays of lower-order truncate high-order values, which is is confusing (and hard to work around).

Ok so Buffer.from(new Uint32Array([0x4701c993])) returns a buffer with a length of 1 and a value in index 0 of 0x93.

This is a bad conversion and seems obviously wrong to me. An explicit 32-bit type was provided, and not just [0x4701c993] (for which the docs says interpreted as octets).

I don't possibly see how this could be the desired behavior, the resulting values are nothing like the input at all. But, here's the spec: https://www.ecma-international.org/ecma-262/6.0/#sec-touint8

Also, the Buffer#write<U><Type><Size><endian>() APIs help little, since they are awkwardly designed when used for such a purpose. (They return bytes written and not the buffer.) Presumably, if I had a whole array of int32s I'd have to write some kind of custom converter, which seems wrong considering we are converting to a runtime standard object from a primitive type.

Suggestion: fix this somehow, or, provide a nicer API to do a reasonable conversion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions