Skip to content

Commit ec3ba90

Browse files
committed
feat: add factory for easier testing
1 parent 1513e2a commit ec3ba90

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

factories/encryption_manager.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

factories/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* @boringnode/encryption
3+
*
4+
* @license MIT
5+
* @copyright Boring Node
6+
*/
7+
8+
export { EncryptionManagerFactory } from './encryption_manager.ts'

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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": {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
})

0 commit comments

Comments
 (0)