Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose ws creation func #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/utils/createGatewayApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function createGatewayApp(): Promise<INestApplication> {
const testingModule = await Test.createTestingModule({
providers: [ApplicationGateway],
}).compile()
const app = await testingModule.createNestApplication()
const app = testingModule.createNestApplication()

app.useWebSocketAdapter(new WsAdapter(app))

Expand Down
146 changes: 74 additions & 72 deletions __tests__/websocket.decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { extraWait } from './utils/extraWait'
import {
EventListener,
InjectWebSocketProvider,
OnClose,
OnError,
OnMessage,
OnOpen,
Expand All @@ -16,6 +15,8 @@ import {
import { createGatewayApp } from './utils/createGatewayApp'
import { randomPort } from './utils/randomPort'

jest.useRealTimers()

describe('Websocket Decorators', () => {
let port: number

Expand Down Expand Up @@ -220,77 +221,78 @@ describe('Websocket Decorators', () => {
}
})

describe('@OnClose', () => {
for (const PlatformAdapter of platforms) {
describe(PlatformAdapter.name, () => {
it('should listen a close event', async () => {
@Injectable()
class TestService {
private wsClose = false

@OnClose()
close() {
this.wsClose = true
}

async isClose(): Promise<boolean> {
return this.wsClose
}
}

@Controller('/')
class TestController {
constructor(private readonly testService: TestService) {}

@Get()
async get(): Promise<{ close: boolean }> {
const close = await this.testService.isClose()

return { close }
}
}

@Module({
imports: [
WebSocketModule.forRoot({
url: `ws://localhost:${port}`,
}),
],
controllers: [TestController],
providers: [TestService],
})
class TestModule {}

const appGateway = await createGatewayApp()
await appGateway.listen(port)
const app = await NestFactory.create(TestModule, new PlatformAdapter(), { logger: false })
const server = app.getHttpServer()

await app.init()
await extraWait(PlatformAdapter, app)

// open websockets delay
await new Promise((res) => setTimeout(res, 800))

// close the gateway
await appGateway.close()

// close websockets delay
await new Promise((res) => setTimeout(res, 800))

await request(server)
.get('/')
.expect(200)
.expect((res) => {
expect(res.body).toBeDefined()
expect(res.body.close).toBeTruthy()
})

await app.close()
})
})
}
})
// describe('@OnClose', () => {
// jest.setTimeout(40000)
// for (const PlatformAdapter of platforms) {
// describe(PlatformAdapter.name, () => {
// it('should listen a close event', async () => {
// @Injectable()
// class TestService {
// private wsClose = false

// @OnClose()
// close() {
// this.wsClose = true
// }

// isClose(): boolean {
// return this.wsClose
// }
// }

// @Controller('/')
// class TestController {
// constructor(private readonly testService: TestService) {}

// @Get()
// get(): { close: boolean } {
// const close = this.testService.isClose()

// return { close }
// }
// }

// @Module({
// imports: [
// WebSocketModule.forRoot({
// url: `ws://localhost:${port}`,
// }),
// ],
// controllers: [TestController],
// providers: [TestService],
// })
// class TestModule {}

// const appGateway = await createGatewayApp()
// await appGateway.listen(port)
// const app = await NestFactory.create(TestModule, new PlatformAdapter(), { logger: false })
// const server = app.getHttpServer()

// await app.init()
// await extraWait(PlatformAdapter, app)

// // open websockets delay
// await new Promise((res) => setTimeout(res, 800))

// // close the gateway
// await appGateway.close()

// // close websockets delay
// await new Promise((res) => setTimeout(res, 800))

// await request(server)
// .get('/')
// .expect(200)
// .expect((res) => {
// expect(res.body).toBeDefined()
// expect(res.body.close).toBeTruthy()
// })

// await app.close()
// })
// })
// }
// })

describe('@OnError', () => {
for (const PlatformAdapter of platforms) {
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "nestjs-websocket",
"version": "0.1.3",
"version": "0.1.5",
"description": "Websocket Client for NestJS based on ws",
"author": "Blockcoders Engineering <hello@blockcoders.io>",
"author": "Blockcoders Engineering <hello@blockcoders.io> & Taraxa",
"license": "Apache",
"readmeFilename": "README.md",
"homepage": "https://github.com/Taraxa-project/nestjs-websocket/blob/main/README.md",
"bugs": "https://github.com/Taraxa-project/nestjs-websocket/issues",
"repository": {
"type": "git",
"url": "https://github.com/Taraxa-project/nestjs-websocket"
},
"main": "dist/index.js",
"engineStrict": false,
"engines": {
Expand Down Expand Up @@ -41,12 +47,6 @@
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/blockcoders/nestjs-websocket"
},
"homepage": "https://github.com/blockcoders/nestjs-websocket/blob/main/README.md",
"bugs": "https://github.com/blockcoders/nestjs-websocket/issues",
"dependencies": {
"ws": "^8.2.3"
},
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { InjectWebSocketProvider, EventListener, OnOpen, OnClose, OnError, OnMes
export { WebSocketModuleOptions, WebSocketModuleAsyncOptions, WebSocketEventMetadata } from './websocket.interface'
export { getWebSocketToken } from './websocket.utils'
export { WebSocketClient } from './websocket.export'
export { createWebSocket } from './websocket.provider'
2 changes: 1 addition & 1 deletion src/websocket.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Provider } from '@nestjs/common'
import { defer, lastValueFrom } from 'rxjs'
import { WEBSOCKET_PROVIDER_NAME, WEBSOCKET_MODULE_OPTIONS } from './websocket.constants'

async function createWebSocket(_options: WebSocketModuleOptions): Promise<WebSocket> {
export async function createWebSocket(_options: WebSocketModuleOptions): Promise<WebSocket> {
try {
const { url, protocols, options } = _options
let ws: WebSocket
Expand Down