Skip to content

Commit 163bdce

Browse files
authored
fix: support file: and chrome-extension: protocols in client (#2954)
1 parent ca9e45d commit 163bdce

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

client-src/clients/SockJSClient.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const BaseClient = require('./BaseClient');
77
module.exports = class SockJSClient extends BaseClient {
88
constructor(url) {
99
super();
10-
this.sock = new SockJS(url);
10+
const sockUrl = url.replace(/^(?:chrome-extension|file)/i, 'http');
11+
this.sock = new SockJS(sockUrl);
1112

1213
this.sock.onerror = (err) => {
1314
log.error(err);

client-src/clients/WebsocketClient.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const BaseClient = require('./BaseClient');
88
module.exports = class WebsocketClient extends BaseClient {
99
constructor(url) {
1010
super();
11-
this.client = new WebSocket(url.replace(/^http/, 'ws'));
11+
const wsUrl = url.replace(/^(?:http|chrome-extension|file)/i, 'ws');
12+
this.client = new WebSocket(wsUrl);
1213

1314
this.client.onerror = (err) => {
1415
log.error(err);

test/client/clients/SockJSClient.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ describe('SockJSClient', () => {
7272
done();
7373
}, 3000);
7474
});
75+
it('should change the protocol from chrome-extension to http', (done) => {
76+
const client = new SockJSClient('chrome-extension://localhost');
77+
expect(client.sock.url).toEqual('http://localhost');
78+
done();
79+
});
80+
it('should change the protocol from file to http', (done) => {
81+
const client = new SockJSClient('file://localhost');
82+
expect(client.sock.url).toEqual('http://localhost');
83+
done();
84+
});
7585
});
7686

7787
afterAll((done) => {

test/client/clients/WebsocketClient.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ describe('WebsocketClient', () => {
6565
done();
6666
}, 3000);
6767
});
68+
it('should change the protocol from chrome-extension to http', (done) => {
69+
const client = new WebsocketClient('chrome-extension://localhost/');
70+
expect(client.client.url).toEqual('ws://localhost/');
71+
done();
72+
});
73+
it('should change the protocol from file to http', (done) => {
74+
const client = new WebsocketClient('file://localhost/');
75+
expect(client.client.url).toEqual('ws://localhost/');
76+
done();
77+
});
6878
});
6979

7080
afterAll((done) => {

0 commit comments

Comments
 (0)