stormcloud-video-player 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +245 -156
- package/dist/stormcloud-vp.min.js +1 -1
- package/lib/index.cjs +9759 -5311
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +193 -17
- package/lib/index.d.ts +193 -17
- package/lib/index.js +9761 -5329
- package/lib/index.js.map +1 -1
- package/lib/player/StormcloudVideoPlayer.cjs +7090 -1508
- package/lib/player/StormcloudVideoPlayer.cjs.map +1 -1
- package/lib/player/StormcloudVideoPlayer.d.cts +95 -9
- package/lib/players/HlsPlayer.cjs +7107 -1525
- package/lib/players/HlsPlayer.cjs.map +1 -1
- package/lib/players/HlsPlayer.d.cts +1 -1
- package/lib/players/index.cjs +7098 -1516
- package/lib/players/index.cjs.map +1 -1
- package/lib/sdk/hlsAdPlayer.cjs +302 -266
- package/lib/sdk/hlsAdPlayer.cjs.map +1 -1
- package/lib/sdk/hlsAdPlayer.d.cts +2 -2
- package/lib/sdk/ima.cjs +1375 -0
- package/lib/sdk/ima.cjs.map +1 -0
- package/lib/sdk/ima.d.cts +12 -0
- package/lib/sdk/prebid.cjs +564 -0
- package/lib/sdk/prebid.cjs.map +1 -0
- package/lib/sdk/prebid.d.cts +5 -0
- package/lib/sdk/prebidController.cjs +1560 -0
- package/lib/sdk/prebidController.cjs.map +1 -0
- package/lib/sdk/prebidController.d.cts +11 -0
- package/lib/sdk/vastParser.cjs +393 -0
- package/lib/sdk/vastParser.cjs.map +1 -0
- package/lib/sdk/vastParser.d.cts +45 -0
- package/lib/{types-Ca4ZDaWw.d.cts → types-g2d4Akez.d.cts} +72 -3
- package/lib/ui/StormcloudVideoPlayer.cjs +7130 -1530
- package/lib/ui/StormcloudVideoPlayer.cjs.map +1 -1
- package/lib/ui/StormcloudVideoPlayer.d.cts +1 -1
- package/lib/utils/browserCompat.cjs +222 -20
- package/lib/utils/browserCompat.cjs.map +1 -1
- package/lib/utils/browserCompat.d.cts +9 -1
- package/lib/utils/tracking.d.cts +1 -1
- package/package.json +2 -1
- package/lib/sdk/adstormPlayer.cjs +0 -908
- package/lib/sdk/adstormPlayer.cjs.map +0 -1
- package/lib/sdk/adstormPlayer.d.cts +0 -9
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
function _array_like_to_array(arr, len) {
|
|
3
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
4
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
5
|
+
return arr2;
|
|
6
|
+
}
|
|
7
|
+
function _array_without_holes(arr) {
|
|
8
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
9
|
+
}
|
|
10
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
11
|
+
try {
|
|
12
|
+
var info = gen[key](arg);
|
|
13
|
+
var value = info.value;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
reject(error);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (info.done) {
|
|
19
|
+
resolve(value);
|
|
20
|
+
} else {
|
|
21
|
+
Promise.resolve(value).then(_next, _throw);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function _async_to_generator(fn) {
|
|
25
|
+
return function() {
|
|
26
|
+
var self = this, args = arguments;
|
|
27
|
+
return new Promise(function(resolve, reject) {
|
|
28
|
+
var gen = fn.apply(self, args);
|
|
29
|
+
function _next(value) {
|
|
30
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
31
|
+
}
|
|
32
|
+
function _throw(err) {
|
|
33
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
34
|
+
}
|
|
35
|
+
_next(undefined);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function _iterable_to_array(iter) {
|
|
40
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
41
|
+
}
|
|
42
|
+
function _non_iterable_spread() {
|
|
43
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
44
|
+
}
|
|
45
|
+
function _to_consumable_array(arr) {
|
|
46
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
47
|
+
}
|
|
48
|
+
function _type_of(obj) {
|
|
49
|
+
"@swc/helpers - typeof";
|
|
50
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
51
|
+
}
|
|
52
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
53
|
+
if (!o) return;
|
|
54
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
55
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
56
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
57
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
58
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
59
|
+
}
|
|
60
|
+
function _ts_generator(thisArg, body) {
|
|
61
|
+
var f, y, t, _ = {
|
|
62
|
+
label: 0,
|
|
63
|
+
sent: function() {
|
|
64
|
+
if (t[0] & 1) throw t[1];
|
|
65
|
+
return t[1];
|
|
66
|
+
},
|
|
67
|
+
trys: [],
|
|
68
|
+
ops: []
|
|
69
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
70
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
71
|
+
return this;
|
|
72
|
+
}), g;
|
|
73
|
+
function verb(n) {
|
|
74
|
+
return function(v) {
|
|
75
|
+
return step([
|
|
76
|
+
n,
|
|
77
|
+
v
|
|
78
|
+
]);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function step(op) {
|
|
82
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
83
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
84
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
85
|
+
if (y = 0, t) op = [
|
|
86
|
+
op[0] & 2,
|
|
87
|
+
t.value
|
|
88
|
+
];
|
|
89
|
+
switch(op[0]){
|
|
90
|
+
case 0:
|
|
91
|
+
case 1:
|
|
92
|
+
t = op;
|
|
93
|
+
break;
|
|
94
|
+
case 4:
|
|
95
|
+
_.label++;
|
|
96
|
+
return {
|
|
97
|
+
value: op[1],
|
|
98
|
+
done: false
|
|
99
|
+
};
|
|
100
|
+
case 5:
|
|
101
|
+
_.label++;
|
|
102
|
+
y = op[1];
|
|
103
|
+
op = [
|
|
104
|
+
0
|
|
105
|
+
];
|
|
106
|
+
continue;
|
|
107
|
+
case 7:
|
|
108
|
+
op = _.ops.pop();
|
|
109
|
+
_.trys.pop();
|
|
110
|
+
continue;
|
|
111
|
+
default:
|
|
112
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
113
|
+
_ = 0;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
117
|
+
_.label = op[1];
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
121
|
+
_.label = t[1];
|
|
122
|
+
t = op;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
if (t && _.label < t[2]) {
|
|
126
|
+
_.label = t[2];
|
|
127
|
+
_.ops.push(op);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
if (t[2]) _.ops.pop();
|
|
131
|
+
_.trys.pop();
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
op = body.call(thisArg, _);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
op = [
|
|
137
|
+
6,
|
|
138
|
+
e
|
|
139
|
+
];
|
|
140
|
+
y = 0;
|
|
141
|
+
} finally{
|
|
142
|
+
f = t = 0;
|
|
143
|
+
}
|
|
144
|
+
if (op[0] & 5) throw op[1];
|
|
145
|
+
return {
|
|
146
|
+
value: op[0] ? op[1] : void 0,
|
|
147
|
+
done: true
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
var __defProp = Object.defineProperty;
|
|
152
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
153
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
154
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
155
|
+
var __export = function(target, all) {
|
|
156
|
+
for(var name in all)__defProp(target, name, {
|
|
157
|
+
get: all[name],
|
|
158
|
+
enumerable: true
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
var __copyProps = function(to, from, except, desc) {
|
|
162
|
+
if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") {
|
|
163
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
164
|
+
try {
|
|
165
|
+
var _loop = function() {
|
|
166
|
+
var key = _step.value;
|
|
167
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
168
|
+
get: function() {
|
|
169
|
+
return from[key];
|
|
170
|
+
},
|
|
171
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
175
|
+
} catch (err) {
|
|
176
|
+
_didIteratorError = true;
|
|
177
|
+
_iteratorError = err;
|
|
178
|
+
} finally{
|
|
179
|
+
try {
|
|
180
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
181
|
+
_iterator.return();
|
|
182
|
+
}
|
|
183
|
+
} finally{
|
|
184
|
+
if (_didIteratorError) {
|
|
185
|
+
throw _iteratorError;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return to;
|
|
191
|
+
};
|
|
192
|
+
var __toCommonJS = function(mod) {
|
|
193
|
+
return __copyProps(__defProp({}, "__esModule", {
|
|
194
|
+
value: true
|
|
195
|
+
}), mod);
|
|
196
|
+
};
|
|
197
|
+
// src/sdk/prebid.ts
|
|
198
|
+
var prebid_exports = {};
|
|
199
|
+
__export(prebid_exports, {
|
|
200
|
+
createPrebidManager: function() {
|
|
201
|
+
return createPrebidManager;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
module.exports = __toCommonJS(prebid_exports);
|
|
205
|
+
var DEFAULT_TIMEOUT_MS = 3e3;
|
|
206
|
+
var AUCTION_PATH = "/openrtb2/auction";
|
|
207
|
+
function createPrebidManager(config) {
|
|
208
|
+
var initialized = false;
|
|
209
|
+
var serverUrl = "";
|
|
210
|
+
var _config_debug;
|
|
211
|
+
var debug = (_config_debug = config.debug) !== null && _config_debug !== void 0 ? _config_debug : false;
|
|
212
|
+
function log() {
|
|
213
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
214
|
+
args[_key] = arguments[_key];
|
|
215
|
+
}
|
|
216
|
+
if (debug) {
|
|
217
|
+
var _console;
|
|
218
|
+
(_console = console).log.apply(_console, [
|
|
219
|
+
"[Prebid]"
|
|
220
|
+
].concat(_to_consumable_array(args)));
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function warn() {
|
|
224
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
225
|
+
args[_key] = arguments[_key];
|
|
226
|
+
}
|
|
227
|
+
var _console;
|
|
228
|
+
(_console = console).warn.apply(_console, [
|
|
229
|
+
"[Prebid]"
|
|
230
|
+
].concat(_to_consumable_array(args)));
|
|
231
|
+
}
|
|
232
|
+
function resolveServerUrl() {
|
|
233
|
+
var _config_ortbRequest_ext_prebid_server, _config_ortbRequest_ext_prebid, _config_ortbRequest_ext, _config_ortbRequest;
|
|
234
|
+
if (config.serverUrl) {
|
|
235
|
+
return config.serverUrl.replace(/\/$/, "");
|
|
236
|
+
}
|
|
237
|
+
var extUrl = (_config_ortbRequest = config.ortbRequest) === null || _config_ortbRequest === void 0 ? void 0 : (_config_ortbRequest_ext = _config_ortbRequest.ext) === null || _config_ortbRequest_ext === void 0 ? void 0 : (_config_ortbRequest_ext_prebid = _config_ortbRequest_ext.prebid) === null || _config_ortbRequest_ext_prebid === void 0 ? void 0 : (_config_ortbRequest_ext_prebid_server = _config_ortbRequest_ext_prebid.server) === null || _config_ortbRequest_ext_prebid_server === void 0 ? void 0 : _config_ortbRequest_ext_prebid_server.externalurl;
|
|
238
|
+
if (typeof extUrl === "string" && extUrl.length > 0) {
|
|
239
|
+
return extUrl.replace(/\/$/, "");
|
|
240
|
+
}
|
|
241
|
+
throw new Error("Prebid Server URL not configured. Provide serverUrl in PrebidConfig or ext.prebid.server.externalurl in the OpenRTB request.");
|
|
242
|
+
}
|
|
243
|
+
function buildRequest() {
|
|
244
|
+
var req = JSON.parse(JSON.stringify(config.ortbRequest));
|
|
245
|
+
req.id = "".concat(req.id || "prebid", "-").concat(Date.now(), "-").concat(Math.random().toString(36).slice(2, 8));
|
|
246
|
+
if (!req.ext) req.ext = {};
|
|
247
|
+
if (!req.ext.prebid) req.ext.prebid = {};
|
|
248
|
+
if (!req.ext.prebid.cache) req.ext.prebid.cache = {};
|
|
249
|
+
if (!req.ext.prebid.cache.vastxml) req.ext.prebid.cache.vastxml = {};
|
|
250
|
+
if (!req.ext.prebid.targeting) {
|
|
251
|
+
req.ext.prebid.targeting = {
|
|
252
|
+
includewinners: true,
|
|
253
|
+
includebidderkeys: false
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (!req.device) req.device = {};
|
|
257
|
+
if (typeof navigator !== "undefined") {
|
|
258
|
+
if (!req.device.ua) req.device.ua = navigator.userAgent;
|
|
259
|
+
if (!req.device.language) {
|
|
260
|
+
req.device.language = (navigator.language || "").split("-")[0] || "en";
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (typeof window !== "undefined") {
|
|
264
|
+
var _window_screen, _window_screen1;
|
|
265
|
+
if (!req.device.w) req.device.w = ((_window_screen = window.screen) === null || _window_screen === void 0 ? void 0 : _window_screen.width) || window.innerWidth;
|
|
266
|
+
if (!req.device.h) req.device.h = ((_window_screen1 = window.screen) === null || _window_screen1 === void 0 ? void 0 : _window_screen1.height) || window.innerHeight;
|
|
267
|
+
}
|
|
268
|
+
if (!req.site) req.site = {};
|
|
269
|
+
if (typeof window !== "undefined") {
|
|
270
|
+
if (!req.site.page) req.site.page = window.location.href;
|
|
271
|
+
if (!req.site.domain) req.site.domain = window.location.hostname;
|
|
272
|
+
}
|
|
273
|
+
return req;
|
|
274
|
+
}
|
|
275
|
+
function parseResponse(data) {
|
|
276
|
+
var bids = [];
|
|
277
|
+
var seatbids = (data === null || data === void 0 ? void 0 : data.seatbid) || [];
|
|
278
|
+
var currency = (data === null || data === void 0 ? void 0 : data.cur) || "USD";
|
|
279
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
280
|
+
try {
|
|
281
|
+
for(var _iterator = seatbids[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
282
|
+
var seatbid = _step.value;
|
|
283
|
+
var seat = seatbid.seat || "unknown";
|
|
284
|
+
var bidArray = seatbid.bid || [];
|
|
285
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
286
|
+
try {
|
|
287
|
+
for(var _iterator1 = bidArray[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
288
|
+
var bid = _step1.value;
|
|
289
|
+
var _bid_ext_prebid_cache_vastXml, _bid_ext_prebid_cache, _bid_ext_prebid, _bid_ext;
|
|
290
|
+
var cacheUrl = (_bid_ext = bid.ext) === null || _bid_ext === void 0 ? void 0 : (_bid_ext_prebid = _bid_ext.prebid) === null || _bid_ext_prebid === void 0 ? void 0 : (_bid_ext_prebid_cache = _bid_ext_prebid.cache) === null || _bid_ext_prebid_cache === void 0 ? void 0 : (_bid_ext_prebid_cache_vastXml = _bid_ext_prebid_cache.vastXml) === null || _bid_ext_prebid_cache_vastXml === void 0 ? void 0 : _bid_ext_prebid_cache_vastXml.url;
|
|
291
|
+
var vastXml = bid.adm || void 0;
|
|
292
|
+
var bidResponse = {
|
|
293
|
+
bidder: seat,
|
|
294
|
+
cpm: bid.price || 0,
|
|
295
|
+
width: bid.w || 0,
|
|
296
|
+
height: bid.h || 0,
|
|
297
|
+
adId: bid.id || "",
|
|
298
|
+
impId: bid.impid || "",
|
|
299
|
+
creativeId: bid.crid || "",
|
|
300
|
+
currency: currency
|
|
301
|
+
};
|
|
302
|
+
if (cacheUrl) bidResponse.vastUrl = cacheUrl;
|
|
303
|
+
if (vastXml) bidResponse.vastXml = vastXml;
|
|
304
|
+
if (bid.adomain) bidResponse.adomain = bid.adomain;
|
|
305
|
+
bids.push(bidResponse);
|
|
306
|
+
}
|
|
307
|
+
} catch (err) {
|
|
308
|
+
_didIteratorError1 = true;
|
|
309
|
+
_iteratorError1 = err;
|
|
310
|
+
} finally{
|
|
311
|
+
try {
|
|
312
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
313
|
+
_iterator1.return();
|
|
314
|
+
}
|
|
315
|
+
} finally{
|
|
316
|
+
if (_didIteratorError1) {
|
|
317
|
+
throw _iteratorError1;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} catch (err) {
|
|
323
|
+
_didIteratorError = true;
|
|
324
|
+
_iteratorError = err;
|
|
325
|
+
} finally{
|
|
326
|
+
try {
|
|
327
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
328
|
+
_iterator.return();
|
|
329
|
+
}
|
|
330
|
+
} finally{
|
|
331
|
+
if (_didIteratorError) {
|
|
332
|
+
throw _iteratorError;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
bids.sort(function(a, b) {
|
|
337
|
+
return b.cpm - a.cpm;
|
|
338
|
+
});
|
|
339
|
+
return bids;
|
|
340
|
+
}
|
|
341
|
+
function extractVastUrl(bids) {
|
|
342
|
+
if (bids.length === 0) return null;
|
|
343
|
+
var winner = bids[0];
|
|
344
|
+
var _config_cpmFloor;
|
|
345
|
+
var cpmFloor = (_config_cpmFloor = config.cpmFloor) !== null && _config_cpmFloor !== void 0 ? _config_cpmFloor : 0;
|
|
346
|
+
if (cpmFloor > 0 && winner.cpm < cpmFloor) {
|
|
347
|
+
log("Winning bid $".concat(winner.cpm.toFixed(2), " below CPM floor $").concat(cpmFloor.toFixed(2), ", rejecting"));
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
if (winner.vastUrl) {
|
|
351
|
+
log("Using cached VAST URL from ".concat(winner.bidder, " ($").concat(winner.cpm.toFixed(2), " ").concat(winner.currency, ")"));
|
|
352
|
+
return winner.vastUrl;
|
|
353
|
+
}
|
|
354
|
+
if (winner.vastXml) {
|
|
355
|
+
log("Creating blob URL from VAST XML (".concat(winner.bidder, ", $").concat(winner.cpm.toFixed(2), ")"));
|
|
356
|
+
try {
|
|
357
|
+
var blob = new Blob([
|
|
358
|
+
winner.vastXml
|
|
359
|
+
], {
|
|
360
|
+
type: "text/xml"
|
|
361
|
+
});
|
|
362
|
+
return URL.createObjectURL(blob);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
warn("Failed to create blob URL from VAST XML:", error);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
function initialize() {
|
|
370
|
+
return _async_to_generator(function() {
|
|
371
|
+
var _config_ortbRequest;
|
|
372
|
+
return _ts_generator(this, function(_state) {
|
|
373
|
+
if (initialized) return [
|
|
374
|
+
2
|
|
375
|
+
];
|
|
376
|
+
serverUrl = resolveServerUrl();
|
|
377
|
+
if (!((_config_ortbRequest = config.ortbRequest) === null || _config_ortbRequest === void 0 ? void 0 : _config_ortbRequest.imp) || config.ortbRequest.imp.length === 0) {
|
|
378
|
+
throw new Error("No impressions (imp) defined in the OpenRTB request.");
|
|
379
|
+
}
|
|
380
|
+
initialized = true;
|
|
381
|
+
log("Initialized with server:", serverUrl);
|
|
382
|
+
log("Bidders:", config.ortbRequest.imp.map(function(imp) {
|
|
383
|
+
var _imp_ext_prebid, _imp_ext;
|
|
384
|
+
var bidders = (_imp_ext = imp.ext) === null || _imp_ext === void 0 ? void 0 : (_imp_ext_prebid = _imp_ext.prebid) === null || _imp_ext_prebid === void 0 ? void 0 : _imp_ext_prebid.bidder;
|
|
385
|
+
return bidders ? Object.keys(bidders).join(", ") : "none";
|
|
386
|
+
}).join("; "));
|
|
387
|
+
return [
|
|
388
|
+
2
|
|
389
|
+
];
|
|
390
|
+
});
|
|
391
|
+
})();
|
|
392
|
+
}
|
|
393
|
+
function requestBids() {
|
|
394
|
+
return _async_to_generator(function() {
|
|
395
|
+
var auctionUrl, request, _config_timeout, _ref, timeout, controller, timeoutId, _data_ext, _data_ext1, fetchOptions, response, body, data, bids, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, b, error;
|
|
396
|
+
return _ts_generator(this, function(_state) {
|
|
397
|
+
switch(_state.label){
|
|
398
|
+
case 0:
|
|
399
|
+
if (!initialized) {
|
|
400
|
+
throw new Error("Prebid not initialized. Call initialize() first.");
|
|
401
|
+
}
|
|
402
|
+
auctionUrl = "".concat(serverUrl).concat(AUCTION_PATH);
|
|
403
|
+
request = buildRequest();
|
|
404
|
+
timeout = (_ref = (_config_timeout = config.timeout) !== null && _config_timeout !== void 0 ? _config_timeout : config.ortbRequest.tmax) !== null && _ref !== void 0 ? _ref : DEFAULT_TIMEOUT_MS;
|
|
405
|
+
log("Sending auction request to:", auctionUrl);
|
|
406
|
+
controller = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
407
|
+
timeoutId = setTimeout(function() {
|
|
408
|
+
controller === null || controller === void 0 ? void 0 : controller.abort();
|
|
409
|
+
}, timeout + 2e3);
|
|
410
|
+
_state.label = 1;
|
|
411
|
+
case 1:
|
|
412
|
+
_state.trys.push([
|
|
413
|
+
1,
|
|
414
|
+
6,
|
|
415
|
+
,
|
|
416
|
+
7
|
|
417
|
+
]);
|
|
418
|
+
fetchOptions = {
|
|
419
|
+
method: "POST",
|
|
420
|
+
headers: {
|
|
421
|
+
"Content-Type": "application/json"
|
|
422
|
+
},
|
|
423
|
+
body: JSON.stringify(request)
|
|
424
|
+
};
|
|
425
|
+
if (controller) {
|
|
426
|
+
fetchOptions.signal = controller.signal;
|
|
427
|
+
}
|
|
428
|
+
return [
|
|
429
|
+
4,
|
|
430
|
+
fetch(auctionUrl, fetchOptions)
|
|
431
|
+
];
|
|
432
|
+
case 2:
|
|
433
|
+
response = _state.sent();
|
|
434
|
+
clearTimeout(timeoutId);
|
|
435
|
+
if (!!response.ok) return [
|
|
436
|
+
3,
|
|
437
|
+
4
|
|
438
|
+
];
|
|
439
|
+
return [
|
|
440
|
+
4,
|
|
441
|
+
response.text().catch(function() {
|
|
442
|
+
return "";
|
|
443
|
+
})
|
|
444
|
+
];
|
|
445
|
+
case 3:
|
|
446
|
+
body = _state.sent();
|
|
447
|
+
throw new Error("Prebid Server returned HTTP ".concat(response.status, ": ").concat(body.slice(0, 200)));
|
|
448
|
+
case 4:
|
|
449
|
+
return [
|
|
450
|
+
4,
|
|
451
|
+
response.json()
|
|
452
|
+
];
|
|
453
|
+
case 5:
|
|
454
|
+
data = _state.sent();
|
|
455
|
+
if (debug && (data === null || data === void 0 ? void 0 : (_data_ext = data.ext) === null || _data_ext === void 0 ? void 0 : _data_ext.responsetimemillis)) {
|
|
456
|
+
log("Bidder response times:", data.ext.responsetimemillis);
|
|
457
|
+
}
|
|
458
|
+
if (debug && (data === null || data === void 0 ? void 0 : (_data_ext1 = data.ext) === null || _data_ext1 === void 0 ? void 0 : _data_ext1.errors)) {
|
|
459
|
+
warn("Auction errors:", data.ext.errors);
|
|
460
|
+
}
|
|
461
|
+
bids = parseResponse(data);
|
|
462
|
+
log("Received ".concat(bids.length, " bid(s)"));
|
|
463
|
+
if (debug) {
|
|
464
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
465
|
+
try {
|
|
466
|
+
for(_iterator = bids[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
467
|
+
b = _step.value;
|
|
468
|
+
log(" ".concat(b.bidder, ": $").concat(b.cpm.toFixed(2), " ").concat(b.currency, " ").concat(b.width, "x").concat(b.height) + (b.vastUrl ? " [cached VAST]" : "") + (b.vastXml && !b.vastUrl ? " [VAST XML]" : ""));
|
|
469
|
+
}
|
|
470
|
+
} catch (err) {
|
|
471
|
+
_didIteratorError = true;
|
|
472
|
+
_iteratorError = err;
|
|
473
|
+
} finally{
|
|
474
|
+
try {
|
|
475
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
476
|
+
_iterator.return();
|
|
477
|
+
}
|
|
478
|
+
} finally{
|
|
479
|
+
if (_didIteratorError) {
|
|
480
|
+
throw _iteratorError;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return [
|
|
486
|
+
2,
|
|
487
|
+
bids
|
|
488
|
+
];
|
|
489
|
+
case 6:
|
|
490
|
+
error = _state.sent();
|
|
491
|
+
clearTimeout(timeoutId);
|
|
492
|
+
if ((error === null || error === void 0 ? void 0 : error.name) === "AbortError") {
|
|
493
|
+
warn("Auction request timed out after ".concat(timeout + 2e3, "ms"));
|
|
494
|
+
return [
|
|
495
|
+
2,
|
|
496
|
+
[]
|
|
497
|
+
];
|
|
498
|
+
}
|
|
499
|
+
throw error;
|
|
500
|
+
case 7:
|
|
501
|
+
return [
|
|
502
|
+
2
|
|
503
|
+
];
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
})();
|
|
507
|
+
}
|
|
508
|
+
function getVastUrl() {
|
|
509
|
+
return _async_to_generator(function() {
|
|
510
|
+
var bids, error;
|
|
511
|
+
return _ts_generator(this, function(_state) {
|
|
512
|
+
switch(_state.label){
|
|
513
|
+
case 0:
|
|
514
|
+
_state.trys.push([
|
|
515
|
+
0,
|
|
516
|
+
2,
|
|
517
|
+
,
|
|
518
|
+
3
|
|
519
|
+
]);
|
|
520
|
+
return [
|
|
521
|
+
4,
|
|
522
|
+
requestBids()
|
|
523
|
+
];
|
|
524
|
+
case 1:
|
|
525
|
+
bids = _state.sent();
|
|
526
|
+
return [
|
|
527
|
+
2,
|
|
528
|
+
extractVastUrl(bids)
|
|
529
|
+
];
|
|
530
|
+
case 2:
|
|
531
|
+
error = _state.sent();
|
|
532
|
+
warn("Failed to get VAST URL:", error);
|
|
533
|
+
return [
|
|
534
|
+
2,
|
|
535
|
+
null
|
|
536
|
+
];
|
|
537
|
+
case 3:
|
|
538
|
+
return [
|
|
539
|
+
2
|
|
540
|
+
];
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
})();
|
|
544
|
+
}
|
|
545
|
+
function destroy() {
|
|
546
|
+
initialized = false;
|
|
547
|
+
serverUrl = "";
|
|
548
|
+
log("Destroyed");
|
|
549
|
+
}
|
|
550
|
+
return {
|
|
551
|
+
initialize: initialize,
|
|
552
|
+
requestBids: requestBids,
|
|
553
|
+
getVastUrl: getVastUrl,
|
|
554
|
+
destroy: destroy,
|
|
555
|
+
get isInitialized () {
|
|
556
|
+
return initialized;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
561
|
+
0 && (module.exports = {
|
|
562
|
+
createPrebidManager: createPrebidManager
|
|
563
|
+
});
|
|
564
|
+
//# sourceMappingURL=prebid.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/ubuntu24/Dev/stormcloud-vp/lib/sdk/prebid.cjs","../../src/sdk/prebid.ts"],"names":["__defProp","Object","defineProperty","__getOwnPropDesc","getOwnPropertyDescriptor","__getOwnPropNames","getOwnPropertyNames","__hasOwnProp","prototype","hasOwnProperty","name","all","target","get","__copyProps","from","key","to","enumerable","except","desc","call","__toCommonJS","mod","value","prebid_exports","createPrebidManager","module","exports","DEFAULT_TIMEOUT_MS","AUCTION_PATH","config","initialized","serverUrl","debug","log","args","console","warn","resolveServerUrl","replace","extUrl","ortbRequest","ext","prebid","server","externalurl","length"],"mappings":"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","sourcesContent":["\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\n\n// src/sdk/prebid.ts\nvar prebid_exports = {};\n__export(prebid_exports, {\n createPrebidManager: () => createPrebidManager\n});\nmodule.exports = __toCommonJS(prebid_exports);\nvar DEFAULT_TIMEOUT_MS = 3e3;\nvar AUCTION_PATH = \"/openrtb2/auction\";\nfunction createPrebidManager(config) {\n let initialized = false;\n let serverUrl = \"\";\n const debug = config.debug ?? false;\n function log(...args) {\n if (debug) {\n console.log(\"[Prebid]\", ...args);\n }\n }\n function warn(...args) {\n console.warn(\"[Prebid]\", ...args);\n }\n function resolveServerUrl() {\n if (config.serverUrl) {\n return config.serverUrl.replace(/\\/$/, \"\");\n }\n const extUrl = config.ortbRequest?.ext?.prebid?.server?.externalurl;\n if (typeof extUrl === \"string\" && extUrl.length > 0) {\n return extUrl.replace(/\\/$/, \"\");\n }\n throw new Error(\n \"Prebid Server URL not configured. Provide serverUrl in PrebidConfig or ext.prebid.server.externalurl in the OpenRTB request.\"\n );\n }\n function buildRequest() {\n const req = JSON.parse(JSON.stringify(config.ortbRequest));\n req.id = `${req.id || \"prebid\"}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;\n if (!req.ext) req.ext = {};\n if (!req.ext.prebid) req.ext.prebid = {};\n if (!req.ext.prebid.cache) req.ext.prebid.cache = {};\n if (!req.ext.prebid.cache.vastxml) req.ext.prebid.cache.vastxml = {};\n if (!req.ext.prebid.targeting) {\n req.ext.prebid.targeting = {\n includewinners: true,\n includebidderkeys: false\n };\n }\n if (!req.device) req.device = {};\n if (typeof navigator !== \"undefined\") {\n if (!req.device.ua) req.device.ua = navigator.userAgent;\n if (!req.device.language) {\n req.device.language = (navigator.language || \"\").split(\"-\")[0] || \"en\";\n }\n }\n if (typeof window !== \"undefined\") {\n if (!req.device.w) req.device.w = window.screen?.width || window.innerWidth;\n if (!req.device.h) req.device.h = window.screen?.height || window.innerHeight;\n }\n if (!req.site) req.site = {};\n if (typeof window !== \"undefined\") {\n if (!req.site.page) req.site.page = window.location.href;\n if (!req.site.domain) req.site.domain = window.location.hostname;\n }\n return req;\n }\n function parseResponse(data) {\n const bids = [];\n const seatbids = data?.seatbid || [];\n const currency = data?.cur || \"USD\";\n for (const seatbid of seatbids) {\n const seat = seatbid.seat || \"unknown\";\n const bidArray = seatbid.bid || [];\n for (const bid of bidArray) {\n const cacheUrl = bid.ext?.prebid?.cache?.vastXml?.url;\n const vastXml = bid.adm || void 0;\n const bidResponse = {\n bidder: seat,\n cpm: bid.price || 0,\n width: bid.w || 0,\n height: bid.h || 0,\n adId: bid.id || \"\",\n impId: bid.impid || \"\",\n creativeId: bid.crid || \"\",\n currency\n };\n if (cacheUrl) bidResponse.vastUrl = cacheUrl;\n if (vastXml) bidResponse.vastXml = vastXml;\n if (bid.adomain) bidResponse.adomain = bid.adomain;\n bids.push(bidResponse);\n }\n }\n bids.sort((a, b) => b.cpm - a.cpm);\n return bids;\n }\n function extractVastUrl(bids) {\n if (bids.length === 0) return null;\n const winner = bids[0];\n const cpmFloor = config.cpmFloor ?? 0;\n if (cpmFloor > 0 && winner.cpm < cpmFloor) {\n log(\n `Winning bid $${winner.cpm.toFixed(2)} below CPM floor $${cpmFloor.toFixed(2)}, rejecting`\n );\n return null;\n }\n if (winner.vastUrl) {\n log(\n `Using cached VAST URL from ${winner.bidder} ($${winner.cpm.toFixed(2)} ${winner.currency})`\n );\n return winner.vastUrl;\n }\n if (winner.vastXml) {\n log(\n `Creating blob URL from VAST XML (${winner.bidder}, $${winner.cpm.toFixed(2)})`\n );\n try {\n const blob = new Blob([winner.vastXml], { type: \"text/xml\" });\n return URL.createObjectURL(blob);\n } catch (error) {\n warn(\"Failed to create blob URL from VAST XML:\", error);\n }\n }\n return null;\n }\n async function initialize() {\n if (initialized) return;\n serverUrl = resolveServerUrl();\n if (!config.ortbRequest?.imp || config.ortbRequest.imp.length === 0) {\n throw new Error(\"No impressions (imp) defined in the OpenRTB request.\");\n }\n initialized = true;\n log(\"Initialized with server:\", serverUrl);\n log(\n \"Bidders:\",\n config.ortbRequest.imp.map((imp) => {\n const bidders = imp.ext?.prebid?.bidder;\n return bidders ? Object.keys(bidders).join(\", \") : \"none\";\n }).join(\"; \")\n );\n }\n async function requestBids() {\n if (!initialized) {\n throw new Error(\"Prebid not initialized. Call initialize() first.\");\n }\n const auctionUrl = `${serverUrl}${AUCTION_PATH}`;\n const request = buildRequest();\n const timeout = config.timeout ?? config.ortbRequest.tmax ?? DEFAULT_TIMEOUT_MS;\n log(\"Sending auction request to:\", auctionUrl);\n const controller = typeof AbortController !== \"undefined\" ? new AbortController() : null;\n const timeoutId = setTimeout(() => {\n controller?.abort();\n }, timeout + 2e3);\n try {\n const fetchOptions = {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(request)\n };\n if (controller) {\n fetchOptions.signal = controller.signal;\n }\n const response = await fetch(auctionUrl, fetchOptions);\n clearTimeout(timeoutId);\n if (!response.ok) {\n const body = await response.text().catch(() => \"\");\n throw new Error(\n `Prebid Server returned HTTP ${response.status}: ${body.slice(0, 200)}`\n );\n }\n const data = await response.json();\n if (debug && data?.ext?.responsetimemillis) {\n log(\"Bidder response times:\", data.ext.responsetimemillis);\n }\n if (debug && data?.ext?.errors) {\n warn(\"Auction errors:\", data.ext.errors);\n }\n const bids = parseResponse(data);\n log(`Received ${bids.length} bid(s)`);\n if (debug) {\n for (const b of bids) {\n log(\n ` ${b.bidder}: $${b.cpm.toFixed(2)} ${b.currency} ${b.width}x${b.height}` + (b.vastUrl ? \" [cached VAST]\" : \"\") + (b.vastXml && !b.vastUrl ? \" [VAST XML]\" : \"\")\n );\n }\n }\n return bids;\n } catch (error) {\n clearTimeout(timeoutId);\n if (error?.name === \"AbortError\") {\n warn(`Auction request timed out after ${timeout + 2e3}ms`);\n return [];\n }\n throw error;\n }\n }\n async function getVastUrl() {\n try {\n const bids = await requestBids();\n return extractVastUrl(bids);\n } catch (error) {\n warn(\"Failed to get VAST URL:\", error);\n return null;\n }\n }\n function destroy() {\n initialized = false;\n serverUrl = \"\";\n log(\"Destroyed\");\n }\n return {\n initialize,\n requestBids,\n getVastUrl,\n destroy,\n get isInitialized() {\n return initialized;\n }\n };\n}\n// Annotate the CommonJS export names for ESM import in node:\n0 && (module.exports = {\n createPrebidManager\n});\n","import type {\n PrebidConfig,\n PrebidBidResponse,\n PrebidManager,\n} from \"../types\";\n\nconst DEFAULT_TIMEOUT_MS = 3000;\nconst AUCTION_PATH = \"/openrtb2/auction\";\n\nexport function createPrebidManager(config: PrebidConfig): PrebidManager {\n let initialized = false;\n let serverUrl = \"\";\n const debug = config.debug ?? false;\n\n function log(...args: any[]): void {\n if (debug) {\n console.log(\"[Prebid]\", ...args);\n }\n }\n\n function warn(...args: any[]): void {\n console.warn(\"[Prebid]\", ...args);\n }\n\n function resolveServerUrl(): string {\n if (config.serverUrl) {\n return config.serverUrl.replace(/\\/$/, \"\");\n }\n\n const extUrl =\n config.ortbRequest?.ext?.prebid?.server?.externalurl;\n if (typeof extUrl === \"string\" && extUrl.length > 0) {\n return extUrl.replace(/\\/$/, \"\");\n }\n\n throw new Error(\n \"Prebid Server URL not configured. Provide serverUrl in PrebidConfig or ext.prebid.server.externalurl in the OpenRTB request.\"\n );\n }\n\n function buildRequest(): Record<string, any> {\n const req = JSON.parse(JSON.stringify(config.ortbRequest));\n\n req.id =\n `${req.id || \"prebid\"}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;\n\n if (!req.ext) req.ext = {};\n if (!req.ext.prebid) req.ext.prebid = {};\n if (!req.ext.prebid.cache) req.ext.prebid.cache = {};\n if (!req.ext.prebid.cache.vastxml) req.ext.prebid.cache.vastxml = {};\n if (!req.ext.prebid.targeting) {\n req.ext.prebid.targeting = {\n includewinners: true,\n includebidderkeys: false,\n };\n }\n\n if (!req.device) req.device = {};\n if (typeof navigator !== \"undefined\") {\n if (!req.device.ua) req.device.ua = navigator.userAgent;\n if (!req.device.language) {\n req.device.language =\n (navigator.language || \"\").split(\"-\")[0] || \"en\";\n }\n }\n if (typeof window !== \"undefined\") {\n if (!req.device.w) req.device.w = window.screen?.width || window.innerWidth;\n if (!req.device.h) req.device.h = window.screen?.height || window.innerHeight;\n }\n\n if (!req.site) req.site = {};\n if (typeof window !== \"undefined\") {\n if (!req.site.page) req.site.page = window.location.href;\n if (!req.site.domain) req.site.domain = window.location.hostname;\n }\n\n return req;\n }\n\n function parseResponse(data: any): PrebidBidResponse[] {\n const bids: PrebidBidResponse[] = [];\n const seatbids: any[] = data?.seatbid || [];\n const currency: string = data?.cur || \"USD\";\n\n for (const seatbid of seatbids) {\n const seat: string = seatbid.seat || \"unknown\";\n const bidArray: any[] = seatbid.bid || [];\n\n for (const bid of bidArray) {\n const cacheUrl: string | undefined =\n bid.ext?.prebid?.cache?.vastXml?.url;\n const vastXml: string | undefined = bid.adm || undefined;\n\n const bidResponse: PrebidBidResponse = {\n bidder: seat,\n cpm: bid.price || 0,\n width: bid.w || 0,\n height: bid.h || 0,\n adId: bid.id || \"\",\n impId: bid.impid || \"\",\n creativeId: bid.crid || \"\",\n currency,\n };\n if (cacheUrl) bidResponse.vastUrl = cacheUrl;\n if (vastXml) bidResponse.vastXml = vastXml;\n if (bid.adomain) bidResponse.adomain = bid.adomain;\n\n bids.push(bidResponse);\n }\n }\n\n bids.sort((a, b) => b.cpm - a.cpm);\n return bids;\n }\n\n function extractVastUrl(bids: PrebidBidResponse[]): string | null {\n if (bids.length === 0) return null;\n\n const winner = bids[0]!;\n\n const cpmFloor = config.cpmFloor ?? 0;\n if (cpmFloor > 0 && winner.cpm < cpmFloor) {\n log(\n `Winning bid $${winner.cpm.toFixed(2)} below CPM floor $${cpmFloor.toFixed(2)}, rejecting`\n );\n return null;\n }\n\n if (winner.vastUrl) {\n log(\n `Using cached VAST URL from ${winner.bidder} ($${winner.cpm.toFixed(2)} ${winner.currency})`\n );\n return winner.vastUrl;\n }\n\n if (winner.vastXml) {\n log(\n `Creating blob URL from VAST XML (${winner.bidder}, $${winner.cpm.toFixed(2)})`\n );\n try {\n const blob = new Blob([winner.vastXml], { type: \"text/xml\" });\n return URL.createObjectURL(blob);\n } catch (error) {\n warn(\"Failed to create blob URL from VAST XML:\", error);\n }\n }\n\n return null;\n }\n\n async function initialize(): Promise<void> {\n if (initialized) return;\n\n serverUrl = resolveServerUrl();\n\n if (\n !config.ortbRequest?.imp ||\n config.ortbRequest.imp.length === 0\n ) {\n throw new Error(\"No impressions (imp) defined in the OpenRTB request.\");\n }\n\n initialized = true;\n log(\"Initialized with server:\", serverUrl);\n log(\n \"Bidders:\",\n config.ortbRequest.imp\n .map((imp) => {\n const bidders = imp.ext?.prebid?.bidder;\n return bidders ? Object.keys(bidders).join(\", \") : \"none\";\n })\n .join(\"; \")\n );\n }\n\n async function requestBids(): Promise<PrebidBidResponse[]> {\n if (!initialized) {\n throw new Error(\"Prebid not initialized. Call initialize() first.\");\n }\n\n const auctionUrl = `${serverUrl}${AUCTION_PATH}`;\n const request = buildRequest();\n const timeout = config.timeout ?? config.ortbRequest.tmax ?? DEFAULT_TIMEOUT_MS;\n\n log(\"Sending auction request to:\", auctionUrl);\n\n const controller =\n typeof AbortController !== \"undefined\"\n ? new AbortController()\n : null;\n const timeoutId = setTimeout(() => {\n controller?.abort();\n }, timeout + 2000);\n\n try {\n const fetchOptions: RequestInit = {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(request),\n };\n if (controller) {\n fetchOptions.signal = controller.signal;\n }\n\n const response = await fetch(auctionUrl, fetchOptions);\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const body = await response.text().catch(() => \"\");\n throw new Error(\n `Prebid Server returned HTTP ${response.status}: ${body.slice(0, 200)}`\n );\n }\n\n const data = await response.json();\n\n if (debug && data?.ext?.responsetimemillis) {\n log(\"Bidder response times:\", data.ext.responsetimemillis);\n }\n if (debug && data?.ext?.errors) {\n warn(\"Auction errors:\", data.ext.errors);\n }\n\n const bids = parseResponse(data);\n log(`Received ${bids.length} bid(s)`);\n\n if (debug) {\n for (const b of bids) {\n log(\n ` ${b.bidder}: $${b.cpm.toFixed(2)} ${b.currency}` +\n ` ${b.width}x${b.height}` +\n (b.vastUrl ? \" [cached VAST]\" : \"\") +\n (b.vastXml && !b.vastUrl ? \" [VAST XML]\" : \"\")\n );\n }\n }\n\n return bids;\n } catch (error: any) {\n clearTimeout(timeoutId);\n\n if (error?.name === \"AbortError\") {\n warn(`Auction request timed out after ${timeout + 2000}ms`);\n return [];\n }\n\n throw error;\n }\n }\n\n async function getVastUrl(): Promise<string | null> {\n try {\n const bids = await requestBids();\n return extractVastUrl(bids);\n } catch (error) {\n warn(\"Failed to get VAST URL:\", error);\n return null;\n }\n }\n\n function destroy(): void {\n initialized = false;\n serverUrl = \"\";\n log(\"Destroyed\");\n }\n\n return {\n initialize,\n requestBids,\n getVastUrl,\n destroy,\n get isInitialized() {\n return initialized;\n },\n };\n}\n"]}
|