Skip to content

Refactor, Bug fixes, and unit tests!#33

Open
JoTurk wants to merge 23 commits intodevfrom
ENG-650
Open

Refactor, Bug fixes, and unit tests!#33
JoTurk wants to merge 23 commits intodevfrom
ENG-650

Conversation

@JoTurk
Copy link
Contributor

@JoTurk JoTurk commented Apr 4, 2025

Changes:

General:

  • Remove the use of weak equality from the codebase (==, !=).
  • Throw instances of Error instead of strings or any primitive types / non-error objects.
  • Avoid iteration over strings using the string's length and charAt / index access, as this will not work for characters outside the BMP, because they are represented by two code units (JavaScript strings are UCS-2), use String iterator instead.
  • Remove a global setInterval (was used to check if the logger was used correctly! we can opt for a lint rule to enforce this instead).
  • Limit the use of any.

Stuff we should remove:

  • FixMap (tested): while the helpers save two lines of code, they add complexity of abstraction, and make the code unfamiliar to new developers.
  • BinaryWriter: It ignores offset and length if provided with a binary array, has a capacity doesn't handle overflow, the behavior isn't well defined, Can't we just use DataView and native binary arrays?
  • iterableEntries We can use a polyfill instead (I'll do this in another PR)!

Queues:

  • Fix a bug where push would ignore queues without capacity.
  • Made _queue protected to optimize the Numbers subclass (single iteration to reset max-min).

Numbers:

  • Fix minimum and maximum stats. (Minimum was always 0) properly reset minimum and maximum, we set them to NaN internally but we return 0 to keep the behavior (I think we should return NaN or infinity/-infinity).

BitReader.ts

  • Fix a memory overflow with values close to 32 bits!

BitRate:

  • Reworked BitRate using a slicing window algorithm to fix and simplify the implementation (the rate was wrong).

Utils.ts:

Bug fixes:

  • fetch: always use await text() to get the response body, to make sure the block is consistent (always async).
  • timeOrigin: Correctly calculate and hold the time origin, instead of returing a new value every time!

Improvements:

  • Fetch: use globalThis.fetch instead of self.fetch.
  • Fetch: Check for response.ok instead of response.status >= 300 to better communicate the intent of resolving ready responses.
  • Fetch: throw Error instead of strings.

Major Refactor:

  • equal: Reworked the function to fix some issues and improve readability:
    • Fixed a bug where NaN was not compared correctly.
    • Fixed a bug where some primitives were not compared when comparing arrays.
    • Added support for comparing objects with primitive values.
    • Compare iterators by their iterator values instead of relaying on length and Symbol.iterator on the same time.
    • Cleaned up the code and fixed some edge cases, by using the standard sameValueZero algorithm
      https://tc39.es/ecma262/multipage/abstract-operations.html#sec-samevaluenonnumber
  • safePromise:
    • Use ReturnType<typeof setTimeout> instead of NodeJS.Timeout.
    • Added Promise<T> to the function signature to make it more explicit.
    • Convert the function to async to make it easier to read.
    • Reject with a real Error instance instead of a string.
    • Avoid setting a timeout if the promise already resolved.
  • trim, trimStart, trimEnd:
    • Use String.prototype.trim, String.prototype.trimStart, String.prototype.trimEnd If available, and if no custom characters are provided.
    • Make it work for characters outside the BMP.
    • Trim all whitespace characters and line terminators by default instead of just ASCII characters (0x20) to match the behavior of String.prototype.trim.

Logs

  • Logs has one main problem, because how we do _bind we can't change the log level after it was constructed, and I'm not sure if we can change this behavior also we can't change local log level, I added tests but i commented it out!

future planning:

  1. parsing SDP and toString output should be reversible by the (note: additional work is needed to make this happen, the test unit was disabled).
@JoTurk JoTurk changed the base branch from main to dev April 4, 2025 17:09
@JoTurk JoTurk marked this pull request as draft April 4, 2025 17:53
error = await response.text();
}
throw (error || response.statusText || response.status).toString();
const response = await globalThis.fetch(input, init);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MathieuPOUX We can simply include a polyfill during the build, there's no need for us to maintain support for older browsers manually ourselves :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add it to this PR!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it'll simplify some stuff like trimStart trimEnd

@JoTurk JoTurk changed the title Small refactor, Bug fixes, and unit tests! Refactor, Bug fixes, and unit tests! Apr 8, 2025
@JoTurk JoTurk marked this pull request as ready for review April 8, 2025 19:49
@JoTurk JoTurk force-pushed the ENG-650 branch 2 times, most recently from a8e9a91 to eb61d4c Compare April 10, 2025 17:59
@JoTurk
Copy link
Contributor Author

JoTurk commented Jul 10, 2025

closing this PR and leaving it as a reference for cherry-picking.

@JoTurk JoTurk closed this Jul 10, 2025
@MathieuPOUX MathieuPOUX reopened this Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants