Skip to content
Merged
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion packages/sdk/src/server/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import { adapters } from "@rails/actioncable";
import * as WS from "ws";

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

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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Missing semicolon
function removeEventListener(type: string, listener: () => void): void

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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Missing semicolon
}
global.addEventListener = () => {}

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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Unexpected empty arrow function

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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Missing semicolon
global.removeEventListener = () => {}

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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Unexpected empty arrow function

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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Missing semicolon
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 a more robust approach for handling browser APIs in Node.js

The current implementation adds global no-op event listeners, which could mask potential issues by silently ignoring events. Consider using a proper polyfill or environment-specific implementation.

-declare global {
-  function addEventListener(type: string, listener: () => void): void
-  function removeEventListener(type: string, listener: () => void): void
-}
-global.addEventListener = () => {}
-global.removeEventListener = () => {}
+import { EventEmitter } from 'events';
+
+class NodeEventAdapter extends EventEmitter {
+  addEventListener(type: string, listener: () => void): void {
+    this.on(type, listener);
+  }
+  
+  removeEventListener(type: string, listener: () => void): void {
+    this.off(type, listener);
+  }
+}
+
+const eventAdapter = new NodeEventAdapter();
+global.addEventListener = eventAdapter.addEventListener.bind(eventAdapter);
+global.removeEventListener = eventAdapter.removeEventListener.bind(eventAdapter);

Also, fix the linting issues by adding semicolons:

 declare global {
-  function addEventListener(type: string, listener: () => void): void
-  function removeEventListener(type: string, listener: () => void): void
+  function addEventListener(type: string, listener: () => void): void;
+  function removeEventListener(type: string, listener: () => void): void;
 }
-global.addEventListener = () => {}
-global.removeEventListener = () => {}
+global.addEventListener = () => {};
+global.removeEventListener = () => {};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
declare global {
function addEventListener(type: string, listener: () => void): void
function removeEventListener(type: string, listener: () => void): void
}
global.addEventListener = () => {}
global.removeEventListener = () => {}
declare global {
function addEventListener(type: string, listener: () => void): void;
function removeEventListener(type: string, listener: () => void): void;
}
global.addEventListener = () => {};
global.removeEventListener = () => {};
🧰 Tools
🪛 eslint (1.23.1)

[error] 8-9: Missing semicolon.

(@typescript-eslint/semi)


[error] 9-10: Missing semicolon.

(@typescript-eslint/semi)


[error] 11-11: Unexpected empty arrow function.

(@typescript-eslint/no-empty-function)


[error] 11-12: Missing semicolon.

(@typescript-eslint/semi)


[error] 12-12: Unexpected empty arrow function.

(@typescript-eslint/no-empty-function)


export type ServerAsyncResponseManagerOpts = {
apiHost: string;
getOauthToken: () => Promise<AccessToken> | AccessToken;
Expand All @@ -21,7 +28,8 @@
}

protected override async getOpts(): Promise<AsyncResponseManagerOpts> {
const token = await this.serverOpts.getOauthToken();
const oauthToken = await this.serverOpts.getOauthToken();
const token = oauthToken.token.access_token;
const projectId = await this.serverOpts.getProjectId();
return {
url: `wss://${this.serverOpts.apiHost}/websocket?oauth_token=${token}`,
Expand Down
Loading