Skip to content

Commit

Permalink
style: 💄 run Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 17, 2023
1 parent 8190bfd commit 0840786
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
56 changes: 45 additions & 11 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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>): void => {
public readonly fchmod: FsCallbackApi['fchmod'] = (
fd: number,
mode: misc.TMode,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

public readonly chmod: FsCallbackApi['chmod'] = (path: misc.PathLike, mode: misc.TMode, callback: misc.TCallback<void>): void => {
public readonly chmod: FsCallbackApi['chmod'] = (
path: misc.PathLike,
mode: misc.TMode,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

public readonly lchmod: FsCallbackApi['lchmod'] = (path: misc.PathLike, mode: misc.TMode, callback: misc.TCallback<void>): void => {
public readonly lchmod: FsCallbackApi['lchmod'] = (
path: misc.PathLike,
mode: misc.TMode,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

public readonly fchown: FsCallbackApi['fchown'] = (fd: number, uid: number, gid: number, callback: misc.TCallback<void>): void => {
public readonly fchown: FsCallbackApi['fchown'] = (
fd: number,
uid: number,
gid: number,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

public readonly chown: FsCallbackApi['chown'] = (path: misc.PathLike, uid: number, gid: number, callback: misc.TCallback<void>): void => {
public readonly chown: FsCallbackApi['chown'] = (
path: misc.PathLike,
uid: number,
gid: number,
callback: misc.TCallback<void>,
): void => {
callback(null);
};

public readonly lchown: FsCallbackApi['lchown'] = (path: misc.PathLike, uid: number, gid: number, callback: misc.TCallback<void>): void => {
public readonly lchown: FsCallbackApi['lchown'] = (
path: misc.PathLike,
uid: number,
gid: number,
callback: misc.TCallback<void>,
): 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);
Expand Down
38 changes: 17 additions & 21 deletions src/fsa-to-node/FsaNodeWriteStream.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,14 +25,18 @@ export class FsaNodeWriteStream extends Writable implements IWriteStream {
protected ready = new Defer<void>();
protected closed: boolean = false;

public constructor(protected readonly handle: Promise<IFileSystemFileHandle>, public readonly path: string, protected readonly options?: IWriteStreamOptions) {
public constructor(
protected readonly handle: Promise<IFileSystemFileHandle>,
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);
});
}
Expand All @@ -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 {}
}
5 changes: 1 addition & 4 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0840786

Please sign in to comment.