0.0.8-alpha

Signed-off-by: T-Hax <>
This commit is contained in:
T-Hax 2023-05-11 18:49:07 +00:00
parent efdc55df2c
commit 798598cfff
37 changed files with 835 additions and 188 deletions

View file

@ -13,7 +13,7 @@
"crypto",
"zk"
],
"version": "0.0.7-alpha",
"version": "0.0.8-alpha",
"engines": {
"node": "^18"
},
@ -25,12 +25,14 @@
],
"dependencies": {
"@tornado/sdk-utils": "workspace:*",
"archiver": "^5.3.1",
"ethers": "^5",
"pouchdb": "^8.0.1",
"pouchdb-adapter-memory": "^8.0.1",
"pouchdb-collate": "^8.0.1"
},
"devDependencies": {
"@types/archiver": "^5.3.2",
"@types/big-integer": "^0.0.31",
"@types/chai": "^4.2.18",
"@types/fs-extra": "^11.0.1",
@ -77,6 +79,5 @@
"tsconfig-paths@4.2.0": {
"unplugged": true
}
},
"stableVersion": "2023.04.28"
}
}

View file

@ -1,6 +1,6 @@
// Big modules
import { BigNumber } from 'ethers'
import { existsSync, mkdirSync, readFileSync } from 'fs'
import { existsSync, mkdirSync, readFileSync, createWriteStream } from 'fs'
import { opendir, readFile, rm } from 'fs/promises'
import { createInterface } from 'readline'
@ -11,6 +11,9 @@ import { AsyncUtils, NumberUtils, ErrorUtils } from '@tornado/sdk-utils'
import PouchDB from 'pouchdb'
import * as PouchDBAdapterMemory from 'pouchdb-adapter-memory'
// Archiving
import archiver from 'archiver'
// @ts-ignore
import { toIndexableString } from 'pouchdb-collate'
@ -592,6 +595,9 @@ export namespace Docs {
export namespace Cache {
export class Base<T extends Docs.Base> {
private _adapter: string
private _path: string
name: string
db: PouchDB.Database<T>
@ -611,7 +617,59 @@ export namespace Cache {
if (!Files.cacheDirExists(options?.cachePath)) Files.makeCacheDir()
this.db = new PouchDB<T>(Files.getCachePath(name, options?.cachePath), { adapter: dbAdapter })
this._path = Files.getCachePath(name, options?.cachePath)
this._adapter = dbAdapter
this.db = new PouchDB<T>(this._path, { adapter: dbAdapter })
}
async exportAsArchive(outDirPath?: string, debug: boolean = false): Promise<void> {
await this.close()
if (outDirPath) outDirPath = outDirPath[outDirPath.length - 1] != '/' ? outDirPath + '/' : outDirPath
const outStream = createWriteStream((outDirPath ?? Files.getCachePath('')) + this.name + '.zip')
const archive = archiver('zip', {
zlib: {
level: 9
}
})
// listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
if (debug)
outStream.on('close', function () {
console.debug('Cache.exportAsArchive: ' + archive.pointer() + ' total bytes written.')
console.debug('Cache.exportAsArchive: fo closed.')
})
// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
if (debug)
outStream.on('end', function () {
console.log('Cache.exportAsArchive: drained.')
})
archive.on('warning', (err) => {
if (err.code != 'ENOENT') throw ErrorUtils.ensureError(err)
})
archive.on('error', (err) => {
throw ErrorUtils.ensureError(err)
})
archive.pipe(outStream)
archive.directory(this._path, this.name)
await archive.finalize()
outStream.close()
this.db = new PouchDB<T>(this._path, { adapter: this._adapter })
}
async get(keys: Array<any>): Promise<T> {
@ -621,11 +679,15 @@ export namespace Cache {
}
async close(): Promise<void> {
await this.db.close()
await this.db.close().catch((err) => {
throw ErrorUtils.ensureError(err)
})
}
async clear(): Promise<void> {
await this.db.destroy()
await this.db.destroy().catch((err) => {
throw ErrorUtils.ensureError(err)
})
}
}