File tree Expand file tree Collapse file tree 4 files changed +71
-0
lines changed
Expand file tree Collapse file tree 4 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @boringnode /encryption
3+ *
4+ * @license MIT
5+ * @copyright Boring Node
6+ */
7+
8+ import { EncryptionManager } from '../src/encryption_manager.ts'
9+ import { ChaCha20Poly1305 } from '../src/drivers/chacha20_poly1305.ts'
10+ import type { ManagerDriverFactory } from '../src/types/main.ts'
11+
12+ type Config < KnownEncryptionDriver extends Record < string , ManagerDriverFactory > > = {
13+ default ?: keyof KnownEncryptionDriver
14+ list : KnownEncryptionDriver
15+ }
16+
17+ export class EncryptionManagerFactory <
18+ KnownEncryptionDriver extends Record < string , ManagerDriverFactory > = {
19+ nova : ( ) => ChaCha20Poly1305
20+ } ,
21+ > {
22+ readonly #config: Config < KnownEncryptionDriver >
23+
24+ constructor ( config ?: { default ?: keyof KnownEncryptionDriver ; list : KnownEncryptionDriver } ) {
25+ this . #config =
26+ config ||
27+ ( {
28+ default : 'nova' ,
29+ list : {
30+ nova : ( ) =>
31+ new ChaCha20Poly1305 ( {
32+ id : 'nova' ,
33+ keys : [ 'averylongradom32charactersstring' ] ,
34+ } ) ,
35+ } ,
36+ } as unknown as Config < KnownEncryptionDriver > )
37+ }
38+
39+ create ( ) {
40+ return new EncryptionManager ( this . #config)
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * @boringnode /encryption
3+ *
4+ * @license MIT
5+ * @copyright Boring Node
6+ */
7+
8+ export { EncryptionManagerFactory } from './encryption_manager.ts'
Original file line number Diff line number Diff line change 1313 "exports" : {
1414 "." : " ./build/index.js" ,
1515 "./drivers/*" : " ./build/src/drivers/*.js" ,
16+ "./factories" : " ./build/factories/main.js" ,
1617 "./types" : " ./build/src/types/main.js"
1718 },
1819 "scripts" : {
Original file line number Diff line number Diff line change 1+ import { test } from '@japa/runner'
2+ import { EncryptionManagerFactory } from '../factories/encryption_manager.ts'
3+ import { EncryptionManager } from '../src/encryption_manager.ts'
4+ import { ChaCha20Poly1305 } from '../src/drivers/chacha20_poly1305.ts'
5+
6+ test . group ( 'Encryption manager factory' , ( ) => {
7+ test ( 'create instance of EncryptionManager using factory' , async ( { assert } ) => {
8+ const encryption = new EncryptionManagerFactory ( ) . create ( )
9+
10+ assert . instanceOf ( encryption , EncryptionManager )
11+
12+ const encryptedValue = encryption . use ( ) . encrypt ( 'secret' )
13+ const driverInstance = new ChaCha20Poly1305 ( {
14+ id : 'nova' ,
15+ keys : [ 'averylongradom32charactersstring' ] ,
16+ } )
17+
18+ assert . equal ( driverInstance . decrypt ( encryptedValue ) , 'secret' )
19+ } )
20+ } )
You can’t perform that action at this time.
0 commit comments