{"id":15834149,"url":"https://github.com/kaelzhang/node-hashed-fs","last_synced_at":"2026-01-20T18:56:01.246Z","repository":{"id":57261657,"uuid":"44790930","full_name":"kaelzhang/node-hashed-fs","owner":"kaelzhang","description":"Handle file system with content hashing","archived":false,"fork":false,"pushed_at":"2016-12-15T12:50:34.000Z","size":24,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T20:16:48.191Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaelzhang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-23T04:31:45.000Z","updated_at":"2018-10-10T15:12:31.000Z","dependencies_parsed_at":"2022-08-25T06:11:25.780Z","dependency_job_id":null,"html_url":"https://github.com/kaelzhang/node-hashed-fs","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-hashed-fs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-hashed-fs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-hashed-fs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaelzhang%2Fnode-hashed-fs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaelzhang","download_url":"https://codeload.github.com/kaelzhang/node-hashed-fs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640466,"owners_count":20971558,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T14:00:53.167Z","updated_at":"2026-01-20T18:56:01.234Z","avatar_url":"https://github.com/kaelzhang.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/kaelzhang/node-hashed-fs.svg?branch=master)](https://travis-ci.org/kaelzhang/node-hashed-fs)\n\u003c!-- optional npm version\n[![NPM version](https://badge.fury.io/js/hashed-fs.svg)](http://badge.fury.io/js/hashed-fs)\n--\u003e\n\u003c!-- optional npm downloads\n[![npm module downloads per month](http://img.shields.io/npm/dm/hashed-fs.svg)](https://www.npmjs.org/package/hashed-fs)\n--\u003e\n\u003c!-- optional dependency status\n[![Dependency Status](https://david-dm.org/kaelzhang/node-hashed-fs.svg)](https://david-dm.org/kaelzhang/node-hashed-fs)\n--\u003e\n\n# hashed-fs\n\nGracefully and safely manipulate file with cached content hash.\n\nhashed-fs will create hash string from the file content, cache it, and pass it to the callback of each instance method.\n\nAll hashed-fs instances in one node session will share the same cache.\n\n## Install\n\n```sh\n$ npm install hashed-fs --save\n```\n\n## Usage\n\n```js\nvar hfs = require('hashed-fs')(options);\nhfs.stat('/path/to/a.js', function(err, stat, hash){\n  stat; // is the nodejs `fs.Stat` object\n  hash; // the result hash\n});\n```\n\n- **options** `Object`\n  - crypto: `function()` method to crypto a file into a `hash`\n  - decorate: `function()` method to decorate the destination filename by flavoring with file `hash`.\n  - cache_file `path=` if specified, hashd-fs will load the cache file at the beginning\n  - only_hashed `Boolean=false` if true, it will only write to the hashed filename. By default, it will write two files.\n\nIn comparison with the corresponding vanilla `fs` method, each hashed-fs method has an additional parameter `hash` of the callback function, which is the encrypted hash of the file content.\n\n#### options.crypto `function(data)`\n\nThis method is an iterator handler generater which should returns a function.\nThe chunks of the file content, i.e, `data`, will be passed into `options.crypto()` one by one. `data` has the structure below:\n\n```js\n{\n  value: \u003cBuffer|String\u003e, // the chunk of data\n  done: \u003ctrue|false\u003e      // whether is the last chunk of data\n}\n```\n\nIf `data.done` is `true`, it means the last chunk of data received, and the `options.crypto` should return the encrypted result.\n\nBy default, it encrypts the file content using md5 algorithm, but you could specify it by yourself.\n\nFor example,\n```js\nvar crypto = require('crypto');\n\nfunction sha256 () {\n  var shasum = crypto.createHash('sha256');\n  return function(data){\n    if (data.value) {\n      shasum.update(data.value);\n    }\n\n    if (data.done) {\n      return shasum.digest('hex');\n    }\n  }\n}\n\nvar hfs = require('hashed-fs')({\n  crypto: sha256\n});\n```\n\n#### options.decorate `function(filename, hash)`\n\nDefines how `hashed-fs` should add hash string to the filename.\n\nIt can be synchronous methods or asynchronous ones by using the common [`this.async()`](https://www.npmjs.com/package/wrap-as-async) style.\n\n### hfs.readFile(filename, callback)\n\n```js\nhfs.readFile('/path/to/a.js', function(err, content, hash){\n  content;  // '// a'\n  hash;     // 'ce2e532998ddb06b4334620d74e0d938'\n});\n```\n\n- **callback** `function(err, content, hash)`\n  - `content` ``\n  - `hash` the `options.crypo()`d hash according to the file content.\n\nReads the file content, and get the stat.\n\n### hfs.copy(filename, dest, callback [, force=false])\n\n- **callback** `function(err, hash)`\n\nCopies the file of `filename` to `dest` along with the hash-decorated `dest`.\n\nIf the file is already in the cache, it will skip copying, except that `force` is `true`.\n\n```js\nhfs.copy('/path/to/a.js', '/dest/to', function(err, hash){\n  hash; // 'ce2e532998ddb06b4334620d74e0d938'\n});\n```\n\nThen the `/dest/to` folder will have **TWO** files:\n\n```sh\n/dest/to\n       | -- a.js          # if options.only_hashed is not true\n       | -- a-ce2e532.js\n```\n\n`ce2e532` is the first seven charactors of the `hash`. By changing the default `options.decorate` method, you could define your custom pathname to the destination.\n\n\n### hfs.stat(filename, callback)\n\n- **callback** `function(err, stat, hash, cached)`\n  - `stat` [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) is the file status of the file\n  - `cached` `Boolean` whether has read from cache.\n\nGets the file stat, and the hashed result.\n\n\n### hfs.writeFile(filename, content, callback)\n\nSimilar to `hfs.copy()`, this method will write **TWO** files\n\n\n### hfs.cache.save(callback)\n\nif `options.cache_file` is specified, it will save the cache to the file.\n\n\n### hfs.cache.map()\n\nReturns the `{filename: hash}` object.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Fnode-hashed-fs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaelzhang%2Fnode-hashed-fs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaelzhang%2Fnode-hashed-fs/lists"}