Skip to content

Commit

Permalink
fix build node externals
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Jan 6, 2025
1 parent 7b6c45d commit f4617c7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
18 changes: 11 additions & 7 deletions node/src/realtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { WSS_API_URI, REALTIME_API_URI, PUBLIC_API_URI } from "./constants";
import { WebSocket } from "ws";

// if WebSocket doesn't exist in the global scope, import it
if (typeof WebSocket === "undefined") {
global.WebSocket = require("ws");
}

export class Realtime {
private socket?: WebSocket;
Expand Down Expand Up @@ -54,19 +58,19 @@ export class Realtime {
this.startHeartbeatCheck();
};

this.socket.onmessage = (event: any) => {
this.socket.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
if (data.type === "heartbeat") {
this.handleHeartbeat();
} else {
if (data?.id) {
fetch(`${REALTIME_API_URI}/api/v1/events/ack`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: data.id }),
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: data.id })
});
}
}
this.handleEvent(data);
}
} catch (err) {
Expand Down Expand Up @@ -118,4 +122,4 @@ export class Realtime {
this.handleEvent = callback;
this.connect();
}
}
}
19 changes: 9 additions & 10 deletions node/test/realtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ afterEach(() => {

// Constructor Tests
test("constructor throws error if no teamApiKey is provided", () => {
expect(() => new Realtime()).toThrow(
"teamApiKey is required for Realtime connection",
);
expect(() => new Realtime()).toThrow("teamApiKey is required for Realtime connection");
});

test("constructor initializes with valid teamApiKey", () => {
Expand Down Expand Up @@ -102,9 +100,7 @@ test("heartbeat message updates lastHeartbeat", async () => {
const initialHeartbeat = instance["lastHeartbeat"];
const heartbeatMessage = { type: "heartbeat" };

mockWebSocket.onmessage?.(
new MessageEvent("message", { data: JSON.stringify(heartbeatMessage) }),
);
mockWebSocket.onmessage?.(new MessageEvent("message", { data: JSON.stringify(heartbeatMessage) }));

expect(instance["lastHeartbeat"]).toBeGreaterThanOrEqual(initialHeartbeat);
});
Expand All @@ -130,9 +126,7 @@ test("attempts to reconnect after socket close", async () => {

// Error Handling Tests
test("logs error when WebSocket encounters an error", async () => {
const consoleErrorSpy = vi
.spyOn(console, "error")
.mockImplementation(() => {});
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});

instance = new Realtime("mock-team-api-key");
instance.listen(() => {});
Expand Down Expand Up @@ -165,6 +159,8 @@ test("stops heartbeat check on socket close", async () => {
clearIntervalSpy.mockRestore();
});



// Acknowledgement Tests

// Acknowledgement Tests
Expand Down Expand Up @@ -206,6 +202,9 @@ test("sends acknowledgment for messages with an id", async () => {
fetchSpy.mockRestore();
});




test("don't send acknowledgment for messages without id", async () => {
instance = new Realtime("mock-team-api-key");
const mockCallback = vi.fn();
Expand All @@ -215,7 +214,7 @@ test("don't send acknowledgment for messages without id", async () => {

mockWebSocket.onopen?.(new Event("open"));

const testPayload = { event: "update", data: { key: "value" } };
const testPayload = { event: "update", data: { key: "value" } };
const mockMessage = { data: JSON.stringify(testPayload) };

mockWebSocket.onmessage?.(new MessageEvent("message", mockMessage));
Expand Down
14 changes: 14 additions & 0 deletions node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"declaration": true,
"declarationDir": "dist/types",
"emitDeclarationOnly": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}

0 comments on commit f4617c7

Please sign in to comment.