stormcloud-video-player 0.8.4 → 0.8.5

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/lib/index.cjs CHANGED
@@ -2448,6 +2448,166 @@ function initializePolyfills() {
2448
2448
  polyfillTextEncoder();
2449
2449
  polyfillPromiseFinally();
2450
2450
  }
2451
+ // src/utils/vastMacros.ts
2452
+ function generateCorrelator() {
2453
+ if (typeof crypto !== "undefined" && crypto.getRandomValues) {
2454
+ try {
2455
+ var _buf_, _buf_1;
2456
+ var buf = new Uint32Array(2);
2457
+ crypto.getRandomValues(buf);
2458
+ var value = ((_buf_ = buf[0]) !== null && _buf_ !== void 0 ? _buf_ : 0) * 2097152 + (((_buf_1 = buf[1]) !== null && _buf_1 !== void 0 ? _buf_1 : 0) & 2097151);
2459
+ if (value > 0) {
2460
+ return String(value);
2461
+ }
2462
+ } catch (unused) {}
2463
+ }
2464
+ return String(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + 1);
2465
+ }
2466
+ var UNEXPANDED_MACRO_PATTERN = /^(\[[^\]]*\]|\{[^}]*\}|%%[^%]*%%)$/;
2467
+ function applyVastMacros(baseUrl, ctx) {
2468
+ var url;
2469
+ try {
2470
+ url = new URL(baseUrl);
2471
+ } catch (unused) {
2472
+ return replaceCorrelatorFallback(baseUrl, ctx.correlator);
2473
+ }
2474
+ var params = url.searchParams;
2475
+ params.set("correlator", ctx.correlator);
2476
+ params.set("scor", ctx.streamCorrelator);
2477
+ if (ctx.pod != null) {
2478
+ params.set("pod", String(ctx.pod));
2479
+ }
2480
+ if (ctx.adPosition != null) {
2481
+ params.set("ppos", String(ctx.adPosition));
2482
+ }
2483
+ if (ctx.pageUrl) {
2484
+ params.set("url", ctx.pageUrl);
2485
+ params.set("description_url", ctx.pageUrl);
2486
+ }
2487
+ if (ctx.adWillPlayMuted != null) {
2488
+ params.set("vpmute", ctx.adWillPlayMuted ? "1" : "0");
2489
+ }
2490
+ if (ctx.adWillAutoPlay != null) {
2491
+ params.set("vpa", ctx.adWillAutoPlay ? "auto" : "click");
2492
+ }
2493
+ if (ctx.deviceId && ctx.deviceIdType) {
2494
+ params.set("rdid", ctx.deviceId);
2495
+ params.set("idtype", ctx.deviceIdType);
2496
+ params.set("is_lat", ctx.limitAdTracking ? "1" : "0");
2497
+ } else {
2498
+ params.delete("rdid");
2499
+ params.delete("idtype");
2500
+ params.delete("is_lat");
2501
+ }
2502
+ var consent = ctx.consent;
2503
+ if ((consent === null || consent === void 0 ? void 0 : consent.gdpr) != null) {
2504
+ params.set("gdpr", consent.gdpr);
2505
+ }
2506
+ if ((consent === null || consent === void 0 ? void 0 : consent.gdprConsent) != null) {
2507
+ params.set("gdpr_consent", consent.gdprConsent);
2508
+ }
2509
+ if ((consent === null || consent === void 0 ? void 0 : consent.usPrivacy) != null) {
2510
+ params.set("us_privacy", consent.usPrivacy);
2511
+ }
2512
+ if (ctx.adTest) {
2513
+ params.set("adtest", "on");
2514
+ }
2515
+ var staleKeys = [];
2516
+ params.forEach(function(value, key) {
2517
+ if (UNEXPANDED_MACRO_PATTERN.test(value)) {
2518
+ staleKeys.push(key);
2519
+ }
2520
+ });
2521
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2522
+ try {
2523
+ for(var _iterator = staleKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2524
+ var key = _step.value;
2525
+ params.delete(key);
2526
+ }
2527
+ } catch (err) {
2528
+ _didIteratorError = true;
2529
+ _iteratorError = err;
2530
+ } finally{
2531
+ try {
2532
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2533
+ _iterator.return();
2534
+ }
2535
+ } finally{
2536
+ if (_didIteratorError) {
2537
+ throw _iteratorError;
2538
+ }
2539
+ }
2540
+ }
2541
+ return url.toString();
2542
+ }
2543
+ function replaceCorrelatorFallback(baseUrl, correlator) {
2544
+ var correlatorRegex = /([?&])correlator=([^&]*)/;
2545
+ if (correlatorRegex.test(baseUrl)) {
2546
+ return baseUrl.replace(correlatorRegex, "$1correlator=".concat(correlator));
2547
+ }
2548
+ var sep = baseUrl.includes("?") ? "&" : "?";
2549
+ return "".concat(baseUrl).concat(sep, "correlator=").concat(correlator);
2550
+ }
2551
+ function fetchConsentSignals() {
2552
+ var timeoutMs = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 1500;
2553
+ var signals = {};
2554
+ if (typeof window === "undefined") {
2555
+ return Promise.resolve(signals);
2556
+ }
2557
+ var tasks = [];
2558
+ var tcfApi = window.__tcfapi;
2559
+ if (typeof tcfApi === "function") {
2560
+ tasks.push(new Promise(function(resolve) {
2561
+ var settled = false;
2562
+ try {
2563
+ tcfApi("addEventListener", 2, function(tcData, success) {
2564
+ if (settled) return;
2565
+ if (success && tcData && (tcData.eventStatus === "tcloaded" || tcData.eventStatus === "useractioncomplete")) {
2566
+ settled = true;
2567
+ signals.gdpr = tcData.gdprApplies ? "1" : "0";
2568
+ if (typeof tcData.tcString === "string" && tcData.tcString) {
2569
+ signals.gdprConsent = tcData.tcString;
2570
+ }
2571
+ try {
2572
+ tcfApi("removeEventListener", 2, function() {}, tcData.listenerId);
2573
+ } catch (unused) {}
2574
+ resolve();
2575
+ }
2576
+ });
2577
+ } catch (unused) {
2578
+ resolve();
2579
+ }
2580
+ setTimeout(function() {
2581
+ if (!settled) {
2582
+ settled = true;
2583
+ resolve();
2584
+ }
2585
+ }, timeoutMs);
2586
+ }));
2587
+ }
2588
+ var uspApi = window.__uspapi;
2589
+ if (typeof uspApi === "function") {
2590
+ tasks.push(new Promise(function(resolve) {
2591
+ try {
2592
+ uspApi("getUSPData", 1, function(data, success) {
2593
+ if (success && typeof (data === null || data === void 0 ? void 0 : data.uspString) === "string" && data.uspString) {
2594
+ signals.usPrivacy = data.uspString;
2595
+ }
2596
+ resolve();
2597
+ });
2598
+ } catch (unused) {
2599
+ resolve();
2600
+ }
2601
+ setTimeout(resolve, timeoutMs);
2602
+ }));
2603
+ }
2604
+ if (tasks.length === 0) {
2605
+ return Promise.resolve(signals);
2606
+ }
2607
+ return Promise.all(tasks).then(function() {
2608
+ return signals;
2609
+ });
2610
+ }
2451
2611
  // src/utils/browserCompat.ts
2452
2612
  function getChromeVersion(ua) {
2453
2613
  var match = ua.match(/Chrome\/(\d+)/);
@@ -2696,6 +2856,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
2696
2856
  this.preloadPoolLoopRunning = false;
2697
2857
  this.adDetectSentForCurrentBreak = false;
2698
2858
  this.palPlaybackStarted = false;
2859
+ this.streamCorrelator = generateCorrelator();
2860
+ this.consentSignals = {};
2861
+ this.podCounter = 0;
2862
+ this.podAssignedByPrefetch = false;
2863
+ this.adRequestPositionInBreak = 0;
2699
2864
  this.continuousFetchLoopRunning = false;
2700
2865
  initializePolyfills();
2701
2866
  var browserOverrides = getBrowserConfigOverrides();
@@ -2768,6 +2933,9 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
2768
2933
  adWillPlayMuted: !!this.config.muted,
2769
2934
  continuousPlayback: (_this_config_lowLatencyMode = this.config.lowLatencyMode) !== null && _this_config_lowLatencyMode !== void 0 ? _this_config_lowLatencyMode : false
2770
2935
  }).catch(function() {});
2936
+ fetchConsentSignals().then(function(signals) {
2937
+ _this.consentSignals = signals;
2938
+ }).catch(function() {});
2771
2939
  this.initializeTracking();
2772
2940
  if (!this.shouldUseNativeHls()) return [
2773
2941
  3,
@@ -5064,25 +5232,35 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5064
5232
  return this.getRemainingAdMs();
5065
5233
  }
5066
5234
  },
5235
+ {
5236
+ key: "beginNewAdPod",
5237
+ value: function beginNewAdPod() {
5238
+ this.podCounter++;
5239
+ this.adRequestPositionInBreak = 0;
5240
+ }
5241
+ },
5067
5242
  {
5068
5243
  key: "generateVastUrlsWithCorrelators",
5069
5244
  value: function generateVastUrlsWithCorrelators(baseUrl, count) {
5070
5245
  var urls = [];
5071
- var baseTimestamp = Date.now();
5072
5246
  for(var i = 0; i < count; i++){
5073
- var timestamp = baseTimestamp + i;
5074
- var random = Math.floor(Math.random() * 1e12);
5075
- var uniqueCorrelator = "".concat(timestamp, "_").concat(random, "_").concat(i);
5076
- var urlWithCorrelator = void 0;
5077
- var correlatorRegex = /([?&])correlator=([^&]*)/;
5078
- if (correlatorRegex.test(baseUrl)) {
5079
- urlWithCorrelator = baseUrl.replace(correlatorRegex, "$1correlator=".concat(uniqueCorrelator));
5080
- } else if (baseUrl.includes("?")) {
5081
- urlWithCorrelator = "".concat(baseUrl, "&correlator=").concat(uniqueCorrelator);
5082
- } else {
5083
- urlWithCorrelator = "".concat(baseUrl, "?correlator=").concat(uniqueCorrelator);
5084
- }
5085
- urls.push(this.palNonce.injectNonce(urlWithCorrelator));
5247
+ this.adRequestPositionInBreak++;
5248
+ var adWillPlayMuted = this.inAdBreak ? this.adPlayer.getOriginalMutedState() : this.video.muted;
5249
+ var urlWithMacros = applyVastMacros(baseUrl, {
5250
+ correlator: generateCorrelator(),
5251
+ streamCorrelator: this.streamCorrelator,
5252
+ pod: this.podCounter > 0 ? this.podCounter : void 0,
5253
+ adPosition: this.adRequestPositionInBreak,
5254
+ pageUrl: typeof window !== "undefined" ? window.location.href : void 0,
5255
+ adWillPlayMuted: adWillPlayMuted,
5256
+ adWillAutoPlay: !!this.config.autoplay,
5257
+ deviceId: this.config.deviceId,
5258
+ deviceIdType: this.config.deviceIdType,
5259
+ limitAdTracking: this.config.limitAdTracking,
5260
+ adTest: this.config.adTest,
5261
+ consent: this.consentSignals
5262
+ });
5263
+ urls.push(this.palNonce.injectNonce(urlWithMacros));
5086
5264
  }
5087
5265
  return urls;
5088
5266
  }
@@ -5152,6 +5330,8 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5152
5330
  }
5153
5331
  return;
5154
5332
  }
5333
+ this.beginNewAdPod();
5334
+ this.podAssignedByPrefetch = true;
5155
5335
  var urlsToPregenerate = 5;
5156
5336
  var generatedUrls = this.generateVastUrlsWithCorrelators(baseVastUrl, urlsToPregenerate);
5157
5337
  this.pendingAdBreak = _object_spread_props(_object_spread({
@@ -5189,6 +5369,7 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5189
5369
  }
5190
5370
  this.pendingAdBreak = null;
5191
5371
  this.pendingScte35CueKey = void 0;
5372
+ this.podAssignedByPrefetch = false;
5192
5373
  }
5193
5374
  },
5194
5375
  {
@@ -5625,6 +5806,11 @@ var StormcloudVideoPlayer = /*#__PURE__*/ function() {
5625
5806
  this.continuousFetchingActive = true;
5626
5807
  this.isShowingPlaceholder = false;
5627
5808
  this.totalAdRequestsInBreak = 0;
5809
+ if (this.podAssignedByPrefetch) {
5810
+ this.podAssignedByPrefetch = false;
5811
+ } else {
5812
+ this.beginNewAdPod();
5813
+ }
5628
5814
  currentMuted = this.video.muted;
5629
5815
  currentVolume = this.video.volume;
5630
5816
  this.adPlayer.updateOriginalMutedState(currentMuted, currentVolume);