-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: supporting new analytics options, changed analytics algorithm
- Loading branch information
1 parent
4d22af7
commit a08fdba
Showing
22 changed files
with
463 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
var reverseVersion = require('./reverseVersion'); | ||
var stringPad = require('./stringPad'); | ||
var base64Map = require('../encoding/base64Map'); | ||
|
||
/** | ||
* @private | ||
* @description Encodes a semVer-like version string | ||
* @param {string} semVer Input can be either x.y.z or x.y | ||
* @return {string} A string built from 3 characters of the base64 table that encode the semVer | ||
*/ | ||
module.exports = function (semVer) { | ||
var strResult = ''; | ||
|
||
// support x.y or x.y.z by using 'parts' as a variable | ||
var parts = semVer.split('.').length; | ||
var paddedStringLength = parts * 6; // we pad to either 12 or 18 characters | ||
|
||
// reverse (but don't mirror) the version. 1.5.15 -> 15.5.1 | ||
// Pad to two spaces, 15.5.1 -> 15.05.01 | ||
var paddedReversedSemver = reverseVersion(semVer); | ||
|
||
// turn 15.05.01 to a string '150501' then to a number 150501 | ||
var num = parseInt(paddedReversedSemver.split('.').join('')); | ||
|
||
// Represent as binary, add left padding to 12 or 18 characters. | ||
// 150,501 -> 100100101111100101 | ||
|
||
var paddedBinary = num.toString(2); | ||
paddedBinary = stringPad(paddedBinary, paddedStringLength, '0'); | ||
|
||
// Stop in case an invalid version number was provided | ||
// paddedBinary must be built from sections of 6 bits | ||
if (paddedBinary.length % 6 !== 0) { | ||
throw 'Version must be smaller than 43.21.26)'; | ||
} | ||
|
||
// turn every 6 bits into a character using the base64Map | ||
paddedBinary.match(/.{1,6}/g).forEach(function (bitString) { | ||
// console.log(bitString); | ||
strResult += base64Map[bitString]; | ||
}); | ||
|
||
return strResult; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var sdkCode = 'M'; // Constant per SDK | ||
|
||
/** | ||
* @description Gets the relevant versions of the SDK(package version, node version and sdkCode) | ||
* @param {'default' | 'x.y.z' | 'x.y' | string} useSDKVersion Default uses package.json version | ||
* @param {'default' | 'x.y.z' | 'x.y' | string} useNodeVersion Default uses process.versions.node | ||
* @return {{sdkSemver:string, techVersion:string, sdkCode:string}} A map of relevant versions and codes | ||
*/ | ||
function getSDKVersions() { | ||
var useSDKVersion = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default'; | ||
var useNodeVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default'; | ||
|
||
var pkgJSONFile = fs.readFileSync(path.join(__dirname, '../../../package.json'), 'utf-8'); | ||
|
||
// allow to pass a custom SDKVersion | ||
var sdkSemver = useSDKVersion === 'default' ? JSON.parse(pkgJSONFile).version : useSDKVersion; | ||
|
||
// allow to pass a custom techVersion (Node version) | ||
var techVersion = useNodeVersion === 'default' ? process.versions.node : useNodeVersion; | ||
|
||
var product = 'A'; | ||
|
||
return { | ||
sdkSemver, | ||
techVersion, | ||
sdkCode, | ||
product | ||
}; | ||
} | ||
|
||
module.exports = getSDKVersions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
var removePatchFromSemver = require('./removePatchFromSemver'); | ||
var encodeVersion = require('./encodeVersion'); | ||
|
||
/** | ||
* @description Gets the SDK signature by encoding the SDK version and tech version | ||
* @param {{ | ||
* [techVersion]:string, | ||
* [sdkSemver]: string, | ||
* [sdkCode]: string, | ||
* [product]: string, | ||
* [feature]: string | ||
* }} analyticsOptions | ||
* @return {string} sdkAnalyticsSignature | ||
*/ | ||
function getSDKAnalyticsSignature() { | ||
var analyticsOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
|
||
try { | ||
var twoPartVersion = removePatchFromSemver(analyticsOptions.techVersion); | ||
var encodedSDKVersion = encodeVersion(analyticsOptions.sdkSemver); | ||
var encodedTechVersion = encodeVersion(twoPartVersion); | ||
var featureCode = analyticsOptions.feature; | ||
var SDKCode = analyticsOptions.sdkCode; | ||
var product = analyticsOptions.product; | ||
var algoVersion = 'B'; // The algo version is determined here, it should not be an argument | ||
|
||
return `${algoVersion}${product}${SDKCode}${encodedSDKVersion}${encodedTechVersion}${featureCode}`; | ||
} catch (e) { | ||
// Either SDK or Node versions were unparsable | ||
return 'E'; | ||
} | ||
} | ||
|
||
/** | ||
* @description Gets the analyticsOptions from options - should include sdkSemver, techVersion, sdkCode, and feature | ||
* @param options | ||
* @returns {{sdkSemver: (string), sdkCode, product, feature: string, techVersion: (string)} || {}} | ||
*/ | ||
function getAnalyticsOptions(options) { | ||
var analyticsOptions = { | ||
sdkSemver: options.sdkSemver, | ||
techVersion: options.techVersion, | ||
sdkCode: options.sdkCode, | ||
product: options.product, | ||
feature: '0' | ||
}; | ||
if (options.urlAnalytics) { | ||
if (options.accessibility) { | ||
analyticsOptions.feature = 'D'; | ||
} | ||
if (options.loading === 'lazy') { | ||
analyticsOptions.feature = 'C'; | ||
} | ||
if (options.responsive) { | ||
analyticsOptions.feature = 'A'; | ||
} | ||
if (options.placeholder) { | ||
analyticsOptions.feature = 'B'; | ||
} | ||
return analyticsOptions; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
getSDKAnalyticsSignature, | ||
getAnalyticsOptions | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @description Removes patch version from the semver if it exists | ||
* Turns x.y.z OR x.y into x.y | ||
* @param {'x.y.z' || 'x.y' || string} semVerStr | ||
*/ | ||
module.exports = function (semVerStr) { | ||
var parts = semVerStr.split('.'); | ||
return `${parts[0]}.${parts[1]}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
var stringPad = require('./stringPad'); | ||
|
||
/** | ||
* @description A semVer like string, x.y.z or x.y is allowed | ||
* Reverses the version positions, x.y.z turns to z.y.x | ||
* Pads each segment with '0' so they have length of 2 | ||
* Example: 1.2.3 -> 03.02.01 | ||
* @param {string} semVer Input can be either x.y.z or x.y | ||
* @return {string} in the form of zz.yy.xx ( | ||
*/ | ||
module.exports = function (semVer) { | ||
if (semVer.split('.').length < 2) { | ||
throw new Error('invalid semVer, must have at least two segments'); | ||
} | ||
|
||
// Split by '.', reverse, create new array with padded values and concat it together | ||
return semVer.split('.').reverse().map(function (segment) { | ||
return stringPad(segment, 2, '0'); | ||
}).join('.'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
function repeatStringNumTimes(string, times) { | ||
var repeatedString = ""; | ||
while (times > 0) { | ||
repeatedString += string; | ||
times--; | ||
} | ||
return repeatedString; | ||
} | ||
|
||
module.exports = function (value, targetLength, padString) { | ||
targetLength = targetLength >> 0; // truncate if number or convert non-number to 0; | ||
padString = String(typeof padString !== 'undefined' ? padString : ' '); | ||
if (value.length > targetLength) { | ||
return String(value); | ||
} else { | ||
targetLength = targetLength - value.length; | ||
if (targetLength > padString.length) { | ||
padString += repeatStringNumTimes(padString, targetLength / padString.length); | ||
} | ||
return padString.slice(0, targetLength) + String(value); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
|
||
var stringPad = require('../analytics/stringPad'); | ||
|
||
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
var num = 0; | ||
|
||
/** | ||
* Map of six-bit binary codes to Base64 characters | ||
*/ | ||
var base64Map = {}; | ||
|
||
[].concat(_toConsumableArray(chars)).forEach(function (char) { | ||
var key = num.toString(2); | ||
key = stringPad(key, 6, '0'); | ||
base64Map[key] = char; | ||
num++; | ||
}); | ||
|
||
module.exports = base64Map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const reverseVersion = require('./reverseVersion'); | ||
const stringPad = require('./stringPad'); | ||
const base64Map = require('../encoding/base64Map'); | ||
|
||
/** | ||
* @private | ||
* @description Encodes a semVer-like version string | ||
* @param {string} semVer Input can be either x.y.z or x.y | ||
* @return {string} A string built from 3 characters of the base64 table that encode the semVer | ||
*/ | ||
module.exports = (semVer) => { | ||
let strResult = ''; | ||
|
||
// support x.y or x.y.z by using 'parts' as a variable | ||
let parts = semVer.split('.').length; | ||
let paddedStringLength = parts * 6; // we pad to either 12 or 18 characters | ||
|
||
// reverse (but don't mirror) the version. 1.5.15 -> 15.5.1 | ||
// Pad to two spaces, 15.5.1 -> 15.05.01 | ||
let paddedReversedSemver = reverseVersion(semVer); | ||
|
||
// turn 15.05.01 to a string '150501' then to a number 150501 | ||
let num = parseInt(paddedReversedSemver.split('.').join('')); | ||
|
||
// Represent as binary, add left padding to 12 or 18 characters. | ||
// 150,501 -> 100100101111100101 | ||
|
||
let paddedBinary = num.toString(2); | ||
paddedBinary = stringPad(paddedBinary, paddedStringLength, '0'); | ||
|
||
// Stop in case an invalid version number was provided | ||
// paddedBinary must be built from sections of 6 bits | ||
if (paddedBinary.length % 6 !== 0) { | ||
throw 'Version must be smaller than 43.21.26)'; | ||
} | ||
|
||
// turn every 6 bits into a character using the base64Map | ||
paddedBinary.match(/.{1,6}/g).forEach((bitString) => { | ||
// console.log(bitString); | ||
strResult += base64Map[bitString]; | ||
}); | ||
|
||
return strResult; | ||
}; |
Oops, something went wrong.