mirror of
https://github.com/vector-im/element-call.git
synced 2026-11-16 01:22:29 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
796e9ace82 | ||
|
|
3c8b381723 |
@@ -1,35 +0,0 @@
|
||||
---
|
||||
name: handoff
|
||||
description: Implement the first cut of a task and stop for a direction check before the quality pass.
|
||||
argument-hint: <the task>
|
||||
disable-model-invocation: true
|
||||
---
|
||||
|
||||
Task: $ARGUMENTS
|
||||
|
||||
Stop after the first implementation and hand it back before the quality pass.
|
||||
|
||||
## Before the hand-off
|
||||
|
||||
- Run the narrowest check that rules out an obviously broken hand-off: the test
|
||||
file for the touched code, `tsc` on its own, a single story. Not the full
|
||||
`pnpm lint`, not the whole suite.
|
||||
- If a wider check is needed before feedback can be useful, keep it to the touched
|
||||
code and say why it was needed.
|
||||
- Leave wider refactors, extra tests and documentation polish out. They are
|
||||
follow-up work, not part of the first hand-off.
|
||||
|
||||
## The hand-off
|
||||
|
||||
Stop and ask whether the direction is right. The message says:
|
||||
|
||||
- what was built and where, in a few lines,
|
||||
- which check ran and what it returned, verbatim if it failed,
|
||||
- what was left out on purpose,
|
||||
- the decisions that could have gone another way.
|
||||
|
||||
Do not continue past this point on your own.
|
||||
|
||||
## After the user confirms
|
||||
|
||||
The skill ends here. Continue with the quality pass as usual.
|
||||
@@ -7,7 +7,7 @@ on:
|
||||
type: string
|
||||
package:
|
||||
type: string # This would ideally be a `choice` type, but that isn't supported yet
|
||||
description: The package type to be built. Must be one of 'full', 'embedded', or 'widget-rtc-sdk'
|
||||
description: The package type to be built. Must be one of 'full', 'embedded', or 'sdk'
|
||||
required: true
|
||||
build_mode:
|
||||
type: string # This would ideally be a `choice` type, but that isn't supported yet
|
||||
|
||||
@@ -72,12 +72,14 @@ jobs:
|
||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
||||
|
||||
build_widget_rtc_sdk:
|
||||
# The widget wrapper around the sdk/ package (widget-rtc-sdk/), not the
|
||||
# package itself. Deployed by pr-deploy.yaml as the pr<N>-widget-rtc-sdk preview.
|
||||
build_sdk_element_call:
|
||||
# The 'sdk' package is the CallViewModel bundle that predates the sdk/
|
||||
# package (build:sdk:production, sdk-target-based-on-call-view-model/), not
|
||||
# the package itself. The name is shared with pr-deploy.yaml, which always
|
||||
# runs from main, so it stays until the bundle's consumers have moved over.
|
||||
uses: ./.github/workflows/build-element-call.yaml
|
||||
with:
|
||||
package: widget-rtc-sdk
|
||||
package: sdk
|
||||
vite_app_version: ${{ github.event.release.tag_name || github.sha }}
|
||||
build_mode: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'development build') && 'development' || 'production' }}
|
||||
secrets:
|
||||
|
||||
@@ -17,7 +17,7 @@ on:
|
||||
package:
|
||||
required: true
|
||||
type: string
|
||||
description: Which package to deploy - 'full', 'embedded', 'widget-rtc-sdk', or 'storybook'
|
||||
description: Which package to deploy - 'full', 'embedded', 'sdk', or 'storybook'
|
||||
artifact_run_id:
|
||||
required: false
|
||||
type: string
|
||||
@@ -64,27 +64,25 @@ jobs:
|
||||
run: curl -s https://raw.githubusercontent.com/element-hq/element-call/main/config/netlify_redirects > webapp/_redirects
|
||||
|
||||
- name: Add config file
|
||||
# Only the app reads a config.json beside it. A widget gets everything
|
||||
# about the homeserver from its host.
|
||||
if: inputs.package == 'full'
|
||||
run: curl -s "https://raw.githubusercontent.com/${INPUTS_PR_HEAD_FULL_NAME}/${INPUTS_PR_HEAD_REF}/config/config_netlify_preview.json" > webapp/config.json
|
||||
# Not needed for storybook deployments
|
||||
if: inputs.package != 'storybook'
|
||||
run: |
|
||||
if [ "${INPUTS_PACKAGE}" = "full" ]; then
|
||||
curl -s "https://raw.githubusercontent.com/${INPUTS_PR_HEAD_FULL_NAME}/${INPUTS_PR_HEAD_REF}/config/config_netlify_preview.json" > webapp/config.json
|
||||
else
|
||||
curl -s "https://raw.githubusercontent.com/${INPUTS_PR_HEAD_FULL_NAME}/${INPUTS_PR_HEAD_REF}/config/config_netlify_preview_sdk.json" > webapp/config.json
|
||||
fi
|
||||
env:
|
||||
INPUTS_PACKAGE: ${{ inputs.package }}
|
||||
INPUTS_PR_HEAD_FULL_NAME: ${{ inputs.pr_head_full_name }}
|
||||
INPUTS_PR_HEAD_REF: ${{ inputs.pr_head_ref }}
|
||||
- name: Add headers file
|
||||
# A widget page on another origin imports the widget RTC SDK bundle as a
|
||||
# module, which the browser only allows with CORS
|
||||
if: inputs.package == 'widget-rtc-sdk'
|
||||
run: |
|
||||
printf '/*\n Access-Control-Allow-Origin: *\n' > webapp/_headers
|
||||
|
||||
- name: ☁️ Deploy to Netlify
|
||||
id: netlify
|
||||
uses: nwtgck/actions-netlify@4cbaf4c08f1a7bfa537d6113472ef4424e4eb654 # v3.0
|
||||
with:
|
||||
publish-dir: webapp
|
||||
deploy-message: "Deploy from GitHub Actions"
|
||||
alias: ${{ inputs.package == 'widget-rtc-sdk' && format('pr{0}-widget-rtc-sdk', inputs.pr_number) || inputs.package == 'storybook' && format('pr{0}-storybook', inputs.pr_number) || format('pr{0}', inputs.pr_number) }}
|
||||
alias: ${{ inputs.package == 'sdk' && format('pr{0}-sdk', inputs.pr_number) || inputs.package == 'storybook' && format('pr{0}-storybook', inputs.pr_number) || format('pr{0}', inputs.pr_number) }}
|
||||
env:
|
||||
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
|
||||
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
|
||||
|
||||
@@ -46,7 +46,7 @@ jobs:
|
||||
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
|
||||
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
|
||||
|
||||
netlify-widget-rtc-sdk:
|
||||
netlify-sdk:
|
||||
needs: prdetails
|
||||
permissions:
|
||||
deployments: write
|
||||
@@ -57,7 +57,7 @@ jobs:
|
||||
pr_head_full_name: ${{ github.event.workflow_run.head_repository.full_name }}
|
||||
pr_head_ref: ${{ needs.prdetails.outputs.pr_data_json && fromJSON(needs.prdetails.outputs.pr_data_json).head.ref }}
|
||||
deployment_ref: ${{ needs.prdetails.outputs.pr_data_json && fromJSON(needs.prdetails.outputs.pr_data_json).head.sha || github.ref || github.head_ref }}
|
||||
package: widget-rtc-sdk
|
||||
package: sdk
|
||||
secrets:
|
||||
ELEMENT_BOT_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
|
||||
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
|
||||
|
||||
@@ -2,7 +2,6 @@ node_modules
|
||||
.DS_Store
|
||||
.env
|
||||
dist
|
||||
/dist-widget-rtc-sdk-example
|
||||
dist-ssr
|
||||
*.local
|
||||
*.bkp
|
||||
@@ -45,7 +44,3 @@ storybook-static
|
||||
|
||||
# Nested checkout made by scripts/layered.sh
|
||||
/matrix-js-sdk/
|
||||
|
||||
# Claude
|
||||
/.claude/*
|
||||
!/.claude/skills/
|
||||
|
||||
@@ -16,35 +16,19 @@ host's page. It is the MatrixRTC reference implementation.
|
||||
`@media`, global `i18next` or the `widget` global — take it from a provider.
|
||||
- The change works standalone, as a widget and as a component. Say what you checked.
|
||||
- Every gate below is green before you push.
|
||||
- Hand off after the first implementation, before the quality pass. Then commit on
|
||||
the user's word where the setup allows it, or hand them the message.
|
||||
|
||||
## Read before you
|
||||
|
||||
| … | … |
|
||||
| -------------------------------- | ---------------------------------------------------------- |
|
||||
| start a task of any size | [docs/agents/workflow.md](docs/agents/workflow.md) |
|
||||
| touch call logic or a view model | [docs/agents/architecture.md](docs/agents/architecture.md) |
|
||||
| write any code | [docs/agents/code-style.md](docs/agents/code-style.md) |
|
||||
| write a test, story or e2e spec | [docs/agents/testing.md](docs/agents/testing.md) |
|
||||
| open a PR | [CONTRIBUTING.md](CONTRIBUTING.md) |
|
||||
|
||||
## Scratch files live in `agent-workspace/`
|
||||
|
||||
Git-ignored. One kebab-case subfolder per task, matching the branch topic:
|
||||
`agent-workspace/<slug>/`.
|
||||
|
||||
| File | Purpose |
|
||||
| --------------------------- | -------------------------------------------------------------------------------- |
|
||||
| `plan.md` | The concrete approach — files, symbols, edit order — once direction is confirmed |
|
||||
| `commit-msg.txt` | `git commit -F agent-workspace/<slug>/commit-msg.txt` |
|
||||
| `pr-body.md` | `gh pr create --body-file agent-workspace/<slug>/pr-body.md` |
|
||||
| `implementation-summary.md` | What was built, decisions, trade-offs |
|
||||
| `NN-prompt.md` | Raw prompt text, numbered, when worth keeping |
|
||||
|
||||
- Nothing here is durable. The folder is ignored, so anything worth keeping is
|
||||
promoted into the PR body or a doc before the task ends.
|
||||
- Never write scratch files to the repo root. Stray `load_test_summary.md` and
|
||||
`config.json_` files are what this folder prevents.
|
||||
- Start `pr-body.md` from `.github/PULL_REQUEST_TEMPLATE.md` and fill every
|
||||
section.
|
||||
| implement from a feature spec | [FEATURES_SPEC/AGENTS.md](FEATURES_SPEC/AGENTS.md) |
|
||||
|
||||
## Gates
|
||||
|
||||
@@ -58,11 +42,6 @@ pnpm i18n:check
|
||||
`knip` fails on dead code; a file deliberately inert ahead of its consumer goes in
|
||||
`knip.ts` `ignoreFiles` with a reason.
|
||||
|
||||
Ready means every gate is green and the change is covered at the layers
|
||||
[testing.md](docs/agents/testing.md) asks for. Read the diff against
|
||||
[code-style.md](docs/agents/code-style.md) first. Re-run the whole checklist after
|
||||
any fix.
|
||||
|
||||
## A good PR
|
||||
|
||||
- One slice, under ~400 changed lines, green on its own, linked to its issue.
|
||||
|
||||
@@ -275,7 +275,11 @@ API is in `component/index.tsx`, `component/api.ts` and `component/host.ts`.
|
||||
|
||||
##### Develop
|
||||
|
||||
- `pnpm build:component` builds the library into `component/dist`.
|
||||
- `pnpm build:component` builds the library into `component/dist`. It ends
|
||||
with `scripts/check-component-bundle.mjs`, which fails the build on what a
|
||||
host must not be handed: an asset inlined as a data URL, a file referenced
|
||||
by an absolute path, the standalone app's client store worker, or a module
|
||||
over its size budget.
|
||||
- `pnpm dev:component` serves a test host on `https://localhost:3001`: two
|
||||
calls side by side in resizable boxes, with host messages logged at the
|
||||
bottom. It uses the development backend, `public/config.json` if present,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"default_server_config": {
|
||||
"m.homeserver": {
|
||||
"base_url": "https://call-unstable.ems.host",
|
||||
"server_name": "call-unstable.ems.host"
|
||||
}
|
||||
},
|
||||
"ssla": "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf",
|
||||
"matrix_rtc_session": {
|
||||
"membership_event_expiry_ms": 180000000,
|
||||
"network_error_retry_ms": 100
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ networks:
|
||||
|
||||
services:
|
||||
auth-service:
|
||||
image: ghcr.io/element-hq/lk-jwt-service:0.10.0
|
||||
image: ghcr.io/element-hq/lk-jwt-service:0.8.0
|
||||
pull_policy: always
|
||||
hostname: auth-server
|
||||
environment:
|
||||
@@ -33,7 +33,7 @@ services:
|
||||
- ecbackend
|
||||
|
||||
auth-service-1:
|
||||
image: ghcr.io/element-hq/lk-jwt-service:0.10.0
|
||||
image: ghcr.io/element-hq/lk-jwt-service:0.8.0
|
||||
pull_policy: always
|
||||
hostname: auth-server-1
|
||||
environment:
|
||||
|
||||
@@ -19,10 +19,10 @@ services:
|
||||
- ./backend/playwright_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z
|
||||
element-web:
|
||||
# Pin to a SHA so that upstream cannot break our tests. Renovate handles regular updates.
|
||||
image: ghcr.io/element-hq/element-web:develop@sha256:fd1c291a228374aff127ceb6eea3c20291b233623bbbd96dcae53eca679fd9d7
|
||||
image: ghcr.io/element-hq/element-web:develop@sha256:72791f0b2541208f8f7edd9c6b5a7eaf88accd8ed1f0c4db1bc62bfe18eb90fe
|
||||
element-web-1:
|
||||
# Pin to a SHA so that upstream cannot break our tests. Renovate handles regular updates.
|
||||
image: ghcr.io/element-hq/element-web:develop@sha256:fd1c291a228374aff127ceb6eea3c20291b233623bbbd96dcae53eca679fd9d7
|
||||
image: ghcr.io/element-hq/element-web:develop@sha256:72791f0b2541208f8f7edd9c6b5a7eaf88accd8ed1f0c4db1bc62bfe18eb90fe
|
||||
volumes:
|
||||
# The other site opens the embedded package under test in place of the
|
||||
# copy its image bundles (the first site opens the dev server's app.
|
||||
|
||||
+1
-5
@@ -14,11 +14,7 @@ This folder contains documentation for setup, usage, and development of Element
|
||||
|
||||
Entry point: [AGENTS.md](../AGENTS.md).
|
||||
|
||||
- [Agent workflow](./agents/workflow.md)
|
||||
- [Architecture](./agents/architecture.md)
|
||||
- [Code style](./agents/code-style.md)
|
||||
- [Testing](./agents/testing.md)
|
||||
|
||||
Opt-in skills in [`.claude/skills/`](../.claude/skills/):
|
||||
|
||||
- `/handoff <task>` — get the first implementation back for a direction check
|
||||
before lint, the full suite and polish.
|
||||
|
||||
@@ -76,11 +76,11 @@ Code that builds in only one is a bug.
|
||||
Packaged exactly like the component (own pnpm project, run pnpm from the repo
|
||||
root, types from `sdk/tsconfig.build.json`, externals checked by
|
||||
`pnpm lint:externals`). Interface only so far; the design is `sdk-plan.md`.
|
||||
- `build:widget-rtc-sdk` — a single bundle for widget pages that wraps the
|
||||
`sdk/` package and the widget API, nothing of `src/`; entry
|
||||
`widget-rtc-sdk/main.ts`. CI builds it as the `widget-rtc-sdk` package
|
||||
(`build:widget-rtc-sdk:production` / `:development`) and deploys it as the
|
||||
`pr<N>-widget-rtc-sdk` Netlify preview.
|
||||
- `build:sdk-target-based-on-call-view-model` — the SDK demo that predates the
|
||||
package, one bundle of the `CallViewModel`; entry
|
||||
`sdk-target-based-on-call-view-model/main.ts`. CI's `sdk` package
|
||||
(`build:sdk:production` / `build:sdk:development`, the Netlify `-sdk` preview)
|
||||
is still this bundle, not `build:sdk`.
|
||||
- `build:component` — `@element-hq/element-call-component`, sources in `component/`
|
||||
(its own pnpm project; run pnpm from the repo root). Host API is in the README;
|
||||
`pnpm lint:externals` rejects an import of a `react` / `react-dom` /
|
||||
|
||||
@@ -60,5 +60,4 @@ pnpm backend # Synapse + LiveKit, required for e2e
|
||||
pnpm test:playwright # or :open
|
||||
pnpm dev:component # component harness, port 3001
|
||||
pnpm dev:sdk # SDK harness, port 3002
|
||||
pnpm dev:widget-rtc-sdk # widget RTC SDK example widget, port 3003
|
||||
```
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# Agent workflow
|
||||
|
||||
## Scratch files live in `agent-workspace/`
|
||||
|
||||
Git-ignored. One kebab-case subfolder per task, matching the branch topic:
|
||||
`agent-workspace/<slug>/`.
|
||||
|
||||
| File | Purpose |
|
||||
| --------------------------- | -------------------------------------------------------------------------------- |
|
||||
| `plan.md` | The concrete approach — files, symbols, edit order — once direction is confirmed |
|
||||
| `commit-msg.txt` | `git commit -F agent-workspace/<slug>/commit-msg.txt` |
|
||||
| `pr-body.md` | `gh pr create --body-file agent-workspace/<slug>/pr-body.md` |
|
||||
| `implementation-summary.md` | What was built, decisions, trade-offs |
|
||||
| `NN-prompt.md` | Raw prompt text, numbered, when worth keeping |
|
||||
|
||||
- Nothing here is durable. The folder is ignored, so anything worth keeping is
|
||||
promoted into the PR body, a doc, or a feature spec before the task ends.
|
||||
- Never write scratch files to the repo root. Stray `load_test_summary.md` and
|
||||
`config.json_` files are what this folder prevents.
|
||||
- Start `pr-body.md` from `.github/PULL_REQUEST_TEMPLATE.md` and fill every
|
||||
section.
|
||||
- Commit subjects are plain imperative English. No conventional-commits prefixes.
|
||||
- A `plan.md` and a feature spec sit at different altitudes; neither replaces the
|
||||
other. A spec in `FEATURES_SPEC/` is durable and deliberately abstract —
|
||||
behaviour, decisions, acceptance criteria — so the feature can be rebuilt against
|
||||
a `main` nobody has seen yet. A plan is one slice of it landing on today's
|
||||
`main`: the paths, symbols and edit order the spec's decisions and criteria must
|
||||
not name. `FEATURES_SPEC/AGENTS.md` wins wherever the two genuinely overlap.
|
||||
|
||||
## Hand off before the quality pass
|
||||
|
||||
- Implement the change, run the narrowest check that rules out an obviously broken
|
||||
handoff, then stop and ask whether the direction is right.
|
||||
- Full `pnpm lint`, the whole suite, coverage and benchmarks come after the
|
||||
direction is confirmed.
|
||||
- Wider refactors, extra tests and documentation polish are follow-up work, not
|
||||
part of the first handoff.
|
||||
- If a check is needed before feedback, keep it to the touched code and say why.
|
||||
|
||||
## Commit and PR readiness
|
||||
|
||||
- Commit once the user confirms direction, or asks for one. Not before. Where the
|
||||
setup has no git identity, a sandbox included, write the message to
|
||||
`commit-msg.txt` and hand it over instead.
|
||||
- Ready means every gate in [AGENTS.md](../../AGENTS.md#gates) is green and the
|
||||
change is covered at the layers [testing.md](./testing.md) asks for. Read the diff
|
||||
against [code-style.md](./code-style.md) first.
|
||||
- Re-run the whole checklist after any fix. Only on green, either way.
|
||||
- Once a human has started reviewing, fix forward — never force-push a
|
||||
regeneration over a review in progress.
|
||||
@@ -14,8 +14,7 @@ export default {
|
||||
"vite-embedded.config.ts",
|
||||
"vite-sdk.config.ts",
|
||||
"vite-sdk-dev.config.ts",
|
||||
"vite-widget-rtc-sdk.config.ts",
|
||||
"vite-widget-rtc-sdk-dev.config.ts",
|
||||
"vite-sdk-target-based-on-call-view-model.config.ts",
|
||||
"vite-component.config.ts",
|
||||
"vite-component-dev.config.ts",
|
||||
],
|
||||
|
||||
+5
-6
@@ -8,7 +8,6 @@
|
||||
"dev:embedded": "vite --config vite-embedded.config.js",
|
||||
"dev:component": "vite --config vite-component-dev.config.ts",
|
||||
"dev:sdk": "vite --config vite-sdk-dev.config.ts",
|
||||
"dev:widget-rtc-sdk": "vite --config vite-widget-rtc-sdk-dev.config.ts",
|
||||
"build": "pnpm build:full",
|
||||
"build:full": "node --max-old-space-size=16384 node_modules/vite/bin/vite.js build",
|
||||
"build:full:production": "pnpm build:full",
|
||||
@@ -19,11 +18,11 @@
|
||||
"build:sdk": "pnpm build:sdk:js && pnpm build:sdk:types",
|
||||
"build:sdk:js": "pnpm build:full --config vite-sdk.config.js",
|
||||
"build:sdk:types": "tsc -p sdk/tsconfig.build.json",
|
||||
"build:widget-rtc-sdk": "pnpm build:full --config vite-widget-rtc-sdk.config.js",
|
||||
"build:widget-rtc-sdk:production": "pnpm build:widget-rtc-sdk",
|
||||
"build:widget-rtc-sdk:development": "pnpm build:widget-rtc-sdk --mode development",
|
||||
"build:sdk-target-based-on-call-view-model": "pnpm build:full --config vite-sdk-target-based-on-call-view-model.config.js",
|
||||
"build:sdk:production": "pnpm build:sdk-target-based-on-call-view-model",
|
||||
"build:sdk:development": "pnpm build:sdk-target-based-on-call-view-model --mode development",
|
||||
"build:component": "pnpm build:component:js && pnpm build:component:types",
|
||||
"build:component:js": "pnpm build:full --config vite-component.config.js",
|
||||
"build:component:js": "pnpm build:full --config vite-component.config.js && node scripts/check-component-bundle.mjs",
|
||||
"build:component:types": "tsc -p component/tsconfig.build.json",
|
||||
"build:component:production": "pnpm build:component",
|
||||
"build:component:development": "pnpm build:component:js --mode development && pnpm build:component:types",
|
||||
@@ -108,7 +107,7 @@
|
||||
"livekit-client": "^2.18.1",
|
||||
"lodash-es": "^4.17.21",
|
||||
"loglevel": "^1.9.1",
|
||||
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#10e4faabbb49baca655492d523b58faa9eea04b9",
|
||||
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#dc8610e796c92553c658300a4882dde961a0678a",
|
||||
"matrix-widget-api": "^1.18.0",
|
||||
"node-stdlib-browser": "^1.3.1",
|
||||
"normalize.css": "^8.0.1",
|
||||
|
||||
@@ -13,7 +13,6 @@ import { fileURLToPath } from "node:url";
|
||||
|
||||
import { COMPONENT_HARNESS_URL } from "./playwright/component/harness.ts";
|
||||
import { SDK_HARNESS_URL } from "./playwright/sdk/harness.ts";
|
||||
import { WIDGET_RTC_SDK_URL } from "./playwright/widget/test-helpers.ts";
|
||||
|
||||
const baseURL = process.env.USE_DOCKER
|
||||
? "http://localhost:8080"
|
||||
@@ -184,20 +183,5 @@ export default defineConfig({
|
||||
timeout: 500,
|
||||
},
|
||||
},
|
||||
{
|
||||
// The widget RTC SDK's example widget, which Element Web opens as a
|
||||
// widget. Bundled rather than from the dev server: Firefox now and then
|
||||
// stalls on the dev server's hundreds of module requests.
|
||||
command:
|
||||
"pnpm exec vite build --config vite-widget-rtc-sdk-dev.config.ts && " +
|
||||
"pnpm exec vite preview --config vite-widget-rtc-sdk-dev.config.ts",
|
||||
url: WIDGET_RTC_SDK_URL,
|
||||
reuseExistingServer: !process.env.CI,
|
||||
ignoreHTTPSErrors: true,
|
||||
gracefulShutdown: {
|
||||
signal: "SIGTERM",
|
||||
timeout: 500,
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -32,11 +32,9 @@ export interface MyFixtures {
|
||||
export const widgetTest = test.extend<MyFixtures>({
|
||||
// allow per-test override: `widgetTest.use({ callType: "dm" })`
|
||||
callType: ["room", { option: true }],
|
||||
asWidget: async ({ browser, callType }, pUse, testInfo) => {
|
||||
// Tests in parallel workers can reach this in the same millisecond.
|
||||
const uniqueSuffix = `${Date.now()}_${testInfo.parallelIndex}`;
|
||||
const brooksDisplayName = `brooks_${uniqueSuffix}`;
|
||||
const whistlerDisplayName = `whistler_${uniqueSuffix}`;
|
||||
asWidget: async ({ browser, callType }, pUse) => {
|
||||
const brooksDisplayName = `brooks_${Date.now()}`;
|
||||
const whistlerDisplayName = `whistler_${Date.now()}`;
|
||||
|
||||
// Register users
|
||||
const {
|
||||
|
||||
@@ -22,12 +22,6 @@ const PASSWORD = "foobarbaz1!";
|
||||
export const HOST1 = "https://app.m.localhost/#/welcome";
|
||||
export const HOST2 = "https://app.othersite.m.localhost/#/welcome";
|
||||
|
||||
/**
|
||||
* Where the widget RTC SDK's example widget is served (`pnpm dev:widget-rtc-sdk`):
|
||||
* `widget-rtc-sdk/index.html`, running the bundle from source.
|
||||
*/
|
||||
export const WIDGET_RTC_SDK_URL = "https://localhost:3003";
|
||||
|
||||
export type RtcMode = "compat" | "2_0";
|
||||
|
||||
export class TestHelpers {
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { expect, type FrameLocator, type Page } from "@playwright/test";
|
||||
|
||||
import { widgetTest } from "../fixtures/widget-user.ts";
|
||||
import { WIDGET_RTC_SDK_URL } from "./test-helpers.ts";
|
||||
|
||||
/**
|
||||
* The widget RTC SDK's example widget, `widget-rtc-sdk/index.html`, added to a
|
||||
* room as a custom widget and opened in two Element Webs: the bundle's whole
|
||||
* public API, from the widget API to the data channel.
|
||||
*/
|
||||
|
||||
widgetTest.describe.configure({ timeout: 300_000 });
|
||||
|
||||
const WIDGET_ID = "widget-rtc-sdk-example";
|
||||
const WIDGET_NAME = "Widget RTC SDK";
|
||||
|
||||
/** Accepts the prompts Element Web shows before it runs a widget, and finds it. */
|
||||
async function openWidget(page: Page): Promise<FrameLocator> {
|
||||
// Only for a widget someone else added
|
||||
const loadPrompt = page.getByRole("button", { name: "Continue" });
|
||||
const capabilitiesPrompt = page.getByRole("button", { name: "Approve" });
|
||||
await expect(loadPrompt.or(capabilitiesPrompt)).toBeVisible({
|
||||
timeout: 30_000,
|
||||
});
|
||||
if (await loadPrompt.isVisible()) await loadPrompt.click();
|
||||
await capabilitiesPrompt.click({ timeout: 30_000 });
|
||||
// The SDK authenticates with the media transport by an OpenID token, and
|
||||
// asks for more than one, so the answer has to be remembered
|
||||
const openIdPrompt = page.getByRole("dialog", {
|
||||
name: "Allow this widget to verify your identity",
|
||||
});
|
||||
await openIdPrompt.getByText("Remember this").click({ timeout: 30_000 });
|
||||
await openIdPrompt.getByRole("button", { name: "Continue" }).click();
|
||||
return page.frameLocator(`iframe[title="${WIDGET_NAME}"]`);
|
||||
}
|
||||
|
||||
widgetTest(
|
||||
"data sent from one widget arrives in the other",
|
||||
async ({ asWidget }) => {
|
||||
const { brooks, whistler } = asWidget;
|
||||
|
||||
// Brooks adds the example widget to the room and pins it to the top, so
|
||||
// that both Element Webs open it without anyone looking for it
|
||||
await brooks.clientHandle.evaluate(
|
||||
async (client, { widgetId, name, url }) => {
|
||||
const roomId = client
|
||||
.getRooms()
|
||||
.find((room) => room.name === "Welcome Room")!.roomId;
|
||||
await client.sendStateEvent(
|
||||
roomId,
|
||||
// @ts-expect-error not in the js-sdk's state event map
|
||||
"im.vector.modular.widgets",
|
||||
{
|
||||
id: widgetId,
|
||||
type: "m.custom",
|
||||
name,
|
||||
url,
|
||||
data: {},
|
||||
creatorUserId: client.getUserId(),
|
||||
},
|
||||
widgetId,
|
||||
);
|
||||
await client.sendStateEvent(
|
||||
roomId,
|
||||
// @ts-expect-error not in the js-sdk's state event map
|
||||
"io.element.widgets.layout",
|
||||
{
|
||||
widgets: {
|
||||
[widgetId]: {
|
||||
container: "top",
|
||||
index: 0,
|
||||
width: 100,
|
||||
height: 50,
|
||||
},
|
||||
},
|
||||
},
|
||||
"",
|
||||
);
|
||||
},
|
||||
{
|
||||
widgetId: WIDGET_ID,
|
||||
name: WIDGET_NAME,
|
||||
// Element Web fills the $ variables in and adds widgetId and parentUrl
|
||||
url:
|
||||
`${WIDGET_RTC_SDK_URL}/?widgetId=$matrix_widget_id&roomId=$matrix_room_id` +
|
||||
"&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id" +
|
||||
"&baseUrl=$org.matrix.msc4039.matrix_base_url",
|
||||
},
|
||||
);
|
||||
|
||||
const brooksWidget = await openWidget(brooks.page);
|
||||
const whistlerWidget = await openWidget(whistler.page);
|
||||
|
||||
// The example joins on load; each sees the other as a member
|
||||
for (const [widget, other] of [
|
||||
[brooksWidget, whistler],
|
||||
[whistlerWidget, brooks],
|
||||
] as const) {
|
||||
await expect(widget.locator("#connect_status")).toHaveText("Connected", {
|
||||
timeout: 60_000,
|
||||
});
|
||||
await expect(widget.locator("#members")).toContainText(other.mxId, {
|
||||
timeout: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
// Data is not queued: the receiver drops what arrives before it has matched
|
||||
// the sender on the media transport, which the example does not show. So
|
||||
// send until it arrives.
|
||||
await expect(async () => {
|
||||
await brooksWidget.getByRole("button", { name: "Send Text" }).click();
|
||||
await expect(whistlerWidget.locator("#data")).toContainText(
|
||||
"Hello, world!",
|
||||
{ timeout: 2_000 },
|
||||
);
|
||||
}).toPass({ timeout: 60_000 });
|
||||
// Our own data is not echoed back
|
||||
await expect(brooksWidget.locator("#data")).not.toContainText(
|
||||
"Hello, world!",
|
||||
);
|
||||
},
|
||||
);
|
||||
Generated
+10
-10
@@ -191,8 +191,8 @@ importers:
|
||||
specifier: ^1.9.1
|
||||
version: 1.9.2
|
||||
matrix-js-sdk:
|
||||
specifier: github:matrix-org/matrix-js-sdk#10e4faabbb49baca655492d523b58faa9eea04b9
|
||||
version: https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/10e4faabbb49baca655492d523b58faa9eea04b9
|
||||
specifier: github:matrix-org/matrix-js-sdk#dc8610e796c92553c658300a4882dde961a0678a
|
||||
version: https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/dc8610e796c92553c658300a4882dde961a0678a
|
||||
matrix-widget-api:
|
||||
specifier: ^1.18.0
|
||||
version: 1.20.0
|
||||
@@ -1195,8 +1195,8 @@ packages:
|
||||
'@types/dom-mediacapture-transform': ^0.1.9
|
||||
livekit-client: ^1.12.0 || ^2.1.0
|
||||
|
||||
'@matrix-org/matrix-sdk-crypto-wasm@18.9.2':
|
||||
resolution: {integrity: sha512-GHeuSeb5Fj6Wg2+NE/WoLJJs9U3nOzttO+2sj7lOaS5gNBpGIksc48N/Q/kK/Z+Lxh7YMhopB7M8b6HyASXnGQ==}
|
||||
'@matrix-org/matrix-sdk-crypto-wasm@18.9.0':
|
||||
resolution: {integrity: sha512-NtLzNDTEfNASBjNCTGZHuk7N0jrQhS6Zx2CM857rCHZYYh/rpbKGQNrJ7azktm4S1P5Jy3WU3nwJTGohGw26qQ==}
|
||||
engines: {node: '>= 18'}
|
||||
|
||||
'@mdx-js/react@3.1.1':
|
||||
@@ -4734,9 +4734,9 @@ packages:
|
||||
matrix-events-sdk@0.0.1:
|
||||
resolution: {integrity: sha512-1QEOsXO+bhyCroIe2/A5OwaxHvBm7EsSQ46DEDn8RBIfQwN5HWBpFvyWWR4QY0KHPPnnJdI99wgRiAl7Ad5qaA==}
|
||||
|
||||
matrix-js-sdk@https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/10e4faabbb49baca655492d523b58faa9eea04b9:
|
||||
resolution: {gitHosted: true, integrity: sha512-7cbPN96wGt93B73jZieW2HBtTnasgKJ7wzTgiV0SjwkNyb50EK3vDa17LdSJpyOwEjFP5edyPRprk865HQJqww==, tarball: https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/10e4faabbb49baca655492d523b58faa9eea04b9}
|
||||
version: 45.1.0
|
||||
matrix-js-sdk@https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/dc8610e796c92553c658300a4882dde961a0678a:
|
||||
resolution: {gitHosted: true, integrity: sha512-Ldt/VkCzBtre6m1cn0FqgrzRV0GrgWIoARgQUQO58ef2Edu2ERFCINPzp/psFS5RwoN3RIy9Rm4oPVc1ATkEWA==, tarball: https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/dc8610e796c92553c658300a4882dde961a0678a}
|
||||
version: 45.0.0
|
||||
engines: {node: '>=22.0.0'}
|
||||
|
||||
matrix-widget-api@1.20.0:
|
||||
@@ -7178,7 +7178,7 @@ snapshots:
|
||||
'@types/dom-mediacapture-transform': 0.1.11
|
||||
livekit-client: 2.23.0(@types/dom-mediacapture-record@1.0.22)
|
||||
|
||||
'@matrix-org/matrix-sdk-crypto-wasm@18.9.2': {}
|
||||
'@matrix-org/matrix-sdk-crypto-wasm@18.9.0': {}
|
||||
|
||||
'@mdx-js/react@3.1.1(@types/react@19.2.18)(react@19.3.1)':
|
||||
dependencies:
|
||||
@@ -10413,10 +10413,10 @@ snapshots:
|
||||
|
||||
matrix-events-sdk@0.0.1: {}
|
||||
|
||||
matrix-js-sdk@https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/10e4faabbb49baca655492d523b58faa9eea04b9:
|
||||
matrix-js-sdk@https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/dc8610e796c92553c658300a4882dde961a0678a:
|
||||
dependencies:
|
||||
'@babel/runtime': 8.0.8
|
||||
'@matrix-org/matrix-sdk-crypto-wasm': 18.9.2
|
||||
'@matrix-org/matrix-sdk-crypto-wasm': 18.9.0
|
||||
another-json: 0.2.0
|
||||
bs58: 6.0.0
|
||||
content-type: 3.1.1
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks the component build's output for what a host must not be handed.
|
||||
*
|
||||
* A library build inlines every asset it meets as a data URL unless the import
|
||||
* says otherwise, and does so silently: one import without `no-inline` and the
|
||||
* module grows by the asset, encoded a third larger, that every host then
|
||||
* downloads and parses before the first call. MediaPipe's runtime alone is
|
||||
* 11 MB. A reference to a file by an absolute path is just as silent and
|
||||
* breaks at runtime instead, since a host serves the module from wherever it
|
||||
* likes. And the standalone app's client store worker, which carries a copy of
|
||||
* the Matrix SDK and its crypto, has no business in a package whose host
|
||||
* supplies the client: the bundler emits it on loading its import, used or
|
||||
* not (see src/utils/initClient.ts). This fails the build on each of those.
|
||||
*/
|
||||
|
||||
import { readdir, readFile, stat } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
const dist = "component/dist";
|
||||
|
||||
/** The longest data URL a chunk may carry: enough for an icon, not a sound. */
|
||||
const inlineLimit = 16 * 1024;
|
||||
|
||||
/**
|
||||
* The module itself, in bytes. Well above where it sits with its assets as
|
||||
* files, so that only an asset finding its way back in trips it.
|
||||
*/
|
||||
const moduleBudget = 4 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* The modules a host bundles: the entry points and their chunks, at the top of
|
||||
* the package. What sits under `assets/` is fetched as it is, by URL.
|
||||
*/
|
||||
async function modules() {
|
||||
const names = await readdir(dist);
|
||||
return names.filter((name) => name.endsWith(".js")).map((n) => join(dist, n));
|
||||
}
|
||||
|
||||
const problems = [];
|
||||
|
||||
for (const name of await readdir(join(dist, "assets")))
|
||||
if (name.includes("IndexedDBWorker"))
|
||||
problems.push(
|
||||
`${dist}/assets/${name}: the standalone app's client store worker is ` +
|
||||
`in the package. Something the component reaches imports the worker; ` +
|
||||
`keep it on the standalone app's side (see src/utils/initClient.ts ` +
|
||||
`and the \`loadClient\` prop of ClientProvider).`,
|
||||
);
|
||||
|
||||
for (const file of await modules()) {
|
||||
const code = await readFile(file, "utf8");
|
||||
|
||||
for (const [, type, payload] of code.matchAll(
|
||||
/data:([a-z0-9/.+-]*);base64,([A-Za-z0-9+/=]*)/g,
|
||||
))
|
||||
if (payload.length > inlineLimit)
|
||||
problems.push(
|
||||
`${file}: a ${type} asset of ${Math.round(payload.length / 1024)} kB ` +
|
||||
`is inlined as a data URL. Import it with \`?url&no-inline\` so ` +
|
||||
`that it is emitted as a file.`,
|
||||
);
|
||||
|
||||
for (const [reference] of code.matchAll(/["'`]\/assets\/[^"'`]+["'`]/g))
|
||||
problems.push(
|
||||
`${file}: ${reference} is an absolute path, which only resolves ` +
|
||||
`against an origin that serves the package at its root. The build ` +
|
||||
`is meant to render relative references; check \`base\` in ` +
|
||||
`vite-component.config.ts.`,
|
||||
);
|
||||
}
|
||||
|
||||
const moduleSize = (await stat(join(dist, "element-call.js"))).size;
|
||||
if (moduleSize > moduleBudget)
|
||||
problems.push(
|
||||
`${dist}/element-call.js is ${Math.round(moduleSize / 1024 / 1024)} MB, ` +
|
||||
`over its ${moduleBudget / 1024 / 1024} MB budget. Something large ` +
|
||||
`has been bundled into the module that a host would rather fetch when ` +
|
||||
`it is needed.`,
|
||||
);
|
||||
|
||||
if (problems.length > 0) {
|
||||
console.error(`The component build is not fit to publish:\n`);
|
||||
for (const problem of problems) console.error(` ${problem}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
# SDK mode (LEGACY)
|
||||
|
||||
> Superseded by the `sdk/` package (`@element-hq/matrixrtc-sdk`) and its harness in
|
||||
> `sdk/dev`; see `sdk/SdkArchitecture.md`. This bundle is in production use, so it
|
||||
> keeps building as a thin layer over that package until its consumers have moved
|
||||
> to the package directly. It will then be removed. Its members are the package's
|
||||
> `RTCMember`s now, in place of its own wrapper around the raw membership.
|
||||
|
||||
EC can be build in sdk mode. This will result in a compiled js file that can be imported in very simple webapps.
|
||||
|
||||
It allows to use matrixRTC in combination with livekit without relying on element call.
|
||||
|
||||
This is done by instantiating the call view model and exposing some useful behaviors (observables) and methods.
|
||||
|
||||
This folder contains an example index.html file that showcases the sdk in use (hosted on localhost:8123 with a webserver allowing cors (for example `npx serve -l 81234 --cors`)) as a godot engine HTML export template.
|
||||
|
||||
## Getting started
|
||||
|
||||
To get started run
|
||||
|
||||
```
|
||||
pnpm install
|
||||
pnpm build:sdk-target-based-on-call-view-model
|
||||
```
|
||||
|
||||
in the repository root. (`pnpm build:sdk` builds the `sdk/` package instead.)
|
||||
|
||||
CI's `sdk` package (`build:sdk:production`, `build:sdk:development`, the
|
||||
`build-output-sdk` artifact and the `pr<N>-sdk` Netlify preview) is still this
|
||||
bundle, not the `sdk/` package, until its consumers have moved over.
|
||||
|
||||
It will create `dist` containing the compiled js file, `matrixrtc-sdk.js`.
|
||||
|
||||
This file needs to be hosted. Locally (via `npx serve -l 81234 --cors`) or on a remote server.
|
||||
|
||||
Now you just need to add the widget to element web via:
|
||||
|
||||
```
|
||||
/addwidget http://localhost:3000?widgetId=$matrix_widget_id&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id&baseUrl=$org.matrix.msc4039.matrix_base_url&roomId=$matrix_room_id
|
||||
```
|
||||
|
||||
## Widgets
|
||||
|
||||
The sdk mode is particularly interesting to be used in widgets. In widgets you do not need to pay attention to matrix login/cs api ...
|
||||
To create a widget see the example `index.html` file in this folder. And add it to EW via:
|
||||
`/addwidget <widgetUrl>` (see **url parameters** for more details on `<widgetUrl>`)
|
||||
|
||||
### url parameters
|
||||
|
||||
The url parameters are needed to pass initial data to the widget. They will automatically be used
|
||||
by the matrixRTCSdk to start the postmessage widget api (communication between the client (e.g. Element Web) and the widget)
|
||||
|
||||
```
|
||||
widgetId = $matrix_widget_id
|
||||
userId = $matrix_user_id
|
||||
deviceId = $org.matrix.msc3819.matrix_device_id
|
||||
baseUrl = $org.matrix.msc4039.matrix_base_url
|
||||
```
|
||||
|
||||
`parentUrl = // will be inserted automatically`
|
||||
|
||||
Full template use as `<widgetUrl>`:
|
||||
|
||||
```
|
||||
http://localhost:3000?widgetId=$matrix_widget_id&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id&baseUrl=$org.matrix.msc4039.matrix_base_url&roomId=$matrix_room_id
|
||||
```
|
||||
|
||||
the `$` prefixed variables will be replaced by EW on widget instantiation. (e.g. `$matrix_user_id` -> `@user:example.com` (url encoding will also be applied automatically by EW) -> `%40user%3Aexample.com`)
|
||||
@@ -1,13 +1,11 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Widget RTC SDK example</title>
|
||||
<title>MatrixRTC Widget</title>
|
||||
<meta charset="utf-8" />
|
||||
<script type="module">
|
||||
// The bundle a pull request's CI deploys: replace 1234 with the PR number.
|
||||
// For a local build use the url `dist/` is served from, for example
|
||||
// "http://localhost:8123/widget-rtc-sdk.js".
|
||||
import { createMatrixRTCSdk } from "https://pr1234-widget-rtc-sdk--element-call.netlify.app/widget-rtc-sdk.js";
|
||||
// TODO use the url where the matrixrtc-sdk.js file from dist is hosted
|
||||
import { createMatrixRTCSdk } from "http://localhost:8123/matrixrtc-sdk.js";
|
||||
|
||||
try {
|
||||
window.matrixRTCSdk = await createMatrixRTCSdk(
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
Copyright 2025-2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LEGACY, in production use
|
||||
*
|
||||
* The entrypoint of `pnpm build:sdk-target-based-on-call-view-model`, a
|
||||
* bundle for widgets that exposes `createMatrixRTCSdk`: join a slot and
|
||||
* exchange realtime data, with the `@element-hq/matrixrtc-sdk` package and
|
||||
* Element Call's call view model doing the work underneath. New consumers use
|
||||
* the package directly; this bundle goes away once the existing ones have.
|
||||
*/
|
||||
|
||||
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
|
||||
import { EMPTY, filter, map, type Observable, of, switchMap, take } from "rxjs";
|
||||
import {
|
||||
type Behavior,
|
||||
createRTCSlot,
|
||||
E2eeType,
|
||||
type LocalRTCMember,
|
||||
ObservableScope,
|
||||
type RemoteRTCMember,
|
||||
type RTCParticipation,
|
||||
} from "@element-hq/matrixrtc-sdk";
|
||||
|
||||
import {
|
||||
callViewModelOptionsFromParams,
|
||||
type CallViewModel,
|
||||
createCallViewModel$,
|
||||
initialPublishRequests,
|
||||
} from "../src/state/CallViewModel/CallViewModel";
|
||||
import {
|
||||
captureSettings,
|
||||
rtcParticipationOptions,
|
||||
rtcSlotOptions,
|
||||
} from "../src/room/InCallView";
|
||||
import { createWidgetHostBridge } from "../src/HostBridge";
|
||||
import { MediaDevices } from "../src/state/MediaDevices";
|
||||
import { MuteStates } from "../src/state/MuteStates";
|
||||
import { getUrlParams } from "../src/UrlParams";
|
||||
import { observeElementSize$ } from "../src/utils/elementSize";
|
||||
import { initializeWidget } from "../src/widget";
|
||||
|
||||
/** The data channel topic this bundle has always used, so that old and new builds talk. */
|
||||
const TOPIC = "matrixRTC";
|
||||
|
||||
export interface MatrixRTCSdk {
|
||||
/** Joins the slot; `connected$` tracks the progress. A second call does nothing. */
|
||||
join: () => void;
|
||||
leave: () => void;
|
||||
/** Ends everything. The sdk cannot be restarted; create a new one. */
|
||||
stop: () => void;
|
||||
connected$: Behavior<boolean>;
|
||||
/** Our own member, null until our membership has been seen in the room. */
|
||||
localMember$: Behavior<LocalRTCMember | null>;
|
||||
/** The other members of the slot, with the media the participation carries for them. */
|
||||
remoteMembers$: Behavior<RemoteRTCMember[]>;
|
||||
/** What other members sent with `sendData`. */
|
||||
data$: Observable<{ rtcBackendIdentity: string; data: string }>;
|
||||
/** Sends to every member on our transport. Rejects while not joined. */
|
||||
sendData: (data: unknown) => Promise<void>;
|
||||
sendRoomMessage: (message: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export async function createMatrixRTCSdk(
|
||||
application = "m.call",
|
||||
id = "",
|
||||
sticky = false,
|
||||
): Promise<MatrixRTCSdk> {
|
||||
const logger = rootLogger.getChild("[MatrixRTCSdk]");
|
||||
const scope = new ObservableScope();
|
||||
const widget = initializeWidget(application, true);
|
||||
if (!widget) throw new Error("No widget: this bundle only runs as a widget");
|
||||
const client = await widget.client;
|
||||
const hostBridge = createWidgetHostBridge(widget);
|
||||
const urlParams = getUrlParams();
|
||||
const { roomId, controlledAudioDevices, callIntent } = urlParams;
|
||||
const room = roomId === null ? null : client.getRoom(roomId);
|
||||
if (room === null) throw new Error("No room: the url needs a joined roomId");
|
||||
|
||||
const mediaDevices = new MediaDevices(scope, {
|
||||
controlledAudioDevices,
|
||||
callIntent,
|
||||
});
|
||||
const muteStates = new MuteStates(
|
||||
scope,
|
||||
mediaDevices,
|
||||
{ audioEnabled: false, videoEnabled: false },
|
||||
hostBridge,
|
||||
);
|
||||
const encryptionSystem = { kind: E2eeType.PER_PARTICIPANT } as const;
|
||||
const capture = captureSettings();
|
||||
const slot = createRTCSlot(scope, client, room, {
|
||||
...rtcSlotOptions(encryptionSystem),
|
||||
application,
|
||||
id,
|
||||
});
|
||||
let vm: CallViewModel | undefined;
|
||||
const failed =
|
||||
(what: string) =>
|
||||
(e: unknown): void =>
|
||||
logger.error(what, e);
|
||||
|
||||
const over = <T>(
|
||||
pick: (participation: RTCParticipation) => Observable<T>,
|
||||
fallback: T,
|
||||
): Behavior<T> =>
|
||||
scope.behavior(
|
||||
slot.participation$.pipe(
|
||||
switchMap((p) => (p === null ? of(fallback) : pick(p))),
|
||||
),
|
||||
);
|
||||
|
||||
return {
|
||||
join: (): void => {
|
||||
if (slot.participation$.value !== null) return;
|
||||
if (sticky)
|
||||
widget.api
|
||||
.setAlwaysOnScreen(true)
|
||||
.catch(failed("Failed to make the widget sticky"));
|
||||
const participation = slot.join({
|
||||
...rtcParticipationOptions(urlParams),
|
||||
publish: initialPublishRequests(muteStates, mediaDevices, capture),
|
||||
});
|
||||
vm = createCallViewModel$(
|
||||
scope,
|
||||
participation,
|
||||
room,
|
||||
mediaDevices,
|
||||
muteStates,
|
||||
{
|
||||
...callViewModelOptionsFromParams(urlParams),
|
||||
encryptionSystem,
|
||||
capture,
|
||||
hostBridge,
|
||||
// The bundle owns its page, so the body is the space it has
|
||||
windowSize$: scope.behavior(observeElementSize$(document.body)),
|
||||
},
|
||||
of({}),
|
||||
of({}),
|
||||
);
|
||||
// Once the hang-up has gone through, unstick and ask the host to close us
|
||||
vm.leave$
|
||||
.pipe(
|
||||
switchMap(() =>
|
||||
participation.state$.pipe(
|
||||
filter((state) => state.kind === "left"),
|
||||
take(1),
|
||||
),
|
||||
),
|
||||
scope.bind(),
|
||||
)
|
||||
.subscribe(() => {
|
||||
hostBridge
|
||||
.setAlwaysOnScreen(false)
|
||||
.catch(failed("Failed to unstick the widget"))
|
||||
.then(() => hostBridge.close?.())
|
||||
.catch(failed("Failed to ask the host to close"));
|
||||
});
|
||||
},
|
||||
leave: (): void => vm?.leave(),
|
||||
stop: (): void => scope.end(),
|
||||
connected$: over(
|
||||
(p) => p.state$.pipe(map((state) => state.kind === "connected")),
|
||||
false,
|
||||
),
|
||||
localMember$: over((p) => p.localMember$, null),
|
||||
remoteMembers$: over((p) => p.remoteMembers$, []),
|
||||
// Attested members only: the SDK has matched the sender
|
||||
data$: slot.participation$.pipe(
|
||||
switchMap((p) => p?.data$ ?? EMPTY),
|
||||
filter(({ topic }) => topic === TOPIC),
|
||||
map(({ member, text }) => ({
|
||||
rtcBackendIdentity: member.rtcBackendIdentity,
|
||||
data: text,
|
||||
})),
|
||||
),
|
||||
sendData: async (data): Promise<void> => {
|
||||
const participation = slot.participation$.value;
|
||||
if (participation === null) throw new Error("Not joined");
|
||||
await participation.sendData(TOPIC, JSON.stringify(data));
|
||||
},
|
||||
sendRoomMessage: async (message): Promise<void> => {
|
||||
await client.sendTextMessage(room.roomId, message);
|
||||
},
|
||||
};
|
||||
}
|
||||
+2
-1
@@ -5,7 +5,8 @@ it joins a MatrixRTC slot in a Matrix room, writes and watches the memberships,
|
||||
connects to the media transport (LiveKit today), publishes the local media and
|
||||
exposes every member and their tracks as observables. It has no UI. Element
|
||||
Call's own `CallViewModel` is one consumer of it; any host can build a different
|
||||
view on top. The design is in [`SdkArchitecture.md`](./SdkArchitecture.md).
|
||||
view on top. The design is in [`SdkArchitecture.md`](./SdkArchitecture.md), what
|
||||
moved out of Element Call's `src/` in [`SdkMigration.md`](./SdkMigration.md).
|
||||
|
||||
**Status:** first implementation. `createRTCSlot` and `slot.join()` join the
|
||||
session, connect to the transport, publish the local media and expose every
|
||||
|
||||
@@ -10,7 +10,8 @@ interface.
|
||||
Element Call becomes one consumer of it: `CallViewModel` keeps what makes a session
|
||||
a _call_ (ringing, auto-leave, layout, sounds, reactions, settings, the host
|
||||
bridge) and takes everything else from the client. The dependency direction is
|
||||
therefore `Element Call → sdk`, never the reverse.
|
||||
therefore `Element Call → sdk`, never the reverse. What moved out of Element
|
||||
Call's `src/` to get there is listed in [`SdkMigration.md`](./SdkMigration.md).
|
||||
|
||||
## The architecture
|
||||
|
||||
@@ -776,6 +777,7 @@ sdk/
|
||||
src/utils/ mapScoped, network retry, display names, test helpers
|
||||
dev/ the harness (below)
|
||||
SdkArchitecture.md this document
|
||||
SdkMigration.md what moved from Element Call's src/ into the SDK
|
||||
```
|
||||
|
||||
The lint rule `element-call/sdk-import-boundary` keeps it that way: nothing under
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
# What moved from `src/` into the SDK
|
||||
|
||||
Element Call's `CallViewModel` used to do everything; now it consumes the SDK and
|
||||
keeps only what makes a session a call. This is the inventory of what moved out
|
||||
of `src/` on the way, so that a reader of either side knows where to look. It
|
||||
names source files, so it ages with the code; the shape they make is in
|
||||
[`SdkArchitecture.md`](./SdkArchitecture.md).
|
||||
|
||||
Everything in the table is gone from `src/`. Each module came with its tests;
|
||||
`sdk/src/utils/test.ts` and `test-fixtures.ts` are the subset of Element Call's
|
||||
test helpers they need. `Behavior`, `ObservableScope`, the observable operators,
|
||||
`E2eeType`, `EncryptionSystem` and `MatrixRTCMode` are re-exported from the SDK
|
||||
by their old `src/` paths, so the rest of Element Call did not have to change.
|
||||
|
||||
| Was in `src/` | Is in `sdk/src/` | What changed on the way |
|
||||
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `state/Behavior.ts`, `state/ObservableScope.ts`, `utils/observable.ts` | `reactive/` | as is |
|
||||
| `state/SessionBehaviors.ts` | `matrixrtc/JsRtcSessionBehaviors.ts` | the memberships and key rotation behaviors only |
|
||||
| `state/CallViewModel/localMember/HomeserverConnected.ts` | `matrixrtc/HomeserverConnected.ts` | the grace period defaults to `defaultSessionTimings` instead of `Config.get()` |
|
||||
| `state/CallViewModel/localMember/LocalMember.ts` | `matrixrtc/LocalMembership.ts`, `media-backend/livekit/LocalMedia.ts`, `participationState.ts` | split into the Matrix side, the media side and the state machine that combines them |
|
||||
| `state/CallViewModel/localMember/LocalTransport.ts`, `RtcTransportAutoDiscovery.ts` | `matrixrtc/LocalTransport.ts`, `TransportAutoDiscovery.ts` | the override and the fallback are transports from the options, not a setting and a resolved config |
|
||||
| `state/CallViewModel/remoteMembers/MatrixLivekitMembers.ts` | `media-backend/livekit/LivekitBackend.ts` (`mediaFor`) | matches participants to members inside the backend; the SDK hands out `RTCMember`s |
|
||||
| `state/CallViewModel/remoteMembers/MatrixMemberMetadata.ts`, `utils/displayname.ts` | `matrixrtc/MatrixMemberMetadata.ts`, `utils/displayname.ts` | as is |
|
||||
| `state/CallViewModel/remoteMembers/Connection.ts` | `media-backend/livekit/Connection.ts` | throws `MatrixRTCError`s; `ConnectionState` is a discriminated union; fetches its own token for its role |
|
||||
| `state/CallViewModel/remoteMembers/ConnectionFactory.ts` | `media-backend/livekit/ConnectionFactory.ts` | `LivekitConnectionFactory` builds the room options from the publish requests and `mediaQuality`, not from settings |
|
||||
| `state/CallViewModel/remoteMembers/ConnectionManager.ts` | `media-backend/livekit/ConnectionMap.ts` | keyed by `TransportLocator`; the publisher role is the local transport's |
|
||||
| `state/CallViewModel/localMember/Publisher.ts` | `media-backend/livekit/Publisher.ts` | rewritten on publish requests instead of `MuteStates` |
|
||||
| `livekit/openIDSFU.ts`, later `livekit/auth/` | `media-backend/livekit/auth/` | the flow is chosen by the transport's shape and our role; the delegated leave timeout is a parameter |
|
||||
| `livekit/options.ts` | `media-backend/livekit/livekitOptions.ts` | `buildLiveKitOptions(mediaQuality?)` only; the `Config` singleton is gone |
|
||||
| `e2ee/matrixKeyProvider.ts`, the key provider selection | `media-backend/livekit/MatrixKeyProvider.ts`, `KeyProvider.ts` | as is; selection keyed off `encryptionSystem` |
|
||||
| `e2ee/e2eeType.ts`, `EncryptionSystem` from `e2ee/sharedKeyManagement.ts` | `encryption.ts` | the React hooks stayed behind |
|
||||
| `MatrixRTCMode`, the timings and media quality types from `config/ConfigOptions.ts` | `config.ts` | `defaultSessionTimings` and `defaultMediaQuality` replace `DEFAULT_CONFIG` |
|
||||
| `utils/errors.ts` | `errors.ts` | `MatrixRTCError` with `code` and `category` replaces `ElementCallError`; messages are plain English, the host translates by code |
|
||||
| `doNetworkOperationWithRetry` from `utils/matrix.ts` | `utils/network.ts` | takes a `shouldRetry` predicate |
|
||||
| the join in `CallViewModel.ts` (`enterRTCSession`) | `matrixrtc/joinJsSdkSession.ts` | the timings and the delayed leave variant are parameters |
|
||||
| `livekit/MatrixAudioRenderer.tsx`, the participant reads in `state/media/` | `media-backend/livekit/LivekitMediaTrack.ts`, `LivekitMemberMedia.ts` | the view models get `MediaTrack`s with `attach`; nothing in `src/` imports a LiveKit participant any more |
|
||||
|
||||
## What still binds Element Call to LiveKit
|
||||
|
||||
Each is a gap in the SDK's API rather than something to rename; the import goes
|
||||
away when the SDK grows the piece. The names Element Call owes to the outside
|
||||
stay as they are: the `livekit.livekit_service_url` key in `config.json`, the
|
||||
persisted setting keys `extended-livekit-logs` and `custom-transport`, the
|
||||
Posthog fields `callBackend` and `callReconnectingCountLivekit`, the rageshake
|
||||
field `call_backend`, and the translated error strings.
|
||||
|
||||
| Gap | Where it shows |
|
||||
| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
||||
| Preview before `join()`: local tracks for the lobby, handed out as `MediaTrack`s | `LobbyView` (`usePreviewTracks`), `VideoPreview` (`LocalVideoTrack`, `facingModeFromLocalTrack`) |
|
||||
| Device enumeration without LiveKit's helpers | `MediaDevices` (`createMediaDeviceObserver`), `CallView` (`Room.getLocalDevices`) |
|
||||
| A `logging` option: log level and a sink for the transport's logs | `initializer.tsx`, the `extended-livekit-logs` setting |
|
||||
| A `capabilities` field: output selection and background processors, asked of the SDK rather than of LiveKit | `CallFooterViewModel` (`supportsAudioOutputSelection`, `supportsBackgroundProcessors`) |
|
||||
| Element Call's error and analytics types on the SDK's | `PosthogEvents` (`DisconnectReason`), `errors.ts` (`ConnectionError`, `LivekitConnectionError`) |
|
||||
@@ -140,7 +140,7 @@ function setup(
|
||||
getDeviceId: () => deviceId,
|
||||
getDomain: () => "example.org",
|
||||
getSyncState: () => SyncState.Syncing,
|
||||
getRTCTransports: vi.fn().mockResolvedValue(homeserverTransports),
|
||||
_unstable_getRTCTransports: vi.fn().mockResolvedValue(homeserverTransports),
|
||||
_unstable_getLivekitToken: vi.fn().mockResolvedValue({ jwt: publisherJWT }),
|
||||
} as unknown as MatrixClient;
|
||||
const members$ = new BehaviorSubject<RTCMember[]>([]);
|
||||
|
||||
@@ -31,7 +31,7 @@ function client(
|
||||
): Parameters<typeof discoverLocalTransport>[0] {
|
||||
return {
|
||||
getDomain: () => "example.org",
|
||||
getRTCTransports: vi.fn().mockResolvedValue(advertised),
|
||||
_unstable_getRTCTransports: vi.fn().mockResolvedValue(advertised),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ describe("discoverLocalTransport", () => {
|
||||
logger,
|
||||
),
|
||||
).resolves.toBe(hostTransport);
|
||||
expect(c.getRTCTransports).not.toHaveBeenCalled();
|
||||
expect(c._unstable_getRTCTransports).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("prefers the homeserver's transport over the host's fallback", async () => {
|
||||
|
||||
@@ -22,7 +22,7 @@ import { TransportAutoDiscovery } from "./TransportAutoDiscovery";
|
||||
* @throws {MatrixRTCTransportMissingError} where none of these exists.
|
||||
*/
|
||||
export async function discoverLocalTransport(
|
||||
client: Pick<MatrixClient, "getDomain" | "getRTCTransports">,
|
||||
client: Pick<MatrixClient, "getDomain" | "_unstable_getRTCTransports">,
|
||||
transportTypes: readonly string[],
|
||||
{
|
||||
transport: override,
|
||||
|
||||
@@ -36,7 +36,11 @@ const configTransport: Transport = {
|
||||
|
||||
function makeClient(): MockedObject<DiscoveryClient> {
|
||||
return {
|
||||
getRTCTransports: vi.fn().mockResolvedValue([]),
|
||||
getDomain: vi.fn().mockReturnValue("example.org"),
|
||||
baseUrl: "https://matrix.example.org",
|
||||
_unstable_getRTCTransports: vi.fn().mockResolvedValue([]),
|
||||
getOpenIdToken: vi.fn(),
|
||||
getDeviceId: vi.fn(),
|
||||
} as unknown as MockedObject<DiscoveryClient>;
|
||||
}
|
||||
|
||||
@@ -53,7 +57,7 @@ describe("TransportAutoDiscovery", () => {
|
||||
"prefers backend transport other app config $transports",
|
||||
async ({ transports }) => {
|
||||
const client = makeClient();
|
||||
client.getRTCTransports.mockResolvedValue(transports);
|
||||
client._unstable_getRTCTransports.mockResolvedValue(transports);
|
||||
|
||||
const discovery = new TransportAutoDiscovery({
|
||||
client,
|
||||
@@ -67,7 +71,7 @@ describe("TransportAutoDiscovery", () => {
|
||||
expect(discoveredTransport).toStrictEqual(backendTransport);
|
||||
expect(discoveredTransport).not.toStrictEqual(configTransport);
|
||||
|
||||
expect(client.getRTCTransports).toHaveBeenCalledTimes(1);
|
||||
expect(client._unstable_getRTCTransports).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -77,7 +81,10 @@ describe("TransportAutoDiscovery", () => {
|
||||
url: "wss://sfu.example.org",
|
||||
};
|
||||
const client = makeClient();
|
||||
client.getRTCTransports.mockResolvedValue([backendTransport, mLivekit]);
|
||||
client._unstable_getRTCTransports.mockResolvedValue([
|
||||
backendTransport,
|
||||
mLivekit,
|
||||
]);
|
||||
|
||||
const discovery = new TransportAutoDiscovery({
|
||||
client,
|
||||
@@ -93,7 +100,7 @@ describe("TransportAutoDiscovery", () => {
|
||||
|
||||
it("Retries limit_exceeded backend transport", async () => {
|
||||
const client = makeClient();
|
||||
client.getRTCTransports
|
||||
client._unstable_getRTCTransports
|
||||
.mockRejectedValueOnce(
|
||||
new MatrixError(
|
||||
{
|
||||
@@ -117,7 +124,7 @@ describe("TransportAutoDiscovery", () => {
|
||||
backendTransport,
|
||||
);
|
||||
|
||||
expect(client.getRTCTransports).toHaveBeenCalledTimes(2);
|
||||
expect(client._unstable_getRTCTransports).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
const INVALID_TEST_CASES: Array<{ transports: Transport[] }> = [
|
||||
@@ -128,7 +135,7 @@ describe("TransportAutoDiscovery", () => {
|
||||
"falls back to config when backend has no (valid) livekit transports $transports",
|
||||
async ({ transports }) => {
|
||||
const client = makeClient();
|
||||
client.getRTCTransports.mockResolvedValue(transports);
|
||||
client._unstable_getRTCTransports.mockResolvedValue(transports);
|
||||
|
||||
const discovery = new TransportAutoDiscovery({
|
||||
client,
|
||||
@@ -145,7 +152,7 @@ describe("TransportAutoDiscovery", () => {
|
||||
|
||||
it("returns null when backend and config are all unavailable", async () => {
|
||||
const client = makeClient();
|
||||
client.getRTCTransports.mockResolvedValue([]);
|
||||
client._unstable_getRTCTransports.mockResolvedValue([]);
|
||||
|
||||
const discovery = new TransportAutoDiscovery({
|
||||
client,
|
||||
|
||||
@@ -10,7 +10,10 @@ import { type Logger } from "matrix-js-sdk/lib/logger";
|
||||
|
||||
import { doNetworkOperationWithRetry } from "../utils/network";
|
||||
|
||||
type TransportDiscoveryClient = Pick<MatrixClient, "getRTCTransports">;
|
||||
type TransportDiscoveryClient = Pick<
|
||||
MatrixClient,
|
||||
"getDomain" | "_unstable_getRTCTransports"
|
||||
>;
|
||||
|
||||
export interface TransportAutoDiscoveryProps {
|
||||
client: TransportDiscoveryClient;
|
||||
@@ -71,7 +74,7 @@ export class TransportAutoDiscovery {
|
||||
this.logger.info("First try to use getRTCTransports end point ...");
|
||||
try {
|
||||
const transportList = await doNetworkOperationWithRetry(async () =>
|
||||
client.getRTCTransports(),
|
||||
client._unstable_getRTCTransports(),
|
||||
);
|
||||
for (const type of this.transportTypes) {
|
||||
const first = transportList.find(
|
||||
|
||||
+2
-12
@@ -30,7 +30,7 @@ import { HomePage } from "./home/HomePage";
|
||||
import { LoginPage } from "./auth/LoginPage";
|
||||
import { RegisterPage } from "./auth/RegisterPage";
|
||||
import { RoomPage } from "./room/RoomPage";
|
||||
import { type ClientLoader, ClientProvider } from "./ClientContext";
|
||||
import { ClientProvider } from "./ClientContext";
|
||||
import { ErrorPage, LoadingPage } from "./FullScreenView";
|
||||
import { Initializer } from "./initializer";
|
||||
import { type WidgetHelpers } from "./widget";
|
||||
@@ -57,16 +57,6 @@ import { LeaveToHomeProvider } from "./LeaveToHomeContext";
|
||||
|
||||
const SentryRoute = Sentry.withSentryReactRouterV7Routing(Route);
|
||||
|
||||
// Element Call's own session, for when no host supplies a client.
|
||||
// - Passed in here, not imported by ClientProvider: this module is only in the
|
||||
// app builds, so the client setup and the store worker behind it stay out of
|
||||
// the component package (see the `loadClient` prop).
|
||||
// - Imported on demand, as it was before, so that a widget, which gets its
|
||||
// client from the host, never downloads it.
|
||||
// - Module-level, so that the provider's effect sees one stable loader.
|
||||
const loadStandaloneClient: ClientLoader = async (...args) =>
|
||||
(await import("./utils/spa")).initSPA(...args);
|
||||
|
||||
interface SimpleProviderProps {
|
||||
children: JSX.Element;
|
||||
}
|
||||
@@ -156,7 +146,7 @@ export const App: FC<Props> = ({ vm, widget }) => {
|
||||
|
||||
const content =
|
||||
loaded && clientReady ? (
|
||||
<ClientProvider client={widgetClient} loadClient={loadStandaloneClient}>
|
||||
<ClientProvider client={widgetClient}>
|
||||
<MediaDevicesContext value={vm.mediaDevices}>
|
||||
<BackgroundEffectsProvider effects={vm.backgroundEffects}>
|
||||
<Sentry.ErrorBoundary
|
||||
|
||||
@@ -11,11 +11,7 @@ import { BrowserRouter } from "react-router-dom";
|
||||
import { type MatrixClient } from "matrix-js-sdk";
|
||||
import { type FC } from "react";
|
||||
|
||||
import {
|
||||
type ClientLoader,
|
||||
ClientProvider,
|
||||
useClientState,
|
||||
} from "./ClientContext";
|
||||
import { ClientProvider, useClientState } from "./ClientContext";
|
||||
|
||||
const mockClient = (userId = "@alice:example.org"): MatrixClient =>
|
||||
({
|
||||
@@ -99,29 +95,10 @@ test("follows the client when the host swaps it", () => {
|
||||
|
||||
test("finds a client of its own when the host supplies none", async () => {
|
||||
const client = mockClient();
|
||||
const loadClient = vi
|
||||
.fn<ClientLoader>()
|
||||
.mockResolvedValue({ client, passwordlessUser: true });
|
||||
vi.doMock("./utils/spa", () => ({
|
||||
initSPA: vi.fn().mockResolvedValue({ client, passwordlessUser: true }),
|
||||
}));
|
||||
|
||||
const { container } = render(
|
||||
<BrowserRouter>
|
||||
<ClientProvider loadClient={loadClient}>
|
||||
<ShowClientState />
|
||||
</ClientProvider>
|
||||
</BrowserRouter>,
|
||||
);
|
||||
|
||||
// Nothing to show until a session has been restored or created
|
||||
expect(container.textContent).toBe("loading");
|
||||
await waitFor(() => expect(container.textContent).toBe("@alice:example.org"));
|
||||
// Given the session store to restore from and to clear on a stale session
|
||||
expect(loadClient).toHaveBeenCalledWith(
|
||||
expect.any(Function),
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
|
||||
test("has no one signed in when given neither a client nor a way to load one", async () => {
|
||||
const { container } = render(
|
||||
<BrowserRouter>
|
||||
<ClientProvider>
|
||||
@@ -130,5 +107,9 @@ test("has no one signed in when given neither a client nor a way to load one", a
|
||||
</BrowserRouter>,
|
||||
);
|
||||
|
||||
await waitFor(() => expect(container.textContent).toBe("unauthenticated"));
|
||||
// Nothing to show until a session has been restored or created
|
||||
expect(container.textContent).toBe("loading");
|
||||
await waitFor(() => expect(container.textContent).toBe("@alice:example.org"));
|
||||
|
||||
vi.doUnmock("./utils/spa");
|
||||
});
|
||||
|
||||
+12
-32
@@ -143,34 +143,9 @@ interface Props {
|
||||
* own.
|
||||
*/
|
||||
client?: MatrixClient;
|
||||
/**
|
||||
* How Element Call finds a client when none is supplied: the standalone app
|
||||
* restores or creates a session of its own. Stable, as a hook dependency.
|
||||
* Left out along with the client, no one is signed in.
|
||||
*
|
||||
* A prop, rather than this module importing `utils/spa` itself, because:
|
||||
* - This module is in the component build, and so is everything it imports,
|
||||
* dynamically or not: a dynamic import becomes a chunk in the package,
|
||||
* not a module left out of it.
|
||||
* - Behind `utils/spa` sits `utils/initClient` and the client store worker,
|
||||
* which the bundler emits as soon as it loads the import (see there).
|
||||
* - `App.tsx`, which the component never imports, is where the standalone
|
||||
* app passes the loader in; the component passes nothing, having the
|
||||
* host's client.
|
||||
*/
|
||||
loadClient?: ClientLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores or creates a session of Element Call's own, kept where the given
|
||||
* functions keep it. Resolves to null when no one is signed in.
|
||||
*/
|
||||
export type ClientLoader = (
|
||||
loadSession: () => Session | undefined,
|
||||
clearSession: () => void,
|
||||
) => Promise<InitResult | null>;
|
||||
|
||||
export const ClientProvider: FC<Props> = ({ children, client, loadClient }) => {
|
||||
export const ClientProvider: FC<Props> = ({ children, client }) => {
|
||||
const leaveToHome = useLeaveToHome();
|
||||
const hostBridge = useHostBridge();
|
||||
|
||||
@@ -204,13 +179,9 @@ export const ClientProvider: FC<Props> = ({ children, client, loadClient }) => {
|
||||
// React does in strict mode), we need to make sure not to doubly initialize
|
||||
// the client.
|
||||
if (initializing.current) return;
|
||||
if (loadClient === undefined) {
|
||||
setInitClientState(null);
|
||||
return;
|
||||
}
|
||||
initializing.current = true;
|
||||
|
||||
loadClient(loadSession, clearSession)
|
||||
loadClient()
|
||||
.then((initResult) => {
|
||||
setInitClientState(initResult);
|
||||
if (PosthogAnalytics.instance.isEnabled())
|
||||
@@ -218,7 +189,7 @@ export const ClientProvider: FC<Props> = ({ children, client, loadClient }) => {
|
||||
})
|
||||
.catch((err) => logger.error(err))
|
||||
.finally(() => (initializing.current = false));
|
||||
}, [client, loadClient]);
|
||||
}, [client]);
|
||||
|
||||
const changePassword = useCallback(
|
||||
async (password: string) => {
|
||||
@@ -389,6 +360,15 @@ export type InitResult = {
|
||||
passwordlessUser: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Restores or creates a session of Element Call's own. Only reached when no
|
||||
* client was supplied for it to use.
|
||||
*/
|
||||
async function loadClient(): Promise<InitResult | null> {
|
||||
const { initSPA } = await import("./utils/spa");
|
||||
return initSPA(loadSession, clearSession);
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
user_id: string;
|
||||
device_id: string;
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
type MatrixClient,
|
||||
} from "matrix-js-sdk";
|
||||
|
||||
import { initClient } from "../utils/initClient";
|
||||
import { initClient } from "../utils/matrix";
|
||||
import { type Session } from "../ClientContext";
|
||||
/**
|
||||
* This provides the login method to login using user credentials.
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
} from "matrix-js-sdk";
|
||||
import { logger } from "matrix-js-sdk/lib/logger";
|
||||
|
||||
import { initClient } from "../utils/initClient";
|
||||
import { initClient } from "../utils/matrix";
|
||||
import { type Session } from "../ClientContext";
|
||||
import { Config } from "../config/Config";
|
||||
import { useUrlParams } from "../UrlParams";
|
||||
|
||||
@@ -748,7 +748,8 @@ export const InCallView: FC<InCallViewProps> = ({
|
||||
|
||||
/**
|
||||
* How the tracks are captured and encoded, from Element Call's settings and
|
||||
* configuration.
|
||||
* configuration. Shared with the legacy SDK bundle so that a widget built from
|
||||
* it behaves like the app.
|
||||
*/
|
||||
export function captureSettings(): CaptureSettings {
|
||||
const config = Config.get();
|
||||
@@ -797,7 +798,8 @@ export function captureSettings(): CaptureSettings {
|
||||
/**
|
||||
* What the client needs from Element Call's configuration, settings and URL:
|
||||
* the SDK reads none of them itself. What to publish depends on the mute
|
||||
* switches and the devices, so the caller adds that.
|
||||
* switches and the devices, so the caller adds that. Shared with the legacy
|
||||
* SDK bundle so that a widget built from it behaves like the app.
|
||||
*/
|
||||
export function rtcSlotOptions(
|
||||
encryptionSystem: EncryptionSystem,
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { EventEmitter } from "events";
|
||||
import {
|
||||
afterEach,
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
type Mock,
|
||||
vi,
|
||||
} from "vitest";
|
||||
import {
|
||||
ClientEvent,
|
||||
createClient,
|
||||
type ICreateClientOpts,
|
||||
IndexedDBStore,
|
||||
MemoryStore,
|
||||
} from "matrix-js-sdk";
|
||||
import type * as MatrixSdk from "matrix-js-sdk";
|
||||
|
||||
import { initClient } from "./initClient";
|
||||
import { flushPromises } from "./test";
|
||||
|
||||
interface Store {
|
||||
startup: Mock<() => Promise<void>>;
|
||||
getSavedSyncToken: Mock<() => Promise<string | null>>;
|
||||
}
|
||||
|
||||
const store = (): Store => ({
|
||||
startup: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
|
||||
getSavedSyncToken: vi
|
||||
.fn<() => Promise<string | null>>()
|
||||
.mockResolvedValue(null),
|
||||
});
|
||||
|
||||
// The client is the SDK's business; what these tests are about is how it is
|
||||
// set up: its stores, its crypto, and when it counts as ready.
|
||||
vi.mock("matrix-js-sdk", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof MatrixSdk>()),
|
||||
createClient: vi.fn(),
|
||||
IndexedDBStore: vi.fn(function (this: Store) {
|
||||
Object.assign(this, store());
|
||||
}),
|
||||
MemoryStore: vi.fn(function (this: Store) {
|
||||
Object.assign(this, store());
|
||||
}),
|
||||
}));
|
||||
|
||||
interface FakeClient extends EventEmitter {
|
||||
store: Store;
|
||||
clearStores: Mock<() => Promise<void>>;
|
||||
initRustCrypto: Mock<() => Promise<void>>;
|
||||
startClient: Mock<() => Promise<void>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A client that, unless told otherwise, reports its first sync a tick after
|
||||
* being started, as the real one does once the network answers.
|
||||
*/
|
||||
function fakeClient(opts: ICreateClientOpts, syncs = true): FakeClient {
|
||||
const client = Object.assign(new EventEmitter(), {
|
||||
store: opts.store as unknown as Store,
|
||||
clearStores: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
|
||||
initRustCrypto: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
|
||||
startClient: vi.fn(async () => {
|
||||
if (!syncs) return;
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
client.emit(ClientEvent.Sync, "PREPARED", null);
|
||||
}),
|
||||
});
|
||||
return client;
|
||||
}
|
||||
|
||||
const options: ICreateClientOpts = { baseUrl: "https://hs.example.org" };
|
||||
const clients = vi.mocked(createClient) as unknown as Mock<
|
||||
(opts: ICreateClientOpts) => FakeClient
|
||||
>;
|
||||
const indexedDBStores = vi.mocked(IndexedDBStore);
|
||||
const memoryStores = vi.mocked(MemoryStore);
|
||||
|
||||
describe("initClient", () => {
|
||||
beforeEach(() => {
|
||||
clients.mockImplementation((opts) => fakeClient(opts));
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("hands back the client once its first sync is done", async () => {
|
||||
const client = await initClient(options, true);
|
||||
|
||||
expect(client).toBe(clients.mock.results[0].value);
|
||||
expect(client.startClient).toHaveBeenCalledWith({
|
||||
clientWellKnownPollPeriod: 600,
|
||||
});
|
||||
expect(clients).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
baseUrl: "https://hs.example.org",
|
||||
useAuthorizationHeader: true,
|
||||
localTimeoutMs: 5000,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the stores when restoring a session", async () => {
|
||||
const client = (await initClient(options, true)) as unknown as FakeClient;
|
||||
expect(client.clearStores).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears the stores before starting them when not restoring", async () => {
|
||||
const client = (await initClient(options, false)) as unknown as FakeClient;
|
||||
|
||||
expect(client.clearStores).toHaveBeenCalled();
|
||||
expect(client.clearStores.mock.invocationCallOrder[0]).toBeLessThan(
|
||||
client.store.startup.mock.invocationCallOrder[0],
|
||||
);
|
||||
});
|
||||
|
||||
it("syncs into memory where there is no IndexedDB", async () => {
|
||||
// jsdom has no indexedDB
|
||||
const client = await initClient(options, true);
|
||||
|
||||
expect(client.store).toBe(memoryStores.mock.instances[0]);
|
||||
expect(indexedDBStores).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("syncs into IndexedDB through a worker, where it was bundled", async () => {
|
||||
vi.stubGlobal("indexedDB", {});
|
||||
vi.stubEnv("DEV", false);
|
||||
|
||||
const client = await initClient(options, true);
|
||||
|
||||
expect(client.store).toBe(indexedDBStores.mock.instances[0]);
|
||||
const [storeOptions] = indexedDBStores.mock.calls[0];
|
||||
expect(storeOptions.dbName).toBe("element-call-sync");
|
||||
expect(storeOptions.workerFactory).toBeTypeOf("function");
|
||||
});
|
||||
|
||||
it("syncs into IndexedDB without a worker in development", async () => {
|
||||
vi.stubGlobal("indexedDB", {});
|
||||
vi.stubEnv("DEV", true);
|
||||
|
||||
await initClient(options, true);
|
||||
|
||||
// The dev server does not bundle the worker
|
||||
expect(indexedDBStores.mock.calls[0][0].workerFactory).toBeUndefined();
|
||||
});
|
||||
|
||||
it("falls back to memory when the IndexedDB store cannot start", async () => {
|
||||
vi.stubGlobal("indexedDB", {});
|
||||
const failing = store();
|
||||
failing.startup.mockRejectedValue(new Error("quota exceeded"));
|
||||
indexedDBStores.mockImplementationOnce(function (this: IndexedDBStore) {
|
||||
Object.assign(this, failing);
|
||||
});
|
||||
|
||||
const client = await initClient(options, true);
|
||||
|
||||
expect(client.store).toBe(memoryStores.mock.instances[0]);
|
||||
expect(client.store.startup).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("carries on without crypto when it cannot be initialised", async () => {
|
||||
clients.mockImplementation((opts) => {
|
||||
const client = fakeClient(opts);
|
||||
client.initRustCrypto.mockRejectedValue(new Error("stores in use"));
|
||||
return client;
|
||||
});
|
||||
|
||||
await expect(initClient(options, true)).resolves.toBeDefined();
|
||||
});
|
||||
|
||||
it("waits for a sync from the network, past one restored from the store", async () => {
|
||||
let client!: FakeClient;
|
||||
clients.mockImplementation((opts) => {
|
||||
client = fakeClient(opts, false);
|
||||
client.store.getSavedSyncToken.mockResolvedValue("saved");
|
||||
return client;
|
||||
});
|
||||
let ready = false;
|
||||
const result = initClient(options, true).then(() => (ready = true));
|
||||
await flushPromises();
|
||||
|
||||
// The first sync the client reports is the one restored from the store
|
||||
client.emit(ClientEvent.Sync, "PREPARED", null);
|
||||
await flushPromises();
|
||||
expect(ready).toBe(false);
|
||||
|
||||
client.emit(ClientEvent.Sync, "PREPARED", null);
|
||||
await result;
|
||||
expect(ready).toBe(true);
|
||||
});
|
||||
|
||||
it("fails when the first sync does", async () => {
|
||||
let client!: FakeClient;
|
||||
clients.mockImplementation((opts) => (client = fakeClient(opts, false)));
|
||||
const result = initClient(options, true);
|
||||
await flushPromises();
|
||||
|
||||
client.emit(ClientEvent.Sync, "ERROR", null, {
|
||||
error: new Error("offline"),
|
||||
});
|
||||
|
||||
await expect(result).rejects.toThrow("offline");
|
||||
});
|
||||
});
|
||||
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
Copyright 2022-2024 New Vector Ltd.
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import {
|
||||
ClientEvent,
|
||||
createClient,
|
||||
type ICreateClientOpts,
|
||||
IndexedDBStore,
|
||||
type MatrixClient,
|
||||
MemoryStore,
|
||||
} from "matrix-js-sdk";
|
||||
import { type ISyncStateData, type SyncState } from "matrix-js-sdk/lib/sync";
|
||||
import { logger } from "matrix-js-sdk/lib/logger";
|
||||
|
||||
// This import is why `initClient` has a module of its own:
|
||||
// - The bundler emits the worker file (11 MB: the Matrix SDK and its crypto
|
||||
// wasm) the moment it loads this import — whether or not anything ever calls
|
||||
// `new IndexedDBWorker()`. Tree shaking drops the call, not the file.
|
||||
// - So it may only sit in a module the component build never loads. It used
|
||||
// to be in `./matrix`, which the component reaches for the room URL helpers,
|
||||
// and the worker shipped in the package, unused and unusable by any host.
|
||||
import IndexedDBWorker from "../IndexedDBWorker?worker";
|
||||
import { getUrlParams } from "../UrlParams";
|
||||
import { Config } from "../config/Config";
|
||||
import { fallbackICEServerAllowed } from "./matrix";
|
||||
|
||||
const SYNC_STORE_NAME = "element-call-sync";
|
||||
|
||||
/**
|
||||
* Initialises and returns a new standalone Matrix Client.
|
||||
* This can only be called safely if no other client is running
|
||||
* otherwise rust crypto will throw since it is not ready to initialize a new session.
|
||||
* If another client is running make sure `.logout()` is called before executing this function.
|
||||
* @param clientOptions Object of options passed through to the client
|
||||
* @param restore If the rust crypto should be reset before the client initialization or
|
||||
* if the initialization should try to restore the crypto state from the indexDB.
|
||||
* @returns The MatrixClient instance
|
||||
*/
|
||||
export async function initClient(
|
||||
clientOptions: ICreateClientOpts,
|
||||
restore: boolean,
|
||||
): Promise<MatrixClient> {
|
||||
let indexedDB: IDBFactory | undefined;
|
||||
try {
|
||||
indexedDB = window.indexedDB;
|
||||
} catch (e) {
|
||||
logger.warn("Could not get indexDB from window.", e);
|
||||
}
|
||||
|
||||
// options we always pass to the client (stuff that we need in order to work)
|
||||
const baseOpts = {
|
||||
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
||||
isVoipWithNoMediaAllowed:
|
||||
Config.get().features?.feature_group_calls_without_video_and_audio,
|
||||
} as ICreateClientOpts;
|
||||
|
||||
if (indexedDB && localStorage) {
|
||||
baseOpts.store = new IndexedDBStore({
|
||||
indexedDB: window.indexedDB,
|
||||
localStorage,
|
||||
dbName: SYNC_STORE_NAME,
|
||||
// We can't use the worker in dev mode because Vite simply doesn't bundle workers
|
||||
// in dev mode: it expects them to use native modules. Ours don't, and even then only
|
||||
// Chrome supports it. (It bundles them fine in production mode.)
|
||||
workerFactory: import.meta.env.DEV
|
||||
? undefined
|
||||
: (): Worker => new IndexedDBWorker(),
|
||||
});
|
||||
} else if (localStorage) {
|
||||
baseOpts.store = new MemoryStore({ localStorage });
|
||||
}
|
||||
|
||||
// XXX: we read from the URL params in RoomPage too:
|
||||
// it would be much better to read them in one place and pass
|
||||
// the values around, but we initialise the matrix client in
|
||||
// many different places so we'd have to pass it into all of
|
||||
// them.
|
||||
const { e2eEnabled } = getUrlParams();
|
||||
if (!e2eEnabled) {
|
||||
logger.info("Disabling E2E: group call signalling will NOT be encrypted.");
|
||||
}
|
||||
|
||||
const client = createClient({
|
||||
...baseOpts,
|
||||
...clientOptions,
|
||||
useAuthorizationHeader: true,
|
||||
// Use a relatively low timeout for API calls: this is a realtime app
|
||||
// so we don't want API calls taking ages, we'd rather they just fail.
|
||||
localTimeoutMs: 5000,
|
||||
useE2eForGroupCall: e2eEnabled,
|
||||
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
||||
});
|
||||
|
||||
// In case of logging in a new matrix account but there is still crypto local store. This is needed for:
|
||||
// - We lost the auth tokens and cannot restore the client resulting in registering a new user.
|
||||
// - We start the sign in flow but are registered with a guest user. (It should additionally log out the guest before)
|
||||
// - A new account is created because of missing LocalStorage: "matrix-auth-store", but the crypto IndexDB is still available.
|
||||
if (!restore) {
|
||||
await client.clearStores();
|
||||
}
|
||||
|
||||
// Start client store.
|
||||
// Note: The `client.store` is used to store things like sync results. It's independent of
|
||||
// the cryptostore, and uses a separate indexeddb database.
|
||||
try {
|
||||
await client.store.startup();
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
"Error starting matrix client indexDB store. Falling back to memory store.",
|
||||
error,
|
||||
);
|
||||
client.store = new MemoryStore({ localStorage });
|
||||
await client.store.startup();
|
||||
}
|
||||
|
||||
// Also creates and starts any crypto related stores.
|
||||
try {
|
||||
await client.initRustCrypto();
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
err,
|
||||
"Make sure to clear client stores before initializing the rust crypto.",
|
||||
);
|
||||
}
|
||||
|
||||
// Once startClient is called, syncs are run asynchronously.
|
||||
// Also, sync completion is communicated only via events.
|
||||
// So, apply the event listener *before* starting the client.
|
||||
// Otherwise, a sync may complete before the listener gets applied,
|
||||
// and we will miss it.
|
||||
const syncPromise = waitForSync(client);
|
||||
await client.startClient({ clientWellKnownPollPeriod: 60 * 10 });
|
||||
await syncPromise;
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
async function waitForSync(client: MatrixClient): Promise<void> {
|
||||
// If there is a saved sync, the client will fire an additional sync event
|
||||
// for restoring it before it runs the first network sync.
|
||||
// However, the sync we want to wait for is the network sync,
|
||||
// as the saved sync may be missing some state.
|
||||
// Thus, don't resolve on the first sync when we know it's for the saved sync.
|
||||
let waitForSavedSync = !!(await client.store.getSavedSyncToken());
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const onSync = (
|
||||
state: SyncState,
|
||||
_old: SyncState | null,
|
||||
data?: ISyncStateData,
|
||||
): void => {
|
||||
if (state === "PREPARED") {
|
||||
if (waitForSavedSync) {
|
||||
waitForSavedSync = false;
|
||||
} else {
|
||||
client.removeListener(ClientEvent.Sync, onSync);
|
||||
resolve();
|
||||
}
|
||||
} else if (state === "ERROR") {
|
||||
client.removeListener(ClientEvent.Sync, onSync);
|
||||
reject(data?.error);
|
||||
}
|
||||
};
|
||||
client.on(ClientEvent.Sync, onSync);
|
||||
});
|
||||
}
|
||||
+152
-8
@@ -5,12 +5,21 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { ClientEvent, Preset, Visibility } from "matrix-js-sdk";
|
||||
import {
|
||||
ClientEvent,
|
||||
createClient,
|
||||
IndexedDBStore,
|
||||
MemoryStore,
|
||||
Preset,
|
||||
Visibility,
|
||||
} from "matrix-js-sdk";
|
||||
import { type ISyncStateData, type SyncState } from "matrix-js-sdk/lib/sync";
|
||||
import { logger } from "matrix-js-sdk/lib/logger";
|
||||
import { secureRandomBase64Url } from "matrix-js-sdk/lib/randomstring";
|
||||
|
||||
import type { MatrixClient, Room } from "matrix-js-sdk";
|
||||
import { generateUrlSearchParams } from "../UrlParams";
|
||||
import type { ICreateClientOpts, MatrixClient, Room } from "matrix-js-sdk";
|
||||
import IndexedDBWorker from "../IndexedDBWorker?worker";
|
||||
import { generateUrlSearchParams, getUrlParams } from "../UrlParams";
|
||||
import { Config } from "../config/Config";
|
||||
import { E2eeType } from "../e2ee/e2eeType";
|
||||
import {
|
||||
@@ -18,14 +27,149 @@ import {
|
||||
saveKeyForRoom,
|
||||
} from "../e2ee/sharedKeyManagement";
|
||||
|
||||
// Shared with the component, which reaches this module for the room URLs. So
|
||||
// nothing in here may import the client store worker: the bundler emits the
|
||||
// worker file on loading the import, used or not. The one function that needs
|
||||
// it, `initClient`, lives in `./initClient` for that reason.
|
||||
|
||||
export const fallbackICEServerAllowed =
|
||||
import.meta.env.VITE_FALLBACK_STUN_ALLOWED === "true";
|
||||
|
||||
const SYNC_STORE_NAME = "element-call-sync";
|
||||
|
||||
async function waitForSync(client: MatrixClient): Promise<void> {
|
||||
// If there is a saved sync, the client will fire an additional sync event
|
||||
// for restoring it before it runs the first network sync.
|
||||
// However, the sync we want to wait for is the network sync,
|
||||
// as the saved sync may be missing some state.
|
||||
// Thus, don't resolve on the first sync when we know it's for the saved sync.
|
||||
let waitForSavedSync = !!(await client.store.getSavedSyncToken());
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const onSync = (
|
||||
state: SyncState,
|
||||
_old: SyncState | null,
|
||||
data?: ISyncStateData,
|
||||
): void => {
|
||||
if (state === "PREPARED") {
|
||||
if (waitForSavedSync) {
|
||||
waitForSavedSync = false;
|
||||
} else {
|
||||
client.removeListener(ClientEvent.Sync, onSync);
|
||||
resolve();
|
||||
}
|
||||
} else if (state === "ERROR") {
|
||||
client.removeListener(ClientEvent.Sync, onSync);
|
||||
reject(data?.error);
|
||||
}
|
||||
};
|
||||
client.on(ClientEvent.Sync, onSync);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises and returns a new standalone Matrix Client.
|
||||
* This can only be called safely if no other client is running
|
||||
* otherwise rust crypto will throw since it is not ready to initialize a new session.
|
||||
* If another client is running make sure `.logout()` is called before executing this function.
|
||||
* @param clientOptions Object of options passed through to the client
|
||||
* @param restore If the rust crypto should be reset before the client initialization or
|
||||
* if the initialization should try to restore the crypto state from the indexDB.
|
||||
* @returns The MatrixClient instance
|
||||
*/
|
||||
export async function initClient(
|
||||
clientOptions: ICreateClientOpts,
|
||||
restore: boolean,
|
||||
): Promise<MatrixClient> {
|
||||
let indexedDB: IDBFactory | undefined;
|
||||
try {
|
||||
indexedDB = window.indexedDB;
|
||||
} catch (e) {
|
||||
logger.warn("Could not get indexDB from window.", e);
|
||||
}
|
||||
|
||||
// options we always pass to the client (stuff that we need in order to work)
|
||||
const baseOpts = {
|
||||
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
||||
isVoipWithNoMediaAllowed:
|
||||
Config.get().features?.feature_group_calls_without_video_and_audio,
|
||||
} as ICreateClientOpts;
|
||||
|
||||
if (indexedDB && localStorage) {
|
||||
baseOpts.store = new IndexedDBStore({
|
||||
indexedDB: window.indexedDB,
|
||||
localStorage,
|
||||
dbName: SYNC_STORE_NAME,
|
||||
// We can't use the worker in dev mode because Vite simply doesn't bundle workers
|
||||
// in dev mode: it expects them to use native modules. Ours don't, and even then only
|
||||
// Chrome supports it. (It bundles them fine in production mode.)
|
||||
workerFactory: import.meta.env.DEV
|
||||
? undefined
|
||||
: (): Worker => new IndexedDBWorker(),
|
||||
});
|
||||
} else if (localStorage) {
|
||||
baseOpts.store = new MemoryStore({ localStorage });
|
||||
}
|
||||
|
||||
// XXX: we read from the URL params in RoomPage too:
|
||||
// it would be much better to read them in one place and pass
|
||||
// the values around, but we initialise the matrix client in
|
||||
// many different places so we'd have to pass it into all of
|
||||
// them.
|
||||
const { e2eEnabled } = getUrlParams();
|
||||
if (!e2eEnabled) {
|
||||
logger.info("Disabling E2E: group call signalling will NOT be encrypted.");
|
||||
}
|
||||
|
||||
const client = createClient({
|
||||
...baseOpts,
|
||||
...clientOptions,
|
||||
useAuthorizationHeader: true,
|
||||
// Use a relatively low timeout for API calls: this is a realtime app
|
||||
// so we don't want API calls taking ages, we'd rather they just fail.
|
||||
localTimeoutMs: 5000,
|
||||
useE2eForGroupCall: e2eEnabled,
|
||||
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
||||
});
|
||||
|
||||
// In case of logging in a new matrix account but there is still crypto local store. This is needed for:
|
||||
// - We lost the auth tokens and cannot restore the client resulting in registering a new user.
|
||||
// - We start the sign in flow but are registered with a guest user. (It should additionally log out the guest before)
|
||||
// - A new account is created because of missing LocalStorage: "matrix-auth-store", but the crypto IndexDB is still available.
|
||||
if (!restore) {
|
||||
await client.clearStores();
|
||||
}
|
||||
|
||||
// Start client store.
|
||||
// Note: The `client.store` is used to store things like sync results. It's independent of
|
||||
// the cryptostore, and uses a separate indexeddb database.
|
||||
try {
|
||||
await client.store.startup();
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
"Error starting matrix client indexDB store. Falling back to memory store.",
|
||||
error,
|
||||
);
|
||||
client.store = new MemoryStore({ localStorage });
|
||||
await client.store.startup();
|
||||
}
|
||||
|
||||
// Also creates and starts any crypto related stores.
|
||||
try {
|
||||
await client.initRustCrypto();
|
||||
} catch (err) {
|
||||
logger.warn(
|
||||
err,
|
||||
"Make sure to clear client stores before initializing the rust crypto.",
|
||||
);
|
||||
}
|
||||
|
||||
// Once startClient is called, syncs are run asynchronously.
|
||||
// Also, sync completion is communicated only via events.
|
||||
// So, apply the event listener *before* starting the client.
|
||||
// Otherwise, a sync may complete before the listener gets applied,
|
||||
// and we will miss it.
|
||||
const syncPromise = waitForSync(client);
|
||||
await client.startClient({ clientWellKnownPollPeriod: 60 * 10 });
|
||||
await syncPromise;
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
export function roomAliasLocalpartFromRoomName(roomName: string): string {
|
||||
return roomName
|
||||
.trim()
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { type MatrixClient, MatrixError } from "matrix-js-sdk";
|
||||
|
||||
import { initClient } from "./initClient";
|
||||
import { initSPA } from "./spa";
|
||||
import { Config } from "../config/Config";
|
||||
import { type Session } from "../ClientContext";
|
||||
|
||||
vi.mock("./initClient", () => ({ initClient: vi.fn() }));
|
||||
|
||||
const session: Session = {
|
||||
user_id: "@alice:example.org",
|
||||
device_id: "AAAA",
|
||||
access_token: "secret",
|
||||
passwordlessUser: true,
|
||||
};
|
||||
const client = {} as MatrixClient;
|
||||
|
||||
describe("initSPA", () => {
|
||||
const clearSession = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(Config, "defaultHomeserverUrl").mockReturnValue(
|
||||
"https://hs.example.org",
|
||||
);
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("continues without a client when no session is stored", async () => {
|
||||
await expect(initSPA(() => undefined, clearSession)).resolves.toBeNull();
|
||||
|
||||
expect(initClient).not.toHaveBeenCalled();
|
||||
expect(clearSession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("restores the stored session into a client", async () => {
|
||||
vi.mocked(initClient).mockResolvedValue(client);
|
||||
|
||||
await expect(initSPA(() => session, clearSession)).resolves.toEqual({
|
||||
client,
|
||||
passwordlessUser: true,
|
||||
});
|
||||
|
||||
expect(initClient).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
baseUrl: "https://hs.example.org",
|
||||
accessToken: "secret",
|
||||
userId: "@alice:example.org",
|
||||
deviceId: "AAAA",
|
||||
}),
|
||||
// Restoring: the crypto store is the session's, not to be wiped
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it("drops a session the homeserver no longer knows", async () => {
|
||||
vi.mocked(initClient).mockRejectedValue(
|
||||
new MatrixError({ errcode: "M_UNKNOWN_TOKEN", error: "Unknown token" }),
|
||||
);
|
||||
|
||||
await expect(initSPA(() => session, clearSession)).resolves.toBeNull();
|
||||
expect(clearSession).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("drops the session and fails on any other error", async () => {
|
||||
vi.mocked(initClient).mockRejectedValue(new Error("offline"));
|
||||
|
||||
await expect(initSPA(() => session, clearSession)).rejects.toThrow(
|
||||
"offline",
|
||||
);
|
||||
expect(clearSession).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
+1
-2
@@ -9,8 +9,7 @@ import { type ICreateClientOpts, MatrixError } from "matrix-js-sdk";
|
||||
import { logger } from "matrix-js-sdk/lib/logger";
|
||||
|
||||
import { Config } from "../config/Config";
|
||||
import { fallbackICEServerAllowed } from "./matrix";
|
||||
import { initClient } from "./initClient";
|
||||
import { fallbackICEServerAllowed, initClient } from "./matrix";
|
||||
import type { InitResult, Session } from "../ClientContext";
|
||||
|
||||
export async function initSPA(
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@
|
||||
"./src/**/*.tsx",
|
||||
"./playwright/**/*.ts",
|
||||
"./sdk/**/*.ts",
|
||||
"./widget-rtc-sdk/**/*.ts",
|
||||
"./sdk-target-based-on-call-view-model/**/*.ts",
|
||||
"./component/**/*.ts",
|
||||
"./component/**/*.tsx"
|
||||
],
|
||||
|
||||
+10
-14
@@ -10,11 +10,11 @@ import { fileURLToPath } from "node:url";
|
||||
import { defineConfig } from "vite";
|
||||
import nodePolyfills from "vite-plugin-node-stdlib-browser";
|
||||
|
||||
import { withoutMatrixCrypto } from "./vite.config.ts";
|
||||
|
||||
// Config for the widget RTC SDK: a single bundle that wraps the sdk/ package for
|
||||
// a widget page. CI deploys it as the `pr<N>-widget-rtc-sdk` Netlify preview.
|
||||
export default defineConfig(({ mode }) => ({
|
||||
// Config for the legacy SDK bundle that predates the sdk/ package: a single
|
||||
// bundle of Element Call's CallViewModel for a plain web page or widget. It is
|
||||
// in production use, so it keeps building on top of the sdk/ package until its
|
||||
// consumers have moved to that package directly.
|
||||
export default defineConfig(() => ({
|
||||
worker: { format: "es" as const },
|
||||
resolve: {
|
||||
alias: {
|
||||
@@ -29,18 +29,14 @@ export default defineConfig(({ mode }) => ({
|
||||
// Otherwise the developer's own config.json in `public` is copied into the output
|
||||
publicDir: false,
|
||||
build: {
|
||||
// Optimised for production (`build:widget-rtc-sdk:production`): minified and
|
||||
// without source maps, which nothing uploads for this bundle. Readable in
|
||||
// development.
|
||||
minify: mode === "production",
|
||||
sourcemap: mode !== "production",
|
||||
sourcemap: true,
|
||||
manifest: true,
|
||||
lib: {
|
||||
formats: ["es" as const],
|
||||
entry: "./widget-rtc-sdk/main.ts",
|
||||
name: "WidgetRtcSdk",
|
||||
fileName: "widget-rtc-sdk",
|
||||
entry: "./sdk-target-based-on-call-view-model/main.ts",
|
||||
name: "MatrixrtcSdk",
|
||||
fileName: "matrixrtc-sdk",
|
||||
},
|
||||
},
|
||||
plugins: [nodePolyfills(), withoutMatrixCrypto()],
|
||||
plugins: [nodePolyfills()],
|
||||
}));
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { defineConfig, searchForWorkspaceRoot } from "vite";
|
||||
import { realpathSync } from "node:fs";
|
||||
import * as fs from "node:fs";
|
||||
|
||||
import { vitePluginsConfig, withoutMatrixCrypto } from "./vite.config.ts";
|
||||
|
||||
// Serves the widget RTC SDK's example widget, `widget-rtc-sdk/index.html`, with
|
||||
// its import of a deployed bundle pointed at the source instead, so that a host
|
||||
// can open the example as a widget against the code in the tree. The shape of
|
||||
// the SDK's development config.
|
||||
//
|
||||
// `vite build` and `vite preview` with this config serve the example bundled,
|
||||
// which the e2e tests use: unbundled, the page is hundreds of module requests,
|
||||
// on which Firefox now and then stalls.
|
||||
export default defineConfig(({ mode }) => {
|
||||
const allow = [searchForWorkspaceRoot(process.cwd())];
|
||||
for (const path of [
|
||||
"node_modules/matrix-js-sdk/node_modules/@matrix-org/matrix-sdk-crypto-wasm",
|
||||
"node_modules/@matrix-org/matrix-sdk-crypto-wasm",
|
||||
]) {
|
||||
try {
|
||||
allow.push(realpathSync(path));
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const base = vitePluginsConfig({ mode, html: false });
|
||||
return {
|
||||
...base,
|
||||
root: "widget-rtc-sdk",
|
||||
publicDir: false,
|
||||
plugins: [
|
||||
...(base.plugins ?? []),
|
||||
// As in the bundle itself (vite-widget-rtc-sdk.config.ts)
|
||||
withoutMatrixCrypto(),
|
||||
{
|
||||
name: "widget-rtc-sdk-from-source",
|
||||
transformIndexHtml: {
|
||||
order: "pre" as const,
|
||||
handler: (html: string): string =>
|
||||
html.replace(
|
||||
/https:\/\/pr\d+-widget-rtc-sdk--element-call\.netlify\.app\/widget-rtc-sdk\.js/,
|
||||
"/main.ts",
|
||||
),
|
||||
},
|
||||
},
|
||||
],
|
||||
server: {
|
||||
host: true,
|
||||
// After the app (3000), the component harness (3001) and the SDK harness
|
||||
// (3002), so that all of them can run at once
|
||||
port: 3003,
|
||||
strictPort: true,
|
||||
fs: { allow },
|
||||
// The same certificate the app uses, which Element Web's pages trust
|
||||
https: {
|
||||
key: fs.readFileSync("./backend/dev_tls_m.localhost.key"),
|
||||
cert: fs.readFileSync("./backend/dev_tls_m.localhost.crt"),
|
||||
},
|
||||
},
|
||||
// The e2e tests' copy, out of the way of `pnpm build:widget-rtc-sdk`'s
|
||||
build: { outDir: "../dist-widget-rtc-sdk-example", emptyOutDir: true },
|
||||
preview: { port: 3003, strictPort: true },
|
||||
worker: {
|
||||
format: "es" as const,
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -195,50 +195,6 @@ export default ({
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Leaves the Matrix SDK's own end-to-end encryption out of a build that only
|
||||
* runs as a widget: the host's client encrypts and decrypts for it, so the Rust
|
||||
* crypto chunk and its WASM (about 10 MB) would only be shipped, never loaded.
|
||||
* The SDK imports it dynamically, from `initRustCrypto` and `preloadRustCrypto`;
|
||||
* here those reject instead.
|
||||
*
|
||||
* With `standaloneClient`, the worker behind the standalone app's sync store
|
||||
* goes too: a second copy of the SDK with the crypto, which only a client of
|
||||
* Element Call's own uses.
|
||||
*/
|
||||
export function withoutMatrixCrypto({
|
||||
standaloneClient = false,
|
||||
}: { standaloneClient?: boolean } = {}): Plugin {
|
||||
const crypto = "\0without-matrix-crypto";
|
||||
const worker = "\0without-matrix-store-worker";
|
||||
const unavailable =
|
||||
"new Error('This build runs as a widget only: the host does the crypto')";
|
||||
return {
|
||||
name: "without-matrix-crypto",
|
||||
enforce: "pre",
|
||||
resolveId(source, importer): string | null {
|
||||
if (
|
||||
importer?.includes("matrix-js-sdk") &&
|
||||
/(^|\/)rust-crypto\/index(\.js)?$/.test(source)
|
||||
)
|
||||
return crypto;
|
||||
if (standaloneClient && source.endsWith("/IndexedDBWorker?worker"))
|
||||
return worker;
|
||||
return null;
|
||||
},
|
||||
load(id): string | null {
|
||||
if (id === crypto)
|
||||
return [
|
||||
`export async function initRustCrypto() { throw ${unavailable}; }`,
|
||||
`export async function preloadRustCrypto() { throw ${unavailable}; }`,
|
||||
].join("\n");
|
||||
if (id === worker)
|
||||
return `export default class { constructor() { throw ${unavailable}; } }`;
|
||||
return null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps a plugin's transform off MediaPipe's WASM loader. The loader is a
|
||||
* classic script that sets `self.ModuleFactory`; transformed into a module it
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
# Widget RTC SDK
|
||||
|
||||
A single JavaScript bundle for widget pages: it connects to the host (for
|
||||
example Element Web) over the widget API and wraps the `sdk/` package
|
||||
(`@element-hq/matrixrtc-sdk`) to join a MatrixRTC slot and exchange realtime
|
||||
data. A widget needs no Matrix login of its own: the host's client does the
|
||||
talking. It uses nothing of Element Call's app; a page that is not a widget
|
||||
uses the `sdk/` package directly (see `sdk/README.md`).
|
||||
|
||||
```ts
|
||||
import { createMatrixRTCSdk } from "<bundle url>/widget-rtc-sdk.js";
|
||||
|
||||
const sdk = await createMatrixRTCSdk("com.example.my-app"); // the rtc application
|
||||
sdk.join();
|
||||
sdk.connected$.subscribe((connected) => console.info({ connected }));
|
||||
sdk.remoteMembers$.subscribe((members) => console.info(members));
|
||||
sdk.data$.subscribe(({ rtcBackendIdentity, data }) =>
|
||||
console.info(rtcBackendIdentity, JSON.parse(data)),
|
||||
);
|
||||
await sdk.sendData({ hello: "world" });
|
||||
sdk.leave();
|
||||
```
|
||||
|
||||
`createMatrixRTCSdk(application = "m.call", id = "", sticky = false)` takes the
|
||||
slot's application and id, and whether to ask the host to keep the widget on
|
||||
screen while joined. The participation publishes no media; `sendData` and
|
||||
`data$` carry JSON over the `matrixRTC` data channel topic. When the
|
||||
participation has left, the widget asks the host to close it.
|
||||
|
||||
If the widget page has a `config.json` beside it, the bundle reads Element
|
||||
Call's `matrix_rtc_mode`, `livekit.livekit_service_url`,
|
||||
`sync_disconnect_grace_period_ms` and `matrix_rtc_session` from it.
|
||||
|
||||
## Using a pull request's build
|
||||
|
||||
CI builds the bundle for every pull request and deploys it to Netlify as
|
||||
`pr<N>-widget-rtc-sdk`, so a widget can import it without building anything:
|
||||
|
||||
```
|
||||
https://pr<N>-widget-rtc-sdk--element-call.netlify.app/widget-rtc-sdk.js
|
||||
```
|
||||
|
||||
1. Host a page that imports it. `index.html` in this folder is one: set the
|
||||
PR number in its import and serve the folder, for example with
|
||||
`npx serve -l 8080`.
|
||||
2. Add the page as a widget in a room in Element Web (see
|
||||
[Widget url](#widget-url)):
|
||||
|
||||
```
|
||||
/addwidget http://localhost:8080?widgetId=$matrix_widget_id&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id&baseUrl=$org.matrix.msc4039.matrix_base_url&roomId=$matrix_room_id
|
||||
```
|
||||
|
||||
3. Open the widget, approve the capabilities it asks for and allow it to
|
||||
verify your identity, ticking "Remember this": it asks for an OpenID token
|
||||
more than once. The example joins on load. Open it from a second account to see members and
|
||||
data arrive.
|
||||
|
||||
## Building it yourself
|
||||
|
||||
```
|
||||
pnpm install
|
||||
pnpm build:widget-rtc-sdk
|
||||
```
|
||||
|
||||
in the repository root writes the bundle to `dist/`, entry `widget-rtc-sdk.js`.
|
||||
Serve it with CORS, so a page on another origin can import it (for example
|
||||
`npx serve -l 8123 --cors dist`), and point the page's import at
|
||||
`http://localhost:8123/widget-rtc-sdk.js`.
|
||||
|
||||
`build:widget-rtc-sdk:production` (what CI deploys) minifies and leaves out
|
||||
source maps; `build:widget-rtc-sdk:development` does neither.
|
||||
|
||||
## Widget url
|
||||
|
||||
The host fills the widget url's parameters in when it opens the widget; the
|
||||
bundle reads them, from the query or from a query in the fragment, to start the
|
||||
widget API:
|
||||
|
||||
```
|
||||
widgetId = $matrix_widget_id
|
||||
userId = $matrix_user_id
|
||||
deviceId = $org.matrix.msc3819.matrix_device_id
|
||||
baseUrl = $org.matrix.msc4039.matrix_base_url
|
||||
roomId = $matrix_room_id
|
||||
```
|
||||
|
||||
`parentUrl` is added by the host. `sendNotificationType` (`ring` or
|
||||
`notification`) optionally notifies the room on join.
|
||||
|
||||
The `$` prefixed variables are replaced by Element Web when it opens the widget
|
||||
(for example `$matrix_user_id` becomes `@user:example.com`, url encoded as
|
||||
`%40user%3Aexample.com`).
|
||||
@@ -1,304 +0,0 @@
|
||||
/*
|
||||
Copyright 2025-2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The entrypoint of `pnpm build:widget-rtc-sdk`: a bundle for widget pages that
|
||||
* exposes `createMatrixRTCSdk`, to join a slot and exchange realtime data. It is
|
||||
* a thin widget wrapper around the `@element-hq/matrixrtc-sdk` package and uses
|
||||
* nothing of Element Call's app.
|
||||
*/
|
||||
|
||||
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
|
||||
import {
|
||||
createRoomWidgetClient,
|
||||
EventType,
|
||||
type MatrixClient,
|
||||
} from "matrix-js-sdk/lib/matrix";
|
||||
import { MatrixCapabilities, WidgetApi } from "matrix-widget-api";
|
||||
import {
|
||||
EMPTY,
|
||||
filter,
|
||||
fromEvent,
|
||||
map,
|
||||
type Observable,
|
||||
of,
|
||||
switchMap,
|
||||
take,
|
||||
} from "rxjs";
|
||||
import {
|
||||
type Behavior,
|
||||
createRTCSlot,
|
||||
type DelayedLeaveTimings,
|
||||
E2eeType,
|
||||
type LocalRTCMember,
|
||||
MatrixRTCMode,
|
||||
ObservableScope,
|
||||
type RemoteRTCMember,
|
||||
type RTCParticipation,
|
||||
type RTCParticipationOptions,
|
||||
} from "@element-hq/matrixrtc-sdk";
|
||||
|
||||
/** The data channel topic this bundle has always used, so that old and new builds talk. */
|
||||
const TOPIC = "matrixRTC";
|
||||
|
||||
/** The widget actions of Element Web's that this bundle takes part in. */
|
||||
const HANGUP_ACTION = "im.vector.hangup";
|
||||
const CLOSE_ACTION = "io.element.close";
|
||||
|
||||
export interface MatrixRTCSdk {
|
||||
/** Joins the slot; `connected$` tracks the progress. A second call does nothing. */
|
||||
join: () => void;
|
||||
leave: () => void;
|
||||
/** Ends everything. The sdk cannot be restarted; create a new one. */
|
||||
stop: () => void;
|
||||
connected$: Behavior<boolean>;
|
||||
/** Our own member, null until our membership has been seen in the room. */
|
||||
localMember$: Behavior<LocalRTCMember | null>;
|
||||
/** The other members of the slot, with the media the participation carries for them. */
|
||||
remoteMembers$: Behavior<RemoteRTCMember[]>;
|
||||
/** What other members sent with `sendData`. */
|
||||
data$: Observable<{ rtcBackendIdentity: string; data: string }>;
|
||||
/** Sends to every member on our transport. Rejects while not joined. */
|
||||
sendData: (data: unknown) => Promise<void>;
|
||||
sendRoomMessage: (message: string) => Promise<void>;
|
||||
}
|
||||
|
||||
/** The subset of Element Call's `config.json` this bundle reads. */
|
||||
interface WidgetConfig {
|
||||
matrix_rtc_mode?: MatrixRTCMode;
|
||||
livekit?: { livekit_service_url: string };
|
||||
sync_disconnect_grace_period_ms?: number;
|
||||
matrix_rtc_session?: {
|
||||
network_error_retry_ms?: number;
|
||||
membership_event_expiry_ms?: number;
|
||||
key_rotation_participant_limit?: number;
|
||||
delayed_leave?: DelayedLeaveTimings;
|
||||
delegated_delayed_leave?: DelayedLeaveTimings;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The parameters the host fills into the widget URL, from the query or from a
|
||||
* query in the fragment, as Element Call reads them.
|
||||
*/
|
||||
function widgetParams(): URLSearchParams {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const hash = window.location.hash;
|
||||
const fragmentQuery = hash.indexOf("?");
|
||||
if (fragmentQuery !== -1)
|
||||
for (const [key, value] of new URLSearchParams(
|
||||
hash.substring(fragmentQuery),
|
||||
))
|
||||
params.set(key, value);
|
||||
return params;
|
||||
}
|
||||
|
||||
/** `config.json` beside the widget page, if there is one. */
|
||||
async function loadConfig(): Promise<WidgetConfig> {
|
||||
try {
|
||||
const response = await fetch("config.json");
|
||||
if (response.ok) return (await response.json()) as WidgetConfig;
|
||||
} catch {}
|
||||
return {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the host over the widget API and builds the Matrix client that
|
||||
* routes every request through it, asking for what the SDK needs.
|
||||
*/
|
||||
function connectWidget(
|
||||
params: URLSearchParams,
|
||||
application: string,
|
||||
): { api: WidgetApi; client: MatrixClient; roomId: string } {
|
||||
const required = (name: string): string => {
|
||||
const value = params.get(name);
|
||||
if (!value) throw new Error(`The widget url needs ${name}`);
|
||||
return value;
|
||||
};
|
||||
const widgetId = required("widgetId");
|
||||
const parentUrl = required("parentUrl");
|
||||
const roomId = required("roomId");
|
||||
const userId = required("userId");
|
||||
const deviceId = required("deviceId");
|
||||
const baseUrl = required("baseUrl");
|
||||
|
||||
const api = new WidgetApi(widgetId, new URL(parentUrl).origin);
|
||||
api.requestCapability(MatrixCapabilities.AlwaysOnScreen);
|
||||
|
||||
const sendRecvEvent = [
|
||||
EventType.CallEncryptionKeysPrefix,
|
||||
EventType.RTCDecline,
|
||||
EventType.RTCMembership,
|
||||
];
|
||||
const sendRecvToDevice = [EventType.CallEncryptionKeysPrefix];
|
||||
const client = createRoomWidgetClient(
|
||||
api,
|
||||
{
|
||||
sendEvent: [
|
||||
...sendRecvEvent,
|
||||
EventType.CallNotify, // Sent as a deprecated fallback
|
||||
EventType.RTCNotification,
|
||||
EventType.RoomMessage, // For sendRoomMessage
|
||||
],
|
||||
receiveEvent: sendRecvEvent,
|
||||
sendState: [
|
||||
userId, // Legacy call membership events
|
||||
`_${userId}_${deviceId}_${application}`, // Session membership events
|
||||
`${userId}_${deviceId}_${application}`, // The above with no leading underscore, for room versions whose auth rules allow it
|
||||
].map((stateKey) => ({
|
||||
eventType: EventType.GroupCallMemberPrefix,
|
||||
stateKey,
|
||||
})),
|
||||
receiveState: [
|
||||
{ eventType: EventType.RoomCreate },
|
||||
{ eventType: EventType.RoomName },
|
||||
{ eventType: EventType.RoomMember },
|
||||
{ eventType: EventType.RoomEncryption },
|
||||
{ eventType: EventType.GroupCallMemberPrefix },
|
||||
{ eventType: EventType.RTCSlot },
|
||||
],
|
||||
sendToDevice: sendRecvToDevice,
|
||||
receiveToDevice: sendRecvToDevice,
|
||||
turnServers: false,
|
||||
rtcTransports: true,
|
||||
rtcLivekitGetToken: true,
|
||||
rtcLivekitDelegateDelayedLeave: true,
|
||||
sendDelayedEvents: true,
|
||||
updateDelayedEvents: true,
|
||||
sendSticky: true,
|
||||
receiveSticky: true,
|
||||
},
|
||||
roomId,
|
||||
{ baseUrl, userId, deviceId, timelineSupport: true },
|
||||
false,
|
||||
);
|
||||
return { api, client, roomId };
|
||||
}
|
||||
|
||||
export async function createMatrixRTCSdk(
|
||||
application = "m.call",
|
||||
id = "",
|
||||
sticky = false,
|
||||
): Promise<MatrixRTCSdk> {
|
||||
const logger = rootLogger.getChild("[MatrixRTCSdk]");
|
||||
const params = widgetParams();
|
||||
const { api, client, roomId } = connectWidget(params, application);
|
||||
const config = await loadConfig();
|
||||
await client.startClient();
|
||||
const room = client.getRoom(roomId);
|
||||
if (room === null) throw new Error("No room: the url needs a joined roomId");
|
||||
|
||||
const scope = new ObservableScope();
|
||||
const slot = createRTCSlot(scope, client, room, {
|
||||
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
|
||||
matrixRTCMode: config.matrix_rtc_mode ?? MatrixRTCMode.Compatibility,
|
||||
application,
|
||||
id,
|
||||
});
|
||||
const session = config.matrix_rtc_session;
|
||||
const sendNotificationType = params.get("sendNotificationType");
|
||||
const participationOptions: RTCParticipationOptions = {
|
||||
// A data-only participation: nothing is published
|
||||
publish: [],
|
||||
sendNotificationType:
|
||||
sendNotificationType === "ring" || sendNotificationType === "notification"
|
||||
? sendNotificationType
|
||||
: undefined,
|
||||
// Only what the config sets: the SDK spreads these over its defaults
|
||||
timings: Object.fromEntries(
|
||||
Object.entries({
|
||||
syncDisconnectGracePeriodMs: config.sync_disconnect_grace_period_ms,
|
||||
networkErrorRetryMs: session?.network_error_retry_ms,
|
||||
membershipEventExpiryMs: session?.membership_event_expiry_ms,
|
||||
keyRotationParticipantLimit: session?.key_rotation_participant_limit,
|
||||
delayedLeave: session?.delayed_leave,
|
||||
delegatedDelayedLeave: session?.delegated_delayed_leave,
|
||||
}).filter(([, value]) => value !== undefined),
|
||||
),
|
||||
fallbackTransport: config.livekit && {
|
||||
type: "livekit",
|
||||
livekit_service_url: config.livekit.livekit_service_url,
|
||||
},
|
||||
};
|
||||
|
||||
const failed =
|
||||
(what: string) =>
|
||||
(e: unknown): void =>
|
||||
logger.error(what, e);
|
||||
|
||||
const over = <T>(
|
||||
pick: (participation: RTCParticipation) => Observable<T>,
|
||||
fallback: T,
|
||||
): Behavior<T> =>
|
||||
scope.behavior(
|
||||
slot.participation$.pipe(
|
||||
switchMap((p) => (p === null ? of(fallback) : pick(p))),
|
||||
),
|
||||
);
|
||||
|
||||
const leave = (): void => slot.participation$.value?.leave();
|
||||
|
||||
// The host's hang-up button
|
||||
fromEvent<CustomEvent>(api, `action:${HANGUP_ACTION}`)
|
||||
.pipe(scope.bind())
|
||||
.subscribe((ev) => {
|
||||
ev.preventDefault();
|
||||
api.transport.reply(ev.detail, {});
|
||||
leave();
|
||||
});
|
||||
|
||||
return {
|
||||
join: (): void => {
|
||||
if (slot.participation$.value !== null) return;
|
||||
if (sticky)
|
||||
api
|
||||
.setAlwaysOnScreen(true)
|
||||
.catch(failed("Failed to make the widget sticky"));
|
||||
const participation = slot.join(participationOptions);
|
||||
// Once the hang-up has gone through, unstick and ask the host to close us
|
||||
participation.state$
|
||||
.pipe(
|
||||
filter((state) => state.kind === "left"),
|
||||
take(1),
|
||||
scope.bind(),
|
||||
)
|
||||
.subscribe(() => {
|
||||
api
|
||||
.setAlwaysOnScreen(false)
|
||||
.catch(failed("Failed to unstick the widget"))
|
||||
.then(async () => api.transport.send(CLOSE_ACTION, {}))
|
||||
.catch(failed("Failed to ask the host to close"))
|
||||
.finally(() => api.transport.stop());
|
||||
});
|
||||
},
|
||||
leave,
|
||||
stop: (): void => scope.end(),
|
||||
connected$: over(
|
||||
(p) => p.state$.pipe(map((state) => state.kind === "connected")),
|
||||
false,
|
||||
),
|
||||
localMember$: over((p) => p.localMember$, null),
|
||||
remoteMembers$: over((p) => p.remoteMembers$, []),
|
||||
// Attested members only: the SDK has matched the sender
|
||||
data$: slot.participation$.pipe(
|
||||
switchMap((p) => p?.data$ ?? EMPTY),
|
||||
filter(({ topic }) => topic === TOPIC),
|
||||
map(({ member, text }) => ({
|
||||
rtcBackendIdentity: member.rtcBackendIdentity,
|
||||
data: text,
|
||||
})),
|
||||
),
|
||||
sendData: async (data): Promise<void> => {
|
||||
const participation = slot.participation$.value;
|
||||
if (participation === null) throw new Error("Not joined");
|
||||
await participation.sendData(TOPIC, JSON.stringify(data));
|
||||
},
|
||||
sendRoomMessage: async (message): Promise<void> => {
|
||||
await client.sendTextMessage(room.roomId, message);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user