Skip to content

Commit e00f849

Browse files
authored
improve typing (#105)
* improve typing * bug fix * don't test cjs
1 parent 52f296c commit e00f849

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

file.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Blob from './index.js';
22

3-
export default class File extends Blob {
3+
const _File = class File extends Blob {
44
#lastModified = 0;
55
#name = '';
66

77
/**
88
* @param {*[]} fileBits
99
* @param {string} fileName
1010
* @param {{lastModified?: number, type?: string}} options
11-
*/ // @ts-ignore
11+
*/// @ts-ignore
1212
constructor(fileBits, fileName, options = {}) {
1313
if (arguments.length < 2) {
1414
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 {
3333
}
3434
}
3535

36-
export { File };
36+
/** @type {typeof globalThis.File} */// @ts-ignore
37+
export const File = _File;
38+
export default File;

from.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {statSync, createReadStream, promises as fs} from 'fs';
22
import {basename} from 'path';
3+
import {MessageChannel} from 'worker_threads';
4+
35
import File from './file.js';
46
import Blob from './index.js';
5-
import {MessageChannel} from 'worker_threads';
67

78
const {stat} = fs;
89

@@ -37,13 +38,15 @@ const fileFrom = (path, type) => stat(path).then(stat => fromFile(stat, path, ty
3738
*/
3839
const fileFromSync = (path, type) => fromFile(statSync(path), path, type);
3940

41+
// @ts-ignore
4042
const fromBlob = (stat, path, type = '') => new Blob([new BlobDataItem({
4143
path,
4244
size: stat.size,
4345
lastModified: stat.mtimeMs,
4446
start: 0
4547
})], {type});
4648

49+
// @ts-ignore
4750
const fromFile = (stat, path, type = '') => new File([new BlobDataItem({
4851
path,
4952
size: stat.size,

index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// TODO (jimmywarting): in the feature use conditional loading with top level await (requires 14.x)
3-
// Node has recently added whatwg stream into core, want to use that instead when it becomes available.
3+
// Node has recently added whatwg stream into core
44

55
import './streams.cjs';
66

@@ -21,8 +21,8 @@ async function * toIterator (parts, clone = true) {
2121
while (position !== end) {
2222
const size = Math.min(end - position, POOL_SIZE);
2323
const chunk = part.buffer.slice(position, position + size);
24-
yield new Uint8Array(chunk);
2524
position += chunk.byteLength;
25+
yield new Uint8Array(chunk);
2626
}
2727
} else {
2828
yield part;
@@ -42,7 +42,7 @@ async function * toIterator (parts, clone = true) {
4242
}
4343
}
4444

45-
export default class Blob {
45+
const _Blob = class Blob {
4646

4747
/** @type {Array.<(Blob|Uint8Array)>} */
4848
#parts = [];
@@ -69,7 +69,7 @@ export default class Blob {
6969
} else if (element instanceof Blob) {
7070
part = element;
7171
} else {
72-
part = new TextEncoder().encode(String(element));
72+
part = new TextEncoder().encode(element);
7373
}
7474

7575
size += ArrayBuffer.isView(part) ? part.byteLength : part.size;
@@ -224,10 +224,12 @@ export default class Blob {
224224
}
225225
}
226226

227-
Object.defineProperties(Blob.prototype, {
227+
Object.defineProperties(_Blob.prototype, {
228228
size: {enumerable: true},
229229
type: {enumerable: true},
230230
slice: {enumerable: true}
231231
});
232232

233-
export { Blob };
233+
/** @type {typeof globalThis.Blob} */
234+
export const Blob = _Blob;
235+
export default Blob;

streams.cjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
/* c8 ignore start */
12
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
23
const POOL_SIZE = 65536;
34

45
if (!globalThis.ReadableStream) {
56
try {
6-
Object.assign(globalThis, require('streasm/web'))
7+
Object.assign(globalThis, require('stream/web'))
78
} catch (error) {
9+
// TODO: Remove when only supporting node >= 16.5.0
810
Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js'))
911
}
1012
}
@@ -32,3 +34,4 @@ try {
3234
}
3335
}
3436
} catch (error) {}
37+
/* c8 ignore end */

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,15 @@ test('can slice zero sized blobs', async t => {
350350
const txt = await blob.slice(0, 0).text();
351351
t.is(txt, '');
352352
});
353+
354+
test('returns a readable stream', t => {
355+
const stream = new File([], '').stream();
356+
t.true(typeof stream.getReader === 'function');
357+
});
358+
359+
test('checking instanceof blob#stream', async t => {
360+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
361+
const {ReadableStream} = await import('stream/web').catch(_ => import('web-streams-polyfill/dist/ponyfill.es2018.js'));
362+
const stream = new File([], '').stream();
363+
t.true(stream instanceof ReadableStream);
364+
});

0 commit comments

Comments
 (0)