@svta/cml-cmcd 2.4.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { SfBareItem, SfItem, SfToken } from "@svta/cml-structured-field-values";
2
- import { ExclusiveRecord, HttpRequest, HttpResponse, ValueOf, ValueOrArray } from "@svta/cml-utils";
2
+ import { DeepReadonly, ExclusiveRecord, HttpRequest, HttpResponse, ValueOf, ValueOrArray } from "@svta/cml-utils";
3
3
 
4
4
  //#region src/CmcdEventType.d.ts
5
5
 
@@ -206,9 +206,17 @@ type CmcdEventType = ValueOf<typeof CmcdEventType>;
206
206
  * revisions to this specification. Clients SHOULD use a reverse-DNS
207
207
  * syntax when defining their own prefix.
208
208
  *
209
+ * The type requires a lowercase hyphenated string. At runtime a key
210
+ * must also satisfy {@link isCmcdCustomKey} — a lowercase first
211
+ * letter, then characters from `a-z 0-9 . -`, with a hyphen that is
212
+ * neither the first nor the last character — the subset of the CMCD
213
+ * custom-key grammar that survives RFC 8941 key serialization. Keys
214
+ * failing it are dropped during preparation. Lowercase reverse-DNS
215
+ * names satisfy all constraints.
216
+ *
209
217
  * @public
210
218
  */
211
- type CmcdCustomKey = `${string}-${string}`;
219
+ type CmcdCustomKey = Lowercase<`${string}-${string}`>;
212
220
  //#endregion
213
221
  //#region src/CmcdCustomValue.d.ts
214
222
  /**
@@ -1376,9 +1384,10 @@ type CmcdHeaderMap = Record<CmcdHeaderField, CmcdKey[]>;
1376
1384
  */
1377
1385
  type CmcdEncodeOptions = {
1378
1386
  /**
1379
- * The version of the CMCD specification to use.
1387
+ * The version of the CMCD specification to use. If not provided, the version
1388
+ * is inferred from the data's `v` key, defaulting to `CMCD_V2`.
1380
1389
  *
1381
- * @defaultValue `1`
1390
+ * @defaultValue `CMCD_V2`
1382
1391
  */
1383
1392
  version?: CmcdVersion;
1384
1393
  /**
@@ -1530,6 +1539,32 @@ declare const CMCD_PARAM = "CMCD";
1530
1539
  */
1531
1540
  declare const CMCD_REQUEST_KEYS: readonly ["ab", "bg", "bl", "br", "bs", "bsa", "bsd", "bsda", "cdn", "cid", "cs", "d", "dfa", "dl", "ec", "lab", "lb", "ltc", "msd", "mtp", "nor", "nr", "ot", "pb", "pr", "pt", "rtp", "sf", "sid", "sn", "st", "sta", "su", "tab", "tb", "tbl", "tpb", "v"];
1532
1541
  //#endregion
1542
+ //#region src/CMCD_REQUEST_PROVENANCE.d.ts
1543
+ /**
1544
+ * The `customData` key under which `CmcdReporter.createRequestReport()`
1545
+ * stamps session provenance on every request it returns.
1546
+ *
1547
+ * The value is a frozen `CmcdRequestProvenance` record: `sid` names the
1548
+ * session that issued the request, `cid` is the content id in effect when
1549
+ * the request was issued, and `data` carries the request's per-call data
1550
+ * as an encoded CMCD string. `CmcdReporter.recordResponseReceived()` reads
1551
+ * the record to attribute a late response to its session and to rebuild
1552
+ * the request-time report keys.
1553
+ * Spread and `Object.assign` carry the record through ordinary request
1554
+ * clones, but `JSON.stringify` and structured clone drop symbol-keyed
1555
+ * properties: read the value before such a boundary and restore it
1556
+ * afterward. The record itself survives JSON, and attribution is by the
1557
+ * `sid` value rather than object identity, so a revived plain-object copy
1558
+ * attributes exactly. A record naming a `sid` the reporter does not retain
1559
+ * drops the response rather than relabeling it.
1560
+ *
1561
+ * Backed by the symbol registry so duplicated copies of this library in
1562
+ * one bundle interoperate. The registry key is stable across versions.
1563
+ *
1564
+ * @public
1565
+ */
1566
+ declare const CMCD_REQUEST_PROVENANCE: unique symbol;
1567
+ //#endregion
1533
1568
  //#region src/CMCD_RESPONSE_KEYS.d.ts
1534
1569
  /**
1535
1570
  * CMCD v2 - Response-only and timing keys.
@@ -1694,8 +1729,102 @@ type CmcdDecodeOptions = {
1694
1729
  * @defaultValue false
1695
1730
  */
1696
1731
  convertToLatest?: boolean;
1732
+ /**
1733
+ * Controls how RFC 8941 token values are represented in the decoded
1734
+ * data. When omitted, tokens are reduced to plain strings, which cannot
1735
+ * be told apart from string values on re-encoding. `true` decodes
1736
+ * tokens as registry `Symbol`s and `false` as `SfToken` instances;
1737
+ * either preserved representation re-encodes as a bare token, so a
1738
+ * decode/encode round trip keeps the wire bytes.
1739
+ */
1740
+ useSymbol?: boolean;
1741
+ };
1742
+ //#endregion
1743
+ //#region src/CmcdTransformRequest.d.ts
1744
+ /**
1745
+ * The media request handed to a report transform, as a read-only view.
1746
+ *
1747
+ * The request is context only. A transform reads it to decide what to do with
1748
+ * the report and must not mutate it: it belongs to the caller, which may still
1749
+ * be using it. Every member is `readonly`, and `customData` is readonly at
1750
+ * every depth, so nested values cannot be written either.
1751
+ *
1752
+ * By default `customData` values are `unknown` rather than `any`, because the
1753
+ * library cannot know the player's shape. Narrow with a cast or bracket access
1754
+ * to read player-specific fields:
1755
+ *
1756
+ * ```ts
1757
+ * transform: (data, request) =>
1758
+ * request?.customData?.['requestType'] === 'segment' ? data : null
1759
+ * ```
1760
+ *
1761
+ * Supply `C` to describe the player's own `customData` instead and those reads
1762
+ * become typed dot access. Annotating one transform is enough: the reporter
1763
+ * infers `C` for every other transform in the same configuration.
1764
+ *
1765
+ * ```ts
1766
+ * type PlayerData = { requestType: string; };
1767
+ *
1768
+ * const segmentsOnly: CmcdEventReportTransform<PlayerData> = (data, request) =>
1769
+ * request?.customData?.requestType === 'segment' ? data : null
1770
+ * ```
1771
+ *
1772
+ * `C` is applied through {@link DeepReadonly}, so describing a nested shape
1773
+ * does not trade the no-mutation guarantee for typed reads.
1774
+ *
1775
+ * Two limits are worth knowing. A mutable body such as `FormData` or
1776
+ * `URLSearchParams` has mutating methods of its own that no type can block, and
1777
+ * JavaScript callers get no enforcement at all. Mutating the request through
1778
+ * either route is unsupported, and the outgoing report may reflect it.
1779
+ *
1780
+ * @typeParam C - The shape of the player's `customData`. Defaults to
1781
+ * `Record<string, unknown>`.
1782
+ *
1783
+ * @public
1784
+ */
1785
+ type CmcdTransformRequest<C = Record<string, unknown>> = Readonly<Omit<HttpRequest, "customData" | "headers">> & {
1786
+ /**
1787
+ * The headers associated with the request.
1788
+ */
1789
+ readonly headers?: Readonly<Record<string, string>>;
1790
+ /**
1791
+ * Any custom data the caller attached to the request, including the
1792
+ * player's own fields.
1793
+ */
1794
+ readonly customData?: DeepReadonly<C>;
1697
1795
  };
1698
1796
  //#endregion
1797
+ //#region src/CmcdEventReportTransform.d.ts
1798
+ /**
1799
+ * Transforms an event-mode CMCD report before it is queued for its
1800
+ * target.
1801
+ *
1802
+ * Return the data to continue, or `null` to cancel the report. The
1803
+ * `data` argument is a per-target copy, so mutating it in place affects
1804
+ * neither the reporter's persistent data nor the reports bound for
1805
+ * other targets.
1806
+ *
1807
+ * The `request` argument is the media request that triggered the event
1808
+ * when one exists (events recorded via
1809
+ * {@link CmcdReporter.recordResponseReceived}), otherwise `undefined`.
1810
+ * It is a read-only view, provided as context only and not to be
1811
+ * mutated; see {@link CmcdTransformRequest}.
1812
+ *
1813
+ * The reporter re-stamps `e` and `sid`, and assigns `sn` and `msd`,
1814
+ * after this function returns, so values written to those keys are
1815
+ * overwritten.
1816
+ *
1817
+ * Must not throw. Exceptions propagate to the caller that recorded the
1818
+ * event, which for time-interval events is the interval timer.
1819
+ *
1820
+ * @typeParam C - The shape of the player's `customData` on the request.
1821
+ * Defaults to `Record<string, unknown>`, whose values are
1822
+ * `unknown` and read with bracket access.
1823
+ *
1824
+ * @public
1825
+ */
1826
+ type CmcdEventReportTransform<C = Record<string, unknown>> = (data: Cmcd, request: CmcdTransformRequest<C> | undefined) => Cmcd | null;
1827
+ //#endregion
1699
1828
  //#region src/CmcdReportConfig.d.ts
1700
1829
  /**
1701
1830
  * Configuration for a CMCD report.
@@ -1723,9 +1852,12 @@ type CmcdReportConfig = {
1723
1852
  /**
1724
1853
  * Configuration for a CMCD event report.
1725
1854
  *
1855
+ * @typeParam C - The shape of the player's `customData` on the request that
1856
+ * triggered the event. Defaults to `Record<string, unknown>`.
1857
+ *
1726
1858
  * @public
1727
1859
  */
1728
- type CmcdEventReportConfig = CmcdReportConfig & {
1860
+ type CmcdEventReportConfig<C = Record<string, unknown>> = CmcdReportConfig & {
1729
1861
  /**
1730
1862
  * The version of the CMCD protocol to use. Must be
1731
1863
  * version 2 or higher for event reporting.
@@ -1759,6 +1891,21 @@ type CmcdEventReportConfig = CmcdReportConfig & {
1759
1891
  * @defaultValue `1`
1760
1892
  */
1761
1893
  batchSize?: number;
1894
+ /**
1895
+ * Transform applied to each of this target's event reports before
1896
+ * it is queued. Return the data to continue, or `null` to cancel
1897
+ * the report for this target.
1898
+ *
1899
+ * Scoped to this target only: targets that share a collector URL
1900
+ * each run their own transform, and a report cancelled here is
1901
+ * still sent to other targets that accept the event.
1902
+ *
1903
+ * @defaultValue `undefined`
1904
+ *
1905
+ * @example
1906
+ * {@includeCode ../test/CmcdReporter.test.ts#example-transform}
1907
+ */
1908
+ transform?: CmcdEventReportTransform<C>;
1762
1909
  };
1763
1910
  //#endregion
1764
1911
  //#region src/CmcdEventsValidationResult.d.ts
@@ -2098,6 +2245,32 @@ declare class CmcdReportRecorder {
2098
2245
  waitForEvents(options?: CmcdReportRecorderWaitOptions): Promise<CmcdRecordedReport[]>;
2099
2246
  }
2100
2247
  //#endregion
2248
+ //#region src/CmcdRequestReportTransform.d.ts
2249
+ /**
2250
+ * Transforms a request-mode CMCD report before it is applied to the
2251
+ * outgoing request.
2252
+ *
2253
+ * Return the data to continue, or `null` to skip CMCD decoration for
2254
+ * this request entirely. The `data` argument is a per-report copy, so
2255
+ * mutating it in place never affects the reporter's persistent data.
2256
+ * The `request` argument is the request passed to
2257
+ * `createRequestReport()`, as a read-only view. It is context only and
2258
+ * must not be mutated; see {@link CmcdTransformRequest}.
2259
+ *
2260
+ * The reporter re-stamps `sid` and assigns `sn` and `msd` after this
2261
+ * function returns, so values written to those keys are overwritten.
2262
+ *
2263
+ * Must not throw. Exceptions propagate to the `createRequestReport()`
2264
+ * caller.
2265
+ *
2266
+ * @typeParam C - The shape of the player's `customData` on the request.
2267
+ * Defaults to `Record<string, unknown>`, whose values are
2268
+ * `unknown` and read with bracket access.
2269
+ *
2270
+ * @public
2271
+ */
2272
+ type CmcdRequestReportTransform<C = Record<string, unknown>> = (data: Cmcd, request: CmcdTransformRequest<C>) => Cmcd | null;
2273
+ //#endregion
2101
2274
  //#region src/CmcdTransmissionMode.d.ts
2102
2275
  /**
2103
2276
  * CMCD `query` transmission mode.
@@ -2151,24 +2324,60 @@ type CmcdTransmissionMode = ValueOf<typeof CmcdTransmissionMode>;
2151
2324
  /**
2152
2325
  * Configuration for a CMCD request report.
2153
2326
  *
2327
+ * @typeParam C - The shape of the player's `customData` on the requests
2328
+ * passed to {@link CmcdReporter.createRequestReport}.
2329
+ * Defaults to `Record<string, unknown>`.
2330
+ *
2154
2331
  * @public
2155
2332
  */
2156
- type CmcdRequestReportConfig = CmcdReportConfig & {
2333
+ type CmcdRequestReportConfig<C = Record<string, unknown>> = CmcdReportConfig & {
2157
2334
  /**
2158
2335
  * The transmission mode to use.
2159
2336
  *
2160
2337
  * @defaultValue `CmcdTransmissionMode.QUERY`
2161
2338
  */
2162
2339
  transmissionMode?: CmcdTransmissionMode;
2340
+ /**
2341
+ * A map of CMCD header fields to the custom keys that should be
2342
+ * emitted in them when the transmission mode is
2343
+ * `CmcdTransmissionMode.HEADERS`. Custom keys not listed in any
2344
+ * shard are emitted in the `CMCD-Request` header. Standard keys
2345
+ * have fixed shards per the CMCD specification and cannot be
2346
+ * re-routed. Has no effect in query transmission mode or on event
2347
+ * reports.
2348
+ *
2349
+ * @defaultValue `undefined`
2350
+ */
2351
+ customHeaderMap?: Partial<CmcdHeaderMap>;
2352
+ /**
2353
+ * Transform applied to each request-mode report before encoding.
2354
+ * Return the data to continue, or `null` to skip CMCD decoration
2355
+ * for that request.
2356
+ *
2357
+ * Applies only to reports created by
2358
+ * {@link CmcdReporter.createRequestReport}. To transform event
2359
+ * reports, set `transform` on the event target instead.
2360
+ *
2361
+ * @defaultValue `undefined`
2362
+ *
2363
+ * @example
2364
+ * {@includeCode ../test/CmcdReporter.test.ts#example-transform}
2365
+ */
2366
+ transform?: CmcdRequestReportTransform<C>;
2163
2367
  };
2164
2368
  //#endregion
2165
2369
  //#region src/CmcdReporterConfig.d.ts
2166
2370
  /**
2167
2371
  * Configuration for a CMCD reporting component.
2168
2372
  *
2373
+ * @typeParam C - The shape of the player's `customData` on the requests this
2374
+ * reporter sees. Annotating a single `transform` is enough for
2375
+ * the rest of the configuration to infer it. Defaults to
2376
+ * `Record<string, unknown>`.
2377
+ *
2169
2378
  * @public
2170
2379
  */
2171
- type CmcdReporterConfig = CmcdRequestReportConfig & {
2380
+ type CmcdReporterConfig<C = Record<string, unknown>> = CmcdRequestReportConfig<C> & {
2172
2381
  /**
2173
2382
  * The session ID. If not provided, a new random session ID will be generated.
2174
2383
  *
@@ -2186,18 +2395,90 @@ type CmcdReporterConfig = CmcdRequestReportConfig & {
2186
2395
  *
2187
2396
  * @defaultValue `undefined`
2188
2397
  */
2189
- eventTargets?: CmcdEventReportConfig[];
2398
+ eventTargets?: CmcdEventReportConfig<C>[];
2399
+ /**
2400
+ * The number of ended sessions the reporter retains state for, in
2401
+ * addition to the current one. Retained sessions let a response that
2402
+ * completes after a `sid` change report under the session that issued
2403
+ * its request, with that session's data snapshot and sequence numbers.
2404
+ * `0` retains nothing, so stale responses are dropped. `Infinity` never
2405
+ * evicts, which also retains every session's unsent report queues.
2406
+ * Numbers are floored; every other input falls back to the default.
2407
+ *
2408
+ * @defaultValue 2
2409
+ */
2410
+ sessionRetention?: number;
2411
+ };
2412
+ //#endregion
2413
+ //#region src/CmcdReporterCustomData.d.ts
2414
+ /**
2415
+ * The `customData` a {@link CmcdReporter} method accepts, for a reporter typed
2416
+ * `C`.
2417
+ *
2418
+ * A reporter left on the default `C` constrains nothing, so requests carrying
2419
+ * any `customData` are accepted exactly as they were before `C` existed. A
2420
+ * reporter given a concrete `C` requires the request to satisfy it, because its
2421
+ * transforms read `customData` as `C` and would otherwise be handed a value
2422
+ * their type does not describe.
2423
+ *
2424
+ * @typeParam C - The reporter's `customData` type.
2425
+ *
2426
+ * @public
2427
+ */
2428
+ type CmcdReporterCustomData<C> = Record<string, unknown> extends C ? any : C;
2429
+ //#endregion
2430
+ //#region src/CmcdRequestProvenance.d.ts
2431
+ /**
2432
+ * The session-provenance record `CmcdReporter.createRequestReport()` stamps
2433
+ * on `customData` under `CMCD_REQUEST_PROVENANCE`.
2434
+ *
2435
+ * `sid` names the session that issued the request and is the attribution
2436
+ * key: `CmcdReporter.recordResponseReceived()` resolves it against the
2437
+ * reporter's retained sessions by value, never by object identity, so a
2438
+ * copy of the record that crosses a JSON boundary attributes exactly once
2439
+ * restored. Session identity rides on the caller's own `sid` values, which
2440
+ * CTA-5004-B expects to be unique per playback session: reusing a `sid`
2441
+ * replaces the retained namesake, so late responses of the replaced
2442
+ * session relabel onto the replacement.
2443
+ *
2444
+ * `cid` is the content id that was current when the record was minted. A
2445
+ * `RESPONSE_RECEIVED` event reports it in place of the session's current
2446
+ * `cid`, so a response that completes after a content change keeps the
2447
+ * meaning it had when the request was issued.
2448
+ *
2449
+ * `data` is the per-call data the request was created with, encoded as a
2450
+ * CMCD string. The reporter decodes it to rebuild the request-time report
2451
+ * keys for a `RESPONSE_RECEIVED` event, so the caller's inputs survive any
2452
+ * boundary the record itself is carried across. It is captured before the
2453
+ * request `transform` and key filter run, so it also rides requests the
2454
+ * reporter does not decorate.
2455
+ *
2456
+ * The reporter freezes every record it mints. The record is constructible:
2457
+ * a hand-built request may carry `{ sid }` to attribute to that session.
2458
+ * A record naming a `sid` the reporter does not retain is dropped.
2459
+ *
2460
+ * @public
2461
+ */
2462
+ type CmcdRequestProvenance = {
2463
+ readonly sid: string;
2464
+ readonly cid?: string;
2465
+ readonly data?: string;
2190
2466
  };
2191
2467
  //#endregion
2192
2468
  //#region src/CmcdRequestReport.d.ts
2193
2469
  /**
2194
2470
  * A report of a CMCD request.
2195
2471
  *
2472
+ * The provenance member is optional so the type stays constructible by
2473
+ * consumers that build or mock request reports; every request returned by
2474
+ * `CmcdReporter.createRequestReport()` carries it.
2475
+ *
2196
2476
  * @public
2197
2477
  */
2198
2478
  type CmcdRequestReport<D = unknown> = HttpRequest & {
2199
2479
  customData: {
2200
2480
  cmcd: Cmcd;
2481
+ [CMCD_REQUEST_PROVENANCE]?: CmcdRequestProvenance;
2201
2482
  } & D;
2202
2483
  headers: Record<string, string>;
2203
2484
  };
@@ -2206,18 +2487,37 @@ type CmcdRequestReport<D = unknown> = HttpRequest & {
2206
2487
  /**
2207
2488
  * The CMCD reporter.
2208
2489
  *
2490
+ * `C` describes the player's own `customData`, which the reporter passes
2491
+ * through to each configured `transform`. It is inferred from the
2492
+ * configuration, so annotating a single `transform` types the request in every
2493
+ * other one; the default leaves `customData` values `unknown`.
2494
+ *
2495
+ * @typeParam C - The shape of the player's `customData`. Defaults to
2496
+ * `Record<string, unknown>`.
2497
+ *
2209
2498
  * @see {@link https://cta-wave.github.io/Resources/common-media-client-data--cta-5004-b.html#reporting-modes-when-we-send-data | CTA-5004-B Reporting Modes}
2210
2499
  *
2211
2500
  * @public
2212
2501
  */
2213
- declare class CmcdReporter {
2502
+ declare class CmcdReporter<C = Record<string, unknown>> {
2214
2503
  private timeOrigin;
2215
- private data;
2216
2504
  private config;
2217
- private msd;
2218
- private eventTargets;
2219
- private lastEmitted;
2220
- private requestTarget;
2505
+ /**
2506
+ * Retained sessions keyed by `sid`, insertion-ordered oldest first, the
2507
+ * current session last. The ended-session count is capped by
2508
+ * `config.sessionRetention`. CTA-5004-B expects a `sid` to be unique
2509
+ * per playback session; a reused one replaces its retained namesake at
2510
+ * the newest position (see `startSession()`).
2511
+ */
2512
+ private sessions;
2513
+ private session;
2514
+ /**
2515
+ * Armed time-interval timers by target config. Timers outlive session
2516
+ * changes (their callbacks report into whichever session is current at
2517
+ * fire time), so they live on the reporter rather than the session.
2518
+ */
2519
+ private intervals;
2520
+ private started;
2221
2521
  private requester;
2222
2522
  /**
2223
2523
  * Creates a new CMCD reporter.
@@ -2227,10 +2527,15 @@ declare class CmcdReporter {
2227
2527
  * The default is a simple wrapper around the
2228
2528
  * native `fetch` API.
2229
2529
  */
2230
- constructor(config: Partial<CmcdReporterConfig>, requester?: (request: HttpRequest) => Promise<{
2530
+ constructor(config: Partial<CmcdReporterConfig<C>>, requester?: (request: HttpRequest) => Promise<{
2231
2531
  status: number;
2232
2532
  }>);
2233
2533
  /**
2534
+ * Creates the state for a new session: fresh counters, gates, queues and
2535
+ * dedup baseline for every configured target.
2536
+ */
2537
+ private createSession;
2538
+ /**
2234
2539
  * Starts the CMCD reporter. Called by the player when the reporter is enabled.
2235
2540
  *
2236
2541
  * Note: This fires an initial time-interval event immediately (synchronously)
@@ -2239,6 +2544,12 @@ declare class CmcdReporter {
2239
2544
  */
2240
2545
  start(): void;
2241
2546
  /**
2547
+ * Arms the time-interval timer for an event target when its config calls
2548
+ * for one. Returns whether a timer was armed. The callback reports into
2549
+ * whichever session is current when it fires.
2550
+ */
2551
+ private armInterval;
2552
+ /**
2242
2553
  * Stops the CMCD reporter. Called by the player when the reporter is disabled.
2243
2554
  *
2244
2555
  * @param flush - Whether to flush the event targets.
@@ -2278,10 +2589,36 @@ declare class CmcdReporter {
2278
2589
  *
2279
2590
  * A `sid` change resets the dedup baseline.
2280
2591
  *
2592
+ * `sid` and `msd` are session-owned: the reporter tracks them itself and
2593
+ * stamps them onto every outgoing report, so this method is the only way
2594
+ * to change them. A per-call value on {@link CmcdReporter.recordEvent},
2595
+ * {@link CmcdReporter.createRequestReport}, or
2596
+ * {@link CmcdReporter.recordResponseReceived} has no effect; response
2597
+ * attribution is by the provenance record alone.
2598
+ * `msd` must be a finite number of milliseconds between `0` and
2599
+ * `999_999_999_999_999` (the RFC 8941 integer maximum); it is rounded to
2600
+ * the nearest integer, and invalid values are ignored.
2601
+ *
2281
2602
  * @param data - The data to update.
2282
2603
  */
2283
2604
  update(data: Partial<Cmcd>): void;
2284
2605
  /**
2606
+ * Starts a new session: the current one is archived in place for stale
2607
+ * attribution, a fresh one takes over, ended sessions are drained, and
2608
+ * the oldest are evicted beyond the configured retention.
2609
+ *
2610
+ * The data store is carried over, and the archived snapshot is frozen:
2611
+ * the ended session's value graph is detached first, so nested values
2612
+ * the caller handed to `update()` can no longer mutate it, and the new
2613
+ * session starts from a shallow copy of the detached graph, so `cid`,
2614
+ * `br`, custom keys and the rest survive a `sid` change as they always
2615
+ * have. Sessions are keyed by `sid`, which CTA-5004-B expects to be
2616
+ * unique per playback session: reusing one replaces the retained
2617
+ * namesake, whose remaining state dies as if evicted and whose late
2618
+ * responses relabel onto the replacement.
2619
+ */
2620
+ private startSession;
2621
+ /**
2285
2622
  * Records an event. Called by the player when an event occurs.
2286
2623
  *
2287
2624
  * For state-change events (`PLAY_STATE`, `PLAYBACK_RATE`, `CONTENT_ID`,
@@ -2314,10 +2651,28 @@ declare class CmcdReporter {
2314
2651
  * @param data - Additional data to record with the event. This data
2315
2652
  * only applies to this event report, except for the dedup
2316
2653
  * field of a state-change event, which is also persisted
2317
- * into the reporter's data store.
2654
+ * into the reporter's data store. Session-owned keys
2655
+ * (`sid`, `msd`) supplied here are ignored; the reporter
2656
+ * stamps its own values.
2318
2657
  */
2319
2658
  recordEvent(type: CmcdEventType, data?: Partial<Cmcd>): void;
2320
2659
  /**
2660
+ * Records an event across every configured target of a session, applying
2661
+ * state-change dedup once before per-target fan-out.
2662
+ *
2663
+ * @param session - The session the event belongs to. Everything the event
2664
+ * touches (data store, dedup baseline, counters, queues)
2665
+ * is that session's; only `recordResponseReceived()` ever
2666
+ * passes an archived one.
2667
+ * @param type - The type of event to record.
2668
+ * @param data - Additional data to record with the event.
2669
+ * @param request - The media request that triggered the event, when
2670
+ * one exists. Only `recordResponseReceived()`
2671
+ * populates this; it is threaded through to each
2672
+ * target's `transform`.
2673
+ */
2674
+ private emitEvent;
2675
+ /**
2321
2676
  * Records an event for a target. Called by the reporter when an event occurs.
2322
2677
  *
2323
2678
  * @param target - The target to record the event for.
@@ -2326,9 +2681,28 @@ declare class CmcdReporter {
2326
2681
  * @param data - Additional data to record with the event. This data
2327
2682
  * only applies to this event report. Persistent data should
2328
2683
  * be updated using `update()`.
2684
+ * @param request - The media request that triggered the event, when
2685
+ * one exists. Passed to the target's `transform`.
2329
2686
  */
2330
2687
  private recordTargetEvent;
2331
2688
  /**
2689
+ * Stamps the reporter-owned fields on a finished event report, encodes it,
2690
+ * and pushes the wire line to the target's queue.
2691
+ *
2692
+ * Called after any transform has run, so a transform cannot bypass the
2693
+ * target's `events` filter via `e`, break `sn` continuity, or substitute
2694
+ * the session identity carried by `sid` and `msd`. Encoding here means a
2695
+ * report that cannot serialize throws inside the recording call that
2696
+ * produced it, instead of rejecting the batch send and re-queueing
2697
+ * forever.
2698
+ *
2699
+ * @param target - The target to queue the report for.
2700
+ * @param config - The configuration for the target.
2701
+ * @param report - The finished report data.
2702
+ * @param type - The type of event being reported.
2703
+ */
2704
+ private queueTargetEvent;
2705
+ /**
2332
2706
  * Records a response-received event. Called by the player when a media
2333
2707
  * request response has been fully received.
2334
2708
  *
@@ -2343,14 +2717,63 @@ declare class CmcdReporter {
2343
2717
  * Additional keys like `ttfbb`, `cmsdd`, `cmsds`, and `smrt` can be
2344
2718
  * supplied via the `data` parameter if the player has access to them.
2345
2719
  *
2720
+ * The request's `customData` is generic, so a player can pass a request
2721
+ * carrying its own taxonomy (e.g. `{ requestType: 'segment' }`) without a
2722
+ * cast. The reporter reads only the provenance record that
2723
+ * {@link CmcdReporter.createRequestReport} writes there; every other key
2724
+ * is left untouched and stays visible to an event target's `transform`
2725
+ * via its `request` argument.
2726
+ *
2727
+ * A reporter given a concrete `C` requires the request to satisfy it, so a
2728
+ * request the configured transforms could not read is rejected here rather
2729
+ * than reaching them; see {@link CmcdReporterCustomData}.
2730
+ *
2731
+ * The event is attributed to the session that issued the request, and to
2732
+ * nothing else: the provenance record that
2733
+ * {@link CmcdReporter.createRequestReport} stored on the request's
2734
+ * `customData` (under {@link CMCD_REQUEST_PROVENANCE}) selects the
2735
+ * session by its `sid`, or the response is dropped rather than
2736
+ * relabeled. There is no other key: a record lost to a serialization
2737
+ * boundary (and not restored; see the bridge notes on
2738
+ * {@link CMCD_REQUEST_PROVENANCE}) and a session no longer retained
2739
+ * (see `CmcdReporterConfig.sessionRetention`) both drop, and a per-call
2740
+ * `data.sid` cannot substitute. The record is honored wherever its
2741
+ * `sid` resolves: a request decorated by another reporter configured
2742
+ * with the same session attributes here, as does a hand-built record.
2743
+ * A response that completes after a `sid` change reports under its own
2744
+ * retained session, with that session's data snapshot and sequence
2745
+ * numbers.
2746
+ *
2747
+ * Request-time report keys come from the record's encoded per-call
2748
+ * `data` snapshot, decoded fresh per response, and the record's `cid`
2749
+ * reports in place of the session's current one, so a response that
2750
+ * completes after a mid-session content change keeps the meaning it
2751
+ * had when its request was issued.
2752
+ *
2753
+ * @typeParam RD - The `customData` this request carries. Defaults to the
2754
+ * reporter's own `C`.
2755
+ *
2346
2756
  * @param response - The HTTP response received.
2347
2757
  * @param data - Additional CMCD data to include with the event.
2348
2758
  * Values provided here override any auto-derived values.
2759
+ * Session-owned keys (`sid`, `msd`) supplied here are
2760
+ * ignored; attribution is by the provenance record alone.
2349
2761
  */
2350
- recordResponseReceived(response: HttpResponse<HttpRequest<{
2762
+ recordResponseReceived<RD extends CmcdReporterCustomData<C> = C>(response: HttpResponse<HttpRequest<RD & {
2351
2763
  cmcd?: Cmcd;
2764
+ [CMCD_REQUEST_PROVENANCE]?: CmcdRequestProvenance;
2352
2765
  }>>, data?: Partial<Cmcd>): void;
2353
2766
  /**
2767
+ * Resolves the session a response belongs to: the provenance record's
2768
+ * `sid` names one of this reporter's retained sessions, or the response
2769
+ * is dropped. There is no other key — a record that was lost, or that
2770
+ * names an evicted or never-seen `sid`, resolves nothing, and
2771
+ * attributing it anywhere else would relabel it. The `sid` is read
2772
+ * structurally, so a JSON-revived copy of a record attributes exactly,
2773
+ * and a hand-built record naming a retained session is honored.
2774
+ */
2775
+ private resolveSession;
2776
+ /**
2354
2777
  * Applies the CMCD request report data to the request. Called by the player
2355
2778
  * before sending the request.
2356
2779
  *
@@ -2367,16 +2790,36 @@ declare class CmcdReporter {
2367
2790
  */
2368
2791
  isRequestReportingEnabled(): boolean;
2369
2792
  /**
2793
+ * The provenance record to stamp on a returned request: the session's
2794
+ * frozen base record, extended with the caller's per-call data encoded
2795
+ * as a CMCD string when there is any. Requests without per-call data
2796
+ * share the base record, so decoration stays a single property write.
2797
+ * The snapshot never contains reporter-stamped fields, and a value that
2798
+ * cannot encode contributes no snapshot rather than failing the
2799
+ * request; its response still attributes and reports derived keys over
2800
+ * session data, matching `decodeSnapshot()`'s tolerance on the read
2801
+ * side.
2802
+ */
2803
+ private createRequestProvenance;
2804
+ /**
2370
2805
  * Creates a new request with the CMCD request report data applied. Called by the player
2371
2806
  * before sending the request.
2372
2807
  *
2808
+ * A reporter given a concrete `C` requires the request's `customData` to
2809
+ * satisfy it, so a request the configured transform could not read is
2810
+ * rejected here rather than reaching it; see
2811
+ * {@link CmcdReporterCustomData}.
2812
+ *
2813
+ * @typeParam R - The request being decorated. Its `customData` must satisfy
2814
+ * the reporter's `C`.
2815
+ *
2373
2816
  * @param request - The request to apply the CMCD request report to.
2374
2817
  * @param data - The data to apply to the request. This data only
2375
2818
  * applies to this request report. Persistent data
2376
2819
  * should be updated using `update()`.
2377
2820
  * @returns The request with the CMCD request report applied.
2378
2821
  */
2379
- createRequestReport<R extends HttpRequest = HttpRequest>(request: R, data?: Partial<Cmcd>): R & CmcdRequestReport<R["customData"]>;
2822
+ createRequestReport<R extends HttpRequest<CmcdReporterCustomData<C>> = HttpRequest<C>>(request: R, data?: Partial<Cmcd>): R & CmcdRequestReport<R["customData"]>;
2380
2823
  /**
2381
2824
  * Processes the event targets. Called by the reporter when an event occurs.
2382
2825
  *
@@ -2387,23 +2830,24 @@ declare class CmcdReporter {
2387
2830
  * Sends an event report. Called by the reporter when a batch is ready to be sent.
2388
2831
  *
2389
2832
  * @param config - The target config to send the event report to.
2390
- * @param data - The data to send in the event report.
2833
+ * @param data - The encoded report lines to send in the event report.
2391
2834
  */
2392
2835
  private sendEventReport;
2393
2836
  /**
2394
- * Cancels the time-interval timer for an event target and clears the stored id.
2395
- * Safe to call when no timer is armed (clearInterval(undefined) is a no-op).
2837
+ * Cancels the time-interval timer for an event target config and clears the
2838
+ * stored id. Safe to call when no timer is armed (clearInterval(undefined)
2839
+ * is a no-op).
2396
2840
  */
2397
2841
  private disarmInterval;
2398
2842
  /**
2399
- * Permanently removes an event target: cancels its timer and removes it from the
2400
- * eventTargets map. Used when the collector signals the target is gone (HTTP 410).
2843
+ * Silences an event target URL for the remainder of its session: drops the
2844
+ * queues and blocks further enqueues for every config in the session that
2845
+ * reports to the URL, per CTA-5004-B's per-target-URL suppression scope,
2846
+ * and cancels their timers when the session is the live one. Used when the
2847
+ * collector signals the target is gone (HTTP 410). A session started after
2848
+ * the disposal is unaffected, since it gets fresh target state.
2401
2849
  */
2402
2850
  private disposeEventTarget;
2403
- /**
2404
- * Resets the session related data. Called when the session ID changes.
2405
- */
2406
- private resetSession;
2407
2851
  }
2408
2852
  //#endregion
2409
2853
  //#region src/CmcdValidationOptions.d.ts
@@ -2572,7 +3016,10 @@ declare function groupCmcdHeaders(cmcd: Cmcd, customHeaderMap?: Partial<CmcdHead
2572
3016
  //#endregion
2573
3017
  //#region src/isCmcdCustomKey.d.ts
2574
3018
  /**
2575
- * Check if a key is a custom key.
3019
+ * Check if a key is a valid custom key: a lowercase first letter, then
3020
+ * characters from `a-z 0-9 . -`, with a hyphen that is neither the first
3021
+ * nor the last character. These are the CTA-5004-B custom-key rules
3022
+ * restricted to names that survive RFC 8941 key serialization.
2576
3023
  *
2577
3024
  * @param key - The key to check.
2578
3025
  *
@@ -2683,6 +3130,9 @@ declare function isCmcdV2Data(data: CmcdData): data is CmcdV2Data;
2683
3130
  * @param options - Options for encoding.
2684
3131
  *
2685
3132
  * @public
3133
+ *
3134
+ * @example
3135
+ * {@includeCode ../test/prepareCmcdData.test.ts#example}
2686
3136
  */
2687
3137
  declare function prepareCmcdData(obj: Record<string, any>, options?: CmcdEncodeOptions): Cmcd;
2688
3138
  //#endregion
@@ -2910,5 +3360,5 @@ declare function validateCmcdStructure(data: Record<string, unknown>, options?:
2910
3360
  */
2911
3361
  declare function validateCmcdValues(data: Record<string, unknown>, options?: CmcdValidationOptions): CmcdValidationResult;
2912
3362
  //#endregion
2913
- export { CMCD_DEFAULT_TIME_INTERVAL, CMCD_EVENT_AD_BREAK_END, CMCD_EVENT_AD_BREAK_START, CMCD_EVENT_AD_END, CMCD_EVENT_AD_START, CMCD_EVENT_BACKGROUNDED_MODE, CMCD_EVENT_BITRATE_CHANGE, CMCD_EVENT_CONTENT_ID, CMCD_EVENT_CUSTOM_EVENT, CMCD_EVENT_ERROR, CMCD_EVENT_KEYS, CMCD_EVENT_MODE, CMCD_EVENT_MUTE, CMCD_EVENT_PLAYBACK_RATE, CMCD_EVENT_PLAYER_COLLAPSE, CMCD_EVENT_PLAYER_EXPAND, CMCD_EVENT_PLAY_STATE, CMCD_EVENT_RESPONSE_RECEIVED, CMCD_EVENT_SKIP, CMCD_EVENT_TIME_INTERVAL, CMCD_EVENT_UNMUTE, CMCD_FORMATTER_MAP, CMCD_HEADERS, CMCD_HEADER_FIELDS, CMCD_HEADER_MAP, CMCD_JSON, CMCD_KEYS, CMCD_MIME_TYPE, CMCD_OBJECT, CMCD_PARAM, CMCD_QUERY, CMCD_RECORDED_REPORT_MODE_EVENT, CMCD_RECORDED_REPORT_MODE_HEADER, CMCD_RECORDED_REPORT_MODE_QUERY, CMCD_RECORDED_REQUEST_TYPE_EVENT, CMCD_RECORDED_REQUEST_TYPE_MANIFEST, CMCD_RECORDED_REQUEST_TYPE_SEGMENT, CMCD_RECORDED_REQUEST_TYPE_UNKNOWN, CMCD_REQUEST, CMCD_REQUEST_KEYS, CMCD_REQUEST_MODE, CMCD_RESPONSE_KEYS, CMCD_SESSION, CMCD_STATUS, CMCD_V1, CMCD_V1_KEYS, CMCD_V2, CMCD_VALIDATION_SEVERITY_ERROR, CMCD_VALIDATION_SEVERITY_WARNING, Cmcd, CmcdCustomKey, CmcdCustomValue, CmcdData, CmcdDataValidationResult, CmcdDecodeOptions, CmcdEncodeOptions, CmcdEvent, CmcdEventReportConfig, CmcdEventType, CmcdEventsValidationResult, CmcdFormatter, CmcdFormatterMap, CmcdFormatterOptions, CmcdHeaderField, CmcdHeaderKey, CmcdHeaderMap, CmcdHeaderValue, CmcdKey, CmcdObjectType, CmcdObjectTypeList, CmcdPlayerState, CmcdRecordedReport, CmcdRecordedReportMode, CmcdRecordedRequestType, CmcdReportConfig, CmcdReportRecorder, CmcdReportRecorderOptions, CmcdReportRecorderWaitOptions, CmcdReporter, CmcdReporterConfig, CmcdReportingMode, CmcdRequest, CmcdRequestDeliver, CmcdRequestKey, CmcdRequestReport, CmcdRequestReportConfig, CmcdResponse, CmcdStreamType, CmcdStreamingFormat, CmcdTransmissionMode, CmcdTransportAdapter, CmcdV1, CmcdV1Data, CmcdV2Data, CmcdValidationIssue, CmcdValidationOptions, CmcdValidationResult, CmcdValidationSeverity, CmcdValue, CmcdVersion, appendCmcdHeaders, appendCmcdQuery, createFetchTransport, createXhrTransport, decodeCmcd, encodeCmcd, fromCmcdHeaders, fromCmcdQuery, fromCmcdUrl, groupCmcdHeaders, isCmcdCustomKey, isCmcdEventKey, isCmcdRequestKey, isCmcdResponseReceivedKey, isCmcdV1Data, isCmcdV1Key, isCmcdV2Data, prepareCmcdData, toCmcdHeaders, toCmcdQuery, toCmcdUrl, toCmcdValue, validateCmcd, validateCmcdEventReport, validateCmcdEvents, validateCmcdHeaders, validateCmcdKeys, validateCmcdRequest, validateCmcdStructure, validateCmcdValues };
3363
+ export { CMCD_DEFAULT_TIME_INTERVAL, CMCD_EVENT_AD_BREAK_END, CMCD_EVENT_AD_BREAK_START, CMCD_EVENT_AD_END, CMCD_EVENT_AD_START, CMCD_EVENT_BACKGROUNDED_MODE, CMCD_EVENT_BITRATE_CHANGE, CMCD_EVENT_CONTENT_ID, CMCD_EVENT_CUSTOM_EVENT, CMCD_EVENT_ERROR, CMCD_EVENT_KEYS, CMCD_EVENT_MODE, CMCD_EVENT_MUTE, CMCD_EVENT_PLAYBACK_RATE, CMCD_EVENT_PLAYER_COLLAPSE, CMCD_EVENT_PLAYER_EXPAND, CMCD_EVENT_PLAY_STATE, CMCD_EVENT_RESPONSE_RECEIVED, CMCD_EVENT_SKIP, CMCD_EVENT_TIME_INTERVAL, CMCD_EVENT_UNMUTE, CMCD_FORMATTER_MAP, CMCD_HEADERS, CMCD_HEADER_FIELDS, CMCD_HEADER_MAP, CMCD_JSON, CMCD_KEYS, CMCD_MIME_TYPE, CMCD_OBJECT, CMCD_PARAM, CMCD_QUERY, CMCD_RECORDED_REPORT_MODE_EVENT, CMCD_RECORDED_REPORT_MODE_HEADER, CMCD_RECORDED_REPORT_MODE_QUERY, CMCD_RECORDED_REQUEST_TYPE_EVENT, CMCD_RECORDED_REQUEST_TYPE_MANIFEST, CMCD_RECORDED_REQUEST_TYPE_SEGMENT, CMCD_RECORDED_REQUEST_TYPE_UNKNOWN, CMCD_REQUEST, CMCD_REQUEST_KEYS, CMCD_REQUEST_MODE, CMCD_REQUEST_PROVENANCE, CMCD_RESPONSE_KEYS, CMCD_SESSION, CMCD_STATUS, CMCD_V1, CMCD_V1_KEYS, CMCD_V2, CMCD_VALIDATION_SEVERITY_ERROR, CMCD_VALIDATION_SEVERITY_WARNING, Cmcd, CmcdCustomKey, CmcdCustomValue, CmcdData, CmcdDataValidationResult, CmcdDecodeOptions, CmcdEncodeOptions, CmcdEvent, CmcdEventReportConfig, CmcdEventReportTransform, CmcdEventType, CmcdEventsValidationResult, CmcdFormatter, CmcdFormatterMap, CmcdFormatterOptions, CmcdHeaderField, CmcdHeaderKey, CmcdHeaderMap, CmcdHeaderValue, CmcdKey, CmcdObjectType, CmcdObjectTypeList, CmcdPlayerState, CmcdRecordedReport, CmcdRecordedReportMode, CmcdRecordedRequestType, CmcdReportConfig, CmcdReportRecorder, CmcdReportRecorderOptions, CmcdReportRecorderWaitOptions, CmcdReporter, CmcdReporterConfig, CmcdReporterCustomData, CmcdReportingMode, CmcdRequest, CmcdRequestDeliver, CmcdRequestKey, CmcdRequestProvenance, CmcdRequestReport, CmcdRequestReportConfig, CmcdRequestReportTransform, CmcdResponse, CmcdStreamType, CmcdStreamingFormat, CmcdTransformRequest, CmcdTransmissionMode, CmcdTransportAdapter, CmcdV1, CmcdV1Data, CmcdV2Data, CmcdValidationIssue, CmcdValidationOptions, CmcdValidationResult, CmcdValidationSeverity, CmcdValue, CmcdVersion, appendCmcdHeaders, appendCmcdQuery, createFetchTransport, createXhrTransport, decodeCmcd, encodeCmcd, fromCmcdHeaders, fromCmcdQuery, fromCmcdUrl, groupCmcdHeaders, isCmcdCustomKey, isCmcdEventKey, isCmcdRequestKey, isCmcdResponseReceivedKey, isCmcdV1Data, isCmcdV1Key, isCmcdV2Data, prepareCmcdData, toCmcdHeaders, toCmcdQuery, toCmcdUrl, toCmcdValue, validateCmcd, validateCmcdEventReport, validateCmcdEvents, validateCmcdHeaders, validateCmcdKeys, validateCmcdRequest, validateCmcdStructure, validateCmcdValues };
2914
3364
  //# sourceMappingURL=index.d.ts.map