fuse3.one
Version:
FUSE3 N-API bindings for Node.js - Linux native FUSE3 implementation
143 lines (125 loc) • 3.72 kB
text/typescript
/**
* FUSE3.ONE - Native FUSE3 bindings for Linux
*
* This package provides Node.js bindings to the native FUSE3 library
* for creating virtual filesystems on Linux systems.
*
* @author REFINIO GmbH
* @license MIT
*/
import { EventEmitter } from 'events';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// FUSE3 operation interfaces
export interface FuseStats {
mtime: Date;
atime: Date;
ctime: Date;
size: number;
mode: number;
uid: number;
gid: number;
}
export interface FuseOperations {
init?: () => void;
getattr?: (path: string) => FuseStats | null;
readdir?: (path: string) => string[];
read?: (path: string, size: number, offset: number) => Buffer | null;
write?: (path: string, buffer: Buffer, offset: number) => number;
create?: (path: string, mode: number) => void;
unlink?: (path: string) => void;
mkdir?: (path: string, mode: number) => void;
rmdir?: (path: string) => void;
rename?: (oldPath: string, newPath: string) => void;
truncate?: (path: string, size: number) => void;
open?: (path: string, flags: number) => number;
release?: (path: string, fd: number) => void;
statfs?: (path: string) => any;
}
// Load native FUSE3 addon
let nativeFuse: any;
try {
nativeFuse = require('bindings')('fuse3_native');
} catch (error) {
throw new Error(`Failed to load native FUSE3 addon: ${error.message}`);
}
/**
* FUSE3 filesystem class
*
* Provides a Node.js interface to the native FUSE3 library
*/
export class Fuse3 extends EventEmitter {
private mounted = false;
private mountPath: string;
private operations: FuseOperations;
private nativeInstance: any;
constructor(mountPath: string, operations: FuseOperations, options: any = {}) {
super();
if (process.platform !== 'linux') {
throw new Error('FUSE3.ONE only works on Linux. For Windows, use projfs-fuse.one');
}
this.mountPath = mountPath;
this.operations = operations;
// Create native FUSE instance
this.nativeInstance = new nativeFuse.Fuse3Mount(mountPath, options);
}
/**
* Mount the filesystem
*/
async mount(): Promise<void> {
if (this.mounted) {
throw new Error('Filesystem is already mounted');
}
try {
await this.nativeInstance.mount(this.operations);
this.mounted = true;
this.emit('mount');
} catch (error) {
throw new Error(`Failed to mount FUSE3 filesystem: ${error.message}`);
}
}
/**
* Unmount the filesystem
*/
async unmount(): Promise<void> {
if (!this.mounted) {
return;
}
try {
await this.nativeInstance.unmount();
this.mounted = false;
this.emit('unmount');
} catch (error) {
throw new Error(`Failed to unmount FUSE3 filesystem: ${error.message}`);
}
}
/**
* Check if filesystem is mounted
*/
isMounted(): boolean {
return this.mounted;
}
/**
* Get mount path
*/
getMountPath(): string {
return this.mountPath;
}
}
// Export default class
export default Fuse3;
// Export error codes for convenience
export const FUSE_ERRORS = {
EPERM: 1,
ENOENT: 2,
EIO: 5,
EACCES: 13,
EEXIST: 17,
ENOTDIR: 20,
EISDIR: 21,
EINVAL: 22,
ENOSPC: 28,
EROFS: 30,
EBUSY: 16,
ENOTEMPTY: 39
};