1
- var Abstract = require ( 'random-access-storage' )
1
+ var RandomAccess = require ( 'random-access-storage' )
2
2
var inherits = require ( 'inherits' )
3
3
var nextTick = require ( 'next-tick' )
4
4
var once = require ( 'once' )
@@ -10,11 +10,13 @@ var DELIM = '\0'
10
10
11
11
module . exports = function ( dbname , xopts ) {
12
12
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 )
18
20
if ( ! idb ) throw new Error ( 'indexedDB not present and not given' )
19
21
var db = null
20
22
var dbqueue = [ ]
@@ -51,15 +53,15 @@ module.exports = function (dbname, xopts) {
51
53
52
54
function Store ( opts ) {
53
55
if ( ! ( this instanceof Store ) ) return new Store ( opts )
54
- Abstract . call ( this )
56
+ RandomAccess . call ( this )
55
57
if ( ! opts ) opts = { }
56
58
if ( typeof opts === 'string' ) opts = { name : opts }
57
59
this . size = opts . size || 4096
58
60
this . name = opts . name
59
61
this . length = opts . length || 0
60
62
this . _getdb = opts . db
61
63
}
62
- inherits ( Store , Abstract )
64
+ inherits ( Store , RandomAccess )
63
65
64
66
Store . prototype . _blocks = function ( i , j ) {
65
67
return blocks ( this . size , i , j )
@@ -116,7 +118,7 @@ Store.prototype._write = function (req) {
116
118
var o = offsets [ i ]
117
119
var len = o . end - o . start
118
120
if ( len === self . size ) {
119
- block = req . data . slice ( j , j + len )
121
+ block = bufferFrom ( req . data . slice ( j , j + len ) )
120
122
} else {
121
123
block = buffers [ i ]
122
124
req . data . copy ( block , o . start , j , j + len )
@@ -131,7 +133,9 @@ Store.prototype._write = function (req) {
131
133
self . length = length
132
134
req . callback ( null )
133
135
} )
134
- store . transaction . addEventListener ( 'error' , req . callback )
136
+ store . transaction . addEventListener ( 'error' , function ( err ) {
137
+ req . callback ( err )
138
+ } )
135
139
}
136
140
}
137
141
@@ -160,6 +164,20 @@ Store.prototype._open = function (req) {
160
164
} )
161
165
}
162
166
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
+
163
181
function backify ( r , cb ) {
164
182
r . addEventListener ( 'success' , function ( ev ) { cb ( null , ev ) } )
165
183
r . addEventListener ( 'error' , cb )
0 commit comments