Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/sdk/src/server/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { adapters } from "@rails/actioncable";
import * as WS from "ws";

declare global {
function addEventListener(type: string, listener: () => void): void;
function removeEventListener(type: string, listener: () => void): void;
}

export type ServerAsyncResponseManagerOpts = {
apiHost: string;
getOauthToken: () => Promise<AccessToken> | AccessToken;
Expand All @@ -16,12 +21,20 @@
constructor(opts: ServerAsyncResponseManagerOpts) {
super();
this.serverOpts = opts;
// eslint-disable-next-line @typescript-eslint/no-empty-function
global.addEventListener = () => {};
// eslint-disable-next-line @typescript-eslint/no-empty-function
global.removeEventListener = () => {};
if (typeof adapters.WebSocket === "undefined")
adapters.WebSocket == WS;
adapters.WebSocket = WS as unknown as WebSocket;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider implementing a proper WebSocket adapter

The current type cast as unknown as WebSocket bypasses TypeScript's type checking, which could hide potential incompatibilities between the 'ws' package and the browser's WebSocket interface.

Consider implementing a proper adapter class:

class NodeWebSocketAdapter implements WebSocket {
  private ws: WS;
  
  constructor(url: string, protocols?: string | string[]) {
    this.ws = new WS(url, protocols);
  }
  
  // Implement necessary WebSocket interface methods
  get readyState(): number {
    return this.ws.readyState;
  }
  
  close(code?: number, reason?: string): void {
    this.ws.close(code, reason);
  }
  
  // ... implement other required methods
}

// Usage
adapters.WebSocket = NodeWebSocketAdapter;

}

protected override async getOpts(): Promise<AsyncResponseManagerOpts> {
const token = await this.serverOpts.getOauthToken();
const oauthToken = await this.serverOpts.getOauthToken();
if (!oauthToken?.token?.access_token) {
throw new Error('Invalid OAuth token structure');

Check failure on line 35 in packages/sdk/src/server/async.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Strings must use doublequote
}
const token = oauthToken.token.access_token;
const projectId = await this.serverOpts.getProjectId();
return {
url: `wss://${this.serverOpts.apiHost}/websocket?oauth_token=${token}`,
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/server/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createBackendClient } from "./index";
import { program } from "commander";

const {
CLIENT_ID, CLIENT_SECRET, PROJECT_ID, API_HOST,
CLIENT_ID, CLIENT_SECRET, PROJECT_ID, API_HOST, ENVIRONMENT,
} = process.env;

if (!CLIENT_ID || !CLIENT_SECRET || !PROJECT_ID) {
Expand All @@ -17,6 +17,9 @@ const client = createBackendClient({
},
projectId: PROJECT_ID,
apiHost: API_HOST,
environment: ENVIRONMENT === "production"
? "production"
: "development",
});

program
Expand Down
Loading