-
Notifications
You must be signed in to change notification settings - Fork 46
Feat/websocket controller #462
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4e2f6a7
feat(controller): add websocket controller support
akitaSummer c003e95
feat(controller): add websocket fetch controller
akitaSummer ab2eb71
feat(standalone): support websocket controllers
akitaSummer 0f644cd
feat(controller): serialize websocket fetch streams
akitaSummer d9182e5
fix(controller): resolve websocket lint errors
akitaSummer 52be2fd
fix(controller): address websocket review feedback
akitaSummer 3ae5b85
test(controller): fix websocket ci compatibility
akitaSummer 2782dd7
test(controller): guard mcp imports on node 16
akitaSummer 348592e
fix(controller): address websocket review feedback
akitaSummer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
core/controller-decorator/src/decorator/websocket-fetch/WebSocketFetchController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { PrototypeUtil, SingletonProto } from '@eggjs/core-decorator'; | ||
| import { StackUtil } from '@eggjs/tegg-common-util'; | ||
| import type { EggProtoImplClass, WebSocketFetchControllerParams } from '@eggjs/tegg-types'; | ||
| import { AccessLevel, ControllerType } from '@eggjs/tegg-types'; | ||
| import ControllerInfoUtil from '../../util/ControllerInfoUtil'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
|
|
||
| export function WebSocketFetchController(param?: WebSocketFetchControllerParams) { | ||
| return function(constructor: EggProtoImplClass) { | ||
| ControllerInfoUtil.setControllerType(constructor, ControllerType.WEBSOCKET_FETCH); | ||
| if (param?.controllerName) { | ||
| ControllerInfoUtil.setControllerName(constructor, param.controllerName); | ||
| } | ||
| if (param?.path) { | ||
| WebSocketInfoUtil.setWebSocketPath(param.path, constructor); | ||
| } | ||
| if (param?.timeout !== undefined) { | ||
| ControllerInfoUtil.setControllerTimeout(param.timeout, constructor); | ||
| } | ||
|
|
||
| const func = SingletonProto({ | ||
| accessLevel: AccessLevel.PUBLIC, | ||
| name: param?.protoName, | ||
| }); | ||
| func(constructor); | ||
|
|
||
| PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5)); | ||
| }; | ||
| } |
62 changes: 62 additions & 0 deletions
62
core/controller-decorator/src/decorator/websocket-fetch/WebSocketFetchMethod.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import assert from 'node:assert'; | ||
| import { ControllerType, WebSocketFetchMethodType } from '@eggjs/tegg-types'; | ||
| import type { EggProtoImplClass, WebSocketFetchLifecycleMethodParams, WebSocketFetchMethodParams } from '@eggjs/tegg-types'; | ||
| import MethodInfoUtil from '../../util/MethodInfoUtil'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
|
|
||
| function setWebSocketFetchMethodType( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| methodType: WebSocketFetchMethodType, | ||
| timeout?: number, | ||
| ) { | ||
| assert(typeof propertyKey === 'string', | ||
| `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`); | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MethodInfoUtil.setMethodControllerType(controllerClazz, methodName, ControllerType.WEBSOCKET_FETCH); | ||
| WebSocketInfoUtil.setWebSocketFetchMethodType(methodType, controllerClazz, methodName); | ||
| if (timeout !== undefined) { | ||
| MethodInfoUtil.setMethodTimeout(timeout, controllerClazz, methodName); | ||
| } | ||
| return { controllerClazz, methodName }; | ||
| } | ||
|
|
||
| export function WebSocketFetchMethod(param?: WebSocketFetchMethodParams) { | ||
| return function(target: any, propertyKey: PropertyKey) { | ||
| const { controllerClazz, methodName } = setWebSocketFetchMethodType( | ||
| target, | ||
| propertyKey, | ||
| WebSocketFetchMethodType.DATA, | ||
| param?.timeout, | ||
| ); | ||
| WebSocketInfoUtil.setWebSocketMethodPath('', controllerClazz, methodName); | ||
| if (param?.priority !== undefined) { | ||
| WebSocketInfoUtil.setWebSocketMethodPriority(param.priority, controllerClazz, methodName); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketFetchOnConnection(param?: WebSocketFetchLifecycleMethodParams) { | ||
| return function(target: any, propertyKey: PropertyKey) { | ||
| setWebSocketFetchMethodType(target, propertyKey, WebSocketFetchMethodType.CONNECTION, param?.timeout); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketFetchOnOpen(param?: WebSocketFetchLifecycleMethodParams) { | ||
| return function(target: any, propertyKey: PropertyKey) { | ||
| setWebSocketFetchMethodType(target, propertyKey, WebSocketFetchMethodType.OPEN, param?.timeout); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketFetchOnError(param?: WebSocketFetchLifecycleMethodParams) { | ||
| return function(target: any, propertyKey: PropertyKey) { | ||
| setWebSocketFetchMethodType(target, propertyKey, WebSocketFetchMethodType.ERROR, param?.timeout); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketFetchOnClose(param?: WebSocketFetchLifecycleMethodParams) { | ||
| return function(target: any, propertyKey: PropertyKey) { | ||
| setWebSocketFetchMethodType(target, propertyKey, WebSocketFetchMethodType.CLOSE, param?.timeout); | ||
| }; | ||
| } |
47 changes: 47 additions & 0 deletions
47
core/controller-decorator/src/decorator/websocket-fetch/WebSocketFetchParam.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import assert from 'node:assert'; | ||
| import { WebSocketParamType } from '@eggjs/tegg-types'; | ||
| import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
|
|
||
| function setWebSocketFetchParamType( | ||
| target: any, | ||
| propertyKey: PropertyKey, | ||
| parameterIndex: number, | ||
| paramType: WebSocketParamType, | ||
| ) { | ||
| assert(typeof propertyKey === 'string', | ||
| `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`); | ||
| const methodName = propertyKey as string; | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| WebSocketInfoUtil.setWebSocketMethodParamType(paramType, parameterIndex, controllerClazz, methodName); | ||
| } | ||
|
|
||
| export function WebSocketData() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| setWebSocketFetchParamType(target, propertyKey, parameterIndex, WebSocketParamType.DATA); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketClose() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| setWebSocketFetchParamType(target, propertyKey, parameterIndex, WebSocketParamType.CLOSE); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketError() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| setWebSocketFetchParamType(target, propertyKey, parameterIndex, WebSocketParamType.ERROR); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketCloseCode() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| setWebSocketFetchParamType(target, propertyKey, parameterIndex, WebSocketParamType.CLOSE_CODE); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketCloseReason() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| setWebSocketFetchParamType(target, propertyKey, parameterIndex, WebSocketParamType.CLOSE_REASON); | ||
| }; | ||
| } |
29 changes: 29 additions & 0 deletions
29
core/controller-decorator/src/decorator/websocket/WebSocketController.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { PrototypeUtil, SingletonProto } from '@eggjs/core-decorator'; | ||
| import { StackUtil } from '@eggjs/tegg-common-util'; | ||
| import type { EggProtoImplClass, WebSocketControllerParams } from '@eggjs/tegg-types'; | ||
| import { AccessLevel, ControllerType } from '@eggjs/tegg-types'; | ||
| import ControllerInfoUtil from '../../util/ControllerInfoUtil'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
|
|
||
| export function WebSocketController(param?: WebSocketControllerParams) { | ||
| return function(constructor: EggProtoImplClass) { | ||
| ControllerInfoUtil.setControllerType(constructor, ControllerType.WEBSOCKET); | ||
| if (param?.controllerName) { | ||
| ControllerInfoUtil.setControllerName(constructor, param.controllerName); | ||
| } | ||
| if (param?.path) { | ||
| WebSocketInfoUtil.setWebSocketPath(param.path, constructor); | ||
| } | ||
| if (param?.timeout !== undefined) { | ||
| ControllerInfoUtil.setControllerTimeout(param.timeout, constructor); | ||
| } | ||
|
|
||
| const func = SingletonProto({ | ||
| accessLevel: AccessLevel.PUBLIC, | ||
| name: param?.protoName, | ||
| }); | ||
| func(constructor); | ||
|
|
||
| PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5)); | ||
| }; | ||
| } |
22 changes: 22 additions & 0 deletions
22
core/controller-decorator/src/decorator/websocket/WebSocketMethod.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import assert from 'node:assert'; | ||
| import { ControllerType } from '@eggjs/tegg-types'; | ||
| import type { EggProtoImplClass, WebSocketMethodParams } from '@eggjs/tegg-types'; | ||
| import MethodInfoUtil from '../../util/MethodInfoUtil'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
|
|
||
| export function WebSocketMethod(param: WebSocketMethodParams) { | ||
| return function(target: any, propertyKey: PropertyKey) { | ||
| assert(typeof propertyKey === 'string', | ||
| `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`); | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| const methodName = propertyKey as string; | ||
| MethodInfoUtil.setMethodControllerType(controllerClazz, methodName, ControllerType.WEBSOCKET); | ||
| WebSocketInfoUtil.setWebSocketMethodPath(param.path, controllerClazz, methodName); | ||
| if (param.priority !== undefined) { | ||
| WebSocketInfoUtil.setWebSocketMethodPriority(param.priority, controllerClazz, methodName); | ||
| } | ||
| if (param.timeout !== undefined) { | ||
| MethodInfoUtil.setMethodTimeout(param.timeout, controllerClazz, methodName); | ||
| } | ||
| }; | ||
| } |
24 changes: 24 additions & 0 deletions
24
core/controller-decorator/src/decorator/websocket/WebSocketParam.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import assert from 'node:assert'; | ||
| import { WebSocketParamType } from '@eggjs/tegg-types'; | ||
| import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
|
|
||
| export function WebSocketSocket() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| assert(typeof propertyKey === 'string', | ||
| `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`); | ||
| const methodName = propertyKey as string; | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| WebSocketInfoUtil.setWebSocketMethodParamType(WebSocketParamType.SOCKET, parameterIndex, controllerClazz, methodName); | ||
| }; | ||
| } | ||
|
|
||
| export function WebSocketStream() { | ||
| return function(target: any, propertyKey: PropertyKey, parameterIndex: number) { | ||
| assert(typeof propertyKey === 'string', | ||
| `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`); | ||
| const methodName = propertyKey as string; | ||
| const controllerClazz = target.constructor as EggProtoImplClass; | ||
| WebSocketInfoUtil.setWebSocketMethodParamType(WebSocketParamType.STREAM, parameterIndex, controllerClazz, methodName); | ||
| }; | ||
| } |
110 changes: 110 additions & 0 deletions
110
core/controller-decorator/src/impl/websocket-fetch/WebSocketFetchControllerMetaBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import assert from 'node:assert'; | ||
| import { PrototypeUtil } from '@eggjs/core-decorator'; | ||
| import { ObjectUtils } from '@eggjs/tegg-common-util'; | ||
| import { ClassUtil } from '@eggjs/tegg-metadata'; | ||
| import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
| import { ControllerType, WebSocketFetchMethodType } from '@eggjs/tegg-types'; | ||
| import { ControllerMetaBuilderFactory } from '../../builder/ControllerMetaBuilderFactory'; | ||
| import { WebSocketFetchControllerMeta, WebSocketFetchMethodMeta } from '../../model'; | ||
| import ControllerInfoUtil from '../../util/ControllerInfoUtil'; | ||
| import { ControllerMetadataUtil } from '../../util/ControllerMetadataUtil'; | ||
| import { ControllerValidator } from '../../util/validator/ControllerValidator'; | ||
| import WebSocketInfoUtil from '../../util/WebSocketInfoUtil'; | ||
| import { WebSocketFetchControllerMethodMetaBuilder } from './WebSocketFetchControllerMethodMetaBuilder'; | ||
|
|
||
| export class WebSocketFetchControllerMetaBuilder { | ||
| private readonly clazz: EggProtoImplClass; | ||
|
|
||
| constructor(clazz: EggProtoImplClass) { | ||
| this.clazz = clazz; | ||
| } | ||
|
|
||
| private buildMethods() { | ||
| const methodNames = ObjectUtils.getProperties(this.clazz.prototype); | ||
| const methods: WebSocketFetchMethodMeta[] = []; | ||
| let connectionMethod: WebSocketFetchMethodMeta | undefined; | ||
| let openMethod: WebSocketFetchMethodMeta | undefined; | ||
| let errorMethod: WebSocketFetchMethodMeta | undefined; | ||
| let closeMethod: WebSocketFetchMethodMeta | undefined; | ||
|
|
||
| for (const methodName of methodNames) { | ||
| const builder = new WebSocketFetchControllerMethodMetaBuilder(this.clazz, methodName); | ||
| const methodMeta = builder.build(); | ||
| if (!methodMeta) { | ||
| continue; | ||
| } | ||
| switch (methodMeta.methodType) { | ||
| case WebSocketFetchMethodType.DATA: { | ||
| methods.push(methodMeta); | ||
| break; | ||
| } | ||
| case WebSocketFetchMethodType.CONNECTION: { | ||
| this.assertSingleLifecycleMethod(connectionMethod, methodMeta.methodType); | ||
| connectionMethod = methodMeta; | ||
| break; | ||
| } | ||
| case WebSocketFetchMethodType.OPEN: { | ||
| this.assertSingleLifecycleMethod(openMethod, methodMeta.methodType); | ||
| openMethod = methodMeta; | ||
| break; | ||
| } | ||
| case WebSocketFetchMethodType.ERROR: { | ||
| this.assertSingleLifecycleMethod(errorMethod, methodMeta.methodType); | ||
| errorMethod = methodMeta; | ||
| break; | ||
| } | ||
| case WebSocketFetchMethodType.CLOSE: { | ||
| this.assertSingleLifecycleMethod(closeMethod, methodMeta.methodType); | ||
| closeMethod = methodMeta; | ||
| break; | ||
| } | ||
| default: | ||
| assert.fail('never arrive'); | ||
| } | ||
| } | ||
| return { methods, connectionMethod, openMethod, errorMethod, closeMethod }; | ||
| } | ||
|
|
||
| private assertSingleLifecycleMethod(methodMeta: WebSocketFetchMethodMeta | undefined, methodType: WebSocketFetchMethodType) { | ||
| if (!methodMeta) { | ||
| return; | ||
| } | ||
| const classDesc = ClassUtil.classDescription(this.clazz); | ||
| throw new Error(`build websocket fetch controller ${classDesc} failed: duplicate ${methodType} lifecycle method`); | ||
| } | ||
|
|
||
| build(): WebSocketFetchControllerMeta { | ||
| ControllerValidator.validate(this.clazz); | ||
| const controllerType = ControllerInfoUtil.getControllerType(this.clazz); | ||
| assert(controllerType === ControllerType.WEBSOCKET_FETCH, 'invalid controller type'); | ||
| const webSocketPath = WebSocketInfoUtil.getWebSocketPath(this.clazz); | ||
| assert(webSocketPath, `build websocket fetch controller ${ClassUtil.classDescription(this.clazz)} failed: path is required`); | ||
| const middlewares = ControllerInfoUtil.getControllerMiddlewares(this.clazz); | ||
| const { methods, connectionMethod, openMethod, errorMethod, closeMethod } = this.buildMethods(); | ||
| assert(methods.length === 1, 'websocket fetch controller must have exactly one @WebSocketFetchMethod'); | ||
| const clazzName = this.clazz.name; | ||
| const controllerName = ControllerInfoUtil.getControllerName(this.clazz) || clazzName; | ||
| const property = PrototypeUtil.getProperty(this.clazz); | ||
| const protoName = property!.name as string; | ||
| const hosts = ControllerInfoUtil.getControllerHosts(this.clazz); | ||
| const timeout = ControllerInfoUtil.getControllerTimeout(this.clazz); | ||
| const metadata = new WebSocketFetchControllerMeta( | ||
| clazzName, protoName, controllerName, webSocketPath, middlewares, methods, hosts, | ||
| connectionMethod, openMethod, errorMethod, closeMethod, timeout); | ||
| ControllerMetadataUtil.setControllerMetadata(this.clazz, metadata); | ||
| for (const method of metadata.methods) { | ||
| const realPath = metadata.getMethodRealPath(method); | ||
| if (!realPath.startsWith('/')) { | ||
| const desc = ClassUtil.classDescription(this.clazz); | ||
| throw new Error(`class ${desc} method ${method.name} path ${realPath} not start with /`); | ||
| } | ||
| } | ||
| return metadata; | ||
| } | ||
|
|
||
| static create(clazz: EggProtoImplClass) { | ||
| return new WebSocketFetchControllerMetaBuilder(clazz); | ||
| } | ||
| } | ||
|
|
||
| ControllerMetaBuilderFactory.registerControllerMetaBuilder(ControllerType.WEBSOCKET_FETCH, WebSocketFetchControllerMetaBuilder.create); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.