Skip to content

Commit

Permalink
feat: HomeAssistant support: various WebSocket and XHR tweaks (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect authored Aug 21, 2024
1 parent 3858718 commit d66f27b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
15 changes: 15 additions & 0 deletions src/http/ziti-dummy-websocket-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ZitiDummyWebSocketWrapper extends EventEmitter {
static OPEN = 1;
static CLOSING = 2;
static CLOSED = 3;
static DONE = 4;

/**
* Create a new `ZitiDummyWebSocketWrapper`.
Expand All @@ -38,6 +39,16 @@ class ZitiDummyWebSocketWrapper extends EventEmitter {

super();

/**
* Constants
*/

this.CONNECTING = 0;
this.OPEN = 1;
this.CLOSING = 2;
this.CLOSED = 3;
this.DONE = 4;

this.address = address;

// Hack for ScadaLTS web app
Expand Down Expand Up @@ -157,6 +168,10 @@ class ZitiDummyWebSocketWrapper extends EventEmitter {
this.addListener(type, listener);
}

removeEventListener(type, listener) {
this.removeListener(type, listener);
}

/**
*
*/
Expand Down
40 changes: 27 additions & 13 deletions src/http/ziti-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { isEqual, isUndefined } from "lodash-es";
import { isEqual, isNull, isUndefined } from "lodash-es";
import { ZitiProgressEventWrapper } from './ziti-event-wrapper';

function ZitiXMLHttpRequest () {
Expand Down Expand Up @@ -100,6 +100,7 @@ function ZitiXMLHttpRequest () {

// Result & response
this.responseBodyText = "";
this.responseText = "";
this.responseXML = "";
this.status = null;
this.statusText = null;
Expand Down Expand Up @@ -160,7 +161,7 @@ function ZitiXMLHttpRequest () {
throw new Error("SecurityError: Request method not allowed");
}

settings = {
this.settings = {
"method": method,
"url": url.toString(),
"async": (typeof async !== "boolean" ? true : async),
Expand All @@ -169,8 +170,8 @@ function ZitiXMLHttpRequest () {
};

// Hack for ScadaLTS web app
if (settings.url.includes(':undefined')) {
settings.url = settings.url.replace(':undefined', '');
if (this.settings.url.includes(':undefined')) {
this.settings.url = this.settings.url.replace(':undefined', '');
}

setState(this.OPENED);
Expand Down Expand Up @@ -281,9 +282,9 @@ function ZitiXMLHttpRequest () {

this.dispatchEvent("loadstart");

settings.body = data;
this.settings.body = data;

settings.headers = headers;
this.settings.headers = headers;

await window.zitiBrowzerRuntime.awaitInitializationComplete();

Expand All @@ -297,22 +298,29 @@ function ZitiXMLHttpRequest () {
// debugger
// }
// }
if (!settings.url.startsWith('/')) {
url = new URL(settings.url, `https://${window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host}`);
if (!this.settings.url.startsWith('/')) {
url = new URL(this.settings.url, `https://${window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host}`);
targetHost = url.hostname;
} else {
url = new URL(`https://${window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host}${settings.url}`);
url = new URL(`https://${window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host}${this.settings.url}`);
targetHost = window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host;
}
if (isEqual(targetHost, window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host)) {
let protocol = url.protocol;
if (!isEqual(protocol, 'https:')) {
url.protocol = 'https:';
settings.url = url.toString();
this.settings.url = url.toString();
}
} else {
url = new URL(`${this.settings.url}`);
let service = await window.zitiBrowzerRuntime.zitiContext.getServiceNameByHostNameAndPort(url.hostname, null);
if (!isNull(service)) {
url = new URL(`https://${service}${url.pathname}${url.search}`);
this.settings.url = url.toString();
}
}

response = await fetch(settings.url, settings);
response = await fetch(this.settings.url, this.settings);

this.status = response.status;
this.statusText = (response.status == 200) ? 'OK' : '';
Expand All @@ -328,7 +336,7 @@ function ZitiXMLHttpRequest () {
self.responseText = self.responseBodyText;
self.response = self.responseBodyText;
self.responseXML = self.responseBodyText;
self.responseURL = settings.url;
self.responseURL = self.settings.url;
self.responseType = '';
}

Expand Down Expand Up @@ -456,7 +464,7 @@ function ZitiXMLHttpRequest () {
if (state == self.LOADING || self.readyState !== state) {
self.readyState = state;

if (settings.async || self.readyState < self.OPENED || self.readyState === self.DONE) {
if (self.settings.async || self.readyState < self.OPENED || self.readyState === self.DONE) {
self.dispatchEvent("readystatechange");
}

Expand All @@ -478,6 +486,12 @@ function ZitiXMLHttpRequest () {
}
}
};

this.upload = {
addEventListener: function(arg1, arg2, arg3) {},
removeEventListener: function(arg1, arg2, arg3) {},
}

};


Expand Down
18 changes: 7 additions & 11 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,18 +863,14 @@ class ZitiBrowzerRuntime {

synchronousXHREncounteredEventHandler(syncXHREvent) {

this.logger.trace(`synchronousXHREncounteredEventHandler() `, syncXHREvent);

/**
* Bypass this for now
*/
// this.logger.trace(`synchronousXHREncounteredEventHandler() `, syncXHREvent);

// window.zitiBrowzerRuntime.browzer_error({
// status: 503,
// code: ZBR_CONSTANTS.ZBR_ERROR_CODE_SYNC_XHR_ENCOUNTERED,
// title: `Ziti Service [${window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service}] is using SynchronousXHR.`,
// message: `This web application is incompatible with BrowZer.`
// });
window.zitiBrowzerRuntime.browzer_error({
status: 503,
code: ZBR_CONSTANTS.ZBR_ERROR_CODE_SYNC_XHR_ENCOUNTERED,
title: `Ziti Service [${window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service}] is using SynchronousXHR.`,
message: `This web application is currently incompatible with BrowZer. Please reach out on https://openziti.discourse.group for support.`
});

}

Expand Down

0 comments on commit d66f27b

Please sign in to comment.