From f6d57ab204ce0ce982f21c42c61dd97cf144a8d3 Mon Sep 17 00:00:00 2001 From: "James P." Date: Tue, 14 May 2024 13:22:55 -0500 Subject: [PATCH] Updated types (needs https://github.com/DefinitelyTyped/DefinitelyTyped/pull/69611) --- src/backend.ts | 16 ++++++------- src/emscripten.ts | 57 +++++++++-------------------------------------- src/plugin.ts | 46 ++++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 79 deletions(-) diff --git a/src/backend.ts b/src/backend.ts index d4a008c..06a646a 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -1,12 +1,12 @@ -import { FileSystemMetadata, Sync, FileSystem } from '@zenfs/core/filesystem.js'; -import { Stats, FileType } from '@zenfs/core/stats.js'; -import { File } from '@zenfs/core/file.js'; -import { ErrnoError, Errno, errorMessages } from '@zenfs/core/error.js'; -import { Cred } from '@zenfs/core/cred.js'; -import { Buffer } from 'buffer'; import type { Backend } from '@zenfs/core'; -import * as emscripten from './emscripten.js'; +import { Cred } from '@zenfs/core/cred.js'; import { basename, dirname } from '@zenfs/core/emulation/path.js'; +import { Errno, ErrnoError, errorMessages } from '@zenfs/core/error.js'; +import { File } from '@zenfs/core/file.js'; +import { FileSystem, FileSystemMetadata, Sync } from '@zenfs/core/filesystem.js'; +import { FileType, Stats } from '@zenfs/core/stats.js'; +import { Buffer } from 'buffer'; +import './emscripten.js'; /** * @hidden @@ -30,7 +30,7 @@ export class EmscriptenFile extends File { protected _fs: EmscriptenFS, protected _FS: typeof FS, public readonly path: string, - protected _stream: emscripten.Stream + protected _stream: FS.FSStream ) { super(); } diff --git a/src/emscripten.ts b/src/emscripten.ts index e576f9e..937c770 100644 --- a/src/emscripten.ts +++ b/src/emscripten.ts @@ -12,52 +12,15 @@ import type { Errno } from '@zenfs/core'; import 'emscripten'; // Note: this is for types only. -export interface Stats { - dev: number; - ino: number; - mode: number; - nlink: number; - uid: number; - gid: number; - rdev: number; - size: number; - blksize: number; - blocks: number; - atime: Date; - mtime: Date; - ctime: Date; - timestamp?: number; -} - -export interface NodeOps { - getattr(node: FS.FSNode): Stats; - setattr(node: FS.FSNode, attr: Stats): void; - lookup(parent: FS.FSNode, name: string): FS.FSNode; - mknod(parent: FS.FSNode, name: string, mode: number, dev: unknown): FS.FSNode; - rename(oldNode: FS.FSNode, newDir: FS.FSNode, newName: string): void; - unlink(parent: FS.FSNode, name: string): void; - rmdir(parent: FS.FSNode, name: string): void; - readdir(node: FS.FSNode): string[]; - symlink(parent: FS.FSNode, newName: string, oldPath: string): void; - readlink(node: FS.FSNode): string; +export interface Mount extends FS.Mount { + opts: { root?: string }; } export declare class Node extends FS.FSNode { - node_ops?: NodeOps; - stream_ops?: StreamOps; -} - -export interface StreamOps { - open(stream: FS.FSStream): void; - close(stream: FS.FSStream): void; - read(stream: FS.FSStream, buffer: Uint8Array, offset: number, length: number, position: number): number; - write(stream: FS.FSStream, buffer: Uint8Array, offset: number, length: number, position: number): number; - llseek(stream: FS.FSStream, offset: number, whence: number): number; -} - -export declare class Stream extends FS.FSStream { - fd?: number; - nfd?: number; + node_ops?: FS.NodeOps; + stream_ops?: FS.StreamOps; + mount: Mount; + parent: Node; } export interface PATH { @@ -71,10 +34,10 @@ export interface Module { ERRNO_CODES: typeof Errno; } -export interface Plugin { - node_ops: NodeOps; - stream_ops: StreamOps; - mount(mount: { opts: { root: string } }): FS.FSNode; +export interface FS { + node_ops: FS.NodeOps; + stream_ops: FS.StreamOps; + mount(mount: FS.Mount & { opts: { root: string } }): FS.FSNode; createNode(parent: FS.FSNode, name: string, mode: number, dev?: unknown): FS.FSNode; getMode(path: string): number; realPath(node: FS.FSNode): string; diff --git a/src/plugin.ts b/src/plugin.ts index 7c119a3..984122a 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2,7 +2,7 @@ import fs, { parseFlag, type Errno, type Stats } from '@zenfs/core'; import * as emscripten from './emscripten.js'; import { assignWithDefaults, pick } from 'utilium'; -class StreamOps implements emscripten.StreamOps { +class StreamOps implements FS.StreamOps { get nodefs(): typeof fs { return this._fs.nodefs; } @@ -19,38 +19,36 @@ class StreamOps implements emscripten.StreamOps { return this._fs.ERRNO_CODES; } - constructor(protected _fs: ZenFSEmscriptenPlugin) {} + constructor(protected _fs: ZenFSEmscriptenNodeFS) {} - public open(stream: emscripten.Stream): void { + public open(stream: FS.FSStream): void { const path = this._fs.realPath(stream.object); - const FS = this.FS; try { - if (FS.isFile(stream.object.mode)) { + if (this.FS.isFile(stream.object.mode)) { stream.nfd = this.nodefs.openSync(path, parseFlag(stream.flags)); } } catch (e) { if (!e.code) { throw e; } - throw new FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]); + throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]); } } - public close(stream: emscripten.Stream): void { - const FS = this.FS; + public close(stream: FS.FSStream): void { try { - if (FS.isFile(stream.object.mode) && stream.nfd) { + if (this.FS.isFile(stream.object.mode) && stream.nfd) { this.nodefs.closeSync(stream.nfd); } } catch (e) { if (!e.code) { throw e; } - throw new FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]); + throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]); } } - public read(stream: emscripten.Stream, buffer: Uint8Array, offset: number, length: number, position: number): number { + public read(stream: FS.FSStream, buffer: Uint8Array, offset: number, length: number, position: number): number { // Avoid copying overhead by reading directly into buffer. try { return this.nodefs.readSync(stream.nfd, Buffer.from(buffer), offset, length, position); @@ -59,7 +57,7 @@ class StreamOps implements emscripten.StreamOps { } } - public write(stream: emscripten.Stream, buffer: Uint8Array, offset: number, length: number, position: number): number { + public write(stream: FS.FSStream, buffer: Uint8Array, offset: number, length: number, position: number): number { // Avoid copying overhead. try { return this.nodefs.writeSync(stream.nfd, buffer, offset, length, position); @@ -68,7 +66,7 @@ class StreamOps implements emscripten.StreamOps { } } - public llseek(stream: emscripten.Stream, offset: number, whence: number): number { + public llseek(stream: FS.FSStream, offset: number, whence: number): number { let position = offset; if (whence === 1) { // SEEK_CUR. @@ -94,7 +92,7 @@ class StreamOps implements emscripten.StreamOps { } } -class EntryOps implements emscripten.NodeOps { +class EntryOps implements FS.NodeOps { get nodefs(): typeof fs { return this._fs.nodefs; } @@ -111,7 +109,7 @@ class EntryOps implements emscripten.NodeOps { return this._fs.ERRNO_CODES; } - constructor(protected _fs: ZenFSEmscriptenPlugin) {} + constructor(protected _fs: ZenFSEmscriptenNodeFS) {} public getattr(node: FS.FSNode): Stats { const path = this._fs.realPath(node); @@ -127,7 +125,7 @@ class EntryOps implements emscripten.NodeOps { return stat; } - public setattr(node: FS.FSNode, attr: emscripten.Stats): void { + public setattr(node: FS.FSNode, attr: FS.Stats): void { const path = this._fs.realPath(node); try { if (attr.mode !== undefined) { @@ -268,9 +266,9 @@ class EntryOps implements emscripten.NodeOps { } } -export default class ZenFSEmscriptenPlugin implements emscripten.Plugin { - public node_ops: emscripten.NodeOps = new EntryOps(this); - public stream_ops: emscripten.StreamOps = new StreamOps(this); +export default class ZenFSEmscriptenNodeFS implements emscripten.FS { + public node_ops: FS.NodeOps = new EntryOps(this); + public stream_ops: FS.StreamOps = new StreamOps(this); public readonly FS: typeof FS; public readonly PATH: emscripten.PATH; @@ -283,11 +281,11 @@ export default class ZenFSEmscriptenPlugin implements emscripten.Plugin { assignWithDefaults(this, pick(emscripten, 'FS', 'PATH', 'ERRNO_CODES')); } - public mount(m: { opts: { root: string } }): FS.FSNode { - return this.createNode(null, '/', this.getMode(m.opts.root), 0); + public mount(mount: emscripten.Mount): emscripten.Node { + return this.createNode(null, '/', this.getMode(mount.opts.root), 0); } - public createNode(parent: FS.FSNode | null, name: string, mode: number, rdev?: number): FS.FSNode { + public createNode(parent: emscripten.Node | null, name: string, mode: number, rdev?: number): emscripten.Node { if (!this.FS.isDir(mode) && !this.FS.isFile(mode) && !this.FS.isLink(mode)) { throw new this.FS.ErrnoError(this.ERRNO_CODES.EINVAL); } @@ -310,7 +308,7 @@ export default class ZenFSEmscriptenPlugin implements emscripten.Plugin { return stat.mode; } - public realPath(node: FS.FSNode): string { + public realPath(node: emscripten.Node): string { const parts: string[] = []; while (node.parent !== node) { parts.push(node.name); @@ -318,6 +316,6 @@ export default class ZenFSEmscriptenPlugin implements emscripten.Plugin { } parts.push(node.mount.opts.root); parts.reverse(); - return this.PATH.join.apply(null, parts); + return this.PATH.join(...parts); } }