Skip to content

Commit

Permalink
Updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed May 14, 2024
1 parent 279d7db commit f6d57ab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 79 deletions.
16 changes: 8 additions & 8 deletions src/backend.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}
Expand Down
57 changes: 10 additions & 47 deletions src/emscripten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
46 changes: 22 additions & 24 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -310,14 +308,14 @@ 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);
node = node.parent;
}
parts.push(node.mount.opts.root);
parts.reverse();
return this.PATH.join.apply(null, parts);
return this.PATH.join(...parts);
}
}

0 comments on commit f6d57ab

Please sign in to comment.