├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fs-block-store 2 | 3 | A simple block store that writes blocks to disk. 4 | 5 | ``` 6 | npm install fs-block-store 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` js 12 | const FSBlockStore = require('fs-block-store') 13 | 14 | const bs = new FSBlockStore('./some-dir') 15 | 16 | // put stuff in 17 | await bs.put(hashOfChunkAsBuf, chunkAsBuf) 18 | 19 | // get stuff out 20 | console.log(await bs.get(hashOfChunkAsBuf)) 21 | ``` 22 | 23 | Stores the chunks as individual files with the following filenames: 24 | 25 | ``` 26 | ./some-dir/{hex[1],hex[2]}/{hex[3],hex[4]}/{hex} 27 | ``` 28 | 29 | ## License 30 | 31 | MIT 32 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const BlockStore = require('./') 2 | const crypto = require('crypto') 3 | 4 | start() 5 | 6 | async function start () { 7 | const bs = new BlockStore('data') 8 | const key = crypto.createHash('sha256').update('hello world').digest('hex') 9 | 10 | await bs.put(key, Buffer.from('hello world')) 11 | console.log(await bs.get(key)) 12 | } 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | 4 | module.exports = class FSBlockStore { 5 | constructor (dir) { 6 | this.directory = dir 7 | } 8 | 9 | get (key) { 10 | const hex = key.toString('hex') 11 | const filename = path.join(this.directory, hex.slice(0, 2), hex.slice(2, 4), hex) 12 | 13 | return fs.promises.readFile(filename) 14 | } 15 | 16 | async put (key, buf) { 17 | const hex = key.toString('hex') 18 | const dir = path.join(this.directory, hex.slice(0, 2), hex.slice(2, 4)) 19 | const filename = path.join(dir, hex) 20 | 21 | await fs.promises.mkdir(dir, { recursive: true }) 22 | return fs.promises.writeFile(filename, buf) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fs-block-store", 3 | "version": "1.0.0", 4 | "description": "A simple block store that writes blocks to disk.", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": {}, 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/mafintosh/fs-block-store.git" 11 | }, 12 | "author": "Mathias Buus (@mafintosh)", 13 | "license": "MIT", 14 | "bugs": { 15 | "url": "https://github.com/mafintosh/fs-block-store/issues" 16 | }, 17 | "homepage": "https://github.com/mafintosh/fs-block-store" 18 | } 19 | --------------------------------------------------------------------------------