|
1 | 1 | import fs from 'node:fs/promises' |
2 | 2 | import { join } from 'node:path' |
3 | | -import 'node-domexception' |
4 | | -import Blob from 'fetch-blob' |
5 | | -import { fileFrom } from 'fetch-blob/from.js' |
6 | 3 | import { errors } from '../util.js' |
7 | | -// import mime from 'mime-types' |
| 4 | + |
| 5 | +import config from '../config.js' |
| 6 | + |
| 7 | +const { |
| 8 | + DOMException |
| 9 | +} = config |
8 | 10 |
|
9 | 11 | const { INVALID, GONE, MISMATCH, MOD_ERR, SYNTAX } = errors |
10 | 12 |
|
11 | | -export class Sink { |
| 13 | +/** |
| 14 | + * @see https://github.com/node-fetch/fetch-blob/blob/0455796ede330ecffd9eb6b9fdf206cc15f90f3e/index.js#L232 |
| 15 | + * @param {*} object |
| 16 | + * @returns {object is Blob} |
| 17 | + */ |
| 18 | +function isBlob (object) { |
| 19 | + return ( |
| 20 | + object && |
| 21 | + typeof object === 'object' && |
| 22 | + typeof object.constructor === 'function' && |
| 23 | + ( |
| 24 | + typeof object.stream === 'function' || |
| 25 | + typeof object.arrayBuffer === 'function' |
| 26 | + ) && |
| 27 | + /^(Blob|File)$/.test(object[Symbol.toStringTag]) |
| 28 | + ) |
| 29 | +} |
12 | 30 |
|
| 31 | +export class Sink { |
13 | 32 | /** |
14 | 33 | * @param {fs.FileHandle} fileHandle |
15 | 34 | * @param {number} size |
@@ -65,7 +84,7 @@ export class Sink { |
65 | 84 | chunk = new Uint8Array(chunk) |
66 | 85 | } else if (typeof chunk === 'string') { |
67 | 86 | chunk = Buffer.from(chunk) |
68 | | - } else if (chunk instanceof Blob) { |
| 87 | + } else if (isBlob(chunk)) { |
69 | 88 | for await (const data of chunk.stream()) { |
70 | 89 | const res = await this._fileHandle.writev([data], this._position) |
71 | 90 | this._position += res.bytesWritten |
@@ -101,6 +120,9 @@ export class FileHandle { |
101 | 120 | await fs.stat(this._path).catch(err => { |
102 | 121 | if (err.code === 'ENOENT') throw new DOMException(...GONE) |
103 | 122 | }) |
| 123 | + |
| 124 | + // TODO: replace once https://github.com/nodejs/node/issues/37340 is fixed |
| 125 | + const { fileFrom } = await import('fetch-blob/from.js') |
104 | 126 | return fileFrom(this._path) |
105 | 127 | } |
106 | 128 |
|
|
0 commit comments