Skip to content

Commit

Permalink
Remove typed-db, fix mangled db, close #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jun 10, 2019
1 parent 1126339 commit eafb0d5
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 47 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"tiny-secp256k1": "^1.1.2",
"ts-loader": "^6.0.1",
"tslint": "^5.16.0",
"typed-db": "^1.0.1-1",
"typescript": "^3.5.1",
"web-ext-types": "^3.1.0",
"webcrypto-liner": "0.1.38",
Expand Down
12 changes: 3 additions & 9 deletions src/database/migrate/old.avatar.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
* ! Scheduled to remove it after Jan/1/2019
* ! This database should be readonly now.
*/
// tslint:disable: deprecation
import { readMangledDB } from './old.mangled.helper.1'
import { PersonIdentifier, Relation } from '../type'
import { deleteDB } from 'idb/with-async-ittr'
import * as Avatar from '../avatar'
import * as People from '../people'

import { Entity, Index, Db } from 'typed-db'
import { buildQuery } from '../../utils/utils'

@Entity()
class AvatarRecord {
@Index({ unique: true })
username!: string
nickname!: string
avatar!: ArrayBuffer
Expand All @@ -27,10 +24,7 @@ class AvatarRecord {
* @deprecated
*/
async function queryAvatarData() {
// tslint:disable-next-line: deprecation
const query = buildQuery(new Db('maskbook-avatar-store', 1), AvatarRecord)
const record = await query(t => t.openCursor().asList())
return record
return readMangledDB('maskbook-avatar-store', 1) as Promise<AvatarRecord[]>
}
const cache = new Map<string, string>()
/**
Expand Down
9 changes: 2 additions & 7 deletions src/database/migrate/old.keystore.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
* ! Scheduled to remove it after May/31/2019
* ! This database should be readonly now.
*/
import { Entity, Index, Db, Key } from 'typed-db'
import { OnlyRunInContext } from '@holoflows/kit/es'
// tslint:disable: deprecation
OnlyRunInContext('background', 'Key Store')
@Entity()
/** DO NOT Change the name of this class! It is used as key in the db! */
class CryptoKeyRecord {
@Index({ unique: true })
@Key()
username!: string
key!: { publicKey: JsonWebKey; privateKey?: JsonWebKey }
algor!: any
Expand All @@ -41,8 +37,7 @@ async function queryPeopleCryptoKey(): Promise<Exporting[]> {
privateKey: priv,
}
}
const query = buildQuery(new Db('maskbook-keystore-demo-v2', 1), CryptoKeyRecord)
const record = await query(t => t.openCursor().asList())
const record: CryptoKeyRecord[] = await readMangledDB('maskbook-keystore-demo-v2', 1)
return Promise.all(record.map(toReadCryptoKey))
}
//#region Store & Read CryptoKey
Expand Down Expand Up @@ -74,7 +69,7 @@ async function generateNewKey(whoami: PersonIdentifier): Promise<People.PersonRe
import { deleteDB } from 'idb/with-async-ittr'
import * as People from '../people'
import { PersonIdentifier } from '../type'
import { buildQuery } from '../../utils/utils'
import { readMangledDB } from './old.mangled.helper.1'

export default async function migrate() {
if (indexedDB.databases) {
Expand Down
15 changes: 4 additions & 11 deletions src/database/migrate/old.localKeys.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@
*/
import { deleteDB } from 'idb/with-async-ittr'
import * as People from '../people'
import { Entity, Index, Db, Key } from 'typed-db'
import { OnlyRunInContext } from '@holoflows/kit/es'
import { buildQuery } from '../../utils/utils'

import { readMangledDB } from './old.mangled.helper.1'
// tslint:disable: deprecation
OnlyRunInContext('background', 'Local Key Store')
@Entity()
/** DO NOT Change the name of this class! It is used as key in the db! */
class LocalCryptoKeyRecord {
@Index({ unique: true })
@Key()
username!: string
key!: CryptoKey
}
/**
* @deprecated
*/
async function getMyLocalKey(): Promise<LocalCryptoKeyRecord | null> {
// tslint:disable-next-line: deprecation
const query = buildQuery(new Db('maskbook-localkeystore-demo-v1', 1), LocalCryptoKeyRecord)
const record = await query(t => t.get('$self'))
if (!record) return null
return record
const [key] = (await readMangledDB('maskbook-localkeystore-demo-v1', 1)) as LocalCryptoKeyRecord[]
return key || null
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/database/migrate/old.mangled.helper.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { openDB } from 'idb/with-async-ittr'
/**
* @deprecated
* ? Webpack mangled the class name that provides to the typed-db lib.
* ? So the database name is also mangled too.
* ? This function is used to read out all data in the 1st version of database.
*/
export async function readMangledDB(dbname: string, version: number) {
const db = await openDB(dbname, version)
const stores = Array.from(db.objectStoreNames)

const results = await Promise.all(stores.map(storeName => db.getAll(storeName)))
return results.reduce((prev, curr) => prev.concat(curr), [])
}
14 changes: 0 additions & 14 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import { Db } from 'typed-db'
import { Newable } from 'typed-db/dist-release/src/db/Db'
import { Store } from 'typed-db/dist-release/src/db/Store'
import 'reflect-metadata'
import { CustomEventId } from './constants'
import { CustomEvents } from '../extension/injected-script/addEventListener'

export { sleep, timeout } from '@holoflows/kit/es/util/sleep'
/**
* Build a db
* @deprecated
*/
export const buildQuery = <Q extends Newable<any>>(db: Db, record: Q) => {
db.use(record)
return <T>(
cb: (tx: Store<InstanceType<Q>>) => Promise<T extends any[] ? T : T | undefined> | void,
mode: 'readonly' | 'readwrite' = 'readonly',
) => db.transaction([record], mode, t => cb(t.for(record)))
}

/**
* Get reference of file in both extension and storybook
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12612,11 +12612,6 @@ type-is@~1.6.17, type-is@~1.6.18:
media-typer "0.3.0"
mime-types "~2.1.24"

typed-db@^1.0.1-1:
version "1.0.1-1"
resolved "https://registry.yarnpkg.com/typed-db/-/typed-db-1.0.1-1.tgz#e1453a4d0edc0a5b54fba9c46347d13e7e51cef7"
integrity sha1-4UU6TQ7cCltU+6nEY0fRPn5Rzvc=

typed-styles@^0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/typed-styles/-/typed-styles-0.0.7.tgz#93392a008794c4595119ff62dde6809dbc40a3d9"
Expand Down

0 comments on commit eafb0d5

Please sign in to comment.