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

Upgrade to TypeScript #39

Merged
merged 20 commits into from
Dec 5, 2023
Merged

Upgrade to TypeScript #39

merged 20 commits into from
Dec 5, 2023

Conversation

sebjf
Copy link
Member

@sebjf sebjf commented Nov 24, 2023

This PR completes #37 - upgrading the server to TypeScript.

It seems Github does not support changing the source branch of a PR, so we need to make a new one. The initial changes were contributed by TheBv, whose branch https://github.com/UCL-VR/ubiq/tree/serverUpgrade1 is based off.

While performing the TypeScript upgrade we have taken the opportunity to make a number of improvements, and start new features to make the server more extensible.
Some features didn't get as far as I'd have liked, but at least the foundations are there (e.g. HTTP admin API). For others, the half completed support has been pulled completely leaving the code cleaner until it can be scoped properly (e.g. subclassing rooms for the scalability project).

In full, we have:

  1. Upgraded the Ubiq, Components and Modules sub-packages to TypeScript
  2. Updated the samples (logcollectorservice & propertiesservice) and made sure they work fully. The rtcpeerconnection sample has been deprecated and removed.
  3. Made sure everything is ESM, including the browser library; removed all CJS shims (e.g. for Rollup)
  4. Added a linter
  5. Updated the browser samples
  6. General cleanup (all of TheBv' FIXME comments and other things I spotted during the review), including the old room observation code.
  7. Added a new module that presents an HTTP API for checking the status and stats, with a dedicated endpoint for uptime monitoring
  8. Changed the schema validation library to zod, which has better integration with TypeScript
  9. Added a suite of unit tests based on Jest, encompassing so far the RoomServer and ICE module, and WebSocket connection

Over a couple of blog posts I'll detail the changes in more detail, but to overview the things that are important to get running:

ts-node

The project has been configured to use ts-node. That is, there is no additional build step or artefacts. But node must be provided the arguments --loader ts-node/esm app.ts, either on the command line or via NODE_OPTIONS.

This is the vscode launch config, for example,

        {
            "type": "node",
            "request": "launch",
            "name": "Launch Server",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "env": {"NODE_OPTIONS": "--loader ts-node/esm"},
            "program": "${workspaceFolder}\\app.ts",
            "console": "integratedTerminal"
        },

And the package.json has a start command added to it which does the same thing. (i.e. npm start)

Compatability

There are no changes to the actual server API, therefore once we have merged, the version on nexus should upgrade in place with the same port numbers transparently.

HTTPS/WSS

Just a reminder, since Chrome seems to be even more stringent nowadays, to use WebSockets, you need a local certificate and private key.

Create these with, e.g.

openssl req -nodes -new -x509 -keyout key.pem -out cert.pem

You will also likely need to visit the URI as an HTTP page so Chrome can show a warning about a mismatched hostname and you can accept it, before wss will work.

Status Module

The status module is in place but it only has a few endpoints at the moment. Hopefully now though it will be easier to upgrade. The status module runs its own HTTP server and a config entry has been added for it. It defaults to 8011. The API is intended to be consumed programmatically. We will need to add a small static page to display its results nicely, but being Json/JavaScript based its easy enough to read still.

rtcpeerconnection sample

This has been removed since wrtc seems out of development and has installation issues. Further our browser samples demonstrate using WebRTC in JavaScript now.

Browser Samples

The browser samples have already had this branch merged. (As that branch is actually an entire copy of the project, it doesn't need to be perfectly in sync at all times with master).
It now has just two (upstream/Node) changes, which we should merge back in here, right at the end:

  1. The NetworkContext holds its own networkId member, instead of assuming the object has it
  2. The LogCollector has a log member so it can be used in client-mode.
  3. The PeerConnectionImpl in Unity exposes the Unity native PeerConnection object

Future Work

Things that ideally would be done in this PR but we didn't manage and its taking too long:

  1. Unit tests for the LogCollector, PeerConnectionManager and AvatarManager
  2. Change the samples (logcollectorservice and propertiesservice to TypeScript).

Larger future projects:

  1. Make the Room and RoomServer strucutre more extensible so that we can add back the observer and KNN code from the scalability samples, without needing to make any upstream changes. This will need both the RoomPeer and Room types to be subclassed (the mistake before was only doing this for the Room type). Now with TypeScript perhaps we can make RoomServer generic in some way?

  2. Make it possible to add 'services' at the same level as the RoomServer. For example, the Get/SetBlob APIs aren't great in their current form. This would be better moved out into a dedicated module, which would be straightforward if we can basically attach NetworkedComponents to a RoomPeer, as if they were attached to a NetworkScene, but before a Room was joined.

Outstanding things

  1. @bnco-dev I had to make a couple of changes to the WebRTC code (surprise!) to get it to work with the browser again. I don't think these should break anything elsewhere but it's worth you giving it a once over.
  2. @bnco-dev There is still one FIXME in ice.ts: I wasn't sure what this should do so I have left it, if you have time to fix before the merge?

@sebjf sebjf requested a review from bnco-dev November 24, 2023 12:19
@sebjf sebjf mentioned this pull request Nov 24, 2023
3 tasks
@sebjf
Copy link
Member Author

sebjf commented Nov 28, 2023

From the in-person discussion just now:

  • Rollback the Unity WebRtc negotiation state machine changes and see if the browser samples still work
  • Pull the last upstream changes from the browser samples

@sebjf
Copy link
Member Author

sebjf commented Nov 29, 2023

@bnco-dev the above has been done now, and the negotiation works with the browser still!

@sebjf
Copy link
Member Author

sebjf commented Dec 4, 2023

Additional jobs before this is done:

  • Ice Server should have unit test to make sure credentials are updated
  • Removed the modified case in unlink

@sebjf sebjf merged commit a31bfee into master Dec 5, 2023
1 check passed
@sebjf sebjf deleted the serverUpgrade1 branch December 7, 2023 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants