Revamp (#83)
* See CHANGELOG for v3.0.0 * removed get-stream * enabled linter just for test.js & from.js * Any blob part is acceptable (jsdoc) since the default fallback is to cast unknown items into strings * async stat version * import from fs/promise instead * Updated the Readme and code examples * known differences, buffer.Blob support, private stream, more test * require node 14 * use fetch v3
This commit is contained in:
@@ -1,56 +1,69 @@
|
||||
const {statSync, createReadStream} = require('fs');
|
||||
const Blob = require('./index.js');
|
||||
const DOMException = require('domexception');
|
||||
import {statSync, createReadStream} from 'fs';
|
||||
import {stat} from 'fs/promises';
|
||||
import DOMException from 'domexception';
|
||||
import Blob from './index.js';
|
||||
|
||||
/**
|
||||
* @param {string} path filepath on the disk
|
||||
* @returns {Blob}
|
||||
*/
|
||||
function blobFrom(path) {
|
||||
const {size, mtime} = statSync(path);
|
||||
const blob = new BlobDataItem({path, size, mtime});
|
||||
const blobFromSync = path => from(statSync(path), path);
|
||||
|
||||
return new Blob([blob]);
|
||||
}
|
||||
/**
|
||||
* @param {string} path filepath on the disk
|
||||
* @returns {Promise<Blob>}
|
||||
*/
|
||||
const blobFrom = path => stat(path).then(stat => from(stat, path));
|
||||
|
||||
const from = (stat, path) => new Blob([new BlobDataItem({
|
||||
path,
|
||||
size: stat.size,
|
||||
lastModified: stat.mtimeMs,
|
||||
start: 0
|
||||
})]);
|
||||
|
||||
/**
|
||||
* This is a blob backed up by a file on the disk
|
||||
* with minium requirement
|
||||
* with minium requirement. Its wrapped around a Blob as a blobPart
|
||||
* so you have no direct access to this.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
class BlobDataItem {
|
||||
#path;
|
||||
#start;
|
||||
|
||||
constructor(options) {
|
||||
this.#path = options.path;
|
||||
this.#start = options.start;
|
||||
this.size = options.size;
|
||||
this.path = options.path;
|
||||
this.start = options.start || 0;
|
||||
this.mtime = options.mtime;
|
||||
this.lastModified = options.lastModified
|
||||
}
|
||||
|
||||
// Slicing arguments is first validated and formated
|
||||
// to not be out of range by Blob.prototype.slice
|
||||
/**
|
||||
* Slicing arguments is first validated and formatted
|
||||
* to not be out of range by Blob.prototype.slice
|
||||
*/
|
||||
slice(start, end) {
|
||||
return new BlobDataItem({
|
||||
path: this.path,
|
||||
start,
|
||||
mtime: this.mtime,
|
||||
size: end - start
|
||||
path: this.#path,
|
||||
lastModified: this.lastModified,
|
||||
size: end - start,
|
||||
start
|
||||
});
|
||||
}
|
||||
|
||||
stream() {
|
||||
if (statSync(this.path).mtime > this.mtime) {
|
||||
async * stream() {
|
||||
const {mtimeMs} = await stat(this.#path)
|
||||
if (mtimeMs > this.lastModified) {
|
||||
throw new DOMException('The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.', 'NotReadableError');
|
||||
}
|
||||
|
||||
if (!this.size) {
|
||||
return new Blob().stream();
|
||||
if (this.size) {
|
||||
yield * createReadStream(this.#path, {
|
||||
start: this.#start,
|
||||
end: this.#start + this.size - 1
|
||||
});
|
||||
}
|
||||
|
||||
return createReadStream(this.path, {
|
||||
start: this.start,
|
||||
end: this.start + this.size - 1
|
||||
});
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag]() {
|
||||
@@ -58,4 +71,5 @@ class BlobDataItem {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = blobFrom;
|
||||
export default blobFromSync;
|
||||
export {Blob, blobFrom, blobFromSync};
|
||||
|
||||
Reference in New Issue
Block a user