From 2e9eba30de1a24c49feb70a9701e8e9ede5a1e86 Mon Sep 17 00:00:00 2001 From: Angus ZENG Date: Sun, 30 Jun 2024 19:55:41 +0800 Subject: [PATCH] feat(protocol): added feature flag properties for channel objects --- CHANGES.md | 1 + src/examples/shared/router.ts | 5 +++++ src/lib/server/LegacyServerChannel.impl.ts | 4 ++++ src/lib/shared/Channel.impl.ts | 4 ++++ src/lib/shared/Shared.decl.ts | 10 ++++++++++ 5 files changed, 24 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 800bff3..6d67b9c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ ## v1.1.0 - feat(protocol): added experimental supports for transports between worker thread. +- feat(protocol): added feature flag properties for channel objects. - fix(protocol): simplified the ITransporter.end method. - fix(decoder): should decode protocol error as special error. - fix(protocol): put protocol-error code in server_internal_server data. diff --git a/src/examples/shared/router.ts b/src/examples/shared/router.ts index db3926b..60cd579 100644 --- a/src/examples/shared/router.ts +++ b/src/examples/shared/router.ts @@ -51,6 +51,11 @@ export const router: Tv.Servers.IRouter = new Tv.Servers.SimpleJsonApiRouter() serverLogs.ok(`Channel#${ctx.channel.id} invoked sendMeMessage with message: ${msg}`); + if (!ctx.channel.isMessageSupported) { + + throw new Tv.errors.cmd_not_impl(); + } + ctx.channel.sendMessage(msg).catch(e => serverLogs.error(`Failed while sending message, error: ${e}`)); }) .registerApi('sendMeMessageAsync', async (ctx, msg: string): Promise => { diff --git a/src/lib/server/LegacyServerChannel.impl.ts b/src/lib/server/LegacyServerChannel.impl.ts index 5742785..2c3f4c3 100644 --- a/src/lib/server/LegacyServerChannel.impl.ts +++ b/src/lib/server/LegacyServerChannel.impl.ts @@ -32,6 +32,10 @@ export class TvLegacyServerChannelV1 extends EventEmitter implements Shared.ICha public get writable(): boolean { return false; } + public get isMessageSupported(): boolean { return false; } + + public get isBinaryStreamSupported(): boolean { return false; } + public get id(): number { return 0; } public get timeout(): number { return 30_000; } diff --git a/src/lib/shared/Channel.impl.ts b/src/lib/shared/Channel.impl.ts index a3ff500..232063a 100644 --- a/src/lib/shared/Channel.impl.ts +++ b/src/lib/shared/Channel.impl.ts @@ -88,6 +88,10 @@ export abstract class AbstractTvChannelV2 public ended: boolean = false; + public get isMessageSupported(): boolean { return true; } + + public get isBinaryStreamSupported(): boolean { return this.streams.maxStreams !== 0; } + public get finished(): boolean { return this._state === EState.ENDED; diff --git a/src/lib/shared/Shared.decl.ts b/src/lib/shared/Shared.decl.ts index 540fdbd..2f16f77 100644 --- a/src/lib/shared/Shared.decl.ts +++ b/src/lib/shared/Shared.decl.ts @@ -303,6 +303,16 @@ export interface IChannelBase extends IEventList */ readonly transporter: ITransporter; + /** + * Whether this channel supports server-push messages or not. + */ + readonly isMessageSupported: boolean; + + /** + * Whether this channel supports binary streams or not. + */ + readonly isBinaryStreamSupported: boolean; + /** * Tells whether this channel is closed on the local side or not. */