-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
141 lines (133 loc) · 4.16 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var Abstract = require('abstract-random-access')
var inherits = require('inherits')
var nextTick = require('next-tick')
var once = require('once')
var blocks = require('./lib/blocks.js')
var bufferFrom = require('buffer-from')
var bufferAlloc = require('buffer-alloc')
var DELIM = '\0'
module.exports = function (dbname, xopts) {
if (!xopts) xopts = {}
var idb = xopts.idb || (typeof window !== 'undefined'
? window.indexedDB || window.mozIndexedDB
|| window.webkitIndexedDB || window.msIndexedDB
: null)
if (!idb) throw new Error('indexedDB not present and not given')
var db = null, dbqueue = []
if (typeof idb.open === 'function') {
var req = idb.open(dbname)
req.addEventListener('upgradeneeded', function () {
db = req.result
db.createObjectStore('data')
})
req.addEventListener('success', function () {
db = req.result
dbqueue.forEach(function (cb) { cb(db) })
dbqueue = null
})
} else {
db = idb
}
return function (name, opts) {
if (typeof name === 'object') {
opts = name
name = opts.name
}
return new Store(Object.assign({ db: getdb }, xopts, opts))
}
function getdb (cb) {
if (db) nextTick(function () { cb(db) })
else dbqueue.push(cb)
}
}
function Store (opts) {
if (!(this instanceof Store)) return new Store(opts)
Abstract.call(this)
if (!opts) opts = {}
if (typeof opts === 'string') opts = { name: opts }
this.size = opts.size || 4096
this.name = opts.name
this.length = opts.length || 0
this._getdb = opts.db
}
inherits(Store, Abstract)
Store.prototype._blocks = function (i, j) {
return blocks(this.size, i, j)
}
Store.prototype._read = function (offset, length, cb) {
var self = this
cb = once(cb)
var buffers = []
self._store('readonly', function (err, store) {
if (err) return cb(err)
var offsets = self._blocks(offset, offset+length)
var pending = offsets.length + 1
var firstBlock = offsets.length > 0 ? offsets[0].block : 0
var j = 0
for (var i = 0; i < offsets.length; i++) (function (o) {
var key = self.name + DELIM + o.block
backify(store.get(key), function (err, ev) {
if (err) return cb(err)
buffers[o.block-firstBlock] = ev.target.result
? bufferFrom(ev.target.result.subarray(o.start,o.end))
: bufferAlloc(o.end-o.start)
if (--pending === 0) cb(null, Buffer.concat(buffers))
})
})(offsets[i])
if (--pending === 0) cb(null, Buffer.concat(buffers))
})
}
Store.prototype._write = function (offset, buf, cb) {
var self = this
cb = once(cb)
self._store('readwrite', function (err, store) {
if (err) return cb(err)
var offsets = self._blocks(offset, offset + buf.length)
var pending = 1
var buffers = {}
for (var i = 0; i < offsets.length; i++) (function (o,i) {
if (o.end-o.start === self.size) return
pending++
var key = self.name + DELIM + o.block
backify(store.get(key), function (err, ev) {
if (err) return cb(err)
buffers[i] = bufferFrom(ev.target.result || bufferAlloc(self.size))
if (--pending === 0) write(store, offsets, buffers)
})
})(offsets[i],i)
if (--pending === 0) write(store, offsets, buffers)
})
function write (store, offsets, buffers) {
for (var i = 0, j = 0; i < offsets.length; i++) {
var o = offsets[i]
var len = o.end - o.start
if (len === self.size) {
block = buf.slice(j,j+len)
} else {
block = buffers[i]
buf.copy(block, o.start, j, j+len)
}
store.put(block,self.name + DELIM + o.block)
j += len
}
store.transaction.addEventListener('complete', function () {
self.length = Math.max(self.length, offset + buf.length)
cb(null)
})
store.transaction.addEventListener('error', cb)
}
}
Store.prototype._store = function (mode, cb) {
cb = once(cb)
var self = this
self._getdb(function (db) {
var tx = db.transaction(['data'], mode)
var store = tx.objectStore('data')
tx.addEventListener('error', cb)
cb(null, store)
})
}
function backify (r, cb) {
r.addEventListener('success', function (ev) { cb(null, ev) })
r.addEventListener('error', cb)
}