Skip to content

Commit

Permalink
fix: 🐛 correctly handle directory paths
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 21, 2023
1 parent 403c271 commit ea909e8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/node-to-fsa/NodeFileSystemDirectoryHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ import type {
*/
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implements IFileSystemDirectoryHandle {
protected readonly ctx: Partial<NodeFsaContext>;
/** Directory path with trailing slash. */
public readonly __path: string;

public constructor(
protected readonly fs: NodeFsaFs,
public readonly __path: string,
path: string,
ctx: Partial<NodeFsaContext> = {},
) {
super('directory', basename(__path, ctx.separator || '/'));
super('directory', basename(path, ctx.separator || '/'));
this.ctx = createCtx(ctx);
this.__path = path[path.length - 1] === this.ctx.separator ? path : path + this.ctx.separator;
}

/**
Expand Down Expand Up @@ -84,7 +88,7 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implemen
options?: GetDirectoryHandleOptions,
): Promise<IFileSystemDirectoryHandle> {
assertName(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle');
const filename = this.__path + this.ctx.separator! + name;
const filename = this.__path + name;
try {
const stats = await this.fs.promises.stat(filename);
if (!stats.isDirectory()) throw newTypeMismatchError();
Expand Down Expand Up @@ -121,7 +125,7 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implemen
*/
public async getFileHandle(name: string, options?: GetFileHandleOptions): Promise<IFileSystemFileHandle> {
assertName(name, 'getFileHandle', 'FileSystemDirectoryHandle');
const filename = this.__path + this.ctx.separator! + name;
const filename = this.__path + name;
try {
const stats = await this.fs.promises.stat(filename);
if (!stats.isFile()) throw newTypeMismatchError();
Expand Down Expand Up @@ -159,7 +163,7 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implemen
public async removeEntry(name: string, { recursive = false }: RemoveEntryOptions = {}): Promise<void> {
assertCanWrite(this.ctx.mode!);
assertName(name, 'removeEntry', 'FileSystemDirectoryHandle');
const filename = this.__path + this.ctx.separator! + name;
const filename = this.__path + name;
const promises = this.fs.promises;
try {
const stats = await promises.stat(filename);
Expand Down
5 changes: 5 additions & 0 deletions src/node-to-fsa/__tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ describe('basename()', () => {
expect(basename('scary.exe', '/')).toBe('scary.exe');
});

test('ignores slash, if it is the last char', () => {
expect(basename('scary.exe/', '/')).toBe('scary.exe');
expect(basename('/ab/c/scary.exe/', '/')).toBe('scary.exe');
});

test('returns last step in path', () => {
expect(basename('/gg/wp/hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('gg/wp/hf/gl.txt', '/')).toBe('gl.txt');
Expand Down
24 changes: 11 additions & 13 deletions src/node-to-fsa/types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import type { IFs } from '..';
import type {FsPromisesApi, FsSynchronousApi} from '../node/types';
import type {FsCommonObjects} from '../node/types/FsCommonObjects';

/**
* Required Node.js `fs` module functions for File System Access API.
*/
export type NodeFsaFs = Pick<
IFs,
| 'promises'
| 'constants'
| 'openSync'
| 'fsyncSync'
| 'statSync'
| 'closeSync'
| 'readSync'
| 'truncateSync'
| 'writeSync'
>;
export type NodeFsaFs = Pick<FsCommonObjects, 'constants'> & {promises: FsPromisesApi} &
Pick<FsSynchronousApi,
| 'openSync'
| 'fsyncSync'
| 'statSync'
| 'closeSync'
| 'readSync'
| 'truncateSync'
| 'writeSync'>;

export interface NodeFsaContext {
separator: '/' | '\\';
Expand Down
1 change: 1 addition & 0 deletions src/node-to-fsa/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ctx = (partial: Partial<NodeFsaContext> = {}): NodeFsaContext => {
};

export const basename = (path: string, separator: string) => {
if (path[path.length - 1] === separator) path = path.slice(0, -1);
const lastSlashIndex = path.lastIndexOf(separator);
return lastSlashIndex === -1 ? path : path.slice(lastSlashIndex + 1);
};
Expand Down

0 comments on commit ea909e8

Please sign in to comment.