CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mz

Promise-based wrappers for Node.js core APIs that modernize callback-based methods to work with async/await patterns

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

fs.mddocs/

File System Operations

Promise-based file system operations that provide modern async/await compatibility for Node.js fs operations. Includes graceful-fs integration when available and a custom exists() implementation.

Capabilities

File Reading Operations

Read file contents and metadata with promise support.

/**
 * Read the entire contents of a file
 * @param path - File path to read
 * @param options - Encoding or options object
 * @returns Promise resolving to file contents
 */
function readFile(path, options): Promise<Buffer | string>;

/**
 * Read a directory to get list of files and subdirectories
 * @param path - Directory path to read
 * @param options - Options object or encoding
 * @returns Promise resolving to array of filenames or Dirent objects
 */
function readdir(path, options): Promise<string[] | Dirent[]>;

/**
 * Read the value of a symbolic link
 * @param path - Path to symbolic link
 * @param options - Options object or encoding
 * @returns Promise resolving to link target
 */
function readlink(path, options): Promise<string>;

File Writing Operations

Write and modify file contents with promise support.

/**
 * Write data to a file, replacing the file if it already exists
 * @param file - File path or file descriptor
 * @param data - Data to write
 * @param options - Options object or encoding
 * @returns Promise resolving when write completes
 */
function writeFile(file, data, options): Promise<void>;

/**
 * Append data to a file, creating the file if it doesn't exist
 * @param file - File path or file descriptor
 * @param data - Data to append
 * @param options - Options object or encoding
 * @returns Promise resolving when append completes
 */
function appendFile(file, data, options): Promise<void>;

/**
 * Write buffer to file at specified position
 * @param fd - File descriptor
 * @param buffer - Buffer containing data to write
 * @param offset - Offset in buffer to start reading from
 * @param length - Number of bytes to write
 * @param position - Position in file to write at
 * @returns Promise resolving to {bytesWritten, buffer}
 */
function write(fd, buffer, offset, length, position): Promise<{bytesWritten: number, buffer: Buffer}>;

File System Metadata Operations

Get and modify file metadata, permissions, and timestamps.

/**
 * Get file statistics
 * @param path - File path
 * @param options - Options object
 * @returns Promise resolving to Stats object
 */
function stat(path, options): Promise<Stats>;

/**
 * Get file statistics, following symbolic links
 * @param path - File path
 * @param options - Options object  
 * @returns Promise resolving to Stats object
 */
function lstat(path, options): Promise<Stats>;

/**
 * Get file statistics by file descriptor
 * @param fd - File descriptor
 * @param options - Options object
 * @returns Promise resolving to Stats object
 */
function fstat(fd, options): Promise<Stats>;

/**
 * Check if file exists (custom implementation)
 * @param path - File path to check
 * @returns Promise resolving to boolean indicating existence
 */
function exists(path): Promise<boolean>;

File System Structure Operations

Create, remove, and modify file system structure.

/**
 * Create a directory
 * @param path - Directory path to create
 * @param options - Options object or mode
 * @returns Promise resolving when directory is created
 */
function mkdir(path, options): Promise<void>;

/**
 * Remove a directory
 * @param path - Directory path to remove
 * @param options - Options object
 * @returns Promise resolving when directory is removed
 */
function rmdir(path, options): Promise<void>;

/**
 * Rename a file or directory
 * @param oldPath - Current path
 * @param newPath - New path
 * @returns Promise resolving when rename completes
 */
function rename(oldPath, newPath): Promise<void>;

/**
 * Delete a file
 * @param path - File path to delete
 * @returns Promise resolving when file is deleted
 */
function unlink(path): Promise<void>;

/**
 * Create a hard link
 * @param existingPath - Path to existing file
 * @param newPath - Path for new link
 * @returns Promise resolving when link is created
 */
function link(existingPath, newPath): Promise<void>;

/**
 * Create a symbolic link
 * @param target - Target path
 * @param path - Link path
 * @param type - Link type (file, dir, junction)
 * @returns Promise resolving when symlink is created
 */
function symlink(target, path, type): Promise<void>;

File Permissions and Ownership

Modify file permissions and ownership.

/**
 * Change file permissions
 * @param path - File path
 * @param mode - Permission mode
 * @returns Promise resolving when permissions are changed
 */
function chmod(path, mode): Promise<void>;

/**
 * Change file permissions by file descriptor
 * @param fd - File descriptor
 * @param mode - Permission mode
 * @returns Promise resolving when permissions are changed
 */
function fchmod(fd, mode): Promise<void>;

/**
 * Change file ownership
 * @param path - File path
 * @param uid - User ID
 * @param gid - Group ID
 * @returns Promise resolving when ownership is changed
 */
function chown(path, uid, gid): Promise<void>;

/**
 * Change file ownership by file descriptor
 * @param fd - File descriptor
 * @param uid - User ID
 * @param gid - Group ID
 * @returns Promise resolving when ownership is changed
 */
function fchown(fd, uid, gid): Promise<void>;

/**
 * Change symbolic link ownership
 * @param path - Link path
 * @param uid - User ID
 * @param gid - Group ID
 * @returns Promise resolving when ownership is changed
 */
function lchown(path, uid, gid): Promise<void>;

File Access and Timestamps

Test file access and modify timestamps.

/**
 * Test file access permissions (if available in Node.js version)
 * @param path - File path
 * @param mode - Access mode constants
 * @returns Promise resolving when access test completes
 */
function access(path, mode): Promise<void>;

/**
 * Change file timestamps
 * @param path - File path
 * @param atime - Access time
 * @param mtime - Modification time
 * @returns Promise resolving when timestamps are changed
 */
function utimes(path, atime, mtime): Promise<void>;

/**
 * Change file timestamps by file descriptor
 * @param fd - File descriptor
 * @param atime - Access time
 * @param mtime - Modification time
 * @returns Promise resolving when timestamps are changed
 */
function futimes(fd, atime, mtime): Promise<void>;

Low-Level File Operations

Direct file descriptor operations and synchronization.

/**
 * Open a file
 * @param path - File path
 * @param flags - File access flags
 * @param mode - File mode (permissions)
 * @returns Promise resolving to file descriptor
 */
function open(path, flags, mode): Promise<number>;

/**
 * Close a file descriptor
 * @param fd - File descriptor to close
 * @returns Promise resolving when file is closed
 */
function close(fd): Promise<void>;

/**
 * Read data from file descriptor
 * @param fd - File descriptor
 * @param buffer - Buffer to read into
 * @param offset - Offset in buffer to start writing
 * @param length - Number of bytes to read
 * @param position - Position in file to read from
 * @returns Promise resolving to {bytesRead, buffer}
 */
function read(fd, buffer, offset, length, position): Promise<{bytesRead: number, buffer: Buffer}>;

/**
 * Synchronize file data to storage
 * @param fd - File descriptor
 * @returns Promise resolving when sync completes
 */
function fsync(fd): Promise<void>;

/**
 * Synchronize file data (not metadata) to storage
 * @param fd - File descriptor
 * @returns Promise resolving when sync completes
 */
function fdatasync(fd): Promise<void>;

/**
 * Truncate file to specified length
 * @param path - File path
 * @param len - Length to truncate to
 * @returns Promise resolving when truncate completes
 */
function truncate(path, len): Promise<void>;

/**
 * Truncate file by file descriptor
 * @param fd - File descriptor
 * @param len - Length to truncate to
 * @returns Promise resolving when truncate completes
 */
function ftruncate(fd, len): Promise<void>;

/**
 * Resolve real path of file
 * @param path - Path to resolve
 * @param options - Options object or encoding
 * @returns Promise resolving to resolved path
 */
function realpath(path, options): Promise<string>;

Node.js Version-Dependent Operations

Operations available in newer Node.js versions (conditionally exported).

/**
 * Copy a file (if available in Node.js version)
 * @param src - Source file path
 * @param dest - Destination file path
 * @param flags - Copy operation flags
 * @returns Promise resolving when copy completes
 */
function copyFile(src, dest, flags): Promise<void>;

/**
 * Create a temporary directory (if available in Node.js version)
 * @param prefix - Prefix for directory name
 * @param options - Options object or encoding
 * @returns Promise resolving to created directory path
 */
function mkdtemp(prefix, options): Promise<string>;

Usage Examples:

const fs = require('mz/fs');

// Read and write files
async function processFile() {
  try {
    // Check if file exists
    if (await fs.exists('input.txt')) {
      // Read file content
      const content = await fs.readFile('input.txt', 'utf8');
      
      // Process content
      const processed = content.toUpperCase();
      
      // Write processed content
      await fs.writeFile('output.txt', processed);
      
      // Get file stats
      const stats = await fs.stat('output.txt');
      console.log('File size:', stats.size);
    }
  } catch (error) {
    console.error('File operation failed:', error);
  }
}

// Directory operations
async function listFiles() {
  try {
    const files = await fs.readdir('./');
    console.log('Files:', files);
  } catch (error) {
    console.error('Failed to read directory:', error);
  }
}

// Callback support is still available
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error:', err);
  } else {
    console.log('File content:', data);
  }
});

Implementation Notes

  • Uses graceful-fs when available, falls back to native fs module
  • All functions support both promise and callback interfaces
  • Custom exists() implementation that always resolves (never rejects)
  • Conditionally exports newer Node.js methods when available
  • Maintains complete compatibility with native fs module behavior

docs

child-process.md

crypto.md

dns.md

fs.md

index.md

readline.md

zlib.md

tile.json