Skip to content

refactor(webstreams): fix layer boundaries and extract EncryptTransform#163

Draft
imaTik0 wants to merge 1 commit intorefactor/webstreams-dead-code-and-typesfrom
refactor/layer-boundaries
Draft

refactor(webstreams): fix layer boundaries and extract EncryptTransform#163
imaTik0 wants to merge 1 commit intorefactor/webstreams-dead-code-and-typesfrom
refactor/layer-boundaries

Conversation

@imaTik0
Copy link
Copy Markdown
Member

@imaTik0 imaTik0 commented Apr 17, 2026

Move service/ types into their owning layer:

  • src/service/EventDispatcher.ts → src/webStreams/EventDispatcher.ts (fix removeOnStateChangeListener filter, add JSDoc, wrap emit in try/catch)
  • src/service/WebRtcInterface.ts → src/webStreams/WebRtcInterface.ts (remove server event types that belong in native layer; add WebRtcMethodCall discriminated union; import Jsep from ApiTypes)

ApiTypes.ts:

  • Add Jsep interface (sdp + RTCSdpType) replacing the loose string in WebRtcInterface
  • Add StreamTrack interface (replaces StreamApi-local definition)
  • Remove dead types: StreamRemoteInfo, StreamList, StreamTrackList, PublishMeta, TrackInfo, StreamRoomInfo, StreamRoomList, TrackType

WebRtcInterfaceImpl.ts:

  • Update import paths to new locations
  • Replace { [K: string]: Function } methodsMap with typed MethodMap
  • Fix method bindings with .bind(this) and typed lambda for updateSessionId
  • methodCall now uses WebRtcMethodCall["name"] as key type

EncryptTransform.ts (new src/webStreams/worker/):

  • Extract per-frame AES-256-GCM logic out of the monolithic worker.ts
  • Pure class: no worker globals, no postMessage, no module state
  • encryptFrame takes lastRms as a parameter; decryptFrame returns rms | null

worker.ts:

  • Use EncryptTransform and new WorkerEvents discriminated unions
  • Add RTCRtpScriptTransform entry point (modern browsers) alongside postMessage
  • Add stop operation with AbortController-based pipeline cancellation
  • logPipelineError ignores AbortError in addition to "Destination stream closed"

KeyStore.ts:

  • Make setKeys async (was fire-and-forget; importKeyAndWipeMaterial is async)
  • Add getEncryptionExternalKeyId() and resolveKeyId() stubs (same value as getEncryptionKeyId for now; session-prefix refactor comes later)

Add EncryptTransform.test.ts covering audio/video encrypt+decrypt round-trips, header preservation, tamper detection, and short-frame passthrough.

Move service/ types into their owning layer:
- src/service/EventDispatcher.ts → src/webStreams/EventDispatcher.ts
  (fix removeOnStateChangeListener filter, add JSDoc, wrap emit in try/catch)
- src/service/WebRtcInterface.ts → src/webStreams/WebRtcInterface.ts
  (remove server event types that belong in native layer; add WebRtcMethodCall
  discriminated union; import Jsep from ApiTypes)

ApiTypes.ts:
- Add Jsep interface (sdp + RTCSdpType) replacing the loose string in WebRtcInterface
- Add StreamTrack interface (replaces StreamApi-local definition)
- Remove dead types: StreamRemoteInfo, StreamList, StreamTrackList, PublishMeta,
  TrackInfo, StreamRoomInfo, StreamRoomList, TrackType

WebRtcInterfaceImpl.ts:
- Update import paths to new locations
- Replace { [K: string]: Function } methodsMap with typed MethodMap
- Fix method bindings with .bind(this) and typed lambda for updateSessionId
- methodCall now uses WebRtcMethodCall["name"] as key type

EncryptTransform.ts (new src/webStreams/worker/):
- Extract per-frame AES-256-GCM logic out of the monolithic worker.ts
- Pure class: no worker globals, no postMessage, no module state
- encryptFrame takes lastRms as a parameter; decryptFrame returns rms | null

worker.ts:
- Use EncryptTransform and new WorkerEvents discriminated unions
- Add RTCRtpScriptTransform entry point (modern browsers) alongside postMessage
- Add stop operation with AbortController-based pipeline cancellation
- logPipelineError ignores AbortError in addition to "Destination stream closed"

KeyStore.ts:
- Make setKeys async (was fire-and-forget; importKeyAndWipeMaterial is async)
- Add getEncryptionExternalKeyId() and resolveKeyId() stubs
  (same value as getEncryptionKeyId for now; session-prefix refactor comes later)

Add EncryptTransform.test.ts covering audio/video encrypt+decrypt round-trips,
header preservation, tamper detection, and short-frame passthrough.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant