1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(node/buffer): make slice be the same as subarray (#19481)

This commit is contained in:
Leo Kettmeir 2023-06-13 21:26:28 +02:00 committed by GitHub
parent 7e81d3c876
commit 92e7287f4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 25 deletions

View file

@ -47,6 +47,7 @@ util::unit_test_factory!(
_fs_watch_test = _fs / _fs_watch_test,
_fs_write_test = _fs / _fs_write_test,
async_hooks_test,
buffer_test,
child_process_test,
crypto_cipher_test = crypto / crypto_cipher_test,
crypto_hash_test = crypto / crypto_hash_test,

View file

@ -0,0 +1,12 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { Buffer } from "node:buffer";
import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
Deno.test({
name: "[node/buffer] slice with infinity returns empty buffer",
fn() {
const buf = Buffer.from([1, 2, 3, 4, 5]);
assertEquals(buf.slice(Infinity).length, 0);
},
});

View file

@ -860,31 +860,7 @@ function _hexSlice(buf, start, end) {
}
Buffer.prototype.slice = function slice(start, end) {
const len = this.length;
start = ~~start;
end = end === void 0 ? len : ~~end;
if (start < 0) {
start += len;
if (start < 0) {
start = 0;
}
} else if (start > len) {
start = len;
}
if (end < 0) {
end += len;
if (end < 0) {
end = 0;
}
} else if (end > len) {
end = len;
}
if (end < start) {
end = start;
}
const newBuf = this.subarray(start, end);
Object.setPrototypeOf(newBuf, Buffer.prototype);
return newBuf;
return this.subarray(start, end);
};
Buffer.prototype.readUintLE = Buffer.prototype.readUIntLE = function readUIntLE(