Skip to content

Commit

Permalink
Enabled strict mode
Browse files Browse the repository at this point in the history
Fixed `WebAccessFS.stat` returning undefined for invalid handles
  • Loading branch information
james-pre committed May 7, 2024
1 parent 96dd107 commit 5f21dde
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
"typescript": "5.2.2"
},
"peerDependencies": {
"@zenfs/core": "^0.9.4"
"@zenfs/core": "^0.9.7"
}
}
6 changes: 3 additions & 3 deletions src/IndexedDB.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { AsyncStore, AsyncStoreOptions, AsyncTransaction, Backend, Ino } from '@zenfs/core';
import { AsyncStoreFS } from '@zenfs/core';
import { convertException } from './utils.js';
import { convertException, type ConvertException } from './utils.js';

function wrap<T>(request: IDBRequest<T>): Promise<T> {
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(request.result);
request.onerror = e => {
e.preventDefault();
reject(convertException(request.error));
reject(convertException(request.error!));
};
});
}
Expand Down Expand Up @@ -45,7 +45,7 @@ export class IndexedDBTransaction implements AsyncTransaction {
try {
this.tx.abort();
} catch (e) {
throw convertException(e);
throw convertException(e as ConvertException);
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/access.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Backend, FileSystemMetadata } from '@zenfs/core';
import { ApiError, Async, ErrorCode, FileSystem, FileType, InMemory, PreloadFile, Stats } from '@zenfs/core';
import { basename, dirname, join } from '@zenfs/core/emulation/path.js';
import { convertException } from './utils.js';
import { convertException, type ConvertException } from './utils.js';

declare global {
interface FileSystemDirectoryHandle {
Expand Down Expand Up @@ -75,7 +75,7 @@ export class WebAccessFS extends Async(FileSystem) {
writable.close();
await this.unlink(oldPath);
} catch (ex) {
throw convertException(ex, oldPath, 'rename');
throw convertException(ex as ConvertException, oldPath, 'rename');
}
}

Expand Down Expand Up @@ -108,6 +108,7 @@ export class WebAccessFS extends Async(FileSystem) {
const { lastModified, size } = await handle.getFile();
return new Stats({ mode: 0o777 | FileType.FILE, size, mtimeMs: lastModified });
}
throw new ApiError(ErrorCode.EBADE, 'Handle is not a directory or file', path, 'stat');
}

public async openFile(path: string, flag: string): Promise<PreloadFile<this>> {
Expand All @@ -121,7 +122,7 @@ export class WebAccessFS extends Async(FileSystem) {
const stats = new Stats({ mode: 0o777 | FileType.FILE, size: file.size, mtimeMs: file.lastModified });
return new PreloadFile(this, path, flag, stats, data);
} catch (ex) {
throw convertException(ex, path, 'openFile');
throw convertException(ex as ConvertException, path, 'openFile');
}
}

Expand All @@ -131,7 +132,7 @@ export class WebAccessFS extends Async(FileSystem) {
try {
await handle.removeEntry(basename(path), { recursive: true });
} catch (ex) {
throw convertException(ex, path, 'unlink');
throw convertException(ex as ConvertException, path, 'unlink');
}
}
}
Expand Down Expand Up @@ -171,7 +172,7 @@ export class WebAccessFS extends Async(FileSystem) {

protected async getHandle(path: string): Promise<FileSystemHandle> {
if (this._handles.has(path)) {
return this._handles.get(path);
return this._handles.get(path)!;
}

let walked = '/';
Expand All @@ -186,13 +187,14 @@ export class WebAccessFS extends Async(FileSystem) {
try {
const dirHandle = await handle.getDirectoryHandle(part);
this._handles.set(walked, dirHandle);
} catch (ex) {
} catch (_ex) {
const ex = _ex as DOMException;
if (ex.name == 'TypeMismatchError') {
try {
const fileHandle = await handle.getFileHandle(part);
this._handles.set(walked, fileHandle);
} catch (ex) {
convertException(ex, walked, 'getHandle');
convertException(ex as ConvertException, walked, 'getHandle');
}
}

Expand All @@ -204,7 +206,7 @@ export class WebAccessFS extends Async(FileSystem) {
}
}

return this._handles.get(path);
return this._handles.get(path)!;
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,23 @@ function errnoForDOMException(ex: DOMException): keyof typeof ErrorCode {
}
}

/**
* @internal
*/
export type ConvertException = ApiError | DOMException | Error;

/**
* Handles converting errors, then rethrowing them
* @internal
*/
export function convertException(ex: Error | ApiError | DOMException, path?: string, syscall?: string): ApiError {
export function convertException(ex: ConvertException, path?: string, syscall?: string): ApiError {
if (ex instanceof ApiError) {
return ex;
}

const code = ex instanceof DOMException ? ErrorCode[errnoForDOMException(ex)] : ErrorCode.EIO;
const error = new ApiError(code, ex.message, path, syscall);
error.stack = ex.stack;
error.stack = ex.stack!;
error.cause = ex.cause;
return error;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"target": "ES2020",
"outDir": "dist",
"lib": ["ESNext", "DOM"],
"strict": true,
"moduleResolution": "NodeNext",
"declaration": true
},
Expand Down

0 comments on commit 5f21dde

Please sign in to comment.