Skip to content

Commit efb318a

Browse files
waterpleadr-bonez
authored andcommitted
feat: lazy loading node-jose (#2177)
1 parent 3c0a822 commit efb318a

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

frontend/.prettierrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"singleQuote": true,
33
"semi": false,
44
"arrowParens": "avoid",
5-
"trailingComma": "all"
5+
"trailingComma": "all",
6+
"htmlWhitespaceSensitivity": "ignore"
67
}

frontend/projects/ui/src/app/services/api/embassy-api.service.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ import { RR, Encrypted } from './api.types'
44
import { DataModel } from 'src/app/services/patch-db/data-model'
55
import { Log } from '@start9labs/shared'
66
import { WebSocketSubjectConfig } from 'rxjs/webSocket'
7-
import * as jose from 'node-jose'
7+
import type { JWK } from 'node-jose'
88

99
export abstract class ApiService {
10+
protected readonly jose = import('node-jose')
11+
1012
readonly patchStream$ = new BehaviorSubject<Update<DataModel>[]>([])
11-
pubkey?: jose.JWK.Key
13+
pubkey?: JWK.Key
1214

1315
async encrypt(toEncrypt: string): Promise<Encrypted> {
14-
if (!this.pubkey) throw new Error('No pubkey found!')
15-
const encrypted = await jose.JWE.createEncrypt(this.pubkey!)
16-
.update(toEncrypt)
17-
.final()
18-
return {
19-
encrypted,
20-
}
16+
const { pubkey } = this
17+
18+
if (!pubkey) throw new Error('No pubkey found!')
19+
20+
const encrypted = await this.jose.then(jose =>
21+
jose.JWE.createEncrypt(pubkey).update(toEncrypt).final(),
22+
)
23+
24+
return { encrypted }
2125
}
2226

2327
// http

frontend/projects/ui/src/app/services/api/embassy-mock-api.service.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { WebSocketSubjectConfig } from 'rxjs/webSocket'
3838
import { AuthService } from '../auth.service'
3939
import { ConnectionService } from '../connection.service'
4040
import { StoreInfo } from '@start9labs/marketplace'
41-
import * as jose from 'node-jose'
4241

4342
const PROGRESS: InstallProgress = {
4443
size: 120,
@@ -122,12 +121,14 @@ export class MockApiService extends ApiService {
122121
// this.pubkey = await keystore.generate('EC', 'P-256')
123122

124123
// generated from backend
125-
this.pubkey = await jose.JWK.asKey({
126-
kty: 'EC',
127-
crv: 'P-256',
128-
x: 'yHTDYSfjU809fkSv9MmN4wuojf5c3cnD7ZDN13n-jz4',
129-
y: '8Mpkn744A5KDag0DmX2YivB63srjbugYZzWc3JOpQXI',
130-
})
124+
this.pubkey = await this.jose.then(jose =>
125+
jose.JWK.asKey({
126+
kty: 'EC',
127+
crv: 'P-256',
128+
x: 'yHTDYSfjU809fkSv9MmN4wuojf5c3cnD7ZDN13n-jz4',
129+
y: '8Mpkn744A5KDag0DmX2YivB63srjbugYZzWc3JOpQXI',
130+
}),
131+
)
131132
}
132133

133134
async login(params: RR.LoginReq): Promise<RR.loginRes> {

0 commit comments

Comments
 (0)