Skip to content

Commit

Permalink
perf: improve encoding and add param for perf in TCString decode
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-prontera committed Dec 14, 2023
1 parent 8933e72 commit c0d7167
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/publish-version
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
BRANCH=$(git rev-parse --abbrev-ref HEAD)
DIFF=$(git diff HEAD)
ROOT=$(git rev-parse --show-toplevel)
npm_package_version=2.7.1
npm_package_version=2.8.0

#if [[ -n $DIFF ]]; then
#
Expand Down
2 changes: 1 addition & 1 deletion bin/version
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

ROOT=$(git rev-parse --show-toplevel)
npm_package_version=2.7.1
npm_package_version=2.8.0

cd $ROOT

Expand Down
4 changes: 2 additions & 2 deletions modules/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubtech-ai/cli",
"version": "2.7.1",
"version": "2.8.0",
"description": "Decode a iab TCF (Transparency and Consent Framework) TC String via the command line",
"author": "Mayank Mishra <[email protected]>",
"homepage": "https://iabtcf.com/",
Expand All @@ -27,7 +27,7 @@
"lint": "eslint `find src -name '*.ts'`"
},
"dependencies": {
"@pubtech-ai/core": "2.7.1"
"@pubtech-ai/core": "2.8.0"
},
"devDependencies": {
"@types/node": "18.17.4",
Expand Down
6 changes: 3 additions & 3 deletions modules/cmpapi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubtech-ai/cmpapi",
"version": "2.7.1",
"version": "2.8.0",
"description": "Ensures other in-page digital marketing technologies have access to CMP transparency and consent information for the iab. Transparency and Consent Framework (TCF).",
"author": "Mayank Mishra <[email protected]>",
"homepage": "https://iabtcf.com/",
Expand Down Expand Up @@ -31,8 +31,8 @@
"@pubtech-ai/core": ">=1.0.0"
},
"devDependencies": {
"@pubtech-ai/stub": "2.7.1",
"@pubtech-ai/testing": "2.7.1",
"@pubtech-ai/stub": "2.8.0",
"@pubtech-ai/testing": "2.8.0",
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@types/mocha": "^9.1.0",
"@types/sinon": "10.0.11",
Expand Down
4 changes: 2 additions & 2 deletions modules/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubtech-ai/core",
"version": "2.7.1",
"version": "2.8.0",
"description": "Ensures consistent encoding and decoding of TC Signals for the iab. Transparency and Consent Framework (TCF).",
"author": "Mayank Mishra <[email protected]>",
"homepage": "https://iabtcf.com/",
Expand Down Expand Up @@ -28,7 +28,7 @@
"test-cov": "rm -rf coverage; nyc --reporter=html mocha"
},
"devDependencies": {
"@pubtech-ai/testing": "2.7.1",
"@pubtech-ai/testing": "2.8.0",
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@types/sinon": "^10.0.11",
"@types/sinon-chai": "3.2.8",
Expand Down
7 changes: 4 additions & 3 deletions modules/core/src/TCString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export class TCString {
* @param {string} encodedTCString - base64url encoded Transparency and
* Consent String to decode - can also be a single or group of segments of
* the string
* @param {string} [tcModel] - model to enhance with the information. If
* @param {TCModel} tcModel - model to enhance with the information. If
* none is passed a new instance of TCModel will be created.
* @param {boolean} useCached - if true it will return a reference to the same TCModel already created previously.
* @return {TCModel} - Returns populated TCModel
*/
public static decode(encodedTCString: string, tcModel?: TCModel): TCModel {
public static decode(encodedTCString: string, tcModel?: TCModel, useCached = false): TCModel {

if (decodeCachedResults[encodedTCString]) {
if (useCached && decodeCachedResults[encodedTCString]) {

return decodeCachedResults[encodedTCString];

Expand Down
35 changes: 30 additions & 5 deletions modules/core/src/encoder/field/PurposeRestrictionVectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ export class PurposeRestrictionVectorEncoder {

const gvlVendorIds = Array.from(prVector.gvl.vendorIds);

const nextGvlVendor = (vendorId, lastVendorId): number => {
const nextGvlVendor = (vendorId, lastVendorId) => {

const firstIndex = gvlVendorIds.indexOf(vendorId);
const lastIndex = gvlVendorIds.indexOf(lastVendorId);

if (lastIndex - firstIndex > 0) {

const nextIndex = gvlVendorIds.indexOf(vendorId + 1);
return gvlVendorIds[nextIndex];

return {
nextVendorId: gvlVendorIds[nextIndex],
index: gvlVendorIds[firstIndex + 1],
};

}

return vendorId;
return {
nextVendorId: vendorId,
index: vendorId,
};

};

Expand Down Expand Up @@ -73,10 +80,28 @@ export class PurposeRestrictionVectorEncoder {

}

let isRangeEncodeRequired = i === len - 1;

if (!isRangeEncodeRequired) {

const {nextVendorId, index} = nextGvlVendor(vendorId, vendors[len - 1]);

if (vendors[i + 1] > nextVendorId) {

isRangeEncodeRequired = true;

} else if (index > i && index < len) {

i = index;

}

}

/**
* either end of the loop or there are GVL vendor IDs before the next one
*/
if (i === len - 1 || vendors[i + 1] > nextGvlVendor(vendorId, vendors[len - 1])) {
if (isRangeEncodeRequired) {

/**
* it's a range entry if we've got something other than the start
Expand Down Expand Up @@ -109,7 +134,7 @@ export class PurposeRestrictionVectorEncoder {
}

/**
* now that the range encoding is built, encode the number of ranges
* now that the range encoding is built, encode the number of ranges
* and then append the range field to the bitString.
*/
bitString += IntEncoder.encode(numEntries, BitLength.numEntries);
Expand Down
2 changes: 1 addition & 1 deletion modules/stub/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubtech-ai/stub",
"version": "2.7.1",
"version": "2.8.0",
"description": "CMP API Stub code",
"author": "Mayank Mishra <[email protected]>",
"homepage": "https://iabtcf.com/",
Expand Down
2 changes: 1 addition & 1 deletion modules/testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubtech-ai/testing",
"version": "2.7.1",
"version": "2.8.0",
"description": "Shared testing utilities",
"author": "Mayank Mishra <[email protected]>",
"homepage": "https://iabtcf.com/",
Expand Down

0 comments on commit c0d7167

Please sign in to comment.