@pubtech-ai/core 2.7.1 → 2.8.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.
@@ -19,9 +19,10 @@ export declare class TCString {
19
19
  * @param {string} encodedTCString - base64url encoded Transparency and
20
20
  * Consent String to decode - can also be a single or group of segments of
21
21
  * the string
22
- * @param {string} [tcModel] - model to enhance with the information. If
22
+ * @param {TCModel} tcModel - model to enhance with the information. If
23
23
  * none is passed a new instance of TCModel will be created.
24
+ * @param {boolean} useCached - if true it will return a reference to the same TCModel already created previously.
24
25
  * @return {TCModel} - Returns populated TCModel
25
26
  */
26
- static decode(encodedTCString: string, tcModel?: TCModel): TCModel;
27
+ static decode(encodedTCString: string, tcModel?: TCModel, useCached?: boolean): TCModel;
27
28
  }
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TCString=void 0;var index_js_1=require("./encoder/index.js"),index_js_2=require("./model/index.js"),IntEncoder_js_1=require("./encoder/field/IntEncoder.js"),TCModel_js_1=require("./TCModel.js"),decodeCachedResults={},TCString=function(){function e(){}return e.encode=function(e,n){var d,r="";return e=index_js_1.SemanticPreEncoder.process(e,n),(d=Array.isArray(null==n?void 0:n.segments)?n.segments:new index_js_1.SegmentSequence(e,n)[""+e.version]).forEach((function(n,s){var t="";s<d.length-1&&(t="."),r+=index_js_1.SegmentEncoder.encode(e,n)+t})),r},e.decode=function(e,n){if(decodeCachedResults[e])return decodeCachedResults[e];decodeCachedResults={};var d=e.split("."),r=d.length;n||(n=new TCModel_js_1.TCModel);for(var s=0;s<r;s++){var t=d[s],o=index_js_1.Base64Url.decode(t.charAt(0)).substr(0,index_js_1.BitLength.segmentType),i=index_js_2.SegmentIDs.ID_TO_KEY[IntEncoder_js_1.IntEncoder.decode(o,index_js_1.BitLength.segmentType).toString()];index_js_1.SegmentEncoder.decode(t,n,i)}return decodeCachedResults[e]=n,n},e}();exports.TCString=TCString;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TCString=void 0;var index_js_1=require("./encoder/index.js"),index_js_2=require("./model/index.js"),IntEncoder_js_1=require("./encoder/field/IntEncoder.js"),TCModel_js_1=require("./TCModel.js"),decodeCachedResults={},TCString=function(){function e(){}return e.encode=function(e,n){var d,r="";return e=index_js_1.SemanticPreEncoder.process(e,n),(d=Array.isArray(null==n?void 0:n.segments)?n.segments:new index_js_1.SegmentSequence(e,n)[""+e.version]).forEach((function(n,s){var t="";s<d.length-1&&(t="."),r+=index_js_1.SegmentEncoder.encode(e,n)+t})),r},e.decode=function(e,n,d){if(void 0===d&&(d=!1),d&&decodeCachedResults[e])return decodeCachedResults[e];decodeCachedResults={};var r=e.split("."),s=r.length;n||(n=new TCModel_js_1.TCModel);for(var t=0;t<s;t++){var o=r[t],i=index_js_1.Base64Url.decode(o.charAt(0)).substr(0,index_js_1.BitLength.segmentType),c=index_js_2.SegmentIDs.ID_TO_KEY[IntEncoder_js_1.IntEncoder.decode(i,index_js_1.BitLength.segmentType).toString()];index_js_1.SegmentEncoder.decode(o,n,c)}return decodeCachedResults[e]=n,n},e}();exports.TCString=TCString;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PurposeRestrictionVectorEncoder=void 0;var BitLength_js_1=require("../BitLength.js"),BooleanEncoder_js_1=require("./BooleanEncoder.js"),index_js_1=require("../../errors/index.js"),IntEncoder_js_1=require("./IntEncoder.js"),index_js_2=require("../../model/index.js"),PurposeRestrictionVectorEncoder=function(){function e(){}return e.encode=function(e){var n=IntEncoder_js_1.IntEncoder.encode(e.numRestrictions,BitLength_js_1.BitLength.numRestrictions);if(!e.isEmpty()){var t=Array.from(e.gvl.vendorIds),r=function(e,n){var r=t.indexOf(e);if(t.indexOf(n)-r>0){var o=t.indexOf(e+1);return t[o]}return e},o=new Map;e.getRestrictions().forEach((function(t){n+=IntEncoder_js_1.IntEncoder.encode(t.purposeId,BitLength_js_1.BitLength.purposeId),n+=IntEncoder_js_1.IntEncoder.encode(t.restrictionType,BitLength_js_1.BitLength.restrictionType);var i=e.getVendorsSet(t),s=0,d="";if(o.has(i)){var _=o.get(i);s=_.numEntries,d=_.rangeField}else{for(var c=Array.from(i).sort((function(e,n){return e-n})),g=c.length,B=0,h=0;h<g;h++){var L=c[h];if(0===B&&(s++,B=L),h===g-1||c[h+1]>r(L,c[g-1])){var j=!(L===B);d+=BooleanEncoder_js_1.BooleanEncoder.encode(j),d+=IntEncoder_js_1.IntEncoder.encode(B,BitLength_js_1.BitLength.vendorId),j&&(d+=IntEncoder_js_1.IntEncoder.encode(L,BitLength_js_1.BitLength.vendorId)),B=0}}o.set(i,{numEntries:s,rangeField:d})}n+=IntEncoder_js_1.IntEncoder.encode(s,BitLength_js_1.BitLength.numEntries),n+=d}))}return n},e.decode=function(e){var n=0,t=new index_js_2.PurposeRestrictionVector,r=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numRestrictions),BitLength_js_1.BitLength.numRestrictions);n+=BitLength_js_1.BitLength.numRestrictions;for(var o=0;o<r;o++){var i=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.purposeId),BitLength_js_1.BitLength.purposeId);n+=BitLength_js_1.BitLength.purposeId;var s=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.restrictionType),BitLength_js_1.BitLength.restrictionType);n+=BitLength_js_1.BitLength.restrictionType;var d=new index_js_2.PurposeRestriction(i,s),_=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numEntries),BitLength_js_1.BitLength.numEntries);n+=BitLength_js_1.BitLength.numEntries;for(var c=0;c<_;c++){var g=BooleanEncoder_js_1.BooleanEncoder.decode(e.substr(n,BitLength_js_1.BitLength.anyBoolean));n+=BitLength_js_1.BitLength.anyBoolean;var B=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,g){var h=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,h<B)throw new index_js_1.DecodingError("Invalid RangeEntry: endVendorId ".concat(h," is less than ").concat(B));for(var L=B;L<=h;L++)t.add(L,d)}else t.add(B,d)}}return t.bitLength=n,t},e}();exports.PurposeRestrictionVectorEncoder=PurposeRestrictionVectorEncoder;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PurposeRestrictionVectorEncoder=void 0;var BitLength_js_1=require("../BitLength.js"),BooleanEncoder_js_1=require("./BooleanEncoder.js"),index_js_1=require("../../errors/index.js"),IntEncoder_js_1=require("./IntEncoder.js"),index_js_2=require("../../model/index.js"),PurposeRestrictionVectorEncoder=function(){function e(){}return e.encode=function(e){var n=IntEncoder_js_1.IntEncoder.encode(e.numRestrictions,BitLength_js_1.BitLength.numRestrictions);if(!e.isEmpty()){var t=Array.from(e.gvl.vendorIds),r=function(e,n){var r=t.indexOf(e);if(t.indexOf(n)-r>0){var o=t.indexOf(e+1);return{nextVendorId:t[o],index:t[r+1]}}return{nextVendorId:e,index:e}},o=new Map;e.getRestrictions().forEach((function(t){n+=IntEncoder_js_1.IntEncoder.encode(t.purposeId,BitLength_js_1.BitLength.purposeId),n+=IntEncoder_js_1.IntEncoder.encode(t.restrictionType,BitLength_js_1.BitLength.restrictionType);var i=e.getVendorsSet(t),s=0,d="";if(o.has(i)){var _=o.get(i);s=_.numEntries,d=_.rangeField}else{for(var c=Array.from(i).sort((function(e,n){return e-n})),g=c.length,B=0,h=0;h<g;h++){var L=c[h];0===B&&(s++,B=L);var j=h===g-1;if(!j){var u=r(L,c[g-1]),a=u.nextVendorId,E=u.index;c[h+1]>a?j=!0:E>h&&E<g&&(h=E)}if(j){var I=!(L===B);d+=BooleanEncoder_js_1.BooleanEncoder.encode(I),d+=IntEncoder_js_1.IntEncoder.encode(B,BitLength_js_1.BitLength.vendorId),I&&(d+=IntEncoder_js_1.IntEncoder.encode(L,BitLength_js_1.BitLength.vendorId)),B=0}}o.set(i,{numEntries:s,rangeField:d})}n+=IntEncoder_js_1.IntEncoder.encode(s,BitLength_js_1.BitLength.numEntries),n+=d}))}return n},e.decode=function(e){var n=0,t=new index_js_2.PurposeRestrictionVector,r=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numRestrictions),BitLength_js_1.BitLength.numRestrictions);n+=BitLength_js_1.BitLength.numRestrictions;for(var o=0;o<r;o++){var i=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.purposeId),BitLength_js_1.BitLength.purposeId);n+=BitLength_js_1.BitLength.purposeId;var s=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.restrictionType),BitLength_js_1.BitLength.restrictionType);n+=BitLength_js_1.BitLength.restrictionType;var d=new index_js_2.PurposeRestriction(i,s),_=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numEntries),BitLength_js_1.BitLength.numEntries);n+=BitLength_js_1.BitLength.numEntries;for(var c=0;c<_;c++){var g=BooleanEncoder_js_1.BooleanEncoder.decode(e.substr(n,BitLength_js_1.BitLength.anyBoolean));n+=BitLength_js_1.BitLength.anyBoolean;var B=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,g){var h=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,h<B)throw new index_js_1.DecodingError("Invalid RangeEntry: endVendorId ".concat(h," is less than ").concat(B));for(var L=B;L<=h;L++)t.add(L,d)}else t.add(B,d)}}return t.bitLength=n,t},e}();exports.PurposeRestrictionVectorEncoder=PurposeRestrictionVectorEncoder;
@@ -19,9 +19,10 @@ export declare class TCString {
19
19
  * @param {string} encodedTCString - base64url encoded Transparency and
20
20
  * Consent String to decode - can also be a single or group of segments of
21
21
  * the string
22
- * @param {string} [tcModel] - model to enhance with the information. If
22
+ * @param {TCModel} tcModel - model to enhance with the information. If
23
23
  * none is passed a new instance of TCModel will be created.
24
+ * @param {boolean} useCached - if true it will return a reference to the same TCModel already created previously.
24
25
  * @return {TCModel} - Returns populated TCModel
25
26
  */
26
- static decode(encodedTCString: string, tcModel?: TCModel): TCModel;
27
+ static decode(encodedTCString: string, tcModel?: TCModel, useCached?: boolean): TCModel;
27
28
  }
@@ -43,12 +43,13 @@ export class TCString {
43
43
  * @param {string} encodedTCString - base64url encoded Transparency and
44
44
  * Consent String to decode - can also be a single or group of segments of
45
45
  * the string
46
- * @param {string} [tcModel] - model to enhance with the information. If
46
+ * @param {TCModel} tcModel - model to enhance with the information. If
47
47
  * none is passed a new instance of TCModel will be created.
48
+ * @param {boolean} useCached - if true it will return a reference to the same TCModel already created previously.
48
49
  * @return {TCModel} - Returns populated TCModel
49
50
  */
50
- static decode(encodedTCString, tcModel) {
51
- if (decodeCachedResults[encodedTCString]) {
51
+ static decode(encodedTCString, tcModel, useCached = false) {
52
+ if (useCached && decodeCachedResults[encodedTCString]) {
52
53
  return decodeCachedResults[encodedTCString];
53
54
  }
54
55
  decodeCachedResults = {};
@@ -15,9 +15,15 @@ export class PurposeRestrictionVectorEncoder {
15
15
  const lastIndex = gvlVendorIds.indexOf(lastVendorId);
16
16
  if (lastIndex - firstIndex > 0) {
17
17
  const nextIndex = gvlVendorIds.indexOf(vendorId + 1);
18
- return gvlVendorIds[nextIndex];
18
+ return {
19
+ nextVendorId: gvlVendorIds[nextIndex],
20
+ index: gvlVendorIds[firstIndex + 1],
21
+ };
19
22
  }
20
- return vendorId;
23
+ return {
24
+ nextVendorId: vendorId,
25
+ index: vendorId,
26
+ };
21
27
  };
22
28
  const cachedResults = new Map();
23
29
  // create each restriction group
@@ -48,10 +54,20 @@ export class PurposeRestrictionVectorEncoder {
48
54
  numEntries++;
49
55
  startId = vendorId;
50
56
  }
57
+ let isRangeEncodeRequired = i === len - 1;
58
+ if (!isRangeEncodeRequired) {
59
+ const { nextVendorId, index } = nextGvlVendor(vendorId, vendors[len - 1]);
60
+ if (vendors[i + 1] > nextVendorId) {
61
+ isRangeEncodeRequired = true;
62
+ }
63
+ else if (index > i && index < len) {
64
+ i = index;
65
+ }
66
+ }
51
67
  /**
52
68
  * either end of the loop or there are GVL vendor IDs before the next one
53
69
  */
54
- if (i === len - 1 || vendors[i + 1] > nextGvlVendor(vendorId, vendors[len - 1])) {
70
+ if (isRangeEncodeRequired) {
55
71
  /**
56
72
  * it's a range entry if we've got something other than the start
57
73
  * ID
@@ -73,7 +89,7 @@ export class PurposeRestrictionVectorEncoder {
73
89
  });
74
90
  }
75
91
  /**
76
- * now that the range encoding is built, encode the number of ranges
92
+ * now that the range encoding is built, encode the number of ranges
77
93
  * and then append the range field to the bitString.
78
94
  */
79
95
  bitString += IntEncoder.encode(numEntries, BitLength.numEntries);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pubtech-ai/core",
3
- "version": "2.7.1",
3
+ "version": "2.8.0",
4
4
  "description": "Ensures consistent encoding and decoding of TC Signals for the iab. Transparency and Consent Framework (TCF).",
5
5
  "author": "Mayank Mishra <mayank@iabtechlab.com>",
6
6
  "homepage": "https://iabtcf.com/",
@@ -28,7 +28,7 @@
28
28
  "test-cov": "rm -rf coverage; nyc --reporter=html mocha"
29
29
  },
30
30
  "devDependencies": {
31
- "@pubtech-ai/testing": "2.7.1",
31
+ "@pubtech-ai/testing": "2.8.0",
32
32
  "@istanbuljs/nyc-config-typescript": "^0.1.3",
33
33
  "@types/sinon": "^10.0.11",
34
34
  "@types/sinon-chai": "3.2.8",