Skip to content

Commit

Permalink
feat: 🎸 implement openSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 7a2fced commit 8ecac69
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
await writeHandle.close();
strictEqual(bytesWritten, 1);
strictEqual(fs.readFileSync('/cool.txt', 'utf8'), 'Worlds!');

console.log('openSync() - can create a file');
strictEqual(fs.existsSync('/new-file.txt'), false);
const fd = fs.openSync('/new-file.txt', 'w');
strictEqual(fs.existsSync('/new-file.txt'), true);
strictEqual(typeof fd, 'number');
};

const main = async () => {
Expand Down
8 changes: 5 additions & 3 deletions src/fsa-to-node/FsaNodeCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export class FsaNodeCore {
*/
protected async getDir(path: string[], create: boolean, funcName?: string): Promise<fsa.IFileSystemDirectoryHandle> {
let curr: fsa.IFileSystemDirectoryHandle = this.root;

const options: fsa.GetDirectoryHandleOptions = { create };

try {
for (const name of path) {
curr = await curr.getDirectoryHandle(name, options);
Expand Down Expand Up @@ -100,7 +98,7 @@ export class FsaNodeCore {
return this.getFileByFd(fd, funcName);
}

protected async getFileById(
public async __getFileById(
id: misc.TFileId,
funcName?: string,
create?: boolean,
Expand All @@ -123,6 +121,10 @@ export class FsaNodeCore {
const [folder, name] = pathToLocation(filename);
const createIfMissing = !!(flags & FLAG.O_CREAT);
const fsaFile = await this.getFile(folder, name, 'open', createIfMissing);
return this.__open2(fsaFile, filename, flags, mode);
}

protected __open2(fsaFile: fsa.IFileSystemFileHandle, filename: string, flags: number, mode: number): FsaNodeFsOpenFile {
const fd = this.newFdNumber();
const file = new FsaNodeFsOpenFile(fd, mode, flags, fsaFile, filename);
this.fds.set(fd, file);
Expand Down
14 changes: 11 additions & 3 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
) => {
const [opts, callback] = optsAndCbGenerator<opts.IReadFileOptions, misc.TDataOut>(getReadFileOptions)(a, b);
const flagsNum = flagsToNumber(opts.flag);
return this.getFileById(id, 'readFile')
return this.__getFileById(id, 'readFile')
.then(file => file.getFile())
.then(file => file.arrayBuffer())
.then(data => {
Expand Down Expand Up @@ -227,7 +227,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
const buf = dataToBuffer(data, opts.encoding);
(async () => {
const createIfMissing = !!(flagsNum & FLAG.O_CREAT);
const file = await this.getFileById(id, 'writeFile', createIfMissing);
const file = await this.__getFileById(id, 'writeFile', createIfMissing);
const writable = await file.createWritable({ keepExistingData: false });
await writable.write(buf);
await writable.close();
Expand Down Expand Up @@ -988,7 +988,15 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
return this.getSyncAdapter().call('write', [filename, data, position || null]);
};

public readonly openSync: FsSynchronousApi['openSync'] = notSupported;
public readonly openSync: FsSynchronousApi['openSync'] = (path: misc.PathLike, flags: misc.TFlags, mode: misc.TMode = MODE.DEFAULT): number => {
const modeNum = modeToNumber(mode);
const filename = pathToFilename(path);
const flagsNum = flagsToNumber(flags);
const adapter = this.getSyncAdapter();
const handle = adapter.call('open', [filename, flagsNum, modeNum]);
const openFile = this.__open2(handle, filename, flagsNum, modeNum);
return openFile.fd;
};

public readonly chmodSync: FsSynchronousApi['chmodSync'] = noop;
public readonly chownSync: FsSynchronousApi['chownSync'] = noop;
Expand Down
2 changes: 2 additions & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {IFileSystemFileHandle} from '../fsa/types';
import type * as opts from '../node/types/options';
import type * as misc from '../node/types/misc';

Expand Down Expand Up @@ -31,6 +32,7 @@ export interface FsaNodeSyncAdapterApi {
data: Uint8Array,
position: number | null,
]): number;
open(req: [filename: string, flags: number, mode: number]): IFileSystemFileHandle;
}

export interface FsaNodeSyncAdapter {
Expand Down
6 changes: 6 additions & 0 deletions src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,11 @@ export class FsaNodeSyncWorker {
const {bytesWritten} = await handle.write(data, 0, data.length, position || undefined);
return bytesWritten;
},
open: async ([filename, flags, mode]): Promise<fsa.IFileSystemFileHandle> => {
const handle = await this.fs.promises.open(filename, flags, mode);
const file = await this.fs.__getFileById(handle.fd);
await handle.close();
return file;
},
};
}

0 comments on commit 8ecac69

Please sign in to comment.