Skip to content

Commit 0b33ee3

Browse files
committed
refactor: use correct ioc container global symbols
1 parent bf6883b commit 0b33ee3

File tree

9 files changed

+36
-55
lines changed

9 files changed

+36
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"dependencies": {
7878
"@poppinss/request": "^1.0.9",
7979
"@poppinss/response": "^1.0.8",
80-
"@poppinss/utils": "^1.0.2",
80+
"@poppinss/utils": "^1.0.3",
8181
"co-compose": "^5.1.1",
8282
"haye": "^2.0.2",
8383
"lodash": "^4.17.11",

src/Server/MiddlewareStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
ResolvedMiddlewareNode,
2222
} from '../contracts'
2323

24-
import { exceptionCodes } from '../helpers'
24+
import { exceptionCodes, iocMethods } from '../helpers'
2525

2626
/**
2727
* Middleware store register and keep all the application middleware at one
@@ -63,7 +63,7 @@ export class MiddlewareStore<Context extends any> implements MiddlewareStoreCont
6363
private _resolveMiddlewareItem (middleware: MiddlewareNode<Context>): ResolvedMiddlewareNode<Context> {
6464
return typeof(middleware) === 'string' ? {
6565
type: 'class',
66-
value: global['use'](middleware),
66+
value: global[iocMethods.use](middleware),
6767
args: [],
6868
} : {
6969
type: 'function',

src/Server/finalMiddlewareHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import { ResolvedMiddlewareNode } from '../contracts'
15+
import { iocMethods } from '../helpers'
1516

1617
/**
1718
* Final middleware handler executes a middleware
@@ -27,5 +28,5 @@ export function finalMiddlewareHandler<Context> (
2728
return middleware.value(params[0], params[1], middleware.args)
2829
}
2930

30-
return global['make'](middleware.value).handle(params[0], params[1], middleware.args)
31+
return global[iocMethods.make](middleware.value).handle(params[0], params[1], middleware.args)
3132
}

src/Server/finalRouteHandler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import { ResolvedControllerNode, HttpContextContract } from '../contracts'
1515
import { useReturnValue } from './useReturnValue'
16+
import { iocMethods } from '../helpers'
1617

1718
/**
1819
* Final handler executes the route handler based on it's resolved
@@ -38,8 +39,8 @@ export async function finalRouteHandler<Context extends HttpContextContract> (ct
3839
* Otherwise lookup the controller inside the IoC container
3940
* and make the response
4041
*/
41-
const controllerInstance = global['make'](handler.namespace)
42-
const returnValue = await global['iocCall'](controllerInstance, handler.method, [ctx])
42+
const controllerInstance = global[iocMethods.make](handler.namespace)
43+
const returnValue = await global[iocMethods.call](controllerInstance, handler.method, [ctx])
4344

4445
if (useReturnValue(returnValue, ctx)) {
4546
ctx.response.send(returnValue)

src/Server/routePreProcessor.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import { Middleware } from 'co-compose'
15-
import { Exception } from '@poppinss/utils'
15+
import { parseIocReference } from '@poppinss/utils'
1616
import {
1717
RouteNode,
1818
HttpContextContract,
@@ -21,7 +21,6 @@ import {
2121

2222
import { finalMiddlewareHandler } from './finalMiddlewareHandler'
2323
import { finalRouteHandler } from './finalRouteHandler'
24-
import { exceptionCodes } from '../helpers'
2524

2625
/**
2726
* Executes the route middleware. This method is only invoked when route
@@ -54,30 +53,6 @@ export function routePreProcessor<Context> (
5453
* Resolve route handler before hand to keep HTTP layer performant
5554
*/
5655
if (typeof (route.handler) === 'string') {
57-
let handler = route.handler
58-
59-
/**
60-
* 1. Do not prepend namespace, if `namespace` starts with `/`.
61-
* 2. Else if `namespace` exists, then prepend the namespace
62-
*/
63-
if (route.handler.startsWith('/')) {
64-
handler = route.handler.substr(1)
65-
} else if (route.meta.namespace) {
66-
handler = `${route.meta.namespace.replace(/\/$/, '')}/${route.handler}`
67-
}
68-
69-
/**
70-
* Split the controller and method. Raise error if `method` is missing
71-
*/
72-
const [ namespace, method ] = handler.split('.')
73-
if (!method) {
74-
throw new Exception(
75-
`Missing controller method on \`${route.pattern}\` route`,
76-
500,
77-
exceptionCodes.E_INVALID_ROUTE_NAMESPACE,
78-
)
79-
}
80-
8156
/**
8257
* Unlike middleware, we do not prefetch controller from the IoC container
8358
* since controllers in an app can grow to a huge number and lazy loading
@@ -86,11 +61,7 @@ export function routePreProcessor<Context> (
8661
* Sometime later, we can introduce `hot cache` in IoC container, which
8762
* avoids lookup cost within the IoC container.
8863
*/
89-
route.meta.resolvedHandler = {
90-
type: 'iocReference',
91-
namespace: namespace,
92-
method,
93-
}
64+
route.meta.resolvedHandler = parseIocReference(route.handler, route.meta.namespace)
9465
} else {
9566
route.meta.resolvedHandler = {
9667
type: 'function',

src/helpers.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,14 @@ export const exceptionCodes = {
139139
E_DUPLICATE_ROUTE_NAME: 'E_DUPLICATE_ROUTE_NAME',
140140
E_MISSING_ROUTE_PARAM_VALUE: 'E_MISSING_ROUTE_PARAM_VALUE',
141141
E_ROUTE_NOT_FOUND: 'E_ROUTE_NOT_FOUND',
142-
E_INVALID_MIDDLEWARE_TYPE: 'E_INVALID_MIDDLEWARE_TYPE',
143142
E_MISSING_NAMED_MIDDLEWARE: 'E_MISSING_NAMED_MIDDLEWARE',
144-
E_INVALID_ROUTE_NAMESPACE: 'E_INVALID_ROUTE_NAMESPACE',
143+
}
144+
145+
/**
146+
* Symbols to use IoC container global methods.
147+
*/
148+
export const iocMethods = {
149+
use: Symbol.for('ioc.use'),
150+
make: Symbol.for('ioc.make'),
151+
call: Symbol.for('ioc.call'),
145152
}

test/middleware-store.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ test.group('Middleware', () => {
7676

7777
const ioc = new Ioc()
7878
ioc.bind('App/Middleware', () => new Middleware())
79-
global['use'] = ioc.use.bind(ioc)
80-
global['make'] = ioc.make.bind(ioc)
79+
global[Symbol.for('ioc.use')] = ioc.use.bind(ioc)
80+
global[Symbol.for('ioc.make')] = ioc.make.bind(ioc)
8181

8282
const middleware = new MiddlewareStore()
8383
middleware.register(['App/Middleware'])
8484
await finalMiddlewareHandler(middleware.get()[0], [{}, async () => {}])
8585

8686
assert.deepEqual(stack, ['middleware class'])
8787

88-
delete global['use']
89-
delete global['make']
88+
delete global[Symbol.for('ioc.use')]
89+
delete global[Symbol.for('ioc.make')]
9090
})
9191

9292
test('process route middleware using the store', async (assert) => {

test/route-pre-processor.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { routePreProcessor } from '../src/Server/routePreProcessor'
1616

1717
test.group('Route pre processor', (group) => {
1818
group.afterEach(() => {
19-
delete global['use']
20-
delete global['make']
19+
delete global[Symbol.for('ioc.use')]
20+
delete global[Symbol.for('ioc.make')]
2121
})
2222

2323
test('process route by resolving function based middleware', (assert) => {
@@ -63,7 +63,7 @@ test.group('Route pre processor', (group) => {
6363

6464
const ioc = new Ioc()
6565
ioc.bind('App/Middleware/Auth', () => Auth)
66-
global['use'] = ioc.use.bind(ioc)
66+
global[Symbol.for('ioc.use')] = ioc.use.bind(ioc)
6767

6868
middlewareStore.registerNamed({ auth: 'App/Middleware/Auth' })
6969

@@ -102,7 +102,7 @@ test.group('Route pre processor', (group) => {
102102

103103
const ioc = new Ioc()
104104
ioc.bind('App/Controllers/Http/UserController', () => UserController)
105-
global['use'] = ioc.use.bind(ioc)
105+
global[Symbol.for('ioc.use')] = ioc.use.bind(ioc)
106106

107107
const route = router.get('/', 'UserController.store').toJSON()
108108
routePreProcessor(route, middlewareStore)
@@ -124,7 +124,7 @@ test.group('Route pre processor', (group) => {
124124

125125
const ioc = new Ioc()
126126
ioc.bind('UserController', () => UserController)
127-
global['use'] = ioc.use.bind(ioc)
127+
global[Symbol.for('ioc.use')] = ioc.use.bind(ioc)
128128

129129
const route = router.get('/', '/UserController.store').toJSON()
130130
routePreProcessor(route, middlewareStore)
@@ -146,12 +146,12 @@ test.group('Route pre processor', (group) => {
146146

147147
const ioc = new Ioc()
148148
ioc.bind('UserController', () => UserController)
149-
global['use'] = ioc.use.bind(ioc)
149+
global[Symbol.for('ioc.use')] = ioc.use.bind(ioc)
150150

151151
const route = router.get('/', '/UserController').toJSON()
152152
const fn = () => routePreProcessor(route, middlewareStore)
153153

154-
assert.throw(fn, 'Missing controller method on `/` route')
154+
assert.throw(fn, 'E_INVALID_IOC_NAMESPACE: Missing method reference on {/UserController} namespace')
155155
})
156156

157157
test('raise error when controller method is missing', (_assert) => {
@@ -163,7 +163,7 @@ test.group('Route pre processor', (group) => {
163163

164164
const ioc = new Ioc()
165165
ioc.bind('App/Controllers/Http/UserController', () => UserController)
166-
global['use'] = ioc.use.bind(ioc)
166+
global[Symbol.for('ioc.use')] = ioc.use.bind(ioc)
167167

168168
const route = router.get('/', '/UserController.store').toJSON()
169169
routePreProcessor(route, middlewareStore)

test/server.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,9 @@ test.group('Server | error handler', () => {
739739

740740
test.group('Server | all', (group) => {
741741
group.afterEach(() => {
742-
delete global['use']
743-
delete global['make']
742+
delete global[Symbol.for('ioc.use')]
743+
delete global[Symbol.for('ioc.call')]
744+
delete global[Symbol.for('ioc.make')]
744745
})
745746

746747
test('raise 404 when route is missing', async (assert) => {
@@ -769,8 +770,8 @@ test.group('Server | all', (group) => {
769770

770771
const ioc = new Ioc()
771772
ioc.bind('App/Controllers/Http/HomeController', () => new HomeController())
772-
global['make'] = ioc.make.bind(ioc)
773-
global['iocCall'] = ioc.call.bind(ioc)
773+
global[Symbol.for('ioc.make')] = ioc.make.bind(ioc)
774+
global[Symbol.for('ioc.call')] = ioc.call.bind(ioc)
774775

775776
router.get('/', 'HomeController.index')
776777

0 commit comments

Comments
 (0)