Skip to content

Commit

Permalink
buffer: fix Blob constructor on various TypedArrays
Browse files Browse the repository at this point in the history
Fixes: nodejs#40705
    
PR-URL: nodejs#40706
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Gozala authored and Linkgoron committed Jan 31, 2022
1 parent c7363f4 commit dc9d597
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ function getSource(source, endings) {

// We copy into a new Uint8Array because the underlying
// BackingStores are going to be detached and owned by
// the Blob. We also don't want to have to worry about
// byte offsets.
source = new Uint8Array(source);
return [source.byteLength, source];
// the Blob.
const { buffer, byteOffset, byteLength } = source;
const slice = buffer.slice(byteOffset, byteOffset + byteLength);
return [byteLength, new Uint8Array(slice)];
}

class Blob {
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,18 @@ assert.throws(() => new Blob({}), {
});
});
}

(async () => {
const blob = new Blob([
new Uint8Array([0x50, 0x41, 0x53, 0x53]),
new Int8Array([0x50, 0x41, 0x53, 0x53]),
new Uint16Array([0x4150, 0x5353]),
new Int16Array([0x4150, 0x5353]),
new Uint32Array([0x53534150]),
new Int32Array([0x53534150]),
new Float32Array([0xD341500000]),
]);

assert.strictEqual(blob.size, 28);
assert.strictEqual(blob.type, '');
})().then(common.mustCall());

0 comments on commit dc9d597

Please sign in to comment.