Skip to content

Commit a25f2cc

Browse files
committed
Fix offset ignored in typed array
Fixes peerjs issue 715
1 parent c4231da commit a25f2cc

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/binarypack.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,16 @@ Packer.prototype.pack = function (value) {
300300
}
301301
} else if ('BYTES_PER_ELEMENT' in value) {
302302
if (binaryFeatures.useArrayBufferView) {
303-
this.pack_bin(new Uint8Array(value.buffer));
303+
this.pack_bin(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
304304
} else {
305-
this.pack_bin(value.buffer);
305+
// Check if the typed array is a view over a larger ArrayBuffer
306+
if (value.byteOffset !== 0 || value.byteLength !== value.buffer.byteLength) {
307+
// If it is, copy it and use the new buffer
308+
this.pack_bin(new Uint8Array(value).buffer);
309+
} else {
310+
// Otherwise use the buffer directly
311+
this.pack_bin(value.buffer);
312+
}
306313
}
307314
} else if ((constructor == Object) || (constructor.toString().startsWith('class'))) {
308315
this.pack_object(value);

0 commit comments

Comments
 (0)