Repository navigation
Summary of Major Updates
- Core-Owned Stream Identity (breaking): Player integrations now pass raw manifest
properties(bitrate, codecs, width, height, ...) to the core, which computes each stream's full identity —identityHash(replacesStream.index),swarmId,streamSwarmId,infoHash— once at registration and freezes it on theStreamobject. Fully wire-compatible with v3: the default derivation is bit-identical (enforced by golden-vector tests), so default-config v4 peers keep sharing swarms with v3 peers. - Predictable Swarm Infohashes: The new
StreamConfig.streamSwarmIdBuildercallback and the Node-safep2p-media-loader-core/serversubpath let a server reproduce the exact infohash every client announces — e.g., to allowlist swarms on a private tracker. - Robust, Observable Stream Registration:
Core.addStreamIfNoneExistsnever throws; registration failures (includingstreamSwarmIdBuildercollisions) are reported via the newonStreamRegistrationErrorevent. TheswarmId-only configuration (no manifest URL) now works end to end. - Public API Sweep — No Internals Exposed: Every core output is a detached snapshot.
StreamWithSegments/SegmentWithStreamare internal; newCore.getStreams()andCore.getStreamSegmentRuntimeIds()cover enumeration and manifest diffing. - Consistent Event Payloads: Segment, peer, and tracker event payloads compose from shared base types; every peer event now reports the
trackerUrlthe peer was discovered from; the deprecatedSegmentLoadDetails.segmentUrlis removed. - Compatibility & Housekeeping: hls.js updated to
^1.7.0(runtime-compatible down to hls.js 1.3, verified by source inspection);wss://tracker.webtorrent.devadded to default trackers; a latent legacy-WebView crash in manifest URL handling fixed.
Migrating from v3? See the migration guide.
Detailed Changes
1. Core-Owned Stream Identity
- Registration API:
Core.addStreamIfNoneExiststakes{ runtimeId, type, properties }(StreamRegistration); the identity fields are computed by the core:// v3 core.addStreamIfNoneExists({ runtimeId: url, type: "main", index: generateStreamShortId({ bitrate, codecs, width, height }), }); // v4 core.addStreamIfNoneExists({ runtimeId: url, type: "main", properties: { bitrate, codecs, width, height }, });
Streamtype:Stream.indexis replaced byStream.identityHash; streams also carryproperties(frozen at registration),swarmId,streamSwarmId, andinfoHash— all computed once and immutable.- Cross-player parity: dedicated
stream-propertiesextractor modules in the hls.js and Shaka integrations, pinned by parity test suites asserting both players derive the sameidentityHashfor equivalent manifest metadata. - Wire compatibility: golden-vector tests freeze the v3 outputs of
generateStreamShortId/getStreamSwarmId/getStreamHashand assert the newcomputeStreamIdentityHash/computeStreamSwarmId/computeInfoHashproduce the same literals. No infohash changes unless a customstreamSwarmIdBuilderis configured.
2. Predictable Swarm Infohashes (Server-Side)
StreamConfig.streamSwarmIdBuilder: builds a custom stream swarm ID per stream from a rich context (swarmId,streamType, rawproperties,identityHash,defaultStreamSwarmId,peerProtocolVersion). The returned string is hashed into the announced infohash, so it fully defines which swarm the stream's peers join. Validated at registration: must be a non-empty string, and two different stream identities may never collide onto one ID (the guard compares the full(swarmId, type, identityHash)tuple).p2p-media-loader-core/serversubpath (Node.js 16+, ESM — CommonJS projects use dynamicimport()): exportscomputeStreamIdentityHash,computeStreamSwarmId,buildStreamSwarmId,computeInfoHash, andPEER_PROTOCOL_VERSIONso a server can reproduce the exact client-side derivation — with or without a custom builder. See "Predicting swarm infohashes on a server" in the API documentation.swarmIdandstreamSwarmIdBuilderare static-only: identity is derived once at registration, so dynamic configuration updates strip them instead of silently desynchronizing announced swarms.
3. Robust, Observable Stream Registration
addStreamIfNoneExistsnever throws: a stream that fails to register (unresolvable swarm ID, invalid or colliding builder output) stays unknown to the core — its segments play through the player's default loader without P2P, other streams are unaffected — and the failure is dispatched via the newonStreamRegistrationErrorevent. A misconfiguredstreamSwarmIdBuilderis now observable in production instead of only in debug logs, and custom integrations can no longer crash their player's manifest parsing on a registration error.swarmId-only configuration: vestigial guards requiringsetManifestResponseUrl()after registration were removed fromloadSegment/isSegmentLoadable, sonew Core({ swarmId })works end to end as documented.- Manifest URL handling (hls.js): the engine now uses hls.js's own resolved
MANIFEST_LOADEDURL (post-redirect for any loader honoring theresponse.urlcontract; verified identical in hls.js 1.3.5, 1.6.16, and 1.7.0). This also fixes a latent crash on legacy WebViews withoutxhr.responseURL.
4. Public API Sweep
- New
Core.getStreams(): lists all registered streams with their computed identities — e.g., every announced infohash — at any time, not just at registration. - New
Core.getStreamSegmentRuntimeIds(streamRuntimeId): a snapshotReadonlySetof the segment runtime IDs registered for a stream, in registration order — the manifest-diffing workflow that previously required reading the segments map. - Detached snapshots everywhere:
getStream()/getStreams()results and theonStreamAddedpayload are snapshots without the internal segments registry; streampropertiesare frozen. No core output shares live internal state, so consumers can neither corrupt the registry nor retain the per-stream segment graph in memory. StreamWithSegmentsandSegmentWithStreamare internal types now —Core.getStream()returns the plain stream.TypedErroris documented public API: the base class of all library errors and warnings;err instanceof TypedErrorcatches any of them, with the machine-readabletypediscriminator.
5. Consistent Event Payloads
- Shared base types: segment events extend
SegmentEventDetails, peer events extendPeerDetails, tracker events extend the newTrackerEventDetails— adding a correlation field to an event family is now a one-line change. PeerDetails.trackerUrl:onPeerConnect,onPeerClose,onPeerError,onPeerWarning, andonPeerConnectErrorall report which tracker the peer was discovered from.- New events:
onStreamAdded(fired once per registered stream with its computed identity, includinginfoHash) andonStreamRegistrationError(see above). - Removed: the deprecated
SegmentLoadDetails.segmentUrl— usesegment.url. ByteRange.start/endarereadonly;SegmentResponse.datadocuments that it may alias the P2P-upload cache buffer (copy withdata.slice(0)before transferring to a worker).
6. Compatibility & Housekeeping
- hls.js
^1.7.0: adapted to the newly typedNullableNetworkDetailsloader callbacks (nullinstead ofundefined/{}for P2P-served fragments — hls.js's own convention). Verified runtime-compatible with hls.js 1.3.x and 1.6.x by source inspection. - Default trackers: added
wss://tracker.webtorrent.dev. - Renamed:
PartialShakaEngineConfig→PartialShakaP2PEngineConfig(Shaka package, matches sibling type names); theSegmentStorageinterface'sstreamIdparameter is renamed tostreamSwarmId(the passed value is unchanged — custom storage implementations keep working). - Docs: new migration guide (linked from the README), a "Predicting swarm infohashes on a server" section in the API documentation, and API docs for v4.0.
Breaking Changes
| v3 | v4 |
|---|---|
Stream.index |
Stream.identityHash |
generateStreamShortId(props) |
computeStreamIdentityHash(properties) |
GenerateStreamShortIdProps |
StreamProperties |
StreamWithSegments, SegmentWithStream |
removed — internal; use Core.getStreamSegmentRuntimeIds() |
SegmentLoadDetails.segmentUrl (deprecated) |
removed — use segment.url |
PartialShakaEngineConfig (shaka package) |
PartialShakaP2PEngineConfig |
addStreamIfNoneExists throws on failure |
reports via onStreamRegistrationError, never throws |
swarmId changeable via dynamic config |
static-only (stripped from dynamic updates) |
Despite the API changes, the wire format is unchanged: v4 clients with default configuration join the same swarms as v3 clients.
Full Changelog: v3.0.1...v4.0.0