Skip to content

Commit

Permalink
refactor: expose driversList as a standard import
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 7, 2023
1 parent 111d5f8 commit bc8aae1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
10 changes: 2 additions & 8 deletions modules/hash/drivers_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* regular usage and specific to AdonisJS container flow.
*/

import { Argon, Bcrypt, Scrypt } from './main.js'
import { RuntimeException } from '@poppinss/utils'
import type { HashDriversList } from '../../src/types.js'

Expand All @@ -27,11 +26,7 @@ class HashDriversCollection {
/**
* List of registered drivers
*/
list: Partial<HashDriversList> = {
bcrypt: (config) => new Bcrypt(config),
argon2: (config) => new Argon(config),
scrypt: (config) => new Scrypt(config),
}
list: Partial<HashDriversList> = {}

/**
* Extend drivers collection and add a custom
Expand Down Expand Up @@ -63,5 +58,4 @@ class HashDriversCollection {
}
}

const hashDriversCollection = new HashDriversCollection()
export default hashDriversCollection
export default new HashDriversCollection()
1 change: 1 addition & 0 deletions modules/hash/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

export * from '@adonisjs/hash'
export { defineConfig } from './define_config.js'
export { default as driversList } from './drivers_collection.js'
12 changes: 6 additions & 6 deletions providers/hash_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
* file that was distributed with this source code.
*/

import { Hash } from '../modules/hash/main.js'
import type { ApplicationService } from '../src/types.js'
import hashDriversCollection from '../modules/hash/drivers_collection.js'
import { Argon, Bcrypt, Hash, Scrypt, driversList } from '../modules/hash/main.js'

/**
* Registers the passwords hasher with the container
Expand All @@ -18,11 +17,12 @@ export default class HashServiceProvider {
constructor(protected app: ApplicationService) {}

/**
* Registers the hash drivers collection to the container. The
* collection can be extended to add custom drivers.
* Registering bundled drivers with the driversList collection
*/
protected registerHashDrivers() {
this.app.container.singleton('hashDrivers', () => hashDriversCollection)
driversList.extend('bcrypt', (config) => new Bcrypt(config))
driversList.extend('scrypt', (config) => new Scrypt(config))
driversList.extend('argon2', (config) => new Argon(config))
}

/**
Expand Down Expand Up @@ -51,8 +51,8 @@ export default class HashServiceProvider {
* Registers bindings
*/
register() {
this.registerHashManager()
this.registerHashDrivers()
this.registerHashManager()
this.registerHash()
}
}
2 changes: 1 addition & 1 deletion src/test_utils/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import Macroable from '@poppinss/macroable'
import { IncomingMessage, ServerResponse } from 'node:http'

import { HttpServerUtils } from './http.js'
import type { ApplicationService } from '../types.js'
import { CookieClient } from '../../modules/http.js'
import type { ApplicationService } from '../types.js'

/**
* Test utils has a collection of helper methods to make testing
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type { LoggerManager } from '../modules/logger.js'
import type { Encryption } from '../modules/encryption.js'
import type { HttpRequestFinishedPayload } from '../types/http.js'
import type { LoggerConfig, LoggerManagerConfig } from '../types/logger.js'
import type hashDriversCollection from '../modules/hash/drivers_collection.js'
import type { Argon, Bcrypt, HashManager, Scrypt } from '../modules/hash/main.js'
import type {
ArgonConfig,
Expand Down Expand Up @@ -129,7 +128,6 @@ export interface ContainerBindings {
emitter: EmitterService
encryption: EncryptionService
hash: HashService
hashDrivers: typeof hashDriversCollection
server: HttpServerService
router: HttpRouterService
testUtils: TestUtils
Expand Down
27 changes: 17 additions & 10 deletions tests/hash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
import { test } from '@japa/runner'
import { HashDriverContract } from '../types/hash.js'
import { IgnitorFactory } from '../factories/core/ignitor.js'
import { Argon, Bcrypt, defineConfig, Hash, HashManager, Scrypt } from '../modules/hash/main.js'
import {
Hash,
Argon,
Bcrypt,
Scrypt,
HashManager,
driversList,
defineConfig,
} from '../modules/hash/main.js'

const BASE_URL = new URL('./tmp/', import.meta.url)

Expand All @@ -27,6 +35,8 @@ test.group('Hash', () => {
},
})

driversList.extend('scrypt', (scryptConfig) => new Scrypt(scryptConfig))

const hash = new HashManager(config)
assert.instanceOf(hash.use('scrypt'), Hash)
expectTypeOf(hash.use).parameters.toMatchTypeOf<['scrypt'?]>()
Expand Down Expand Up @@ -65,11 +75,10 @@ test.group('Hash', () => {
const app = ignitor.createApp('web')
await app.init()
await app.boot()
const hashDrivers = await app.container.make('hashDrivers')

assert.instanceOf(hashDrivers.create('bcrypt', {}), Bcrypt)
assert.instanceOf(hashDrivers.create('scrypt', {}), Scrypt)
assert.instanceOf(hashDrivers.create('argon2', {}), Argon)
assert.instanceOf(driversList.create('bcrypt', {}), Bcrypt)
assert.instanceOf(driversList.create('scrypt', {}), Scrypt)
assert.instanceOf(driversList.create('argon2', {}), Argon)
})

test('raise error when trying to create unknown hash driver', async ({ assert }) => {
Expand All @@ -89,10 +98,9 @@ test.group('Hash', () => {
await app.init()
await app.boot()

const hashDrivers = await app.container.make('hashDrivers')
assert.throws(
// @ts-expect-error
() => hashDrivers.create('foo', {}),
() => driversList.create('foo', {}),
'Unknown hash driver "foo". Make sure the driver is registered'
)
})
Expand Down Expand Up @@ -132,12 +140,11 @@ test.group('Hash', () => {
}
}

const hashDrivers = await app.container.make('hashDrivers')
// @ts-expect-error
hashDrivers.extend('fake', () => new FakeHash())
driversList.extend('fake', () => new FakeHash())

// @ts-expect-error
const fakeDriver = hashDrivers.create('fake')
const fakeDriver = driversList.create('fake')
assert.instanceOf(fakeDriver, FakeHash)
})
})

0 comments on commit bc8aae1

Please sign in to comment.