npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

micro-memfs

v1.2.0

Published

A simple, tiny, in-memory mock file system for all environments.

Downloads

2

Readme

micro-memfs

micro-memfs is a tiny in-memory "file system" for use in a simple applications that need a mocked file system i.e. unit testing, web apps or games.

  • Small 860 bytes (minified + gzipped in empty esbuild project). Size Limit controls the size.
  • Self-contained No dependencies & all utilities are managed internally (i.e. no reliance on process.cwd())
  • Simple For quick, small fake file systems there's no need for a fully mocked test-focused library weighing multiple kilobytes.
  • Flexible Supports "executables" to store/run JS and standard path-like path resolution to support absolute and relative paths.

API

The mock file system is made up of directories, files and "executables".

Files map a path to some text. This text can encode whatever you'd like. A file must have an extension - e.g. directory/file.txt.

Executables are sync/async functions that will return some output. These are analogous to regular old binaries and thus their source code can be read with readFile() too.

type CommandFunc = (args: string[]) => string[] | Promise<string[]>;

interface MicroFs {
  /** Check if a path exists in the file system. */
  exists(path: string): boolean;
  /** Check if a given path is a directory or a file/doesn't exist. This method also ensures the path exists. */
  isDirectory(path: string): boolean;
  /** Read the text content of a file in the file system.  */
  readFile(pth: string): string | undefined;
  /** Read the contents of a directory in the file system. */
  readDir(pth: string): string[];
  /** Get or set the cwd (current working directory of this file system) */
  cwd(pth?: string): string;
  /** 
   * Find an "executable" in the file system returning a callable function if found. 
   * Use this to prevent your linter going crazy if you try to `eval()` the output from `readFile() `
   * */
  findExecutable(prog: string): CommandFunc | null;
}

Example

import microfs from 'micro-memfs';

const fs = microfs({
  "usr/file.txt": "Some text content",
},
{
  "usr/emphasize": (args) => args.concat(["!"]),
});

fs.cwd() // '/'
fs.exists('usr') // true
fs.isDirectory('usr') // true
fs.cwd('usr') // '/usr'
fs.readFile('file.txt') // 'Some text content'
fs.readFile('emphasize') // '(args) => args.concat(["!"])'
const emphasize = fs.findExecutable("emphasize");
emphasize(['hello', 'world']) // -> ['hello', 'world']
fs.cwd('another-dir') // will throw an `Error` as /usr/another-dir doesn't exist

TODO

  • Support for empty directories
  • File/directory addition/deletion after instantiation of the file system
  • Extend tests to cover all methods

Licenses

Contributions

Contributions are always welcome! Please submit them on GitHub :)