This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
228 lines (199 loc) · 6.72 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* globals window */
const EventEmitter = require('events')
const level = require('level-browserify')
const sublevel = require('subleveldown')
const levelPromisify = require('level-promise')
const {debug, veryDebug, assert, getObjectChecksum, URL} = require('./lib/util')
const {SchemaError} = require('./lib/errors')
const TableDef = require('./lib/table-def')
const Indexer = require('./lib/indexer')
const WebDBTable = require('./lib/table')
const flatten = require('lodash.flatten')
class WebDB extends EventEmitter {
constructor (name, opts = {}) {
super()
if (typeof window === 'undefined' && !opts.DatArchive) {
throw new Error('Must provide {DatArchive} opt when using WebDB outside the browser.')
}
this.level = false
this.name = name
this.isBeingOpened = false
this.isOpen = false
this.DatArchive = opts.DatArchive || window.DatArchive
this._indexMetaLevel = null
this._tableSchemaLevel = null
this._tableDefs = {}
this._archives = {}
this._tablesToRebuild = []
this._activeSchema = null
this._tableFilePatterns = []
this._dbReadyPromise = new Promise((resolve, reject) => {
this.once('open', () => resolve(this))
this.once('open-failed', reject)
})
}
async open () {
// guard against duplicate opens
if (this.isBeingOpened || this.level) {
veryDebug('duplicate open, returning ready promise')
return this._dbReadyPromise
}
if (this.isOpen) {
return
}
this.isBeingOpened = true // TODO needed?
var neededRebuilds = []
// open the db
debug('opening')
try {
this.level = level(this.name, {valueEncoding: 'json'})
this._tableSchemaLevel = sublevel(this.level, '_tableSchema', {valueEncoding: 'json'})
levelPromisify(this._tableSchemaLevel)
this._indexMetaLevel = sublevel(this.level, '_indexMeta', {valueEncoding: 'json'})
levelPromisify(this._indexMetaLevel)
// construct the tables
const tableNames = Object.keys(this._tableDefs)
debug('adding tables', tableNames)
tableNames.forEach(tableName => {
this[tableName] = new WebDBTable(this, tableName, this._tableDefs[tableName])
this._tableFilePatterns.push(this[tableName]._filePattern)
})
this._tableFilePatterns = flatten(this._tableFilePatterns)
// detect table-definition changes
for (let i = 0; i < tableNames.length; i++) {
let tableName = tableNames[i]
let tableChecksum = this._tableDefs[tableName].checksum
// load the saved checksum
let lastChecksum
try {
let tableMeta = await this._tableSchemaLevel.get(tableName)
lastChecksum = tableMeta.checksum
} catch (e) {}
// compare
if (lastChecksum !== tableChecksum) {
neededRebuilds.push(tableName)
}
}
// run rebuilds
// TODO go per-table
await Indexer.resetOutdatedIndexes(this, neededRebuilds)
this.emit('indexes-reset')
// save checksums
for (let i = 0; i < tableNames.length; i++) {
let tableName = tableNames[i]
let tableChecksum = this._tableDefs[tableName].checksum
await this._tableSchemaLevel.put(tableName, {checksum: tableChecksum})
}
this.isBeingOpened = false
this.isOpen = true
// events
debug('opened')
this.emit('open')
} catch (e) {
console.error('Upgrade has failed', e)
this.isBeingOpened = false
this.emit('open-failed', e)
throw e
}
return {
rebuilds: neededRebuilds
}
}
async close () {
if (!this.isOpen) return
debug('closing')
this.isOpen = false
if (this.level) {
this.listSources().forEach(url => Indexer.unwatchArchive(this, this._archives[url]))
this._archives = {}
await new Promise(resolve => this.level.close(resolve))
this.level = null
veryDebug('db .level closed')
} else {
veryDebug('db .level didnt yet exist')
}
}
async delete () {
if (this.isOpen) {
await this.close()
}
await WebDB.delete(this.name)
}
define (tableName, definition) {
assert(!this.level && !this.isBeingOpened, SchemaError, 'Cannot define a table when database is open')
let checksum = getObjectChecksum(definition)
TableDef.validateAndSanitize(definition)
definition.checksum = checksum
this._tableDefs[tableName] = definition
}
get tables () {
return Object.keys(this._tableDefs)
.filter(name => !name.startsWith('_'))
.map(name => this[name])
}
async indexArchive (archive, opts = {}) {
opts.watch = (typeof opts.watch === 'boolean') ? opts.watch : true
// handle array case
if (Array.isArray(archive)) {
return Promise.all(archive.map(a => this.indexArchive(a, opts)))
}
// create our own new DatArchive instance
archive = typeof archive === 'string' ? new (this.DatArchive)(archive) : archive
debug('WebDB.indexArchive', archive.url)
if (!(archive.url in this._archives)) {
// store and process
this._archives[archive.url] = archive
await Indexer.addArchive(this, archive, opts)
} else {
await Indexer.indexArchive(this, archive)
}
}
async unindexArchive (archive) {
archive = typeof archive === 'string' ? new (this.DatArchive)(archive) : archive
if (archive.url in this._archives) {
debug('WebDB.unindexArchive', archive.url)
delete this._archives[archive.url]
await Indexer.removeArchive(this, archive)
}
}
async indexFile (archive, filepath) {
if (typeof archive === 'string') {
const urlp = new URL(archive)
archive = new (this.DatArchive)(urlp.protocol + '//' + urlp.hostname)
return this.indexFile(archive, urlp.pathname)
}
await Indexer.readAndIndexFile(this, archive, filepath)
}
async unindexFile (archive, filepath) {
if (typeof archive === 'string') {
const urlp = new URL(archive)
archive = new (this.DatArchive)(urlp.protocol + '//' + urlp.hostname)
return this.indexFile(archive, urlp.pathname)
}
await Indexer.unindexFile(this, archive, filepath)
}
listSources () {
return Object.keys(this._archives)
}
isSource (url) {
if (!url) return false
if (url.url) url = url.url // an archive
return (url in this._archives)
}
static list () {
// TODO
}
static delete (name) {
if (typeof level.destroy !== 'function') {
throw new Error('Cannot .delete() databases outside of the browser environment. You should just delete the files manually.')
}
// delete the database from indexeddb
return new Promise((resolve, reject) => {
level.destroy(name, err => {
if (err) reject(err)
else resolve()
})
})
}
}
module.exports = WebDB