@croct/sdk 0.20.0 → 0.20.1

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.
@@ -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) {
@@ -1,4 +1,4 @@
1
- import { Cache } from './cache.cjs';
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 Cache {
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;
@@ -1,4 +1,4 @@
1
- import { Cache } from './cache.js';
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 Cache {
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;
@@ -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.20.0";
28
+ const VERSION = "0.20.1";
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.20.0";
4
- declare const CLIENT_LIBRARY = "Croct SDK JS v0.20.0";
3
+ declare const VERSION = "0.20.1";
4
+ declare const CLIENT_LIBRARY = "Croct SDK JS v0.20.1";
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.20.0";
4
- declare const CLIENT_LIBRARY = "Croct SDK JS v0.20.0";
3
+ declare const VERSION = "0.20.1";
4
+ declare const CLIENT_LIBRARY = "Croct SDK JS v0.20.1";
5
5
 
6
6
  export { BASE_ENDPOINT_URL, CLIENT_LIBRARY, MAX_QUERY_LENGTH, VERSION };
package/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const BASE_ENDPOINT_URL = "https://api.croct.io";
2
2
  const MAX_QUERY_LENGTH = parseInt("<@maxQueryLength@>", 10);
3
- const VERSION = "0.20.0";
3
+ const VERSION = "0.20.1";
4
4
  const CLIENT_LIBRARY = `Croct SDK JS v${VERSION}`;
5
5
  export {
6
6
  BASE_ENDPOINT_URL,
package/container.cjs CHANGED
@@ -129,6 +129,8 @@ const _Container = class _Container {
129
129
  const tabStorage = this.getSessionStorage();
130
130
  if (browserCache instanceof import_cache.LocalStorageCache) {
131
131
  this.removeTokenSyncListener = import_cache.LocalStorageCache.autoSync(browserCache);
132
+ } else if (browserCache instanceof import_cookieCache.CookieCache) {
133
+ this.removeTokenSyncListener = import_cookieCache.CookieCache.autoSync(browserCache);
132
134
  }
133
135
  return import_context.Context.load({
134
136
  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 { TrackingEventProcessor, Tracker } from './tracker.cjs';
5
+ import { Tracker, TrackingEventProcessor } 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';
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 { TrackingEventProcessor, Tracker } from './tracker.js';
5
+ import { Tracker, TrackingEventProcessor } 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';
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";
@@ -113,6 +107,8 @@ const _Container = class _Container {
113
107
  const tabStorage = this.getSessionStorage();
114
108
  if (browserCache instanceof LocalStorageCache) {
115
109
  this.removeTokenSyncListener = LocalStorageCache.autoSync(browserCache);
110
+ } else if (browserCache instanceof CookieCache) {
111
+ this.removeTokenSyncListener = CookieCache.autoSync(browserCache);
116
112
  }
117
113
  return Context.load({
118
114
  tokenScope: this.configuration.tokenScope,
@@ -1,5 +1,5 @@
1
1
  import { JsonObject } from '@croct/json';
2
- import { FetchResponseOptions, ContentFetcher, FetchResponse } from '../contentFetcher.cjs';
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 { FetchResponseOptions, ContentFetcher, FetchResponse } from '../contentFetcher.js';
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@croct/sdk",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "description": "Croct SDK for JavaScript.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -80,20 +80,22 @@
80
80
  "postbuild": "./post-build.mjs"
81
81
  },
82
82
  "dependencies": {
83
- "@croct/json": "^2.0.1",
84
83
  "@croct/content-model": "^0.21.1",
84
+ "@croct/json": "^2.0.1",
85
85
  "tslib": "^2.5.0"
86
86
  },
87
87
  "devDependencies": {
88
- "esbuild-fix-imports-plugin": "^1.0.19",
89
- "@croct/eslint-plugin": "^0.7.0",
88
+ "@croct/eslint-plugin": "^0.8.3",
89
+ "@eslint/eslintrc": "^3.3.3",
90
+ "@fetch-mock/jest": "^0.2.20",
91
+ "@typescript-eslint/parser": "^8.53.0",
90
92
  "@types/jest": "^30.0.0",
91
- "eslint": "^8.57.0",
92
- "fetch-mock": "^9.11.0",
93
+ "esbuild-fix-imports-plugin": "^1.0.19",
94
+ "eslint": "^9.39.0",
95
+ "fetch-mock": "^12.6.0",
93
96
  "jest": "^30.0.0",
94
97
  "jest-environment-jsdom": "^30.0.0",
95
- "jest-extended": "^6.0.0",
96
- "node-fetch": "^2.6.7",
98
+ "jest-extended": "^7.0.0",
97
99
  "ts-jest": "^29.0.3",
98
100
  "tsup": "^8.4.0",
99
101
  "typescript": "^5.0.0"
@@ -32,7 +32,7 @@ class CachedTokenStore {
32
32
  }
33
33
  try {
34
34
  return import_token.Token.parse(data);
35
- } catch (error) {
35
+ } catch {
36
36
  return null;
37
37
  }
38
38
  }
@@ -10,7 +10,7 @@ class CachedTokenStore {
10
10
  }
11
11
  try {
12
12
  return Token.parse(data);
13
- } catch (error) {
13
+ } catch {
14
14
  return null;
15
15
  }
16
16
  }
package/token/token.cjs CHANGED
@@ -64,7 +64,7 @@ const _Token = class _Token {
64
64
  try {
65
65
  headers = JSON.parse((0, import_base64Url.base64UrlDecode)(parts[0], true));
66
66
  payload = JSON.parse((0, import_base64Url.base64UrlDecode)(parts[1], true));
67
- } catch (error) {
67
+ } catch {
68
68
  throw new Error("The token is corrupted.");
69
69
  }
70
70
  return _Token.of(headers, payload, parts[2]);
package/token/token.js CHANGED
@@ -41,7 +41,7 @@ const _Token = class _Token {
41
41
  try {
42
42
  headers = JSON.parse(base64UrlDecode(parts[0], true));
43
43
  payload = JSON.parse(base64UrlDecode(parts[1], true));
44
- } catch (error) {
44
+ } catch {
45
45
  throw new Error("The token is corrupted.");
46
46
  }
47
47
  return _Token.of(headers, payload, parts[2]);
package/tracker.d.cts CHANGED
@@ -2,8 +2,8 @@ import { Logger } from './logging/logger.cjs';
2
2
  import { Tab } from './tab.cjs';
3
3
  import { OutputChannel } from './channel/channel.cjs';
4
4
  import { RetryPolicy } from './retry/policy.cjs';
5
- import { Token, TokenProvider } from './token/token.cjs';
6
- import { TrackingEvent, TrackingEventContext, Beacon, PartialTrackingEvent } from './trackingEvents.cjs';
5
+ import { TokenProvider, Token } from './token/token.cjs';
6
+ import { Beacon, TrackingEvent, TrackingEventContext, PartialTrackingEvent } from './trackingEvents.cjs';
7
7
  import './eventManager.cjs';
8
8
  import '@croct/json';
9
9
  import './apiKey.cjs';
package/tracker.d.ts CHANGED
@@ -2,8 +2,8 @@ import { Logger } from './logging/logger.js';
2
2
  import { Tab } from './tab.js';
3
3
  import { OutputChannel } from './channel/channel.js';
4
4
  import { RetryPolicy } from './retry/policy.js';
5
- import { Token, TokenProvider } from './token/token.js';
6
- import { TrackingEvent, TrackingEventContext, Beacon, PartialTrackingEvent } from './trackingEvents.js';
5
+ import { TokenProvider, Token } from './token/token.js';
6
+ import { Beacon, TrackingEvent, TrackingEventContext, PartialTrackingEvent } from './trackingEvents.js';
7
7
  import './eventManager.js';
8
8
  import '@croct/json';
9
9
  import './apiKey.js';
package/tracker.js CHANGED
@@ -1,9 +1,6 @@
1
1
  import { NullLogger } from "./logging/index.js";
2
2
  import { formatCause } from "./error.js";
3
- import {
4
- isCartPartialEvent,
5
- isIdentifiedUserEvent
6
- } from "./trackingEvents.js";
3
+ import { isCartPartialEvent, isIdentifiedUserEvent } from "./trackingEvents.js";
7
4
  const trackedEvents = {};
8
5
  class Tracker {
9
6
  constructor(config) {