From 656653d6d938732ce6d390ce3cc8fef12150bd59 Mon Sep 17 00:00:00 2001 From: T1B0 Date: Thu, 1 Feb 2024 14:30:05 +0100 Subject: [PATCH] fix: fix message header typing --- src/tcp.send-message.e2e.ts | 11 +++-- src/type.utils.ts | 14 ++++++ src/wire/command.code.ts | 15 ++++-- src/wire/message/header.type.ts | 79 +++++++++++++++++++------------- src/wire/message/header.utils.ts | 17 ++++--- 5 files changed, 88 insertions(+), 48 deletions(-) create mode 100644 src/type.utils.ts diff --git a/src/tcp.send-message.e2e.ts b/src/tcp.send-message.e2e.ts index fe94670..69177d3 100644 --- a/src/tcp.send-message.e2e.ts +++ b/src/tcp.send-message.e2e.ts @@ -15,6 +15,9 @@ import { purgeStream } from './wire/stream/purge-stream.command.js'; import { getOffset } from './wire/offset/get-offset.command.js'; import { storeOffset } from './wire/offset/store-offset.command.js'; import { uint8ToBuf } from './wire/number.utils.js'; +import { Message } from './wire/message/poll.utils.js'; +import { CreateMessage } from './wire/message/message.utils.js'; +import { HeaderKind } from './wire/message/header.type.js'; try { @@ -52,11 +55,11 @@ try { // const r_createTopic = await createTopic(s)(topic1); // console.log('RESPONSE_createTopic', r_createTopic); - const h0 = { 'foo': { kind: 9, value: 1 } }; - const h1 = { 'x-header-string-1': { kind: 2, value: 'incredible' } }; - const h2 = { 'x-header-bool': { kind: 3, value: false } }; + const h0 = { 'foo': { kind: HeaderKind.Uint8, value: 1 } }; + const h1 = { 'x-header-string-1': { kind: HeaderKind.String, value: 'incredible' } }; + const h2 = { 'x-header-bool': { kind: HeaderKind.Bool, value: false } }; - const msg: SendMessages = { + const msg = { streamId, topicId, messages: [ diff --git a/src/type.utils.ts b/src/type.utils.ts new file mode 100644 index 0000000..0d93ecf --- /dev/null +++ b/src/type.utils.ts @@ -0,0 +1,14 @@ + +export function reverseRecord< + T extends PropertyKey, + U extends PropertyKey, +>(input: Record) { + return Object.fromEntries( + Object.entries(input).map(([key, value]) => [ + value, + key, + ]), + ) as Record +} + +export type ValueOf = T[keyof T]; diff --git a/src/wire/command.code.ts b/src/wire/command.code.ts index bb3700b..dbbd967 100644 --- a/src/wire/command.code.ts +++ b/src/wire/command.code.ts @@ -1,3 +1,6 @@ + +import { reverseRecord } from '../type.utils.js'; + type CodeMap = Record; export const COMMAND_CODE: CodeMap = { @@ -45,11 +48,13 @@ export const COMMAND_CODE: CodeMap = { LeaveGroup: '605', }; -const reverseCommandCodeMap = Object.keys(COMMAND_CODE).reduce( - (ac, c: string) => { - ac[COMMAND_CODE[c]] = c; - return ac; - }, {}); +// const reverseCommandCodeMap = Object.keys(COMMAND_CODE).reduce( +// (ac, c: string) => { +// ac[COMMAND_CODE[c]] = c; +// return ac; +// }, {}); + +const reverseCommandCodeMap = reverseRecord(COMMAND_CODE); export const translateCommandCode = (code: number): string => { return reverseCommandCodeMap[code.toString()] || `unknow_command_code_${code}` diff --git a/src/wire/message/header.type.ts b/src/wire/message/header.type.ts index 5dc79fa..2b7a7ad 100644 --- a/src/wire/message/header.type.ts +++ b/src/wire/message/header.type.ts @@ -1,94 +1,107 @@ -export enum HeaderKind { - Raw = 1, - String = 2, - Bool = 3, - Int8 = 4, - Int16 = 5, - Int32 = 6, - Int64 = 7, - Int128 = 8, - Uint8 = 9, - Uint16 = 10, - Uint32 = 11, - Uint64 = 12, - Uint128 = 13, - Float = 14, - Double = 15 -}; +import { type ValueOf, reverseRecord } from "../../type.utils.js"; + +export const HeaderKind = { + Raw: 1, + String: 2, + Bool: 3, + Int8: 4, + Int16: 5, + Int32: 6, + Int64: 7, + Int128: 8, + Uint8: 9, + Uint16: 10, + Uint32: 11, + Uint64: 12, + Uint128: 13, + Float: 14, + Double: 15 +} as const; + +export type HeaderKind = typeof HeaderKind; + +export type HeaderKindId = keyof HeaderKind; +export type HeaderKindValue = ValueOf; + +export const HeaderKindIds = Object.keys(HeaderKind) as HeaderKindId[]; +export const HeaderKindValues = Object.values(HeaderKind) as HeaderKindValue[]; + +export const ReverseHeaderKind = reverseRecord(HeaderKind); + export type HeaderValueRaw = { - kind: HeaderKind.Raw, + kind: HeaderKind['Raw'], value: Buffer } export type HeaderValueString = { - kind: HeaderKind.String, + kind: HeaderKind['String'] value: string } export type HeaderValueBool = { - kind: HeaderKind.Bool, + kind: HeaderKind['Bool'], value: boolean } export type HeaderValueInt8 = { - kind: HeaderKind.Int8, + kind: HeaderKind['Int8'], value: number } export type HeaderValueInt16 = { - kind: HeaderKind.Int16, + kind: HeaderKind['Int16'], value: number -} +}; export type HeaderValueInt32 = { - kind: HeaderKind.Int32, + kind: HeaderKind['Int32'], value: number } export type HeaderValueInt64 = { - kind: HeaderKind.Int64, + kind: HeaderKind['Int64'], value: bigint } export type HeaderValueInt128 = { - kind: HeaderKind.Int128, + kind: HeaderKind['Int128'], value: Buffer // | ArrayBuffer // ? } export type HeaderValueUint8 = { - kind: HeaderKind.Uint8, + kind: HeaderKind['Uint8'], value: number } export type HeaderValueUint16 = { - kind: HeaderKind.Uint16, + kind: HeaderKind['Uint16'], value: number } export type HeaderValueUint32 = { - kind: HeaderKind.Uint32, + kind: HeaderKind['Uint32'], value: number } export type HeaderValueUint64 = { - kind: HeaderKind.Uint64, + kind: HeaderKind['Uint64'], value: bigint } export type HeaderValueUint128 = { - kind: HeaderKind.Uint128, + kind: HeaderKind['Uint128'], value: Buffer // | ArrayBuffer // ? } export type HeaderValueFloat = { - kind: HeaderKind.Float, + kind: HeaderKind['Float'], value: number } export type HeaderValueDouble = { - kind: HeaderKind.Double, + kind: HeaderKind['Double'], value: number } diff --git a/src/wire/message/header.utils.ts b/src/wire/message/header.utils.ts index 6dff586..1fb8a66 100644 --- a/src/wire/message/header.utils.ts +++ b/src/wire/message/header.utils.ts @@ -29,7 +29,11 @@ import { type HeaderValueUint128, type HeaderValueFloat, type HeaderValueDouble, - HeaderKind + HeaderKind, + HeaderKindValues, + HeaderKindId, + HeaderKindValue, + ReverseHeaderKind } from './header.type.js'; @@ -54,7 +58,7 @@ export type Headers = Record; type BinaryHeaderValue = { - kind: HeaderKind, + kind: number,// HeaderKind, value: Buffer } @@ -132,14 +136,14 @@ type ParsedHeaderDeserialized = { data: HeaderWithKey } -export const mapHeaderKind = (k: number): string => { - if (!(k in HeaderKind)) +export const mapHeaderKind = (k: number): HeaderKindId => { + if (!(HeaderKindValues.includes(k as HeaderKindValue))) throw new Error(`unknow header kind: ${k}`); - return HeaderKind[k]; + return ReverseHeaderKind[k as HeaderKindValue]; } export const deserializeHeaderValue = - (kind: HeaderKind, value: Buffer): ParsedHeaderValue => { + (kind: number, value: Buffer): ParsedHeaderValue => { switch (kind) { case HeaderKind.Int128: case HeaderKind.Uint128: @@ -156,6 +160,7 @@ export const deserializeHeaderValue = case HeaderKind.Bool: return value.readUInt8() === 1; case HeaderKind.Float: return value.readFloatLE(); case HeaderKind.Double: return value.readDoubleLE(); + default: throw new Error(`deserializeHeaderValue: invalid HeaderKind ${kind}`); } };