@blotoutio/edgetag-sdk-js 0.7.0 → 0.7.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 (4) hide show
  1. package/README.md +1 -1
  2. package/index.cjs +129 -36
  3. package/index.esm.js +111 -36
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # EdgeTag SDK Core
2
2
 
3
- More info on https://app.edgetag.io/integration/js
3
+ More info on https://app.edgetag.io/docs/js
package/index.cjs CHANGED
@@ -4,6 +4,24 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var uuid = require('uuid');
6
6
 
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n["default"] = e;
22
+ return Object.freeze(n);
23
+ }
24
+
7
25
  var api = /*#__PURE__*/Object.freeze({
8
26
  __proto__: null,
9
27
  get init () { return init; },
@@ -128,6 +146,39 @@ const setAllowedProviders = (providers) => {
128
146
  allowedProvider = providers;
129
147
  };
130
148
 
149
+ const checkConsent = (providers) => {
150
+ if (isConsentDisabled()) {
151
+ return true;
152
+ }
153
+ const consent = getConsent();
154
+ if (!consent) {
155
+ return false;
156
+ }
157
+ const consentLength = Object.keys(consent).length;
158
+ if (!consentLength || (consentLength === 1 && consent['all'] === false)) {
159
+ return false;
160
+ }
161
+ const providersLength = Object.keys(providers || {}).length;
162
+ if (!providers ||
163
+ providersLength === 0 ||
164
+ (providersLength === 1 && providers['all'])) {
165
+ return (Object.values(consent).find((isAllowed) => isAllowed) || false);
166
+ }
167
+ for (const [key, value] of Object.entries(providers)) {
168
+ if (value === false || consent[key] === false) {
169
+ continue;
170
+ }
171
+ // we have consent
172
+ if (consent[key] ||
173
+ (consent['all'] === true && consent[key] === undefined)) {
174
+ // we have provider
175
+ if (value || (providers['all'] && providers[key] === undefined)) {
176
+ return true;
177
+ }
178
+ }
179
+ }
180
+ return false;
181
+ };
131
182
  const getUserAgent = () => {
132
183
  try {
133
184
  const nav = navigator;
@@ -143,23 +194,20 @@ const isProviderIncluded = (providers, providerValue) => {
143
194
  return (providerValue || (providers['all'] === true && providerValue === undefined));
144
195
  };
145
196
  const allowTag = (providers) => {
146
- const consent = getConsent();
147
- if (isConsentDisabled()) {
148
- return true;
149
- }
150
- if (!consent) {
197
+ if (!checkConsent(providers)) {
151
198
  return false;
152
199
  }
153
- if (consent['all']) {
200
+ const providersLength = Object.values(providers || {}).length;
201
+ if (!providers || providersLength === 0) {
154
202
  return true;
155
203
  }
156
- if (!providers) {
157
- return (Object.values(consent).find((isAllowed) => isAllowed) || false);
158
- }
159
204
  const allProviders = getAllowedProviders();
205
+ const allProvidersLength = Object.keys(allProviders || {}).length;
206
+ if (allProvidersLength === 0) {
207
+ return true;
208
+ }
160
209
  for (const provider of allProviders) {
161
- if (isProviderIncluded(providers, !!providers[provider]) &&
162
- consent[provider]) {
210
+ if (isProviderIncluded(providers, providers[provider])) {
163
211
  return true;
164
212
  }
165
213
  }
@@ -198,25 +246,15 @@ const allowProviderWithConsent = (providers, providerId) => {
198
246
  if (!allowProvider(providerId)) {
199
247
  return false;
200
248
  }
249
+ if (!checkConsent(providers)) {
250
+ return false;
251
+ }
201
252
  if (providers && Object.keys(providers).length) {
202
- const tagProvider = providers[providerId];
203
- if (!(tagProvider ||
204
- (providers['all'] === true && tagProvider === undefined) ||
205
- (providers['all'] === false && tagProvider === true))) {
253
+ if (!isProviderIncluded(providers, providers[providerId])) {
206
254
  return false;
207
255
  }
208
256
  }
209
- const consent = getConsent();
210
- if (isConsentDisabled()) {
211
- return true;
212
- }
213
- if (!consent) {
214
- return false;
215
- }
216
- if (consent['all']) {
217
- return true;
218
- }
219
- return consent[providerId];
257
+ return true;
220
258
  };
221
259
 
222
260
  const tagStorage = 'edgeTag';
@@ -361,21 +399,70 @@ const setUserId = (userId) => {
361
399
  initUserId = userId;
362
400
  };
363
401
 
402
+ const getHeaders = () => ({
403
+ 'Content-type': 'application/json; charset=utf-8',
404
+ Accept: 'application/json; charset=utf-8',
405
+ EdgeTagUserId: handleGetUserId(),
406
+ });
364
407
  const beacon = (url, payload) => {
365
- let blob;
366
- if (payload) {
367
- blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
408
+ try {
409
+ let blob;
410
+ if (payload) {
411
+ blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
412
+ }
413
+ return navigator.sendBeacon(url, blob);
414
+ }
415
+ catch (e) {
416
+ console.log('Beacon not supported.');
417
+ return Promise.reject(new Error('Beacon not supported'));
368
418
  }
369
- return navigator.sendBeacon(url, blob);
370
419
  };
420
+ const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
421
+ const https = yield Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('https')); });
422
+ const options = {
423
+ method,
424
+ headers: getHeaders(),
425
+ };
426
+ return new Promise((resolve, reject) => {
427
+ const req = https.request(url, options, (res) => {
428
+ res.on('data', (data) => {
429
+ try {
430
+ data = JSON.parse(data);
431
+ }
432
+ catch (_a) {
433
+ // do nothing
434
+ }
435
+ resolve({
436
+ body: data,
437
+ status: res.statusCode || 500,
438
+ });
439
+ });
440
+ });
441
+ req.on('error', (e) => {
442
+ reject(new Error(e.message));
443
+ });
444
+ if (payload && method !== 'GET') {
445
+ req.write(JSON.stringify(payload));
446
+ }
447
+ req.end();
448
+ });
449
+ });
371
450
  const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
451
+ if (typeof fetch === 'undefined') {
452
+ return yield fallbackAjax(method, url, payload)
453
+ .then((data) => {
454
+ if (data.status < 200 || data.status >= 300) {
455
+ return Promise.reject(new Error(JSON.stringify(data.body)));
456
+ }
457
+ return Promise.resolve(data.body);
458
+ })
459
+ .catch((error) => {
460
+ return Promise.reject(new Error(error));
461
+ });
462
+ }
372
463
  return yield fetch(url, {
373
464
  method,
374
- headers: {
375
- 'Content-type': 'application/json; charset=utf-8',
376
- Accept: 'application/json; charset=utf-8',
377
- EdgeTagUserId: handleGetUserId(),
378
- },
465
+ headers: getHeaders(),
379
466
  body: JSON.stringify(payload),
380
467
  credentials: 'include',
381
468
  })
@@ -465,6 +552,12 @@ const getConsent = () => {
465
552
  return memoryConsent;
466
553
  };
467
554
 
555
+ const encodeString = (name) => {
556
+ if (typeof btoa === 'undefined') {
557
+ return Buffer.from(name).toString('base64');
558
+ }
559
+ return btoa(name);
560
+ };
468
561
  const generateEventId = (name) => {
469
562
  let time = Date.now().toString();
470
563
  if (typeof performance !== 'undefined' &&
@@ -474,7 +567,7 @@ const generateEventId = (name) => {
474
567
  time = perf.toFixed(4);
475
568
  }
476
569
  }
477
- return `${btoa(name)}-${uuid.v4()}-${time}`;
570
+ return `${encodeString(name)}-${uuid.v4()}-${time}`;
478
571
  };
479
572
 
480
573
  const manifestVariables = {};
package/index.esm.js CHANGED
@@ -124,6 +124,39 @@ const setAllowedProviders = (providers) => {
124
124
  allowedProvider = providers;
125
125
  };
126
126
 
127
+ const checkConsent = (providers) => {
128
+ if (isConsentDisabled()) {
129
+ return true;
130
+ }
131
+ const consent = getConsent();
132
+ if (!consent) {
133
+ return false;
134
+ }
135
+ const consentLength = Object.keys(consent).length;
136
+ if (!consentLength || (consentLength === 1 && consent['all'] === false)) {
137
+ return false;
138
+ }
139
+ const providersLength = Object.keys(providers || {}).length;
140
+ if (!providers ||
141
+ providersLength === 0 ||
142
+ (providersLength === 1 && providers['all'])) {
143
+ return (Object.values(consent).find((isAllowed) => isAllowed) || false);
144
+ }
145
+ for (const [key, value] of Object.entries(providers)) {
146
+ if (value === false || consent[key] === false) {
147
+ continue;
148
+ }
149
+ // we have consent
150
+ if (consent[key] ||
151
+ (consent['all'] === true && consent[key] === undefined)) {
152
+ // we have provider
153
+ if (value || (providers['all'] && providers[key] === undefined)) {
154
+ return true;
155
+ }
156
+ }
157
+ }
158
+ return false;
159
+ };
127
160
  const getUserAgent = () => {
128
161
  try {
129
162
  const nav = navigator;
@@ -139,23 +172,20 @@ const isProviderIncluded = (providers, providerValue) => {
139
172
  return (providerValue || (providers['all'] === true && providerValue === undefined));
140
173
  };
141
174
  const allowTag = (providers) => {
142
- const consent = getConsent();
143
- if (isConsentDisabled()) {
144
- return true;
145
- }
146
- if (!consent) {
175
+ if (!checkConsent(providers)) {
147
176
  return false;
148
177
  }
149
- if (consent['all']) {
178
+ const providersLength = Object.values(providers || {}).length;
179
+ if (!providers || providersLength === 0) {
150
180
  return true;
151
181
  }
152
- if (!providers) {
153
- return (Object.values(consent).find((isAllowed) => isAllowed) || false);
154
- }
155
182
  const allProviders = getAllowedProviders();
183
+ const allProvidersLength = Object.keys(allProviders || {}).length;
184
+ if (allProvidersLength === 0) {
185
+ return true;
186
+ }
156
187
  for (const provider of allProviders) {
157
- if (isProviderIncluded(providers, !!providers[provider]) &&
158
- consent[provider]) {
188
+ if (isProviderIncluded(providers, providers[provider])) {
159
189
  return true;
160
190
  }
161
191
  }
@@ -194,25 +224,15 @@ const allowProviderWithConsent = (providers, providerId) => {
194
224
  if (!allowProvider(providerId)) {
195
225
  return false;
196
226
  }
227
+ if (!checkConsent(providers)) {
228
+ return false;
229
+ }
197
230
  if (providers && Object.keys(providers).length) {
198
- const tagProvider = providers[providerId];
199
- if (!(tagProvider ||
200
- (providers['all'] === true && tagProvider === undefined) ||
201
- (providers['all'] === false && tagProvider === true))) {
231
+ if (!isProviderIncluded(providers, providers[providerId])) {
202
232
  return false;
203
233
  }
204
234
  }
205
- const consent = getConsent();
206
- if (isConsentDisabled()) {
207
- return true;
208
- }
209
- if (!consent) {
210
- return false;
211
- }
212
- if (consent['all']) {
213
- return true;
214
- }
215
- return consent[providerId];
235
+ return true;
216
236
  };
217
237
 
218
238
  const tagStorage = 'edgeTag';
@@ -357,21 +377,70 @@ const setUserId = (userId) => {
357
377
  initUserId = userId;
358
378
  };
359
379
 
380
+ const getHeaders = () => ({
381
+ 'Content-type': 'application/json; charset=utf-8',
382
+ Accept: 'application/json; charset=utf-8',
383
+ EdgeTagUserId: handleGetUserId(),
384
+ });
360
385
  const beacon = (url, payload) => {
361
- let blob;
362
- if (payload) {
363
- blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
386
+ try {
387
+ let blob;
388
+ if (payload) {
389
+ blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
390
+ }
391
+ return navigator.sendBeacon(url, blob);
392
+ }
393
+ catch (e) {
394
+ console.log('Beacon not supported.');
395
+ return Promise.reject(new Error('Beacon not supported'));
364
396
  }
365
- return navigator.sendBeacon(url, blob);
366
397
  };
398
+ const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
399
+ const https = yield import('https');
400
+ const options = {
401
+ method,
402
+ headers: getHeaders(),
403
+ };
404
+ return new Promise((resolve, reject) => {
405
+ const req = https.request(url, options, (res) => {
406
+ res.on('data', (data) => {
407
+ try {
408
+ data = JSON.parse(data);
409
+ }
410
+ catch (_a) {
411
+ // do nothing
412
+ }
413
+ resolve({
414
+ body: data,
415
+ status: res.statusCode || 500,
416
+ });
417
+ });
418
+ });
419
+ req.on('error', (e) => {
420
+ reject(new Error(e.message));
421
+ });
422
+ if (payload && method !== 'GET') {
423
+ req.write(JSON.stringify(payload));
424
+ }
425
+ req.end();
426
+ });
427
+ });
367
428
  const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
429
+ if (typeof fetch === 'undefined') {
430
+ return yield fallbackAjax(method, url, payload)
431
+ .then((data) => {
432
+ if (data.status < 200 || data.status >= 300) {
433
+ return Promise.reject(new Error(JSON.stringify(data.body)));
434
+ }
435
+ return Promise.resolve(data.body);
436
+ })
437
+ .catch((error) => {
438
+ return Promise.reject(new Error(error));
439
+ });
440
+ }
368
441
  return yield fetch(url, {
369
442
  method,
370
- headers: {
371
- 'Content-type': 'application/json; charset=utf-8',
372
- Accept: 'application/json; charset=utf-8',
373
- EdgeTagUserId: handleGetUserId(),
374
- },
443
+ headers: getHeaders(),
375
444
  body: JSON.stringify(payload),
376
445
  credentials: 'include',
377
446
  })
@@ -461,6 +530,12 @@ const getConsent = () => {
461
530
  return memoryConsent;
462
531
  };
463
532
 
533
+ const encodeString = (name) => {
534
+ if (typeof btoa === 'undefined') {
535
+ return Buffer.from(name).toString('base64');
536
+ }
537
+ return btoa(name);
538
+ };
464
539
  const generateEventId = (name) => {
465
540
  let time = Date.now().toString();
466
541
  if (typeof performance !== 'undefined' &&
@@ -470,7 +545,7 @@ const generateEventId = (name) => {
470
545
  time = perf.toFixed(4);
471
546
  }
472
547
  }
473
- return `${btoa(name)}-${v4()}-${time}`;
548
+ return `${encodeString(name)}-${v4()}-${time}`;
474
549
  };
475
550
 
476
551
  const manifestVariables = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-js",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "JS SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",