Skip to content

Commit

Permalink
[CM-944] Expose internal interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Oct 5, 2023
1 parent 8d3747a commit 99f6b38
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 22 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./internal": {
"types": "./dist/internal.d.ts",
"require": "./dist/internal.js",
"import": "./dist/internal.mjs"
},
"./package.json": "./package.json"
},
"files": [
Expand Down
11 changes: 7 additions & 4 deletions rollup/dist.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const OUTPUT_DIR = './dist'

export default [
{
input: './src/index.ts',
input: ['./src/index.ts', './src/internal.ts'],
output: [
{
file: `${OUTPUT_DIR}/index.js`,
dir: `${OUTPUT_DIR}`,
format: 'cjs',
sourcemap: true
}
Expand All @@ -38,8 +38,11 @@ export default [
]
},
{
input: `${OUTPUT_DIR}/dts/src/index.d.ts`,
output: [{ file: `${OUTPUT_DIR}/index.d.ts`, format: 'es' }],
input: {
index: `${OUTPUT_DIR}/dts/src/index.d.ts`,
internal: `${OUTPUT_DIR}/dts/src/internal.d.ts`
},
output: [{ dir: OUTPUT_DIR, format: 'es' }],
plugins: [dts(), del({ targets: `${OUTPUT_DIR}/dts`, hook: 'buildEnd' })],
}
]
10 changes: 4 additions & 6 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ export class StorageHandlerBackedCache implements DurableCache {
return null
}

let _meta
let meta: RecordMetadata
try {
_meta = this.parseMetaRecord(metaRecord)
meta = this.parseMetaRecord(metaRecord)
} catch (e) {
this.eventBus.emitErrorWithMessage('Cache', 'Failed reading meta from cookies', e)
// delete this so we don't keep trying to read it
this.deleteCookie(key)
this.deleteCookie(metaRecordKey)
return null
}
const meta = _meta!

const expiresAt = meta.expiresAt
if (expiresAt && expiresAt.getTime() <= Date.now()) {
Expand All @@ -123,16 +122,15 @@ export class StorageHandlerBackedCache implements DurableCache {
return null
}

let _meta
let meta: RecordMetadata
try {
_meta = this.parseMetaRecord(metaRecord)
meta = this.parseMetaRecord(metaRecord)
} catch (e) {
this.eventBus.emitErrorWithMessage('Cache', 'Failed reading meta from ls', e)
this.handler.removeDataFromLocalStorage(key)
this.handler.removeDataFromLocalStorage(metaRecordKey)
return null
}
const meta = _meta!

const expiresAt = meta.expiresAt
if (expiresAt && expiresAt.getTime() <= Date.now()) {
Expand Down
15 changes: 12 additions & 3 deletions src/handlers/storage-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StorageStrategies, StorageStrategy } from '../model/storage-strategy'
import { EventBus, ReadOnlyStorageHandler, StorageHandler, strEqualsIgnoreCase } from 'live-connect-common'
import { WrappingContext } from '../utils/wrapping'
import { Wrapped, WrappingContext } from '../utils/wrapping'

const noop = () => undefined

Expand All @@ -13,7 +13,11 @@ function wrapWrite<T extends object, K extends keyof T & string>(wrapper: Wrappi
}

export class WrappedReadOnlyStorageHandler implements ReadOnlyStorageHandler {
private minimalFunctions
private minimalFunctions: {
getCookie: Wrapped<ReadOnlyStorageHandler['getCookie']>,
getDataFromLocalStorage: Wrapped<ReadOnlyStorageHandler['getDataFromLocalStorage']>,
localStorageIsEnabled: Wrapped<ReadOnlyStorageHandler['localStorageIsEnabled']>,
}

protected constructor (storageStrategy: StorageStrategy, wrapper: WrappingContext<ReadOnlyStorageHandler>) {
this.minimalFunctions = {
Expand Down Expand Up @@ -45,7 +49,12 @@ export class WrappedReadOnlyStorageHandler implements ReadOnlyStorageHandler {

export class WrappedStorageHandler extends WrappedReadOnlyStorageHandler implements StorageHandler {
storageStrategy: StorageStrategy
private functions
private functions: {
setCookie: Wrapped<StorageHandler['setCookie']>,
removeDataFromLocalStorage: Wrapped<StorageHandler['removeDataFromLocalStorage']>,
setDataInLocalStorage: Wrapped<StorageHandler['setDataInLocalStorage']>,
findSimilarCookies: Wrapped<StorageHandler['findSimilarCookies']>,
}

protected constructor (storageStrategy: StorageStrategy, wrapper: WrappingContext<StorageHandler>) {
super(storageStrategy, wrapper)
Expand Down
3 changes: 3 additions & 0 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// no backwards compatibility guarantees for internal APIs
export { DurableCache, StorageHandlerBackedCache } from './cache'
export { WrappedStorageHandler } from './handlers/storage-handler'
3 changes: 2 additions & 1 deletion src/minimal-live-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { WrappedCallHandler } from './handlers/call-handler'
import { enrichIdentifiers } from './enrichers/identifiers-nohash'

// @ts-ignore
function _minimalInitialization(liveConnectConfig: LiveConnectConfig, externalStorageHandler: ReadOnlyStorageHandler, externalCallHandler: CallHandler, eventBus: EventBus, push: (event: unknown) => void): ILiveConnect {
function _minimalInitialization(liveConnectConfig: LiveConnectConfig, externalStorageHandler: ReadOnlyStorageHandler, externalCallHandler: CallHandler, eventBus: EventBus, push: (...events: unknown[]) => void): ILiveConnect {
try {
const validLiveConnectConfig = {
...removeInvalidPairs(liveConnectConfig, eventBus),
Expand Down Expand Up @@ -65,6 +65,7 @@ function _initializeWithGlobalName(liveConnectConfig: LiveConnectConfig, externa
const lc = _minimalInitialization(liveConnectConfig, externalStorageHandler, externalCallHandler, eventBus, push)

window.liQ_instances = window.liQ_instances || []
// @ts-ignore
if (window.liQ_instances.filter(i => i.config.globalVarName === lc.config.globalVarName).length === 0) {
window.liQ_instances.push(lc)
}
Expand Down
4 changes: 3 additions & 1 deletion src/standard-live-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ function _standardInitialization(liveConnectConfig: LiveConnectConfig, externalS
resolutionCallUrl: resolver.getUrl.bind(resolver),
config: validLiveConnectConfig,
eventBus,
storageHandler
storageHandler,
cache
}
} catch (x) {
console.error(x)
Expand Down Expand Up @@ -176,6 +177,7 @@ function _initializeWithGlobalName(liveConnectConfig: LiveConnectConfig, externa
window[lc.config.globalVarName] = lc

window.liQ_instances = window.liQ_instances || []
// @ts-ignore
if (window.liQ_instances.filter(i => i.config.globalVarName === lc.config.globalVarName).length === 0) {
window.liQ_instances.push(lc)
}
Expand Down
10 changes: 6 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DurableCache } from './cache'
import { WrappedStorageHandler } from './handlers/storage-handler'
import { ResolutionMetadata } from './idex'
import { StorageStrategy } from './model/storage-strategy'
Expand Down Expand Up @@ -96,17 +97,18 @@ export interface EventBus extends ErrorBus {
}

export interface ILiveConnect {
ready: boolean
push: (event: unknown) => void
fire: () => void
ready?: boolean
push: (...event: unknown[]) => void
fire?: () => void
resolve?: (
successCallBack: (result: IdentityResultionResult, meta: ResolutionMetadata) => void,
errorCallBack: () => void,
additionalParams?: ResolutionParams
) => void
resolutionCallUrl?: (additionalParams: ResolutionParams) => string
peopleVerifiedId?: string
config: LiveConnectConfig
config?: LiveConnectConfig
eventBus?: EventBus,
storageHandler?: WrappedStorageHandler,
cache?: DurableCache,
}
2 changes: 1 addition & 1 deletion src/typings/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare global {
interface Window {
// eslint-disable-next-line camelcase
liQ_instances?: ILiveConnect[]
liQ?: ILiveConnect
liQ?: ILiveConnect | unknown[]
XDomainRequest?: { new(): XDomainRequest; prototype: XDomainRequest; create(): XDomainRequest }; // for IE compat
[k: string]: unknown // allow accessing arbitrary fields
msCrypto: Crypto
Expand Down
4 changes: 2 additions & 2 deletions src/utils/wrapping.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventBus } from '../types'
import { isFunction, isObject } from 'live-connect-common'

export type NoopFunction = () => undefined
export type Wrapped<T> = NonNullable<T> | (() => undefined)

const noop = () => undefined

Expand All @@ -18,7 +18,7 @@ export class WrappingContext<T extends object> {
this.eventBus = eventBus
}

wrap<K extends keyof T & string>(functionName: K): NonNullable<T[K]> | NoopFunction {
wrap<K extends keyof T & string>(functionName: K): Wrapped<T[K]> {
if (isObject(this.obj)) {
const member = this.obj[functionName]
if (isFunction(member)) {
Expand Down

0 comments on commit 99f6b38

Please sign in to comment.