Skip to content

Commit 02c2b6c

Browse files
Copilotstreamich
andcommitted
feat: revert index.ts changes and implement queryPermission method
Co-authored-by: streamich <[email protected]>
1 parent c7c268c commit 02c2b6c

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

src/fsa/CoreFileSystemDirectoryHandle.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
GetFileHandleOptions,
1616
IFileSystemDirectoryHandle,
1717
IFileSystemFileHandle,
18+
IFileSystemHandle,
1819
RemoveEntryOptions,
1920
} from './types';
2021
import type { Superblock } from '../core/Superblock';
@@ -27,7 +28,7 @@ import { MODE, FLAGS } from '../node/constants';
2728
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
2829
*/
2930
export class CoreFileSystemDirectoryHandle extends CoreFileSystemHandle implements IFileSystemDirectoryHandle {
30-
protected readonly ctx: Partial<CoreFsaContext>;
31+
protected readonly ctx: CoreFsaContext;
3132
/** Directory path with trailing slash. */
3233
public readonly __path: string;
3334

@@ -36,8 +37,9 @@ export class CoreFileSystemDirectoryHandle extends CoreFileSystemHandle implemen
3637
path: string,
3738
ctx: Partial<CoreFsaContext> = {},
3839
) {
39-
super('directory', basename(path, ctx.separator || '/'));
40-
this.ctx = createCtx(ctx);
40+
const fullCtx = createCtx(ctx);
41+
super('directory', basename(path, fullCtx.separator), fullCtx);
42+
this.ctx = fullCtx;
4143
this.__path = path[path.length - 1] === this.ctx.separator ? path : path + this.ctx.separator;
4244
}
4345

@@ -242,7 +244,7 @@ export class CoreFileSystemDirectoryHandle extends CoreFileSystemHandle implemen
242244
* @param possibleDescendant The {@link CoreFileSystemHandle} from which
243245
* to return the relative path.
244246
*/
245-
public async resolve(possibleDescendant: CoreFileSystemHandle): Promise<string[] | null> {
247+
public async resolve(possibleDescendant: IFileSystemHandle): Promise<string[] | null> {
246248
if (
247249
possibleDescendant instanceof CoreFileSystemDirectoryHandle ||
248250
possibleDescendant instanceof CoreFileSystemFileHandle

src/fsa/CoreFileSystemFileHandle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export class CoreFileSystemFileHandle extends CoreFileSystemHandle implements IF
2020
public readonly __path: string,
2121
ctx: Partial<CoreFsaContext> = {},
2222
) {
23-
ctx = createCtx(ctx);
24-
super('file', basename(__path, ctx.separator!));
25-
this.ctx = ctx as CoreFsaContext;
23+
const fullCtx = createCtx(ctx);
24+
super('file', basename(__path, fullCtx.separator), fullCtx);
25+
this.ctx = fullCtx;
2626
}
2727

2828
/**

src/fsa/CoreFileSystemHandle.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CorePermissionStatus } from './CorePermissionStatus';
2-
import type { IFileSystemHandle, FileSystemHandlePermissionDescriptor } from './types';
2+
import type { IFileSystemHandle, FileSystemHandlePermissionDescriptor, CoreFsaContext } from './types';
33

44
/**
55
* Represents a File System Access API file handle `FileSystemHandle` object,
@@ -8,10 +8,15 @@ import type { IFileSystemHandle, FileSystemHandlePermissionDescriptor } from './
88
* @see [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle)
99
*/
1010
export abstract class CoreFileSystemHandle implements IFileSystemHandle {
11+
protected readonly ctx: CoreFsaContext;
12+
1113
constructor(
1214
public readonly kind: 'file' | 'directory',
1315
public readonly name: string,
14-
) {}
16+
ctx: CoreFsaContext,
17+
) {
18+
this.ctx = ctx;
19+
}
1520

1621
/**
1722
* Compares two handles to see if the associated entries (either a file or directory) match.
@@ -30,7 +35,17 @@ export abstract class CoreFileSystemHandle implements IFileSystemHandle {
3035
public queryPermission(
3136
fileSystemHandlePermissionDescriptor: FileSystemHandlePermissionDescriptor,
3237
): CorePermissionStatus {
33-
throw new Error('Not implemented');
38+
// Check if the requested mode is compatible with the context mode
39+
const requestedMode = fileSystemHandlePermissionDescriptor.mode;
40+
const contextMode = this.ctx.mode;
41+
42+
// If requesting readwrite but context only allows read, deny
43+
if (requestedMode === 'readwrite' && contextMode === 'read') {
44+
return new CorePermissionStatus('denied', requestedMode);
45+
}
46+
47+
// Otherwise grant the permission
48+
return new CorePermissionStatus('granted', requestedMode);
3449
}
3550

3651
/**

src/fsa/__tests__/CoreFileSystemHandle.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,35 @@ onlyOnNode20('CoreFileSystemHandle', () => {
5252
expect(file.isSameEntry(folder)).toBe(false);
5353
});
5454

55-
test('queryPermission throws not implemented error', async () => {
55+
test('queryPermission returns permission status based on context mode', async () => {
5656
const { dir } = setup({ 'test.txt': 'content' });
5757
const file = await dir.getFileHandle('test.txt');
58-
expect(() => file.queryPermission({ mode: 'read' })).toThrow('Not implemented');
58+
59+
// Test read permission request (should be granted since context allows readwrite)
60+
const readPermission = file.queryPermission({ mode: 'read' });
61+
expect(readPermission.state).toBe('granted');
62+
expect(readPermission.name).toBe('read');
63+
64+
// Test readwrite permission request (should be granted since context allows readwrite)
65+
const readwritePermission = file.queryPermission({ mode: 'readwrite' });
66+
expect(readwritePermission.state).toBe('granted');
67+
expect(readwritePermission.name).toBe('readwrite');
68+
});
69+
70+
test('queryPermission denies readwrite when context only allows read', async () => {
71+
const core = Superblock.fromJSON({ 'test.txt': 'content' }, '/');
72+
const dir = new CoreFileSystemDirectoryHandle(core, '/', { mode: 'read' });
73+
const file = await dir.getFileHandle('test.txt');
74+
75+
// Test read permission request (should be granted)
76+
const readPermission = file.queryPermission({ mode: 'read' });
77+
expect(readPermission.state).toBe('granted');
78+
expect(readPermission.name).toBe('read');
79+
80+
// Test readwrite permission request (should be denied since context only allows read)
81+
const readwritePermission = file.queryPermission({ mode: 'readwrite' });
82+
expect(readwritePermission.state).toBe('denied');
83+
expect(readwritePermission.name).toBe('readwrite');
5984
});
6085

6186
test('requestPermission throws not implemented error', async () => {

src/index.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,6 @@ import type { FsPromisesApi } from './node/types';
77
import type * as misc from './node/types/misc';
88
import { fsSynchronousApiList } from './node/lists/fsSynchronousApiList';
99
import { fsCallbackApiList } from './node/lists/fsCallbackApiList';
10-
11-
// Re-export core FSA implementation
12-
export {
13-
CoreFileSystemHandle,
14-
CoreFileSystemDirectoryHandle,
15-
CoreFileSystemFileHandle,
16-
CoreFileSystemSyncAccessHandle,
17-
CoreFileSystemWritableFileStream,
18-
CorePermissionStatus,
19-
coreToFsa,
20-
type CoreFsaContext,
21-
} from './fsa';
22-
23-
// Re-export node-to-fsa implementation
24-
export {
25-
NodeFileSystemHandle,
26-
NodeFileSystemDirectoryHandle,
27-
NodeFileSystemFileHandle,
28-
nodeToFsa,
29-
type NodeFsaContext,
30-
type NodeFsaFs,
31-
} from './node-to-fsa';
32-
3310
const { F_OK, R_OK, W_OK, X_OK } = constants;
3411

3512
export { DirectoryJSON, NestedDirectoryJSON, Volume };

0 commit comments

Comments
 (0)