Skip to content

Commit

Permalink
Updated some types
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed May 14, 2024
1 parent 0b19147 commit 279d7db
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
16 changes: 12 additions & 4 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
},
"peerDependencies": {
"@zenfs/core": "^0.10.0"
},
"dependencies": {
"utilium": "^0.3.4"
}
}
12 changes: 12 additions & 0 deletions src/emscripten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Adapted from Emscripten's NodeFS:
* https://raw.github.com/kripken/emscripten/master/src/library_nodefs.js
*/
import type { Errno } from '@zenfs/core';
import 'emscripten'; // Note: this is for types only.

export interface Stats {
Expand Down Expand Up @@ -59,6 +60,17 @@ export declare class Stream extends FS.FSStream {
nfd?: number;
}

export interface PATH {
join(...parts: string[]): string;
join2(a: string, b: string): string;
}

export interface Module {
FS: typeof FS;
PATH: PATH;
ERRNO_CODES: typeof Errno;
}

export interface Plugin {
node_ops: NodeOps;
stream_ops: StreamOps;
Expand Down
56 changes: 30 additions & 26 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs, { parseFlag, type Stats } from '@zenfs/core';
import type * as emscripten from './emscripten.js';
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 {
get nodefs(): typeof fs {
Expand Down Expand Up @@ -31,7 +32,7 @@ class StreamOps implements emscripten.StreamOps {
if (!e.code) {
throw e;
}
throw new FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -45,7 +46,7 @@ class StreamOps implements emscripten.StreamOps {
if (!e.code) {
throw e;
}
throw new FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -54,7 +55,7 @@ class StreamOps implements emscripten.StreamOps {
try {
return this.nodefs.readSync(stream.nfd, Buffer.from(buffer), offset, length, position);
} catch (e) {
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -63,7 +64,7 @@ class StreamOps implements emscripten.StreamOps {
try {
return this.nodefs.writeSync(stream.nfd, buffer, offset, length, position);
} catch (e) {
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -79,7 +80,7 @@ class StreamOps implements emscripten.StreamOps {
const stat = this.nodefs.fstatSync(stream.nfd);
position += stat.size;
} catch (e) {
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
return stat;
}
Expand All @@ -145,7 +146,7 @@ class EntryOps implements emscripten.NodeOps {
// Ignore not supported errors. Emscripten does utimesSync when it
// writes files, but never really requires the value to be set.
if (e.code !== 'ENOTSUP') {
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}
if (attr.size !== undefined) {
Expand All @@ -155,7 +156,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}
}
Expand All @@ -180,7 +181,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
return node;
}
Expand All @@ -198,7 +199,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -210,7 +211,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -222,7 +223,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -238,7 +239,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -250,7 +251,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}

Expand All @@ -262,7 +263,7 @@ class EntryOps implements emscripten.NodeOps {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
}
}
Expand All @@ -271,23 +272,26 @@ export default class ZenFSEmscriptenPlugin implements emscripten.Plugin {
public node_ops: emscripten.NodeOps = new EntryOps(this);
public stream_ops: emscripten.StreamOps = new StreamOps(this);

public readonly FS: typeof FS;
public readonly PATH: emscripten.PATH;
public readonly ERRNO_CODES: typeof Errno;

constructor(
public readonly FS = globalThis.FS,
public readonly PATH = globalThis.PATH,
public readonly ERRNO_CODES = globalThis.ERRNO_CODES,
emscripten: emscripten.Module,
public readonly nodefs: typeof fs = fs
) {}
) {
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 createNode(parent: FS.FSNode | null, name: string, mode: number, rdev?: number): FS.FSNode {
const FS = this.FS;
if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) {
throw new FS.ErrnoError(this.ERRNO_CODES.EINVAL);
if (!this.FS.isDir(mode) && !this.FS.isFile(mode) && !this.FS.isLink(mode)) {
throw new this.FS.ErrnoError(this.ERRNO_CODES.EINVAL);

Check failure on line 292 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest

No overload matches this call.

Check failure on line 292 in src/plugin.ts

View workflow job for this annotation

GitHub Actions / macos-latest

No overload matches this call.
}
const node: emscripten.Node = new FS.FSNode(parent, name, mode, rdev);
const node: emscripten.Node = new this.FS.FSNode(parent, name, mode, rdev);
node.node_ops = this.node_ops;
node.stream_ops = this.stream_ops;
return node;
Expand All @@ -301,7 +305,7 @@ export default class ZenFSEmscriptenPlugin implements emscripten.Plugin {
if (!e.code) {
throw e;
}
throw new this.FS.ErrnoError(this.ERRNO_CODES[e.code]);
throw new this.FS.ErrnoError(this.ERRNO_CODES[(e as FS.ErrnoError).code]);
}
return stat.mode;
}
Expand Down

0 comments on commit 279d7db

Please sign in to comment.