Skip to content

Commit acbd0be

Browse files
authored
buffer: faster type check
Also add support for any TypedArray as target. PR-URL: #54088 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 358ff74 commit acbd0be

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/buffer.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const {
2525
Array,
26+
ArrayBufferIsView,
2627
ArrayIsArray,
2728
ArrayPrototypeForEach,
2829
MathFloor,
@@ -202,9 +203,9 @@ function toInteger(n, defaultVal) {
202203
}
203204

204205
function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
205-
if (!isUint8Array(source))
206+
if (!ArrayBufferIsView(source))
206207
throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source);
207-
if (!isUint8Array(target))
208+
if (!ArrayBufferIsView(target))
208209
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
209210

210211
if (targetStart === undefined) {
@@ -219,30 +220,30 @@ function copyImpl(source, target, targetStart, sourceStart, sourceEnd) {
219220
sourceStart = 0;
220221
} else {
221222
sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0);
222-
if (sourceStart < 0 || sourceStart > source.length)
223-
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.length}`, sourceStart);
223+
if (sourceStart < 0 || sourceStart > source.byteLength)
224+
throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart);
224225
}
225226

226227
if (sourceEnd === undefined) {
227-
sourceEnd = source.length;
228+
sourceEnd = source.byteLength;
228229
} else {
229230
sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0);
230231
if (sourceEnd < 0)
231232
throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd);
232233
}
233234

234-
if (targetStart >= target.length || sourceStart >= sourceEnd)
235+
if (targetStart >= target.byteLength || sourceStart >= sourceEnd)
235236
return 0;
236237

237238
return _copyActual(source, target, targetStart, sourceStart, sourceEnd);
238239
}
239240

240241
function _copyActual(source, target, targetStart, sourceStart, sourceEnd) {
241-
if (sourceEnd - sourceStart > target.length - targetStart)
242-
sourceEnd = sourceStart + target.length - targetStart;
242+
if (sourceEnd - sourceStart > target.byteLength - targetStart)
243+
sourceEnd = sourceStart + target.byteLength - targetStart;
243244

244245
let nb = sourceEnd - sourceStart;
245-
const sourceLen = source.length - sourceStart;
246+
const sourceLen = source.byteLength - sourceStart;
246247
if (nb > sourceLen)
247248
nb = sourceLen;
248249

0 commit comments

Comments
 (0)