{"id":24698570,"url":"https://github.com/faradayio/journaling-hash","last_synced_at":"2026-05-15T23:32:16.203Z","repository":{"id":26244802,"uuid":"29691798","full_name":"faradayio/journaling-hash","owner":"faradayio","description":"Immutable hash that can back itself up to a file and rebuild itself","archived":false,"fork":false,"pushed_at":"2015-01-24T18:23:12.000Z","size":152,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-02-27T13:29:09.108Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/faradayio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-22T17:53:21.000Z","updated_at":"2020-09-10T18:53:04.000Z","dependencies_parsed_at":"2022-09-09T00:31:24.595Z","dependency_job_id":null,"html_url":"https://github.com/faradayio/journaling-hash","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fjournaling-hash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fjournaling-hash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fjournaling-hash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fjournaling-hash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faradayio","download_url":"https://codeload.github.com/faradayio/journaling-hash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244902936,"owners_count":20529114,"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":"2025-01-27T04:29:20.575Z","updated_at":"2026-05-15T23:32:11.174Z","avatar_url":"https://github.com/faradayio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# journaling-hash\nImmutable hash that can back itself up to a file and rebuild itself\n\n\u003e \"I get knocked down, but I get up again\" - William Shakespeare\n\n## Quick use\n\n```console\nnpm install journaling-hash\n```\n\nHere is a simple example that will increment `number_of_cookies` every time you run it. If it crashes for some reason while incrementing the cookie count or writing to the file, the next time it is run it will recover to the last good state.\n\n```javascript\nvar journalingHash = require('journaling-hash');\n\njournalingHash('./state.log').then(function(state){\n  if (!state.has('number_of_cookies')) {\n    state.update({\n      number_of_cookies: 0\n    });\n  } else {\n    state.update({\n      number_of_cookies: state.get('number_of_cookies') + 1\n    });\n  }\n});\n```\n\n## Slow use\n\nHere is a less contrived example. It aquires a new cookie every 5 seconds, but if you kill the process it won't lose any of the cookies it's kept.\n\n```javascript\nvar journalingHash = require('journaling-hash');\n\nvar cookieTypes = ['Chocolate Chip Cookie', 'Macadamia Nut Cookie', 'Double Chocolate Cookie', 'Pretend Cookie'];\nvar downloadCookie = function(callback){\n  console.log('Starting cookie download...');\n  setTimeout(function(){\n    //give a random cookie\n    callback(cookieTypes[Math.floor(Math.random()*cookieTypes.length)]);\n  }, 5000);\n};\n\njournalingHash('./state.log').then(function(state){\n\n  if (!state.has('number_of_cookies')) {\n    console.log('First run. Prepare for cookies.');\n  } else {\n    console.log('Resuming a previous cookie aquisition operation.');\n    console.log(state.get('number_of_cookies')+' cookies found in '+state.journalPath);\n  }\n\n  var getCookie = function(){\n    downloadCookie(function(newCookie){\n      var cookieCount = state.get('number_of_cookies') || 0;\n      cookieCount++;\n\n      console.log('got a '+newCookie, 'bringing the total to '+cookieCount);\n\n      update = {\n        number_of_cookies: cookieCount,\n        cookies: {}\n      };\n      update['cookies'][cookieCount] = newCookie;\n\n      state.update(update).then(getCookie);\n    });\n  };\n\n  getCookie();\n});\n```\n\n## How it works\n\nWhen you run `journalingHash(path)` it opens that path, reads it, and reconstructs an [immutable hash](https://www.npmjs.com/package/immutable-hash) from it.\n\nThe promise is resolved with an object which serves as your interface to the hash.\n\nYou can use any of the methods that [immutable hash](https://www.npmjs.com/package/immutable-hash) provides.\n\nIn order to make changes to the hash, you must use the `update` function which is provided. This will `patch` the hash, and persist the changes to the filesystem.\n\n## Note\n\n`hash.patch(...)` will return a new immutable hash.\n\n`hash.update(...)` will call `hash.patch` and use the result to replace the internally stored hash, returning a promise that will resolve once the change has been written to the journal.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaradayio%2Fjournaling-hash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaradayio%2Fjournaling-hash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaradayio%2Fjournaling-hash/lists"}