@zenfs/core
Advanced tools
Comparing version 0.12.4 to 0.12.5
@@ -249,2 +249,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
statSync(): Stats; | ||
protected _truncate(length: number): void; | ||
/** | ||
@@ -260,2 +261,3 @@ * Asynchronous truncate. | ||
truncateSync(length: number): void; | ||
protected _write(buffer: Uint8Array, offset?: number, length?: number, position?: number): number; | ||
/** | ||
@@ -288,2 +290,3 @@ * Write buffer to the file. | ||
writeSync(buffer: Uint8Array, offset?: number, length?: number, position?: number): number; | ||
protected _read(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number; | ||
/** | ||
@@ -290,0 +293,0 @@ * Read data from the file. |
145
dist/file.js
@@ -239,2 +239,18 @@ import { O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY, S_IFMT } from './emulation/constants.js'; | ||
} | ||
_truncate(length) { | ||
this.dirty = true; | ||
if (!isWriteable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a writeable mode.'); | ||
} | ||
this.stats.mtimeMs = Date.now(); | ||
if (length > this._buffer.length) { | ||
const data = new Uint8Array(length - this._buffer.length); | ||
// Write will set stats.size and handle syncing. | ||
this.writeSync(data, 0, data.length, this._buffer.length); | ||
return; | ||
} | ||
this.stats.size = length; | ||
// Truncate. | ||
this._buffer = this._buffer.slice(0, length); | ||
} | ||
/** | ||
@@ -245,4 +261,4 @@ * Asynchronous truncate. | ||
async truncate(length) { | ||
this.truncateSync(length); | ||
return this.sync(); | ||
this._truncate(length); | ||
await this.sync(); | ||
} | ||
@@ -254,2 +270,6 @@ /** | ||
truncateSync(length) { | ||
this._truncate(length); | ||
this.syncSync(); | ||
} | ||
_write(buffer, offset = 0, length = this.stats.size, position = this.position) { | ||
this.dirty = true; | ||
@@ -259,13 +279,22 @@ if (!isWriteable(this.flag)) { | ||
} | ||
const end = position + length; | ||
if (end > this.stats.size) { | ||
this.stats.size = end; | ||
if (end > this._buffer.byteLength) { | ||
if (this._buffer.buffer.resizable && this._buffer.buffer.maxByteLength <= end) { | ||
this._buffer.buffer.resize(end); | ||
} | ||
else { | ||
// Extend the buffer! | ||
const newBuffer = new Uint8Array(new ArrayBuffer(end, this.fs.metadata().noResizableBuffers ? {} : { maxByteLength: size_max })); | ||
newBuffer.set(this._buffer); | ||
this._buffer = newBuffer; | ||
} | ||
} | ||
} | ||
const slice = buffer.slice(offset, offset + length); | ||
this._buffer.set(slice, position); | ||
this.stats.mtimeMs = Date.now(); | ||
if (length > this._buffer.length) { | ||
const data = new Uint8Array(length - this._buffer.length); | ||
// Write will set stats.size and handle syncing. | ||
this.writeSync(data, 0, data.length, this._buffer.length); | ||
return; | ||
} | ||
this.stats.size = length; | ||
// Truncate. | ||
this._buffer = this._buffer.slice(0, length); | ||
this.syncSync(); | ||
this.position = position + slice.byteLength; | ||
return slice.byteLength; | ||
} | ||
@@ -284,4 +313,4 @@ /** | ||
*/ | ||
async write(buffer, offset = 0, length = this.stats.size, position = this.position) { | ||
const bytesWritten = this.writeSync(buffer, offset, length, position); | ||
async write(buffer, offset, length, position) { | ||
const bytesWritten = this._write(buffer, offset, length, position); | ||
await this.sync(); | ||
@@ -304,28 +333,25 @@ return bytesWritten; | ||
writeSync(buffer, offset = 0, length = this.stats.size, position = this.position) { | ||
const bytesWritten = this._write(buffer, offset, length, position); | ||
this.syncSync(); | ||
return bytesWritten; | ||
} | ||
_read(buffer, offset = 0, length = this.stats.size, position) { | ||
if (!isReadable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a readable mode.'); | ||
} | ||
this.dirty = true; | ||
if (!isWriteable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a writeable mode.'); | ||
} | ||
const end = position + length; | ||
position ?? (position = this.position); | ||
let end = position + length; | ||
if (end > this.stats.size) { | ||
this.stats.size = end; | ||
if (end > this._buffer.byteLength) { | ||
if (this._buffer.buffer.resizable && this._buffer.buffer.maxByteLength <= end) { | ||
this._buffer.buffer.resize(end); | ||
} | ||
else { | ||
// Extend the buffer! | ||
const newBuffer = new Uint8Array(new ArrayBuffer(end, this.fs.metadata().noResizableBuffers ? {} : { maxByteLength: size_max })); | ||
newBuffer.set(this._buffer); | ||
this._buffer = newBuffer; | ||
} | ||
} | ||
end = position + Math.max(this.stats.size - position, 0); | ||
} | ||
const slice = buffer.slice(offset, offset + length); | ||
this._buffer.set(slice, position); | ||
const bytesWritten = slice.byteLength; | ||
this.stats.mtimeMs = Date.now(); | ||
this.position = position + bytesWritten; | ||
this.syncSync(); | ||
return bytesWritten; | ||
this.stats.atimeMs = Date.now(); | ||
this._position = end; | ||
const bytesRead = end - position; | ||
if (bytesRead == 0) { | ||
// No copy/read. Return immediatly for better performance | ||
return bytesRead; | ||
} | ||
new Uint8Array(buffer.buffer, offset, length).set(this._buffer.slice(position, end)); | ||
return bytesRead; | ||
} | ||
@@ -343,4 +369,6 @@ /** | ||
*/ | ||
async read(buffer, offset = 0, length = this.stats.size, position = 0) { | ||
return { bytesRead: this.readSync(buffer, offset, length, position), buffer }; | ||
async read(buffer, offset, length, position) { | ||
const bytesRead = this._read(buffer, offset, length, position); | ||
await this.sync(); | ||
return { bytesRead, buffer }; | ||
} | ||
@@ -358,21 +386,5 @@ /** | ||
*/ | ||
readSync(buffer, offset = 0, length = this.stats.size, position) { | ||
if (!isReadable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a readable mode.'); | ||
} | ||
this.dirty = true; | ||
position ?? (position = this.position); | ||
let end = position + length; | ||
if (end > this.stats.size) { | ||
end = position + Math.max(this.stats.size - position, 0); | ||
} | ||
this.stats.atimeMs = Date.now(); | ||
this._position = end; | ||
const bytesRead = end - position; | ||
this.syncSync(); | ||
if (bytesRead == 0) { | ||
// No copy/read. Return immediatly for better performance | ||
return bytesRead; | ||
} | ||
new Uint8Array(buffer.buffer, offset, length).set(this._buffer.slice(position, end)); | ||
readSync(buffer, offset, length, position) { | ||
const bytesRead = this._read(buffer, offset, length, position); | ||
this.statSync(); | ||
return bytesRead; | ||
@@ -385,3 +397,5 @@ } | ||
async chmod(mode) { | ||
this.chmodSync(mode); | ||
this.dirty = true; | ||
this.stats.chmod(mode); | ||
await this.sync(); | ||
} | ||
@@ -403,3 +417,5 @@ /** | ||
async chown(uid, gid) { | ||
this.chownSync(uid, gid); | ||
this.dirty = true; | ||
this.stats.chown(uid, gid); | ||
await this.sync(); | ||
} | ||
@@ -417,3 +433,6 @@ /** | ||
async utimes(atime, mtime) { | ||
this.utimesSync(atime, mtime); | ||
this.dirty = true; | ||
this.stats.atime = atime; | ||
this.stats.mtime = mtime; | ||
await this.sync(); | ||
} | ||
@@ -426,6 +445,6 @@ utimesSync(atime, mtime) { | ||
} | ||
_setType(type) { | ||
async _setType(type) { | ||
this.dirty = true; | ||
this.stats.mode = (this.stats.mode & ~S_IFMT) | type; | ||
return this.sync(); | ||
await this.sync(); | ||
} | ||
@@ -432,0 +451,0 @@ _setTypeSync(type) { |
{ | ||
"name": "@zenfs/core", | ||
"version": "0.12.4", | ||
"version": "0.12.5", | ||
"description": "A filesystem in your browser", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
153
src/file.ts
@@ -442,2 +442,19 @@ import type { FileReadResult } from 'node:fs/promises'; | ||
protected _truncate(length: number): void { | ||
this.dirty = true; | ||
if (!isWriteable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a writeable mode.'); | ||
} | ||
this.stats.mtimeMs = Date.now(); | ||
if (length > this._buffer.length) { | ||
const data = new Uint8Array(length - this._buffer.length); | ||
// Write will set stats.size and handle syncing. | ||
this.writeSync(data, 0, data.length, this._buffer.length); | ||
return; | ||
} | ||
this.stats.size = length; | ||
// Truncate. | ||
this._buffer = this._buffer.slice(0, length); | ||
} | ||
/** | ||
@@ -448,4 +465,4 @@ * Asynchronous truncate. | ||
public async truncate(length: number): Promise<void> { | ||
this.truncateSync(length); | ||
return this.sync(); | ||
this._truncate(length); | ||
await this.sync(); | ||
} | ||
@@ -458,2 +475,7 @@ | ||
public truncateSync(length: number): void { | ||
this._truncate(length); | ||
this.syncSync(); | ||
} | ||
protected _write(buffer: Uint8Array, offset: number = 0, length: number = this.stats.size, position: number = this.position): number { | ||
this.dirty = true; | ||
@@ -463,13 +485,22 @@ if (!isWriteable(this.flag)) { | ||
} | ||
const end = position + length; | ||
if (end > this.stats.size) { | ||
this.stats.size = end; | ||
if (end > this._buffer.byteLength) { | ||
if (this._buffer.buffer.resizable && this._buffer.buffer.maxByteLength! <= end) { | ||
this._buffer.buffer.resize(end); | ||
} else { | ||
// Extend the buffer! | ||
const newBuffer = new Uint8Array(new ArrayBuffer(end, this.fs.metadata().noResizableBuffers ? {} : { maxByteLength: size_max })); | ||
newBuffer.set(this._buffer); | ||
this._buffer = newBuffer; | ||
} | ||
} | ||
} | ||
const slice = buffer.slice(offset, offset + length); | ||
this._buffer.set(slice, position); | ||
this.stats.mtimeMs = Date.now(); | ||
if (length > this._buffer.length) { | ||
const data = new Uint8Array(length - this._buffer.length); | ||
// Write will set stats.size and handle syncing. | ||
this.writeSync(data, 0, data.length, this._buffer.length); | ||
return; | ||
} | ||
this.stats.size = length; | ||
// Truncate. | ||
this._buffer = this._buffer.slice(0, length); | ||
this.syncSync(); | ||
this.position = position + slice.byteLength; | ||
return slice.byteLength; | ||
} | ||
@@ -489,4 +520,4 @@ | ||
*/ | ||
public async write(buffer: Uint8Array, offset: number = 0, length: number = this.stats.size, position: number = this.position): Promise<number> { | ||
const bytesWritten = this.writeSync(buffer, offset, length, position); | ||
public async write(buffer: Uint8Array, offset?: number, length?: number, position?: number): Promise<number> { | ||
const bytesWritten = this._write(buffer, offset, length, position); | ||
await this.sync(); | ||
@@ -510,28 +541,26 @@ return bytesWritten; | ||
public writeSync(buffer: Uint8Array, offset: number = 0, length: number = this.stats.size, position: number = this.position): number { | ||
const bytesWritten = this._write(buffer, offset, length, position); | ||
this.syncSync(); | ||
return bytesWritten; | ||
} | ||
protected _read(buffer: ArrayBufferView, offset: number = 0, length: number = this.stats.size, position?: number): number { | ||
if (!isReadable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a readable mode.'); | ||
} | ||
this.dirty = true; | ||
if (!isWriteable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a writeable mode.'); | ||
} | ||
const end = position + length; | ||
position ??= this.position; | ||
let end = position + length; | ||
if (end > this.stats.size) { | ||
this.stats.size = end; | ||
if (end > this._buffer.byteLength) { | ||
if (this._buffer.buffer.resizable && this._buffer.buffer.maxByteLength! <= end) { | ||
this._buffer.buffer.resize(end); | ||
} else { | ||
// Extend the buffer! | ||
const newBuffer = new Uint8Array(new ArrayBuffer(end, this.fs.metadata().noResizableBuffers ? {} : { maxByteLength: size_max })); | ||
newBuffer.set(this._buffer); | ||
this._buffer = newBuffer; | ||
} | ||
} | ||
end = position + Math.max(this.stats.size - position, 0); | ||
} | ||
const slice = buffer.slice(offset, offset + length); | ||
this._buffer.set(slice, position); | ||
const bytesWritten = slice.byteLength; | ||
this.stats.mtimeMs = Date.now(); | ||
this.position = position + bytesWritten; | ||
this.syncSync(); | ||
return bytesWritten; | ||
this.stats.atimeMs = Date.now(); | ||
this._position = end; | ||
const bytesRead = end - position; | ||
if (bytesRead == 0) { | ||
// No copy/read. Return immediatly for better performance | ||
return bytesRead; | ||
} | ||
new Uint8Array(buffer.buffer, offset, length).set(this._buffer.slice(position, end)); | ||
return bytesRead; | ||
} | ||
@@ -550,9 +579,6 @@ | ||
*/ | ||
public async read<TBuffer extends ArrayBufferView>( | ||
buffer: TBuffer, | ||
offset: number = 0, | ||
length: number = this.stats.size, | ||
position: number = 0 | ||
): Promise<{ bytesRead: number; buffer: TBuffer }> { | ||
return { bytesRead: this.readSync(buffer, offset, length, position), buffer }; | ||
public async read<TBuffer extends ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{ bytesRead: number; buffer: TBuffer }> { | ||
const bytesRead = this._read(buffer, offset, length, position); | ||
await this.sync(); | ||
return { bytesRead, buffer }; | ||
} | ||
@@ -571,21 +597,5 @@ | ||
*/ | ||
public readSync(buffer: ArrayBufferView, offset: number = 0, length: number = this.stats.size, position?: number): number { | ||
if (!isReadable(this.flag)) { | ||
throw new ErrnoError(Errno.EPERM, 'File not opened with a readable mode.'); | ||
} | ||
this.dirty = true; | ||
position ??= this.position; | ||
let end = position + length; | ||
if (end > this.stats.size) { | ||
end = position + Math.max(this.stats.size - position, 0); | ||
} | ||
this.stats.atimeMs = Date.now(); | ||
this._position = end; | ||
const bytesRead = end - position; | ||
this.syncSync(); | ||
if (bytesRead == 0) { | ||
// No copy/read. Return immediatly for better performance | ||
return bytesRead; | ||
} | ||
new Uint8Array(buffer.buffer, offset, length).set(this._buffer.slice(position, end)); | ||
public readSync(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number { | ||
const bytesRead = this._read(buffer, offset, length, position); | ||
this.statSync(); | ||
return bytesRead; | ||
@@ -599,3 +609,5 @@ } | ||
public async chmod(mode: number): Promise<void> { | ||
this.chmodSync(mode); | ||
this.dirty = true; | ||
this.stats.chmod(mode); | ||
await this.sync(); | ||
} | ||
@@ -619,3 +631,5 @@ | ||
public async chown(uid: number, gid: number): Promise<void> { | ||
this.chownSync(uid, gid); | ||
this.dirty = true; | ||
this.stats.chown(uid, gid); | ||
await this.sync(); | ||
} | ||
@@ -635,3 +649,6 @@ | ||
public async utimes(atime: Date, mtime: Date): Promise<void> { | ||
this.utimesSync(atime, mtime); | ||
this.dirty = true; | ||
this.stats.atime = atime; | ||
this.stats.mtime = mtime; | ||
await this.sync(); | ||
} | ||
@@ -646,6 +663,6 @@ | ||
public _setType(type: FileType): Promise<void> { | ||
public async _setType(type: FileType): Promise<void> { | ||
this.dirty = true; | ||
this.stats.mode = (this.stats.mode & ~S_IFMT) | type; | ||
return this.sync(); | ||
await this.sync(); | ||
} | ||
@@ -652,0 +669,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1860324
20507