@croct/sdk 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache/cookieCache.cjs +31 -0
- package/cache/cookieCache.d.cts +8 -2
- package/cache/cookieCache.d.ts +8 -2
- package/cache/cookieCache.js +31 -0
- package/channel/encodedChannel.cjs +1 -1
- package/channel/encodedChannel.js +1 -1
- package/constants.cjs +1 -1
- package/constants.d.cts +2 -2
- package/constants.d.ts +2 -2
- package/constants.js +1 -1
- package/container.cjs +3 -2
- package/container.d.cts +2 -4
- package/container.d.ts +2 -4
- package/container.js +4 -9
- package/contentFetcher.d.cts +1 -0
- package/contentFetcher.d.ts +1 -0
- package/facade/contentFetcherFacade.d.cts +1 -1
- package/facade/contentFetcherFacade.d.ts +1 -1
- package/facade/sdkFacade.cjs +39 -53
- package/facade/sdkFacade.js +39 -53
- package/index.d.cts +1 -4
- package/index.d.ts +1 -4
- package/package.json +10 -8
- package/schema/ecommerceSchemas.cjs +4 -0
- package/schema/ecommerceSchemas.js +4 -0
- package/schema/sdkSchemas.cjs +0 -1
- package/schema/sdkSchemas.js +0 -1
- package/sdk.d.cts +1 -5
- package/sdk.d.ts +1 -5
- package/token/cachedTokenStore.cjs +1 -1
- package/token/cachedTokenStore.js +1 -1
- package/token/token.cjs +1 -1
- package/token/token.js +1 -1
- package/tracker.cjs +7 -21
- package/tracker.d.cts +8 -9
- package/tracker.d.ts +8 -9
- package/tracker.js +8 -25
- package/trackingEvents.d.cts +1 -0
- package/trackingEvents.d.ts +1 -0
- package/eventSubjectProcessor.cjs +0 -83
- package/eventSubjectProcessor.d.cts +0 -22
- package/eventSubjectProcessor.d.ts +0 -22
- package/eventSubjectProcessor.js +0 -60
package/cache/cookieCache.cjs
CHANGED
|
@@ -22,6 +22,7 @@ __export(cookieCache_exports, {
|
|
|
22
22
|
module.exports = __toCommonJS(cookieCache_exports);
|
|
23
23
|
class CookieCache {
|
|
24
24
|
constructor(config, defaultSecure = window.location.protocol === "https:") {
|
|
25
|
+
this.listeners = [];
|
|
25
26
|
this.config = {
|
|
26
27
|
...config,
|
|
27
28
|
path: config.path ?? "/"
|
|
@@ -52,6 +53,36 @@ class CookieCache {
|
|
|
52
53
|
maxAge: 0
|
|
53
54
|
});
|
|
54
55
|
}
|
|
56
|
+
static autoSync(cache) {
|
|
57
|
+
if (typeof window.cookieStore === "undefined") {
|
|
58
|
+
return () => {
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const listener = cache.sync.bind(cache);
|
|
62
|
+
window.cookieStore.addEventListener("change", listener);
|
|
63
|
+
return () => window.cookieStore.removeEventListener("change", listener);
|
|
64
|
+
}
|
|
65
|
+
addListener(listener) {
|
|
66
|
+
if (!this.listeners.includes(listener)) {
|
|
67
|
+
this.listeners.push(listener);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
removeListener(listener) {
|
|
71
|
+
const index = this.listeners.indexOf(listener);
|
|
72
|
+
if (index > -1) {
|
|
73
|
+
this.listeners.splice(index, 1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
sync(event) {
|
|
77
|
+
const isRelevant = [...event.changed, ...event.deleted].some((cookie) => cookie.name !== void 0 && CookieCache.decode(cookie.name) === this.config.name);
|
|
78
|
+
if (!isRelevant) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
this.notifyChange(this.get());
|
|
82
|
+
}
|
|
83
|
+
notifyChange(value) {
|
|
84
|
+
this.listeners.forEach((listener) => listener(value));
|
|
85
|
+
}
|
|
55
86
|
static serializeCookie(value, config) {
|
|
56
87
|
const cookie = [`${CookieCache.encode(config.name)}=${CookieCache.encode(value)}`];
|
|
57
88
|
if (config.maxAge !== void 0) {
|
package/cache/cookieCache.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ObservableCache, CacheListener } from './cache.cjs';
|
|
2
2
|
|
|
3
3
|
type CookieCacheConfiguration = {
|
|
4
4
|
name: string;
|
|
@@ -8,12 +8,18 @@ type CookieCacheConfiguration = {
|
|
|
8
8
|
path?: string;
|
|
9
9
|
sameSite?: 'strict' | 'lax' | 'none';
|
|
10
10
|
};
|
|
11
|
-
declare class CookieCache implements
|
|
11
|
+
declare class CookieCache implements ObservableCache {
|
|
12
12
|
private readonly config;
|
|
13
|
+
private readonly listeners;
|
|
13
14
|
constructor(config: CookieCacheConfiguration, defaultSecure?: boolean);
|
|
14
15
|
get(): string | null;
|
|
15
16
|
put(value: string): void;
|
|
16
17
|
clear(): void;
|
|
18
|
+
static autoSync(cache: CookieCache): () => void;
|
|
19
|
+
addListener(listener: CacheListener): void;
|
|
20
|
+
removeListener(listener: CacheListener): void;
|
|
21
|
+
private sync;
|
|
22
|
+
private notifyChange;
|
|
17
23
|
private static serializeCookie;
|
|
18
24
|
private static encode;
|
|
19
25
|
private static decode;
|
package/cache/cookieCache.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ObservableCache, CacheListener } from './cache.js';
|
|
2
2
|
|
|
3
3
|
type CookieCacheConfiguration = {
|
|
4
4
|
name: string;
|
|
@@ -8,12 +8,18 @@ type CookieCacheConfiguration = {
|
|
|
8
8
|
path?: string;
|
|
9
9
|
sameSite?: 'strict' | 'lax' | 'none';
|
|
10
10
|
};
|
|
11
|
-
declare class CookieCache implements
|
|
11
|
+
declare class CookieCache implements ObservableCache {
|
|
12
12
|
private readonly config;
|
|
13
|
+
private readonly listeners;
|
|
13
14
|
constructor(config: CookieCacheConfiguration, defaultSecure?: boolean);
|
|
14
15
|
get(): string | null;
|
|
15
16
|
put(value: string): void;
|
|
16
17
|
clear(): void;
|
|
18
|
+
static autoSync(cache: CookieCache): () => void;
|
|
19
|
+
addListener(listener: CacheListener): void;
|
|
20
|
+
removeListener(listener: CacheListener): void;
|
|
21
|
+
private sync;
|
|
22
|
+
private notifyChange;
|
|
17
23
|
private static serializeCookie;
|
|
18
24
|
private static encode;
|
|
19
25
|
private static decode;
|
package/cache/cookieCache.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
class CookieCache {
|
|
2
2
|
constructor(config, defaultSecure = window.location.protocol === "https:") {
|
|
3
|
+
this.listeners = [];
|
|
3
4
|
this.config = {
|
|
4
5
|
...config,
|
|
5
6
|
path: config.path ?? "/"
|
|
@@ -30,6 +31,36 @@ class CookieCache {
|
|
|
30
31
|
maxAge: 0
|
|
31
32
|
});
|
|
32
33
|
}
|
|
34
|
+
static autoSync(cache) {
|
|
35
|
+
if (typeof window.cookieStore === "undefined") {
|
|
36
|
+
return () => {
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const listener = cache.sync.bind(cache);
|
|
40
|
+
window.cookieStore.addEventListener("change", listener);
|
|
41
|
+
return () => window.cookieStore.removeEventListener("change", listener);
|
|
42
|
+
}
|
|
43
|
+
addListener(listener) {
|
|
44
|
+
if (!this.listeners.includes(listener)) {
|
|
45
|
+
this.listeners.push(listener);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
removeListener(listener) {
|
|
49
|
+
const index = this.listeners.indexOf(listener);
|
|
50
|
+
if (index > -1) {
|
|
51
|
+
this.listeners.splice(index, 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
sync(event) {
|
|
55
|
+
const isRelevant = [...event.changed, ...event.deleted].some((cookie) => cookie.name !== void 0 && CookieCache.decode(cookie.name) === this.config.name);
|
|
56
|
+
if (!isRelevant) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.notifyChange(this.get());
|
|
60
|
+
}
|
|
61
|
+
notifyChange(value) {
|
|
62
|
+
this.listeners.forEach((listener) => listener(value));
|
|
63
|
+
}
|
|
33
64
|
static serializeCookie(value, config) {
|
|
34
65
|
const cookie = [`${CookieCache.encode(config.name)}=${CookieCache.encode(value)}`];
|
|
35
66
|
if (config.maxAge !== void 0) {
|
|
@@ -26,7 +26,7 @@ class EncodedChannel {
|
|
|
26
26
|
this.encode = encoder;
|
|
27
27
|
}
|
|
28
28
|
publish(message) {
|
|
29
|
-
return this.encode(message).then((result) => this.channel.publish(result));
|
|
29
|
+
return this.encode(message).then(((result) => this.channel.publish(result)));
|
|
30
30
|
}
|
|
31
31
|
close() {
|
|
32
32
|
return this.channel.close();
|
|
@@ -4,7 +4,7 @@ class EncodedChannel {
|
|
|
4
4
|
this.encode = encoder;
|
|
5
5
|
}
|
|
6
6
|
publish(message) {
|
|
7
|
-
return this.encode(message).then((result) => this.channel.publish(result));
|
|
7
|
+
return this.encode(message).then(((result) => this.channel.publish(result)));
|
|
8
8
|
}
|
|
9
9
|
close() {
|
|
10
10
|
return this.channel.close();
|
package/constants.cjs
CHANGED
|
@@ -25,7 +25,7 @@ __export(constants_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(constants_exports);
|
|
26
26
|
const BASE_ENDPOINT_URL = "https://api.croct.io";
|
|
27
27
|
const MAX_QUERY_LENGTH = parseInt("<@maxQueryLength@>", 10);
|
|
28
|
-
const VERSION = "0.
|
|
28
|
+
const VERSION = "0.21.0";
|
|
29
29
|
const CLIENT_LIBRARY = `Croct SDK JS v${VERSION}`;
|
|
30
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
31
31
|
0 && (module.exports = {
|
package/constants.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare const BASE_ENDPOINT_URL = "https://api.croct.io";
|
|
2
2
|
declare const MAX_QUERY_LENGTH: number;
|
|
3
|
-
declare const VERSION = "0.
|
|
4
|
-
declare const CLIENT_LIBRARY = "Croct SDK JS v0.
|
|
3
|
+
declare const VERSION = "0.21.0";
|
|
4
|
+
declare const CLIENT_LIBRARY = "Croct SDK JS v0.21.0";
|
|
5
5
|
|
|
6
6
|
export { BASE_ENDPOINT_URL, CLIENT_LIBRARY, MAX_QUERY_LENGTH, VERSION };
|
package/constants.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare const BASE_ENDPOINT_URL = "https://api.croct.io";
|
|
2
2
|
declare const MAX_QUERY_LENGTH: number;
|
|
3
|
-
declare const VERSION = "0.
|
|
4
|
-
declare const CLIENT_LIBRARY = "Croct SDK JS v0.
|
|
3
|
+
declare const VERSION = "0.21.0";
|
|
4
|
+
declare const CLIENT_LIBRARY = "Croct SDK JS v0.21.0";
|
|
5
5
|
|
|
6
6
|
export { BASE_ENDPOINT_URL, CLIENT_LIBRARY, MAX_QUERY_LENGTH, VERSION };
|
package/constants.js
CHANGED
package/container.cjs
CHANGED
|
@@ -98,8 +98,7 @@ const _Container = class _Container {
|
|
|
98
98
|
inactivityRetryPolicy: new import_retry.ArbitraryPolicy([3e4, 3e4, 12e4, 12e4, 3e5, 3e5, 9e5]),
|
|
99
99
|
logger: this.getLogger("Tracker"),
|
|
100
100
|
channel: this.getBeaconChannel(),
|
|
101
|
-
eventMetadata: this.configuration.eventMetadata
|
|
102
|
-
processor: this.configuration.eventProcessor === void 0 ? void 0 : this.configuration.eventProcessor(this)
|
|
101
|
+
eventMetadata: this.configuration.eventMetadata
|
|
103
102
|
});
|
|
104
103
|
const queue = this.getBeaconQueue();
|
|
105
104
|
queue.addCallback("halfEmpty", tracker.unsuspend);
|
|
@@ -129,6 +128,8 @@ const _Container = class _Container {
|
|
|
129
128
|
const tabStorage = this.getSessionStorage();
|
|
130
129
|
if (browserCache instanceof import_cache.LocalStorageCache) {
|
|
131
130
|
this.removeTokenSyncListener = import_cache.LocalStorageCache.autoSync(browserCache);
|
|
131
|
+
} else if (browserCache instanceof import_cookieCache.CookieCache) {
|
|
132
|
+
this.removeTokenSyncListener = import_cookieCache.CookieCache.autoSync(browserCache);
|
|
132
133
|
}
|
|
133
134
|
return import_context.Context.load({
|
|
134
135
|
tokenScope: this.configuration.tokenScope,
|
package/container.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import { Logger } from './logging/logger.cjs';
|
|
|
2
2
|
import { TokenScope, Context } from './context.cjs';
|
|
3
3
|
import { MonitoredQueue } from './queue/monitoredQueue.cjs';
|
|
4
4
|
import { TokenStore } from './token/token.cjs';
|
|
5
|
-
import {
|
|
5
|
+
import { Tracker } from './tracker.cjs';
|
|
6
6
|
import { Evaluator } from './evaluator.cjs';
|
|
7
7
|
import { CidAssigner } from './cid/assigner.cjs';
|
|
8
8
|
import { EventManager } from './eventManager.cjs';
|
|
@@ -22,7 +22,6 @@ import './utilityTypes.cjs';
|
|
|
22
22
|
import './sourceLocation.cjs';
|
|
23
23
|
import '@croct/content-model/definition';
|
|
24
24
|
|
|
25
|
-
type DependencyResolver<T> = (container: Container) => T;
|
|
26
25
|
type Configuration = {
|
|
27
26
|
appId: string;
|
|
28
27
|
tokenScope: TokenScope;
|
|
@@ -45,7 +44,6 @@ type Configuration = {
|
|
|
45
44
|
eventMetadata?: {
|
|
46
45
|
[key: string]: string;
|
|
47
46
|
};
|
|
48
|
-
eventProcessor?: DependencyResolver<TrackingEventProcessor>;
|
|
49
47
|
defaultFetchTimeout?: number;
|
|
50
48
|
defaultPreferredLocale?: string;
|
|
51
49
|
};
|
|
@@ -93,4 +91,4 @@ declare class Container {
|
|
|
93
91
|
dispose(): Promise<void>;
|
|
94
92
|
}
|
|
95
93
|
|
|
96
|
-
export { type Configuration, Container
|
|
94
|
+
export { type Configuration, Container };
|
package/container.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Logger } from './logging/logger.js';
|
|
|
2
2
|
import { TokenScope, Context } from './context.js';
|
|
3
3
|
import { MonitoredQueue } from './queue/monitoredQueue.js';
|
|
4
4
|
import { TokenStore } from './token/token.js';
|
|
5
|
-
import {
|
|
5
|
+
import { Tracker } from './tracker.js';
|
|
6
6
|
import { Evaluator } from './evaluator.js';
|
|
7
7
|
import { CidAssigner } from './cid/assigner.js';
|
|
8
8
|
import { EventManager } from './eventManager.js';
|
|
@@ -22,7 +22,6 @@ import './utilityTypes.js';
|
|
|
22
22
|
import './sourceLocation.js';
|
|
23
23
|
import '@croct/content-model/definition';
|
|
24
24
|
|
|
25
|
-
type DependencyResolver<T> = (container: Container) => T;
|
|
26
25
|
type Configuration = {
|
|
27
26
|
appId: string;
|
|
28
27
|
tokenScope: TokenScope;
|
|
@@ -45,7 +44,6 @@ type Configuration = {
|
|
|
45
44
|
eventMetadata?: {
|
|
46
45
|
[key: string]: string;
|
|
47
46
|
};
|
|
48
|
-
eventProcessor?: DependencyResolver<TrackingEventProcessor>;
|
|
49
47
|
defaultFetchTimeout?: number;
|
|
50
48
|
defaultPreferredLocale?: string;
|
|
51
49
|
};
|
|
@@ -93,4 +91,4 @@ declare class Container {
|
|
|
93
91
|
dispose(): Promise<void>;
|
|
94
92
|
}
|
|
95
93
|
|
|
96
|
-
export { type Configuration, Container
|
|
94
|
+
export { type Configuration, Container };
|
package/container.js
CHANGED
|
@@ -11,13 +11,7 @@ import { CachedAssigner, RemoteAssigner, FixedAssigner } from "./cid/index.js";
|
|
|
11
11
|
import { SynchronousEventManager } from "./eventManager.js";
|
|
12
12
|
import { LocalStorageCache } from "./cache/index.js";
|
|
13
13
|
import { TimeStamper } from "./channel/guaranteedChannel.js";
|
|
14
|
-
import {
|
|
15
|
-
QueuedChannel,
|
|
16
|
-
RetryChannel,
|
|
17
|
-
GuaranteedChannel,
|
|
18
|
-
EncodedChannel,
|
|
19
|
-
SandboxChannel
|
|
20
|
-
} from "./channel/index.js";
|
|
14
|
+
import { QueuedChannel, RetryChannel, GuaranteedChannel, EncodedChannel, SandboxChannel } from "./channel/index.js";
|
|
21
15
|
import { ContentFetcher } from "./contentFetcher.js";
|
|
22
16
|
import { CookieCache } from "./cache/cookieCache.js";
|
|
23
17
|
import { FilteredLogger } from "./logging/filteredLogger.js";
|
|
@@ -82,8 +76,7 @@ const _Container = class _Container {
|
|
|
82
76
|
inactivityRetryPolicy: new ArbitraryPolicy([3e4, 3e4, 12e4, 12e4, 3e5, 3e5, 9e5]),
|
|
83
77
|
logger: this.getLogger("Tracker"),
|
|
84
78
|
channel: this.getBeaconChannel(),
|
|
85
|
-
eventMetadata: this.configuration.eventMetadata
|
|
86
|
-
processor: this.configuration.eventProcessor === void 0 ? void 0 : this.configuration.eventProcessor(this)
|
|
79
|
+
eventMetadata: this.configuration.eventMetadata
|
|
87
80
|
});
|
|
88
81
|
const queue = this.getBeaconQueue();
|
|
89
82
|
queue.addCallback("halfEmpty", tracker.unsuspend);
|
|
@@ -113,6 +106,8 @@ const _Container = class _Container {
|
|
|
113
106
|
const tabStorage = this.getSessionStorage();
|
|
114
107
|
if (browserCache instanceof LocalStorageCache) {
|
|
115
108
|
this.removeTokenSyncListener = LocalStorageCache.autoSync(browserCache);
|
|
109
|
+
} else if (browserCache instanceof CookieCache) {
|
|
110
|
+
this.removeTokenSyncListener = CookieCache.autoSync(browserCache);
|
|
116
111
|
}
|
|
117
112
|
return Context.load({
|
|
118
113
|
tokenScope: this.configuration.tokenScope,
|
package/contentFetcher.d.cts
CHANGED
package/contentFetcher.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JsonObject } from '@croct/json';
|
|
2
|
-
import {
|
|
2
|
+
import { ContentFetcher, FetchResponseOptions, FetchResponse } from '../contentFetcher.cjs';
|
|
3
3
|
import { ContextFactory } from './evaluatorFacade.cjs';
|
|
4
4
|
import { TokenProvider } from '../token/token.cjs';
|
|
5
5
|
import { CidAssigner } from '../cid/assigner.cjs';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JsonObject } from '@croct/json';
|
|
2
|
-
import {
|
|
2
|
+
import { ContentFetcher, FetchResponseOptions, FetchResponse } from '../contentFetcher.js';
|
|
3
3
|
import { ContextFactory } from './evaluatorFacade.js';
|
|
4
4
|
import { TokenProvider } from '../token/token.js';
|
|
5
5
|
import { CidAssigner } from '../cid/assigner.js';
|
package/facade/sdkFacade.cjs
CHANGED
|
@@ -29,7 +29,6 @@ var import_schema = require('../schema/index.cjs');
|
|
|
29
29
|
var import_sdk = require('../sdk.cjs');
|
|
30
30
|
var import_sessionFacade = require('./sessionFacade.cjs');
|
|
31
31
|
var import_contentFetcherFacade = require('./contentFetcherFacade.cjs');
|
|
32
|
-
var import_eventSubjectProcessor = require('../eventSubjectProcessor.cjs');
|
|
33
32
|
function validateConfiguration(configuration) {
|
|
34
33
|
try {
|
|
35
34
|
import_schema.sdkFacadeConfigurationSchema.validate(configuration);
|
|
@@ -47,37 +46,57 @@ class SdkFacade {
|
|
|
47
46
|
if (userId !== void 0 && token !== void 0) {
|
|
48
47
|
throw new Error("Either the user ID or token can be specified, but not both.");
|
|
49
48
|
}
|
|
50
|
-
const sdk =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
49
|
+
const sdk = import_sdk.Sdk.init({
|
|
50
|
+
...containerConfiguration,
|
|
51
|
+
tokenScope: containerConfiguration.tokenScope ?? "global",
|
|
52
|
+
debug: containerConfiguration.debug ?? false,
|
|
53
|
+
test: containerConfiguration.test ?? false,
|
|
54
|
+
disableCidMirroring: containerConfiguration.disableCidMirroring ?? false
|
|
55
|
+
});
|
|
56
|
+
const facade = new SdkFacade(sdk);
|
|
57
|
+
sdk.eventManager.addListener("tokenChanged", ({ oldToken, newToken }) => {
|
|
58
|
+
const oldSubject = oldToken?.getSubject() ?? null;
|
|
59
|
+
const newSubject = newToken?.getSubject() ?? null;
|
|
60
|
+
if (newSubject === oldSubject) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (oldToken !== null && oldSubject !== null) {
|
|
64
|
+
facade.trackInternalEvent(
|
|
65
|
+
{
|
|
66
|
+
type: "userSignedOut",
|
|
67
|
+
userId: oldSubject
|
|
68
|
+
},
|
|
69
|
+
oldToken
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (newSubject !== null) {
|
|
73
|
+
facade.trackInternalEvent({
|
|
74
|
+
type: "userSignedIn",
|
|
75
|
+
userId: newSubject
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
});
|
|
60
79
|
if (userId !== void 0) {
|
|
61
|
-
const currentToken =
|
|
80
|
+
const currentToken = facade.context.getToken();
|
|
62
81
|
const currentSubject = currentToken?.getSubject() ?? null;
|
|
63
82
|
if (currentSubject !== userId) {
|
|
64
83
|
if (userId === null) {
|
|
65
|
-
|
|
84
|
+
facade.unsetToken();
|
|
66
85
|
} else {
|
|
67
|
-
|
|
86
|
+
facade.identify(userId);
|
|
68
87
|
}
|
|
69
88
|
}
|
|
70
89
|
} else if (token !== void 0) {
|
|
71
90
|
if (token === null) {
|
|
72
|
-
|
|
91
|
+
facade.unsetToken();
|
|
73
92
|
} else {
|
|
74
|
-
|
|
93
|
+
facade.setToken(import_token.Token.parse(token));
|
|
75
94
|
}
|
|
76
95
|
}
|
|
77
96
|
if (track) {
|
|
78
|
-
|
|
97
|
+
facade.tracker.enable();
|
|
79
98
|
}
|
|
80
|
-
return
|
|
99
|
+
return facade;
|
|
81
100
|
}
|
|
82
101
|
get context() {
|
|
83
102
|
return this.sdk.context;
|
|
@@ -163,50 +182,17 @@ class SdkFacade {
|
|
|
163
182
|
if (currentToken !== null && currentToken.toString() === token.toString()) {
|
|
164
183
|
return;
|
|
165
184
|
}
|
|
166
|
-
const currentSubject = currentToken?.getSubject() ?? null;
|
|
167
|
-
const subject = token.getSubject();
|
|
168
|
-
const logger = this.getLogger();
|
|
169
|
-
if (subject === currentSubject) {
|
|
170
|
-
this.context.setToken(token);
|
|
171
|
-
logger.debug("Token refreshed");
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
if (currentSubject !== null) {
|
|
175
|
-
this.trackInternalEvent({
|
|
176
|
-
type: "userSignedOut",
|
|
177
|
-
userId: currentSubject
|
|
178
|
-
});
|
|
179
|
-
logger.info("User signed out");
|
|
180
|
-
}
|
|
181
185
|
this.context.setToken(token);
|
|
182
|
-
if (subject !== null) {
|
|
183
|
-
this.trackInternalEvent({
|
|
184
|
-
type: "userSignedIn",
|
|
185
|
-
userId: subject
|
|
186
|
-
});
|
|
187
|
-
logger.info(`User signed in as ${subject}`);
|
|
188
|
-
}
|
|
189
|
-
logger.debug("New token saved, ");
|
|
190
186
|
}
|
|
191
187
|
unsetToken() {
|
|
192
188
|
const token = this.getToken();
|
|
193
189
|
if (token === null) {
|
|
194
190
|
return;
|
|
195
191
|
}
|
|
196
|
-
const logger = this.getLogger();
|
|
197
|
-
const subject = token.getSubject();
|
|
198
|
-
if (subject !== null) {
|
|
199
|
-
this.trackInternalEvent({
|
|
200
|
-
type: "userSignedOut",
|
|
201
|
-
userId: subject
|
|
202
|
-
});
|
|
203
|
-
logger.info("User signed out");
|
|
204
|
-
}
|
|
205
192
|
this.context.setToken(null);
|
|
206
|
-
logger.debug("Token removed");
|
|
207
193
|
}
|
|
208
|
-
trackInternalEvent(event) {
|
|
209
|
-
this.sdk.tracker.track(event).catch(() => {
|
|
194
|
+
trackInternalEvent(event, token) {
|
|
195
|
+
this.sdk.tracker.track(event, { token }).catch(() => {
|
|
210
196
|
});
|
|
211
197
|
}
|
|
212
198
|
getLogger(...namespace) {
|
package/facade/sdkFacade.js
CHANGED
|
@@ -7,7 +7,6 @@ import { sdkFacadeConfigurationSchema } from "../schema/index.js";
|
|
|
7
7
|
import { Sdk } from "../sdk.js";
|
|
8
8
|
import { SessionFacade } from "./sessionFacade.js";
|
|
9
9
|
import { ContentFetcherFacade } from "./contentFetcherFacade.js";
|
|
10
|
-
import { EventSubjectProcessor } from "../eventSubjectProcessor.js";
|
|
11
10
|
function validateConfiguration(configuration) {
|
|
12
11
|
try {
|
|
13
12
|
sdkFacadeConfigurationSchema.validate(configuration);
|
|
@@ -25,37 +24,57 @@ class SdkFacade {
|
|
|
25
24
|
if (userId !== void 0 && token !== void 0) {
|
|
26
25
|
throw new Error("Either the user ID or token can be specified, but not both.");
|
|
27
26
|
}
|
|
28
|
-
const sdk =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
const sdk = Sdk.init({
|
|
28
|
+
...containerConfiguration,
|
|
29
|
+
tokenScope: containerConfiguration.tokenScope ?? "global",
|
|
30
|
+
debug: containerConfiguration.debug ?? false,
|
|
31
|
+
test: containerConfiguration.test ?? false,
|
|
32
|
+
disableCidMirroring: containerConfiguration.disableCidMirroring ?? false
|
|
33
|
+
});
|
|
34
|
+
const facade = new SdkFacade(sdk);
|
|
35
|
+
sdk.eventManager.addListener("tokenChanged", ({ oldToken, newToken }) => {
|
|
36
|
+
const oldSubject = oldToken?.getSubject() ?? null;
|
|
37
|
+
const newSubject = newToken?.getSubject() ?? null;
|
|
38
|
+
if (newSubject === oldSubject) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (oldToken !== null && oldSubject !== null) {
|
|
42
|
+
facade.trackInternalEvent(
|
|
43
|
+
{
|
|
44
|
+
type: "userSignedOut",
|
|
45
|
+
userId: oldSubject
|
|
46
|
+
},
|
|
47
|
+
oldToken
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if (newSubject !== null) {
|
|
51
|
+
facade.trackInternalEvent({
|
|
52
|
+
type: "userSignedIn",
|
|
53
|
+
userId: newSubject
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
38
57
|
if (userId !== void 0) {
|
|
39
|
-
const currentToken =
|
|
58
|
+
const currentToken = facade.context.getToken();
|
|
40
59
|
const currentSubject = currentToken?.getSubject() ?? null;
|
|
41
60
|
if (currentSubject !== userId) {
|
|
42
61
|
if (userId === null) {
|
|
43
|
-
|
|
62
|
+
facade.unsetToken();
|
|
44
63
|
} else {
|
|
45
|
-
|
|
64
|
+
facade.identify(userId);
|
|
46
65
|
}
|
|
47
66
|
}
|
|
48
67
|
} else if (token !== void 0) {
|
|
49
68
|
if (token === null) {
|
|
50
|
-
|
|
69
|
+
facade.unsetToken();
|
|
51
70
|
} else {
|
|
52
|
-
|
|
71
|
+
facade.setToken(Token.parse(token));
|
|
53
72
|
}
|
|
54
73
|
}
|
|
55
74
|
if (track) {
|
|
56
|
-
|
|
75
|
+
facade.tracker.enable();
|
|
57
76
|
}
|
|
58
|
-
return
|
|
77
|
+
return facade;
|
|
59
78
|
}
|
|
60
79
|
get context() {
|
|
61
80
|
return this.sdk.context;
|
|
@@ -141,50 +160,17 @@ class SdkFacade {
|
|
|
141
160
|
if (currentToken !== null && currentToken.toString() === token.toString()) {
|
|
142
161
|
return;
|
|
143
162
|
}
|
|
144
|
-
const currentSubject = currentToken?.getSubject() ?? null;
|
|
145
|
-
const subject = token.getSubject();
|
|
146
|
-
const logger = this.getLogger();
|
|
147
|
-
if (subject === currentSubject) {
|
|
148
|
-
this.context.setToken(token);
|
|
149
|
-
logger.debug("Token refreshed");
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
if (currentSubject !== null) {
|
|
153
|
-
this.trackInternalEvent({
|
|
154
|
-
type: "userSignedOut",
|
|
155
|
-
userId: currentSubject
|
|
156
|
-
});
|
|
157
|
-
logger.info("User signed out");
|
|
158
|
-
}
|
|
159
163
|
this.context.setToken(token);
|
|
160
|
-
if (subject !== null) {
|
|
161
|
-
this.trackInternalEvent({
|
|
162
|
-
type: "userSignedIn",
|
|
163
|
-
userId: subject
|
|
164
|
-
});
|
|
165
|
-
logger.info(`User signed in as ${subject}`);
|
|
166
|
-
}
|
|
167
|
-
logger.debug("New token saved, ");
|
|
168
164
|
}
|
|
169
165
|
unsetToken() {
|
|
170
166
|
const token = this.getToken();
|
|
171
167
|
if (token === null) {
|
|
172
168
|
return;
|
|
173
169
|
}
|
|
174
|
-
const logger = this.getLogger();
|
|
175
|
-
const subject = token.getSubject();
|
|
176
|
-
if (subject !== null) {
|
|
177
|
-
this.trackInternalEvent({
|
|
178
|
-
type: "userSignedOut",
|
|
179
|
-
userId: subject
|
|
180
|
-
});
|
|
181
|
-
logger.info("User signed out");
|
|
182
|
-
}
|
|
183
170
|
this.context.setToken(null);
|
|
184
|
-
logger.debug("Token removed");
|
|
185
171
|
}
|
|
186
|
-
trackInternalEvent(event) {
|
|
187
|
-
this.sdk.tracker.track(event).catch(() => {
|
|
172
|
+
trackInternalEvent(event, token) {
|
|
173
|
+
this.sdk.tracker.track(event, { token }).catch(() => {
|
|
188
174
|
});
|
|
189
175
|
}
|
|
190
176
|
getLogger(...namespace) {
|
package/index.d.cts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export { VERSION } from './constants.cjs';
|
|
2
2
|
export { Configuration, Sdk } from './sdk.cjs';
|
|
3
|
-
import './container.cjs';
|
|
4
|
-
import './logging/logger.cjs';
|
|
5
3
|
import './context.cjs';
|
|
6
4
|
import './token/token.cjs';
|
|
7
5
|
import '@croct/json';
|
|
@@ -10,8 +8,7 @@ import './cache/cache.cjs';
|
|
|
10
8
|
import './tab.cjs';
|
|
11
9
|
import './eventManager.cjs';
|
|
12
10
|
import './sdkEvents.cjs';
|
|
13
|
-
import './
|
|
14
|
-
import './queue/queue.cjs';
|
|
11
|
+
import './logging/logger.cjs';
|
|
15
12
|
import './tracker.cjs';
|
|
16
13
|
import './channel/channel.cjs';
|
|
17
14
|
import './retry/policy.cjs';
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export { VERSION } from './constants.js';
|
|
2
2
|
export { Configuration, Sdk } from './sdk.js';
|
|
3
|
-
import './container.js';
|
|
4
|
-
import './logging/logger.js';
|
|
5
3
|
import './context.js';
|
|
6
4
|
import './token/token.js';
|
|
7
5
|
import '@croct/json';
|
|
@@ -10,8 +8,7 @@ import './cache/cache.js';
|
|
|
10
8
|
import './tab.js';
|
|
11
9
|
import './eventManager.js';
|
|
12
10
|
import './sdkEvents.js';
|
|
13
|
-
import './
|
|
14
|
-
import './queue/queue.js';
|
|
11
|
+
import './logging/logger.js';
|
|
15
12
|
import './tracker.js';
|
|
16
13
|
import './channel/channel.js';
|
|
17
14
|
import './retry/policy.js';
|