|
| 1 | +var concat = require('concat-stream') |
| 2 | +var fs = require('fs-blob-store') |
| 3 | +var path = require('path') |
| 4 | +var lockFile = require('lockfile') |
| 5 | + |
| 6 | +function BlobStore (base_path) { |
| 7 | + this.store = fs(base_path) |
| 8 | + this.LOCK_PATH = path.join(base_path, 'repo.lock') |
| 9 | +} |
| 10 | + |
| 11 | +BlobStore.prototype = { |
| 12 | + |
| 13 | + /** |
| 14 | + * Read a blob on a given path |
| 15 | + * It holds the repo.lock while reading |
| 16 | + * |
| 17 | + * @param {String} key |
| 18 | + * @param {Function} cb |
| 19 | + * @return {ReadableStream} |
| 20 | + */ |
| 21 | + read: function (key, cb) { |
| 22 | + var store = this.store |
| 23 | + var LOCK_PATH = this.LOCK_PATH |
| 24 | + var rs = store.createReadStream(key) |
| 25 | + |
| 26 | + function onFinish (buff) { |
| 27 | + lockFile.unlock(LOCK_PATH, function (err) { |
| 28 | + if (err) return cb(err) |
| 29 | + |
| 30 | + cb(null, buff.toString('utf8')) |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + function onLock (err) { |
| 35 | + if (err) return cb(err) |
| 36 | + |
| 37 | + rs.on('error', cb) |
| 38 | + rs.pipe(concat(onFinish)) |
| 39 | + } |
| 40 | + |
| 41 | + lockFile.lock(LOCK_PATH, {}, onLock) |
| 42 | + |
| 43 | + return rs |
| 44 | + }, |
| 45 | + |
| 46 | + /** |
| 47 | + * Read a blob on a given path |
| 48 | + * It does not lock |
| 49 | + * |
| 50 | + * @param {String} key |
| 51 | + * @param {Function} cb |
| 52 | + * @return {ReadableStream} |
| 53 | + */ |
| 54 | + readWithoutLock: function (key, cb) { |
| 55 | + var rs = this.store.createReadStream(key) |
| 56 | + |
| 57 | + rs.on('error', cb) |
| 58 | + rs.pipe(concat(function (buff) { |
| 59 | + cb(null, buff.toString('utf8')) |
| 60 | + })) |
| 61 | + |
| 62 | + return rs |
| 63 | + }, |
| 64 | + |
| 65 | + /** |
| 66 | + * Write the contents to the blob in the given path |
| 67 | + * It holds the repo.lock while reading |
| 68 | + * |
| 69 | + * @param {String} key |
| 70 | + * @param {Function} cb |
| 71 | + * @return {WritableStream} |
| 72 | + */ |
| 73 | + write: function (key, content, cb) { |
| 74 | + var store = this.store |
| 75 | + var LOCK_PATH = this.LOCK_PATH |
| 76 | + var ws = store.createWriteStream(key) |
| 77 | + |
| 78 | + function onFinish (err) { |
| 79 | + if (err) return cb(err) |
| 80 | + |
| 81 | + lockFile.unlock(LOCK_PATH, cb) |
| 82 | + } |
| 83 | + |
| 84 | + function onLock (err) { |
| 85 | + if (err) return cb(err) |
| 86 | + |
| 87 | + ws.on('error', cb) |
| 88 | + ws.on('finish', onFinish) |
| 89 | + |
| 90 | + ws.write(content) |
| 91 | + ws.end() |
| 92 | + } |
| 93 | + |
| 94 | + lockFile.lock(LOCK_PATH, {}, onLock) |
| 95 | + |
| 96 | + return ws |
| 97 | + }, |
| 98 | + |
| 99 | + /** |
| 100 | + * Writes content to a blob on a given path |
| 101 | + * It does not lock |
| 102 | + * |
| 103 | + * @param {String} key |
| 104 | + * @param {String} content |
| 105 | + * @param {Function} cb |
| 106 | + * @return {WritableStream} |
| 107 | + */ |
| 108 | + writeWithoutLock: function (key, content, cb) { |
| 109 | + var ws = this.store.createWriteStream(key) |
| 110 | + |
| 111 | + ws.on('error', cb) |
| 112 | + ws.on('finish', cb) |
| 113 | + |
| 114 | + ws.write(content) |
| 115 | + ws.end() |
| 116 | + |
| 117 | + return ws |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +module.exports = BlobStore |
0 commit comments