Skip to content

Commit

Permalink
feat: 🎸 add missing synchronous method types
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 22, 2023
1 parent e6f922b commit ac38b5d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 53 deletions.
18 changes: 17 additions & 1 deletion src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const notSupported: (...args: any[]) => any = () => {
throw new Error('Method not supported by the File System Access API.');
};

const notImplemented: (...args: any[]) => any = () => {
throw new Error('Not implemented');
};

const noop: (...args: any[]) => any = () => {};

/**
Expand Down Expand Up @@ -1022,7 +1026,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
a: string | Buffer | ArrayBufferView | DataView,
b?: number,
c?: number | BufferEncoding,
d?: number,
d?: number | null,
): number => {
const [, buf, offset, length, position] = util.getWriteSyncArgs(fd, a, b, c, d);
const filename = this.getFileName(fd);
Expand All @@ -1044,6 +1048,14 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
return openFile.fd;
};

public readonly writevSync: FsSynchronousApi['writevSync'] = (fd: number, buffers: ArrayBufferView[], position?: number | null): void => {
if (buffers.length === 0) return;
this.writeSync(fd, buffers[0], 0, buffers[0].byteLength, position);
for (let i = 1; i < buffers.length; i++) {
this.writeSync(fd, buffers[i], 0, buffers[i].byteLength, null);
}
};

public readonly fdatasyncSync: FsSynchronousApi['fdatasyncSync'] = noop;
public readonly fsyncSync: FsSynchronousApi['fsyncSync'] = noop;
public readonly chmodSync: FsSynchronousApi['chmodSync'] = noop;
Expand All @@ -1054,6 +1066,10 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
public readonly lchmodSync: FsSynchronousApi['lchmodSync'] = noop;
public readonly lchownSync: FsSynchronousApi['lchownSync'] = noop;
public readonly utimesSync: FsSynchronousApi['utimesSync'] = noop;
public readonly lutimesSync: FsSynchronousApi['lutimesSync'] = noop;

public readonly cpSync: FsSynchronousApi['cpSync'] = notImplemented;
public readonly statfsSync: FsSynchronousApi['statfsSync'] = notImplemented;

public readonly symlinkSync: FsSynchronousApi['symlinkSync'] = notSupported;
public readonly linkSync: FsSynchronousApi['linkSync'] = notSupported;
Expand Down
5 changes: 5 additions & 0 deletions src/node/lists/fsSynchronousApiList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ export const fsSynchronousApiList: Array<keyof FsSynchronousApi> = [
'utimesSync',
'writeFileSync',
'writeSync',

// 'cpSync',
// 'lutimesSync',
// 'statfsSync',
// 'writevSync',
];
102 changes: 53 additions & 49 deletions src/node/types/FsSynchronousApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,41 @@ import type * as misc from './misc';
import type * as opts from './options';

export interface FsSynchronousApi {
openSync(path: misc.PathLike, flags: misc.TFlags, mode?: misc.TMode): number;
accessSync(path: misc.PathLike, mode?: number): void;
appendFileSync(id: misc.TFileId, data: misc.TData, options?: opts.IAppendFileOptions | string): void;
chmodSync(path: misc.PathLike, mode: misc.TMode): void;
chownSync(path: misc.PathLike, uid: number, gid: number): void;
closeSync(fd: number): void;
copyFileSync(src: misc.PathLike, dest: misc.PathLike, flags?: misc.TFlagsCopy): void;
cpSync(src: string | URL, dest: string | URL, options?: opts.ICpOptions): void;
existsSync(path: misc.PathLike): boolean;
fchmodSync(fd: number, mode: misc.TMode): void;
fchownSync(fd: number, uid: number, gid: number): void;
fdatasyncSync(fd: number): void;
fstatSync(fd: number, options: { bigint: false }): misc.IStats<number>;
fstatSync(fd: number, options: { bigint: true }): misc.IStats<bigint>;
fstatSync(fd: number): misc.IStats<number>;
fsyncSync(fd: number): void;
ftruncateSync(fd: number, len?: number): void;
futimesSync(fd: number, atime: misc.TTime, mtime: misc.TTime): void;
lchmodSync(path: misc.PathLike, mode: misc.TMode): void;
lchownSync(path: misc.PathLike, uid: number, gid: number): void;
lutimesSync(path: misc.PathLike, atime: number | string | Date, time: number | string | Date): void;
linkSync(existingPath: misc.PathLike, newPath: misc.PathLike): void;
lstatSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry: false }): misc.IStats<number> | undefined;
lstatSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry?: true | undefined }): misc.IStats<number>;
lstatSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry: false }): misc.IStats<bigint> | undefined;
lstatSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry?: true | undefined }): misc.IStats<bigint>;
lstatSync(path: misc.PathLike, options: { throwIfNoEntry: false }): misc.IStats<number> | undefined;
lstatSync(path: misc.PathLike, options: { throwIfNoEntry?: true | undefined }): misc.IStats<number>;
lstatSync(path: misc.PathLike): misc.IStats<number>;
mkdirSync(path: misc.PathLike, options: opts.IMkdirOptions & { recursive: true }): string | undefined;
mkdirSync(path: misc.PathLike, options?: misc.TMode | (opts.IMkdirOptions & { recursive?: false })): void;
mkdirSync(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): string | undefined;
mkdtempSync(prefix: string, options?: opts.IOptions): misc.TDataOut;
openSync(path: misc.PathLike, flags: misc.TFlags, mode?: misc.TMode): number;
readdirSync(path: misc.PathLike, options?: opts.IReaddirOptions | string): misc.TDataOut[] | misc.IDirent[];
readlinkSync(path: misc.PathLike, options?: opts.IOptions): misc.TDataOut;
readSync(
fd: number,
buffer: Buffer | ArrayBufferView | DataView,
Expand All @@ -12,59 +45,30 @@ export interface FsSynchronousApi {
position: number,
): number;
readFileSync(file: misc.TFileId, options?: opts.IReadFileOptions | string): misc.TDataOut;
realpathSync(path: misc.PathLike, options?: opts.IRealpathOptions | string): misc.TDataOut;
renameSync(oldPath: misc.PathLike, newPath: misc.PathLike): void;
rmdirSync(path: misc.PathLike, options?: opts.IRmdirOptions): void;
rmSync(path: misc.PathLike, options?: opts.IRmOptions): void;
statSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry: false }): misc.IStats<number> | undefined;
statSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry?: true }): misc.IStats<number>;
statSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry: false }): misc.IStats<bigint> | undefined;
statSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry?: true }): misc.IStats<bigint>;
statSync(path: misc.PathLike, options: { throwIfNoEntry: false }): misc.IStats<number> | undefined;
statSync(path: misc.PathLike, options: { throwIfNoEntry?: true }): misc.IStats<number>;
statSync(path: misc.PathLike): misc.IStats<number>;
statfsSync(path: misc.PathLike, options?: opts.IStafsOptions): void;
symlinkSync(target: misc.PathLike, path: misc.PathLike, type?: misc.symlink.Type): void;
truncateSync(id: misc.TFileId, len?: number): void;
unlinkSync(path: misc.PathLike): void;
utimesSync(path: misc.PathLike, atime: misc.TTime, mtime: misc.TTime): void;
writeFileSync(id: misc.TFileId, data: misc.TData, options?: opts.IWriteFileOptions): void;
writeSync(
fd: number,
buffer: Buffer | ArrayBufferView | DataView,
offset?: number,
length?: number,
position?: number,
position?: number | null,
): number;
writeSync(fd: number, str: string, position?: number, encoding?: BufferEncoding): number;
writeFileSync(id: misc.TFileId, data: misc.TData, options?: opts.IWriteFileOptions): void;
copyFileSync(src: misc.PathLike, dest: misc.PathLike, flags?: misc.TFlagsCopy): void;
linkSync(existingPath: misc.PathLike, newPath: misc.PathLike): void;
unlinkSync(path: misc.PathLike): void;
symlinkSync(target: misc.PathLike, path: misc.PathLike, type?: misc.symlink.Type): void;
realpathSync(path: misc.PathLike, options?: opts.IRealpathOptions | string): misc.TDataOut;
lstatSync(path: misc.PathLike): misc.IStats<number>;
lstatSync(path: misc.PathLike, options: { throwIfNoEntry?: true | undefined }): misc.IStats<number>;
lstatSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry?: true | undefined }): misc.IStats<number>;
lstatSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry?: true | undefined }): misc.IStats<bigint>;
lstatSync(path: misc.PathLike, options: { throwIfNoEntry: false }): misc.IStats<number> | undefined;
lstatSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry: false }): misc.IStats<number> | undefined;
lstatSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry: false }): misc.IStats<bigint> | undefined;
statSync(path: misc.PathLike): misc.IStats<number>;
statSync(path: misc.PathLike, options: { throwIfNoEntry?: true }): misc.IStats<number>;
statSync(path: misc.PathLike, options: { throwIfNoEntry: false }): misc.IStats<number> | undefined;
statSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry?: true }): misc.IStats<number>;
statSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry?: true }): misc.IStats<bigint>;
statSync(path: misc.PathLike, options: { bigint: false; throwIfNoEntry: false }): misc.IStats<number> | undefined;
statSync(path: misc.PathLike, options: { bigint: true; throwIfNoEntry: false }): misc.IStats<bigint> | undefined;
fstatSync(fd: number): misc.IStats<number>;
fstatSync(fd: number, options: { bigint: false }): misc.IStats<number>;
fstatSync(fd: number, options: { bigint: true }): misc.IStats<bigint>;
renameSync(oldPath: misc.PathLike, newPath: misc.PathLike): void;
existsSync(path: misc.PathLike): boolean;
accessSync(path: misc.PathLike, mode?: number): void;
appendFileSync(id: misc.TFileId, data: misc.TData, options?: opts.IAppendFileOptions | string): void;
readdirSync(path: misc.PathLike, options?: opts.IReaddirOptions | string): misc.TDataOut[] | misc.IDirent[];
readlinkSync(path: misc.PathLike, options?: opts.IOptions): misc.TDataOut;
fsyncSync(fd: number): void;
fdatasyncSync(fd: number): void;
ftruncateSync(fd: number, len?: number): void;
truncateSync(id: misc.TFileId, len?: number): void;
futimesSync(fd: number, atime: misc.TTime, mtime: misc.TTime): void;
utimesSync(path: misc.PathLike, atime: misc.TTime, mtime: misc.TTime): void;
mkdirSync(path: misc.PathLike, options: opts.IMkdirOptions & { recursive: true }): string | undefined;
mkdirSync(path: misc.PathLike, options?: misc.TMode | (opts.IMkdirOptions & { recursive?: false })): void;
mkdirSync(path: misc.PathLike, options?: misc.TMode | opts.IMkdirOptions): string | undefined;
mkdtempSync(prefix: string, options?: opts.IOptions): misc.TDataOut;
rmdirSync(path: misc.PathLike, options?: opts.IRmdirOptions): void;
rmSync(path: misc.PathLike, options?: opts.IRmOptions): void;
fchmodSync(fd: number, mode: misc.TMode): void;
chmodSync(path: misc.PathLike, mode: misc.TMode): void;
lchmodSync(path: misc.PathLike, mode: misc.TMode): void;
fchownSync(fd: number, uid: number, gid: number): void;
chownSync(path: misc.PathLike, uid: number, gid: number): void;
lchownSync(path: misc.PathLike, uid: number, gid: number): void;
writevSync(fd: number, buffers: ArrayBufferView[], position?: number | null): void;
}
37 changes: 37 additions & 0 deletions src/node/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,40 @@ export interface IWatchOptions extends IOptions {
persistent?: boolean;
recursive?: boolean;
}

export interface ICpOptions {
/** dereference symlinks. Default: false. */
dereference?: boolean;
/**
* When force is false, and the destination exists, throw an error.
* Default: false.
*/
errorOnExist?: boolean;
/**
* Function to filter copied files/directories. Return true to copy the item,
* false to ignore it. Default: undefined.
*/
filter?: (src: string, dest: string) => boolean;
/**
* Overwrite existing file or directory. The copy operation will ignore errors
* if you set this to false and the destination exists. Use the errorOnExist
* option to change this behavior. Default: true.
*/
force?: boolean;
/**
* Integer, modifiers for copy operation. Default: 0. See mode flag of
* `fs.copyFileSync()`.
*/
mode: number;
/** When true timestamps from src will be preserved. Default: false. */
preserveTimestamps: boolean;
/** Copy directories recursively Default: false. */
recursive: boolean;
/** When true, path resolution for symlinks will be skipped. Default: false. */
verbatimSymlinks: boolean;
}

export interface IStafsOptions {
/** Whether the numeric values in the returned `StatFs` object should be bigint. */
bigint?: boolean;
}
6 changes: 3 additions & 3 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ export const getWriteSyncArgs = (
a: string | Buffer | ArrayBufferView | DataView,
b?: number,
c?: number | BufferEncoding,
d?: number,
): [fd: number, buf: Buffer, offset: number, length?: number, position?: number] => {
d?: number | null,
): [fd: number, buf: Buffer, offset: number, length?: number, position?: number | null] => {
validateFd(fd);
let encoding: BufferEncoding | undefined;
let offset: number | undefined;
let length: number | undefined;
let position: number | undefined;
let position: number | null | undefined;
const isBuffer = typeof a !== 'string';
if (isBuffer) {
offset = (b || 0) | 0;
Expand Down
10 changes: 10 additions & 0 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
unixify,
} from './node/util';
import type { PathLike, symlink } from 'fs';
import type { FsSynchronousApi } from './node/types';

const resolveCrossPlatform = pathModule.resolve;
const {
Expand Down Expand Up @@ -218,6 +219,10 @@ function flattenJSON(nestedJSON: NestedDirectoryJSON): DirectoryJSON {
return flatJSON;
}

const notImplemented: (...args: any[]) => any = () => {
throw new Error('Not implemented');
};

/**
* `Volume` represents a file system.
*/
Expand Down Expand Up @@ -1874,6 +1879,11 @@ export class Volume implements FsCallbackApi {

return watcher;
}

public cpSync: FsSynchronousApi['cpSync'] = notImplemented;
public lutimesSync: FsSynchronousApi['lutimesSync'] = notImplemented;
public statfsSync: FsSynchronousApi['statfsSync'] = notImplemented;
public writevSync: FsSynchronousApi['writevSync'] = notImplemented;
}

function emitStop(self) {
Expand Down

0 comments on commit ac38b5d

Please sign in to comment.