Skip to content

Commit 9f04004

Browse files
committed
refactor: use @boringnode/transmit and change how we define routes
1 parent 6d026a5 commit 9f04004

24 files changed

+557
-1426
lines changed

index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
*/
99

1010
export { configure } from './configure.js'
11-
export { Transmit } from './src/transmit.js'
1211
export { defineConfig } from './src/define_config.js'
1312
export { stubsRoot } from './stubs/main.js'

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
".": "./build/index.js",
1919
"./transports": "./build/transports.js",
2020
"./transmit_provider": "./build/providers/transmit_provider.js",
21+
"./controllers/*": "./build/controllers/*.js",
2122
"./services/main": "./build/services/transmit.js",
2223
"./types": "./build/src/types/main.js"
2324
},
@@ -39,17 +40,15 @@
3940
"version": "npm run build"
4041
},
4142
"dependencies": {
42-
"@boringnode/bus": "^0.5.0",
43-
"@poppinss/matchit": "^3.1.2",
44-
"@poppinss/utils": "^6.7.2",
45-
"emittery": "^1.0.3"
43+
"@boringnode/transmit": "^0.1.1"
4644
},
4745
"devDependencies": {
4846
"@adonisjs/assembler": "^7.2.3",
4947
"@adonisjs/core": "^6.3.1",
5048
"@adonisjs/eslint-config": "^1.3.0",
5149
"@adonisjs/prettier-config": "^1.3.0",
5250
"@adonisjs/tsconfig": "^1.3.0",
51+
"@boringnode/bus": "^0.6.0",
5352
"@japa/assert": "2.1.0",
5453
"@japa/runner": "3.1.1",
5554
"@swc/core": "1.4.11",

providers/transmit_provider.ts

+4-45
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
*/
99

1010
import '../src/types/extended.js'
11-
import { Transmit } from '../src/transmit.js'
11+
import { TransmitAdonisAdapter } from '../src/transmit.js'
1212
import type { ApplicationService } from '@adonisjs/core/types'
13-
import type { TransmitConfig } from '../src/types/main.js'
1413
import type { Transport } from '@boringnode/bus/types/main'
14+
import type { TransmitConfig } from '@boringnode/transmit/types'
1515

1616
export default class TransmitProvider {
1717
constructor(protected app: ApplicationService) {}
1818

1919
register() {
2020
this.app.container.singleton('transmit', async () => {
21+
const router = await this.app.container.make('router')
2122
const config = this.app.config.get<TransmitConfig>('transmit', {})
2223

2324
let transport: Transport | null = null
@@ -26,52 +27,10 @@ export default class TransmitProvider {
2627
transport = config.transport.driver()
2728
}
2829

29-
return new Transmit(config, transport)
30+
return new TransmitAdonisAdapter(config, router, transport)
3031
})
3132
}
3233

33-
async boot() {
34-
const router = await this.app.container.make('router')
35-
const transmit = await this.app.container.make('transmit')
36-
const config = this.app.config.get<TransmitConfig>('transmit', {})
37-
38-
const registerRoute = router.get('__transmit/events', (ctx) => {
39-
transmit.$createStream(ctx)
40-
})
41-
42-
const subscribeRoute = router.post('__transmit/subscribe', async (ctx) => {
43-
const uid = ctx.request.input('uid')
44-
const channel = ctx.request.input('channel')
45-
46-
const success = await transmit.$subscribeToChannel(uid, channel, ctx)
47-
48-
if (!success) {
49-
return ctx.response.badRequest()
50-
}
51-
52-
return ctx.response.noContent()
53-
})
54-
55-
const unsubscribeRoute = router.post('__transmit/unsubscribe', (ctx) => {
56-
const uid = ctx.request.input('uid')
57-
const channel = ctx.request.input('channel')
58-
59-
const success = transmit.$unsubscribeFromChannel(uid, channel, ctx)
60-
61-
if (!success) {
62-
return ctx.response.badRequest()
63-
}
64-
65-
return ctx.response.noContent()
66-
})
67-
68-
if (config.routeHandlerModifier) {
69-
config.routeHandlerModifier(registerRoute)
70-
config.routeHandlerModifier(subscribeRoute)
71-
config.routeHandlerModifier(unsubscribeRoute)
72-
}
73-
}
74-
7534
async shutdown() {
7635
const transmit = await this.app.container.make('transmit')
7736

services/transmit.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
*/
99

1010
import app from '@adonisjs/core/services/app'
11-
import { Transmit } from '../src/transmit.js'
11+
import { TransmitAdonisAdapter } from '../src/transmit.js'
1212

13-
let transmit: Transmit
13+
let transmit: TransmitAdonisAdapter
1414

1515
await app.booted(async () => {
1616
transmit = await app.container.make('transmit')
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @adonisjs/transmit
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { RuntimeException } from '@poppinss/utils'
11+
import transmit from '../../services/transmit.js'
12+
import type { HttpContext } from '@adonisjs/core/http'
13+
14+
export default class EventStreamController {
15+
handle(ctx: HttpContext) {
16+
const uid = ctx.request.input('uid')
17+
18+
if (!uid) {
19+
throw new RuntimeException('Missing required field "uid" in the request body')
20+
}
21+
22+
const stream = transmit.createStream({
23+
uid,
24+
context: ctx,
25+
request: ctx.request.request,
26+
response: ctx.response.response,
27+
})
28+
29+
return ctx.response.stream(stream)
30+
}
31+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @adonisjs/transmit
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import transmit from '../../services/transmit.js'
11+
import type { HttpContext } from '@adonisjs/core/http'
12+
13+
export default class SubscribeController {
14+
async handle(ctx: HttpContext) {
15+
const uid = ctx.request.input('uid')
16+
const channel = ctx.request.input('channel')
17+
18+
const success = await transmit.subscribe({
19+
uid,
20+
channel,
21+
context: ctx,
22+
})
23+
24+
if (!success) {
25+
return ctx.response.badRequest()
26+
}
27+
28+
return ctx.response.noContent()
29+
}
30+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @adonisjs/transmit
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import transmit from '../../services/transmit.js'
11+
import type { HttpContext } from '@adonisjs/core/http'
12+
13+
export default class UnsubscribeController {
14+
async handle(ctx: HttpContext) {
15+
const uid = ctx.request.input('uid')
16+
const channel = ctx.request.input('channel')
17+
18+
const success = await transmit.unsubscribe({
19+
uid,
20+
channel,
21+
context: ctx,
22+
})
23+
24+
if (!success) {
25+
return ctx.response.badRequest()
26+
}
27+
28+
return ctx.response.noContent
29+
}
30+
}

src/define_config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import type { TransmitConfig } from './types/main.js'
10+
import { TransmitConfig } from '@boringnode/transmit/types'
1111

1212
export function defineConfig<T extends TransmitConfig>(config: T): T {
1313
if (config.transport && typeof config.transport.channel === 'undefined') {

src/secure_channel_store.ts

-30
This file was deleted.

src/stream.ts

-106
This file was deleted.

0 commit comments

Comments
 (0)