@zenfs/core
Advanced tools
Comparing version 1.8.7 to 1.8.8
@@ -69,17 +69,17 @@ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { | ||
class AsyncFS extends FS { | ||
get _queueRunning() { | ||
return !!this._queue.length; | ||
async done() { | ||
await this._promise; | ||
} | ||
queueDone() { | ||
return new Promise(resolve => { | ||
const check = () => (this._queueRunning ? setTimeout(check) : resolve()); | ||
check(); | ||
}); | ||
return this.done(); | ||
} | ||
_async(promise) { | ||
if (!this._promise) { | ||
this._promise = promise; | ||
return; | ||
} | ||
this._promise = this._promise.then(() => promise); | ||
} | ||
constructor(...args) { | ||
super(...args); | ||
/** | ||
* Queue of pending asynchronous operations. | ||
*/ | ||
this._queue = []; | ||
this._isInitialized = false; | ||
@@ -128,3 +128,3 @@ this._patchAsync(); | ||
this._sync.renameSync(oldPath, newPath); | ||
this.queue('rename', oldPath, newPath); | ||
this._async(this.rename(oldPath, newPath)); | ||
} | ||
@@ -138,3 +138,3 @@ statSync(path) { | ||
this._sync.createFileSync(path, flag, mode, options); | ||
this.queue('createFile', path, flag, mode, options); | ||
this._async(this.createFile(path, flag, mode, options)); | ||
return this.openFileSync(path, flag); | ||
@@ -150,3 +150,3 @@ } | ||
this._sync.unlinkSync(path); | ||
this.queue('unlink', path); | ||
this._async(this.unlink(path)); | ||
} | ||
@@ -156,3 +156,3 @@ rmdirSync(path) { | ||
this._sync.rmdirSync(path); | ||
this.queue('rmdir', path); | ||
this._async(this.rmdir(path)); | ||
} | ||
@@ -162,3 +162,3 @@ mkdirSync(path, mode, options) { | ||
this._sync.mkdirSync(path, mode, options); | ||
this.queue('mkdir', path, mode, options); | ||
this._async(this.mkdir(path, mode, options)); | ||
} | ||
@@ -172,3 +172,3 @@ readdirSync(path) { | ||
this._sync.linkSync(srcpath, dstpath); | ||
this.queue('link', srcpath, dstpath); | ||
this._async(this.link(srcpath, dstpath)); | ||
} | ||
@@ -178,3 +178,3 @@ syncSync(path, data, stats) { | ||
this._sync.syncSync(path, data, stats); | ||
this.queue('sync', path, data, stats); | ||
this._async(this.sync(path, data, stats)); | ||
} | ||
@@ -192,3 +192,3 @@ existsSync(path) { | ||
this._sync.writeSync(path, buffer, offset); | ||
this.queue('write', path, buffer, offset); | ||
this._async(this.write(path, buffer, offset)); | ||
} | ||
@@ -233,21 +233,2 @@ /** | ||
* @internal | ||
*/ | ||
async _next() { | ||
if (!this._queueRunning) { | ||
return; | ||
} | ||
const [method, ...args] = this._queue.shift(); | ||
// @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple) | ||
await this[method](...args); | ||
await this._next(); | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
queue(...op) { | ||
this._queue.push(op); | ||
void this._next(); | ||
} | ||
/** | ||
* @internal | ||
* Patch all async methods to also call their synchronous counterparts unless called from the queue | ||
@@ -276,3 +257,3 @@ */ | ||
const result = await originalMethod.apply(this, args); | ||
if (new Error().stack.includes(`at async ${this.constructor.name}._next`) || !this._isInitialized) | ||
if (new Error().stack.includes(`at <computed> [as ${key}]`) || !this._isInitialized) | ||
return result; | ||
@@ -279,0 +260,0 @@ try { |
@@ -40,2 +40,8 @@ /** | ||
} | ||
async read(path, buffer, offset, end) { | ||
return this.readSync(path, buffer, offset, end); | ||
} | ||
async write(path, buffer, offset) { | ||
return this.writeSync(path, buffer, offset); | ||
} | ||
} | ||
@@ -42,0 +48,0 @@ return SyncFS; |
{ | ||
"name": "@zenfs/core", | ||
"version": "1.8.7", | ||
"version": "1.8.8", | ||
"description": "A filesystem, anywhere", | ||
@@ -5,0 +5,0 @@ "funding": { |
@@ -32,3 +32,3 @@ import assert from 'node:assert/strict'; | ||
assert((await fs.promises.readFile(fn)).equals(expected)); | ||
assert.deepEqual(await fs.promises.readFile(fn), expected); | ||
@@ -35,0 +35,0 @@ await fs.promises.unlink(fn); |
656262
16491