@@ -1,6 +1,6 @@
|
||||
import Blob from './index.js';
|
||||
|
||||
export default class File extends Blob {
|
||||
const _File = class File extends Blob {
|
||||
#lastModified = 0;
|
||||
#name = '';
|
||||
|
||||
@@ -8,7 +8,7 @@ export default class File extends Blob {
|
||||
* @param {*[]} fileBits
|
||||
* @param {string} fileName
|
||||
* @param {{lastModified?: number, type?: string}} options
|
||||
*/ // @ts-ignore
|
||||
*/// @ts-ignore
|
||||
constructor(fileBits, fileName, options = {}) {
|
||||
if (arguments.length < 2) {
|
||||
throw new TypeError(`Failed to construct 'File': 2 arguments required, but only ${arguments.length} present.`);
|
||||
@@ -33,4 +33,6 @@ export default class File extends Blob {
|
||||
}
|
||||
}
|
||||
|
||||
export { File };
|
||||
/** @type {typeof globalThis.File} */// @ts-ignore
|
||||
export const File = _File;
|
||||
export default File;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import {statSync, createReadStream, promises as fs} from 'fs';
|
||||
import {basename} from 'path';
|
||||
import {MessageChannel} from 'worker_threads';
|
||||
|
||||
import File from './file.js';
|
||||
import Blob from './index.js';
|
||||
import {MessageChannel} from 'worker_threads';
|
||||
|
||||
const {stat} = fs;
|
||||
|
||||
@@ -37,6 +38,7 @@ const fileFrom = (path, type) => stat(path).then(stat => fromFile(stat, path, ty
|
||||
*/
|
||||
const fileFromSync = (path, type) => fromFile(statSync(path), path, type);
|
||||
|
||||
// @ts-ignore
|
||||
const fromBlob = (stat, path, type = '') => new Blob([new BlobDataItem({
|
||||
path,
|
||||
size: stat.size,
|
||||
@@ -44,6 +46,7 @@ const fromBlob = (stat, path, type = '') => new Blob([new BlobDataItem({
|
||||
start: 0
|
||||
})], {type});
|
||||
|
||||
// @ts-ignore
|
||||
const fromFile = (stat, path, type = '') => new File([new BlobDataItem({
|
||||
path,
|
||||
size: stat.size,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
// TODO (jimmywarting): in the feature use conditional loading with top level await (requires 14.x)
|
||||
// Node has recently added whatwg stream into core, want to use that instead when it becomes available.
|
||||
// Node has recently added whatwg stream into core
|
||||
|
||||
import './streams.cjs';
|
||||
|
||||
@@ -21,8 +21,8 @@ async function * toIterator (parts, clone = true) {
|
||||
while (position !== end) {
|
||||
const size = Math.min(end - position, POOL_SIZE);
|
||||
const chunk = part.buffer.slice(position, position + size);
|
||||
yield new Uint8Array(chunk);
|
||||
position += chunk.byteLength;
|
||||
yield new Uint8Array(chunk);
|
||||
}
|
||||
} else {
|
||||
yield part;
|
||||
@@ -42,7 +42,7 @@ async function * toIterator (parts, clone = true) {
|
||||
}
|
||||
}
|
||||
|
||||
export default class Blob {
|
||||
const _Blob = class Blob {
|
||||
|
||||
/** @type {Array.<(Blob|Uint8Array)>} */
|
||||
#parts = [];
|
||||
@@ -69,7 +69,7 @@ export default class Blob {
|
||||
} else if (element instanceof Blob) {
|
||||
part = element;
|
||||
} else {
|
||||
part = new TextEncoder().encode(String(element));
|
||||
part = new TextEncoder().encode(element);
|
||||
}
|
||||
|
||||
size += ArrayBuffer.isView(part) ? part.byteLength : part.size;
|
||||
@@ -224,10 +224,12 @@ export default class Blob {
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperties(Blob.prototype, {
|
||||
Object.defineProperties(_Blob.prototype, {
|
||||
size: {enumerable: true},
|
||||
type: {enumerable: true},
|
||||
slice: {enumerable: true}
|
||||
});
|
||||
|
||||
export { Blob };
|
||||
/** @type {typeof globalThis.Blob} */
|
||||
export const Blob = _Blob;
|
||||
export default Blob;
|
||||
|
||||
+4
-1
@@ -1,10 +1,12 @@
|
||||
/* c8 ignore start */
|
||||
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
|
||||
const POOL_SIZE = 65536;
|
||||
|
||||
if (!globalThis.ReadableStream) {
|
||||
try {
|
||||
Object.assign(globalThis, require('streasm/web'))
|
||||
Object.assign(globalThis, require('stream/web'))
|
||||
} catch (error) {
|
||||
// TODO: Remove when only supporting node >= 16.5.0
|
||||
Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js'))
|
||||
}
|
||||
}
|
||||
@@ -32,3 +34,4 @@ try {
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
/* c8 ignore end */
|
||||
|
||||
@@ -350,3 +350,15 @@ test('can slice zero sized blobs', async t => {
|
||||
const txt = await blob.slice(0, 0).text();
|
||||
t.is(txt, '');
|
||||
});
|
||||
|
||||
test('returns a readable stream', t => {
|
||||
const stream = new File([], '').stream();
|
||||
t.true(typeof stream.getReader === 'function');
|
||||
});
|
||||
|
||||
test('checking instanceof blob#stream', async t => {
|
||||
// eslint-disable-next-line node/no-unsupported-features/es-syntax
|
||||
const {ReadableStream} = await import('stream/web').catch(_ => import('web-streams-polyfill/dist/ponyfill.es2018.js'));
|
||||
const stream = new File([], '').stream();
|
||||
t.true(stream instanceof ReadableStream);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user