Skip to content

Commit 9918e64

Browse files
refactor: code
1 parent a1b5c5a commit 9918e64

File tree

5 files changed

+11
-36
lines changed

5 files changed

+11
-36
lines changed

client-src/utils/createSocketURL.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ function createSocketURL(parsedURL) {
3232
hostname = self.location.hostname;
3333
}
3434

35+
if (!hostname && protocol === 'file:') {
36+
hostname = 'localhost';
37+
}
38+
3539
// `hostname` can be empty when the script path is relative. In that case, specifying a protocol would result in an invalid URL.
3640
// When https is used in the app, secure websockets are always necessary because the browser doesn't accept non-secure websockets.
3741
if (hostname && isInAddrAny && self.location.protocol === 'https:') {

client-src/utils/parseURL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function parseURL(resourceQuery) {
2929
// is to allow parsing of path-relative or protocol-relative URLs,
3030
// and will have no effect if `scriptSource` is a fully valid URL.
3131
scriptSourceURL = new URL(scriptSource, self.location.href);
32-
} catch (e) {
32+
} catch (error) {
3333
// URL parsing failed, do nothing.
3434
// We will still proceed to see if we can recover using `resourceQuery`
3535
}

test/client/clients/SockJSClient.test.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,6 @@ describe('SockJSClient', () => {
7474
done();
7575
}, 3000);
7676
});
77-
78-
it('should change the protocol from chrome-extension to http', (done) => {
79-
const client = new SockJSClient('chrome-extension://localhost');
80-
81-
expect(client.sock.url).toEqual('http://localhost');
82-
83-
done();
84-
});
85-
86-
it('should change the protocol from file to http', (done) => {
87-
const client = new SockJSClient('file://localhost');
88-
expect(client.sock.url).toEqual('http://localhost');
89-
90-
done();
91-
});
9277
});
9378

9479
afterAll((done) => {

test/client/clients/WebsocketClient.test.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('WebsocketClient', () => {
4242
}, 1000);
4343
});
4444

45-
const client = new WebsocketClient(`http://localhost:${port}/ws-server`);
45+
const client = new WebsocketClient(`ws://localhost:${port}/ws-server`);
4646
const data = [];
4747

4848
client.onOpen(() => {
@@ -68,22 +68,6 @@ describe('WebsocketClient', () => {
6868
done();
6969
}, 3000);
7070
});
71-
72-
it('should change the protocol from chrome-extension to http', (done) => {
73-
const client = new WebsocketClient('chrome-extension://localhost/');
74-
75-
expect(client.client.url).toEqual('ws://localhost/');
76-
77-
done();
78-
});
79-
80-
it('should change the protocol from file to http', (done) => {
81-
const client = new WebsocketClient('file://localhost/');
82-
83-
expect(client.client.url).toEqual('ws://localhost/');
84-
85-
done();
86-
});
8771
});
8872

8973
afterAll((done) => {

test/client/utils/createSocketURL.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,20 @@ describe("'createSocketURL' function ", () => {
111111
['?https://[::1]:8080/', 'http://[::1]:8080/', 'wss://[::1]:8080/ws'],
112112
[null, 'http://[::1]:8080/', 'ws://[::1]:8080/ws'],
113113
[null, 'https://[::1]:8080/', 'wss://[::1]:8080/ws'],
114-
[null, 'file:///home/user/project/index.html', 'ws:/ws'],
114+
[null, 'file:///home/user/project/index.html', 'ws://localhost/ws'],
115+
[null, 'chrome-extension://localhost/', 'ws://localhost/ws'],
116+
[null, 'file://localhost/', 'ws://localhost/ws'],
115117
];
116118

117119
samples.forEach(([__resourceQuery, location, expected]) => {
118120
jest.doMock('../../../client-src/utils/getCurrentScriptSource', () => () =>
119-
location
121+
new URL('./entry.js', location).toString()
120122
);
121123

122124
const createSocketURL = require('../../../client-src/utils/createSocketURL');
123125
const parseURL = require('../../../client-src/utils/parseURL');
124126

125-
test.only(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
127+
test(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
126128
const selfLocation = new URL(location);
127129

128130
delete window.location;

0 commit comments

Comments
 (0)