Skip to content

Commit

Permalink
fix: fix message header typing
Browse files Browse the repository at this point in the history
  • Loading branch information
T1B0 committed Jul 4, 2024
1 parent f9bd442 commit 1276374
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 48 deletions.
11 changes: 7 additions & 4 deletions src/tcp.send-message.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: [
Expand Down
14 changes: 14 additions & 0 deletions src/type.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,14 @@

export function reverseRecord<
T extends PropertyKey,
U extends PropertyKey,
>(input: Record<T, U>) {
return Object.fromEntries(
Object.entries(input).map(([key, value]) => [
value,
key,
]),
) as Record<U, T>
}

export type ValueOf<T> = T[keyof T];
15 changes: 10 additions & 5 deletions src/wire/command.code.ts
Original file line number Diff line number Diff line change
@@ -1,3 1,6 @@

import { reverseRecord } from '../type.utils.js';

type CodeMap = Record<string, string>;

export const COMMAND_CODE: CodeMap = {
Expand Down Expand Up @@ -45,11 48,13 @@ export const COMMAND_CODE: CodeMap = {
LeaveGroup: '605',
};

const reverseCommandCodeMap = Object.keys(COMMAND_CODE).reduce<CodeMap>(
(ac, c: string) => {
ac[COMMAND_CODE[c]] = c;
return ac;
}, {});
// const reverseCommandCodeMap = Object.keys(COMMAND_CODE).reduce<CodeMap>(
// (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}`
Expand Down
79 changes: 46 additions & 33 deletions src/wire/message/header.type.ts
Original file line number Diff line number Diff line change
@@ -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<HeaderKind>;

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
}

Expand Down
17 changes: 11 additions & 6 deletions src/wire/message/header.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 29,11 @@ import {
type HeaderValueUint128,
type HeaderValueFloat,
type HeaderValueDouble,
HeaderKind
HeaderKind,
HeaderKindValues,
HeaderKindId,
HeaderKindValue,
ReverseHeaderKind
} from './header.type.js';


Expand All @@ -54,7 58,7 @@ export type Headers = Record<string, HeaderValue>;


type BinaryHeaderValue = {
kind: HeaderKind,
kind: number,// HeaderKind,
value: Buffer
}

Expand Down Expand Up @@ -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:
Expand All @@ -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}`);
}
};

Expand Down

0 comments on commit 1276374

Please sign in to comment.