@blotoutio/edgetag-sdk-browser 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 (3) hide show
  1. package/README.md +1 -1
  2. package/index.js +115 -36
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # EdgeTag SDK Browser
2
2
 
3
- More info on https://app.edgetag.io/integration/js
3
+ More info on https://app.edgetag.io/docs/js
package/index.js CHANGED
@@ -125,6 +125,39 @@
125
125
  allowedProvider = providers;
126
126
  };
127
127
 
128
+ const checkConsent = (providers) => {
129
+ if (isConsentDisabled()) {
130
+ return true;
131
+ }
132
+ const consent = getConsent();
133
+ if (!consent) {
134
+ return false;
135
+ }
136
+ const consentLength = Object.keys(consent).length;
137
+ if (!consentLength || (consentLength === 1 && consent['all'] === false)) {
138
+ return false;
139
+ }
140
+ const providersLength = Object.keys(providers || {}).length;
141
+ if (!providers ||
142
+ providersLength === 0 ||
143
+ (providersLength === 1 && providers['all'])) {
144
+ return (Object.values(consent).find((isAllowed) => isAllowed) || false);
145
+ }
146
+ for (const [key, value] of Object.entries(providers)) {
147
+ if (value === false || consent[key] === false) {
148
+ continue;
149
+ }
150
+ // we have consent
151
+ if (consent[key] ||
152
+ (consent['all'] === true && consent[key] === undefined)) {
153
+ // we have provider
154
+ if (value || (providers['all'] && providers[key] === undefined)) {
155
+ return true;
156
+ }
157
+ }
158
+ }
159
+ return false;
160
+ };
128
161
  const getUserAgent = () => {
129
162
  try {
130
163
  const nav = navigator;
@@ -140,23 +173,20 @@
140
173
  return (providerValue || (providers['all'] === true && providerValue === undefined));
141
174
  };
142
175
  const allowTag = (providers) => {
143
- const consent = getConsent();
144
- if (isConsentDisabled()) {
145
- return true;
146
- }
147
- if (!consent) {
176
+ if (!checkConsent(providers)) {
148
177
  return false;
149
178
  }
150
- if (consent['all']) {
179
+ const providersLength = Object.values(providers || {}).length;
180
+ if (!providers || providersLength === 0) {
151
181
  return true;
152
182
  }
153
- if (!providers) {
154
- return (Object.values(consent).find((isAllowed) => isAllowed) || false);
155
- }
156
183
  const allProviders = getAllowedProviders();
184
+ const allProvidersLength = Object.keys(allProviders || {}).length;
185
+ if (allProvidersLength === 0) {
186
+ return true;
187
+ }
157
188
  for (const provider of allProviders) {
158
- if (isProviderIncluded(providers, !!providers[provider]) &&
159
- consent[provider]) {
189
+ if (isProviderIncluded(providers, providers[provider])) {
160
190
  return true;
161
191
  }
162
192
  }
@@ -195,25 +225,15 @@
195
225
  if (!allowProvider(providerId)) {
196
226
  return false;
197
227
  }
228
+ if (!checkConsent(providers)) {
229
+ return false;
230
+ }
198
231
  if (providers && Object.keys(providers).length) {
199
- const tagProvider = providers[providerId];
200
- if (!(tagProvider ||
201
- (providers['all'] === true && tagProvider === undefined) ||
202
- (providers['all'] === false && tagProvider === true))) {
232
+ if (!isProviderIncluded(providers, providers[providerId])) {
203
233
  return false;
204
234
  }
205
235
  }
206
- const consent = getConsent();
207
- if (isConsentDisabled()) {
208
- return true;
209
- }
210
- if (!consent) {
211
- return false;
212
- }
213
- if (consent['all']) {
214
- return true;
215
- }
216
- return consent[providerId];
236
+ return true;
217
237
  };
218
238
 
219
239
  const tagStorage = 'edgeTag';
@@ -358,21 +378,70 @@
358
378
  initUserId = userId;
359
379
  };
360
380
 
381
+ const getHeaders = () => ({
382
+ 'Content-type': 'application/json; charset=utf-8',
383
+ Accept: 'application/json; charset=utf-8',
384
+ EdgeTagUserId: handleGetUserId(),
385
+ });
361
386
  const beacon = (url, payload) => {
362
- let blob;
363
- if (payload) {
364
- blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
387
+ try {
388
+ let blob;
389
+ if (payload) {
390
+ blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
391
+ }
392
+ return navigator.sendBeacon(url, blob);
393
+ }
394
+ catch (e) {
395
+ console.log('Beacon not supported.');
396
+ return Promise.reject(new Error('Beacon not supported'));
365
397
  }
366
- return navigator.sendBeacon(url, blob);
367
398
  };
399
+ const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
400
+ const https = yield import('https');
401
+ const options = {
402
+ method,
403
+ headers: getHeaders(),
404
+ };
405
+ return new Promise((resolve, reject) => {
406
+ const req = https.request(url, options, (res) => {
407
+ res.on('data', (data) => {
408
+ try {
409
+ data = JSON.parse(data);
410
+ }
411
+ catch (_a) {
412
+ // do nothing
413
+ }
414
+ resolve({
415
+ body: data,
416
+ status: res.statusCode || 500,
417
+ });
418
+ });
419
+ });
420
+ req.on('error', (e) => {
421
+ reject(new Error(e.message));
422
+ });
423
+ if (payload && method !== 'GET') {
424
+ req.write(JSON.stringify(payload));
425
+ }
426
+ req.end();
427
+ });
428
+ });
368
429
  const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
430
+ if (typeof fetch === 'undefined') {
431
+ return yield fallbackAjax(method, url, payload)
432
+ .then((data) => {
433
+ if (data.status < 200 || data.status >= 300) {
434
+ return Promise.reject(new Error(JSON.stringify(data.body)));
435
+ }
436
+ return Promise.resolve(data.body);
437
+ })
438
+ .catch((error) => {
439
+ return Promise.reject(new Error(error));
440
+ });
441
+ }
369
442
  return yield fetch(url, {
370
443
  method,
371
- headers: {
372
- 'Content-type': 'application/json; charset=utf-8',
373
- Accept: 'application/json; charset=utf-8',
374
- EdgeTagUserId: handleGetUserId(),
375
- },
444
+ headers: getHeaders(),
376
445
  body: JSON.stringify(payload),
377
446
  credentials: 'include',
378
447
  })
@@ -942,6 +1011,12 @@
942
1011
 
943
1012
  v35('v5', 0x50, sha1);
944
1013
 
1014
+ const encodeString = (name) => {
1015
+ if (typeof btoa === 'undefined') {
1016
+ return Buffer.from(name).toString('base64');
1017
+ }
1018
+ return btoa(name);
1019
+ };
945
1020
  const generateEventId = (name) => {
946
1021
  let time = Date.now().toString();
947
1022
  if (typeof performance !== 'undefined' &&
@@ -951,7 +1026,7 @@
951
1026
  time = perf.toFixed(4);
952
1027
  }
953
1028
  }
954
- return `${btoa(name)}-${v4()}-${time}`;
1029
+ return `${encodeString(name)}-${v4()}-${time}`;
955
1030
  };
956
1031
 
957
1032
  const manifestVariables = {};
@@ -1282,6 +1357,10 @@
1282
1357
  keys(callback) {
1283
1358
  keys(callback);
1284
1359
  }
1360
+
1361
+ getUserId() {
1362
+ return getUserId()
1363
+ }
1285
1364
  }
1286
1365
 
1287
1366
  var api = new API();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",