forked from DiegoRBaquero/lf-chunk-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (98 loc) · 3.4 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module.exports = Storage
var localforage = require('localforage')
function Storage (chunkLength, opts) {
if (!(this instanceof Storage)) return new Storage(chunkLength, opts)
if (!opts) opts = {}
this.chunkLength = Number(chunkLength)
if (!this.chunkLength) throw new Error('First argument must be a chunk length')
this.prefix = opts.prefix || opts.files ? opts.files[0].path : Math.random().toString(36)
this.chunks = []
this.closed = false
this.length = Number(opts.length) || Infinity
this.overlap = false
if (this.length !== Infinity) {
this.lastChunkLength = (this.length % this.chunkLength) || this.chunkLength
this.lastChunkIndex = Math.ceil(this.length / this.chunkLength) - 1
}
this.localForage = localforage.createInstance({})
}
Storage.prototype.put = function (index, buf, cb) {
console.info('put ' + index)
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
var isLastChunk = (index === this.lastChunkIndex)
if (isLastChunk && buf.length !== this.lastChunkLength) {
return nextTick(cb, new Error('Last chunk length must be ' + this.lastChunkLength))
}
if (!isLastChunk && buf.length !== this.chunkLength) {
return nextTick(cb, new Error('Chunk length must be ' + this.chunkLength))
}
var toInsert = JSON.stringify(JSON.parse(JSON.stringify({object: buf, length: buf.length, time: Date.now()})))
this.localForage.setItem(this.prefix + '_' + index, toInsert, function(err, value) {
console.info('huh')
if(err) {
console.error(err)
this.chunks[index] = buf
return
}
nextTick(cb, null)
})
}
Storage.prototype.get = function (index, opts, cb) {
console.info('get ' + index)
if (typeof opts === 'function') return this.get(index, null, opts)
if (this.closed) return nextTick(cb, new Error('Storage is closed'))
var buf = this.chunks[index]
if (!buf) {
console.info('not in mem ' + index)
this.localForage.getItem(this.prefix + '_' + index, function (err, lsItem) {
if (err != null) {
console.error(err)
return nextTick(cb, new Error('Chunk not found'))
}
if (lsItem != null) {
console.info('creating buf ' + index)
buf = new Buffer(JSON.parse(lsItem).object.data)
} else {
console.info('item is null')
}
if (!buf) return nextTick(cb, new Error('Chunk not found'))
if (!opts) return nextTick(cb, null, buf)
var offset = opts.offset || 0
var len = opts.length || (buf.length - offset)
nextTick(cb, null, buf.slice(offset, len + offset))
})
} else {
console.info('In memory ' + index)
if (!opts) return nextTick(cb, null, buf)
var offset = opts.offset || 0
var len = opts.length || (buf.length - offset)
nextTick(cb, null, buf.slice(offset, len + offset))
}
}
Storage.prototype.close = function (cb) {
if (this.closed) return nextTick(cb, new Error('Storage is already closed'))
this.closed = true
nextTick(cb, null)
}
Storage.prototype.destroy = function (cb) {
var self = this
this.closed = true
this.chunks = null
this.localForage.keys(function (err, keys) {
if (err != null) {
console.error(err)
}
for (var key in keys) {
key = keys[key]
if (key.startsWith(self.prefix)) {
self.localForage.removeItem(key)
}
}
nextTick(cb, null)
})
}
function nextTick (cb, err, val) {
process.nextTick(function () {
if (cb) cb(err, val)
})
}