@blotoutio/edgetag-sdk-browser 0.7.3 → 0.8.1

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 (2) hide show
  1. package/index.js +232 -197
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -299,29 +299,6 @@
299
299
  const keyPrefix = `_worker`;
300
300
 
301
301
  const initKey = `${keyPrefix}Store`;
302
- const getCookieValue = (key) => {
303
- try {
304
- if (!document || !document.cookie) {
305
- return '';
306
- }
307
- const name = `${key}=`;
308
- const decodedCookie = decodeURIComponent(document.cookie);
309
- const ca = decodedCookie.split(';');
310
- for (let i = 0; i < ca.length; i++) {
311
- let c = ca[i];
312
- while (c.charAt(0) === ' ') {
313
- c = c.substring(1);
314
- }
315
- if (c.indexOf(name) === 0) {
316
- return c.substring(name.length, c.length);
317
- }
318
- }
319
- return '';
320
- }
321
- catch (_a) {
322
- return '';
323
- }
324
- };
325
302
  const saveDataPerKey = (persistType, provider, value, key) => {
326
303
  const storage = getData$1(persistType);
327
304
  if (!storage['data']) {
@@ -425,157 +402,6 @@
425
402
  }
426
403
  };
427
404
 
428
- let initUserId = '';
429
- const handleGetUserId = () => {
430
- if (initUserId) {
431
- return initUserId;
432
- }
433
- return getCookieValue('tag_user_id');
434
- };
435
- const setUserId = (userId) => {
436
- initUserId = userId;
437
- };
438
-
439
- const getHeaders = () => ({
440
- 'Content-type': 'application/json; charset=utf-8',
441
- Accept: 'application/json; charset=utf-8',
442
- EdgeTagUserId: handleGetUserId(),
443
- });
444
- const beacon = (url, payload) => {
445
- try {
446
- let blob;
447
- if (payload) {
448
- blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
449
- }
450
- return navigator.sendBeacon(url, blob);
451
- }
452
- catch (e) {
453
- return Promise.reject(new Error('Beacon not supported.'));
454
- }
455
- };
456
- const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
457
- const https = yield import('https');
458
- const options = {
459
- method,
460
- headers: getHeaders(),
461
- };
462
- return new Promise((resolve, reject) => {
463
- const req = https.request(url, options, (res) => {
464
- res.on('data', (data) => {
465
- try {
466
- data = JSON.parse(data);
467
- }
468
- catch (_a) {
469
- // do nothing
470
- }
471
- resolve({
472
- body: data,
473
- status: res.statusCode || 500,
474
- });
475
- });
476
- });
477
- req.on('error', (e) => {
478
- reject(new Error(e.message));
479
- });
480
- if (payload && method !== 'GET') {
481
- req.write(JSON.stringify(payload));
482
- }
483
- req.end();
484
- });
485
- });
486
- const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
487
- if (typeof fetch === 'undefined') {
488
- return yield fallbackAjax(method, url, payload)
489
- .then((data) => {
490
- if (data.status < 200 || data.status >= 300) {
491
- return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
492
- }
493
- return Promise.resolve(data.body);
494
- })
495
- .catch((error) => {
496
- return Promise.reject(new Error(error));
497
- });
498
- }
499
- return yield fetch(url, {
500
- method,
501
- headers: getHeaders(),
502
- body: JSON.stringify(payload),
503
- credentials: 'include',
504
- })
505
- .then((response) => response.json().then((data) => ({ status: response.status, body: data })))
506
- .then((data) => {
507
- if (data.status < 200 || data.status >= 300) {
508
- return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
509
- }
510
- return Promise.resolve(data.body);
511
- })
512
- .catch((error) => {
513
- return Promise.reject(new Error(error));
514
- });
515
- });
516
- const getStandardPayload = (payload) => {
517
- const data = Object.assign({ pageUrl: getPageUrl(), pageTitle: getPageTitle(), userAgent: getUserAgent(), referrer: getReferrer(), search: getSearch() }, (payload || {}));
518
- let storage = {};
519
- const session = getData$1('session');
520
- if (session) {
521
- storage = Object.assign(Object.assign({}, storage), session);
522
- }
523
- const local = getData$1('local');
524
- if (local) {
525
- storage = Object.assign(Object.assign({}, storage), local);
526
- }
527
- data.storage = storage;
528
- return data;
529
- };
530
- function postRequest(url, data, options) {
531
- return __awaiter(this, void 0, void 0, function* () {
532
- if (!url) {
533
- return Promise.reject(new Error('URL is empty.'));
534
- }
535
- const payload = getStandardPayload(data);
536
- if (options && options.method === 'beacon') {
537
- return Promise.resolve(beacon(url, payload));
538
- }
539
- return yield ajax('POST', url, payload);
540
- });
541
- }
542
- function getRequest(url, options) {
543
- return __awaiter(this, void 0, void 0, function* () {
544
- if (!url) {
545
- return Promise.reject(new Error('URL is empty.'));
546
- }
547
- if (options && options.method === 'beacon') {
548
- return {
549
- result: Promise.resolve(beacon(url)),
550
- };
551
- }
552
- return yield ajax('GET', url);
553
- });
554
- }
555
-
556
- let memoryConsent;
557
- const saveConsent = (consent) => {
558
- setConsent(consent);
559
- savePerKey('local', tagStorage, consent, consentKey);
560
- };
561
- const handleConsent = (consent) => {
562
- const payload = {
563
- consentString: consent,
564
- };
565
- saveConsent(consent);
566
- postRequest(getConsentURL(), payload).catch(error);
567
- };
568
- const setConsent = (newConsent) => {
569
- memoryConsent = newConsent;
570
- };
571
- const getConsent = () => {
572
- const storageConsent = getDataPerKey('local', tagStorage, consentKey);
573
- if (storageConsent) {
574
- return storageConsent;
575
- }
576
- return memoryConsent;
577
- };
578
-
579
405
  // Unique ID creation requires a high quality random # generator. In the browser we therefore
580
406
  // require the crypto API and do not support built-in fallback to lower quality random number
581
407
  // generators (like Math.random()).
@@ -1074,6 +900,181 @@
1074
900
  return `${encodeString(name)}-${v4()}-${time}`;
1075
901
  };
1076
902
 
903
+ const getCookieValue = (key) => {
904
+ try {
905
+ if (!document || !document.cookie) {
906
+ return '';
907
+ }
908
+ const name = `${key}=`;
909
+ const decodedCookie = decodeURIComponent(document.cookie);
910
+ const ca = decodedCookie.split(';');
911
+ for (let i = 0; i < ca.length; i++) {
912
+ let c = ca[i];
913
+ while (c.charAt(0) === ' ') {
914
+ c = c.substring(1);
915
+ }
916
+ if (c.indexOf(name) === 0) {
917
+ return c.substring(name.length, c.length);
918
+ }
919
+ }
920
+ return '';
921
+ }
922
+ catch (_a) {
923
+ return '';
924
+ }
925
+ };
926
+
927
+ let initUserId = '';
928
+ const handleGetUserId = () => {
929
+ if (initUserId) {
930
+ return initUserId;
931
+ }
932
+ return getCookieValue('tag_user_id');
933
+ };
934
+ const setUserId = (userId) => {
935
+ initUserId = userId;
936
+ };
937
+
938
+ const getHeaders = () => ({
939
+ 'Content-type': 'application/json; charset=utf-8',
940
+ Accept: 'application/json; charset=utf-8',
941
+ EdgeTagUserId: handleGetUserId(),
942
+ });
943
+ const beacon = (url, payload) => {
944
+ try {
945
+ let blob;
946
+ if (payload) {
947
+ blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
948
+ }
949
+ return navigator.sendBeacon(url, blob);
950
+ }
951
+ catch (e) {
952
+ return Promise.reject(new Error('Beacon not supported.'));
953
+ }
954
+ };
955
+ const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
956
+ const https = yield import('https');
957
+ const options = {
958
+ method,
959
+ headers: getHeaders(),
960
+ };
961
+ return new Promise((resolve, reject) => {
962
+ const req = https.request(url, options, (res) => {
963
+ res.on('data', (data) => {
964
+ try {
965
+ data = JSON.parse(data);
966
+ }
967
+ catch (_a) {
968
+ // do nothing
969
+ }
970
+ resolve({
971
+ body: data,
972
+ status: res.statusCode || 500,
973
+ });
974
+ });
975
+ });
976
+ req.on('error', (e) => {
977
+ reject(new Error(e.message));
978
+ });
979
+ if (payload && method !== 'GET') {
980
+ req.write(JSON.stringify(payload));
981
+ }
982
+ req.end();
983
+ });
984
+ });
985
+ const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
986
+ if (typeof fetch === 'undefined') {
987
+ return yield fallbackAjax(method, url, payload)
988
+ .then((data) => {
989
+ if (data.status < 200 || data.status >= 300) {
990
+ return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
991
+ }
992
+ return Promise.resolve(data.body);
993
+ })
994
+ .catch((error) => {
995
+ return Promise.reject(new Error(error));
996
+ });
997
+ }
998
+ return yield fetch(url, {
999
+ method,
1000
+ headers: getHeaders(),
1001
+ body: JSON.stringify(payload),
1002
+ credentials: 'include',
1003
+ })
1004
+ .then((response) => response.json().then((data) => ({ status: response.status, body: data })))
1005
+ .then((data) => {
1006
+ if (data.status < 200 || data.status >= 300) {
1007
+ return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
1008
+ }
1009
+ return Promise.resolve(data.body);
1010
+ })
1011
+ .catch((error) => {
1012
+ return Promise.reject(new Error(error));
1013
+ });
1014
+ });
1015
+ const getStandardPayload = (payload) => {
1016
+ const data = Object.assign({ pageUrl: getPageUrl(), pageTitle: getPageTitle(), userAgent: getUserAgent(), referrer: getReferrer(), search: getSearch() }, (payload || {}));
1017
+ let storage = {};
1018
+ const session = getData$1('session');
1019
+ if (session) {
1020
+ storage = Object.assign(Object.assign({}, storage), session);
1021
+ }
1022
+ const local = getData$1('local');
1023
+ if (local) {
1024
+ storage = Object.assign(Object.assign({}, storage), local);
1025
+ }
1026
+ data.storage = storage;
1027
+ return data;
1028
+ };
1029
+ function postRequest(url, data, options) {
1030
+ return __awaiter(this, void 0, void 0, function* () {
1031
+ if (!url) {
1032
+ return Promise.reject(new Error('URL is empty.'));
1033
+ }
1034
+ const payload = getStandardPayload(data);
1035
+ if (options && options.method === 'beacon') {
1036
+ return Promise.resolve(beacon(url, payload));
1037
+ }
1038
+ return yield ajax('POST', url, payload);
1039
+ });
1040
+ }
1041
+ function getRequest(url, options) {
1042
+ return __awaiter(this, void 0, void 0, function* () {
1043
+ if (!url) {
1044
+ return Promise.reject(new Error('URL is empty.'));
1045
+ }
1046
+ if (options && options.method === 'beacon') {
1047
+ return {
1048
+ result: Promise.resolve(beacon(url)),
1049
+ };
1050
+ }
1051
+ return yield ajax('GET', url);
1052
+ });
1053
+ }
1054
+
1055
+ let memoryConsent;
1056
+ const saveConsent = (consent) => {
1057
+ setConsent(consent);
1058
+ savePerKey('local', tagStorage, consent, consentKey);
1059
+ };
1060
+ const handleConsent = (consent) => {
1061
+ const payload = {
1062
+ consentString: consent,
1063
+ };
1064
+ saveConsent(consent);
1065
+ postRequest(getConsentURL(), payload).catch(error);
1066
+ };
1067
+ const setConsent = (newConsent) => {
1068
+ memoryConsent = newConsent;
1069
+ };
1070
+ const getConsent = () => {
1071
+ const storageConsent = getDataPerKey('local', tagStorage, consentKey);
1072
+ if (storageConsent) {
1073
+ return storageConsent;
1074
+ }
1075
+ return memoryConsent;
1076
+ };
1077
+
1077
1078
  const manifestVariables = {};
1078
1079
  const addProviderVariable = (name, variables) => {
1079
1080
  manifestVariables[name] = variables;
@@ -1167,12 +1168,25 @@
1167
1168
  });
1168
1169
  };
1169
1170
 
1170
- const handleData = (data, options) => {
1171
+ const handleData = (data, providers, options) => {
1171
1172
  if (!data || Object.keys(data).length === 0) {
1172
1173
  error('Provide data for data API.');
1173
1174
  return;
1174
1175
  }
1175
1176
  saveKV(data);
1177
+ const providerPackages = getProvidersPackage();
1178
+ const userId = handleGetUserId();
1179
+ if (providerPackages) {
1180
+ Object.values(providerPackages).forEach((pkg) => {
1181
+ if (!pkg || !pkg.user || !allowProviderWithConsent(providers, pkg.name)) {
1182
+ return;
1183
+ }
1184
+ pkg.user({
1185
+ userId,
1186
+ data,
1187
+ });
1188
+ });
1189
+ }
1176
1190
  postRequest(getDataURL(), { data }, options).catch(error);
1177
1191
  };
1178
1192
 
@@ -1258,6 +1272,18 @@
1258
1272
  });
1259
1273
  };
1260
1274
 
1275
+ const handleGetData = (keys, callback) => {
1276
+ if (!keys || keys.length === 0) {
1277
+ error('Provide keys for get data API.');
1278
+ return;
1279
+ }
1280
+ getRequest(getGetDataURL(keys))
1281
+ .then((result) => {
1282
+ callback((result === null || result === void 0 ? void 0 : result.result) || {});
1283
+ })
1284
+ .catch(error);
1285
+ };
1286
+
1261
1287
  const handleManifest = (manifest) => {
1262
1288
  const providerPackages = getProvidersPackage();
1263
1289
  const userId = handleGetUserId();
@@ -1277,7 +1303,15 @@
1277
1303
  if (providerPackages) {
1278
1304
  const pkg = providerPackages[provider.package];
1279
1305
  if (pkg && pkg.init && allowProvider(pkg.name)) {
1280
- pkg.init({ userId, manifest: provider, sendTag });
1306
+ const initData = {
1307
+ userId,
1308
+ manifest: provider,
1309
+ sendTag,
1310
+ sendEdgeData: handleData,
1311
+ getEdgeData: handleGetData,
1312
+ keyName: `${keyPrefix}Store`,
1313
+ };
1314
+ pkg.init(initData);
1281
1315
  }
1282
1316
  }
1283
1317
  });
@@ -1313,7 +1347,7 @@
1313
1347
  .catch(error);
1314
1348
  };
1315
1349
 
1316
- const handleUser = (key, value, options) => {
1350
+ const handleUser = (key, value, providers, options) => {
1317
1351
  if (!key || !value) {
1318
1352
  error('Key or Value is missing in user API.');
1319
1353
  return;
@@ -1321,24 +1355,25 @@
1321
1355
  saveKV({
1322
1356
  [key]: value,
1323
1357
  });
1358
+ const providerPackages = getProvidersPackage();
1359
+ const userId = handleGetUserId();
1360
+ if (providerPackages) {
1361
+ Object.values(providerPackages).forEach((pkg) => {
1362
+ if (!pkg || !pkg.user || !allowProviderWithConsent(providers, pkg.name)) {
1363
+ return;
1364
+ }
1365
+ pkg.user({
1366
+ userId,
1367
+ data: { [key]: value },
1368
+ });
1369
+ });
1370
+ }
1324
1371
  postRequest(getUserURL(), {
1325
1372
  key,
1326
1373
  value,
1327
1374
  }, options).catch(error);
1328
1375
  };
1329
1376
 
1330
- const handleGetData = (keys, callback) => {
1331
- if (!keys || keys.length === 0) {
1332
- error('Provide keys for get data API.');
1333
- return;
1334
- }
1335
- getRequest(getGetDataURL(keys))
1336
- .then((result) => {
1337
- callback((result === null || result === void 0 ? void 0 : result.result) || {});
1338
- })
1339
- .catch(error);
1340
- };
1341
-
1342
1377
  const handleKeys = (callback) => {
1343
1378
  getRequest(getKeysURL())
1344
1379
  .then((result) => {
@@ -1356,11 +1391,11 @@
1356
1391
  const consent = (consent) => {
1357
1392
  handleConsent(consent);
1358
1393
  };
1359
- const user = (key, value, options) => {
1360
- handleUser(key, value, options);
1394
+ const user = (key, value, providers, options) => {
1395
+ handleUser(key, value, providers, options);
1361
1396
  };
1362
- const data = (data, options) => {
1363
- handleData(data, options);
1397
+ const data = (data, providers, options) => {
1398
+ handleData(data, providers, options);
1364
1399
  };
1365
1400
  const getData = (keys, callback) => {
1366
1401
  handleGetData(keys, callback);
@@ -1387,12 +1422,12 @@
1387
1422
  consent(consentString);
1388
1423
  }
1389
1424
 
1390
- user(key, value, options) {
1391
- user(key, value, options);
1425
+ user(key, value, providers, options) {
1426
+ user(key, value, providers, options);
1392
1427
  }
1393
1428
 
1394
- data(data$1, options) {
1395
- data(data$1, options);
1429
+ data(data$1, providers, options) {
1430
+ data(data$1, providers, options);
1396
1431
  }
1397
1432
 
1398
1433
  getData(keys, callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.7.3",
3
+ "version": "0.8.1",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",