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

@bcgov/file-cache

v1.0.6

Published

Simple file storage that generates hashes for stored files

Downloads

27

Readme

[Deprecated] file-cache

Note: This package has been deprecated and will no longer be receiving support updates.

npm downloads License img

Library to store files locally identified by a hash of the file contents. A sub-directory is created and identified by a hash of the file, the original file is then stored under the hash sub-directory.

The hash is created when writing the binary contents to disk. Each file will generate a unique hash.

/root-dir
.. /hash
.. / ../original-file.ext

Installation

npm i @bcgov/file-cache

Configuration

The root directory for the file cache can be specified by an option (fileCachePath), or an environment variable (FILE_CACHE_PATH), or will just use the temp directory as specified by the Operating System.

Options

The default FILE_CACHE_PATH will use the Operating System's default temporary directory. However, you may override this behavior with the following:

Direct

const FileCache = require('@bcgov/file-cache');
const fileCache = new FileCache({fileCachePath: '/var/usr/file-cache'});

Environment Variables

export FILE_CACHE_PATH = '/var/usr/file-cache';
const FileCache = require('@bcgov/file-cache');
const fileCache = new FileCache();

Usage

create/initialize

Create a new fileCache object, the configured directory will be verified and created if required. Will throw an Error if the directory does not exist and cannot be created.

const FileCache = require('@bcgov/file-cache');
const fileCache = new FileCache({fileCachePath: '/var/usr/file-cache'});

async write

Write contents of a buffer to the file cache.

const writeFileResult = await fileCache.write(input.content, input.fileName, 'binary', {overwrite: true});

| Parameters | Description | | --- | --- | | content | string or buffer of data | | name | Filename or extension or content. If extension, then UUID will be used for the name: UUID.ext | | contentEncodingType | Encoding type of content. ex. base64, bin, hex. Default is base64 | | options | Object for optional work. default is {overwrite = false}. | | options.overwrite | If true, then allow the destination file to be overwritten if it exists, otherwise will return an error. |

Returns

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "hash": null
}

| Field | Description | | --- | --- | | success | boolean, true indicates data written to cache, false otherwise | | errorType | number - error number if not successful. | | errorMsg | string - error message if not successful. | | hash | string - hash/key identifying the file |

async move

Move existing file to file cache.

const moveFileResult = await fileCache.move(req.file.path, req.file.originalname);

| Parameters | Description | | --- | --- | | source | existing file | | name | name of file with extension ex. my-word-file.docx | | options | Object for optional work. default is {overwrite = false}. | | options.overwrite | If true, then allow the destination file to be overwritten if it exists, otherwise will return an error. |

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "hash": null
}

| Field | Description | | --- | --- | | success | boolean, true indicates data written to cache, false otherwise | | errorType | number - error number if not successful. | | errorMsg | string - error message if not successful. | | hash | string - hash/key identifying the file |

async read

Read file from cache. Will throw an Error if file not found or error reading from file system.

const cachedFile = await fileCache.read(hash);

| Parameters | Description | | --- | --- | | hash | string - hash/key for file in cache |

Returns file

See fs.readFile

find

Find a file in the cache.

const findFileResult = fileCache.find(hash);

| Parameters | Description | | --- | --- | | hash | string - hash/key for file in cache |

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null,
    "hash": null,
    "name": null,
    "ext": null,
    "dir": null,
    "path": null
}

| Field | Description | | --- | --- | | success | boolean, true indicates file found to cache, false otherwise | | errorType | number - error number if not successful. | | errorMsg | string - error message if not successful. | | hash | string - hash/key identifying the file | | name | name original file. ex. my-word-file.docx | | ext | extension portion of file name. ex. .docx | | dir | directory portion of full path (root dir + hash) | | path | full path to original file in cache |

remove

Remove a file from the cache.

const removeFileResult = await fileCache.remove(hash);

| Parameters | Description | | --- | --- | | hash | string - hash/key for file in cache |

Returns object

{
    "success": false,
    "errorType": null,
    "errorMsg": null
}

| Field | Description | | --- | --- | | success | boolean, true indicates file found and removed from cache, false otherwise | | errorType | number - error number if not successful. | | errorMsg | string - error message if not successful. |