Skip to content

Commit

Permalink
Outside of browser environment
Browse files Browse the repository at this point in the history
  • Loading branch information
sirknightj committed Oct 2, 2023
1 parent 67e6604 commit 12cbbec
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ module.exports = {
transform: {
'^.+\\.ts$': 'ts-jest',
},
testEnvironment: "jsdom",
testEnvironment: 'jsdom',
clearMocks: true,
};
62 changes: 36 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
"webpack-merge": "^4.2.2"
},
"dependencies": {
"Buffer": "^0.0.0",
"isomorphic-webcrypto": "^2.3.6",
"jsdom": "^20.0.0",
"json-schema": "^0.4.0",
"json5": "^2.2.3",
"tslib": "^1.10.0",
"ua-parser-js": "^1.0.35",
"ws": "^8.14.2",
"xml2js": "^0.5.0"
},
"overrides": {
Expand Down
26 changes: 26 additions & 0 deletions src/SignalingClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,30 @@ describe('SignalingClient', () => {
});
});
});

describe('outsideBrowser', () => {
it('parseJSONObjectFromBase64String', done => {
global.atob = undefined;
const client = new SignalingClient(config as SignalingClientConfig);
client.once('sdpAnswer', (sdpAnswer, _) => {
expect(sdpAnswer).toEqual(SDP_ANSWER_OBJECT);
done();
});
client.once('open', () => {
MockWebSocket.instance.emit('message', { data: SDP_ANSWER_MASTER_MESSAGE });
});
client.open();
});

it('serializeJSONObjectAsBase64String', done => {
global.btoa = undefined;
const client = new SignalingClient(config as SignalingClientConfig);
client.open();
client.on('open', () => {
client.sendSdpOffer(SDP_OFFER);
expect(MockWebSocket.instance.send).toHaveBeenCalledWith(SDP_OFFER_VIEWER_STRING);
done();
});
});
});
});
21 changes: 15 additions & 6 deletions src/SignalingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export class SignalingClient extends EventEmitter {
return;
}

this.websocket = new WebSocket(signedURL);
/* istanbul ignore next */
this.websocket = new (global.WebSocket || require('ws'))(signedURL);

this.websocket.addEventListener('open', this.onOpen);
this.websocket.addEventListener('message', this.onMessage);
Expand Down Expand Up @@ -166,7 +167,7 @@ export class SignalingClient extends EventEmitter {
* @param {string} [recipientClientId] - ID of the client to send the message to. Required for 'MASTER' role. Should not be present for 'VIEWER' role.
*/
public sendSdpOffer(sdpOffer: RTCSessionDescription, recipientClientId?: string): void {
this.sendMessage(MessageType.SDP_OFFER, sdpOffer.toJSON(), recipientClientId);
this.sendMessage(MessageType.SDP_OFFER, sdpOffer, recipientClientId);
}

/**
Expand All @@ -177,7 +178,7 @@ export class SignalingClient extends EventEmitter {
* @param {string} [recipientClientId] - ID of the client to send the message to. Required for 'MASTER' role. Should not be present for 'VIEWER' role.
*/
public sendSdpAnswer(sdpAnswer: RTCSessionDescription, recipientClientId?: string): void {
this.sendMessage(MessageType.SDP_ANSWER, sdpAnswer.toJSON(), recipientClientId);
this.sendMessage(MessageType.SDP_ANSWER, sdpAnswer, recipientClientId);
}

/**
Expand All @@ -188,7 +189,7 @@ export class SignalingClient extends EventEmitter {
* @param {string} [recipientClientId] - ID of the client to send the message to. Required for 'MASTER' role. Should not be present for 'VIEWER' role.
*/
public sendIceCandidate(iceCandidate: RTCIceCandidate, recipientClientId?: string): void {
this.sendMessage(MessageType.ICE_CANDIDATE, iceCandidate.toJSON(), recipientClientId);
this.sendMessage(MessageType.ICE_CANDIDATE, iceCandidate, recipientClientId);
}

/**
Expand Down Expand Up @@ -266,14 +267,22 @@ export class SignalingClient extends EventEmitter {
* Takes the given base64 encoded string and decodes it into a JSON object.
*/
private static parseJSONObjectFromBase64String(base64EncodedString: string): object {
return JSON.parse(atob(base64EncodedString));
try {
return JSON.parse(atob(base64EncodedString));
} catch (e) {
return JSON.parse(Buffer.from(base64EncodedString, 'base64').toString());
}
}

/**
* Takes the given JSON object and encodes it into a base64 string.
*/
private static serializeJSONObjectAsBase64String(object: object): string {
return btoa(JSON.stringify(object));
try {
return btoa(JSON.stringify(object));
} catch (e) {
return Buffer.from(JSON.stringify(object)).toString('base64');
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion webpack.dev.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const merge = require('webpack-merge');
const webpack = require('webpack');

module.exports = merge.smart(require('./webpack.config'), {
mode: 'development',
Expand All @@ -11,7 +12,7 @@ module.exports = merge.smart(require('./webpack.config'), {

devServer: {
static: {
directory: path.join(__dirname, "examples")
directory: path.join(__dirname, 'examples'),
},
devMiddleware: {
publicPath: '/',
Expand Down

0 comments on commit 12cbbec

Please sign in to comment.