Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the 3rd party assert lib #32

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@whereby/jslib-media",
"description": "Media library for Whereby",
"version": "1.4.0",
"version": "1.4.1",
"private": false,
"license": "MIT",
"homepage": "https://github.com/whereby/jslib-media",
Expand All @@ -20,7 +20,6 @@
"format:check": "prettier --check './**/*.{js,ts,yml,json}'"
},
"dependencies": {
"assert": "^2.0.0",
"events": "^3.3.0",
"mediasoup-client": "3.6.100",
"rtcstats": "github:whereby/rtcstats#v5.3.0",
Expand Down
3 changes: 1 addition & 2 deletions src/model/RtcStream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from "assert";

import { TYPES as CONNECTION_STATUS_TYPES } from "./connectionStatusConstants";
import assert from "../utils/assert";
const CAMERA_STREAM_ID = "0";

export const STREAM_TYPES = {
Expand Down
75 changes: 75 additions & 0 deletions src/utils/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Source: https://github.com/browserify/commonjs-assert
// License: MIT

class AssertionError extends Error {
constructor(options) {
super(options.message);

this.name = "AssertionError";
this.code = "ERR_ASSERTION";
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;

if (Error.captureStackTrace) {
Error.captureStackTrace(this, options.stackStartFn);
}
}
}

function innerOk(fn, argLen, value, message) {
if (!value) {
let generatedMessage = false;

if (argLen === 0) {
generatedMessage = true;
message = "No value argument passed to `assert.ok()`";
} else if (message instanceof Error) {
throw message;
}

const err = new AssertionError({
actual: value,
expected: true,
message,
operator: "==",
stackStartFn: fn,
});
err.generatedMessage = generatedMessage;
throw err;
}
}

function innerFail(obj) {
if (obj.message instanceof Error) throw obj.message;

throw new AssertionError(obj);
}

// Pure assertion tests whether a value is truthy, as determined
// by !!value.
function ok(...args) {
innerOk(ok, args.length, ...args);
}

const assert = ok;

assert.notEqual = function notEqual(actual, expected, message) {
if (arguments.length < 2) {
throw new Error("'actual' and 'expected' arguments are required");
}
// eslint-disable-next-line eqeqeq
if (actual == expected) {
innerFail({
actual,
expected,
message,
operator: "!=",
stackStartFn: notEqual,
});
}
};

assert.ok = ok;

module.exports = assert;
2 changes: 1 addition & 1 deletion src/webrtc/BaseRtcManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ServerSocket from "../utils/ServerSocket";
import * as webrtcBugDetector from "./bugDetector";
import rtcManagerEvents from "./rtcManagerEvents";
import Session from "./Session";
import assert from "assert";
import assert from "../utils/assert";
import rtcStats from "./rtcStatsService";
import { MAXIMUM_TURN_BANDWIDTH, MAXIMUM_TURN_BANDWIDTH_UNLIMITED, MEDIA_JITTER_BUFFER_TARGET } from "./constants";
import adapter from "webrtc-adapter";
Expand Down
3 changes: 1 addition & 2 deletions src/webrtc/MediaDevices.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from "assert";

import getConstraints from "./mediaConstraints";
import assert from "../utils/assert";

export const isMobile = /mobi/i.test(navigator.userAgent);

Expand Down
2 changes: 1 addition & 1 deletion src/webrtc/VegaRtcManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from "assert";
import { Device } from "mediasoup-client";
import { PROTOCOL_REQUESTS, PROTOCOL_RESPONSES } from "../model/protocol";
import * as CONNECTION_STATUS from "../model/connectionStatusConstants";
Expand All @@ -10,6 +9,7 @@ import VegaConnection from "./VegaConnection";
import { getMediaSettings, modifyMediaCapabilities } from "../utils/mediaSettings";
import { MEDIA_JITTER_BUFFER_TARGET } from "./constants";
import { getHandler } from "../utils/getHandler";
import assert from "../utils/assert";
import { v4 as uuidv4 } from "uuid";
import createMicAnalyser from "./VegaMicAnalyser";
import { maybeTurnOnly } from "../utils/transportSettings";
Expand Down
73 changes: 73 additions & 0 deletions tests/utils/assert.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import assert from "../../src/utils/assert";

describe("assert", () => {
describe("ok", () => {
it("should throw when value is not passed", () => {
expect(() => {
assert.ok();
}).toThrow("No value argument passed to `assert.ok()`");
});

it.each`
value | message
${false} | ${"false is passed"}
${0} | ${"0 is passed"}
${""} | ${"empty string is passed"}
${null} | ${"null is passed"}
${undefined} | ${"undefined is passed"}
`("should throw an error when $message", ({ value, message }) => {
expect(() => {
assert.ok(value, message);
}).toThrow();
});

it.each`
value | message
${true} | ${"true is passed"}
${1} | ${"number is passed"}
${"test"} | ${"string is passed"}
${[]} | ${"array is passed"}
${{}} | ${"object is passed"}
${() => {}} | ${"function is passed"}
`("should not throw an error when $message", ({ value, message }) => {
expect(() => {
assert.ok(value, message);
}).not.toThrow();
});
});

describe("notEqual", () => {
it("should throw when actual and expected are not passed", () => {
expect(() => {
assert.notEqual();
}).toThrow("'actual' and 'expected' arguments are required");
});

it.each`
actual | expected | message
${"test"} | ${"test"} | ${"strings are equal"}
${1} | ${1} | ${"numbers are equal"}
${true} | ${true} | ${"booleans are equal"}
${null} | ${null} | ${"nulls are equal"}
${undefined} | ${undefined} | ${"undefineds are equal"}
${null} | ${undefined} | ${"null and undefined are passed"}
`("should throw an error when $message", ({ actual, expected, message }) => {
expect(() => {
assert.notEqual(actual, expected, message);
}).toThrow();
});

it.each`
actual | expected | message
${"test"} | ${"test1"} | ${"strings are not equal"}
${1} | ${2} | ${"numbers are not equal"}
${true} | ${false} | ${"booleans are not equal"}
${[]} | ${[]} | ${"arrays are passed"}
${{}} | ${{}} | ${"objects are passed"}
`("should not throw an error when $message", ({ actual, expected, message }) => {
expect(() => {
assert.notEqual(actual, expected, message);
}).not.toThrow();
});
});
});
4 changes: 4 additions & 0 deletions tests/webrtc/MediaDevices.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe("stopStreamTracks", () => {
stream = helpers.createMockedMediaStream([videoTrack, audioTrack]);
});

it("should throw when only is not audio or video", () => {
expect(() => MediaDevices.stopStreamTracks(stream, "random-string")).toThrow();
});

it("should stop all tracks in the stream", () => {
MediaDevices.stopStreamTracks(stream);

Expand Down
56 changes: 56 additions & 0 deletions tests/webrtc/VegaRtcManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jest.mock("webrtc-adapter", () => {

import * as CONNECTION_STATUS from "../../src/model/connectionStatusConstants";
import VegaRtcManager from "../../src/webrtc/VegaRtcManager";
import { itShouldThrowIfMissing } from "../helpers";

const originalNavigator = global.navigator;
const originalMediasoupDevice = mediasoupClient.Device;
Expand Down Expand Up @@ -77,6 +78,61 @@ describe("VegaRtcManager", () => {
});
});

describe("constructor", () => {
const selfId = helpers.randomString("client-");
const room = { name: helpers.randomString("/room-"), iceServers: {} };

itShouldThrowIfMissing("selfId", () => {
//eslint-disable-next-line no-new
new VegaRtcManager({
room,
emitter,
serverSocket,
webrtcProvider,
});
});

itShouldThrowIfMissing("room", () => {
//eslint-disable-next-line no-new
new VegaRtcManager({
selfId,
emitter,
serverSocket,
webrtcProvider,
});
});

itShouldThrowIfMissing("emitter", () => {
//eslint-disable-next-line no-new
new VegaRtcManager({
selfId,
room,
serverSocket,
webrtcProvider,
});
});

itShouldThrowIfMissing("serverSocket", () => {
//eslint-disable-next-line no-new
new VegaRtcManager({
selfId,
room,
emitter,
webrtcProvider,
});
});

itShouldThrowIfMissing("webrtcProvider", () => {
//eslint-disable-next-line no-new
new VegaRtcManager({
selfId,
room,
emitter,
serverSocket,
});
});
});

describe("stopOrResumeVideo", () => {
let localStream;

Expand Down
Loading