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 15, 2023
1 parent fbe4592 commit 0001df2
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 53 deletions.
11 changes: 7 additions & 4 deletions src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ export class NodeFileSystemSyncAccessHandle {
/**
* Writes the content of a specified buffer to the file associated with the
* handle, optionally at a given offset.
*
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle/write
* @param buffer
* @param options
* @param buffer
* @param options
*/
public async write(buffer: ArrayBuffer | ArrayBufferView | DataView, options: FileSystemReadWriteOptions = {}): Promise<number> {
public async write(
buffer: ArrayBuffer | ArrayBufferView | DataView,
options: FileSystemReadWriteOptions = {},
): Promise<number> {
const buf: Buffer | ArrayBufferView = buffer instanceof ArrayBuffer ? Buffer.from(buffer) : buffer;
try {
return this.fs.writeSync(this.fd, buf, 0, buffer.byteLength, options.at || 0);
Expand Down
24 changes: 11 additions & 13 deletions src/node-to-fsa/NodeFileSystemWritableFileStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import type { NodeFsaFs } from './types';
* `.crswap` and then replaces the original file with the copy only when the
* `close()` method is called. If the `abort()` method is called, the `.crswap`
* file is deleted.
*
*
* If a file name with with extension `.crswap` is already taken, it
* creates a new swap file with extension `.1.crswap` and so on.
*/
export const createSwapFile = async (fs: NodeFsaFs, path: string, keepExistingData: boolean): Promise<[handle: IFileHandle, path: string]> => {
export const createSwapFile = async (
fs: NodeFsaFs,
path: string,
keepExistingData: boolean,
): Promise<[handle: IFileHandle, path: string]> => {
let handle: undefined | IFileHandle;
let swapPath: string = path + '.crswap';
try {
handle = await fs.promises.open(swapPath, 'ax');
} catch (error) {
if (!error || typeof error !== 'object' || error.code !== 'EEXIST')
throw error;
if (!error || typeof error !== 'object' || error.code !== 'EEXIST') throw error;
}
if (!handle) {
for (let i = 1; i < 1000; i++) {
Expand All @@ -26,18 +29,15 @@ export const createSwapFile = async (fs: NodeFsaFs, path: string, keepExistingDa
handle = await fs.promises.open(swapPath, 'ax');
break;
} catch (error) {
if (!error || typeof error !== 'object' || error.code !== 'EEXIST')
throw error;
if (!error || typeof error !== 'object' || error.code !== 'EEXIST') throw error;
}
}
}
if (!handle) throw new Error(`Could not create a swap file for "${path}".`);
if (keepExistingData)
await fs.promises.copyFile(path, swapPath, fs.constants.COPYFILE_FICLONE);
if (keepExistingData) await fs.promises.copyFile(path, swapPath, fs.constants.COPYFILE_FICLONE);
return [handle, swapPath];
};


interface SwapFile {
/** Swap file full path name. */
path: string;
Expand Down Expand Up @@ -155,14 +155,12 @@ export class NodeFileSystemWritableFileStream extends WritableStream {
return this.writeBase(params.data);
}
case 'truncate': {
if (typeof params.size !== 'number')
throw new TypeError('Missing required argument: size');
if (typeof params.size !== 'number') throw new TypeError('Missing required argument: size');
if (this.swap.offset > params.size) this.swap.offset = params.size;
return this.truncate(params.size);
}
case 'seek':
if (typeof params.position !== 'number')
throw new TypeError('Missing required argument: position');
if (typeof params.position !== 'number') throw new TypeError('Missing required argument: position');
return this.seek(params.position);
default:
throw new TypeError('Invalid argument: params');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ maybe('NodeFileSystemSyncAccessHandle', () => {
});
const entry = await dir.getFileHandle('file.txt');
const sync = await entry.createSyncAccessHandle!();
const res = await sync.write(Buffer.from('Hello'), {at: 7});
const res = await sync.write(Buffer.from('Hello'), { at: 7 });
expect(res).toBe(5);
expect(fs.readFileSync('/file.txt', 'utf8')).toBe('0123456Hello');
});
Expand Down Expand Up @@ -173,7 +173,7 @@ maybe('NodeFileSystemSyncAccessHandle', () => {
const entry = await dir.getFileHandle('file.txt');
const sync = await entry.createSyncAccessHandle!();
try {
await sync.write(Buffer.from('a'), {at: 100});
await sync.write(Buffer.from('a'), { at: 100 });
// ?
} catch (error) {
// ?
Expand Down
78 changes: 48 additions & 30 deletions src/node-to-fsa/__tests__/NodeFileSystemWritableFileStream.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {IFsWithVolume, memfs} from '../..';
import {FileHandle} from '../../promises';
import {createSwapFile} from '../NodeFileSystemWritableFileStream';
import { IFsWithVolume, memfs } from '../..';
import { FileHandle } from '../../promises';
import { createSwapFile } from '../NodeFileSystemWritableFileStream';

describe('createSwapFile()', () => {
test('can create a swap file', async () => {
const fs = memfs({
'/file.txt': 'Hello, world!',
}, '/') as IFsWithVolume;
const fs = memfs(
{
'/file.txt': 'Hello, world!',
},
'/',
) as IFsWithVolume;
const handle = await createSwapFile(fs, '/file.txt', false);
expect(handle).toBeInstanceOf(FileHandle);
expect(fs.__vol.toJSON()).toEqual({
Expand All @@ -16,9 +19,12 @@ describe('createSwapFile()', () => {
});

test('can create a swap file at subfolder', async () => {
const fs = memfs({
'/foo/file.txt': 'Hello, world!',
}, '/') as IFsWithVolume;
const fs = memfs(
{
'/foo/file.txt': 'Hello, world!',
},
'/',
) as IFsWithVolume;
const handle = await createSwapFile(fs, '/foo/file.txt', false);
expect(handle).toBeInstanceOf(FileHandle);
expect(fs.__vol.toJSON()).toEqual({
Expand All @@ -28,10 +34,13 @@ describe('createSwapFile()', () => {
});

test('can create a swap file when the default swap file name is in use', async () => {
const fs = memfs({
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
}, '/') as IFsWithVolume;
const fs = memfs(
{
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
},
'/',
) as IFsWithVolume;
const handle = await createSwapFile(fs, '/foo/file.txt', false);
expect(handle).toBeInstanceOf(FileHandle);
expect(fs.__vol.toJSON()).toEqual({
Expand All @@ -42,11 +51,14 @@ describe('createSwapFile()', () => {
});

test('can create a swap file when the first two names are already taken', async () => {
const fs = memfs({
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
'/foo/file.txt.1.crswap': 'blah',
}, '/') as IFsWithVolume;
const fs = memfs(
{
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
'/foo/file.txt.1.crswap': 'blah',
},
'/',
) as IFsWithVolume;
const handle = await createSwapFile(fs, '/foo/file.txt', false);
expect(handle).toBeInstanceOf(FileHandle);
expect(fs.__vol.toJSON()).toEqual({
Expand All @@ -58,12 +70,15 @@ describe('createSwapFile()', () => {
});

test('can create a swap file when the first three names are already taken', async () => {
const fs = memfs({
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
'/foo/file.txt.1.crswap': 'blah',
'/foo/file.txt.2.crswap': 'brawo',
}, '/') as IFsWithVolume;
const fs = memfs(
{
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
'/foo/file.txt.1.crswap': 'blah',
'/foo/file.txt.2.crswap': 'brawo',
},
'/',
) as IFsWithVolume;
const handle = await createSwapFile(fs, '/foo/file.txt', false);
expect(handle).toBeInstanceOf(FileHandle);
expect(fs.__vol.toJSON()).toEqual({
Expand All @@ -76,12 +91,15 @@ describe('createSwapFile()', () => {
});

test('can copy existing data into the swap file', async () => {
const fs = memfs({
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
'/foo/file.txt.1.crswap': 'blah',
'/foo/file.txt.2.crswap': 'brawo',
}, '/') as IFsWithVolume;
const fs = memfs(
{
'/foo/file.txt': 'Hello, world!',
'/foo/file.txt.crswap': 'lala',
'/foo/file.txt.1.crswap': 'blah',
'/foo/file.txt.2.crswap': 'brawo',
},
'/',
) as IFsWithVolume;
const handle = await createSwapFile(fs, '/foo/file.txt', true);
expect(handle).toBeInstanceOf(FileHandle);
expect(fs.__vol.toJSON()).toEqual({
Expand Down
10 changes: 9 additions & 1 deletion src/node-to-fsa/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import type { IFs } from '..';
*/
export type NodeFsaFs = Pick<
IFs,
'promises' | 'constants' | 'openSync' | 'fsyncSync' | 'statSync' | 'closeSync' | 'readSync' | 'truncateSync' | 'writeSync'
| 'promises'
| 'constants'
| 'openSync'
| 'fsyncSync'
| 'statSync'
| 'closeSync'
| 'readSync'
| 'truncateSync'
| 'writeSync'
>;

export interface NodeFsaContext {
Expand Down
24 changes: 21 additions & 3 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,9 +1199,21 @@ export class Volume {
return file.write(buf, offset, length, position);
}

writeSync(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset?: number, length?: number, position?: number): number;
writeSync(
fd: number,
buffer: Buffer | ArrayBufferView | DataView,
offset?: number,
length?: number,
position?: number,
): number;
writeSync(fd: number, str: string, position?: number, encoding?: BufferEncoding): number;
writeSync(fd: number, a: string | Buffer | ArrayBufferView | DataView, b?: number, c?: number | BufferEncoding, d?: number): number {
writeSync(
fd: number,
a: string | Buffer | ArrayBufferView | DataView,
b?: number,
c?: number | BufferEncoding,
d?: number,
): number {
validateFd(fd);

let encoding: BufferEncoding | undefined;
Expand Down Expand Up @@ -1235,7 +1247,13 @@ export class Volume {

write(fd: number, buffer: Buffer | ArrayBufferView | DataView, callback: (...args) => void);
write(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, callback: (...args) => void);
write(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, callback: (...args) => void);
write(
fd: number,
buffer: Buffer | ArrayBufferView | DataView,
offset: number,
length: number,
callback: (...args) => void,
);
write(
fd: number,
buffer: Buffer | ArrayBufferView | DataView,
Expand Down

0 comments on commit 0001df2

Please sign in to comment.