Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<!-- markdownlint-disable MD024 -->
# Changelog

## [1.0.7] - 2024-11-21

### Changed

- The backend client now correctly uses asynchronous messaging to handle long running
operations.
- Updated the backend command line tool to respect the `ENVIRONMENT` env variable
if set.

## [1.0.6] - 2024-11-20

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/sdk",
"version": "1.0.6",
"version": "1.0.7",
"description": "Pipedream SDK",
"main": "dist/server/server/index.js",
"module": "dist/server/server/index.js",
Expand Down
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 type { AsyncResponseManagerOpts } from "../shared/async";
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 @@ export class ServerAsyncResponseManager extends AsyncResponseManager {
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");
}
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