@newrelic/video-core 5.0.2 → 5.1.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +13 -0
  3. package/dist/cjs/browser/index.js +1 -1
  4. package/dist/cjs/browser/index.js.LICENSE.txt +1 -1
  5. package/dist/cjs/browser/index.js.map +1 -1
  6. package/dist/cjs/index.js +1 -1
  7. package/dist/cjs/index.js.LICENSE.txt +1 -1
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/vega/index.js +1 -1
  10. package/dist/cjs/vega/index.js.LICENSE.txt +1 -1
  11. package/dist/cjs/vega/index.js.map +1 -1
  12. package/dist/esm/browser/index.js +1 -1
  13. package/dist/esm/browser/index.js.LICENSE.txt +1 -1
  14. package/dist/esm/browser/index.js.map +1 -1
  15. package/dist/esm/index.js +1 -1
  16. package/dist/esm/index.js.LICENSE.txt +1 -1
  17. package/dist/esm/index.js.map +1 -1
  18. package/dist/esm/vega/index.js +1 -1
  19. package/dist/esm/vega/index.js.LICENSE.txt +1 -1
  20. package/dist/esm/vega/index.js.map +1 -1
  21. package/dist/types/browser/agent.d.ts +46 -0
  22. package/dist/types/browser/harvestScheduler.d.ts +125 -0
  23. package/dist/types/browser/index.d.ts +34 -0
  24. package/dist/types/chrono.d.ts +52 -0
  25. package/dist/types/connectedDevice/connectedDeviceAgent.d.ts +63 -0
  26. package/dist/types/connectedDevice/connectedDeviceConstants.d.ts +51 -0
  27. package/dist/types/connectedDevice/connectedDeviceHarvester.d.ts +198 -0
  28. package/dist/types/connectedDevice/index.d.ts +30 -0
  29. package/dist/types/constants.d.ts +30 -0
  30. package/dist/types/core.d.ts +54 -0
  31. package/dist/types/emitter.d.ts +42 -0
  32. package/dist/types/eventAggregator.d.ts +97 -0
  33. package/dist/types/index.d.ts +33 -0
  34. package/dist/types/log.d.ts +61 -0
  35. package/dist/types/obfuscate.d.ts +13 -0
  36. package/dist/types/optimizedHttpClient.d.ts +83 -0
  37. package/dist/types/recordEvent.d.ts +30 -0
  38. package/dist/types/retryQueueHandler.d.ts +46 -0
  39. package/dist/types/tracker.d.ts +176 -0
  40. package/dist/types/utils/eventBuilder.d.ts +69 -0
  41. package/dist/types/utils/harvestTimer.d.ts +45 -0
  42. package/dist/types/utils/index.d.ts +38 -0
  43. package/dist/types/utils/qoeFilters.d.ts +88 -0
  44. package/dist/types/videoConfiguration.d.ts +90 -0
  45. package/dist/types/videotracker.d.ts +366 -0
  46. package/dist/types/videotrackerstate.d.ts +228 -0
  47. package/dist/umd/nrvideo.min.js +1 -1
  48. package/dist/umd/nrvideo.min.js.LICENSE.txt +1 -1
  49. package/dist/umd/nrvideo.min.js.map +1 -1
  50. package/package.json +14 -2
  51. package/src/browser/{agent.js → agent.ts} +21 -16
  52. package/src/browser/{harvestScheduler.js → harvestScheduler.ts} +51 -24
  53. package/src/{chrono.js → chrono.ts} +29 -24
  54. package/src/connectedDevice/{connectedDeviceAgent.js → connectedDeviceAgent.ts} +14 -10
  55. package/src/connectedDevice/{connectedDeviceConstants.js → connectedDeviceConstants.ts} +5 -3
  56. package/src/connectedDevice/{connectedDeviceHarvester.js → connectedDeviceHarvester.ts} +71 -34
  57. package/src/constants.ts +62 -0
  58. package/src/{core.js → core.ts} +31 -18
  59. package/src/{emitter.js → emitter.ts} +16 -5
  60. package/src/{eventAggregator.js → eventAggregator.ts} +33 -19
  61. package/src/global.d.ts +25 -0
  62. package/src/{log.js → log.ts} +23 -17
  63. package/src/{obfuscate.js → obfuscate.ts} +6 -1
  64. package/src/{optimizedHttpClient.js → optimizedHttpClient.ts} +37 -13
  65. package/src/{recordEvent.js → recordEvent.ts} +11 -9
  66. package/src/{retryQueueHandler.js → retryQueueHandler.ts} +17 -12
  67. package/src/{tracker.js → tracker.ts} +56 -23
  68. package/src/utils/{eventBuilder.js → eventBuilder.ts} +31 -14
  69. package/src/utils/{harvestTimer.js → harvestTimer.ts} +21 -4
  70. package/src/utils/{index.js → index.ts} +14 -14
  71. package/src/utils/{qoeFilters.js → qoeFilters.ts} +19 -8
  72. package/src/{videoConfiguration.js → videoConfiguration.ts} +44 -10
  73. package/src/{videotracker.js → videotracker.ts} +119 -78
  74. package/src/{videotrackerstate.js → videotrackerstate.ts} +113 -49
  75. package/src/constants.js +0 -63
  76. /package/src/browser/{index.js → index.ts} +0 -0
  77. /package/src/connectedDevice/{index.js → index.ts} +0 -0
  78. /package/src/{index.js → index.ts} +0 -0
@@ -0,0 +1,69 @@
1
+ export type EventAttributes = Record<string, any>;
2
+ export interface Harvester {
3
+ addEvent(eventObject: EventAttributes): boolean;
4
+ setHarvestInterval(ms: number): void;
5
+ /** Set (or clear, with `null`) the callback run just before the next harvest drain. */
6
+ setBeforeDrainCallback(callback: (() => void) | null): void;
7
+ /** Merge fresh QoE KPI values into the buffered QOE_AGGREGATE event for a view. */
8
+ refreshQoeKpis(freshKpis: EventAttributes, viewId?: string): void;
9
+ /** Force the next harvest cycle to be treated as a QoE cycle. Only the browser harvester supports this — the connected-device harvester does not implement it. */
10
+ forceNextQoeCycle?(): void;
11
+ }
12
+ interface BuildEventObjectsOptions {
13
+ addTimeSinceLoad?: boolean;
14
+ }
15
+ /**
16
+ * Pure helper that constructs the wire-format event objects from `recordEvent`
17
+ * arguments. Used by `src/recordEvent.js`, which is the single shared
18
+ * implementation re-exported from all three subpath entry points:
19
+ *
20
+ * - `src/recordEvent.js` (registry dispatcher; runtime hot path
21
+ * and the only direct caller of this helper)
22
+ * - `src/browser/index.js` (Browser subpath re-export)
23
+ * - `src/connectedDevice/index.js` (Vega subpath re-export)
24
+ *
25
+ * Pipeline-specific concerns (which info global to read, which harvester to
26
+ * call, which qoeAggregate config to check) stay at the call site. This helper
27
+ * is purely about event-object shape.
28
+ *
29
+ * @param {string} eventType - One of `Constants.VALID_EVENT_TYPES`.
30
+ * @param {object} attributes - Caller-supplied attributes including optional `qoe`.
31
+ * @param {object} info - The pipeline's resolved `info` object (with `appName` /
32
+ * `applicationID` for namespacing).
33
+ * @param {{ addTimeSinceLoad?: boolean }} [opts] - When `true`, stamp
34
+ * `timeSinceLoad` on the event. Browser sets this; Vega does not.
35
+ *
36
+ * @returns {{ eventObject: object, qoeEventObject: object|null }}
37
+ */
38
+ export declare function buildEventObjects(eventType: string, attributes: EventAttributes, info: Record<string, any>, { addTimeSinceLoad }?: BuildEventObjectsOptions): {
39
+ eventObject: EventAttributes;
40
+ qoeEventObject: EventAttributes | null;
41
+ };
42
+ /**
43
+ * Build + dispatch a video analytics event to the given harvester.
44
+ *
45
+ * This is the full dispatch loop shared by all three `recordEvent` entry
46
+ * points:
47
+ * 1. Build `eventObject` (and optional `qoeEventObject`) via
48
+ * `buildEventObjects`.
49
+ * 2. Call `harvester.addEvent(eventObject)`.
50
+ * 3. If qoeEnabled and a QoE companion event was produced, also call
51
+ * `harvester.addEvent(qoeEventObject)`.
52
+ * 4. Return a boolean indicating overall success.
53
+ *
54
+ * Pipeline-specific concerns — which global to read for `info`, which
55
+ * harvester to use, whether qoeAggregate is on — are resolved by the caller.
56
+ * This function only handles the build + dispatch.
57
+ *
58
+ * @param {string} eventType
59
+ * @param {object} attributes
60
+ * @param {object} info - Resolved `info` from the pipeline's config global.
61
+ * @param {{ addEvent: function }} harvester - The registered harvester for
62
+ * the current pipeline. Returns false if null/undefined.
63
+ * @param {boolean} qoeEnabled
64
+ * @param {{ addTimeSinceLoad?: boolean }} [opts] - Forwarded to
65
+ * `buildEventObjects`.
66
+ * @returns {boolean}
67
+ */
68
+ export declare function dispatchRecordEvent(eventType: string, attributes: EventAttributes, info: Record<string, any>, harvester: Harvester | null | undefined, qoeEnabled: boolean, { addTimeSinceLoad }?: BuildEventObjectsOptions): boolean;
69
+ export {};
@@ -0,0 +1,45 @@
1
+ export interface HarvestTimerOptions {
2
+ /** Initial tick interval in ms. */
3
+ interval: number;
4
+ /** Pipeline-specific drain callback. Awaited; the next tick is scheduled in `finally`. */
5
+ onTick: () => Promise<void> | void;
6
+ /** Prefix for the error log when `onTick` throws (e.g. "HarvestScheduler"). */
7
+ errorLabel: string;
8
+ }
9
+ export interface HarvestTimer {
10
+ start: () => void;
11
+ stop: () => void;
12
+ cancelAndReschedule: () => void;
13
+ updateInterval: (ms: number) => void;
14
+ isRunning: () => boolean;
15
+ }
16
+ /**
17
+ * Periodic harvest timer using chained `setTimeout`. Each tick schedules the
18
+ * next one only after the current `onTick` callback resolves — by construction
19
+ * there can never be overlapping ticks, even if `onTick` is async and slow.
20
+ *
21
+ * Used by both pipelines:
22
+ * - `browser/harvestScheduler.js` — Browser pipeline drain orchestration
23
+ * - `connectedDevice/connectedDeviceHarvester.js` — Vega CAF pipeline
24
+ *
25
+ * Single source of truth for harvest timing. Future timer changes (drift
26
+ * compensation, jitter, exponential back-off, etc.) land here and apply to
27
+ * both pipelines automatically.
28
+ *
29
+ * @param {object} opts
30
+ * @param {number} opts.interval - Initial tick interval in ms.
31
+ * @param {() => Promise<void>|void} opts.onTick - Pipeline-specific drain
32
+ * callback. Awaited; the next tick is scheduled in `finally` so a slow or
33
+ * throwing tick can't overlap with the next one.
34
+ * @param {string} opts.errorLabel - Prefix for the error log when `onTick`
35
+ * throws (e.g. "HarvestScheduler", "ConnectedDeviceHarvester").
36
+ *
37
+ * @returns {{
38
+ * start: () => void,
39
+ * stop: () => void,
40
+ * cancelAndReschedule: () => void,
41
+ * updateInterval: (ms: number) => void,
42
+ * isRunning: () => boolean,
43
+ * }}
44
+ */
45
+ export declare function createHarvestTimer({ interval, onTick, errorLabel }: HarvestTimerOptions): HarvestTimer;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Builds the harvest URL with proper query parameters.
3
+ * @returns {string} Harvest URL
4
+ */
5
+ export declare function buildUrl(fallbackUrl?: string): string | null;
6
+ export declare function dataSize(data: unknown): number | undefined;
7
+ /**
8
+ * Determines if a request should be retried based on HTTP status code
9
+ * @param {number} status - HTTP status code
10
+ * @returns {boolean} - True if request should be retried
11
+ */
12
+ export declare function shouldRetry(status: number): boolean;
13
+ /**
14
+ * Compresses a JSON payload using the Compression Streams API with Gzip.
15
+ * see @description(https://developer.mozilla.org/en-US/docs/Web/API/Compression_Streams_API)
16
+ * @param {object} payload - The JSON object to compress.
17
+ * @returns {Promise<Blob>} A Promise that resolves with a Blob of the Gzipped data.
18
+ */
19
+ export declare function compressPayload(payload: unknown): Promise<Blob>;
20
+ /**
21
+ * Decompresses a gzipped Blob back to a JSON object using the Compression Streams API.
22
+ * @param {Blob|ArrayBuffer|Uint8Array} compressedData - The gzipped data to decompress.
23
+ * @returns {Promise<object>} A Promise that resolves with the decompressed JSON object.
24
+ */
25
+ export declare function decompressPayload(compressedData: Blob | ArrayBuffer | Uint8Array): Promise<unknown>;
26
+ /**
27
+ * Filters an object to include only the specified keys.
28
+ * Creates a new object containing only the key-value pairs from the source object
29
+ * that match the provided keys array.
30
+ * @param {string[]} keys - Array of keys to extract from the object. If empty, null, or not an array, returns the original object.
31
+ * @param {object} obj - The source object to extract entries from.
32
+ * @returns {object} A new object containing only the entries that match the specified keys. Returns an empty object if obj is invalid.
33
+ * @example
34
+ * const data = { name: 'John', age: 30, city: 'NYC', country: 'USA' };
35
+ * const filtered = getObjectEntriesForKeys(['name', 'city'], data);
36
+ * // Returns: { name: 'John', city: 'NYC' }
37
+ */
38
+ export declare function getObjectEntriesForKeys(keys: string[], obj: Record<string, unknown>): Record<string, unknown>;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * QoE-related buffer + cycle helpers shared by both pipelines.
3
+ *
4
+ * All six helpers were previously duplicated between
5
+ * - `browser/agent.js` (`addEvent`, `refreshQoeKpis`)
6
+ * - `browser/harvestScheduler.js` (cycle filter, dirty splice, _qoeKpisUnchanged, _saveQoeKpis)
7
+ * - `connectedDevice/connectedDeviceHarvester.js` (same six)
8
+ *
9
+ * They're pure helpers — no class state. Callers pass in the buffer / snapshot
10
+ * map / event array. Keeping them shared makes the QoE behavior one source of
11
+ * truth across pipelines, so a fix landed once applies to both Browser and
12
+ * Vega (and any future CAF device pipeline).
13
+ *
14
+ * @module utils/qoeFilters
15
+ */
16
+ type QoeEvent = Record<string, any>;
17
+ type QoeSnapshots = Record<string, Record<string, any>>;
18
+ interface QoeBuffer {
19
+ addOrReplaceByActionNameAndViewId(actionName: string, viewId: string, event: QoeEvent): boolean;
20
+ addOrReplaceByActionName(actionName: string, event: QoeEvent): boolean;
21
+ add(event: QoeEvent): boolean;
22
+ findByActionNameAndViewId(actionName: string, viewId: string): QoeEvent | null;
23
+ findByActionName(actionName: string): QoeEvent | null;
24
+ }
25
+ /**
26
+ * Add an event to the buffer with QOE_AGGREGATE dedup. QOE_AGGREGATE events
27
+ * are deduplicated by `(actionName, viewId)` so multiple players sharing one
28
+ * harvester each get exactly one buffered QoE event per view. Non-QoE events
29
+ * are appended as-is, preserving the emit-time `timestamp` already set by
30
+ * `recordEvent.js`.
31
+ *
32
+ * @param {NrVideoEventAggregator} buffer
33
+ * @param {object} eventObject
34
+ * @returns {boolean} True if added/replaced.
35
+ */
36
+ export declare function bufferEventWithQoeDedup(buffer: QoeBuffer, eventObject: QoeEvent): boolean;
37
+ /**
38
+ * Update QoE KPI fields on the buffered QOE_AGGREGATE event for a given viewId.
39
+ * Looks up the existing buffered event, merges the fresh KPI values for keys
40
+ * listed in `Constants.QOE_KPI_KEYS`, and replaces it in the buffer. No-op if
41
+ * no QOE_AGGREGATE event is currently buffered.
42
+ *
43
+ * @param {NrVideoEventAggregator} buffer
44
+ * @param {object} freshKpis
45
+ * @param {string} [viewId]
46
+ */
47
+ export declare function refreshQoeKpisInBuffer(buffer: QoeBuffer, freshKpis: QoeEvent, viewId?: string): void;
48
+ /**
49
+ * Cross-cycle dirty check — true iff every KPI field on `event` equals the
50
+ * snapshot saved for that event's viewId.
51
+ *
52
+ * @param {Object<string, object>} snapshots - viewId → KPI snapshot
53
+ * @param {object} event
54
+ * @returns {boolean}
55
+ */
56
+ export declare function qoeKpisUnchanged(snapshots: QoeSnapshots, event: QoeEvent): boolean;
57
+ /**
58
+ * Save a snapshot of an event's QoE KPI fields, keyed by viewId, for the next
59
+ * cross-cycle dirty check.
60
+ *
61
+ * @param {Object<string, object>} snapshots - viewId → KPI snapshot (mutated in place)
62
+ * @param {object} event
63
+ */
64
+ export declare function saveQoeKpiSnapshot(snapshots: QoeSnapshots, event: QoeEvent): void;
65
+ /**
66
+ * Partition drained events by the QoE-cycle filter. On a QoE cycle, all events
67
+ * pass through. On a non-QoE cycle, QOE_AGGREGATE events are re-buffered (so
68
+ * they ship on the next QoE cycle) and only non-QoE events are returned.
69
+ *
70
+ * @param {object[]} drained
71
+ * @param {boolean} isQoeCycle
72
+ * @param {NrVideoEventAggregator} buffer
73
+ * @returns {object[]}
74
+ */
75
+ export declare function partitionByQoeCycle(drained: QoeEvent[], isQoeCycle: boolean, buffer: QoeBuffer): QoeEvent[];
76
+ /**
77
+ * Cross-cycle dirty splice. Walks the array in reverse and drops any
78
+ * QOE_AGGREGATE event whose KPI fields are unchanged since the last send (so
79
+ * we don't waste bytes shipping identical aggregates). Saves a fresh snapshot
80
+ * for any QOE_AGGREGATE that does ship. Mutates `filtered` in place.
81
+ *
82
+ * @param {object[]} filtered
83
+ * @param {Object<string, object>} snapshots - viewId → KPI snapshot (read+written)
84
+ * @param {boolean} isForced - When true, skip the dirty check and ship every
85
+ * QOE_AGGREGATE regardless of KPI sameness (final flush, CONTENT_END, etc.)
86
+ */
87
+ export declare function applyQoeDirtyFilter(filtered: QoeEvent[], snapshots: QoeSnapshots, isForced: boolean): void;
88
+ export {};
@@ -0,0 +1,90 @@
1
+ import { VegaEndpoint } from "./connectedDevice/connectedDeviceConstants";
2
+ import { ObfuscationRule } from "./obfuscate";
3
+ export interface NrVideoBrowserInfo {
4
+ region?: string;
5
+ beacon: string;
6
+ licenseKey: string;
7
+ applicationID?: string;
8
+ appName?: string;
9
+ }
10
+ export interface NrVideoBrowserConfig {
11
+ qoeAggregate: boolean;
12
+ qoeIntervalFactor: number;
13
+ obfuscate: ObfuscationRule[];
14
+ }
15
+ export interface NrVideoCdInfo {
16
+ accountId?: string;
17
+ applicationToken: string;
18
+ endpoint: VegaEndpoint;
19
+ appName?: string;
20
+ applicationID?: string;
21
+ deviceInfo?: unknown;
22
+ }
23
+ export interface NrVideoCdConfig {
24
+ qoeAggregate: boolean;
25
+ qoeIntervalFactor: number;
26
+ obfuscate: ObfuscationRule[];
27
+ }
28
+ /** Raw, unvalidated user-supplied config — shape varies by pipeline (`src`). */
29
+ export type RawVideoConfigInfo = Record<string, any>;
30
+ export type RawVideoConfigOptions = Record<string, any>;
31
+ /**
32
+ * Enhanced video analytics configuration system that extends the existing auth configuration.
33
+ * Provides feature flags, retry policies, and advanced harvesting options.
34
+ */
35
+ declare class VideoConfiguration {
36
+ /**
37
+ * Validates and sets the video analytics configuration.
38
+ * @param {object} userInfo - User provided configuration
39
+ * @param {object} [config] - Optional configuration object
40
+ * @returns {boolean} True if configuration is valid and set
41
+ */
42
+ setConfiguration(userInfo: RawVideoConfigInfo, config?: RawVideoConfigOptions, src?: string): boolean;
43
+ /**
44
+ * Validates required Vega configuration fields.
45
+ * @param {object} info
46
+ * @returns {boolean} True if valid
47
+ */
48
+ validateVegaFields(info: RawVideoConfigInfo): boolean;
49
+ /**
50
+ * Validates required configuration fields.
51
+ * @param {object} config - Configuration to validate
52
+ * @returns {boolean} True if valid
53
+ */
54
+ validateRequiredFields(info: RawVideoConfigInfo): boolean;
55
+ /**
56
+ * Validates optional config fields.
57
+ * @param {object} config - Config to validate
58
+ * @returns {boolean} True if valid
59
+ */
60
+ validateConfigFields(config?: RawVideoConfigOptions): boolean;
61
+ /**
62
+ * Filters obfuscation rules, warning about and removing invalid ones.
63
+ * @param {Array} rules - Raw obfuscation rules
64
+ * @returns {Array} Valid rules only
65
+ */
66
+ filterObfuscateRules(rules?: any[]): ObfuscationRule[];
67
+ /**
68
+ * Sanitizes qoeIntervalFactor, defaulting to Constants.DEFAULT_QOE_INTERVAL_FACTOR
69
+ * if the value is not a positive integer.
70
+ * @param {*} value
71
+ * @returns {number}
72
+ */
73
+ sanitizeQoeIntervalFactor(value: unknown): number;
74
+ /**
75
+ * Initializes the global NRVIDEO configuration object.
76
+ * @param {object} userInfo - User provided configuration
77
+ * @param {object} [config] - Optional configuration object
78
+ */
79
+ initializeGlobalConfig(userInfo: RawVideoConfigInfo, config?: RawVideoConfigOptions, src?: string): void;
80
+ }
81
+ declare const videoConfiguration: VideoConfiguration;
82
+ /**
83
+ * Sets the video analytics configuration.
84
+ * @param {object} info - Info configuration object
85
+ * @param {object} [config] - Optional configuration object
86
+ * @returns {boolean} True if configuration was set successfully
87
+ */
88
+ export declare function setVideoConfig(info: RawVideoConfigInfo, config?: RawVideoConfigOptions, src?: string): boolean;
89
+ export { videoConfiguration };
90
+ export default VideoConfiguration;