Skip to content

Commit c377663

Browse files
match substack's fixes
1 parent cb2145e commit c377663

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Abstract = require('random-access-storage')
1+
var RandomAccess = require('random-access-storage')
22
var inherits = require('inherits')
33
var nextTick = require('next-tick')
44
var once = require('once')
@@ -10,11 +10,13 @@ var DELIM = '\0'
1010

1111
module.exports = function (dbname, xopts) {
1212
if (!xopts) xopts = {}
13-
var idb = xopts.idb || (typeof window !== 'undefined'
14-
? window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB
15-
: !indexedDB
16-
? null
17-
: indexedDB)
13+
14+
var win = typeof window !== 'undefined' ? window
15+
: (typeof self !== 'undefined' ? self : {})
16+
17+
var idb = xopts.idb || (typeof win !== 'undefined'
18+
? win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB
19+
: null)
1820
if (!idb) throw new Error('indexedDB not present and not given')
1921
var db = null
2022
var dbqueue = []
@@ -51,15 +53,15 @@ module.exports = function (dbname, xopts) {
5153

5254
function Store (opts) {
5355
if (!(this instanceof Store)) return new Store(opts)
54-
Abstract.call(this)
56+
RandomAccess.call(this)
5557
if (!opts) opts = {}
5658
if (typeof opts === 'string') opts = { name: opts }
5759
this.size = opts.size || 4096
5860
this.name = opts.name
5961
this.length = opts.length || 0
6062
this._getdb = opts.db
6163
}
62-
inherits(Store, Abstract)
64+
inherits(Store, RandomAccess)
6365

6466
Store.prototype._blocks = function (i, j) {
6567
return blocks(this.size, i, j)
@@ -116,7 +118,7 @@ Store.prototype._write = function (req) {
116118
var o = offsets[i]
117119
var len = o.end - o.start
118120
if (len === self.size) {
119-
block = req.data.slice(j, j + len)
121+
block = bufferFrom(req.data.slice(j, j + len))
120122
} else {
121123
block = buffers[i]
122124
req.data.copy(block, o.start, j, j + len)
@@ -131,7 +133,9 @@ Store.prototype._write = function (req) {
131133
self.length = length
132134
req.callback(null)
133135
})
134-
store.transaction.addEventListener('error', req.callback)
136+
store.transaction.addEventListener('error', function (err) {
137+
req.callback(err)
138+
})
135139
}
136140
}
137141

@@ -160,6 +164,20 @@ Store.prototype._open = function (req) {
160164
})
161165
}
162166

167+
Store.prototype._close = function (req) {
168+
this._getdb(function (db) {
169+
db.close()
170+
req.callback()
171+
})
172+
}
173+
174+
Store.prototype._stat = function (req) {
175+
var self = this
176+
nextTick(function () {
177+
req.callback(null, { size: self.length })
178+
})
179+
}
180+
163181
function backify (r, cb) {
164182
r.addEventListener('success', function (ev) { cb(null, ev) })
165183
r.addEventListener('error', cb)

0 commit comments

Comments
 (0)