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

@mojule/memory-fs

v0.2.6

Published

A simple in-memory filesystem. Holds data in a javascript object.

Downloads

3

Readme

memory-fs

A simple in-memory filesystem. Holds data in a javascript object.

Forked from memory-fs in order to allow promisifying the functions, see Changes below

const MemoryFS = require( '@mojule/memory-fs' )
const fs = MemoryFS()

fs.mkdirpSync( '/a/test/dir' )
fs.writeFileSync( '/a/test/dir/file.txt', 'Hello World' )
fs.readFileSync( '/a/test/dir/file.txt' ) // returns Buffer( "Hello World" )

// Async variants too
fs.unlink( '/a/test/dir/file.txt', err => {
  // ...
})

fs.readdirSync( '/a/test' ) // returns [ 'dir' ]
fs.statSync( '/a/test/dir' ).isDirectory() // returns true
fs.rmdirSync( '/a/test/dir' )

fs.mkdirpSync( 'C:\\use\\windows\\style\\paths' )

Options

You can patch the API by passing options - MemoryFS is missing many functions from fs, you can add them as needed, or you can add custom functions

Patch functions have the same signature as the original function except for taking an additional first argument, fs - this object contains all of the existing functions and the data property that holds the backing store

const MemoryFS = require( '@mojule/memory-fs' )

// when called on `fs` instance the signature will be isEmptyFileSync( path )
const isEmptyFileSync = ( fs, path ) => {
  const file = fs.readFileSync( path )

  return file.length === 0
}

const api = { isEmptyFileSync }
const asyncNames = 'isEmptyFile'

const fs = MemoryFS( {}, { api, asyncNames } )

fs.writeFileSync( 'test.txt', 'abc' )
fs.writeFileSync( 'empty.txt', '' )

fs.isEmptyFileSync( 'test.txt' ) // false
fs.isEmptyFileSync( 'empty.txt' ) // true

fs.isEmptyFile( 'empty.txt', ( err, result ) => {
  if( err ) return console.error( err )

  console.log( result )
})

If you want to patch existing methods, require the existing api.js from the src folder, get the method(s) you want, and write a wrapper method that can call the original:

const MemoryFS = require( '@mojule/memory-fs' )
const existing = require( '@mojule/memory-fs/src/api' )

const log = [ 'writeFile', 'mkdir' ]

const api = {}

log.forEach( name => {
  api[ name ] = ( ...args ) => {
    console.log( ...args )

    return existing[ name ]( ...args )
  }
})

// writeFile and mkdir should now log their args out when called
const fs = MemoryFS( {}, { api } )

Changes from webpack/memory-fs

  • MemoryFS is a factory function rather than a class constructor, so that the resultant fs methods can be promisified without having to call .bind. It should be completely backwards compatible aside from you have to drop the new when instantiating it, and some utility functions being moved (below)
  • Cleaned up conversion of sync -> async functions
  • Extracted repeated code throughout wherever it was traversing the data with a path out to a single function to make it easier to maintain and allow a possible refactor in future to use different backing stores
  • Moved normalize, join and pathToArray utility functions from fs instance to MemoryFS - eg MemoryFS.normalize( ... ) etc.
  • Made the API patchable via options

License

MIT

This is a fork of webpack/memory-fs