Skip to content

Commit aa3b4a3

Browse files
committed
construct Blob with sliced buffer
1 parent 91a8b24 commit aa3b4a3

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

index.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ const wm = new WeakMap();
1111

1212
class Blob {
1313
/**
14-
* The Blob() constructor returns a new Blob object. The content of the blob consists of the concatenation of the values given in the parameter array.
15-
* @param {(ArrayBufferLike | ArrayBufferView | Blob | Buffer | string)[]} blobParts
16-
* @param {{ type?: string }} [options]
17-
*/
14+
* The Blob() constructor returns a new Blob object. The content of the blob consists of the concatenation of the values given in the parameter array.
15+
* @param {(ArrayBufferLike | ArrayBufferView | Blob | Buffer | string)[]} blobParts
16+
* @param {{ type?: string }} [options]
17+
*/
1818
constructor(blobParts = [], options = {type: ''}) {
1919
const buffers = blobParts.map(element => {
2020
if (Buffer.isBuffer(element)) {
2121
return element;
2222
}
2323

2424
if (ArrayBuffer.isView(element)) {
25-
return Buffer.from(element.buffer, element.byteOffset, element.byteLength);
25+
return Buffer.from(
26+
element.buffer,
27+
element.byteOffset,
28+
element.byteLength
29+
);
2630
}
2731

2832
if (types.isAnyArrayBuffer(element)) {
@@ -33,12 +37,15 @@ class Blob {
3337
return wm.get(element).buffer;
3438
}
3539

36-
return Buffer.from(typeof element === 'string' ? element : String(element));
40+
return Buffer.from(
41+
typeof element === 'string' ? element : String(element)
42+
);
3743
});
3844

3945
const buffer = Buffer.concat(buffers);
4046

41-
const type = options.type === undefined ? '' : String(options.type).toLowerCase();
47+
const type =
48+
options.type === undefined ? '' : String(options.type).toLowerCase();
4249

4350
wm.set(this, {
4451
type: /[^\u0020-\u007E]/.test(type) ? '' : type,
@@ -47,38 +54,41 @@ class Blob {
4754
}
4855

4956
/**
50-
* The Blob interface's size property returns the size of the Blob in bytes.
51-
*/
57+
* The Blob interface's size property returns the size of the Blob in bytes.
58+
*/
5259
get size() {
5360
return wm.get(this).buffer.byteLength;
5461
}
5562

5663
/**
57-
* The type property of a Blob object returns the MIME type of the file.
58-
*/
64+
* The type property of a Blob object returns the MIME type of the file.
65+
*/
5966
get type() {
6067
return wm.get(this).type;
6168
}
6269

6370
/**
64-
* The text() method in the Blob interface returns a Promise that resolves with a string containing the contents of the blob, interpreted as UTF-8.
65-
*/
71+
* The text() method in the Blob interface returns a Promise that resolves with a string containing the contents of the blob, interpreted as UTF-8.
72+
*/
6673
async text() {
6774
return wm.get(this).buffer.toString();
6875
}
6976

7077
/**
71-
* The arrayBuffer() method in the Blob interface returns a Promise that resolves with the contents of the blob as binary data contained in an ArrayBuffer.
72-
*/
78+
* The arrayBuffer() method in the Blob interface returns a Promise that resolves with the contents of the blob as binary data contained in an ArrayBuffer.
79+
*/
7380
async arrayBuffer() {
7481
const buf = wm.get(this).buffer;
75-
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
82+
const ab = buf.buffer.slice(
83+
buf.byteOffset,
84+
buf.byteOffset + buf.byteLength
85+
);
7686
return ab;
7787
}
7888

7989
/**
80-
* The Blob interface's stream() method returns a ReadableStream which upon reading returns the data contained within the Blob.
81-
*/
90+
* The Blob interface's stream() method returns a ReadableStream which upon reading returns the data contained within the Blob.
91+
*/
8292
stream() {
8393
return Readable.from(wm.get(this).buffer);
8494
}
@@ -88,19 +98,19 @@ class Blob {
8898
}
8999

90100
/**
91-
* @returns {string}
92-
*/
101+
* @returns {string}
102+
*/
93103
toString() {
94104
return Object.prototype.toString.call(this);
95105
}
96106

97107
/**
98-
* The Blob interface's slice() method creates and returns a new Blob object which contains data from a subset of the blob on which it's called.
99-
*
100-
* @param {number} [start]
101-
* @param {number} [end]
102-
* @param {string} [contentType]
103-
*/
108+
* The Blob interface's slice() method creates and returns a new Blob object which contains data from a subset of the blob on which it's called.
109+
*
110+
* @param {number} [start]
111+
* @param {number} [end]
112+
* @param {string} [contentType]
113+
*/
104114
slice(start, end, contentType) {
105115
const {size} = this;
106116

@@ -124,14 +134,10 @@ class Blob {
124134
}
125135

126136
const span = Math.max(relativeEnd - relativeStart, 0);
127-
const slicedBuffer = wm.get(this).buffer.slice(
128-
relativeStart,
129-
relativeStart + span
130-
);
131-
const blob = new Blob([], {type: contentType});
132-
const _ = wm.get(blob);
133-
_.buffer = slicedBuffer;
134-
return blob;
137+
const slicedBuffer = wm
138+
.get(this)
139+
.buffer.slice(relativeStart, relativeStart + span);
140+
return new Blob([slicedBuffer], {type: contentType});
135141
}
136142
}
137143

0 commit comments

Comments
 (0)