@streamlayer/feature-advertisement 1.1.0 → 1.1.1-rc.1342

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 (62) hide show
  1. package/classic/package.json +5 -0
  2. package/lib/cjs/advertisement.d.ts +64 -0
  3. package/lib/cjs/advertisement.js +307 -0
  4. package/lib/cjs/index.d.ts +19 -0
  5. package/lib/cjs/index.js +39 -0
  6. package/lib/cjs/package.json +1 -0
  7. package/lib/cjs/units/external-ad.d.ts +58 -0
  8. package/lib/cjs/units/external-ad.js +390 -0
  9. package/lib/cjs/units/external-pause-ad.d.ts +50 -0
  10. package/lib/cjs/units/external-pause-ad.js +253 -0
  11. package/lib/cjs/units/gam.d.ts +16 -0
  12. package/lib/cjs/units/gam.js +104 -0
  13. package/lib/cjs/units/parse-vast-extensions.d.ts +17 -0
  14. package/lib/cjs/units/parse-vast-extensions.js +52 -0
  15. package/lib/cjs/units/pause-ad-card.d.ts +49 -0
  16. package/lib/cjs/units/pause-ad-card.js +136 -0
  17. package/lib/cjs/units/pause-ad.d.ts +16 -0
  18. package/lib/cjs/units/pause-ad.js +52 -0
  19. package/lib/cjs/units/ssai-ad-break.d.ts +21 -0
  20. package/lib/cjs/units/ssai-ad-break.js +11 -0
  21. package/lib/cjs/units/ssai-creative.d.ts +89 -0
  22. package/lib/cjs/units/ssai-creative.js +136 -0
  23. package/lib/cjs/units/ssai-detector.d.ts +180 -0
  24. package/lib/cjs/units/ssai-detector.js +1156 -0
  25. package/lib/cjs/units/ssai-preload.d.ts +20 -0
  26. package/lib/cjs/units/ssai-preload.js +62 -0
  27. package/lib/cjs/units/ssai-providers.d.ts +117 -0
  28. package/lib/cjs/units/ssai-providers.js +263 -0
  29. package/lib/cjs/units/vast-tracker.d.ts +45 -0
  30. package/lib/cjs/units/vast-tracker.js +64 -0
  31. package/lib/classic/_virtual/_rollupPluginBabelHelpers.js +71 -0
  32. package/lib/classic/advertisement.js +331 -0
  33. package/lib/classic/cjs/_virtual/_rollupPluginBabelHelpers.js +77 -0
  34. package/lib/classic/cjs/advertisement.js +333 -0
  35. package/lib/classic/cjs/index.js +47 -0
  36. package/lib/classic/cjs/package.json +1 -0
  37. package/lib/classic/cjs/units/external-ad.js +453 -0
  38. package/lib/classic/cjs/units/external-pause-ad.js +274 -0
  39. package/lib/classic/cjs/units/gam.js +110 -0
  40. package/lib/classic/cjs/units/parse-vast-extensions.js +56 -0
  41. package/lib/classic/cjs/units/pause-ad-card.js +134 -0
  42. package/lib/classic/cjs/units/pause-ad.js +72 -0
  43. package/lib/classic/cjs/units/ssai-ad-break.js +11 -0
  44. package/lib/classic/cjs/units/ssai-creative.js +142 -0
  45. package/lib/classic/cjs/units/ssai-detector.js +1170 -0
  46. package/lib/classic/cjs/units/ssai-preload.js +72 -0
  47. package/lib/classic/cjs/units/ssai-providers.js +299 -0
  48. package/lib/classic/cjs/units/vast-tracker.js +81 -0
  49. package/lib/classic/index.js +28 -0
  50. package/lib/classic/units/external-ad.js +447 -0
  51. package/lib/classic/units/external-pause-ad.js +271 -0
  52. package/lib/classic/units/gam.js +108 -0
  53. package/lib/classic/units/parse-vast-extensions.js +54 -0
  54. package/lib/classic/units/pause-ad-card.js +131 -0
  55. package/lib/classic/units/pause-ad.js +70 -0
  56. package/lib/classic/units/ssai-ad-break.js +9 -0
  57. package/lib/classic/units/ssai-creative.js +140 -0
  58. package/lib/classic/units/ssai-detector.js +1167 -0
  59. package/lib/classic/units/ssai-preload.js +68 -0
  60. package/lib/classic/units/ssai-providers.js +289 -0
  61. package/lib/classic/units/vast-tracker.js +77 -0
  62. package/package.json +41 -8
@@ -0,0 +1,453 @@
1
+ 'use strict';
2
+
3
+ var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js');
4
+ require('core-js/modules/es.symbol.description.js');
5
+ require('core-js/modules/es.error.cause.js');
6
+ require('core-js/modules/es.array.iterator.js');
7
+ require('core-js/modules/es.array.reduce.js');
8
+ require('core-js/modules/es.array.sort.js');
9
+ require('core-js/modules/es.json.stringify.js');
10
+ require('core-js/modules/es.promise.js');
11
+ require('core-js/modules/es.regexp.exec.js');
12
+ require('core-js/modules/es.string.replace.js');
13
+ require('core-js/modules/es.string.replace-all.js');
14
+ require('core-js/modules/esnext.iterator.constructor.js');
15
+ require('core-js/modules/esnext.iterator.filter.js');
16
+ require('core-js/modules/esnext.iterator.find.js');
17
+ require('core-js/modules/esnext.iterator.map.js');
18
+ require('core-js/modules/esnext.iterator.reduce.js');
19
+ require('core-js/modules/web.dom-collections.iterator.js');
20
+ require('core-js/modules/web.url.js');
21
+ require('core-js/modules/web.url.to-json.js');
22
+ require('core-js/modules/web.url-search-params.js');
23
+ require('core-js/modules/web.url-search-params.delete.js');
24
+ require('core-js/modules/web.url-search-params.has.js');
25
+ require('core-js/modules/web.url-search-params.size.js');
26
+ var classic = require('@streamlayer/sdk-web-logger/classic');
27
+ var vastClient = require('@dailymotion/vast-client');
28
+ require('@streamlayer/sdk-web-api/classic');
29
+ var VMAP = require('vmap');
30
+ var gam = require('./gam.js');
31
+ var parseVastExtensions = require('./parse-vast-extensions.js');
32
+
33
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
34
+
35
+ var VMAP__default = /*#__PURE__*/_interopDefault(VMAP);
36
+
37
+ const log = classic.createLogger('ui:gam-external-ad');
38
+ /** Default budget for a single VAST resolution before it is treated as "no ad". */
39
+ const VAST_FETCH_TIMEOUT_MS = 10000;
40
+ /**
41
+ * Resolve `promise`, but fall back to `undefined` if it doesn't settle within `ms`.
42
+ * A slow/unreachable VAST endpoint must degrade to "no ad" instead of leaving the
43
+ * fetcher (and the sidebar) stuck waiting. Uses a timer rather than `AbortController`
44
+ * so it stays safe on the ES5/Chrome-32 target.
45
+ */
46
+ const withTimeout = (promise, ms) => {
47
+ return Promise.race([promise, new Promise(resolve => {
48
+ setTimeout(() => resolve(undefined), ms);
49
+ })]);
50
+ };
51
+ // const preloadImage = (src: string) => {
52
+ // return new Promise((resolve, reject) => {
53
+ // const image = new Image()
54
+ // image.onload = () => resolve(src)
55
+ // image.onerror = () => reject()
56
+ // image.src = src
57
+ // })
58
+ // }
59
+ /**
60
+ * Get the first valid creative from an ad
61
+ */
62
+ const getCreative = ad => {
63
+ return ad.creatives.find(creative => creative.mediaFiles);
64
+ };
65
+ /**
66
+ * Get the URL of the first media file in the creative
67
+ */
68
+ const getMediaFileUrl = creative => {
69
+ var _mediaFiles$find;
70
+ const mediaFiles = creative === null || creative === void 0 ? void 0 : creative.mediaFiles;
71
+ if (!(mediaFiles !== null && mediaFiles !== void 0 && mediaFiles.length)) {
72
+ return '';
73
+ }
74
+ return ((_mediaFiles$find = mediaFiles.find(mediaFile => mediaFile.fileURL)) === null || _mediaFiles$find === void 0 ? void 0 : _mediaFiles$find.fileURL) || '';
75
+ };
76
+ /**
77
+ * Extract the linear creative's `<ClickThrough>` URL. Sources the sidebar CTA (QR code
78
+ * + click-through). `videoClickThroughURLTemplate` isn't strongly typed in the client lib.
79
+ */
80
+ const getClickThrough = creative => {
81
+ var _creative$videoClickT;
82
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
83
+ // @ts-ignore — vast-client types `videoClickThroughURLTemplate` loosely
84
+ return (creative === null || creative === void 0 || (_creative$videoClickT = creative.videoClickThroughURLTemplate) === null || _creative$videoClickT === void 0 ? void 0 : _creative$videoClickT.url) || undefined;
85
+ };
86
+ /**
87
+ * Extract every `<Companion>` variation of the first `<CompanionAds>` creative that carries a
88
+ * `<StaticResource>`, in document order. `width`/`height` are XML attributes (strings at
89
+ * runtime), normalised to numbers so the size mapping can compare them.
90
+ */
91
+ const getCompanions = ad => {
92
+ const creative = ad.creatives.find(item => item.type === 'companion');
93
+ return ((creative === null || creative === void 0 ? void 0 : creative.variations) || []).reduce((acc, variation) => {
94
+ var _variation$staticReso, _variation$companionC;
95
+ const imageUrl = 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;
96
+ if (!imageUrl) {
97
+ return acc;
98
+ }
99
+ return acc.concat({
100
+ imageUrl,
101
+ clickUrl: (_variation$companionC = variation.companionClickThroughURLTemplate) !== null && _variation$companionC !== void 0 ? _variation$companionC : undefined,
102
+ width: Number(variation.width) || undefined,
103
+ height: Number(variation.height) || undefined,
104
+ creative,
105
+ variation
106
+ });
107
+ }, []);
108
+ };
109
+ /** Product companion mapping: the 300x75 companion is the logo, the banner-sized one the media. */
110
+ const LOGO_COMPANION_WIDTH = 300;
111
+ const LOGO_COMPANION_HEIGHT = 75;
112
+ const companionArea = companion => (companion.width || 0) * (companion.height || 0);
113
+ /**
114
+ * Split a `<CompanionAds>` creative's variations into the logo and media slots.
115
+ *
116
+ * The pair mapping activates only for a tag that ships an exact 300x75 companion next to at
117
+ * least one other. Deliberately conservative pending broader product sizes: on any other tag
118
+ * no companion is promoted to the logo slot and the first one stays the media/strip candidate.
119
+ */
120
+ const selectCompanions = companions => {
121
+ const logo = companions.length > 1 ? companions.find(companion => companion.width === LOGO_COMPANION_WIDTH && companion.height === LOGO_COMPANION_HEIGHT) : undefined;
122
+ if (!logo) {
123
+ return {
124
+ logo: undefined,
125
+ media: companions[0]
126
+ };
127
+ }
128
+ const media = companions.filter(companion => companion !== logo).sort((a, b) => companionArea(a) - companionArea(b)).pop();
129
+ return {
130
+ logo,
131
+ media
132
+ };
133
+ };
134
+ /**
135
+ * Extract the first `<NonLinearAds>` creative's static-image variation. Both the creative (which
136
+ * owns the `<TrackingEvents>`) and the variation (which owns the click-through) are kept —
137
+ * `VASTTracker` needs both.
138
+ *
139
+ * `@dailymotion/vast-client` 6.4.2 emits `nonlinearClickThroughURLTemplate` (lower-case `l`),
140
+ * which the `@types/vast-client` shim declares as `nonLinearClickThroughURLTemplate` — the
141
+ * runtime spelling is authoritative.
142
+ */
143
+ const getNonLinear = ad => {
144
+ var _creative$variations, _variation$nonlinearC;
145
+ const creative = ad.creatives.find(item => item.type === 'nonlinear');
146
+ const variation = creative === null || creative === void 0 || (_creative$variations = creative.variations) === null || _creative$variations === void 0 ? void 0 : _creative$variations[0];
147
+ const imageUrl = variation === null || variation === void 0 ? void 0 : variation.staticResource;
148
+ if (!creative || !variation || !imageUrl) {
149
+ return undefined;
150
+ }
151
+ return {
152
+ imageUrl,
153
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
154
+ // @ts-ignore — see the runtime-vs-shim spelling note above
155
+ clickUrl: (_variation$nonlinearC = variation.nonlinearClickThroughURLTemplate) !== null && _variation$nonlinearC !== void 0 ? _variation$nonlinearC : undefined,
156
+ width: Number(variation.width) || undefined,
157
+ height: Number(variation.height) || undefined,
158
+ creative,
159
+ variation
160
+ };
161
+ };
162
+ /**
163
+ * Map a parsed VAST response into our `AdData` shape. Shared by both acquisition
164
+ * paths — the GAM/VMAP tag flow and the Prebid auction flow — so the two always
165
+ * produce identical downstream data. Returns `undefined` when the response has no
166
+ * ad carrying creatives.
167
+ */
168
+ const buildAdData = (parsedVAST, order) => {
169
+ var _validAd$extensions$f;
170
+ const validAd = parsedVAST.ads.find(ads => ads.creatives.length > 0);
171
+ if (!validAd) {
172
+ log.warn({
173
+ order,
174
+ ads: parsedVAST.ads.length
175
+ }, 'vast response carries no ad with creatives — treated as no-ad');
176
+ return undefined;
177
+ }
178
+ const extension = (_validAd$extensions$f = validAd.extensions.find(ext => ext.attributes.type.startsWith('streamlayer'))) === null || _validAd$extensions$f === void 0 ? void 0 : _validAd$extensions$f.children[0];
179
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
180
+ // @ts-expect-error
181
+ const parsedExtension = parseVastExtensions.parseVastExtension(extension);
182
+ const creative = getCreative(validAd);
183
+ const mediaFileUrl = getMediaFileUrl(creative);
184
+ const companions = getCompanions(validAd);
185
+ const {
186
+ logo,
187
+ media: mediaCompanion
188
+ } = selectCompanions(companions);
189
+ const nonLinear = getNonLinear(validAd);
190
+ const clickThrough = getClickThrough(creative) || (nonLinear === null || nonLinear === void 0 ? void 0 : nonLinear.clickUrl) || (mediaCompanion === null || mediaCompanion === void 0 ? void 0 : mediaCompanion.clickUrl);
191
+ log.info({
192
+ order,
193
+ adId: validAd.id,
194
+ title: validAd.title || undefined,
195
+ found: {
196
+ linear: !!creative,
197
+ mediaFile: mediaFileUrl || undefined,
198
+ nonLinear: nonLinear ? `${nonLinear.width}x${nonLinear.height}` : undefined,
199
+ companions: companions.map(item => `${item.width}x${item.height}`)
200
+ },
201
+ selected: {
202
+ logo: logo ? {
203
+ size: `${logo.width}x${logo.height}`,
204
+ url: logo.imageUrl
205
+ } : undefined,
206
+ media: mediaCompanion ? {
207
+ size: `${mediaCompanion.width}x${mediaCompanion.height}`,
208
+ url: mediaCompanion.imageUrl
209
+ } : undefined,
210
+ clickThrough: !!clickThrough
211
+ }
212
+ }, 'vast ad parsed');
213
+ return {
214
+ creative,
215
+ order,
216
+ mediaFileUrl,
217
+ companion: companions[0],
218
+ logo,
219
+ mediaCompanion,
220
+ nonLinear,
221
+ clickThrough,
222
+ title: validAd.title || undefined,
223
+ description: validAd.description || undefined,
224
+ ad: validAd,
225
+ extension: parsedExtension,
226
+ raw: parsedVAST
227
+ };
228
+ };
229
+ /** Path of the self-hosted Prebid Server OpenRTB auction endpoint. */
230
+ const PREBID_AUCTION_PATH = '/openrtb2/auction';
231
+ /**
232
+ * A Prebid link points at our Prebid Server auction endpoint (staging:
233
+ * `https://prebid.next.streamlayer.io/openrtb2/auction`) rather than at a VAST tag.
234
+ * Detected by path so both staging and production hosts match.
235
+ */
236
+ const isPrebidUrl = url => url.pathname.endsWith(PREBID_AUCTION_PATH);
237
+ /** Unique per-auction request id — Prebid responses are never cached/reused (RFC §5). */
238
+ const createRequestId = () => `sl-web-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
239
+ /**
240
+ * Build the OpenRTB request body for a single video auction. Placement config is
241
+ * carried on the Prebid link's query string so the consumer-facing interface stays a
242
+ * single URL string:
243
+ * - `configId` → `imp.ext.prebid.storedrequest.id` (production stored request).
244
+ * - `storedauctionresponse` → `imp.ext.prebid.storedauctionresponse.id` (staging mock).
245
+ * - `test=1` → top-level `test` flag.
246
+ * - `w` / `h` → video dimensions (default 1920×1080).
247
+ */
248
+ const buildAuctionRequest = url => {
249
+ var _ref, _params$get, _params$get2;
250
+ const params = url.searchParams;
251
+ const width = Number(params.get('w')) || 1920;
252
+ const height = Number(params.get('h')) || 1080;
253
+ const configId = (_ref = (_params$get = params.get('configId')) !== null && _params$get !== void 0 ? _params$get : params.get('config_id')) !== null && _ref !== void 0 ? _ref : undefined;
254
+ const storedAuctionResponse = (_params$get2 = params.get('storedauctionresponse')) !== null && _params$get2 !== void 0 ? _params$get2 : undefined;
255
+ const prebid = {};
256
+ if (storedAuctionResponse) {
257
+ prebid.storedauctionresponse = {
258
+ id: storedAuctionResponse
259
+ };
260
+ }
261
+ if (configId) {
262
+ prebid.storedrequest = {
263
+ id: configId
264
+ };
265
+ }
266
+ prebid.bidder = {
267
+ appnexus: {
268
+ placementId: 13144370
269
+ }
270
+ };
271
+ const request = {
272
+ id: createRequestId(),
273
+ imp: [{
274
+ id: '1',
275
+ video: {
276
+ mimes: ['video/mp4'],
277
+ protocols: [2, 3, 5, 6],
278
+ w: width,
279
+ h: height,
280
+ placement: 1,
281
+ linearity: 1
282
+ },
283
+ ext: {
284
+ prebid
285
+ }
286
+ }],
287
+ site: {
288
+ page: typeof location !== 'undefined' ? location.href : url.origin
289
+ },
290
+ tmax: 500
291
+ };
292
+ if (params.get('test') === '1') {
293
+ request.test = 1;
294
+ }
295
+ return request;
296
+ };
297
+ /**
298
+ * Run one Prebid Server auction and turn the winning inline VAST into an `AdStore`.
299
+ * Every bid's `adm` is inline VAST XML (no URL to fetch); bids are ordered by price
300
+ * so the winner plays first (RFC §2). A single bad bid must not drop the batch.
301
+ */
302
+ const fetchPrebidAds = /*#__PURE__*/function () {
303
+ var _ref2 = _rollupPluginBabelHelpers.asyncToGenerator(function* (prebidUrl) {
304
+ var _auction$seatbid, _params$get3, _params$get4;
305
+ const response = yield fetch(prebidUrl.toString(), {
306
+ method: 'POST',
307
+ headers: {
308
+ 'Content-Type': 'application/json'
309
+ },
310
+ body: JSON.stringify(buildAuctionRequest(prebidUrl))
311
+ });
312
+ if (!response.ok) {
313
+ throw new Error(`prebid auction failed with status ${response.status}`);
314
+ }
315
+ const auction = yield response.json();
316
+ const bids = ((_auction$seatbid = auction.seatbid) !== null && _auction$seatbid !== void 0 ? _auction$seatbid : []).reduce((acc, seat) => {
317
+ var _seat$bid;
318
+ return acc.concat((_seat$bid = seat.bid) !== null && _seat$bid !== void 0 ? _seat$bid : []);
319
+ }, []).filter(bid => !!bid.adm)
320
+ // Highest price wins — the winner is order 0 (RFC §2).
321
+ .sort((a, b) => {
322
+ var _b$price, _a$price;
323
+ return ((_b$price = b.price) !== null && _b$price !== void 0 ? _b$price : 0) - ((_a$price = a.price) !== null && _a$price !== void 0 ? _a$price : 0);
324
+ });
325
+ const parser = new DOMParser();
326
+ const loadedAds = yield Promise.all(bids.map((bid, i) => {
327
+ const xmlDoc = parser.parseFromString(bid.adm, 'text/xml');
328
+ // A fresh `VASTParser` per bid isolates parser state, mirroring the fresh
329
+ // `VASTClient` per link in the tag flow. `resolveAll: true` follows any
330
+ // `VASTAdTagURI` wrapper chain the bid's inline VAST may still contain.
331
+ return new vastClient.VASTParser()
332
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
333
+ // @ts-ignore
334
+ .parseVAST(xmlDoc, {
335
+ resolveAll: true,
336
+ wrapperLimit: 10,
337
+ allowMultipleAds: true
338
+ }).then(parsedVAST => buildAdData(parsedVAST, i)).catch(err => {
339
+ // A single failing bid must not drop the whole batch.
340
+ log.error(err, 'failed to parse prebid VAST');
341
+ return undefined;
342
+ });
343
+ }));
344
+ const params = prebidUrl.searchParams;
345
+ const autoplay = ((_params$get3 = params.get('vpa')) === null || _params$get3 === void 0 ? void 0 : _params$get3.replaceAll('?', '')) !== 'click';
346
+ const muted = ((_params$get4 = params.get('vpmute')) === null || _params$get4 === void 0 ? void 0 : _params$get4.replaceAll('?', '')) === '1';
347
+ return {
348
+ ads: loadedAds.filter(d => !!d),
349
+ autoplay,
350
+ muted
351
+ };
352
+ });
353
+ return function fetchPrebidAds(_x) {
354
+ return _ref2.apply(this, arguments);
355
+ };
356
+ }();
357
+ const externalAd = instance => {
358
+ return gamUrl => instance.transport.nanoquery.createFetcherStore([gamUrl], {
359
+ dedupeTime: Infinity,
360
+ revalidateInterval: 0,
361
+ fetcher: function () {
362
+ var _ref3 = _rollupPluginBabelHelpers.asyncToGenerator(function* (adUrl) {
363
+ var _noncedLink$searchPar, _noncedLink$searchPar2, _noncedLink$searchPar3;
364
+ if (!adUrl || typeof adUrl !== 'string') {
365
+ throw new Error('ad url is empty');
366
+ }
367
+ const parsedAdUrl = (() => {
368
+ try {
369
+ return new URL(adUrl);
370
+ } catch (_unused) {
371
+ return undefined;
372
+ }
373
+ })();
374
+ // Prebid links hit our Prebid Server auction endpoint instead of a VAST tag.
375
+ // Run the OpenRTB auction and parse the winning bid's inline VAST (`adm`) into
376
+ // the same `AdStore` shape the GAM/VMAP path returns. See rfcs/prebid.
377
+ if (parsedAdUrl && isPrebidUrl(parsedAdUrl)) {
378
+ return fetchPrebidAds(parsedAdUrl);
379
+ }
380
+ const generateUrl = gam.gam(adUrl);
381
+ const generatedUrl = yield generateUrl();
382
+ const noncedLink = generatedUrl === null || generatedUrl === void 0 ? void 0 : generatedUrl.url;
383
+ if (!noncedLink) {
384
+ throw new Error('ad url is not generated');
385
+ }
386
+ let links;
387
+ if (((_noncedLink$searchPar = noncedLink.searchParams.get('output')) === null || _noncedLink$searchPar === void 0 ? void 0 : _noncedLink$searchPar.replaceAll('?', '')) === 'vmap') {
388
+ const response = yield withTimeout(fetch(noncedLink.toString()), VAST_FETCH_TIMEOUT_MS);
389
+ if (!response) {
390
+ throw new Error('vmap fetch timed out');
391
+ }
392
+ const parser = new DOMParser();
393
+ const xmlStr = yield response.text();
394
+ const xmlDoc = parser.parseFromString(xmlStr, 'text/xml');
395
+ const vmap = new VMAP__default.default(xmlDoc);
396
+ links = vmap.adBreaks.map(ad => {
397
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
398
+ // @ts-expect-error
399
+ return ad.adSource.adTagURI.uri;
400
+ });
401
+ } else {
402
+ links = [noncedLink.toString()];
403
+ }
404
+ const loadedAds = yield Promise.all(links.map((link, i) => {
405
+ // `resolveAll: true` makes the client follow every `VASTAdTagURI` wrapper chain
406
+ // (up to the default depth of 10) and resolve all Ads, merging wrapper-level
407
+ // Impression/tracking URLs into the inline Ad. The default (`resolveAll: false`)
408
+ // only resolves the first standalone Ad and defers wrappers/remaining Ads to
409
+ // `getNextAds()`, which this fetcher never calls. A fresh `VASTClient` per link
410
+ // isolates parser state so concurrent (VMAP) resolutions don't clobber each other.
411
+ return withTimeout(new vastClient.VASTClient()
412
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
413
+ // @ts-ignore
414
+ .get(link, {
415
+ resolveAll: true,
416
+ wrapperLimit: 10,
417
+ allowMultipleAds: true
418
+ }), VAST_FETCH_TIMEOUT_MS).then(parsedVAST => {
419
+ // `parsedVAST` is `undefined` when the resolution timed out (see `withTimeout`).
420
+ if (!parsedVAST) {
421
+ log.warn({
422
+ link,
423
+ timeoutMs: VAST_FETCH_TIMEOUT_MS
424
+ }, 'vast resolution timed out — treated as no-ad');
425
+ return undefined;
426
+ }
427
+ return buildAdData(parsedVAST, i);
428
+ }).catch(err => {
429
+ // A single failing wrapper chain must not drop the whole batch.
430
+ log.error(err, 'failed to resolve VAST');
431
+ return undefined;
432
+ });
433
+ }));
434
+ const autoplay = ((_noncedLink$searchPar2 = noncedLink.searchParams.get('vpa')) === null || _noncedLink$searchPar2 === void 0 ? void 0 : _noncedLink$searchPar2.replaceAll('?', '')) !== 'click';
435
+ const muted = ((_noncedLink$searchPar3 = noncedLink.searchParams.get('vpmute')) === null || _noncedLink$searchPar3 === void 0 ? void 0 : _noncedLink$searchPar3.replaceAll('?', '')) === '1';
436
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
437
+ // @ts-expect-error
438
+ const nonceManager = generatedUrl.manager;
439
+ return {
440
+ ads: loadedAds.filter(d => !!d),
441
+ autoplay,
442
+ muted,
443
+ nonceManager
444
+ };
445
+ });
446
+ return function fetcher(_x2) {
447
+ return _ref3.apply(this, arguments);
448
+ };
449
+ }()
450
+ });
451
+ };
452
+
453
+ exports.externalAd = externalAd;