Skip to content

Commit d4f1823

Browse files
committed
Switch Node to TypeScript build
1 parent 0edc3c8 commit d4f1823

File tree

6 files changed

+70
-40
lines changed

6 files changed

+70
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ docs/.vuepress/cache/
6666
docs/.vuepress/temp/
6767
docs/.vuepress/.temp/
6868
node/coverage/
69+
node/dist/
6970

7071
# Build outputs
7172
cppbuild/

node/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
"name": "epoch",
33
"version": "0.1.0",
44
"type": "commonjs",
5-
"main": "index.js",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
67
"scripts": {
7-
"test": "c8 --reporter=lcov --reporter=text node --test"
8+
"build": "tsc -p tsconfig.json",
9+
"test": "npm run build && c8 --reporter=lcov --reporter=text node --test"
810
},
911
"devDependencies": {
10-
"c8": "9.1.0"
12+
"c8": "9.1.0",
13+
"@types/node": "22.13.1",
14+
"typescript": "5.5.4"
1115
}
1216
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
export type ActorIdParts = {
2+
region: number;
3+
server: number;
4+
processType: number;
5+
processIndex: number;
6+
actorIndex: number;
7+
};
8+
9+
export type ActorIdCodec = {
10+
name: string;
11+
encode(parts: ActorIdParts): bigint;
12+
decode(value: bigint): ActorIdParts;
13+
};
14+
115
const REGION_BITS = 10n;
216
const SERVER_BITS = 12n;
317
const PROCESS_TYPE_BITS = 6n;
@@ -16,7 +30,7 @@ const PROCESS_TYPE_SHIFT = PROCESS_INDEX_SHIFT + PROCESS_INDEX_BITS;
1630
const SERVER_SHIFT = PROCESS_TYPE_SHIFT + PROCESS_TYPE_BITS;
1731
const REGION_SHIFT = SERVER_SHIFT + SERVER_BITS;
1832

19-
const defaultActorIdCodec = {
33+
export const defaultActorIdCodec: ActorIdCodec = {
2034
name: "default",
2135
encode(parts) {
2236
const region = BigInt(parts.region);
@@ -51,16 +65,10 @@ const defaultActorIdCodec = {
5165
}
5266
};
5367

54-
function encodeActorId(parts, codec = defaultActorIdCodec) {
68+
export function encodeActorId(parts: ActorIdParts, codec: ActorIdCodec = defaultActorIdCodec): bigint {
5569
return codec.encode(parts);
5670
}
5771

58-
function decodeActorId(value, codec = defaultActorIdCodec) {
72+
export function decodeActorId(value: bigint, codec: ActorIdCodec = defaultActorIdCodec): ActorIdParts {
5973
return codec.decode(value);
6074
}
61-
62-
module.exports = {
63-
defaultActorIdCodec,
64-
encodeActorId,
65-
decodeActorId
66-
};

node/index.js renamed to node/src/index.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
function version() {
1+
import { decodeActorId, defaultActorIdCodec, encodeActorId } from "./actor-id";
2+
3+
export type Message = {
4+
epoch: number;
5+
channelId: number;
6+
sourceId: number;
7+
sourceSeq: number;
8+
schemaId: number;
9+
qos?: number;
10+
payload: number;
11+
};
12+
13+
export type AeronConfig = {
14+
channel: string;
15+
streamId: number;
16+
aeronDirectory: string;
17+
};
18+
19+
export function version(): string {
220
return "0.1.0";
321
}
422

5-
const actorId = require("./actor-id");
6-
723
const FNV_OFFSET_BASIS = 0xcbf29ce484222325n;
824
const FNV_PRIME = 0x100000001b3n;
925
const FNV_MASK = 0xffffffffffffffffn;
1026

11-
function fnv1a64Hex(input) {
27+
export function fnv1a64Hex(input: string): string {
1228
let hash = FNV_OFFSET_BASIS;
1329
const bytes = Buffer.from(input, "utf8");
1430
for (const b of bytes) {
@@ -18,7 +34,7 @@ function fnv1a64Hex(input) {
1834
return hash.toString(16).padStart(16, "0");
1935
}
2036

21-
function processMessages(messages) {
37+
export function processMessages(messages: Message[]) {
2238
messages.sort((a, b) => {
2339
if (a.epoch !== b.epoch) return a.epoch - b.epoch;
2440
if (a.channelId !== b.channelId) return a.channelId - b.channelId;
@@ -29,9 +45,9 @@ function processMessages(messages) {
2945
return a.sourceSeq - b.sourceSeq;
3046
});
3147

32-
const results = [];
48+
const results: { epoch: number; state: number; hash: string }[] = [];
3349
let state = 0;
34-
let currentEpoch = null;
50+
let currentEpoch: number | null = null;
3551

3652
for (const message of messages) {
3753
if (currentEpoch === null) {
@@ -59,16 +75,14 @@ function processMessages(messages) {
5975
return results;
6076
}
6177

62-
class InMemoryTransport {
63-
constructor() {
64-
this.queue = [];
65-
}
78+
export class InMemoryTransport {
79+
private queue: Message[] = [];
6680

67-
send(message) {
81+
send(message: Message) {
6882
this.queue.push(message);
6983
}
7084

71-
poll(max) {
85+
poll(max: number) {
7286
if (!max || this.queue.length === 0) {
7387
return [];
7488
}
@@ -81,10 +95,8 @@ class InMemoryTransport {
8195
}
8296
}
8397

84-
class AeronTransport {
85-
constructor(config) {
86-
this.config = config;
87-
}
98+
export class AeronTransport {
99+
constructor(public readonly config: AeronConfig) {}
88100

89101
send() {
90102
throw new Error("Aeron transport not linked");
@@ -97,13 +109,4 @@ class AeronTransport {
97109
close() {}
98110
}
99111

100-
module.exports = {
101-
version,
102-
fnv1a64Hex,
103-
processMessages,
104-
InMemoryTransport,
105-
AeronTransport,
106-
defaultActorIdCodec: actorId.defaultActorIdCodec,
107-
encodeActorId: actorId.encodeActorId,
108-
decodeActorId: actorId.decodeActorId
109-
};
112+
export { defaultActorIdCodec, encodeActorId, decodeActorId };

node/test/version.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const assert = require("node:assert/strict");
33

44
const fs = require("node:fs");
55
const path = require("node:path");
6-
const epoch = require("../index.js");
6+
const epoch = require("../dist/index.js");
77

88
test("vector matches expected", () => {
99
const vectorPath = locateVectorFile();

node/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "CommonJS",
5+
"rootDir": "src",
6+
"outDir": "dist",
7+
"declaration": true,
8+
"strict": true,
9+
"esModuleInterop": false,
10+
"forceConsistentCasingInFileNames": true,
11+
"skipLibCheck": true
12+
},
13+
"include": ["src/**/*.ts"]
14+
}

0 commit comments

Comments
 (0)