Skip to content

Commit 9b95c41

Browse files
Ms2geraduh95
authored andcommitted
lib: fix sequence argument handling in Blob constructor
This uses the existing WebIDL infrastructure to handle the iteration over the argument correctly according to the specification. Note that we can't avoid looping over the input twice: we only know the value of the 'endings' option after converting the blob parts into an array. PR-URL: #62179 Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com>
1 parent 6b7280b commit 9b95c41

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

lib/internal/blob.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {
4-
ArrayFrom,
54
MathMax,
65
MathMin,
76
ObjectDefineProperties,
@@ -15,7 +14,6 @@ const {
1514
StringPrototypeSplit,
1615
StringPrototypeToLowerCase,
1716
Symbol,
18-
SymbolIterator,
1917
SymbolToStringTag,
2018
Uint8Array,
2119
} = primordials;
@@ -54,12 +52,15 @@ const {
5452
lazyDOMException,
5553
} = require('internal/util');
5654
const { inspect } = require('internal/util/inspect');
57-
const { convertToInt } = require('internal/webidl');
55+
const {
56+
converters,
57+
convertToInt,
58+
createSequenceConverter,
59+
} = require('internal/webidl');
5860

5961
const {
6062
codes: {
6163
ERR_BUFFER_TOO_LARGE,
62-
ERR_INVALID_ARG_TYPE,
6364
ERR_INVALID_ARG_VALUE,
6465
ERR_INVALID_STATE,
6566
ERR_INVALID_THIS,
@@ -112,7 +113,6 @@ function getSource(source, endings) {
112113
if (isAnyArrayBuffer(source)) {
113114
source = new Uint8Array(source);
114115
} else if (!isArrayBufferView(source)) {
115-
source = `${source}`;
116116
if (endings === 'native')
117117
source = RegExpPrototypeSymbolReplace(/\n|\r\n/g, source, EOL);
118118
source = enc.encode(source);
@@ -126,6 +126,13 @@ function getSource(source, endings) {
126126
return [byteLength, new Uint8Array(slice)];
127127
}
128128

129+
const sourcesConverter = createSequenceConverter((source, opts = kEmptyObject) => {
130+
if (isBlob(source) || isAnyArrayBuffer(source) || isArrayBufferView(source)) {
131+
return source;
132+
}
133+
return converters.DOMString(source, opts);
134+
});
135+
129136
class Blob {
130137
/**
131138
* @typedef {string|ArrayBuffer|ArrayBufferView|Blob} SourcePart
@@ -142,11 +149,8 @@ class Blob {
142149
constructor(sources = [], options) {
143150
markTransferMode(this, true, false);
144151

145-
if (sources === null ||
146-
typeof sources[SymbolIterator] !== 'function' ||
147-