|
2 | 2 |
|
3 | 3 | const {
|
4 | 4 | ArrayFrom,
|
| 5 | + MathMax, |
| 6 | + MathMin, |
5 | 7 | ObjectDefineProperty,
|
6 | 8 | ObjectSetPrototypeOf,
|
7 | 9 | PromiseResolve,
|
@@ -43,21 +45,21 @@ const {
|
43 | 45 | codes: {
|
44 | 46 | ERR_INVALID_ARG_TYPE,
|
45 | 47 | ERR_BUFFER_TOO_LARGE,
|
46 |
| - ERR_OUT_OF_RANGE, |
47 | 48 | }
|
48 | 49 | } = require('internal/errors');
|
49 | 50 |
|
50 | 51 | const {
|
51 | 52 | validateObject,
|
52 | 53 | validateString,
|
53 |
| - validateUint32, |
54 | 54 | isUint32,
|
55 | 55 | } = require('internal/validators');
|
56 | 56 |
|
57 | 57 | const kHandle = Symbol('kHandle');
|
58 | 58 | const kType = Symbol('kType');
|
59 | 59 | const kLength = Symbol('kLength');
|
60 | 60 |
|
| 61 | +const disallowedTypeCharacters = /[^\u{0020}-\u{007E}]/u; |
| 62 | + |
61 | 63 | let Buffer;
|
62 | 64 |
|
63 | 65 | function lazyBuffer() {
|
@@ -141,7 +143,7 @@ class Blob extends JSTransferable {
|
141 | 143 | super();
|
142 | 144 | this[kHandle] = createBlob(sources_, length);
|
143 | 145 | this[kLength] = length;
|
144 |
| - this[kType] = RegExpPrototypeTest(/[^\u{0020}-\u{007E}]/u, type) ? |
| 146 | + this[kType] = RegExpPrototypeTest(disallowedTypeCharacters, type) ? |
145 | 147 | '' : StringPrototypeToLowerCase(type);
|
146 | 148 | }
|
147 | 149 |
|
@@ -180,18 +182,32 @@ class Blob extends JSTransferable {
|
180 | 182 |
|
181 | 183 | get size() { return this[kLength]; }
|
182 | 184 |
|
183 |
| - slice(start = 0, end = (this[kLength]), type = this[kType]) { |
184 |
| - validateUint32(start, 'start'); |
185 |
| - if (end < 0) end = this[kLength] + end; |
186 |
| - validateUint32(end, 'end'); |
187 |
| - validateString(type, 'type'); |
188 |
| - if (end < start) |
189 |
| - throw new ERR_OUT_OF_RANGE('end', 'greater than start', end); |
190 |
| - if (end > this[kLength]) |
191 |
| - throw new ERR_OUT_OF_RANGE('end', 'less than or equal to length', end); |
| 185 | + slice(start = 0, end = this[kLength], contentType = '') { |
| 186 | + if (start < 0) { |
| 187 | + start = MathMax(this[kLength] + start, 0); |
| 188 | + } else { |
| 189 | + start = MathMin(start, this[kLength]); |
| 190 | + } |
| 191 | + start |= 0; |
| 192 | + |
| 193 | + if (end < 0) { |
| 194 | + end = MathMax(this[kLength] + end, 0); |
| 195 | + } else { |
| 196 | + end = MathMin(end, this[kLength]); |
| 197 | + } |
| 198 | + end |= 0; |
| 199 | + |
| 200 | + contentType = `${contentType}`; |
| 201 | + if (RegExpPrototypeTest(disallowedTypeCharacters, contentType)) { |
| 202 | + contentType = ''; |
| 203 | + } else { |
| 204 | + contentType = StringPrototypeToLowerCase(contentType); |
| 205 | + } |
| 206 | + |
| 207 | + const span = MathMax(end - start, 0); |
| 208 | + |
192 | 209 | return new InternalBlob(
|
193 |
| - this[kHandle].slice(start, end), |
194 |
| - end - start, type); |
| 210 | + this[kHandle].slice(start, start + span), span, contentType); |
195 | 211 | }
|
196 | 212 |
|
197 | 213 | async arrayBuffer() {
|
|
0 commit comments