Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No Ticket] Cleanup pass and enable strict mode #103

Merged
merged 14 commits into from
Feb 1, 2023
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ module.exports = {
'plugin:@typescript-eslint/recommended'
],
globals: {},
rules: {}
rules: {
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/no-explicit-any": "error"
}
}
2 changes: 1 addition & 1 deletion rollup/dist.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
],
plugins: [
cleaner({targets: [OUTPUT_DIR]}),
ts({transpileOnly: true, tsconfig: resolvedConfig => ({...resolvedConfig, declaration: true})}),
ts(),
resolve(),
commonjs(),
strip(),
Expand Down
2 changes: 1 addition & 1 deletion rollup/test-resources.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
},
plugins: [
cleaner({targets: [OUTPUT_DIR]}),
ts({transpileOnly: true}),
ts({tsconfig: resolvedConfig => ({...resolvedConfig, declaration: false})}),
resolve(),
commonjs(),
babel(),
Expand Down
2 changes: 1 addition & 1 deletion src/config-validators/remove-invalid-pairs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventBus, LiveConnectConfig } from '../types'

export function removeInvalidPairs (config: LiveConnectConfig, eventBus: EventBus) {
export function removeInvalidPairs (config: LiveConnectConfig, eventBus: EventBus): LiveConnectConfig {
if (config && config.appId && config.distributorId) {
const distributorId = config.distributorId
delete config.distributorId
Expand Down
9 changes: 5 additions & 4 deletions src/enrichers/identifiers-nohash.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { containsEmailField, isEmail } from '../utils/email'
import { safeToString, isArray, trim } from '../utils/types'
import { EventBus, IMinimalStorageHandler, State } from '../types'
import { EventBus, RetrievedIdentifier, State } from '../types'
import { MinimalStorageHandler } from '../handlers/storage-handler'

export function enrich (state: State, storageHandler: IMinimalStorageHandler, eventBus: EventBus): State {
export function enrich (state: State, storageHandler: MinimalStorageHandler, eventBus: EventBus): State {
try {
return _parseIdentifiersToResolve(state, storageHandler)
} catch (e) {
Expand All @@ -11,10 +12,10 @@ export function enrich (state: State, storageHandler: IMinimalStorageHandler, ev
}
}

function _parseIdentifiersToResolve (state: State, storageHandler: IMinimalStorageHandler): State {
function _parseIdentifiersToResolve (state: State, storageHandler: MinimalStorageHandler): State {
state.identifiersToResolve = state.identifiersToResolve || []
const cookieNames = isArray(state.identifiersToResolve) ? state.identifiersToResolve : safeToString(state.identifiersToResolve).split(',')
const identifiers = []
const identifiers: RetrievedIdentifier[] = []
for (let i = 0; i < cookieNames.length; i++) {
const identifierName = trim(cookieNames[i])
const identifierValue = storageHandler.getCookie(identifierName) || storageHandler.getDataFromLocalStorage(identifierName)
Expand Down
25 changes: 14 additions & 11 deletions src/enrichers/identifiers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { replaceEmailsWithHashes } from '../utils/email'
import { safeToString, isString, isArray } from '../utils/types'
import { EventBus, HashedEmail, IMinimalStorageHandler, State } from '../types'
import { EventBus, HashedEmail, State, RetrievedIdentifier } from '../types'
import { MinimalStorageHandler } from '../handlers/storage-handler'

export function enrich (state: State, storageHandler: IMinimalStorageHandler, eventBus: EventBus) {
export function enrich (state: State, storageHandler: MinimalStorageHandler, eventBus: EventBus): State {
try {
return _getIdentifiers(_parseIdentifiersToResolve(state), storageHandler)
} catch (e) {
eventBus.emitError('IdentifiersEnricher', e)
if (eventBus) {
eventBus.emitError('IdentifiersEnricher', e)
}
return {}
}
}

function _parseIdentifiersToResolve (state: State): string[] {
let cookieNames = []
let cookieNames: string[] = []
if (state.identifiersToResolve) {
if (isArray(state.identifiersToResolve)) {
cookieNames = state.identifiersToResolve as string[]
Expand All @@ -26,9 +29,9 @@ function _parseIdentifiersToResolve (state: State): string[] {
return cookieNames
}

function _getIdentifiers (cookieNames: string[], storageHandler: IMinimalStorageHandler): State {
const identifiers = []
let hashes = []
function _getIdentifiers (cookieNames: string[], storageHandler: MinimalStorageHandler): State {
const identifiers: RetrievedIdentifier[] = []
let hashes: HashedEmail[] = []
for (let i = 0; i < cookieNames.length; i++) {
const identifierName = cookieNames[i]
const identifierValue = storageHandler.getCookie(identifierName) || storageHandler.getDataFromLocalStorage(identifierName)
Expand All @@ -48,12 +51,12 @@ function _getIdentifiers (cookieNames: string[], storageHandler: IMinimalStorage
}

function _deduplicateHashes (hashes: HashedEmail[]): HashedEmail[] {
const seen = {}
const result = []
const seen = new Set<string>()
const result: HashedEmail[] = []
for (let i = 0; i < hashes.length; i++) {
if (!(hashes[i].md5 in seen)) {
if (!seen.has(hashes[i].md5)) {
result.push(hashes[i])
seen[hashes[i].md5] = true
seen.add(hashes[i].md5)
}
}
return result
Expand Down
11 changes: 7 additions & 4 deletions src/enrichers/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { getPage, getReferrer, getContextElements } from '../utils/page'
/**
* @private
*/
let _currentPage = null
let _currentPage: State | null = null

export function enrich (state: State): State {
if (!_currentPage) {
_currentPage = {
if (_currentPage) {
return _currentPage
} else {
const result = {
pageUrl: getPage(),
referrer: getReferrer(),
contextElements: getContextElements(state.privacyMode, state.contextSelectors, state.contextElementsLength)
}
_currentPage = result
return result
}
return _currentPage
}
7 changes: 4 additions & 3 deletions src/enrichers/people-verified.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PEOPLE_VERIFIED_LS_ENTRY } from '../utils/consts'
import { EventBus, IMinimalStorageHandler, State } from '../types'
import { EventBus, State } from '../types'
import { MinimalStorageHandler } from '../handlers/storage-handler'

export function enrich (state: State, storageHandler: IMinimalStorageHandler, eventBus: EventBus) {
export function enrich (state: State, storageHandler: MinimalStorageHandler, eventBus: EventBus): State {
try {
return { peopleVerifiedId: state.peopleVerifiedId || storageHandler.getDataFromLocalStorage(PEOPLE_VERIFIED_LS_ENTRY) }
return { peopleVerifiedId: state.peopleVerifiedId || storageHandler.getDataFromLocalStorage(PEOPLE_VERIFIED_LS_ENTRY) || undefined }
3link marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
eventBus.emitError('PeopleVerifiedEnrich', e)
return {}
Expand Down
34 changes: 16 additions & 18 deletions src/events/error-pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { PixelSender } from '../pixel/sender'
import { StateWrapper } from '../pixel/state'
import * as page from '../enrichers/page'
import * as C from '../utils/consts'
import { EventBus, ICallHandler, IPixelSender, State } from '../types'
import { EventBus, State } from '../types'
import { CallHandler } from '../handlers/call-handler'
import { isRecord, isString } from '../utils/types'

let _state = null
let _pixelSender: IPixelSender = null
const MAX_ERROR_FIELD_LENGTH = 120

const _defaultReturn: State = {
Expand All @@ -15,35 +15,35 @@ const _defaultReturn: State = {
}
}

function _asInt (field: any): number | undefined {
function _asInt (field: unknown): number | undefined {
try {
const intValue = field * 1
const intValue = (field as number) * 1
leonelcuevas marked this conversation as resolved.
Show resolved Hide resolved
return isNaN(intValue) ? undefined : intValue
} catch {

}
}

function _truncate (value: string): string {
function _truncate (value: unknown): string | undefined {
try {
if (value && value.length && value.length > MAX_ERROR_FIELD_LENGTH) {
if (isString(value) && value.length && value.length > MAX_ERROR_FIELD_LENGTH) {
return `${value.substr(0, MAX_ERROR_FIELD_LENGTH)}...`
} else {
return value
return `${value}`
}
} catch {
}
}

export function asErrorDetails (e: any): State {
if (e) {
export function asErrorDetails (e: unknown): State {
if (isRecord(e)) {
return {
errorDetails: {
message: _truncate(e.message),
name: _truncate(e.name),
message: _truncate(e.message) || '',
name: _truncate(e.name) || '',
stackTrace: _truncate(e.stack),
lineNumber: _asInt(e.lineNumber),
lineColumn: _asInt(e.lineColumn),
columnNumber: _asInt(e.columnNumber),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

fileName: _truncate(e.fileName)
}
}
Expand All @@ -52,14 +52,12 @@ export function asErrorDetails (e: any): State {
}
}

export function register (state: State, callHandler: ICallHandler, eventBus: EventBus): void {
export function register (state: State, callHandler: CallHandler, eventBus: EventBus): void {
try {
_pixelSender = new PixelSender(state, callHandler, eventBus)
_state = state || {}
const pixelSender = new PixelSender(state, callHandler, eventBus)

eventBus.on(C.ERRORS_PREFIX, (error) => {
console.log(error, _state)
_pixelSender.sendPixel(new StateWrapper(asErrorDetails(error), eventBus).combineWith(_state || {}).combineWith(page.enrich({})))
pixelSender.sendPixel(new StateWrapper(asErrorDetails(error), eventBus).combineWith(state || {}).combineWith(page.enrich({})))
})
} catch (e) {
console.error('handlers.error.register', e)
Expand Down
10 changes: 6 additions & 4 deletions src/events/event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// @ts-nocheck
wi101 marked this conversation as resolved.
Show resolved Hide resolved
import { ReplayEmitter, wrapError } from './replayemitter'
import * as C from '../utils/consts'
import { isFunction } from '../utils/types'
import { EventBus } from '../types'

function initBus (size?: number): EventBus {
if (typeof size === 'undefined') {
size = 5
if (typeof size === 'number' && size >= 0) {
return new ReplayEmitter(size)
} else {
return new ReplayEmitter(5)
}
return new ReplayEmitter(size)
}

function extendBusIfNeeded (bus: EventBus) {
Expand All @@ -29,7 +31,7 @@ export function LocalEventBus (size = 5) {
return initBus(size)
}

export function GlobalEventBus (name: string, size: number, errorCallback: (error: any) => void): EventBus {
export function GlobalEventBus (name: string, size: number, errorCallback: (error: unknown) => void): EventBus {
try {
if (!window) {
errorCallback(new Error('Bus can only be attached to the window, which is not present'))
Expand Down
Loading