Comparing version 1.12.1 to 1.13.0
128
index.js
@@ -0,1 +1,2 @@ | ||
const EventEmitter = require('events') | ||
const { sep, resolve, isAbsolute, toNamespacedPath } = require('path') | ||
@@ -1284,2 +1285,20 @@ const { Readable, Writable } = require('streamx') | ||
function watch (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 = {} | ||
} | ||
if (typeof opts === 'string') opts = { encoding: opts } | ||
else if (!opts) opts = {} | ||
const watcher = new Watcher(path, opts) | ||
if (cb) watcher.on('change', cb) | ||
return watcher | ||
} | ||
class Stats { | ||
@@ -1606,2 +1625,107 @@ constructor (dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeMs, mtimeMs, ctimeMs, birthtimeMs) { | ||
class Watcher extends EventEmitter { | ||
constructor (path, opts) { | ||
const { | ||
persistent = true, | ||
recursive = false, | ||
encoding = 'utf8' | ||
} = opts | ||
super() | ||
this._closed = false | ||
this._encoding = encoding | ||
this._handle = binding.watcherInit(path, recursive, this, this._onevent, this._onclose) | ||
if (!persistent) this.unref() | ||
} | ||
_onevent (err, events, filename) { | ||
if (err) { | ||
this.close() | ||
this.emit('error', err) | ||
} else { | ||
let eventType | ||
if (events & binding.UV_RENAME) { | ||
eventType = 'rename' | ||
} else if (events & binding.UV_CHANGE) { | ||
eventType = 'change' | ||
} | ||
this.emit('change', eventType, this._encoding === 'buffer' | ||
? Buffer.from(filename) | ||
: Buffer.from(filename).toString(this._encoding) | ||
) | ||
} | ||
} | ||
_onclose () { | ||
this.emit('close') | ||
} | ||
close () { | ||
if (this._closed) return | ||
this._closed = true | ||
binding.watcherClose(this._handle) | ||
} | ||
ref () { | ||
if (this._handle) binding.watcherRef(this._handle) | ||
return this | ||
} | ||
unref () { | ||
if (this._handle) binding.watcherUnref(this._handle) | ||
return this | ||
} | ||
[Symbol.asyncIterator] () { | ||
const buffer = [] | ||
let done = false | ||
let error = null | ||
let next = null | ||
this | ||
.on('change', (eventType, filename) => { | ||
if (next) { | ||
next.resolve({ done: false, value: { eventType, filename } }) | ||
next = null | ||
} else { | ||
buffer.push({ eventType, filename }) | ||
} | ||
}) | ||
.on('error', (err) => { | ||
done = true | ||
error = err | ||
if (next) { | ||
next.reject(error) | ||
next = null | ||
} | ||
}) | ||
.on('close', () => { | ||
done = true | ||
if (next) { | ||
next.resolve({ done }) | ||
next = null | ||
} | ||
}) | ||
return { | ||
next: () => new Promise((resolve, reject) => { | ||
if (error) return reject(error) | ||
if (buffer.length) return resolve({ done: false, value: buffer.shift() }) | ||
if (done) return resolve({ done }) | ||
next = { resolve, reject } | ||
}) | ||
} | ||
} | ||
} | ||
exports.promises = {} | ||
@@ -1639,2 +1763,3 @@ | ||
exports.unlink = unlink | ||
exports.watch = watch | ||
exports.write = write | ||
@@ -1684,5 +1809,8 @@ exports.writeFile = writeFile | ||
exports.promises.watch = watch // Already async iterable | ||
exports.Stats = Stats | ||
exports.Dir = Dir | ||
exports.Dirent = Dirent | ||
exports.Watcher = Watcher | ||
@@ -1689,0 +1817,0 @@ exports.ReadStream = FileReadStream |
{ | ||
"name": "bare-fs", | ||
"version": "1.12.1", | ||
"version": "1.13.0", | ||
"description": "Native file system for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -36,2 +36,3 @@ # bare-fs | ||
fs.unlink | ||
fs.watch | ||
fs.write | ||
@@ -57,2 +58,3 @@ fs.writev | ||
fs.promises.unlink | ||
fs.promises.watch | ||
fs.promises.writeFile | ||
@@ -59,0 +61,0 @@ |
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
117141
1448
90