-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: obfuscated precomputed assignments (#164)
* make getMD5HashWithSalt * add tests * Export saltedHasher * Fix tests * Alter test to use obfuscated file * Change branch name for test data * Get all the tests to pass * Make more obvious that the salt was decoded * Switch to using appendBinary for the salt * Clean up * Include salt in convenience method for setting precomputed flag store * Add a helper to convert context attributes to subject attributes * Change default to isObfuscated since we expect the precomputed api to mainly be used by clients * v4.7.1-alpha.0 * Revert "v4.7.1-alpha.0" This reverts commit b81175f. * v4.7.0-alpha.0 * Switch to initializing the client with an options object * Make response data not optional * precomputedFlag variable casing * update hashing * fix lint * handoff and address comments * bump version * Inf is a numeric attribute too * Remove unnecessary public methods * Remove more unnecessary functions * Add to exported interfaces * Update src/interfaces.ts Co-authored-by: Oleksii Shmalko <[email protected]> * Update src/attributes.ts attributes is ContextAttributes Co-authored-by: Oleksii Shmalko <[email protected]> * Remove redundant 'subjectAttributes as ContextAttributes' * Also print error if store is missing salt * Remove buildContextAttributes * v4.8.0-alpha.0 --------- Co-authored-by: Ty Potter <[email protected]> Co-authored-by: Leo Romanovsky <[email protected]> Co-authored-by: Oleksii Shmalko <[email protected]>
- Loading branch information
1 parent
4e979df
commit 65986fe
Showing
16 changed files
with
472 additions
and
334 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
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,76 @@ | ||
import { Attributes, BanditActions, BanditSubjectAttributes, ContextAttributes } from './types'; | ||
|
||
export function isInstanceOfContextualAttributes( | ||
attributes: unknown, | ||
): attributes is ContextAttributes { | ||
return Boolean( | ||
typeof attributes === 'object' && | ||
attributes && // exclude null | ||
'numericAttributes' in attributes && | ||
'categoricalAttributes' in attributes, | ||
); | ||
} | ||
|
||
export function ensureNonContextualSubjectAttributes( | ||
subjectAttributes: BanditSubjectAttributes, | ||
): Attributes { | ||
let result: Attributes; | ||
if (isInstanceOfContextualAttributes(subjectAttributes)) { | ||
const contextualSubjectAttributes = subjectAttributes; | ||
result = { | ||
...contextualSubjectAttributes.numericAttributes, | ||
...contextualSubjectAttributes.categoricalAttributes, | ||
}; | ||
} else { | ||
// Attributes are non-contextual | ||
result = subjectAttributes as Attributes; | ||
} | ||
return result; | ||
} | ||
|
||
export function ensureContextualSubjectAttributes( | ||
subjectAttributes: BanditSubjectAttributes, | ||
): ContextAttributes { | ||
if (isInstanceOfContextualAttributes(subjectAttributes)) { | ||
return subjectAttributes; | ||
} else { | ||
return deduceAttributeContext(subjectAttributes as Attributes); | ||
} | ||
} | ||
|
||
export function deduceAttributeContext(attributes: Attributes): ContextAttributes { | ||
const contextualAttributes: ContextAttributes = { | ||
numericAttributes: {}, | ||
categoricalAttributes: {}, | ||
}; | ||
Object.entries(attributes).forEach(([attribute, value]) => { | ||
const isNumeric = typeof value === 'number'; | ||
if (isNumeric) { | ||
contextualAttributes.numericAttributes[attribute] = value; | ||
} else { | ||
contextualAttributes.categoricalAttributes[attribute] = value; | ||
} | ||
}); | ||
return contextualAttributes; | ||
} | ||
|
||
export function ensureActionsWithContextualAttributes( | ||
actions: BanditActions, | ||
): Record<string, ContextAttributes> { | ||
let result: Record<string, ContextAttributes> = {}; | ||
if (Array.isArray(actions)) { | ||
// no context | ||
actions.forEach((action) => { | ||
result[action] = { numericAttributes: {}, categoricalAttributes: {} }; | ||
}); | ||
} else if (!Object.values(actions).every(isInstanceOfContextualAttributes)) { | ||
// Actions have non-contextual attributes; bucket based on number or not | ||
Object.entries(actions).forEach(([action, attributes]) => { | ||
result[action] = deduceAttributeContext(attributes); | ||
}); | ||
} else { | ||
// Actions already have contextual attributes | ||
result = actions as Record<string, ContextAttributes>; | ||
} | ||
return result; | ||
} |
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
Oops, something went wrong.