Skip to content

Commit

Permalink
refactor: use usingEdgeJS flag to detect is edge is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 27, 2023
1 parent 9d72af8 commit 0e9e8e0
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions providers/shield_provider.ts
Expand Up @@ -7,10 +7,8 @@
* file that was distributed with this source code.
*/

import { Edge } from 'edge.js'
import type { ApplicationService } from '@adonisjs/core/types'

import debug from '../src/debug.js'
import type { ShieldConfig } from '../src/types.js'
import ShieldMiddleware from '../src/shield_middleware.js'

Expand All @@ -19,16 +17,6 @@ import ShieldMiddleware from '../src/shield_middleware.js'
*/
export default class ShieldProvider {
constructor(protected app: ApplicationService) {}
/**
* Returns edge when it's installed
*/
protected async getEdge(): Promise<Edge | undefined> {
try {
const { default: edge } = await import('edge.js')
debug('Detected edge.js package. Adding shield primitives to it')
return edge
} catch {}
}

/**
* Register ShieldMiddleware to the container
Expand All @@ -37,9 +25,13 @@ export default class ShieldProvider {
this.app.container.bind(ShieldMiddleware, async () => {
const config = this.app.config.get<ShieldConfig>('shield', {})
const encryption = await this.app.container.make('encryption')
const edge = await this.getEdge()

return new ShieldMiddleware(config, encryption, edge)
if (this.app.usingEdgeJS) {
const edge = await import('edge.js')
return new ShieldMiddleware(config, encryption, edge.default)
}

return new ShieldMiddleware(config, encryption)
})
}
}

0 comments on commit 0e9e8e0

Please sign in to comment.