Skip to content

Commit

Permalink
feat: 🎸 implement accessSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 0d87cef commit accebdb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
const list = await fs.promises.readdir('');
console.log(list);

// fs.accessSync('/test.txt', fs.constants.F_OK);
console.log('/test.txt', fs.statSync('/test.txt'), fs.statSync('/test.txt').isFile(), fs.statSync('/test.txt').isDirectory());
console.log('/dir', fs.statSync('/dir'), fs.statSync('/dir').isFile(), fs.statSync('/dir').isDirectory());
// await fs.promises.mkdir('storage/a/b/c', {recursive: true});
Expand Down
3 changes: 3 additions & 0 deletions demo/fsa/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(self as any).process = require('process/browser');
(self as any).Buffer = require('buffer').Buffer;

import {FsaNodeSyncWorker} from "../../src/fsa-to-node/worker/FsaNodeSyncWorker";

if (typeof window === 'undefined') {
Expand Down
6 changes: 6 additions & 0 deletions src/fsa-to-node/FsaNodeCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export class FsaNodeCore {
protected syncAdapter?: FsaNodeSyncAdapter,
) {}

protected getSyncAdapter(): FsaNodeSyncAdapter {
const adapter = this.syncAdapter;
if (!adapter) throw new Error('No sync adapter');
return adapter;
}

/**
* A list of reusable (opened and closed) file descriptors, that should be
* used first before creating a new file descriptor.
Expand Down
11 changes: 8 additions & 3 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,18 +761,23 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
options?: opts.IStatOptions,
): misc.IStats<any> => {
const { bigint = true, throwIfNoEntry = true } = getStatOptions(options);
const adapter = this.syncAdapter;
if (!adapter) throw new Error('No sync adapter');
const filename = pathToFilename(path);
const location = pathToLocation(filename);
const adapter = this.getSyncAdapter();
const res = adapter.call('stat', location);
const stats = new FsaNodeStats(bigint, res.size ?? 0, res.kind);
return stats;
};

public readonly lstatSync: FsSynchronousApi['lstatSync'] = this.statSync;

public readonly accessSync: FsSynchronousApi['accessSync'] = noop;
public readonly accessSync: FsSynchronousApi['accessSync'] = (path: misc.PathLike, mode: number = AMODE.F_OK): void => {
const filename = pathToFilename(path);
mode = mode | 0;
const adapter = this.getSyncAdapter();
adapter.call('access', {filename, mode});
};

public readonly appendFileSync: FsSynchronousApi['appendFileSync'] = notSupported;
public readonly chmodSync: FsSynchronousApi['chmodSync'] = noop;
public readonly chownSync: FsSynchronousApi['chownSync'] = noop;
Expand Down
1 change: 1 addition & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type FsLocation = [folder: string[], file: string];
*/
export interface FsaNodeSyncAdapterApi {
stat(location: FsLocation): FsaNodeSyncAdapterStats;
access(req: {filename: string, mode: number}): void;
}

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
@@ -1,6 +1,7 @@
import { AsyncCallback, SyncMessenger } from './SyncMessenger';
import { encode, decode } from 'json-joy/es6/json-pack/msgpack/util';
import { FsaNodeWorkerMessageCode } from './constants';
import {FsaNodeFs} from '../FsaNodeFs';
import type * as fsa from '../../fsa/types';
import type {
FsaNodeWorkerError,
Expand All @@ -15,6 +16,7 @@ export class FsaNodeSyncWorker {
protected readonly sab: SharedArrayBuffer = new SharedArrayBuffer(1024 * 32);
protected readonly messenger = new SyncMessenger(this.sab);
protected root!: fsa.IFileSystemDirectoryHandle;
protected fs!: FsaNodeFs;

public start() {
onmessage = e => {
Expand All @@ -31,6 +33,7 @@ export class FsaNodeSyncWorker {
case FsaNodeWorkerMessageCode.SetRoot: {
const [, id, dir] = msg;
this.root = dir;
this.fs = new FsaNodeFs(this.root);
const response: FsaNodeWorkerMsgRootSet = [FsaNodeWorkerMessageCode.RootSet, id];
postMessage(response);
this.messenger.serveAsync(this.onRequest);
Expand Down Expand Up @@ -118,5 +121,8 @@ export class FsaNodeSyncWorker {
kind: handle.kind,
};
},
access: async ({filename, mode}): Promise<void> => {
await this.fs.promises.access(filename, mode);
},
};
}

0 comments on commit accebdb

Please sign in to comment.