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

block-cache

v2.0.0

Published

fs read (incl. stream) operations cached in an lru-cache for consistent memory usage with custom fs support (hyperdrive).

Downloads

5

Readme

block-cache

Build Status JavaScript Style Guide Maintainability Test Coverage

block-cache is a transparent(ish) cache that keeps data split in blocks in an in-memory lru-cache. This is useful if you want to process a file, reusing previously downloaded parts and improving the general performance without caching more than your given memory limit.

The cache does not expose the passed-in API at any point which makes it suitable as a Sandbox.

npm i block-cache --save

Usage

The API of block-cache is comparable to the fs API but all callbacks are optional and if omitted will result in a Promise returned.

Here is a simple example of reading a file into the local cache.

const fs = require('fs')
const {Cache, CachedFile} = require('block-cache')

const cache = new Cache(fs, {
  blkSize: 1024,
  cacheSize: 2 * 1024 * 1024 // 2 MB
})
const fp = await cache.open('./Readme.md')
const data = await cache.read(fp)

console.log(data)

await cache.close(fp)

This example reads the entirety of the ./Readme.md file into a 2 mega-byte cache in 1 kilo-byte sized blocks and then closes the data. Even if the fp is closed: the block stay in the cache!

Use-case: file parsing

This library usually comes in play when you have to parse parts of a file depending on the header. Take the beginning of this GIF parser for example:

const fs = require('fs')
const {Cache, CachedFile} = require('block-cache')

const cache = new Cache(fs, {
  blkSize: 1024,
  cacheSize: 2 * 1024 * 1024 // 2 MB
})
const fp = await cache.open('./Readme.md')
const signature = (await fp.read(null, 0, 6)).toString()
if (signature === 'GIF87a' || signature === 'GIF89a') {
  const packed = await fp.read(null, 0, 10)
  // etc.
}

await cache.close(fp)

As you can see in this example code, it is necessary to read only parts of a file at a time. Very small parts. But most of those bytes are already present in the cache. So, while the first operation needed to read 1Kb of the file, the second operation can already use it from the cached data.

API


new Cache(fs[, opts])
  • fs is a FileSystem (require('fs'))) or Hyperdrive archive (object).
  • opts.cache is a lru-cache instance (object, optional).
  • opts.cacheSize is the size of the lru-cache to be created in case a opts.cache is missing. Defaults to Cache.DEFAULT_CACHE_SIZE (integer).
  • opts.blkSize is the default size in bytes of a cache-block. Defaults to CachedFile.DEFAULT_BLK_SIZE. (integer).
  • opts.prefix is an optional prefix that can be added to the cached data, useful if you want to reuse the same opts.cache for multiple Cache instances. Defaults to ''. (string)

cache.open(path[, opts, cb])

Creates a cached file pointer reference for a given path. Note: It will open the file reference in r mode.

  • path path to read the file from (string).
  • opts.blkSize is the size in bytes of a cache-block. Defaults to the opts.blkSize defined in the Cache.
  • cb(Error, CachedFile) is an optional async callback handler method. The method will return a Promise if the callback is not defined.

cache.close(fp[, cb])

Closes a created file pointer reference. After closing, future requests on the CachedFile will result in an err.code === 'ERR_CLOSED error.

  • fp is a CachedFile instance, created with .open or .openSync
  • cb(Error) is an optional async callback handler method. The method will return a Promise if the callback is not defined.

cache.disconnect()

Disconnects the cache from the file system instance. Any future operations on the Cache or CachedFile instances create with the Cache will result in an err.code === 'ERR_DISCONNECTED' error. Disconnect also closes all open file pointer references on the underlying file system.


cache.openSync(path[, opts])

like cache.open but synchronous.


cache.read(fd[, buffer, offset, length, position, cb])

Reads the content of an opened file into a given buffer.

  • fd is a CachedFile instance, created with .open or .openSync
  • buffer is a Buffer instance to write into. Unlike the Node API, this is optional which means that the reader will create a buffer instance if null or undefined is passed-in.
  • offset is the offset in the buffer to start writing at.
  • length is an integer specifying the number of bytes to read into buffer, defaults to length of the file (integer).
  • position is an argument specifying where to begin reading from in the file. The file descriptor will remember the end of the last read in the fd.position property. It will default to 0.
  • cb(Error, Buffer) is an optional async callback handler method. The method will return a Promise if the callback is not defined.

cache.createReadStream(path[, opts, cb])

Creates a cached file pointer reference for a given path and then reads it through a stream.

  • path is the path to read the file from (string).
  • opts.blkSize is the block size for each block to be cached. Defaults to cache.opts.blkSize. (integer).
  • opts.start is the start from while to read the file. Defaults to 0. (integer)
  • opts.end is the end until which to read the file. Defaults to the end of the file. (integer)

Cache.DEFAULT_CACHE_SIZE

The default size of a cache created if opts.cache is not passed in: 10485760 (integer, equals 10 MegaByte)


new CachedFile(cache, path[, opts])

Creates a new instance for reading one file. The blocks will still be stored in the passed-in cache object. While it is possible to instantiate a new CachedFile, you can not pass-in a cache directly, use the .open, .openSync or .createReadStream to interact with the cache

  • cacheInternal a subset of the Cache API that is not accessible from outside.
  • cacheInternal.open(path, opts, cb) opens a file pointer to a given path on the underlying fs.
  • cacheInternal.stat(path, cb) receives the stat file from the underlying fs
  • cacheInternal.close(fp, cb) closes a file pointer on the underlying fs.
  • cacheInternal.read(fp, prefix, start, end, cb) reads bytes from the underlying fs into a buffer.
  • opts.blkSize specifies the block size for this file pointer (integer). Defaults to CachedFile.DEFAULT_BLK_SIZE.

cachedFile.close([cb])

Closes the instance. After closing, future requests on the CachedFile will result in an err.code === 'ERR_CLOSED error.

  • cb(Error) is an optional async callback handler method. The method will return a Promise if the callback is not defined.

cachedFile.read([buffer, offset, length, position, cb])

Like cache.read but without the need to pass a descriptor.


cachedFile.createReadStream([opts, cb])

Like cache.createReadStream but without the need to pass a descriptor.


cachedFile.size([cb])

The size of the file as noted in the file descriptor.


cachedFile.stat([cb])

Retreives the actual Stats of the file through fs.stat.


CachedFile.DEFAULT_BLK_SIZE

The default opts.blkSize used for caching: 512 (integer, equals 512 Byte).

Acknowledgement

This project was made for and supported by dotloom.

License

MIT