-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (51 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var pump = require('pump')
var duplexify = require('duplexify')
var debug = require('debug')('skim-blob-store')
var noop = function() {}
var Skim = function(backend, remote) {
if (!(this instanceof Skim)) return new Skim(backend, remote)
this.backend = backend
this.remote = remote
}
Skim.prototype.createReadStream = function(obj) {
var backend = this.backend
var remote = this.remote
var dup = duplexify()
dup.setWritable(false)
backend.exists(obj, function(err, exists) {
if (err) return dup.destroy(err)
if (exists) debug('createReadStream from local')
else debug('createReadStream copying from remote')
if (exists) return dup.setReadable(backend.createReadStream(obj))
// copy remote blob into the local backend
pump(remote.createReadStream(obj), backend.createWriteStream(obj, function(err, meta) {
if (err) return dup.destroy(err)
dup.setReadable(backend.createReadStream(meta))
}), function(err) {
if (err) return dup.destroy(err) // in case the backend doesn't do destroy
})
})
return dup
}
Skim.prototype.createWriteStream = function() {
return this.backend.createWriteStream.apply(this.backend, arguments)
}
Skim.prototype.remove = function(obj, cb) {
if (!cb) cb = noop
var backend = this.backend
var remote = this.remote
backend.remove(obj, function(err) {
if (err) return cb(err)
remote.remove(obj, cb)
})
}
Skim.prototype.exists = function(obj, cb) {
var backend = this.backend
var remote = this.remote
backend.exists(obj, function(err, exists) {
if (err) return cb(err)
if (exists) return cb(null, exists)
remote.exists(obj, cb)
})
}
module.exports = Skim