Giter VIP home page Giter VIP logo

mem-fs's Introduction

mem-fs

Simple in-memory vinyl file store.

Usage

Loading a file

You access a file using store#get() method. If the file is in memory, it will be used. Otherwise, we'll load the file from the file-system.

import { create } from 'mem-fs';

const store = create();
store.get('/test/file.txt');

When trying to load a file we cannot read from disk, an empty Vinyl file will be returned. The contents of this file will be set to null.

Trying to get a directory or any invalid files will also return an empty Vinyl file pointer.

Adding/updating a file

You update file references by using store#add() method. This method take a vinyl file object as parameter.

import File from 'vinyl';
import { create } from 'mem-fs';

const coffeeFile = new File({
  cwd: '/',
  base: '/test/',
  path: '/test/file.coffee',
  contents: new Buffer('test = 123'),
});

const store = create();
store.add(coffeeFile);

Iterating over the file system

Using store#each(cb(file, index)), you can iterate over every file stored in the file system.

Get all files

Using store#all(), you can get every file stored in the file system.

Check existence in the file system

Using store#existsInMemory(), you can check if the file already exists in the file system without loading it from disk.

Stream every file stored in the file system

Using store#stream(), you can create a stream with every file stored in the file system.

Pass stored files through a pipeline

store#pipeline() generates a new map with yielded files in transforms. If no transform is passed, files references are updated.

mem-fs's People

Contributors

sboudrias avatar mshima avatar greenkeeper[bot] avatar askvortsov1 avatar manuth avatar sindresorhus avatar snyk-bot avatar

Stargazers

Curie avatar Beacon Zhang avatar Keith avatar antx avatar lenq avatar Alex Yang avatar Joscha Götzer avatar  avatar  avatar Ashley Engelund avatar Andrei Surugiu avatar Anne Thorpe avatar  avatar Vassiliy Kuzenkov avatar Eleven avatar  avatar Kumarajiva avatar yemu avatar  avatar Seva D. avatar  avatar Fy avatar Christian Tapia avatar Bonnie Dipasquale avatar oudafei avatar LGD.HuaFEEng avatar  avatar Roman Kovalchuk avatar Liutsing Robert avatar Heartlander avatar  avatar  avatar chlorine avatar  avatar xQuotes avatar fishcui avatar Brian Faust avatar Caio Gondim avatar Brandon Bayer avatar Kevin avatar Pearce Liang avatar 宝宝大人 avatar  avatar fengxs avatar bethon avatar waskito shidiq avatar Gabriel Valencia avatar Daniel Gorbatov avatar  avatar  avatar Rom's avatar Vania Ye avatar  avatar JerryC avatar Jun Zhang avatar Ye Miancheng avatar Haruka Asakura avatar Leo Young avatar Tomi li avatar Muhammad Shahabipour avatar Ajay Poshak avatar franklife avatar 阿平 avatar Hsun avatar HISAME SHIZUMARU avatar Adrian Perez avatar  avatar 螃蟹 avatar Seiya IZUMI avatar  avatar Nothing avatar ADoyle avatar Peter Šándor avatar Danya avatar Ruben Paz avatar Nelson Pecora avatar Alexey Lizurchik avatar Juan Picado avatar Stephan Schneider avatar Ernst Salzmann avatar John O'Connor avatar Jerzerak avatar Nikita Khomyakov avatar Wilson Young avatar Siddhartha Lahiri avatar daodao avatar Tsotne Nazarashvili avatar Erik Jung avatar  avatar noyobo avatar Franco Correa avatar Vaughan Rouesnel avatar  avatar  avatar xiaoming avatar  avatar Ilya Radchenko avatar  avatar Simon Fan avatar Gabo Esquivel avatar

Watchers

George avatar James Cloos avatar  avatar  avatar Michael Anthony avatar Whoami avatar Arthur Verschaeve avatar Robert Johnson avatar thelastrider avatar  avatar

mem-fs's Issues

intent of 'change' event?

In lines 39-40 on index.js

  Store.prototype.add = function (file) {
    store[file.path] = file;
    this.emit('change');
    return this;
  };

I'm assuming this.emit('change') is meant to indicate the internal store object has been modified.

If this is true, then Store.prototype.get should also emit a change event due to the load function modifying the internal store.

I created a few branches:

If it's not a bug, please explain the intent so I understand the code better.

Thanks much

TypeScript Declaration is broken

The current type-declaration require esModuleInterop top be enabled (which is disabled by default - the decision whether or not to use esmoduleInterop should be left to the user consuming this module).

Incorrect use of Vinyl#base

I believe that file.base is being set incorrectly. In the source:

file = new File({
    cwd: process.cwd(),
    base: path.basename(filepath),
    ...

According to the documentation, file.base is intended to be a directory and file.basename is the file's basename. Setting file.base to the basename of the file results in an incorrect value being returned from the file.relative getter.

I ran into this issue while attempting to create a utility that would override mem-fs-editor's .commit() method to generate a zip file using gulp-zip.

Consider adding typings

I've added TypeScript typings for mem-fs in DefinitelyTyped/DefinitelyTyped#24434

Would you consider providing typings for this module? Alternatively if you don't want to do that, could the information about the typings be provided in the project README.

I'm happy to contribute PRs with these changes, but didn't want to make a PR for something of no interest to the project.

Alternative solution

I find vinyl files to be abnormal files. it's not so web-ish.
NodeJS now also ships with Blob built in (and Files too, ...in a roundabout way)
this blobs have .stream(), .arrayBuffer(), .text(), .slice(), name & type something that can easily be shared with consistently with deno, browsers and Deno.land without needing any custom file wrappers.

if you are using fetch-blob then you can get files/blob's that are just references point to some location on the disc (just like vinyl files) You can also construct own in memory Files.
this blobs are immutable, re-readable and also slice:able (and when you slice a blob then you are just creating a new ref point with a new start/end offset

And for the storage you could just use Map, WeakMap, Set, or WeakSet. and maybe just use the path as the key.

so all in all:

import { File, fileFromSync, Blob } from 'fetch-blob/from.js'

var store = new Map();

var coffeeFile = new File(['test = 123'], 'test/file.coffee', { type, lastModified });
var pkg = fileFromSync('./package.json')

// will not allocate much memory, will just be 2 references point to other  files to read from
var both = new File([coffeeFile, pkg], '') 

store.set(coffeeFile.name, coffeeFile)
store.set(pkg.name, pkg)

// Map also has a set/delete/has/forEach method...

// to stream every file in the store: use something like:
new Blob(store.values()).stream()

You could also extend Map to override the get method and fallback to using the fs if the file isn't stored

Add a Method to Check If a File Is Loaded

Currently there is no way to check if a file is loaded into the memory store. The get method only works to check for files that don't exists on disk or in mem-fs.

I propose adding a method called exists or loaded that returns a boolean indicating if the file is currently present in mem-fs.

Document states

Need to consider documenting state of a file and making it more obvious.

I probably want to namespace state: file['mem-fs:state']. State should tell if the file is an empty pointer (no file existed) or if it is a file pointer (file was found on disk).

Currently there's this vague "state" property that is assigned. This need to be normalized.

Stricter #get() operation

Right now #get() will return file pointer if we try to get a directory. We should be stricter about that and throw if what we try to get is not a file.

Same thing about permissions issue. We shouldn't have a catch all clause - ideally we'd only catch read errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.