Skip to content

Commit

Permalink
fix: resolve protocol (#1)
Browse files Browse the repository at this point in the history
* fix: resolve protocol

Close #3
  • Loading branch information
wqcstrong committed May 24, 2023
1 parent e47a2fc commit 4fbc609
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 757 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"build": "rollup -c",
"postbuild": "cp -R ./types ./dist",
"build:watch": "rollup -c -w",
"pretest": "pm2 restart tests/server/index.js",
"test": "jest --silent",
"posttest": "pm2 stop tests/server/index.js",
"lint": "eslint --ext .js,.ts ./src",
"lint:fix": "eslint --ext .js,.ts --fix ./src",
"prettier": "prettier --write '**/*.{js,jsx,tsx,ts,md,json}'",
Expand Down Expand Up @@ -76,7 +74,6 @@
"jsdom": "^21.1.1",
"less": "^4.1.1",
"lint-staged": "^10.5.4",
"pm2": "^5.3.0",
"postcss": "^8.4.21",
"prettier": "^2.2.1",
"rollup": "^3.10.0",
Expand Down
25 changes: 15 additions & 10 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@ interface TCreateRoom {
tags: Record<string, any>;
}
/* c8 ignore start */
const resolveProtocol = () => {
const { protocol } = new URL(document.currentScript?.baseURI || '');
if (protocol.startsWith('https')) {
return ['https://', 'wss://'];
const resolvedProtocol = (() => {
try {
const { protocol } = new URL(document.currentScript?.getAttribute('src')!);
if (protocol.startsWith('https')) {
return ['https://', 'wss://'];
}
} catch (e) {
console.error(e);
console.error(
'[PageSpy] Failed to resolve the protocol and fallback to [http://, ws://]',
);
}
return ['http://', 'ws://'];
};
})();

/* c8 ignore stop */
export default class Request {
protocol: string[] = [];

constructor(public base: string = '') {
/* c8 ignore next 3 */
if (!base) {
throw Error('The api base url cannot be empty');
}
this.protocol = resolveProtocol();
}

createRoom(project: string): Promise<TResponse<TCreateRoom>> {
const device = parseUserAgent();
const name = combineName(device);
return fetch(
`${this.protocol[0]}${this.base}/api/v1/room/create?name=${name}&group=${project}`,
`${resolvedProtocol[0]}${this.base}/api/v1/room/create?name=${name}&group=${project}`,
{
method: 'POST',
},
Expand All @@ -61,6 +66,6 @@ export default class Request {
}
return acc + kv;
}, '');
return `${this.protocol[1]}${this.base}/api/v1/ws/room/join?${params}`;
return `${resolvedProtocol[1]}${this.base}/api/v1/ws/room/join?${params}`;
}
}
4 changes: 4 additions & 0 deletions tests/plugins/network.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import NetworkPlugin from 'src/plugins/network';
import startServer from '../server/index';

const stopServer = startServer();

const {
open: originOpen,
Expand All @@ -17,6 +20,7 @@ afterEach(() => {
window.fetch = originFetch;
window.navigator.sendBeacon = originSendBeacon;
});
afterAll(stopServer);

describe('Network plugin', () => {
it('Wrap XMLHttpRequest prototype and add `onreadystatechange` on instance', (done) => {
Expand Down
16 changes: 11 additions & 5 deletions tests/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ app.get('/posts/:id', (_, res) => {
res.send(data[0]);
});

try {
app.listen(6677, () => {
const startServer = () => {
const server = app.listen(6677, () => {
console.log('Test server is RUNNING at http://localhost:6677');
});
} catch (e) {
console.log(`Test server start failed. \n${e.message}`);
}
server.unref();
return () => {
server.close((err) => {
console.log('Http server closed.');
});
};
};

module.exports = startServer;
Loading

0 comments on commit 4fbc609

Please sign in to comment.