Skip to content

Commit a2450ef

Browse files
committed
buf: move buffer.slice to runtime deprecation
Signed-off-by: hainenber <[email protected]>
1 parent 7fd3688 commit a2450ef

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3476,14 +3476,17 @@ const w = new Writable({
34763476

34773477
<!-- YAML
34783478
changes:
3479+
- version: v26.0.0
3480+
pr-url: https://github.com/nodejs/node/pull/60820
3481+
description: Runtime deprecation.
34793482
- version:
34803483
- v17.5.0
34813484
- v16.15.0
34823485
pr-url: https://github.com/nodejs/node/pull/41596
34833486
description: Documentation-only deprecation.
34843487
-->
34853488

3486-
Type: Documentation-only
3489+
Type: Runtime
34873490

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

lib/buffer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const {
8888
kIsEncodingSymbol,
8989
defineLazyProperties,
9090
encodingsMap,
91+
deprecate,
9192
} = require('internal/util');
9293
const {
9394
isAnyArrayBuffer,
@@ -1185,9 +1186,7 @@ Buffer.prototype.subarray = function subarray(start, end) {
11851186
return new FastBuffer(this.buffer, this.byteOffset + start, newLength);
11861187
};
11871188

1188-
Buffer.prototype.slice = function slice(start, end) {
1189-
return this.subarray(start, end);
1190-
};
1189+
Buffer.prototype.slice = deprecate(Buffer.prototype.subarray, 'Buffer.prototype.slice is deprecated.', 'DEP0158');
11911190

11921191
function swap(b, n, m) {
11931192
const i = b[n];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
24+
const common = require('../common');
25+
26+
common.expectWarning('DeprecationWarning',
27+
'Buffer.prototype.slice is deprecated.', 'DEP0158');
28+
29+
const buf = Buffer.from('0123456789', 'utf8');
30+
buf.slice(-10, 10);
31+
buf.slice();
32+
buf.slice('foobar');
33+
buf.slice(2);
34+
35+
Buffer.alloc(0).slice(0, 1);

0 commit comments

Comments
 (0)