Skip to content

fix: CORS issues with new websocket implementation #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 14, 2023
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
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
lib/
socket.io-websocket-only.js
doc/resource
coverage/
build/
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ package.tgz
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.DS_Store
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"dist"
],
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.2",
"@types/uuid": "^9.0.2",
"cross-fetch": "^3.1.6",
"dayjs": "^1.11.8",
Expand All @@ -40,7 +39,8 @@
"typescript": "^5.1.3",
"vite": "^4.3.9",
"vite-plugin-dts": "^2.3.0",
"vitest": "^0.32.2"
"vitest": "^0.32.2",
"ws": "^8.13.0"
},
"repository": {
"type": "git",
Expand All @@ -58,10 +58,19 @@
},
"homepage": "http://ftrack.com",
"dependencies": {
"isomorphic-ws": "^5.0.0",
"loglevel": "^1.8.1",
"moment": "^2.29.4",
"uuid": "^9.0.0"
},
"peerDependencies": {
"ws": "^8.13.0"
},
"peerDependenciesMeta": {
"ws": {
"optional": true
}
},
"lint-staged": {
"*.{js,ts}": "eslint --cache --fix --max-warnings=0",
"*.{js,ts,css,md,json,jsx,scss,yml}": "prettier --write"
Expand Down
10 changes: 5 additions & 5 deletions source/event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// :copyright: Copyright (c) 2016 ftrack
import { v4 as uuidV4 } from "uuid";

import { EventSource } from "./event_hub.js";
/**
* ftrack API Event class.
*/
Expand All @@ -11,7 +11,7 @@ export class Event {
target: string;
inReplyToEvent: string | null;
id: string;
source?: any;
source?: EventSource;
};

/**
Expand All @@ -37,20 +37,20 @@ export class Event {
}

/** Return event data. */
getData(): { [key: string]: any } {
getData() {
return this._data;
}

/** Add source to event data, keeping any already avalable source information */
prepareSource(source: object): void {
prepareSource(source: EventSource): void {
this._data.source = {
...source,
...this._data.source,
};
}

/** Add source to event data, replacing any previous data. */
addSource(source: any): void {
addSource(source: EventSource): void {
this._data.source = source;
}
}
26 changes: 8 additions & 18 deletions source/event_hub.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// :copyright: Copyright (c) 2016 ftrack
import { v4 as uuidV4 } from "uuid";
import loglevel from "loglevel";
import * as io from "./socket.io-websocket-only.js";
import io from "./simple_socketio.js";
import { Event } from "./event.js";
import {
EventServerConnectionTimeoutError,
Expand Down Expand Up @@ -72,11 +72,11 @@ export type EventPayload =
| UpdateEventPayload;

export interface EventSource {
clientToken: string;
clientToken?: string;
applicationId: string;
user: {
username: string;
id: string;
id?: string;
};
id: string;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ export class EventHub {
};
private _unsentEvents: ConnectionCallback[];
private _subscribers: Subscriber[];
private _socketIo: io.SocketIO | null;
private _socketIo: io | null;

/**
* Construct EventHub instance with API credentials.
Expand Down Expand Up @@ -177,17 +177,7 @@ export class EventHub {

/** Connect to the event server. */
connect(): void {
this._socketIo = io.connect(this._serverUrl, {
"max reconnection attempts": Infinity,
"reconnection limit": 10000,
"reconnection delay": 5000,
transports: ["websocket"],
query: new URLSearchParams({
api_user: this._apiUser,
api_key: this._apiKey,
}).toString(),
});

this._socketIo = io.connect(this._serverUrl, this._apiUser, this._apiKey);
this._socketIo.on("connect", this._onSocketConnected);
this._socketIo.on("ftrack.event", this._handle);
}
Expand All @@ -197,7 +187,7 @@ export class EventHub {
* @return {Boolean}
*/
isConnected(): boolean {
return (this._socketIo && this._socketIo.socket.connected) || false;
return this._socketIo?.isConnected() || false;
}

/**
Expand Down Expand Up @@ -247,7 +237,7 @@ export class EventHub {
*
* If timeout is non-zero, the promise will be rejected if the event is not
* sent before the timeout is reached. Should be specified as seconds and
* will default to 10.
* will default to 30.
*
* @param {Event} event Event instance to publish
* @param {Function} [options.onReply] Function to be invoked when a reply
Expand Down Expand Up @@ -368,7 +358,7 @@ export class EventHub {
// Force reconnect socket if not automatically reconnected. This
// happens for example in Adobe After Effects when rendering a
// sequence takes longer than ~30s and the JS thread is blocked.
this._socketIo.socket.reconnect();
this._socketIo.reconnect();
}
} else {
callback();
Expand Down
Loading