// If the store is full
private memoryChunks: { [ id: number ]: Buffer | true } = {}
private databaseName: string
- private putBulkTimeout
- private cleanerInterval
+ private putBulkTimeout: any
+ private cleanerInterval: any
private db: ChunkDatabase
private expirationDB: ExpirationDatabase
private readonly length: number
private readonly lastChunkLength: number
private readonly lastChunkIndex: number
- constructor (chunkLength: number, opts) {
+ constructor (chunkLength: number, opts: any) {
super()
this.databaseName = 'webtorrent-chunks-'
}, PeertubeChunkStore.BUFFERING_PUT_MS)
}
- get (index: number, opts, cb) {
+ get (index: number, opts: any, cb: any): any {
if (typeof opts === 'function') return this.get(index, null, opts)
// IndexDB could be slow, use our memory index first
const memoryChunk = this.memoryChunks[index]
- if (memoryChunk === undefined) return cb(null, new Buffer(0))
+ if (memoryChunk === undefined) {
+ const err = new Error('Chunk not found') as any
+ err['notFound'] = true
+
+ return process.nextTick(() => cb(err))
+ }
+
// Chunk in memory
if (memoryChunk !== true) return cb(null, memoryChunk)
})
}
- close (db) {
+ close (db: any) {
return this.destroy(db)
}
- async destroy (cb) {
+ async destroy (cb: any) {
try {
if (this.pendingPut) {
clearTimeout(this.putBulkTimeout)
this.cleanerInterval = null
}
+ if (this.db) {
+ await this.db.close()
+
+ await this.dropDatabase(this.databaseName)
+ }
+
if (this.expirationDB) {
await this.expirationDB.close()
this.expirationDB = null
}
- if (this.db) {
- console.log('Destroying IndexDB database %s.', this.databaseName)
- await this.db.close()
- await this.db.delete()
- }
-
return cb()
} catch (err) {
console.error('Cannot destroy peertube chunk store.', err)
}, PeertubeChunkStore.CLEANER_INTERVAL_MS)
}
- private checkExpiration () {
- this.expirationDB.transaction('rw', this.expirationDB.databases, async () => {
- // Update our database expiration since we are alive
- await this.expirationDB.databases.put({
- name: this.databaseName,
- expiration: new Date().getTime() + PeertubeChunkStore.CLEANER_EXPIRATION_MS
- })
+ private async checkExpiration () {
+ let databasesToDeleteInfo: { name: string }[] = []
- const now = new Date().getTime()
- const databasesToDeleteInfo = await this.expirationDB.databases.where('expiration').below(now).toArray()
+ try {
+ await this.expirationDB.transaction('rw', this.expirationDB.databases, async () => {
+ // Update our database expiration since we are alive
+ await this.expirationDB.databases.put({
+ name: this.databaseName,
+ expiration: new Date().getTime() + PeertubeChunkStore.CLEANER_EXPIRATION_MS
+ })
- for (const databaseToDeleteInfo of databasesToDeleteInfo) {
- await this.dropDatabase(databaseToDeleteInfo.name)
+ const now = new Date().getTime()
+ databasesToDeleteInfo = await this.expirationDB.databases.where('expiration').below(now).toArray()
+ })
+ } catch (err) {
+ console.error('Cannot update expiration of fetch expired databases.', err)
+ }
- await this.expirationDB.databases.where({ name: databaseToDeleteInfo.name }).delete()
- }
- }).catch(err => console.error('Cannot check expiration.', err))
+ for (const databaseToDeleteInfo of databasesToDeleteInfo) {
+ await this.dropDatabase(databaseToDeleteInfo.name)
+ }
}
- private dropDatabase (databaseName: string) {
+ private async dropDatabase (databaseName: string) {
const dbToDelete = new ChunkDatabase(databaseName)
+ console.log('Destroying IndexDB database %s.', databaseName)
+
+ try {
+ await dbToDelete.delete()
- console.log('Deleting %s.', databaseName)
- return dbToDelete.delete()
- .catch(err => console.error('Cannot delete %s.', databaseName))
+ await this.expirationDB.transaction('rw', this.expirationDB.databases, () => {
+ return this.expirationDB.databases.where({ name: databaseName }).delete()
+ })
+ } catch (err) {
+ console.error('Cannot delete %s.', databaseName, err)
+ }
}
- private nextTick (cb, err, val?) {
+ private nextTick (cb: any, err: Error, val?: any) {
process.nextTick(() => cb(err, val), undefined)
}
}