Skip to content

improve typing #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions file.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Blob from './index.js';

export default class File extends Blob {
const _File = class File extends Blob {
#lastModified = 0;
#name = '';

/**
* @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.`);
Expand All @@ -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;
5 changes: 4 additions & 1 deletion from.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -37,13 +38,15 @@ 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,
lastModified: stat.mtimeMs,
start: 0
})], {type});

// @ts-ignore
const fromFile = (stat, path, type = '') => new File([new BlobDataItem({
path,
size: stat.size,
Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand All @@ -42,7 +42,7 @@ async function * toIterator (parts, clone = true) {
}
}

export default class Blob {
const _Blob = class Blob {

/** @type {Array.<(Blob|Uint8Array)>} */
#parts = [];
Expand All @@ -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;
Expand Down Expand Up @@ -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;
5 changes: 4 additions & 1 deletion streams.cjs
Original file line number Diff line number Diff line change
@@ -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'))
}
}
Expand Down Expand Up @@ -32,3 +34,4 @@ try {
}
}
} catch (error) {}
/* c8 ignore end */
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});