feat: lazy loading node-jose (#2177)

This commit is contained in:
Alex Inkin
2023-03-04 02:31:41 +08:00
committed by Aiden McClelland
parent 3c0a82293c
commit efb318a979
3 changed files with 23 additions and 17 deletions

View File

@@ -2,5 +2,6 @@
"singleQuote": true, "singleQuote": true,
"semi": false, "semi": false,
"arrowParens": "avoid", "arrowParens": "avoid",
"trailingComma": "all" "trailingComma": "all",
"htmlWhitespaceSensitivity": "ignore"
} }

View File

@@ -4,20 +4,24 @@ import { RR, Encrypted } from './api.types'
import { DataModel } from 'src/app/services/patch-db/data-model' import { DataModel } from 'src/app/services/patch-db/data-model'
import { Log } from '@start9labs/shared' import { Log } from '@start9labs/shared'
import { WebSocketSubjectConfig } from 'rxjs/webSocket' import { WebSocketSubjectConfig } from 'rxjs/webSocket'
import * as jose from 'node-jose' import type { JWK } from 'node-jose'
export abstract class ApiService { export abstract class ApiService {
protected readonly jose = import('node-jose')
readonly patchStream$ = new BehaviorSubject<Update<DataModel>[]>([]) readonly patchStream$ = new BehaviorSubject<Update<DataModel>[]>([])
pubkey?: jose.JWK.Key pubkey?: JWK.Key
async encrypt(toEncrypt: string): Promise<Encrypted> { async encrypt(toEncrypt: string): Promise<Encrypted> {
if (!this.pubkey) throw new Error('No pubkey found!') const { pubkey } = this
const encrypted = await jose.JWE.createEncrypt(this.pubkey!)
.update(toEncrypt) if (!pubkey) throw new Error('No pubkey found!')
.final()
return { const encrypted = await this.jose.then(jose =>
encrypted, jose.JWE.createEncrypt(pubkey).update(toEncrypt).final(),
} )
return { encrypted }
} }
// http // http

View File

@@ -38,7 +38,6 @@ import { WebSocketSubjectConfig } from 'rxjs/webSocket'
import { AuthService } from '../auth.service' import { AuthService } from '../auth.service'
import { ConnectionService } from '../connection.service' import { ConnectionService } from '../connection.service'
import { StoreInfo } from '@start9labs/marketplace' import { StoreInfo } from '@start9labs/marketplace'
import * as jose from 'node-jose'
const PROGRESS: InstallProgress = { const PROGRESS: InstallProgress = {
size: 120, size: 120,
@@ -122,12 +121,14 @@ export class MockApiService extends ApiService {
// this.pubkey = await keystore.generate('EC', 'P-256') // this.pubkey = await keystore.generate('EC', 'P-256')
// generated from backend // generated from backend
this.pubkey = await jose.JWK.asKey({ this.pubkey = await this.jose.then(jose =>
kty: 'EC', jose.JWK.asKey({
crv: 'P-256', kty: 'EC',
x: 'yHTDYSfjU809fkSv9MmN4wuojf5c3cnD7ZDN13n-jz4', crv: 'P-256',
y: '8Mpkn744A5KDag0DmX2YivB63srjbugYZzWc3JOpQXI', x: 'yHTDYSfjU809fkSv9MmN4wuojf5c3cnD7ZDN13n-jz4',
}) y: '8Mpkn744A5KDag0DmX2YivB63srjbugYZzWc3JOpQXI',
}),
)
} }
async login(params: RR.LoginReq): Promise<RR.loginRes> { async login(params: RR.LoginReq): Promise<RR.loginRes> {