Skip to content

Commit

Permalink
use const enum instead of bare consts
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Nov 14, 2023
1 parent 48e346f commit ef9b7aa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
10 changes: 5 additions & 5 deletions router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { Pushable } from 'it-pushable';
import { Server } from './server';
import {
OpaqueTransportMessage,
StreamClosedBit,
StreamOpenBit,
ControlFlags,
msg,
} from '../transport/message';
import { Static } from '@sinclair/typebox';
Expand Down Expand Up @@ -100,7 +99,7 @@ export const createClient = <Srv extends Server<Record<string, AnyService>>>(
rawIn as object,
);

m.controlFlags |= StreamOpenBit;
m.controlFlags |= ControlFlags.StreamOpenBit;
transport.send(m);
}
})();
Expand All @@ -124,7 +123,7 @@ export const createClient = <Srv extends Server<Record<string, AnyService>>>(
streamId,
{},
);
closeMessage.controlFlags |= StreamClosedBit;
closeMessage.controlFlags |= ControlFlags.StreamClosedBit;
transport.send(closeMessage);
transport.removeMessageListener(listener);
};
Expand All @@ -141,7 +140,8 @@ export const createClient = <Srv extends Server<Record<string, AnyService>>>(
input as object,
);

m.controlFlags |= StreamOpenBit | StreamClosedBit;
m.controlFlags |=
ControlFlags.StreamOpenBit | ControlFlags.StreamClosedBit;
transport.send(m);
return waitForMessage(transport, belongsToSameStream);
}
Expand Down
14 changes: 6 additions & 8 deletions transport/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
AckBit,
StreamClosedBit,
StreamOpenBit,
ControlFlags,
isAck,
isStreamClose,
isStreamOpen,
Expand All @@ -13,7 +11,7 @@ import { describe, test, expect } from 'vitest';
describe('message helpers', () => {
test('ack', () => {
const m = msg('a', 'b', 'svc', 'proc', 'stream', { test: 1 });
m.controlFlags |= AckBit;
m.controlFlags |= ControlFlags.AckBit;
expect(m).toHaveProperty('controlFlags');
expect(isAck(m.controlFlags)).toBe(true);
expect(isStreamOpen(m.controlFlags)).toBe(false);
Expand All @@ -22,7 +20,7 @@ describe('message helpers', () => {

test('streamOpen', () => {
const m = msg('a', 'b', 'svc', 'proc', 'stream', { test: 1 });
m.controlFlags |= StreamOpenBit;
m.controlFlags |= ControlFlags.StreamOpenBit;
expect(m).toHaveProperty('controlFlags');
expect(isAck(m.controlFlags)).toBe(false);
expect(isStreamOpen(m.controlFlags)).toBe(true);
Expand All @@ -31,7 +29,7 @@ describe('message helpers', () => {

test('streamClose', () => {
const m = msg('a', 'b', 'svc', 'proc', 'stream', { test: 1 });
m.controlFlags |= StreamClosedBit;
m.controlFlags |= ControlFlags.StreamClosedBit;
expect(m).toHaveProperty('controlFlags');
expect(isAck(m.controlFlags)).toBe(false);
expect(isStreamOpen(m.controlFlags)).toBe(false);
Expand All @@ -57,10 +55,10 @@ describe('message helpers', () => {

test('combining control flags works', () => {
const m = msg('a', 'b', 'svc', 'proc', 'stream', { test: 1 });
m.controlFlags |= StreamOpenBit;
m.controlFlags |= ControlFlags.StreamOpenBit;
expect(isStreamOpen(m.controlFlags)).toBe(true);
expect(isStreamClose(m.controlFlags)).toBe(false);
m.controlFlags |= StreamClosedBit;
m.controlFlags |= ControlFlags.StreamClosedBit;
expect(isStreamOpen(m.controlFlags)).toBe(true);
expect(isStreamClose(m.controlFlags)).toBe(true);
});
Expand Down
19 changes: 13 additions & 6 deletions transport/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Type, TSchema } from '@sinclair/typebox';
import { nanoid } from 'nanoid';

// bit masks for control flags
export const AckBit = 0b0001;
export const StreamOpenBit = 0b0010;
export const StreamClosedBit = 0b0100;
export const enum ControlFlags {
AckBit = 0b0001,
StreamOpenBit = 0b0010,
StreamClosedBit = 0b0100,
}

// look at https://github.com/websockets/ws#use-the-nodejs-streams-api for a duplex stream we can use
export const TransportMessageSchema = <T extends TSchema>(t: T) =>
Expand Down Expand Up @@ -78,13 +80,18 @@ export function reply<Payload extends object>(
}

export function isAck(controlFlag: number): boolean {
return (controlFlag & AckBit) === AckBit;
return (controlFlag & ControlFlags.AckBit) === ControlFlags.AckBit;
}

export function isStreamOpen(controlFlag: number): boolean {
return (controlFlag & StreamOpenBit) === StreamOpenBit;
return (
(controlFlag & ControlFlags.StreamOpenBit) === ControlFlags.StreamOpenBit
);
}

export function isStreamClose(controlFlag: number): boolean {
return (controlFlag & StreamClosedBit) === StreamClosedBit;
return (
(controlFlag & ControlFlags.StreamClosedBit) ===
ControlFlags.StreamClosedBit
);
}
4 changes: 2 additions & 2 deletions transport/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Codec } from '../codec/types';
import { Value } from '@sinclair/typebox/value';
import {
AckBit,
ControlFlags,
MessageId,
OpaqueTransportMessage,
OpaqueTransportMessageSchema,
Expand Down Expand Up @@ -58,7 +58,7 @@ export abstract class Transport {
}

const ackMsg = reply(parsedMsg, { ack: parsedMsg.id });
ackMsg.controlFlags = AckBit;
ackMsg.controlFlags = ControlFlags.AckBit;
ackMsg.from = this.clientId;
this.send(ackMsg);
} else {
Expand Down

0 comments on commit ef9b7aa

Please sign in to comment.