feross / feross/buffer

Buffer.alloc with non-buffer fill

Open
#278 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.9k
Forks
260
PR merge metrics
No merged PRs in 30d

Description

Hi @feross!

If you pass a `` as the `fill` to `Buffer.alloc(size[, fill[, encoding]])` node (v14.15.1) will copy those bytes verbatim into the buffer, whereas with the `ferros/buffer` package, it does *something* and the end result is that the resulting buffer is filled with strange data.

```js
const input = [ 247, 108, 225 ]
const typedArr = new Uint8Array(input)
const output = Array.from(Buffer.alloc(typedArr.byteLength, typedArr))

// node:
// [ 247, 108, 225 ] (matches input, expected)

// feross/buffer:
//  [ 239, 191, 189 ] (???)
```

It works fine for numbers between 0 and 127, but things get weird once you're out of those bounds (Uint8 should work for numbers between 0 - 255).

Workaround is to pass a `Buffer` as the `fill` value:

```JS
const output = Array.from(Buffer.alloc(typedArr.byteLength, Buffer.from(typedArr)))

// node:
// [ 247, 108, 225 ] (cool)

// feross/buffer:
// [ 247, 108, 225 ] (cool)
```

I'd perhaps PR a fix for this specific issue, but I feel like the real problem is that this package needs to be refreshed with the latest buffer code + tests from node, rather than me patching in a specific fix? i.e. Perhaps related to #177. I looked into this briefly and the transform from node looked non-trivial if you haven't done it before.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.