Skip to content

Use subarray instead of the deprecated Buffer#slice - #35

Merged
NullVoxPopuli merged 1 commit into
ember-tooling:mainfrom
tylerturdenpants:buffer-subarray
Sep 8, 2026
Merged

NullVoxPopuli merged 1 commit into
ember-tooling:mainfrom
tylerturdenpants:buffer-subarray

Conversation

@tylerturdenpants

Copy link
Copy Markdown
Contributor

Buffer.prototype.slice is deprecated in favour of subarray, and this repo uses it in six places. From @types/node:

/** @deprecated Use `subarray` instead. */
slice(start?: number, end?: number): Buffer<ArrayBuffer>;

It got deprecated because it does not behave like the Uint8Array.prototype.slice it shadows. The Buffer one returns a view over the same memory, the TypedArray one copies. subarray is the honest name for the view, and a view is what all six call sites want, since every one of them immediately calls .toString() on the result.

No behaviour change. Both return a view over the same memory and produce identical output, so this is a rename at the call site:

const a = Buffer.from("hello world");
a.slice(2, 7).equals(a.subarray(2, 7)); // true

The two result.slice(...) calls in Transformer#toString are untouched. Those are String.prototype.slice, which is not deprecated.

tsc was already flagging every one of these, though as a suggestion rather than an error, so pnpm lint was passing and it was easy to miss. Came up in review on #33, and it does not belong in a BOM fix, so it is here on its own.

pnpm test is 70 green. pnpm lint passes lint:types, lint:package and lint:published-types.

`Buffer.prototype.slice` is deprecated in favour of `subarray`. From
`@types/node`:

    /** @deprecated Use `subarray` instead. */
    slice(start?: number, end?: number): Buffer<ArrayBuffer>;

It was deprecated because it does not behave like the
`Uint8Array.prototype.slice` it shadows: the Buffer one returns a view over
the same memory, the TypedArray one copies. `subarray` is the honest name for
the view, and a view is what every one of these call sites wants, since each
one immediately calls `.toString()` on the result.

No behaviour change. Both return a view over the same memory and produce
identical output, so this is a rename at the call site.

The two `result.slice(...)` calls in `Transformer#toString` are left alone.
Those are `String.prototype.slice`, which is not deprecated.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@NullVoxPopuli NullVoxPopuli added the bug Something isn't working label Sep 8, 2026
@NullVoxPopuli
NullVoxPopuli merged commit 441599a into ember-tooling:main Sep 8, 2026
4 checks passed
@github-actions github-actions Bot mentioned this pull request Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants