Skip to content

Commit

Permalink
fix: handle channel not found (#72)
Browse files Browse the repository at this point in the history
* feat: get client method to get client data

* refactor: no longer importing node fetch

* feat: get client response body type

* fix: handle channel not found and update event names

* refactor: change peer event names

* doc: add get client method information

* refactor: CHANNEL_CLOSED with reasons data

* refactor: change peer event names

* refactor: change closed to peer_closed event

* refactor: move to dev dependencies
  • Loading branch information
faiq-naufal authored Nov 2, 2023
1 parent ceb43a2 commit 6408d15
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 83 deletions.
56 changes: 16 additions & 40 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"mocha": "^10.0.0",
"mocha-explorer-launcher-scripts": "^0.4.0",
"nock": "^13.2.9",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
Expand All @@ -59,18 +60,17 @@
"rollup-plugin-dts": "^6.0.1",
"semantic-release": "^21.0.1",
"sinon": "^14.0.0",
"tsc-files": "^1.1.3",
"typescript": "^5.2.2"
},
"dependencies": {
"@fingerprintjs/fingerprintjs": "^3.4.0",
"camelcase-keys": "^8.0.2",
"lit": "^2.6.1",
"lodash-es": "^4.17.21",
"mocha-explorer-launcher-scripts": "^0.4.0",
"node-fetch": "^2.6.1",
"shaka-player": "^4.3.4",
"snakecase-keys": "^5.4.4",
"tsc-files": "^1.1.3"
"snakecase-keys": "^5.4.4"
},
"engines": {
"node": " >=14.16 || >=16.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/room/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ await room.endRoom(roomData.data.roomId);

A method to create and register a new client to the room. It expects two parameters. The `roomId` is required. The second parameter is an optional config to set a custom client data. This method will return a promise.

- `room.getClient(roomId: string, clientId: string)`

A method to get the client data. It expects a `roomId` and `clientId` as parameters. This method will return a promise.

- `room.setClientName(roomId: string, clientId: string, clientName: string)`

A method to set a client name based on `clientId`. This is useful for setting a friendly name or label on a specific client. It requires `roomId`, `clientId` and `clientName` parameters to be set. This method will return a promise.
Expand Down
16 changes: 16 additions & 0 deletions packages/room/api/api-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ export declare namespace RoomAPIType {
}
}

type GetClientResponseBody = BaseResponseBody & {
data: {
id: string
name: string
peer_connection_state: RTCPeerConnectionState
ice_peer_connection_state: RTCIceConnectionState
events: {
[key: string]: {
name: string
timestamp: number
data: { [key: string]: string | null }
}
}
}
}

type SetClientNameResponse = BaseResponseBody & {
data: {
client_id: string
Expand Down
36 changes: 36 additions & 0 deletions packages/room/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,41 @@ export const createApi = ({ fetcher }) => {
return client
}

/**
* @param {string} roomId
* @param {string} clientId
*/
getClient = async (roomId, clientId) => {
if (roomId.trim().length === 0) {
throw new Error('Room ID must be a valid string')
}

if (clientId.trim().length === 0) {
throw new Error('Client ID must be a valid string')
}

/** @type {import('./api-types.js').RoomAPIType.GetClientResponseBody} */
const response = await this._fetcher.get(
`/rooms/${roomId}/client/${clientId}`
)

const data = response.data || {}
const events = data.events || {}

return {
code: response.code || 500,
ok: response.ok || false,
message: response.message || '',
data: {
clientId: data.id || '',
clientName: data.name || '',
connectionState: data.peer_connection_state || '',
iceConnectionState: data.ice_peer_connection_state || '',
events: events,
},
}
}

/**
*
* @param {string} roomId
Expand Down Expand Up @@ -460,6 +495,7 @@ export const createApi = ({ fetcher }) => {
createRoom: api.createRoom,
getRoom: api.getRoom,
registerClient: api.registerClient,
getClient: api.getClient,
setClientName: api.setClientName,
sendIceCandidate: api.sendIceCandidate,
checkNegotiateAllowed: api.checkNegotiateAllowed,
Expand Down
15 changes: 0 additions & 15 deletions packages/room/api/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import nodeFetch, {
Headers as NodeHeaders,
Request as NodeRequest,
Response as NodeResponse,
} from 'node-fetch'

if (!('fetch' in globalThis)) {
Object.assign(globalThis, {
fetch: nodeFetch,
Headers: NodeHeaders,
Request: NodeRequest,
Response: NodeResponse,
})
}

export const createFetcher = () => {
const Fetcher = class {
_baseUrl
Expand Down
8 changes: 4 additions & 4 deletions packages/room/bandwidth-controller/bandwidth-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export class BandwidthController {
this.#statsInterval = null
this.#internalDataChannel = null

this.#event.on(PeerEvents.PEER_CONNECTED, this.#onPeerConnected)
this.#event.on(PeerEvents.PEER_DISCONNECTED, this.#onPeerDisconnected)
this.#event.on(PeerEvents.PEER_OPENED, this.#onPeerOpened)
this.#event.on(PeerEvents.PEER_CLOSED, this.#onPeerClosed)
this.#event.on(
PeerEvents._INTERNAL_DATACHANNEL_AVAILABLE,
this.#onInternalDataChannelAvailable
)
}

#onPeerConnected = () => {
#onPeerOpened = () => {
if (this.#statsInterval) {
clearInterval(this.#statsInterval)
this.#statsInterval = null
Expand All @@ -44,7 +44,7 @@ export class BandwidthController {
this.#statsInterval = setInterval(this.#updateStats, 3000)
}

#onPeerDisconnected = () => {
#onPeerClosed = () => {
if (this.#statsInterval) {
clearInterval(this.#statsInterval)
this.#statsInterval = null
Expand Down
4 changes: 2 additions & 2 deletions packages/room/channel/channel-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export declare namespace RoomChannelType {
}

type ChannelEvents = {
CHANNEL_CONNECTED: 'channelConnected'
CHANNEL_DISCONNECTED: 'channelDisconnected'
CHANNEL_OPENED: 'channelOpened'
CHANNEL_CLOSED: 'channelClosed'
}

type TrackSource = {
Expand Down
Loading

0 comments on commit 6408d15

Please sign in to comment.