diff --git a/src/fsa-to-node/FsaNodeFs.ts b/src/fsa-to-node/FsaNodeFs.ts index ad06512eb..eeb258e43 100644 --- a/src/fsa-to-node/FsaNodeFs.ts +++ b/src/fsa-to-node/FsaNodeFs.ts @@ -40,13 +40,13 @@ import { constants } from '../constants'; import { FsaNodeStats } from './FsaNodeStats'; import process from '../process'; import { FsSynchronousApi } from '../node/types/FsSynchronousApi'; -import {FsaNodeWriteStream} from './FsaNodeWriteStream'; +import { FsaNodeWriteStream } from './FsaNodeWriteStream'; import type { FsCallbackApi, FsPromisesApi } from '../node/types'; import type * as misc from '../node/types/misc'; import type * as opts from '../node/types/options'; import type * as fsa from '../fsa/types'; import type { FsCommonObjects } from '../node/types/FsCommonObjects'; -import type {WritevCallback} from '../node/types/callback'; +import type { WritevCallback } from '../node/types/callback'; const notSupported: (...args: any[]) => any = () => { throw new Error('Method not supported by the File System Access API.'); @@ -269,7 +269,7 @@ export class FsaNodeFs implements FsCallbackApi, FsSynchronousApi, FsCommonObjec fd: number, buffers: ArrayBufferView[], a: number | null | WritevCallback, - b?: WritevCallback + b?: WritevCallback, ): void => { validateFd(fd); let position: number | null = null; @@ -767,32 +767,66 @@ export class FsaNodeFs implements FsCallbackApi, FsSynchronousApi, FsCommonObjec ); }; - public readonly fchmod: FsCallbackApi['fchmod'] = (fd: number, mode: misc.TMode, callback: misc.TCallback): void => { + public readonly fchmod: FsCallbackApi['fchmod'] = ( + fd: number, + mode: misc.TMode, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly chmod: FsCallbackApi['chmod'] = (path: misc.PathLike, mode: misc.TMode, callback: misc.TCallback): void => { + public readonly chmod: FsCallbackApi['chmod'] = ( + path: misc.PathLike, + mode: misc.TMode, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly lchmod: FsCallbackApi['lchmod'] = (path: misc.PathLike, mode: misc.TMode, callback: misc.TCallback): void => { + public readonly lchmod: FsCallbackApi['lchmod'] = ( + path: misc.PathLike, + mode: misc.TMode, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly fchown: FsCallbackApi['fchown'] = (fd: number, uid: number, gid: number, callback: misc.TCallback): void => { + public readonly fchown: FsCallbackApi['fchown'] = ( + fd: number, + uid: number, + gid: number, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly chown: FsCallbackApi['chown'] = (path: misc.PathLike, uid: number, gid: number, callback: misc.TCallback): void => { + public readonly chown: FsCallbackApi['chown'] = ( + path: misc.PathLike, + uid: number, + gid: number, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly lchown: FsCallbackApi['lchown'] = (path: misc.PathLike, uid: number, gid: number, callback: misc.TCallback): void => { + public readonly lchown: FsCallbackApi['lchown'] = ( + path: misc.PathLike, + uid: number, + gid: number, + callback: misc.TCallback, + ): void => { callback(null); }; - public readonly createWriteStream: FsCallbackApi['createWriteStream'] = (path: misc.PathLike, options?: opts.IWriteStreamOptions | string): FsaNodeWriteStream => { - const optionsObj: opts.IWriteStreamOptions = !options ? {} : typeof options === 'object' ? options : { encoding: options } as opts.IWriteStreamOptions; + public readonly createWriteStream: FsCallbackApi['createWriteStream'] = ( + path: misc.PathLike, + options?: opts.IWriteStreamOptions | string, + ): FsaNodeWriteStream => { + const optionsObj: opts.IWriteStreamOptions = !options + ? {} + : typeof options === 'object' + ? options + : ({ encoding: options } as opts.IWriteStreamOptions); const filename = pathToFilename(path); const location = pathToLocation(filename); const flags = flagsToNumber(optionsObj.flags); diff --git a/src/fsa-to-node/FsaNodeWriteStream.ts b/src/fsa-to-node/FsaNodeWriteStream.ts index b5edf4db9..3a28e3fcc 100644 --- a/src/fsa-to-node/FsaNodeWriteStream.ts +++ b/src/fsa-to-node/FsaNodeWriteStream.ts @@ -1,20 +1,20 @@ -import {Writable} from 'stream'; -import {Defer} from 'thingies/es6/Defer'; -import type {IFileSystemFileHandle} from '../fsa/types'; -import type {IWriteStream} from '../node/types/misc'; -import type {IWriteStreamOptions} from '../node/types/options'; +import { Writable } from 'stream'; +import { Defer } from 'thingies/es6/Defer'; +import type { IFileSystemFileHandle } from '../fsa/types'; +import type { IWriteStream } from '../node/types/misc'; +import type { IWriteStreamOptions } from '../node/types/options'; /** * This WriteStream implementation does not build on top of the `fs` module, * but instead uses the lower-level `FileSystemFileHandle` interface. The reason * is the different semantics in `fs` and FSA (File System Access API) write streams. - * + * * When data is written to an FSA file, a new FSA stream is created, it copies * the file to a temporary swap file. After each written chunk, that swap file * is closed and the original file is replaced with the swap file. This means, * if WriteStream was built on top of `fs`, each chunk write would result in * a file copy, write, close, rename operations, which is not what we want. - * + * * Instead this implementation hooks into the lower-level and closes the swap * file only once the stream is closed. The downside is that the written data * is not immediately visible to other processes (because it is written to the @@ -25,14 +25,18 @@ export class FsaNodeWriteStream extends Writable implements IWriteStream { protected ready = new Defer(); protected closed: boolean = false; - public constructor(protected readonly handle: Promise, public readonly path: string, protected readonly options?: IWriteStreamOptions) { + public constructor( + protected readonly handle: Promise, + public readonly path: string, + protected readonly options?: IWriteStreamOptions, + ) { super(); handle .then(() => { this.__pending = false; this.ready.resolve(); }) - .catch((error) => { + .catch(error => { this.ready.reject(error); }); } @@ -47,21 +51,13 @@ export class FsaNodeWriteStream extends Writable implements IWriteStream { return this.__pending; } - public close(): void { - - } + public close(): void {} // ----------------------------------------------------------------- Writable - _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void { - - } - - _writev(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void { - - } + _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void {} - _final(callback: (error?: Error | null) => void): void { + _writev(chunks: Array<{ chunk: any; encoding: string }>, callback: (error?: Error | null) => void): void {} - } + _final(callback: (error?: Error | null) => void): void {} } diff --git a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts index 8dd1e0e3f..971b357d2 100644 --- a/src/fsa-to-node/__tests__/FsaNodeFs.test.ts +++ b/src/fsa-to-node/__tests__/FsaNodeFs.test.ts @@ -367,10 +367,7 @@ describe('.writev()', () => { }), ); const [bytesWritten, data] = await new Promise<[number, any]>((resolve, reject) => { - const buffers = [ - Buffer.from('a'), - Buffer.from('b'), - ]; + const buffers = [Buffer.from('a'), Buffer.from('b')]; fs.writev(fd, buffers, (err, bytesWritten, data) => { if (err) reject(err); else resolve([bytesWritten!, data]); diff --git a/src/volume.ts b/src/volume.ts index 22c41b7de..9750004ac 100644 --- a/src/volume.ts +++ b/src/volume.ts @@ -50,7 +50,7 @@ import { bufferToEncoding, } from './node/util'; import type { PathLike, symlink } from 'fs'; -import {WritevCallback} from './node/types/callback'; +import { WritevCallback } from './node/types/callback'; const resolveCrossPlatform = pathModule.resolve; const {