@streamlayer/feature-advertisement 1.1.0 → 1.1.1-rc.10.p6eb7304a

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 (103) hide show
  1. package/classic/package.json +5 -0
  2. package/lib/advertisement.d.ts +6 -6
  3. package/lib/advertisement.js +32 -17
  4. package/lib/cjs/advertisement.d.ts +64 -0
  5. package/lib/cjs/advertisement.js +322 -0
  6. package/lib/cjs/index.d.ts +21 -0
  7. package/lib/cjs/index.js +41 -0
  8. package/lib/cjs/package.json +1 -0
  9. package/lib/cjs/units/ad-macros.d.ts +7 -0
  10. package/lib/cjs/units/ad-macros.js +91 -0
  11. package/lib/cjs/units/external-ad.d.ts +127 -0
  12. package/lib/cjs/units/external-ad.js +421 -0
  13. package/lib/cjs/units/external-pause-ad.d.ts +50 -0
  14. package/lib/cjs/units/external-pause-ad.js +259 -0
  15. package/lib/cjs/units/gam.d.ts +26 -0
  16. package/lib/cjs/units/gam.js +152 -0
  17. package/lib/cjs/units/parse-vast-extensions.d.ts +26 -0
  18. package/lib/cjs/units/parse-vast-extensions.js +56 -0
  19. package/lib/cjs/units/pause-ad-card.d.ts +49 -0
  20. package/lib/cjs/units/pause-ad-card.js +136 -0
  21. package/lib/cjs/units/pause-ad.d.ts +16 -0
  22. package/lib/cjs/units/pause-ad.js +52 -0
  23. package/lib/cjs/units/sponsor-logo.d.ts +78 -0
  24. package/lib/cjs/units/sponsor-logo.js +194 -0
  25. package/lib/cjs/units/ssai-ad-break.d.ts +21 -0
  26. package/lib/cjs/units/ssai-ad-break.js +11 -0
  27. package/lib/cjs/units/ssai-creative.d.ts +89 -0
  28. package/lib/cjs/units/ssai-creative.js +136 -0
  29. package/lib/cjs/units/ssai-detector.d.ts +180 -0
  30. package/lib/cjs/units/ssai-detector.js +1172 -0
  31. package/lib/cjs/units/ssai-log.d.ts +16 -0
  32. package/lib/cjs/units/ssai-log.js +23 -0
  33. package/lib/cjs/units/ssai-preload.d.ts +20 -0
  34. package/lib/cjs/units/ssai-preload.js +62 -0
  35. package/lib/cjs/units/ssai-providers.d.ts +117 -0
  36. package/lib/cjs/units/ssai-providers.js +300 -0
  37. package/lib/cjs/units/vast-tracker.d.ts +45 -0
  38. package/lib/cjs/units/vast-tracker.js +64 -0
  39. package/lib/classic/_virtual/_rollupPluginBabelHelpers.js +71 -0
  40. package/lib/classic/advertisement.js +348 -0
  41. package/lib/classic/cjs/_virtual/_rollupPluginBabelHelpers.js +77 -0
  42. package/lib/classic/cjs/advertisement.js +350 -0
  43. package/lib/classic/cjs/index.js +49 -0
  44. package/lib/classic/cjs/package.json +1 -0
  45. package/lib/classic/cjs/units/ad-macros.js +115 -0
  46. package/lib/classic/cjs/units/external-ad.js +500 -0
  47. package/lib/classic/cjs/units/external-pause-ad.js +278 -0
  48. package/lib/classic/cjs/units/gam.js +173 -0
  49. package/lib/classic/cjs/units/parse-vast-extensions.js +60 -0
  50. package/lib/classic/cjs/units/pause-ad-card.js +134 -0
  51. package/lib/classic/cjs/units/pause-ad.js +72 -0
  52. package/lib/classic/cjs/units/sponsor-logo.js +201 -0
  53. package/lib/classic/cjs/units/ssai-ad-break.js +11 -0
  54. package/lib/classic/cjs/units/ssai-creative.js +142 -0
  55. package/lib/classic/cjs/units/ssai-detector.js +1195 -0
  56. package/lib/classic/cjs/units/ssai-log.js +23 -0
  57. package/lib/classic/cjs/units/ssai-preload.js +72 -0
  58. package/lib/classic/cjs/units/ssai-providers.js +361 -0
  59. package/lib/classic/cjs/units/vast-tracker.js +81 -0
  60. package/lib/classic/index.js +29 -0
  61. package/lib/classic/units/ad-macros.js +113 -0
  62. package/lib/classic/units/external-ad.js +491 -0
  63. package/lib/classic/units/external-pause-ad.js +275 -0
  64. package/lib/classic/units/gam.js +171 -0
  65. package/lib/classic/units/parse-vast-extensions.js +58 -0
  66. package/lib/classic/units/pause-ad-card.js +131 -0
  67. package/lib/classic/units/pause-ad.js +70 -0
  68. package/lib/classic/units/sponsor-logo.js +197 -0
  69. package/lib/classic/units/ssai-ad-break.js +9 -0
  70. package/lib/classic/units/ssai-creative.js +140 -0
  71. package/lib/classic/units/ssai-detector.js +1192 -0
  72. package/lib/classic/units/ssai-log.js +21 -0
  73. package/lib/classic/units/ssai-preload.js +68 -0
  74. package/lib/classic/units/ssai-providers.js +351 -0
  75. package/lib/classic/units/vast-tracker.js +77 -0
  76. package/lib/index.d.ts +15 -13
  77. package/lib/index.js +9 -8
  78. package/lib/units/ad-macros.d.ts +7 -0
  79. package/lib/units/ad-macros.js +87 -0
  80. package/lib/units/external-ad.d.ts +71 -2
  81. package/lib/units/external-ad.js +94 -66
  82. package/lib/units/external-pause-ad.js +7 -1
  83. package/lib/units/gam.d.ts +11 -1
  84. package/lib/units/gam.js +65 -17
  85. package/lib/units/parse-vast-extensions.d.ts +10 -1
  86. package/lib/units/parse-vast-extensions.js +5 -1
  87. package/lib/units/pause-ad.d.ts +1 -1
  88. package/lib/units/pause-ad.js +1 -1
  89. package/lib/units/sponsor-logo.d.ts +78 -0
  90. package/lib/units/sponsor-logo.js +188 -0
  91. package/lib/units/ssai-ad-break.d.ts +1 -1
  92. package/lib/units/ssai-creative.d.ts +1 -1
  93. package/lib/units/ssai-creative.js +1 -1
  94. package/lib/units/ssai-detector.d.ts +1 -1
  95. package/lib/units/ssai-detector.js +27 -11
  96. package/lib/units/ssai-log.d.ts +16 -0
  97. package/lib/units/ssai-log.js +18 -0
  98. package/lib/units/ssai-preload.d.ts +1 -1
  99. package/lib/units/ssai-providers.d.ts +1 -1
  100. package/lib/units/ssai-providers.js +48 -11
  101. package/lib/units/vast-tracker.d.ts +1 -1
  102. package/lib/vast-client.d.ts +9 -0
  103. package/package.json +42 -9
@@ -0,0 +1,275 @@
1
+ import { asyncToGenerator as _asyncToGenerator } from '../_virtual/_rollupPluginBabelHelpers.js';
2
+ import 'core-js/modules/es.error.cause.js';
3
+ import 'core-js/modules/es.array.iterator.js';
4
+ import 'core-js/modules/es.array.sort.js';
5
+ import 'core-js/modules/es.promise.js';
6
+ import 'core-js/modules/esnext.iterator.constructor.js';
7
+ import 'core-js/modules/esnext.iterator.filter.js';
8
+ import 'core-js/modules/esnext.iterator.find.js';
9
+ import 'core-js/modules/esnext.iterator.some.js';
10
+ import 'core-js/modules/web.dom-collections.iterator.js';
11
+ import { eventBus } from '@streamlayer/sdk-web-interfaces/classic';
12
+ import { createLogger } from '@streamlayer/sdk-web-logger/classic';
13
+ import '@streamlayer/sdk-web-api/classic';
14
+ import { VASTClient } from '@dailymotion/vast-client';
15
+ import { substituteAdTagMacros } from './ad-macros.js';
16
+
17
+ const FULLBLEED_VIDEO_TEMPLATES = ['fullbleed-video', 'fullbleed-video-center', 'fullbleed-video-left'];
18
+ /**
19
+ * `indexOf` rather than `Array.prototype.includes`: this package ships to Chrome 32
20
+ * TV webviews and must stay ES5-compatible without a polyfill.
21
+ */
22
+ const isFullbleedVideoTemplate = template => !!template && FULLBLEED_VIDEO_TEMPLATES.indexOf(template) !== -1;
23
+ const MP4_MIME = 'video/mp4';
24
+ // Programmatic spec (Index Exchange): bitrate window for a linear pause-ad MP4.
25
+ const MIN_BITRATE = 5000;
26
+ const MAX_BITRATE = 40000;
27
+ const TARGET_WIDTH = 1920;
28
+ const TARGET_HEIGHT = 1080;
29
+ const bitrateDistanceToWindow = bitrate => {
30
+ if (bitrate < MIN_BITRATE) return MIN_BITRATE - bitrate;
31
+ if (bitrate > MAX_BITRATE) return bitrate - MAX_BITRATE;
32
+ return 0;
33
+ };
34
+ const resolutionDistanceToTarget = file => {
35
+ var _file$width, _file$height;
36
+ return Math.abs(((_file$width = file.width) !== null && _file$width !== void 0 ? _file$width : 0) - TARGET_WIDTH) + Math.abs(((_file$height = file.height) !== null && _file$height !== void 0 ? _file$height : 0) - TARGET_HEIGHT);
37
+ };
38
+ /**
39
+ * Pick the best linear MP4 from a VAST creative's media files: prefer the
40
+ * highest bitrate inside the 5000–40000 kbps window; if none qualify, pick the
41
+ * one closest to the window. Ties break toward 1920×1080. Returns `undefined`
42
+ * when no MP4 is present so image-only VAST keeps working.
43
+ */
44
+ const selectMp4MediaFile = (mediaFiles = []) => {
45
+ const mp4s = (mediaFiles !== null && mediaFiles !== void 0 ? mediaFiles : []).filter(file => !!(file !== null && file !== void 0 && file.fileURL) && file.mimeType === MP4_MIME);
46
+ if (mp4s.length === 0) {
47
+ return undefined;
48
+ }
49
+ const inWindow = mp4s.filter(file => {
50
+ var _file$bitrate;
51
+ const bitrate = (_file$bitrate = file.bitrate) !== null && _file$bitrate !== void 0 ? _file$bitrate : 0;
52
+ return bitrate >= MIN_BITRATE && bitrate <= MAX_BITRATE;
53
+ });
54
+ if (inWindow.length > 0) {
55
+ return [...inWindow].sort((a, b) => {
56
+ var _b$bitrate, _a$bitrate;
57
+ const byBitrate = ((_b$bitrate = b.bitrate) !== null && _b$bitrate !== void 0 ? _b$bitrate : 0) - ((_a$bitrate = a.bitrate) !== null && _a$bitrate !== void 0 ? _a$bitrate : 0);
58
+ return byBitrate !== 0 ? byBitrate : resolutionDistanceToTarget(a) - resolutionDistanceToTarget(b);
59
+ })[0];
60
+ }
61
+ return [...mp4s].sort((a, b) => {
62
+ var _a$bitrate2, _b$bitrate2;
63
+ const byWindow = bitrateDistanceToWindow((_a$bitrate2 = a.bitrate) !== null && _a$bitrate2 !== void 0 ? _a$bitrate2 : 0) - bitrateDistanceToWindow((_b$bitrate2 = b.bitrate) !== null && _b$bitrate2 !== void 0 ? _b$bitrate2 : 0);
64
+ return byWindow !== 0 ? byWindow : resolutionDistanceToTarget(a) - resolutionDistanceToTarget(b);
65
+ })[0];
66
+ };
67
+ const vastClient = new VASTClient();
68
+ const log = createLogger('ui:gam-static');
69
+ const preloadImage = src => {
70
+ return new Promise((resolve, reject) => {
71
+ const image = new Image();
72
+ image.onload = () => resolve(src);
73
+ image.onerror = () => reject();
74
+ image.src = src;
75
+ });
76
+ };
77
+ const externalPauseAd = instance => {
78
+ return (gamUrl, $enabled, options) => {
79
+ var _options$template;
80
+ return (
81
+ // `template` MUST be a key part: nanoquery joins parts with `''` (no separator)
82
+ // and its `cache` Map is global per transport, so two stores that produce the
83
+ // same joined key share data AND cross-trigger each other's `revalidate()`.
84
+ // The `tpl:` prefix keeps the url/template boundary unambiguous. Never let this
85
+ // part be `undefined` — an undefined part nulls the WHOLE key and permanently
86
+ // suspends fetching (that is exactly how `$enabled: false` works).
87
+ instance.transport.nanoquery.createFetcherStore([gamUrl, $enabled, `tpl:${(_options$template = options.template) !== null && _options$template !== void 0 ? _options$template : 'default'}`], {
88
+ dedupeTime: options.prefetch ? options.refetchInterval : Infinity,
89
+ revalidateInterval: options.refetchInterval > 0 ? options.refetchInterval : 0,
90
+ // A creative-type mismatch is DETERMINISTIC: this tag returned no usable linear
91
+ // MP4 and retrying it can never change that, so cancel nanoquery's BACKOFF RETRY
92
+ // LOOP (any falsy return does) instead of re-hitting the ad server for the rest of
93
+ // the session — with the default `pauseAdDelay: 0` the store is enabled from mount.
94
+ // This only cancels the retry loop: `revalidateInterval` still re-runs the fetcher
95
+ // on its timer, and `Advertisement.getExternalPauseAd` forces a `revalidate()` on
96
+ // every `$enabled` false → true edge, so with `pauseAdDelay > 0` the tag is still
97
+ // re-requested on every pause.
98
+ // Transient failures (network, CORS, malformed XML, 5xx) still retry; the backoff
99
+ // below mirrors nanoquery's `defaultOnErrorRetry` so the two templates stay on the
100
+ // same curve. The jitter is not decorative — it spreads out retries that would
101
+ // otherwise fire in lockstep when the failure is correlated across stores/clients
102
+ // (ad-server 5xx, CORS), which the `pauseAdDelay: 0` prefetch-at-mount makes more
103
+ // likely.
104
+ // On `default` this is `undefined`, i.e. exactly as if the option were not passed —
105
+ // true as long as the shared nanoquery instance sets no global `onErrorRetry` (an
106
+ // explicit `undefined` DOES shadow a global one, and only then does nanoquery's
107
+ // destructuring default restore `defaultOnErrorRetry`).
108
+ onErrorRetry: isFullbleedVideoTemplate(options.template) ? ({
109
+ error,
110
+ retryCount
111
+ }) => {
112
+ if (error instanceof Error && error.message === 'no_valid_ad') {
113
+ return false;
114
+ }
115
+ return ~~((Math.random() + 0.5) * (1 << Math.min(retryCount, 8))) * 2000;
116
+ } : undefined,
117
+ fetcher: function () {
118
+ var _ref = _asyncToGenerator(function* (adUrl) {
119
+ if (!adUrl || typeof adUrl !== 'string') {
120
+ throw new Error('ad url is empty');
121
+ }
122
+ eventBus.emit('exposedPauseAd', {
123
+ action: 'load',
124
+ payload: {}
125
+ });
126
+ try {
127
+ var _options$template2, _parsedVAST$ads$find;
128
+ // `resolveAll: true` follows every `VASTAdTagURI` wrapper chain and resolves all
129
+ // Ads (the default only resolves the first standalone Ad and stops at the wrapper).
130
+ // Pause-ad tags do not go through `gam()`, so fill the macros here — with the host's values
131
+ // as they are NOW, since this fetcher re-runs on every pause. The store key stays the
132
+ // configured url: `ExposedPauseAd` compares against it.
133
+ const parsedVAST = yield vastClient.get(substituteAdTagMacros(adUrl, instance.sdk.options.get().adTagParams), {
134
+ resolveAll: true
135
+ });
136
+ const template = (_options$template2 = options.template) !== null && _options$template2 !== void 0 ? _options$template2 : 'default';
137
+ // `fullbleed-video` needs an ad that actually carries a usable linear MP4. With
138
+ // `resolveAll: true` the response can be a pod/wrapper chain, so prefer the first
139
+ // ad that satisfies the template and fall back to the first ad with any creative
140
+ // (which preserves the existing `no_valid_ad` error and event sequencing).
141
+ const validAd = isFullbleedVideoTemplate(template) ? (_parsedVAST$ads$find = parsedVAST.ads.find(ad => ad.creatives.some(creative => {
142
+ var _selectMp4MediaFile;
143
+ return creative.type === 'linear' && (// eslint-disable-next-line @typescript-eslint/ban-ts-comment
144
+ // @ts-ignore vast-client linear creative carries mediaFiles
145
+ (_selectMp4MediaFile = selectMp4MediaFile(creative.mediaFiles)) === null || _selectMp4MediaFile === void 0 ? void 0 : _selectMp4MediaFile.fileURL);
146
+ }))) !== null && _parsedVAST$ads$find !== void 0 ? _parsedVAST$ads$find : parsedVAST.ads.find(ads => ads.creatives.length > 0) : parsedVAST.ads.find(ads => ads.creatives.length > 0);
147
+ if (validAd) {
148
+ var _companion$variations, _variation$staticReso;
149
+ const parentId = validAd.id || '';
150
+ log.debug(validAd, 'validAd');
151
+ // The HOST decides which creative wins (SC-322-2). `fullbleed-video` asks
152
+ // for the linear MP4 explicitly, so it is resolved FIRST and never falls
153
+ // back to a static image — the nonlinear/companion branches below `return`
154
+ // early, so without this a VAST carrying BOTH an image and a linear MP4
155
+ // (e.g. Google `single_ad_samples`) would never reach the linear creative.
156
+ // `default` keeps the pre-SC-322 nonlinear → companion order below and can
157
+ // never produce a video.
158
+ if (isFullbleedVideoTemplate(template)) {
159
+ const linear = validAd.creatives.find(creative => {
160
+ var _selectMp4MediaFile2;
161
+ return creative.type === 'linear' && (// eslint-disable-next-line @typescript-eslint/ban-ts-comment
162
+ // @ts-ignore vast-client linear creative carries mediaFiles
163
+ (_selectMp4MediaFile2 = selectMp4MediaFile(creative.mediaFiles)) === null || _selectMp4MediaFile2 === void 0 ? void 0 : _selectMp4MediaFile2.fileURL);
164
+ });
165
+ if (linear) {
166
+ log.debug(linear, 'linear');
167
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
168
+ // @ts-ignore vast-client linear creative carries mediaFiles
169
+ const mediaFile = selectMp4MediaFile(linear.mediaFiles);
170
+ if (mediaFile !== null && mediaFile !== void 0 && mediaFile.fileURL) {
171
+ const id = linear.adId || linear.id || parentId;
172
+ eventBus.emit('exposedPauseAd', {
173
+ action: 'loaded',
174
+ payload: {
175
+ id,
176
+ parentId
177
+ }
178
+ });
179
+ return {
180
+ id,
181
+ parentId,
182
+ url: adUrl,
183
+ backgroundVideoSrc: mediaFile.fileURL,
184
+ ad: validAd,
185
+ creative: linear
186
+ };
187
+ }
188
+ }
189
+ // Host asked for full-bleed video and this VAST carries no usable linear
190
+ // MP4 → fail closed so `ExposedPauseAd`'s error effect auto-closes the ad,
191
+ // exactly like any other unusable VAST. See §5 of the brief.
192
+ throw new Error('no_valid_ad');
193
+ }
194
+ const nonlinear = validAd.creatives.find(creative => creative.type === 'nonlinear');
195
+ if (nonlinear) {
196
+ var _nonlinear$variations;
197
+ const id = nonlinear.adId || nonlinear.id || parentId;
198
+ log.debug(nonlinear, 'nonlinear');
199
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
200
+ // @ts-ignore
201
+ const variation = (_nonlinear$variations = nonlinear.variations) === null || _nonlinear$variations === void 0 ? void 0 : _nonlinear$variations[0];
202
+ const staticResource = variation === null || variation === void 0 ? void 0 : variation.staticResource;
203
+ const adUrlTemplate = variation === null || variation === void 0 ? void 0 : variation.nonlinearClickThroughURLTemplate;
204
+ if (staticResource) {
205
+ yield preloadImage(staticResource);
206
+ eventBus.emit('exposedPauseAd', {
207
+ action: 'loaded',
208
+ payload: {
209
+ id,
210
+ parentId
211
+ }
212
+ });
213
+ return {
214
+ id,
215
+ parentId,
216
+ url: adUrl,
217
+ imageSrc: staticResource,
218
+ adUrl: adUrlTemplate,
219
+ ad: validAd,
220
+ creative: nonlinear,
221
+ variation
222
+ };
223
+ }
224
+ }
225
+ const companion = validAd.creatives.find(creative => creative.type === 'companion');
226
+ log.debug(companion, 'companion');
227
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
228
+ // @ts-ignore
229
+ const variation = companion === null || companion === void 0 || (_companion$variations = companion.variations) === null || _companion$variations === void 0 ? void 0 : _companion$variations[0];
230
+ const staticResource = variation === null || variation === void 0 || (_variation$staticReso = variation.staticResources) === null || _variation$staticReso === void 0 || (_variation$staticReso = _variation$staticReso[0]) === null || _variation$staticReso === void 0 ? void 0 : _variation$staticReso.url;
231
+ const adUrlTemplate = variation === null || variation === void 0 ? void 0 : variation.companionClickThroughURLTemplate;
232
+ // `&& companion` is for TS narrowing only (companion.adId access below);
233
+ // it is runtime-redundant since staticResource derives from companion.
234
+ if (staticResource && companion) {
235
+ const id = companion.adId || companion.id || parentId;
236
+ yield preloadImage(staticResource);
237
+ eventBus.emit('exposedPauseAd', {
238
+ action: 'loaded',
239
+ payload: {
240
+ id,
241
+ parentId
242
+ }
243
+ });
244
+ return {
245
+ id,
246
+ parentId,
247
+ url: adUrl,
248
+ imageSrc: staticResource,
249
+ adUrl: adUrlTemplate,
250
+ ad: validAd,
251
+ creative: companion,
252
+ variation
253
+ };
254
+ }
255
+ }
256
+ throw new Error('no_valid_ad');
257
+ } catch (err) {
258
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
259
+ // @ts-expect-error
260
+ if (err.message !== 'no_valid_ad' && err.message !== 'ad url is empty') {
261
+ console.error(err);
262
+ }
263
+ throw err;
264
+ }
265
+ });
266
+ return function fetcher(_x) {
267
+ return _ref.apply(this, arguments);
268
+ };
269
+ }()
270
+ })
271
+ );
272
+ };
273
+ };
274
+
275
+ export { externalPauseAd, isFullbleedVideoTemplate };
@@ -0,0 +1,171 @@
1
+ import { asyncToGenerator as _asyncToGenerator } from '../_virtual/_rollupPluginBabelHelpers.js';
2
+ import 'core-js/modules/es.error.cause.js';
3
+ import 'core-js/modules/es.array.iterator.js';
4
+ import 'core-js/modules/es.promise.js';
5
+ import 'core-js/modules/web.dom-collections.iterator.js';
6
+ import 'core-js/modules/web.url.js';
7
+ import 'core-js/modules/web.url.to-json.js';
8
+ import 'core-js/modules/web.url-search-params.js';
9
+ import 'core-js/modules/web.url-search-params.delete.js';
10
+ import 'core-js/modules/web.url-search-params.has.js';
11
+ import 'core-js/modules/web.url-search-params.size.js';
12
+ import { safeLocalStorage, safeSessionStorage } from '@streamlayer/sdk-web-storage/classic';
13
+ import { v4, validate, v5 } from 'uuid';
14
+ import { substituteAdTagMacros } from './ad-macros.js';
15
+
16
+ const urlIsNotGam = url => {
17
+ return url.origin !== 'https://pubads.g.doubleclick.net';
18
+ };
19
+ const palScriptUrl = 'https://imasdk.googleapis.com/pal/sdkloader/pal.js';
20
+ const getPal = () => {
21
+ // @ts-expect-error Google PAL is supplied by the host or the script loader below.
22
+ return typeof goog === 'undefined' ? undefined : goog.pal;
23
+ };
24
+ let palReady;
25
+ const ensurePal = /*#__PURE__*/function () {
26
+ var _ref = _asyncToGenerator(function* () {
27
+ if (getPal()) {
28
+ return;
29
+ }
30
+ if (!palReady) {
31
+ palReady = new Promise((resolve, reject) => {
32
+ if (typeof document === 'undefined') {
33
+ reject(new Error('Google PAL requires a browser document'));
34
+ return;
35
+ }
36
+ const existing = document.querySelector(`script[src="${palScriptUrl}"]`);
37
+ const script = existing || document.createElement('script');
38
+ const finish = error => {
39
+ clearTimeout(timeout);
40
+ script.removeEventListener('load', onLoad);
41
+ script.removeEventListener('error', onError);
42
+ if (error) {
43
+ if (!existing) {
44
+ script.remove();
45
+ }
46
+ reject(error);
47
+ } else {
48
+ resolve();
49
+ }
50
+ };
51
+ const onLoad = () => finish(getPal() ? undefined : new Error('Google PAL did not initialize'));
52
+ const onError = () => finish(new Error('Google PAL failed to load'));
53
+ const timeout = setTimeout(() => finish(new Error('Google PAL loading timed out')), 10000);
54
+ script.addEventListener('load', onLoad);
55
+ script.addEventListener('error', onError);
56
+ if (!existing) {
57
+ script.async = true;
58
+ script.src = palScriptUrl;
59
+ document.head.appendChild(script);
60
+ }
61
+ }).catch(error => {
62
+ palReady = undefined;
63
+ throw error;
64
+ });
65
+ }
66
+ yield palReady;
67
+ palReady = undefined;
68
+ });
69
+ return function ensurePal() {
70
+ return _ref.apply(this, arguments);
71
+ };
72
+ }();
73
+ const gam = (rawUrl, adContainer, adTagParams) => {
74
+ // Every VAST/VMAP tag funnels through here, so this is where the macros get filled — for GAM and
75
+ // for any other vendor alike. Studio stores a programmatic tag verbatim, so an unexpanded
76
+ // `[timestamp]` / `[clickmacro]` / `${GDPR}` would otherwise reach the ad server literal, and a
77
+ // literal macro comes back copied into the vendor's click-through. `adTagParams` are the host's
78
+ // consent and click values, read by the caller at request time. The GAM `[placeholder]` tokens
79
+ // are left alone here and filled per parameter below.
80
+ const url = substituteAdTagMacros(rawUrl, adTagParams);
81
+ const parsedUrl = (() => {
82
+ try {
83
+ const parsedUrl = new URL(url);
84
+ return parsedUrl;
85
+ } catch (_err) {
86
+ return undefined;
87
+ }
88
+ })();
89
+ // Non-GAM VAST/VMAP URLs carry no GAM ad-request params and need no PAL nonce, so
90
+ // resolve them directly. PAL is only required for pubads.g.doubleclick.net requests —
91
+ // raw-VAST consumers (e.g. injected sidebar / l-bar) must not depend on the Google PAL
92
+ // SDK being loaded, so this must run before touching `goog`.
93
+ if (parsedUrl && urlIsNotGam(parsedUrl)) {
94
+ // eslint-disable-next-line require-await
95
+ return /*#__PURE__*/_asyncToGenerator(function* () {
96
+ return {
97
+ url: parsedUrl
98
+ };
99
+ });
100
+ }
101
+ if (!parsedUrl) {
102
+ // eslint-disable-next-line require-await
103
+ return /*#__PURE__*/_asyncToGenerator(function* () {
104
+ return undefined;
105
+ });
106
+ }
107
+ const generateUrl = /*#__PURE__*/function () {
108
+ var _ref4 = _asyncToGenerator(function* () {
109
+ try {
110
+ var _adContainer$clientHe, _adContainer$clientWi;
111
+ yield ensurePal();
112
+ const pal = getPal();
113
+ const consentSettings = new pal.ConsentSettings();
114
+ consentSettings.allowStorage = false;
115
+ const nonceLoader = new pal.NonceLoader(consentSettings);
116
+ const parsedUrl = new URL(url);
117
+ const request = new pal.NonceRequest();
118
+ let deviceId = safeLocalStorage().getItem('sl-device-id') || v4();
119
+ let sessionId = safeSessionStorage().getItem('sl-ad-session-id');
120
+ if (!sessionId) {
121
+ sessionId = v4();
122
+ safeSessionStorage().setItem('sl-ad-session-id', sessionId);
123
+ }
124
+ if (!validate(deviceId)) {
125
+ deviceId = v5(`https://${deviceId}`, v5.URL);
126
+ }
127
+ if (!parsedUrl.searchParams.get('url')) {
128
+ parsedUrl.searchParams.set('url', 'https://streamlayer.io');
129
+ }
130
+ if (!parsedUrl.searchParams.get('description_url')) {
131
+ parsedUrl.searchParams.set('description_url', 'https://streamlayer.io');
132
+ }
133
+ request.adWillAutoPlay = parsedUrl.searchParams.get('vpa') === 'auto';
134
+ request.adWillPlayMuted = parsedUrl.searchParams.get('vpmute') === '1';
135
+ request.continuousPlayback = true;
136
+ request.descriptionUrl = parsedUrl.searchParams.get('description_url');
137
+ request.iconsSupported = true;
138
+ request.sessionId = deviceId;
139
+ // Player support for VPAID 2.0, OMID 1.0, and SIMID 1.1
140
+ request.supportedApiFrameworks = '2,7,9';
141
+ request.videoHeight = (_adContainer$clientHe = adContainer === null || adContainer === void 0 ? void 0 : adContainer.clientHeight) !== null && _adContainer$clientHe !== void 0 ? _adContainer$clientHe : 0;
142
+ request.videoWidth = (_adContainer$clientWi = adContainer === null || adContainer === void 0 ? void 0 : adContainer.clientWidth) !== null && _adContainer$clientWi !== void 0 ? _adContainer$clientWi : 0;
143
+ const nonceManager = yield nonceLoader.loadNonceManager(request);
144
+ if (parsedUrl.searchParams.get('is_lat') === '[placeholder]') {
145
+ parsedUrl.searchParams.set('is_lat', '1');
146
+ }
147
+ if (parsedUrl.searchParams.get('rdid') === '[placeholder]') {
148
+ parsedUrl.searchParams.set('rdid', deviceId);
149
+ }
150
+ if (parsedUrl.searchParams.get('correlator') === '[placeholder]') {
151
+ parsedUrl.searchParams.set('correlator', sessionId);
152
+ }
153
+ parsedUrl.searchParams.set('wta', '1');
154
+ parsedUrl.searchParams.set('givn', nonceManager.getNonce());
155
+ return {
156
+ url: parsedUrl,
157
+ manager: nonceManager
158
+ };
159
+ } catch (adError) {
160
+ console.log('AdsManager could not be started', adError);
161
+ return undefined;
162
+ }
163
+ });
164
+ return function generateUrl() {
165
+ return _ref4.apply(this, arguments);
166
+ };
167
+ }();
168
+ return generateUrl;
169
+ };
170
+
171
+ export { gam };
@@ -0,0 +1,58 @@
1
+ import 'core-js/modules/esnext.iterator.constructor.js';
2
+ import 'core-js/modules/esnext.iterator.filter.js';
3
+ import 'core-js/modules/esnext.iterator.find.js';
4
+ import 'core-js/modules/esnext.iterator.map.js';
5
+ import { AdType } from '@streamlayer/sl-eslib/interactive/interactive.common_pb';
6
+
7
+ /**
8
+ * Extension root element name → the placement it names. Only `DoubleBox` is defined by the
9
+ * StreamLayer extension schema today; anything else is a vendor extension we do not interpret.
10
+ */
11
+ const MAP_EXTENSION_NAME_AD_TYPE = {
12
+ DoubleBox: AdType.SIDE_BY_SIDE
13
+ };
14
+ function parseVastExtension(root) {
15
+ var _child$value2, _child2, _child$value3, _child3, _child$value4, _child4, _backgrounds$find, _backgrounds$find2, _logos$find;
16
+ if (!root) {
17
+ return undefined;
18
+ }
19
+ const child = (node, name) => node.children.find(c => c.name === name);
20
+ const childrenByName = (node, name) => node.children.filter(c => c.name === name);
21
+ const urlOf = node => {
22
+ var _child$value, _child;
23
+ return (_child$value = (_child = child(node, 'Url')) === null || _child === void 0 ? void 0 : _child.value) !== null && _child$value !== void 0 ? _child$value : '';
24
+ };
25
+ const num = v => v == null ? NaN : Number(v);
26
+ const adName = (_child$value2 = (_child2 = child(root, 'AdName')) === null || _child2 === void 0 ? void 0 : _child2.value) !== null && _child$value2 !== void 0 ? _child$value2 : '';
27
+ const backgroundsNode = child(root, 'Backgrounds');
28
+ const backgrounds = backgroundsNode ? childrenByName(backgroundsNode, 'Background').map(bg => ({
29
+ orientation: bg.attributes.orientation,
30
+ width: num(bg.attributes.width),
31
+ height: num(bg.attributes.height),
32
+ type: bg.attributes.type,
33
+ url: urlOf(bg)
34
+ })) : [];
35
+ const logosNode = child(root, 'Logos');
36
+ const logos = logosNode ? childrenByName(logosNode, 'Logo').map(logo => ({
37
+ role: logo.attributes.role,
38
+ width: num(logo.attributes.width),
39
+ height: num(logo.attributes.height),
40
+ type: logo.attributes.type,
41
+ url: urlOf(logo)
42
+ })) : [];
43
+ const ctaNode = child(root, 'CTA');
44
+ const cta = {
45
+ label: ctaNode ? (_child$value3 = (_child3 = child(ctaNode, 'Label')) === null || _child3 === void 0 ? void 0 : _child3.value) !== null && _child$value3 !== void 0 ? _child$value3 : '' : '',
46
+ type: ctaNode ? (_child$value4 = (_child4 = child(ctaNode, 'Type')) === null || _child4 === void 0 ? void 0 : _child4.value) !== null && _child$value4 !== void 0 ? _child$value4 : '' : ''
47
+ };
48
+ return {
49
+ adName,
50
+ adType: MAP_EXTENSION_NAME_AD_TYPE[root.name],
51
+ landscapeBackground: (_backgrounds$find = backgrounds.find(b => b.orientation === 'landscape')) === null || _backgrounds$find === void 0 ? void 0 : _backgrounds$find.url,
52
+ portraitBackground: (_backgrounds$find2 = backgrounds.find(b => b.orientation === 'portrait')) === null || _backgrounds$find2 === void 0 ? void 0 : _backgrounds$find2.url,
53
+ sponsorLogo: (_logos$find = logos.find(l => l.role === 'header')) === null || _logos$find === void 0 ? void 0 : _logos$find.url,
54
+ ctaLabel: cta.label
55
+ };
56
+ }
57
+
58
+ export { parseVastExtension };
@@ -0,0 +1,131 @@
1
+ import 'core-js/modules/esnext.iterator.constructor.js';
2
+ import 'core-js/modules/esnext.iterator.filter.js';
3
+ import 'core-js/modules/esnext.iterator.map.js';
4
+ import { FactoidMediaMode, CardFormat, PromotionFormat, PauseAdLayout, PromotionButtonType, QRCode, UserAccountInfo, NotificationSponsorLogoMode, UseGeneralLogo } from '@streamlayer/sl-eslib/interactive/interactive.common_pb';
5
+
6
+ /**
7
+ * Insight body cap (SC-353). Studio enforces it on the Additional Content field; the
8
+ * SDK clamps as well so a card authored before the limit landed cannot overflow the
9
+ * screen — the format is explicitly designed to fit without scrolling.
10
+ */
11
+ const INSIGHT_BODY_MAX_LENGTH = 600;
12
+ /**
13
+ * Sponsor logo for a factoid card. Same General-tab opt-in as the promotion path below,
14
+ * but the logo lives on `question.promotion.sponsor` (a factoid card carries no
15
+ * `PromotionOptions`) and Studio writes the uploaded file to `logo`, not `centerLogo`.
16
+ *
17
+ * `logo` therefore WINS over `centerLogo`: the latter is an optional field carried over from the
18
+ * notification/header surfaces, so a card that kept a stale `centerLogo` from an earlier revision
19
+ * would otherwise render it instead of the Insight logo the author just uploaded.
20
+ */
21
+ const resolveInsightSponsorLogo = question => {
22
+ var _question$promotion, _question$promotion2;
23
+ const sponsor = (_question$promotion = question.promotion) === null || _question$promotion === void 0 ? void 0 : _question$promotion.sponsor;
24
+ if ((sponsor === null || sponsor === void 0 ? void 0 : sponsor.logoPosition) === NotificationSponsorLogoMode.NOTIFICATION_SPONSOR_LOGO_NONE) {
25
+ return undefined;
26
+ }
27
+ return (sponsor === null || sponsor === void 0 ? void 0 : sponsor.useGeneralLogo) === UseGeneralLogo.ENABLED ? ((_question$promotion2 = question.promotion) === null || _question$promotion2 === void 0 || (_question$promotion2 = _question$promotion2.generalTabMetadata) === null || _question$promotion2 === void 0 ? void 0 : _question$promotion2.advertiserTransparentLogo) || undefined : (sponsor === null || sponsor === void 0 ? void 0 : sponsor.logo) || (sponsor === null || sponsor === void 0 ? void 0 : sponsor.centerLogo) || undefined;
28
+ };
29
+ const MEDIA_MODE_BY_PROTO = {
30
+ [FactoidMediaMode.IMAGE]: 'image',
31
+ [FactoidMediaMode.CIRCLE]: 'circle'
32
+ };
33
+ /**
34
+ * Insight Pause Ad, Full Bleed (SC-353). A factoid card, so none of the promotion
35
+ * plumbing above applies: the heading is the question subject, the body is
36
+ * `FactoidOptions.body`, and the sponsor logo comes off `question.promotion.sponsor`.
37
+ *
38
+ * `FACTOID_MEDIA_MODE_VIDEO` is deliberately treated as "no media": the full-bleed
39
+ * format has no player chrome and no way to start playback, unlike the sidebar variant.
40
+ */
41
+ const parseInsightPauseAdCard = question => {
42
+ var _question$options, _MEDIA_MODE_BY_PROTO$, _question$backgroundI;
43
+ const factoidOptions = ((_question$options = question.options) === null || _question$options === void 0 ? void 0 : _question$options.options.case) === 'factoid' ? question.options.options.value : undefined;
44
+ if (!factoidOptions) {
45
+ return null;
46
+ }
47
+ const mediaMode = (_MEDIA_MODE_BY_PROTO$ = MEDIA_MODE_BY_PROTO[factoidOptions.mode]) !== null && _MEDIA_MODE_BY_PROTO$ !== void 0 ? _MEDIA_MODE_BY_PROTO$ : 'none';
48
+ const body = factoidOptions.body.slice(0, INSIGHT_BODY_MAX_LENGTH);
49
+ return {
50
+ id: question.id,
51
+ parentId: question.eventId || question.id,
52
+ // `landscapeUrl` ONLY. `portraitUrl` keeps stale artwork from earlier revisions of the
53
+ // card that Studio no longer shows in its Background Image slot, so falling back to it
54
+ // paints a background the author has already removed. Unlike the standard card, an
55
+ // Insight one renders fine with no background at all — its copy is the ad.
56
+ backgroundImageSrc: ((_question$backgroundI = question.backgroundImage) === null || _question$backgroundI === void 0 ? void 0 : _question$backgroundI.landscapeUrl) || undefined,
57
+ kind: 'insight',
58
+ layout: 'left',
59
+ isInteractive: false,
60
+ logoSrc: resolveInsightSponsorLogo(question),
61
+ title: question.subject || undefined,
62
+ description: body || undefined,
63
+ mediaSrc: mediaMode === 'none' ? undefined : factoidOptions.image || undefined,
64
+ mediaMode
65
+ };
66
+ };
67
+ const parsePauseAdCard = question => {
68
+ var _question$options2, _question$backgroundI2, _question$options3, _pauseAdOptions$layou, _question$promotion3, _promotionOptions$ban, _promotionOptions$ban2, _pauseAdOptions$qrCod, _pauseAdOptions$qrCod2;
69
+ if (((_question$options2 = question.options) === null || _question$options2 === void 0 ? void 0 : _question$options2.options.case) === 'factoid') {
70
+ return parseInsightPauseAdCard(question);
71
+ }
72
+ const backgroundImageSrc = (_question$backgroundI2 = question.backgroundImage) === null || _question$backgroundI2 === void 0 ? void 0 : _question$backgroundI2.landscapeUrl;
73
+ if (!backgroundImageSrc) {
74
+ return null;
75
+ }
76
+ const promotionOptions = ((_question$options3 = question.options) === null || _question$options3 === void 0 ? void 0 : _question$options3.options.case) === 'promotion' ? question.options.options.value : undefined;
77
+ const pauseAdOptions = promotionOptions === null || promotionOptions === void 0 ? void 0 : promotionOptions.pauseAdOptions;
78
+ const optionsSponsor = promotionOptions === null || promotionOptions === void 0 ? void 0 : promotionOptions.sponsor;
79
+ const layoutProto = (_pauseAdOptions$layou = pauseAdOptions === null || pauseAdOptions === void 0 ? void 0 : pauseAdOptions.layout) !== null && _pauseAdOptions$layou !== void 0 ? _pauseAdOptions$layou : PauseAdLayout.UNSET;
80
+ const layout = layoutProto === PauseAdLayout.CENTER ? 'center' : layoutProto === PauseAdLayout.IMAGE ? 'image' : 'left';
81
+ const isInteractive = (promotionOptions === null || promotionOptions === void 0 ? void 0 : promotionOptions.buttonType) === PromotionButtonType.SEND_TO_USER;
82
+ const hasQr = (pauseAdOptions === null || pauseAdOptions === void 0 ? void 0 : pauseAdOptions.qrCode) === QRCode.QR_CODE_ENABLED && !!(pauseAdOptions !== null && pauseAdOptions !== void 0 && pauseAdOptions.qrCodeUrl);
83
+ let ctaLabel;
84
+ let ctaUrl;
85
+ if (isInteractive && (promotionOptions === null || promotionOptions === void 0 ? void 0 : promotionOptions.buttonOptions.case) === 'sendToUserOptions') {
86
+ const accountInfo = promotionOptions.buttonOptions.value.userAccountInfo;
87
+ const isPhone = accountInfo === UserAccountInfo.PHONE;
88
+ ctaUrl = isPhone ? 'sendToPhone:' : 'sendToEmail:';
89
+ ctaLabel = isPhone ? 'Send info to account phone' : 'Send info to account email';
90
+ }
91
+ const logoSrc = (optionsSponsor === null || optionsSponsor === void 0 ? void 0 : optionsSponsor.logoPosition) === NotificationSponsorLogoMode.NOTIFICATION_SPONSOR_LOGO_NONE ? undefined : (optionsSponsor === null || optionsSponsor === void 0 ? void 0 : optionsSponsor.useGeneralLogo) === UseGeneralLogo.ENABLED ? ((_question$promotion3 = question.promotion) === null || _question$promotion3 === void 0 || (_question$promotion3 = _question$promotion3.generalTabMetadata) === null || _question$promotion3 === void 0 ? void 0 : _question$promotion3.advertiserTransparentLogo) || undefined : (optionsSponsor === null || optionsSponsor === void 0 ? void 0 : optionsSponsor.centerLogo) || undefined;
92
+ return {
93
+ id: question.id,
94
+ parentId: question.eventId || question.id,
95
+ backgroundImageSrc,
96
+ layout,
97
+ logoSrc,
98
+ title: (promotionOptions === null || promotionOptions === void 0 || (_promotionOptions$ban = promotionOptions.banner) === null || _promotionOptions$ban === void 0 ? void 0 : _promotionOptions$ban.title) || undefined,
99
+ description: (promotionOptions === null || promotionOptions === void 0 || (_promotionOptions$ban2 = promotionOptions.banner) === null || _promotionOptions$ban2 === void 0 ? void 0 : _promotionOptions$ban2.body) || undefined,
100
+ isInteractive,
101
+ ctaLabel: ctaLabel || undefined,
102
+ ctaUrl: ctaUrl || undefined,
103
+ qrCodeUrl: hasQr ? (_pauseAdOptions$qrCod = pauseAdOptions === null || pauseAdOptions === void 0 ? void 0 : pauseAdOptions.qrCodeUrl) !== null && _pauseAdOptions$qrCod !== void 0 ? _pauseAdOptions$qrCod : undefined : undefined,
104
+ qrCodeLabel: hasQr ? (_pauseAdOptions$qrCod2 = pauseAdOptions === null || pauseAdOptions === void 0 ? void 0 : pauseAdOptions.qrCodeLabel) !== null && _pauseAdOptions$qrCod2 !== void 0 ? _pauseAdOptions$qrCod2 : undefined : undefined
105
+ };
106
+ };
107
+ /**
108
+ * Which cards from `pauseAdList` the SDK can actually draw. Both renderers are full bleed; a
109
+ * Sidebar-format pause ad has no chrome yet and is skipped.
110
+ */
111
+ const isFullBleedPauseAd = question => {
112
+ var _question$options4, _question$options5;
113
+ if (((_question$options4 = question.options) === null || _question$options4 === void 0 ? void 0 : _question$options4.options.case) === 'factoid') {
114
+ // Insight Pause Ad (SC-353). Studio does not write `CardFormat` on these cards yet
115
+ // (SLW-7062), so an UNSET format is taken as Full Bleed — the only Insight pause-ad chrome
116
+ // the SDK has. An explicit Sidebar stays out. Drop the UNSET arm once Studio sets the field.
117
+ return question.format === CardFormat.FULL_BLEED || question.format === CardFormat.UNSET;
118
+ }
119
+ const promotionOptions = ((_question$options5 = question.options) === null || _question$options5 === void 0 ? void 0 : _question$options5.options.case) === 'promotion' ? question.options.options.value : undefined;
120
+ return (promotionOptions === null || promotionOptions === void 0 ? void 0 : promotionOptions.format) === PromotionFormat.FULL_BLEED || question.format === CardFormat.FULL_BLEED;
121
+ };
122
+ /**
123
+ * The `pauseAdList` response, shaped for the renderers: keep the cards the SDK has chrome for,
124
+ * turn each into a `PauseAdCard`, drop the ones that cannot be drawn.
125
+ *
126
+ * Pure on purpose — `pause-ad.ts` owns the store and the transport, this owns the card shapes,
127
+ * and only this half needs unit tests.
128
+ */
129
+ const parsePauseAdCards = questions => questions.filter(isFullBleedPauseAd).map(parsePauseAdCard).filter(card => card !== null);
130
+
131
+ export { INSIGHT_BODY_MAX_LENGTH, parsePauseAdCards };