Comparing version 1.10.0 to 1.11.0
342
index.js
const { sep, resolve, isAbsolute, toNamespacedPath } = require('path') | ||
const { Readable, Writable, getStreamError } = require('streamx') | ||
const { Readable, Writable } = require('streamx') | ||
const binding = require('./binding') | ||
@@ -529,3 +529,3 @@ | ||
function mkdirp (path, mode, cb) { | ||
function mkdirRecursive (path, mode, cb) { | ||
mkdir(path, { mode }, function (err) { | ||
@@ -547,3 +547,3 @@ if (err === null) return cb(null, 0, null) | ||
mkdirp(path.slice(0, i), mode, function (err) { | ||
mkdirRecursive(path.slice(0, i), mode, function (err) { | ||
if (err) return cb(err, err.errno, null) | ||
@@ -572,3 +572,3 @@ mkdir(path, { mode }, cb) | ||
if (opts.recursive) return mkdirp(path, mode, cb) | ||
if (opts.recursive) return mkdirRecursive(path, mode, cb) | ||
@@ -580,3 +580,3 @@ const req = getReq() | ||
function mkdirpSync (path, mode) { | ||
function mkdirResursiveSync (path, mode) { | ||
try { | ||
@@ -593,3 +593,3 @@ mkdirSync(path, { mode }) | ||
mkdirpSync(path.slice(0, i), { mode }) | ||
mkdirResursiveSync(path.slice(0, i), { mode }) | ||
mkdirSync(path, { mode }) | ||
@@ -609,3 +609,3 @@ } | ||
if (opts.recursive) return mkdirpSync(path, mode) | ||
if (opts.recursive) return mkdirResursiveSync(path, mode) | ||
@@ -637,2 +637,103 @@ binding.mkdirSync(path, mode) | ||
function rmRecursive (path, opts, cb) { | ||
rmdir(path, function (err) { | ||
if (err === null) return cb(null) | ||
if (err.code !== 'ENOTEMPTY') return cb(err) | ||
readdir(path, function (err, files) { | ||
if (err) return cb(err) | ||
if (files.length === 0) return rmdir(path, cb) | ||
let missing = files.length | ||
let done = false | ||
for (const file of files) { | ||
rm(path + sep + file, opts, function (err) { | ||
if (done) return | ||
if (err) { | ||
done = true | ||
return cb(err) | ||
} | ||
if (--missing === 0) rmdir(path, cb) | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
function rm (path, opts, cb) { | ||
if (typeof path !== 'string') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')') | ||
} | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} else if (typeof cb !== 'function') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')') | ||
} | ||
if (!opts) opts = {} | ||
lstat(path, function (err, st) { | ||
if (err) { | ||
return cb(err.code === 'ENOENT' && opts.force ? null : err) | ||
} | ||
if (st.isDirectory()) { | ||
if (opts.recursive) return rmRecursive(path, opts, cb) | ||
const err = new Error('is a directory') | ||
err.code = 'EISDIR' | ||
return cb(err) | ||
} | ||
unlink(path, cb) | ||
}) | ||
} | ||
function rmRecursiveSync (path, opts) { | ||
try { | ||
rmdirSync(path) | ||
} catch (err) { | ||
if (err.code !== 'ENOTEMPTY') throw err | ||
const files = readdirSync(path) | ||
for (const file of files) { | ||
rmSync(path + sep + file, opts) | ||
} | ||
rmdirSync(path) | ||
} | ||
} | ||
function rmSync (path, opts) { | ||
if (typeof path !== 'string') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')') | ||
} | ||
if (!opts) opts = {} | ||
try { | ||
const st = lstatSync(path) | ||
if (st.isDirectory()) { | ||
if (opts.recursive) return rmRecursiveSync(path, opts) | ||
const err = new Error('is a directory') | ||
err.code = 'EISDIR' | ||
throw err | ||
} | ||
unlinkSync(path) | ||
} catch (err) { | ||
if (err.code !== 'ENOENT' || !opts.force) throw err | ||
} | ||
} | ||
function unlink (path, cb) { | ||
@@ -690,2 +791,56 @@ if (typeof path !== 'string') { | ||
function realpath (path, opts, cb) { | ||
if (typeof path !== 'string') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')') | ||
} | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} else if (typeof cb !== 'function') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')') | ||
} | ||
if (typeof opts === 'string') opts = { encoding: opts } | ||
else if (!opts) opts = {} | ||
const { | ||
encoding = 'utf8' | ||
} = opts | ||
const data = Buffer.allocUnsafe(binding.sizeofFSPath) | ||
const req = getReq() | ||
req.callback = function (err, _) { | ||
if (err) return cb(err, null) | ||
let path = data.subarray(0, data.indexOf(0)) | ||
if (encoding !== 'buffer') path = path.toString(encoding) | ||
cb(null, path) | ||
} | ||
binding.realpath(req.handle, path, data) | ||
} | ||
function realpathSync (path, opts) { | ||
if (typeof path !== 'string') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')') | ||
} | ||
if (typeof opts === 'string') opts = { encoding: opts } | ||
else if (!opts) opts = {} | ||
const { | ||
encoding = 'utf8' | ||
} = opts | ||
const data = Buffer.allocUnsafe(binding.sizeofFSPath) | ||
binding.realpathSync(path, data) | ||
path = data.subarray(0, data.indexOf(0)) | ||
if (encoding !== 'buffer') path = path.toString(encoding) | ||
return path | ||
} | ||
function readlink (path, opts, cb) { | ||
@@ -871,2 +1026,15 @@ if (typeof path !== 'string') { | ||
function opendirSync (path, opts) { | ||
if (typeof path !== 'string') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')') | ||
} | ||
if (typeof opts === 'string') opts = { encoding: opts } | ||
else if (!opts) opts = {} | ||
const data = Buffer.allocUnsafe(binding.sizeofFSDir) | ||
binding.opendirSync(path, data) | ||
return new Dir(path, data, opts) | ||
} | ||
function readdir (path, opts, cb) { | ||
@@ -885,3 +1053,3 @@ if (typeof path !== 'string') { | ||
if (typeof opts === 'string') opts = { encoding: opts } | ||
if (!opts) opts = {} | ||
else if (!opts) opts = {} | ||
@@ -895,9 +1063,33 @@ const { | ||
const result = [] | ||
dir | ||
.on('data', (entry) => result.push(withFileTypes ? entry : entry.name)) | ||
.on('error', (err) => cb(err, null)) | ||
.on('end', () => cb(null, result)) | ||
for await (const entry of dir) { | ||
result.push(withFileTypes ? entry : entry.name) | ||
} | ||
cb(null, result) | ||
}) | ||
} | ||
function readdirSync (path, opts) { | ||
if (typeof path !== 'string') { | ||
throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')') | ||
} | ||
if (typeof opts === 'string') opts = { encoding: opts } | ||
else if (!opts) opts = {} | ||
const { | ||
withFileTypes = false | ||
} = opts | ||
const dir = opendirSync(path, opts) | ||
const result = [] | ||
while (true) { | ||
const entry = dir.readSync() | ||
if (entry === null) break | ||
result.push(withFileTypes ? entry : entry.name) | ||
} | ||
return result | ||
} | ||
function readFile (path, opts, cb) { | ||
@@ -1225,3 +1417,3 @@ if (typeof path !== 'string') { | ||
class Dir extends Readable { | ||
class Dir { | ||
constructor (path, handle, opts = {}) { | ||
@@ -1233,7 +1425,7 @@ const { | ||
super() | ||
this._handle = handle | ||
this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize) | ||
this._encoding = encoding | ||
this._buffer = [] | ||
this._ended = false | ||
@@ -1243,19 +1435,24 @@ this.path = path | ||
_read (cb) { | ||
const self = this | ||
read (cb) { | ||
if (!cb) return promisify(this.read.bind(this)) | ||
if (this._buffer.length) return queueMicrotask(() => cb(null, this._buffer.shift())) | ||
if (this._ended) return queueMicrotask(() => cb(null, null)) | ||
const data = [] | ||
const req = getReq() | ||
req.callback = function (err, _) { | ||
if (err) return cb(err) | ||
if (data.length === 0) self.push(null) | ||
req.callback = (err, _) => { | ||
if (err) return cb(err, null) | ||
if (data.length === 0) this._ended = true | ||
else { | ||
for (const entry of data) { | ||
let name = Buffer.from(entry.name) | ||
if (self._encoding !== 'buffer') name = name.toString(self._encoding) | ||
self.push(new Dirent(self.path, name, entry.type)) | ||
if (this._encoding !== 'buffer') name = name.toString(this._encoding) | ||
this._buffer.push(new Dirent(this.path, name, entry.type)) | ||
} | ||
} | ||
cb(null) | ||
if (this._ended) return cb(null, null) | ||
cb(null, this._buffer.shift()) | ||
} | ||
@@ -1266,6 +1463,30 @@ | ||
_destroy (cb) { | ||
readSync () { | ||
if (this._buffer.length) return this._buffer.shift() | ||
if (this._ended) return null | ||
const data = [] | ||
binding.readdirSync(this._handle, this._dirents, data) | ||
if (data.length === 0) this._ended = true | ||
else { | ||
for (const entry of data) { | ||
let name = Buffer.from(entry.name) | ||
if (this._encoding !== 'buffer') name = name.toString(this._encoding) | ||
this._buffer.push(new Dirent(this.path, name, entry.type)) | ||
} | ||
} | ||
if (this._ended) return null | ||
return this._buffer.shift() | ||
} | ||
close (cb) { | ||
if (!cb) return promisify(this.close.bind(this)) | ||
const req = getReq() | ||
req.callback = function (err, _) { | ||
req.callback = (err, _) => { | ||
this._handle = null | ||
cb(err) | ||
@@ -1277,8 +1498,42 @@ } | ||
close (cb = noop) { | ||
if (this.destroyed) return cb(null) | ||
this | ||
.once('close', () => cb(getStreamError(this))) | ||
.destroy() | ||
closeSync () { | ||
binding.closedirSync(this._handle) | ||
this._handle = null | ||
} | ||
[Symbol.iterator] () { | ||
return { | ||
next: () => { | ||
if (this._buffer.length) { | ||
return { done: false, value: this._buffer.shift() } | ||
} | ||
if (this._ended) { | ||
this.closeSync() | ||
return { done: true } | ||
} | ||
const entry = this.readSync() | ||
return { done: entry === null, value: entry } | ||
} | ||
} | ||
} | ||
[Symbol.asyncIterator] () { | ||
return { | ||
next: () => new Promise((resolve, reject) => { | ||
if (this._buffer.length) { | ||
return resolve({ done: false, value: this._buffer.shift() }) | ||
} | ||
if (this._ended) { | ||
return this.close((err) => err ? reject(err) : resolve({ done: true })) | ||
} | ||
this.read((err, entry) => err ? reject(err) : resolve({ done: entry === null, value: entry })) | ||
}) | ||
} | ||
} | ||
} | ||
@@ -1288,4 +1543,3 @@ | ||
constructor (path, name, type) { | ||
this._type = type | ||
this.type = type | ||
this.path = path | ||
@@ -1296,27 +1550,27 @@ this.name = name | ||
isFile () { | ||
return this._type === constants.UV_DIRENT_FILE | ||
return this.type === constants.UV_DIRENT_FILE | ||
} | ||
isDirectory () { | ||
return this._type === constants.UV_DIRENT_DIR | ||
return this.type === constants.UV_DIRENT_DIR | ||
} | ||
isSymbolicLink () { | ||
return this._type === constants.UV_DIRENT_LINK | ||
return this.type === constants.UV_DIRENT_LINK | ||
} | ||
isFIFO () { | ||
return this._type === constants.UV_DIRENT_FIFO | ||
return this.type === constants.UV_DIRENT_FIFO | ||
} | ||
isSocket () { | ||
return this._type === constants.UV_DIRENT_SOCKET | ||
return this.type === constants.UV_DIRENT_SOCKET | ||
} | ||
isCharacterDevice () { | ||
return this._type === constants.UV_DIRENT_CHAR | ||
return this.type === constants.UV_DIRENT_CHAR | ||
} | ||
isBlockDevice () { | ||
return this._type === constants.UV_DIRENT_BLOCK | ||
return this.type === constants.UV_DIRENT_BLOCK | ||
} | ||
@@ -1349,3 +1603,5 @@ } | ||
exports.readv = readv | ||
exports.realpath = realpath | ||
exports.rename = rename | ||
exports.rm = rm | ||
exports.rmdir = rmdir | ||
@@ -1366,6 +1622,10 @@ exports.stat = stat | ||
exports.openSync = openSync | ||
exports.opendirSync = opendirSync | ||
exports.readFileSync = readFileSync | ||
exports.readSync = readSync | ||
exports.readdirSync = readdirSync | ||
exports.readlinkSync = readlinkSync | ||
exports.realpathSync = realpathSync | ||
exports.renameSync = renameSync | ||
exports.rmSync = rmSync | ||
exports.rmdirSync = rmdirSync | ||
@@ -1385,3 +1645,5 @@ exports.statSync = statSync | ||
exports.promises.readlink = promisify(readlink) | ||
exports.promises.realpath = promisify(realpath) | ||
exports.promises.rename = promisify(rename) | ||
exports.promises.rm = promisify(rm) | ||
exports.promises.rmdir = promisify(rmdir) | ||
@@ -1388,0 +1650,0 @@ exports.promises.stat = promisify(stat) |
{ | ||
"name": "bare-fs", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "Native file system for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,2 +29,3 @@ # bare-fs | ||
fs.readv | ||
fs.realpath | ||
fs.rename | ||
@@ -48,2 +49,3 @@ fs.rmdir | ||
fs.promises.readlink | ||
fs.promises.realpath | ||
fs.promises.rename | ||
@@ -66,4 +68,7 @@ fs.promises.rmdir | ||
fs.openSync | ||
fs.opendirSync | ||
fs.readSync | ||
fs.readdirSync | ||
fs.readlinkSync | ||
fs.realpathSync | ||
fs.renameSync | ||
@@ -70,0 +75,0 @@ fs.rmdirSync |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106122
1305
85