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 0753937 commit 1b92a84
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 56 deletions.
40 changes: 20 additions & 20 deletions src/consts/FLAG.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Constants used in `open` system calls, see [open(2)](http://man7.org/linux/man-pages/man2/open.2.html).
export const enum FLAG {
O_RDONLY = 0,
O_WRONLY = 1,
O_RDWR = 2,
O_ACCMODE = 3,
O_CREAT = 64,
O_EXCL = 128,
O_NOCTTY = 256,
O_TRUNC = 512,
O_APPEND = 1024,
O_NONBLOCK = 2048,
O_DSYNC = 4096,
FASYNC = 8192,
O_DIRECT = 16384,
O_LARGEFILE = 0,
O_DIRECTORY = 65536,
O_NOFOLLOW = 131072,
O_NOATIME = 262144,
O_CLOEXEC = 524288,
O_SYNC = 1052672,
O_NDELAY = 2048,
O_RDONLY = 0,
O_WRONLY = 1,
O_RDWR = 2,
O_ACCMODE = 3,
O_CREAT = 64,
O_EXCL = 128,
O_NOCTTY = 256,
O_TRUNC = 512,
O_APPEND = 1024,
O_NONBLOCK = 2048,
O_DSYNC = 4096,
FASYNC = 8192,
O_DIRECT = 16384,
O_LARGEFILE = 0,
O_DIRECTORY = 65536,
O_NOFOLLOW = 131072,
O_NOATIME = 262144,
O_CLOEXEC = 524288,
O_SYNC = 1052672,
O_NDELAY = 2048,
}
51 changes: 40 additions & 11 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import { FsaToNodeConstants } from './constants';
import { bufferToEncoding } from '../volume';
import { FsaNodeFsOpenFile } from './FsaNodeFsOpenFile';
import { FsaNodeDirent } from './FsaNodeDirent';
import {FLAG} from '../consts/FLAG';
import {AMODE} from '../consts/AMODE';
import { FLAG } from '../consts/FLAG';
import { AMODE } from '../consts/AMODE';
import type { FsCallbackApi, FsPromisesApi } from '../node/types';
import type * as misc from '../node/types/misc';
import type * as opts from '../node/types/options';
Expand Down Expand Up @@ -81,13 +81,23 @@ export class FsaNodeFs implements FsCallbackApi {
return curr;
}

private async getFile(path: string[], name: string, funcName?: string, create?: boolean): Promise<fsa.IFileSystemFileHandle> {
private async getFile(
path: string[],
name: string,
funcName?: string,
create?: boolean,
): Promise<fsa.IFileSystemFileHandle> {
const dir = await this.getDir(path, false, funcName);
const file = await dir.getFileHandle(name, { create });
return file;
}

private async getFileOrDir(path: string[], name: string, funcName?: string, create?: boolean): Promise<fsa.IFileSystemFileHandle | fsa.IFileSystemDirectoryHandle> {
private async getFileOrDir(
path: string[],
name: string,
funcName?: string,
create?: boolean,
): Promise<fsa.IFileSystemFileHandle | fsa.IFileSystemDirectoryHandle> {
const dir = await this.getDir(path, false, funcName);
try {
const file = await dir.getFileHandle(name);
Expand Down Expand Up @@ -121,7 +131,6 @@ export class FsaNodeFs implements FsCallbackApi {
return await dir.getFileHandle(name, { create: true });
}


// ------------------------------------------------------------ FsCallbackApi

public readonly open: FsCallbackApi['open'] = (
Expand Down Expand Up @@ -197,14 +206,24 @@ export class FsaNodeFs implements FsCallbackApi {
});
};

public readonly write: FsCallbackApi['write'] = (fd: number, a?: unknown, b?: unknown, c?: unknown, d?: unknown, e?: unknown) => {
public readonly write: FsCallbackApi['write'] = (
fd: number,
a?: unknown,
b?: unknown,
c?: unknown,
d?: unknown,
e?: unknown,
) => {
const [, asStr, buf, offset, length, position, cb] = getWriteArgs(fd, a, b, c, d, e);
(async () => {
const openFile = await this.getFileByFd(fd, 'write');
const data = buf.subarray(offset, offset + length);
await openFile.write(data, position);
return length;
})().then((bytesWritten) => cb(null, bytesWritten, asStr ? a : buf), (error) => cb(error));
})().then(
bytesWritten => cb(null, bytesWritten, asStr ? a : buf),
error => cb(error),
);
};

writeFile(id: misc.TFileId, data: misc.TData, callback: misc.TCallback<void>);
Expand Down Expand Up @@ -305,13 +324,20 @@ export class FsaNodeFs implements FsCallbackApi {
throw new Error('Not implemented');
}

public readonly exists: FsCallbackApi['exists'] = (path: misc.PathLike, callback: (exists: boolean) => void): void => {
public readonly exists: FsCallbackApi['exists'] = (
path: misc.PathLike,
callback: (exists: boolean) => void,
): void => {
const filename = pathToFilename(path);
if (typeof callback !== 'function') throw Error(ERRSTR.CB);
this.access(path, AMODE.F_OK, (error) => callback(!error));
this.access(path, AMODE.F_OK, error => callback(!error));
};

public readonly access: FsCallbackApi['access'] = (path: misc.PathLike, a: misc.TCallback<void> | number, b?: misc.TCallback<void>) => {
public readonly access: FsCallbackApi['access'] = (
path: misc.PathLike,
a: misc.TCallback<void> | number,
b?: misc.TCallback<void>,
) => {
let mode: number = AMODE.F_OK;
let callback: misc.TCallback<void>;
if (typeof a !== 'function') {
Expand Down Expand Up @@ -352,7 +378,10 @@ export class FsaNodeFs implements FsCallbackApi {
throw createError('EACCESS', 'access', filename);
}
}
})().then(() => callback(null), error => callback(error));
})().then(
() => callback(null),
error => callback(error),
);
};

public readonly appendFile: FsCallbackApi['appendFile'] = (id: misc.TFileId, data: misc.TData, a, b?) => {
Expand Down
4 changes: 2 additions & 2 deletions src/fsa-to-node/FsaNodeFsOpenFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FLAG} from '../consts/FLAG';
import { FLAG } from '../consts/FLAG';
import type * as fsa from '../fsa/types';
import type * as misc from '../node/types/misc';

Expand Down Expand Up @@ -30,7 +30,7 @@ export class FsaNodeFsOpenFile {

public async write(data: Uint8Array, seek: number | null): Promise<void> {
if (typeof seek !== 'number') seek = this.seek;
const writer = await this.file.createWritable({keepExistingData: this.keepExistingData});
const writer = await this.file.createWritable({ keepExistingData: this.keepExistingData });
await writer.write({
type: 'write',
data,
Expand Down
34 changes: 19 additions & 15 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IFsWithVolume, NestedDirectoryJSON, memfs } from '../..';
import {AMODE} from '../../consts/AMODE';
import { AMODE } from '../../consts/AMODE';
import { nodeToFsa } from '../../node-to-fsa';
import { IDirent } from '../../node/types/misc';
import { FsaNodeFs } from '../FsaNodeFs';
Expand Down Expand Up @@ -312,10 +312,12 @@ describe('.appendFile()', () => {
describe('.write()', () => {
test('can write to a file', async () => {
const { fs, mfs } = setup({});
const fd = await new Promise<number>((resolve, reject) => fs.open('/test.txt', 'w', (err, fd) => {
if (err) reject(err);
else resolve(fd!);
}));
const fd = await new Promise<number>((resolve, reject) =>
fs.open('/test.txt', 'w', (err, fd) => {
if (err) reject(err);
else resolve(fd!);
}),
);
const [bytesWritten, data] = await new Promise<[number, any]>((resolve, reject) => {
fs.write(fd, 'a', (err, bytesWritten, data) => {
if (err) reject(err);
Expand All @@ -329,10 +331,12 @@ describe('.write()', () => {

test('can write to a file twice sequentially', async () => {
const { fs, mfs } = setup({});
const fd = await new Promise<number>((resolve, reject) => fs.open('/test.txt', 'w', (err, fd) => {
if (err) reject(err);
else resolve(fd!);
}));
const fd = await new Promise<number>((resolve, reject) =>
fs.open('/test.txt', 'w', (err, fd) => {
if (err) reject(err);
else resolve(fd!);
}),
);
const res1 = await new Promise<[number, any]>((resolve, reject) => {
fs.write(fd, 'a', (err, bytesWritten, data) => {
if (err) reject(err);
Expand All @@ -357,8 +361,8 @@ describe('.exists()', () => {
test('can works for folders and files', async () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const exists = async (path: string): Promise<boolean> => {
return new Promise((resolve) => {
fs.exists(path, (exists) => resolve(exists));
return new Promise(resolve => {
fs.exists(path, exists => resolve(exists));
});
};
expect(await exists('/folder')).toBe(true);
Expand Down Expand Up @@ -393,7 +397,7 @@ describe('.access()', () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }, 'read');
try {
await fs.promises.access('/folder/file', AMODE.W_OK);
throw new Error('should not be here')
throw new Error('should not be here');
} catch (error) {
expect(error.code).toBe('EACCESS');
}
Expand All @@ -403,7 +407,7 @@ describe('.access()', () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
try {
await fs.promises.access('/folder/file', AMODE.X_OK);
throw new Error('should not be here')
throw new Error('should not be here');
} catch (error) {
expect(error.code).toBe('EACCESS');
}
Expand All @@ -430,7 +434,7 @@ describe('.access()', () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' }, 'read');
try {
await fs.promises.access('/folder', AMODE.W_OK);
throw new Error('should not be here')
throw new Error('should not be here');
} catch (error) {
expect(error.code).toBe('EACCESS');
}
Expand All @@ -440,7 +444,7 @@ describe('.access()', () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
try {
await fs.promises.access('/folder', AMODE.X_OK);
throw new Error('should not be here')
throw new Error('should not be here');
} catch (error) {
expect(error.code).toBe('EACCESS');
}
Expand Down
4 changes: 2 additions & 2 deletions src/fsa-to-node/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IFileSystemDirectoryHandle} from '../fsa/types';
import { IFileSystemDirectoryHandle } from '../fsa/types';
import { FsaToNodeConstants } from './constants';
import type { FsLocation } from './types';

Expand All @@ -13,7 +13,7 @@ export const pathToLocation = (path: string): FsLocation => {

export const testDirectoryIsWritable = async (dir: IFileSystemDirectoryHandle): Promise<boolean> => {
const testFileName = '__memfs_writable_test_file_' + Math.random().toString(36).slice(2) + Date.now();
try {
try {
await dir.getFileHandle(testFileName, { create: true });
return true;
} catch {
Expand Down
25 changes: 19 additions & 6 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,22 @@ export function dataToBuffer(data: misc.TData, encoding: string = ENCODING_UTF8)
else return bufferFrom(String(data), encoding);
}

export const getWriteArgs =
(fd: number, a?: unknown, b?: unknown, c?: unknown, d?: unknown, e?: unknown):
[fd: number, dataAsStr: boolean, buf: Buffer, offset: number, length: number, position: number | null, callback: (...args) => void] => {
export const getWriteArgs = (
fd: number,
a?: unknown,
b?: unknown,
c?: unknown,
d?: unknown,
e?: unknown,
): [
fd: number,
dataAsStr: boolean,
buf: Buffer,
offset: number,
length: number,
position: number | null,
callback: (...args) => void,
] => {
validateFd(fd);
let offset: number = 0;
let length: number | undefined;
Expand All @@ -194,14 +207,14 @@ export const getWriteArgs =
if (tipb === 'function') {
callback = <(...args) => void>b;
} else if (tipc === 'function') {
offset = <number>b | 0;
offset = (<number>b) | 0;
callback = <(...args) => void>c;
} else if (tipd === 'function') {
offset = <number>b | 0;
offset = (<number>b) | 0;
length = <number>c;
callback = <(...args) => void>d;
} else {
offset = <number>b | 0;
offset = (<number>b) | 0;
length = <number>c;
position = <number | null>d;
callback = <(...args) => void>e;
Expand Down

0 comments on commit 1b92a84

Please sign in to comment.