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

mem-blob-fs

v1.0.2

Published

Simple in-memory blob file store

Downloads

5

Readme

mem-blob-fs

Simple in-memory blob file store (similar to mem-fs but uses standard web File's instead of vinyl)

Usage

mem-blob-fs depends on

  • 1 thing: the File constructor.
  • 2 things if you want to be able to fallback to filesystem.

It generally depends on fetch-blob/from.js on the NodeJS side but it's an optional dependency and also works without it in the browser

The store extends both Map and EventTarget and has the same functionality. Therefore it's perfectly fine to just construct the store with a 2d array of [[key, value]] pair.

import Store from 'mem-blob-fs'
import * as fs from 'fetch-blob/from.js'

Store.fs = fs

const store = new Store([
  ['pkg.json', new File(['things to store'])],
  ['readme.md', new File(['things to store2'])],
])

Loading a file

You access a file using store#get() method. If the file is in memory, it will be used. Otherwise, the overwritten get fn will load the file from the node:fs

import Store from 'mem-blob-fs'
import * as fs from 'fetch-blob/from.js'

Store.fs = fs // required to fallback to files on the disc.

const store = new Store()
const file = store.get('test/file.txt')
await file.text()

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

Trying to get a directory or any invalid files will also return an empty File.

Adding/updating a file

You update file references by using store#set(path, file) method. This method take a regular file or blob object as 2nd parameter.

import Store from 'mem-blob-fs'

const store = new Store()
const file = new File(['test = 123'], 'file.coffee')
store.set('test/file.coffee', coffeeFile)

Using store#set will trigger a change event every time

// evt is a ChangeEvent class that extends normal Event \w two new props
function fn (evt) {
  evt.path
  evt.file
}

store.addEventListener('change', fn, { signal, once })`

Iterating over the file system

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

Map also has Symbol.iterator, .values(), and .keys() that returns an iterator so you can use for..of loops

Get all files

Using store#values(), (provided provided by Map) will give you can an iterator of all files stored in the memory.

Check existence in the file system

Using store#has(path), 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#values(), you can create a iterator that will all yield blobs use it to construct a new file to concatenate it into one single large file

// one large concatenated file (nothing is read until you actually start reading)
const file = new File(store#values(), '')

await file.text()
await file.arrayBuffer()
file.stream() // whatwg ReadableStream