Skip to content

Commit

Permalink
buffer: deprecate buffer.slice
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingr committed Jan 19, 2022
1 parent d8f6b38 commit d1f4857
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 8 additions & 4 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ In particular:
without copying. This behavior can be surprising, and only exists for legacy
compatibility. [`TypedArray.prototype.subarray()`][] can be used to achieve
the behavior of [`Buffer.prototype.slice()`][`buf.slice()`] on both `Buffer`s
and other `TypedArray`s.
and other `TypedArray`s and should be preferred.
* [`buf.toString()`][] is incompatible with its `TypedArray` equivalent.
* A number of methods, e.g. [`buf.indexOf()`][], support additional arguments.

Expand Down Expand Up @@ -2056,7 +2056,7 @@ If `value` is:
* a string, `value` is interpreted according to the character encoding in
`encoding`.
* a `Buffer` or [`Uint8Array`][], `value` will be used in its entirety.
To compare a partial `Buffer`, use [`buf.slice()`][].
To compare a partial `Buffer`, use [`buf.subarray`][].
* a number, `value` will be interpreted as an unsigned 8-bit integer
value between `0` and `255`.

Expand Down Expand Up @@ -3389,6 +3389,9 @@ console.log(buf.subarray(-5, -2).toString());
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41596
description: The buf.slice method has been deprecated.
- version:
- v7.1.0
- v6.9.2
Expand All @@ -3406,11 +3409,11 @@ changes:
**Default:** [`buf.length`][].
* Returns: {Buffer}

> Stability: 0 - Deprecated: Use [`buf.subarray`][] instead.
Returns a new `Buffer` that references the same memory as the original, but
offset and cropped by the `start` and `end` indices.

This is the same behavior as `buf.subarray()`.

This method is not compatible with the `Uint8Array.prototype.slice()`,
which is a superclass of `Buffer`. To copy the slice, use
`Uint8Array.prototype.slice()`.
Expand Down Expand Up @@ -5355,6 +5358,7 @@ introducing security vulnerabilities into an application.
[`buf.keys()`]: #bufkeys
[`buf.length`]: #buflength
[`buf.slice()`]: #bufslicestart-end
[`buf.subarray`]: #bufsubarraystart-end
[`buf.toString()`]: #buftostringencoding-start-end
[`buf.values()`]: #bufvalues
[`buffer.constants.MAX_LENGTH`]: #bufferconstantsmax_length
Expand Down
16 changes: 16 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,22 @@ const w = new Writable({
});
```

### DEP0158: buffer.slice(start, end)

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/40860
description: Documentation-only deprecation.
-->

Type: Documentation-only

This method was deprecated because it is not compatible with the
`Uint8Array.prototype.slice()`, which is a superclass of `Buffer`.

Use `buffer.subarray(start, end)` which does the same thing instead.

[Legacy URL API]: url.md#legacy-url-api
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
Expand Down

0 comments on commit d1f4857

Please sign in to comment.