Skip to content

new Blob([new Uint16Array([1])]) has size 1 instead of 2 #40705

Closed
@Gozala

Description

Version

v16.13.0

Platform

Darwin vashli.local 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

If you run following with node 16.3

node -e 'assert.equal(new (require("buffer").Blob)([new Uint16Array([1])]).size, 2)'

Produces output

node:assert:123
  throw new AssertionError(obj);

AssertionError [ERR_ASSERTION]: 1 == 2
    at [eval]:1:8
    at Script.runInThisContext (node:vm:129:12)
    at Object.runInThisContext (node:vm:305:38)
    at node:internal/process/execution:81:19
    at [eval]-wrapper:6:22
    at evalScript (node:internal/process/execution:80:60)
    at node:internal/main/eval_string:27:3 {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: 1,
  expected: 2,
  operator: '=='
}

Which mismatches expected Blob behavior

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

assert should pass and generally blob behavior should match one from web

What do you see instead?

No response

Additional information

Issue with the following line

node/lib/internal/blob.js

Lines 109 to 128 in dd52c05

function getSource(source, endings) {
if (isBlob(source))
return [source.size, source[kHandle]];
if (isAnyArrayBuffer(source)) {
source = new Uint8Array(source);
} else if (!isArrayBufferView(source)) {
source = `${source}`;
if (endings === 'native')
source = RegExpPrototypeSymbolReplace(/\n|\r\n/g, source, lazyEOL());
source = enc.encode(source);
}
// 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];
}

Which should return following instead

  source = new Uint8Array(source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength));
  return [source.byteLength, source];

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions