-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate encoding/decoding of RemoteConfig
- Loading branch information
1 parent
6919703
commit d6510d5
Showing
4 changed files
with
45 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { IEncryptionService } from "@/common/encryption/EncryptionService"; | ||
import RemoteConfig, { RemoteConfigSchema } from "./RemoteConfig"; | ||
|
||
/** | ||
* Encodes and decodes remote configs. | ||
* | ||
* The remote config is first stringified to JSON, then encrypted, and finally encoded in base64. | ||
* | ||
* At the receiving end, the encoded string is first decoded from base64, then decrypted, and finally parsed as JSON. | ||
*/ | ||
export default class RemoteConfigEncoder { | ||
private readonly encryptionService: IEncryptionService; | ||
|
||
constructor(encryptionService: IEncryptionService) { | ||
this.encryptionService = encryptionService; | ||
} | ||
|
||
encode(remoteConfig: RemoteConfig): string { | ||
const jsonString = JSON.stringify(remoteConfig); | ||
const encryptedString = this.encryptionService.encrypt(jsonString); | ||
return Buffer.from(encryptedString).toString('base64'); | ||
} | ||
|
||
decode(encodedString: string): RemoteConfig { | ||
const decodedString = Buffer.from(encodedString, 'base64').toString('utf-8'); | ||
const decryptedString = this.encryptionService.decrypt(decodedString); | ||
return RemoteConfigSchema.parse(JSON.parse(decryptedString)); | ||
} | ||
} |