Skip to content

Commit

Permalink
Convert enums to constant objects with as const for better type saf…
Browse files Browse the repository at this point in the history
…ety (#204)

* Convert enums to constant objects with `as const` for better type safety

Replaced TypeScript `enum` declarations with constant objects using `as const`. This change preserves the same api, but removes enums, and allows using the values directly without importing the constant. Enums are problematic because they require transpilation, with this change erasure is possible.

Signed-off-by: Alberto Ricart <[email protected]>

* fix tests

Signed-off-by: Alberto Ricart <[email protected]>

---------

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart authored Feb 7, 2025
1 parent 47338f4 commit 747c3e5
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 231 deletions.
18 changes: 10 additions & 8 deletions core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,16 @@ export interface ServersChanged {
readonly deleted: string[];
}

export enum Match {
// Exact option is case sensitive
Exact = 0,
// Case sensitive, but key is transformed to Canonical MIME representation
CanonicalMIME,
// Case insensitive matches
IgnoreCase,
}
export const Match = {
// Exact option is case-sensitive
Exact: "exact",
// Case-sensitive, but key is transformed to Canonical MIME representation
CanonicalMIME: "canonical",
// Case-insensitive matches
IgnoreCase: "insensitive",
} as const;

export type Match = typeof Match[keyof typeof Match];

export interface MsgHdrs extends Iterable<[string, string[]]> {
hasError: boolean;
Expand Down
12 changes: 6 additions & 6 deletions core/src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class MsgHdrsImpl implements MsgHdrs {
return keys;
}

findKeys(k: string, match = Match.Exact): string[] {
findKeys(k: string, match: Match = Match.Exact): string[] {
const keys = this.keys();
switch (match) {
case Match.Exact:
Expand Down Expand Up @@ -230,16 +230,16 @@ export class MsgHdrsImpl implements MsgHdrs {
return "";
}

has(k: string, match = Match.Exact): boolean {
has(k: string, match: Match = Match.Exact): boolean {
return this.findKeys(k, match).length > 0;
}

set(k: string, v: string, match = Match.Exact): void {
set(k: string, v: string, match: Match = Match.Exact): void {
this.delete(k, match);
this.append(k, v, match);
}

append(k: string, v: string, match = Match.Exact): void {
append(k: string, v: string, match: Match = Match.Exact): void {
// validate the key
const ck = canonicalMIMEHeaderKey(k);
if (match === Match.CanonicalMIME) {
Expand All @@ -259,7 +259,7 @@ export class MsgHdrsImpl implements MsgHdrs {
a.push(value);
}

values(k: string, match = Match.Exact): string[] {
values(k: string, match: Match = Match.Exact): string[] {
const buf: string[] = [];
const keys = this.findKeys(k, match);
keys.forEach((v) => {
Expand All @@ -271,7 +271,7 @@ export class MsgHdrsImpl implements MsgHdrs {
return buf;
}

delete(k: string, match = Match.Exact): void {
delete(k: string, match: Match = Match.Exact): void {
const keys = this.findKeys(k, match);
keys.forEach((v) => {
this.headers.delete(v);
Expand Down
152 changes: 79 additions & 73 deletions core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import { DenoBuffer } from "./denobuffer.ts";
import { TD } from "./encoders.ts";
import type { Dispatcher } from "./core.ts";

export enum Kind {
OK,
ERR,
MSG,
INFO,
PING,
PONG,
}
export const Kind = {
OK: 0,
ERR: 1,
MSG: 2,
INFO: 3,
PING: 4,
PONG: 5,
} as const;

export type Kind = typeof Kind[keyof typeof Kind];

export interface ParserEvent {
kind: Kind;
Expand Down Expand Up @@ -680,69 +682,73 @@ export class Parser {
}
}

export enum State {
OP_START = 0,
OP_PLUS,
OP_PLUS_O,
OP_PLUS_OK,
OP_MINUS,
OP_MINUS_E,
OP_MINUS_ER,
OP_MINUS_ERR,
OP_MINUS_ERR_SPC,
MINUS_ERR_ARG,
OP_M,
OP_MS,
OP_MSG,
OP_MSG_SPC,
MSG_ARG,
MSG_PAYLOAD,
MSG_END,
OP_H,
OP_P,
OP_PI,
OP_PIN,
OP_PING,
OP_PO,
OP_PON,
OP_PONG,
OP_I,
OP_IN,
OP_INF,
OP_INFO,
OP_INFO_SPC,
INFO_ARG,
}
export const State = {
OP_START: 0,
OP_PLUS: 1,
OP_PLUS_O: 2,
OP_PLUS_OK: 3,
OP_MINUS: 4,
OP_MINUS_E: 5,
OP_MINUS_ER: 6,
OP_MINUS_ERR: 7,
OP_MINUS_ERR_SPC: 8,
MINUS_ERR_ARG: 9,
OP_M: 10,
OP_MS: 11,
OP_MSG: 12,
OP_MSG_SPC: 13,
MSG_ARG: 14,
MSG_PAYLOAD: 15,
MSG_END: 16,
OP_H: 17,
OP_P: 18,
OP_PI: 19,
OP_PIN: 20,
OP_PING: 21,
OP_PO: 22,
OP_PON: 23,
OP_PONG: 24,
OP_I: 25,
OP_IN: 26,
OP_INF: 27,
OP_INFO: 28,
OP_INFO_SPC: 29,
INFO_ARG: 30,
} as const;

enum cc {
CR = "\r".charCodeAt(0),
E = "E".charCodeAt(0),
e = "e".charCodeAt(0),
F = "F".charCodeAt(0),
f = "f".charCodeAt(0),
G = "G".charCodeAt(0),
g = "g".charCodeAt(0),
H = "H".charCodeAt(0),
h = "h".charCodeAt(0),
I = "I".charCodeAt(0),
i = "i".charCodeAt(0),
K = "K".charCodeAt(0),
k = "k".charCodeAt(0),
M = "M".charCodeAt(0),
m = "m".charCodeAt(0),
MINUS = "-".charCodeAt(0),
N = "N".charCodeAt(0),
n = "n".charCodeAt(0),
NL = "\n".charCodeAt(0),
O = "O".charCodeAt(0),
o = "o".charCodeAt(0),
P = "P".charCodeAt(0),
p = "p".charCodeAt(0),
PLUS = "+".charCodeAt(0),
R = "R".charCodeAt(0),
r = "r".charCodeAt(0),
S = "S".charCodeAt(0),
s = "s".charCodeAt(0),
SPACE = " ".charCodeAt(0),
TAB = "\t".charCodeAt(0),
}
export type State = typeof State[keyof typeof State];

export const cc = {
CR: "\r".charCodeAt(0),
E: "E".charCodeAt(0),
e: "e".charCodeAt(0),
F: "F".charCodeAt(0),
f: "f".charCodeAt(0),
G: "G".charCodeAt(0),
g: "g".charCodeAt(0),
H: "H".charCodeAt(0),
h: "h".charCodeAt(0),
I: "I".charCodeAt(0),
i: "i".charCodeAt(0),
K: "K".charCodeAt(0),
k: "k".charCodeAt(0),
M: "M".charCodeAt(0),
m: "m".charCodeAt(0),
MINUS: "-".charCodeAt(0),
N: "N".charCodeAt(0),
n: "n".charCodeAt(0),
NL: "\n".charCodeAt(0),
O: "O".charCodeAt(0),
o: "o".charCodeAt(0),
P: "P".charCodeAt(0),
p: "p".charCodeAt(0),
PLUS: "+".charCodeAt(0),
R: "R".charCodeAt(0),
r: "r".charCodeAt(0),
S: "S".charCodeAt(0),
s: "s".charCodeAt(0),
SPACE: " ".charCodeAt(0),
TAB: "\t".charCodeAt(0),
} as const;

export type cc = typeof cc[keyof typeof cc];
42 changes: 22 additions & 20 deletions core/src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,26 @@ export function compare(a: SemVer, b: SemVer): number {
return 0;
}

export enum Feature {
JS_KV = "js_kv",
JS_OBJECTSTORE = "js_objectstore",
JS_PULL_MAX_BYTES = "js_pull_max_bytes",
JS_NEW_CONSUMER_CREATE_API = "js_new_consumer_create",
JS_ALLOW_DIRECT = "js_allow_direct",
JS_MULTIPLE_CONSUMER_FILTER = "js_multiple_consumer_filter",
JS_SIMPLIFICATION = "js_simplification",
JS_STREAM_CONSUMER_METADATA = "js_stream_consumer_metadata",
JS_CONSUMER_FILTER_SUBJECTS = "js_consumer_filter_subjects",
JS_STREAM_FIRST_SEQ = "js_stream_first_seq",
JS_STREAM_SUBJECT_TRANSFORM = "js_stream_subject_transform",
JS_STREAM_SOURCE_SUBJECT_TRANSFORM = "js_stream_source_subject_transform",
JS_STREAM_COMPRESSION = "js_stream_compression",
JS_DEFAULT_CONSUMER_LIMITS = "js_default_consumer_limits",
JS_BATCH_DIRECT_GET = "js_batch_direct_get",
JS_PRIORITY_GROUPS = "js_priority_groups",
}
export const Feature = {
JS_KV: "js_kv",
JS_OBJECTSTORE: "js_objectstore",
JS_PULL_MAX_BYTES: "js_pull_max_bytes",
JS_NEW_CONSUMER_CREATE_API: "js_new_consumer_create",
JS_ALLOW_DIRECT: "js_allow_direct",
JS_MULTIPLE_CONSUMER_FILTER: "js_multiple_consumer_filter",
JS_SIMPLIFICATION: "js_simplification",
JS_STREAM_CONSUMER_METADATA: "js_stream_consumer_metadata",
JS_CONSUMER_FILTER_SUBJECTS: "js_consumer_filter_subjects",
JS_STREAM_FIRST_SEQ: "js_stream_first_seq",
JS_STREAM_SUBJECT_TRANSFORM: "js_stream_subject_transform",
JS_STREAM_SOURCE_SUBJECT_TRANSFORM: "js_stream_source_subject_transform",
JS_STREAM_COMPRESSION: "js_stream_compression",
JS_DEFAULT_CONSUMER_LIMITS: "js_default_consumer_limits",
JS_BATCH_DIRECT_GET: "js_batch_direct_get",
JS_PRIORITY_GROUPS: "js_priority_groups",
} as const;

export type Feature = typeof Feature[keyof typeof Feature];

type FeatureVersion = {
ok: boolean;
Expand All @@ -63,8 +65,8 @@ type FeatureVersion = {

export class Features {
server!: SemVer;
features: Map<Feature, FeatureVersion>;
disabled: Feature[];
features: Map<string, FeatureVersion>;
disabled: string[];
constructor(v: SemVer) {
this.features = new Map<Feature, FeatureVersion>();
this.disabled = [];
Expand Down
13 changes: 8 additions & 5 deletions jetstream/src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ import type {
import { JetStreamError, JetStreamStatus } from "./jserrors.ts";
import { minValidation } from "./jsutil.ts";

enum PullConsumerType {
Unset = -1,
Consume,
Fetch,
}
export const PullConsumerType = {
Unset: "",
Consume: "consume",
Fetch: "fetch",
} as const;

export type PullConsumerType =
typeof PullConsumerType[keyof typeof PullConsumerType];

export type OrderedConsumerState = {
namePrefix: string;
Expand Down
Loading

0 comments on commit 747c3e5

Please sign in to comment.