@croct/sdk 0.10.0 → 0.11.0-alpha.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.
Files changed (231) hide show
  1. package/.src/activeRecord.ts +150 -0
  2. package/.src/base64Url.ts +18 -0
  3. package/.src/cache/cache.ts +15 -0
  4. package/.src/cache/fallbackCache.ts +29 -0
  5. package/.src/cache/inMemoryCache.ts +21 -0
  6. package/.src/cache/index.ts +4 -0
  7. package/.src/cache/localStorageCache.ts +85 -0
  8. package/.src/channel/beaconSocketChannel.ts +153 -0
  9. package/.src/channel/channel.ts +20 -0
  10. package/.src/channel/encodedChannel.ts +21 -0
  11. package/.src/channel/guaranteedChannel.ts +131 -0
  12. package/.src/channel/index.ts +8 -0
  13. package/.src/channel/queuedChannel.ts +112 -0
  14. package/.src/channel/retryChannel.ts +90 -0
  15. package/.src/channel/sandboxChannel.ts +43 -0
  16. package/.src/channel/socketChannel.ts +217 -0
  17. package/.src/cid/assigner.ts +3 -0
  18. package/.src/cid/cachedAssigner.ts +35 -0
  19. package/.src/cid/fixedAssigner.ts +13 -0
  20. package/.src/cid/index.ts +4 -0
  21. package/.src/cid/remoteAssigner.ts +47 -0
  22. package/.src/constants.ts +6 -0
  23. package/.src/container.ts +388 -0
  24. package/.src/contentFetcher.ts +226 -0
  25. package/.src/context.ts +137 -0
  26. package/.src/error.ts +31 -0
  27. package/.src/evaluator.ts +251 -0
  28. package/.src/eventManager.ts +53 -0
  29. package/.src/facade/contentFetcherFacade.ts +69 -0
  30. package/.src/facade/evaluatorFacade.ts +152 -0
  31. package/.src/facade/index.ts +7 -0
  32. package/.src/facade/sdkFacade.ts +291 -0
  33. package/.src/facade/sessionFacade.ts +14 -0
  34. package/.src/facade/sessionPatch.ts +32 -0
  35. package/.src/facade/trackerFacade.ts +98 -0
  36. package/.src/facade/userFacade.ts +26 -0
  37. package/.src/facade/userPatch.ts +32 -0
  38. package/.src/index.ts +4 -0
  39. package/.src/logging/consoleLogger.ts +37 -0
  40. package/.src/logging/index.ts +4 -0
  41. package/.src/logging/logger.ts +13 -0
  42. package/.src/logging/namespacedLogger.ts +32 -0
  43. package/.src/logging/nullLogger.ts +19 -0
  44. package/.src/namespacedStorage.ts +69 -0
  45. package/.src/patch.ts +64 -0
  46. package/.src/queue/capacityRestrictedQueue.ts +44 -0
  47. package/.src/queue/inMemoryQueue.ts +43 -0
  48. package/.src/queue/index.ts +5 -0
  49. package/.src/queue/monitoredQueue.ts +168 -0
  50. package/.src/queue/persistentQueue.ts +84 -0
  51. package/.src/queue/queue.ts +15 -0
  52. package/.src/retry/arbitraryPolicy.ts +21 -0
  53. package/.src/retry/backoffPolicy.ts +84 -0
  54. package/.src/retry/index.ts +5 -0
  55. package/.src/retry/maxAttemptsPolicy.ts +28 -0
  56. package/.src/retry/neverPolicy.ts +11 -0
  57. package/.src/retry/policy.ts +5 -0
  58. package/.src/schema/attributeSchema.ts +6 -0
  59. package/.src/schema/contentFetcherSchemas.ts +23 -0
  60. package/.src/schema/contentSchemas.ts +44 -0
  61. package/.src/schema/contextSchemas.ts +5 -0
  62. package/.src/schema/ecommerceSchemas.ts +179 -0
  63. package/.src/schema/evaluatorSchemas.ts +11 -0
  64. package/.src/schema/eventSchemas.ts +150 -0
  65. package/.src/schema/index.ts +11 -0
  66. package/.src/schema/loggerSchema.ts +12 -0
  67. package/.src/schema/operationSchemas.ts +102 -0
  68. package/.src/schema/sdkFacadeSchemas.ts +44 -0
  69. package/.src/schema/sdkSchemas.ts +49 -0
  70. package/.src/schema/tokenSchema.ts +42 -0
  71. package/.src/schema/userSchema.ts +184 -0
  72. package/.src/sdk.ts +174 -0
  73. package/.src/sdkEvents.ts +15 -0
  74. package/.src/sourceLocation.ts +85 -0
  75. package/.src/tab.ts +148 -0
  76. package/.src/token/cachedTokenStore.ts +34 -0
  77. package/.src/token/inMemoryTokenStore.ts +13 -0
  78. package/.src/token/index.ts +4 -0
  79. package/.src/token/replicatedTokenStore.ts +21 -0
  80. package/.src/token/token.ts +164 -0
  81. package/.src/tracker.ts +460 -0
  82. package/.src/trackingEvents.ts +456 -0
  83. package/.src/transformer.ts +7 -0
  84. package/.src/utilityTypes.ts +3 -0
  85. package/.src/uuid.ts +43 -0
  86. package/.src/validation/arrayType.ts +71 -0
  87. package/.src/validation/booleanType.ts +22 -0
  88. package/.src/validation/functionType.ts +22 -0
  89. package/.src/validation/index.ts +12 -0
  90. package/.src/validation/jsonType.ts +157 -0
  91. package/.src/validation/mixedSchema.ts +7 -0
  92. package/.src/validation/nullType.ts +22 -0
  93. package/.src/validation/numberType.ts +59 -0
  94. package/.src/validation/objectType.ts +138 -0
  95. package/.src/validation/schema.ts +21 -0
  96. package/.src/validation/stringType.ts +118 -0
  97. package/.src/validation/unionType.ts +53 -0
  98. package/.src/validation/violation.ts +23 -0
  99. package/activeRecord.js +33 -36
  100. package/base64Url.js +1 -0
  101. package/cache/cache.js +1 -0
  102. package/cache/fallbackCache.js +16 -32
  103. package/cache/inMemoryCache.js +10 -10
  104. package/cache/index.js +2 -1
  105. package/cache/localStorageCache.js +25 -25
  106. package/channel/beaconSocketChannel.d.ts +1 -1
  107. package/channel/beaconSocketChannel.js +50 -79
  108. package/channel/channel.d.ts +1 -1
  109. package/channel/channel.js +1 -0
  110. package/channel/encodedChannel.js +9 -10
  111. package/channel/guaranteedChannel.d.ts +4 -4
  112. package/channel/guaranteedChannel.js +42 -43
  113. package/channel/index.js +2 -1
  114. package/channel/queuedChannel.js +36 -64
  115. package/channel/retryChannel.d.ts +1 -1
  116. package/channel/retryChannel.js +45 -77
  117. package/channel/sandboxChannel.js +18 -18
  118. package/channel/socketChannel.d.ts +4 -4
  119. package/channel/socketChannel.js +78 -79
  120. package/cid/assigner.js +1 -0
  121. package/cid/cachedAssigner.js +16 -27
  122. package/cid/fixedAssigner.js +6 -6
  123. package/cid/index.js +2 -1
  124. package/cid/remoteAssigner.js +24 -36
  125. package/constants.d.ts +6 -5
  126. package/constants.js +7 -5
  127. package/container.d.ts +13 -6
  128. package/container.js +153 -168
  129. package/contentFetcher.d.ts +59 -0
  130. package/contentFetcher.js +130 -0
  131. package/context.d.ts +3 -3
  132. package/context.js +37 -38
  133. package/error.js +3 -2
  134. package/evaluator.d.ts +33 -24
  135. package/evaluator.js +127 -117
  136. package/eventManager.d.ts +1 -1
  137. package/eventManager.js +15 -15
  138. package/facade/contentFetcherFacade.d.ts +27 -0
  139. package/facade/contentFetcherFacade.js +41 -0
  140. package/facade/evaluatorFacade.d.ts +13 -3
  141. package/facade/evaluatorFacade.js +58 -72
  142. package/facade/index.js +1 -0
  143. package/facade/sdkFacade.d.ts +10 -3
  144. package/facade/sdkFacade.js +130 -141
  145. package/facade/sessionFacade.js +7 -7
  146. package/facade/sessionPatch.js +10 -13
  147. package/facade/trackerFacade.js +33 -38
  148. package/facade/userFacade.js +11 -11
  149. package/facade/userPatch.js +10 -13
  150. package/index.js +3 -2
  151. package/logging/consoleLogger.js +19 -35
  152. package/logging/index.js +2 -1
  153. package/logging/logger.js +1 -0
  154. package/logging/namespacedLogger.js +15 -15
  155. package/logging/nullLogger.js +11 -13
  156. package/namespacedStorage.js +31 -47
  157. package/package.json +13 -16
  158. package/patch.d.ts +1 -1
  159. package/patch.js +1 -0
  160. package/queue/capacityRestrictedQueue.js +18 -18
  161. package/queue/inMemoryQueue.js +23 -28
  162. package/queue/index.js +2 -1
  163. package/queue/monitoredQueue.d.ts +2 -2
  164. package/queue/monitoredQueue.js +40 -40
  165. package/queue/persistentQueue.js +34 -38
  166. package/queue/queue.js +1 -0
  167. package/retry/arbitraryPolicy.js +9 -10
  168. package/retry/backoffPolicy.d.ts +1 -1
  169. package/retry/backoffPolicy.js +12 -13
  170. package/retry/index.js +2 -1
  171. package/retry/maxAttemptsPolicy.js +8 -8
  172. package/retry/neverPolicy.js +7 -9
  173. package/retry/policy.js +1 -0
  174. package/schema/attributeSchema.js +2 -1
  175. package/schema/contentFetcherSchemas.d.ts +2 -0
  176. package/schema/contentFetcherSchemas.js +23 -0
  177. package/schema/contentSchemas.js +2 -1
  178. package/schema/contextSchemas.js +2 -1
  179. package/schema/ecommerceSchemas.js +2 -1
  180. package/schema/evaluatorSchemas.d.ts +2 -0
  181. package/schema/{evaluationSchemas.js → evaluatorSchemas.js} +4 -3
  182. package/schema/eventSchemas.js +6 -7
  183. package/schema/index.d.ts +2 -1
  184. package/schema/index.js +4 -2
  185. package/schema/loggerSchema.js +2 -1
  186. package/schema/operationSchemas.js +9 -8
  187. package/schema/sdkFacadeSchemas.js +10 -6
  188. package/schema/sdkSchemas.js +9 -5
  189. package/schema/tokenSchema.js +6 -4
  190. package/schema/userSchema.js +3 -2
  191. package/sdk.d.ts +9 -3
  192. package/sdk.js +82 -127
  193. package/sdkEvents.d.ts +3 -3
  194. package/sdkEvents.js +1 -0
  195. package/sourceLocation.d.ts +3 -3
  196. package/sourceLocation.js +14 -14
  197. package/tab.d.ts +5 -5
  198. package/tab.js +51 -80
  199. package/token/cachedTokenStore.js +10 -10
  200. package/token/inMemoryTokenStore.js +8 -8
  201. package/token/index.js +2 -1
  202. package/token/replicatedTokenStore.js +8 -8
  203. package/token/token.d.ts +9 -5
  204. package/token/token.js +64 -57
  205. package/tracker.d.ts +4 -4
  206. package/tracker.js +146 -122
  207. package/trackingEvents.d.ts +36 -36
  208. package/trackingEvents.js +13 -6
  209. package/transformer.js +2 -1
  210. package/utilityTypes.d.ts +2 -2
  211. package/utilityTypes.js +1 -0
  212. package/uuid.js +10 -7
  213. package/validation/arrayType.d.ts +2 -2
  214. package/validation/arrayType.js +30 -27
  215. package/validation/booleanType.js +12 -15
  216. package/validation/functionType.js +12 -15
  217. package/validation/index.js +2 -1
  218. package/validation/jsonType.d.ts +2 -2
  219. package/validation/jsonType.js +62 -80
  220. package/validation/mixedSchema.js +5 -7
  221. package/validation/nullType.js +12 -15
  222. package/validation/numberType.d.ts +1 -1
  223. package/validation/numberType.js +24 -22
  224. package/validation/objectType.d.ts +1 -1
  225. package/validation/objectType.js +62 -72
  226. package/validation/schema.js +7 -10
  227. package/validation/stringType.d.ts +1 -1
  228. package/validation/stringType.js +37 -47
  229. package/validation/unionType.js +28 -77
  230. package/validation/violation.js +2 -2
  231. package/schema/evaluationSchemas.d.ts +0 -2
package/tab.js CHANGED
@@ -1,125 +1,96 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Tab = void 0;
4
- var eventManager_1 = require("./eventManager");
5
- var EventMap = {
4
+ const eventManager_1 = require("./eventManager");
5
+ const EventMap = {
6
6
  focus: 'focus',
7
7
  blur: 'blur',
8
8
  beforeunload: 'unload',
9
9
  DOMContentLoaded: 'load',
10
10
  visibilitychange: 'visibilityChange',
11
11
  };
12
- var Tab = /** @class */ (function () {
13
- function Tab(id, isNew, urlSanitizer) {
12
+ class Tab {
13
+ constructor(id, isNew, urlSanitizer) {
14
14
  this.eventManager = new eventManager_1.SynchronousEventManager();
15
15
  this.id = id;
16
16
  this.isNew = isNew;
17
17
  this.urlSanitizer = urlSanitizer;
18
18
  this.initialize();
19
19
  }
20
- Tab.prototype.initialize = function () {
21
- var _this = this;
22
- var listener = function (event) {
23
- _this.emit(EventMap[event.type], new CustomEvent(EventMap[event.type], { detail: { tab: _this } }));
20
+ initialize() {
21
+ const listener = (event) => {
22
+ this.emit(EventMap[event.type], new CustomEvent(EventMap[event.type], { detail: { tab: this } }));
24
23
  };
25
24
  window.addEventListener('focus', listener, true);
26
25
  window.addEventListener('blur', listener, true);
27
26
  window.addEventListener('beforeunload', listener, true);
28
27
  window.addEventListener('DOMContentLoaded', listener, true);
29
- document.addEventListener('visibilitychange', function () {
30
- _this.emit('visibilityChange', new CustomEvent('visibilityChange', {
28
+ document.addEventListener('visibilitychange', () => {
29
+ this.emit('visibilityChange', new CustomEvent('visibilityChange', {
31
30
  detail: {
32
- tab: _this,
33
- visible: _this.isVisible,
31
+ tab: this,
32
+ visible: this.isVisible,
34
33
  },
35
34
  }));
36
35
  }, true);
37
- Tab.addUrlChangeListener(function (url) {
38
- _this.emit('urlChange', new CustomEvent('urlChange', { detail: { tab: _this, url: _this.sanitizeUrl(url) } }));
36
+ Tab.addUrlChangeListener(url => {
37
+ this.emit('urlChange', new CustomEvent('urlChange', { detail: { tab: this, url: this.sanitizeUrl(url) } }));
39
38
  });
40
- };
41
- Object.defineProperty(Tab.prototype, "url", {
42
- get: function () {
43
- return this.sanitizeUrl(window.location.href);
44
- },
45
- enumerable: false,
46
- configurable: true
47
- });
48
- Object.defineProperty(Tab.prototype, "title", {
49
- get: function () {
50
- return document.title;
51
- },
52
- enumerable: false,
53
- configurable: true
54
- });
55
- Object.defineProperty(Tab.prototype, "referrer", {
56
- get: function () {
57
- return document.referrer === '' ? '' : this.sanitizeUrl(document.referrer);
58
- },
59
- enumerable: false,
60
- configurable: true
61
- });
62
- Object.defineProperty(Tab.prototype, "isVisible", {
63
- get: function () {
64
- return document.visibilityState === 'visible';
65
- },
66
- enumerable: false,
67
- configurable: true
68
- });
69
- Object.defineProperty(Tab.prototype, "document", {
70
- get: function () {
71
- return document;
72
- },
73
- enumerable: false,
74
- configurable: true
75
- });
76
- Tab.prototype.addListener = function (type, listener) {
39
+ }
40
+ get url() {
41
+ return this.sanitizeUrl(window.location.href);
42
+ }
43
+ get title() {
44
+ return document.title;
45
+ }
46
+ get referrer() {
47
+ return document.referrer === '' ? '' : this.sanitizeUrl(document.referrer);
48
+ }
49
+ get isVisible() {
50
+ return document.visibilityState === 'visible';
51
+ }
52
+ get document() {
53
+ return document;
54
+ }
55
+ addListener(type, listener) {
77
56
  this.eventManager.addListener(type, listener);
78
- };
79
- Tab.prototype.removeListener = function (type, listener) {
57
+ }
58
+ removeListener(type, listener) {
80
59
  this.eventManager.removeListener(type, listener);
81
- };
82
- Tab.prototype.sanitizeUrl = function (url) {
83
- var normalized = window.encodeURI(window.decodeURI(url));
60
+ }
61
+ sanitizeUrl(url) {
62
+ const normalized = window.encodeURI(window.decodeURI(url));
84
63
  if (this.urlSanitizer !== undefined) {
85
64
  return this.urlSanitizer(normalized).toString();
86
65
  }
87
66
  return normalized;
88
- };
89
- Tab.prototype.emit = function (type, event) {
67
+ }
68
+ emit(type, event) {
90
69
  this.eventManager.dispatch(type, event);
91
- };
92
- Tab.addUrlChangeListener = function (listener) {
93
- var url = window.location.href;
94
- var updateUrl = function () {
95
- var currentUrl = window.location.href;
70
+ }
71
+ static addUrlChangeListener(listener) {
72
+ let url = window.location.href;
73
+ const updateUrl = () => {
74
+ const currentUrl = window.location.href;
96
75
  if (url !== currentUrl) {
97
76
  listener(currentUrl);
98
77
  url = currentUrl;
99
78
  }
100
79
  };
101
- var pushState = window.history.pushState;
102
- window.history.pushState = function interceptPushState() {
103
- var args = [];
104
- for (var _i = 0; _i < arguments.length; _i++) {
105
- args[_i] = arguments[_i];
106
- }
107
- var result = pushState.apply(window.history, args);
80
+ const { pushState } = window.history;
81
+ window.history.pushState = function interceptPushState(...args) {
82
+ const result = pushState.apply(window.history, args);
108
83
  updateUrl();
109
84
  return result;
110
85
  };
111
- var replaceState = window.history.replaceState;
112
- window.history.replaceState = function interceptReplaceState() {
113
- var args = [];
114
- for (var _i = 0; _i < arguments.length; _i++) {
115
- args[_i] = arguments[_i];
116
- }
117
- var result = replaceState.apply(window.history, args);
86
+ const { replaceState } = window.history;
87
+ window.history.replaceState = function interceptReplaceState(...args) {
88
+ const result = replaceState.apply(window.history, args);
118
89
  updateUrl();
119
90
  return result;
120
91
  };
121
92
  window.addEventListener('popstate', updateUrl, true);
122
- };
123
- return Tab;
124
- }());
93
+ }
94
+ }
125
95
  exports.Tab = Tab;
96
+ //# sourceMappingURL=tab.js.map
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CachedTokenStore = void 0;
4
- var token_1 = require("./token");
5
- var CachedTokenStore = /** @class */ (function () {
6
- function CachedTokenStore(cache) {
4
+ const token_1 = require("./token");
5
+ class CachedTokenStore {
6
+ constructor(cache) {
7
7
  this.cache = cache;
8
8
  }
9
- CachedTokenStore.prototype.getToken = function () {
10
- var data = this.cache.get();
9
+ getToken() {
10
+ const data = this.cache.get();
11
11
  if (data === null) {
12
12
  return null;
13
13
  }
@@ -17,14 +17,14 @@ var CachedTokenStore = /** @class */ (function () {
17
17
  catch (error) {
18
18
  return null;
19
19
  }
20
- };
21
- CachedTokenStore.prototype.setToken = function (token) {
20
+ }
21
+ setToken(token) {
22
22
  if (token === null) {
23
23
  this.cache.clear();
24
24
  return;
25
25
  }
26
26
  this.cache.put(token.toString());
27
- };
28
- return CachedTokenStore;
29
- }());
27
+ }
28
+ }
30
29
  exports.CachedTokenStore = CachedTokenStore;
30
+ //# sourceMappingURL=cachedTokenStore.js.map
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryTokenStore = void 0;
4
- var InMemoryTokenStore = /** @class */ (function () {
5
- function InMemoryTokenStore() {
4
+ class InMemoryTokenStore {
5
+ constructor() {
6
6
  this.token = null;
7
7
  }
8
- InMemoryTokenStore.prototype.getToken = function () {
8
+ getToken() {
9
9
  return this.token;
10
- };
11
- InMemoryTokenStore.prototype.setToken = function (token) {
10
+ }
11
+ setToken(token) {
12
12
  this.token = token;
13
- };
14
- return InMemoryTokenStore;
15
- }());
13
+ }
14
+ }
16
15
  exports.InMemoryTokenStore = InMemoryTokenStore;
16
+ //# sourceMappingURL=inMemoryTokenStore.js.map
package/token/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReplicatedTokenStore = exports.InMemoryTokenStore = exports.CachedTokenStore = void 0;
4
- var tslib_1 = require("tslib");
4
+ const tslib_1 = require("tslib");
5
5
  tslib_1.__exportStar(require("./token"), exports);
6
6
  var cachedTokenStore_1 = require("./cachedTokenStore");
7
7
  Object.defineProperty(exports, "CachedTokenStore", { enumerable: true, get: function () { return cachedTokenStore_1.CachedTokenStore; } });
@@ -9,3 +9,4 @@ var inMemoryTokenStore_1 = require("./inMemoryTokenStore");
9
9
  Object.defineProperty(exports, "InMemoryTokenStore", { enumerable: true, get: function () { return inMemoryTokenStore_1.InMemoryTokenStore; } });
10
10
  var replicatedTokenStore_1 = require("./replicatedTokenStore");
11
11
  Object.defineProperty(exports, "ReplicatedTokenStore", { enumerable: true, get: function () { return replicatedTokenStore_1.ReplicatedTokenStore; } });
12
+ //# sourceMappingURL=index.js.map
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReplicatedTokenStore = void 0;
4
- var ReplicatedTokenStore = /** @class */ (function () {
5
- function ReplicatedTokenStore(primary, secondary) {
4
+ class ReplicatedTokenStore {
5
+ constructor(primary, secondary) {
6
6
  this.primary = primary;
7
7
  this.secondary = secondary;
8
8
  }
9
- ReplicatedTokenStore.prototype.getToken = function () {
9
+ getToken() {
10
10
  return this.primary.getToken();
11
- };
12
- ReplicatedTokenStore.prototype.setToken = function (token) {
11
+ }
12
+ setToken(token) {
13
13
  this.primary.setToken(token);
14
14
  this.secondary.setToken(token);
15
- };
16
- return ReplicatedTokenStore;
17
- }());
15
+ }
16
+ }
18
17
  exports.ReplicatedTokenStore = ReplicatedTokenStore;
18
+ //# sourceMappingURL=replicatedTokenStore.js.map
package/token/token.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- export declare type Headers = {
1
+ import { JsonObject } from '@croct/json';
2
+ export type Headers = {
2
3
  typ: string;
3
4
  alg: string;
4
5
  kid?: string;
5
- appId: string;
6
+ appId?: string;
6
7
  };
7
- export declare type Claims = {
8
+ type Claims = {
8
9
  iss: string;
9
10
  aud: string | string[];
10
11
  iat: number;
@@ -12,15 +13,17 @@ export declare type Claims = {
12
13
  sub?: string;
13
14
  jid?: string;
14
15
  };
16
+ export type TokenPayload = JsonObject & Claims;
15
17
  export declare class Token {
16
18
  private readonly headers;
17
- private readonly claims;
19
+ private readonly payload;
18
20
  private readonly signature;
19
21
  private constructor();
20
22
  static issue(appId: string, subject?: string | null, timestamp?: number): Token;
21
23
  static parse(token: string): Token;
24
+ static of(headers: Headers, payload: TokenPayload, signature?: string): Token;
22
25
  getHeaders(): Headers;
23
- getClaims(): Claims;
26
+ getPayload(): TokenPayload;
24
27
  getSignature(): string;
25
28
  isAnonymous(): boolean;
26
29
  getSubject(): string | null;
@@ -39,3 +42,4 @@ export declare class FixedTokenProvider implements TokenProvider {
39
42
  constructor(token: Token | null);
40
43
  getToken(): Token | null;
41
44
  }
45
+ export {};
package/token/token.js CHANGED
@@ -1,20 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FixedTokenProvider = exports.Token = void 0;
4
- var tslib_1 = require("tslib");
5
- var base64Url_1 = require("../base64Url");
6
- var schema_1 = require("../schema");
7
- var error_1 = require("../error");
8
- var Token = /** @class */ (function () {
9
- function Token(headers, claims, signature) {
10
- if (signature === void 0) { signature = ''; }
4
+ const base64Url_1 = require("../base64Url");
5
+ const schema_1 = require("../schema");
6
+ const error_1 = require("../error");
7
+ class Token {
8
+ constructor(headers, payload, signature = '') {
11
9
  this.headers = headers;
12
- this.claims = claims;
10
+ this.payload = payload;
13
11
  this.signature = signature;
14
12
  }
15
- Token.issue = function (appId, subject, timestamp) {
16
- if (subject === void 0) { subject = null; }
17
- if (timestamp === void 0) { timestamp = Math.floor(Date.now() / 1000); }
13
+ static issue(appId, subject = null, timestamp = Math.floor(Date.now() / 1000)) {
18
14
  if (timestamp < 0) {
19
15
  throw new Error('The timestamp must be non-negative.');
20
16
  }
@@ -25,75 +21,86 @@ var Token = /** @class */ (function () {
25
21
  typ: 'JWT',
26
22
  alg: 'none',
27
23
  appId: appId,
28
- }, tslib_1.__assign({ iss: 'croct.io', aud: 'croct.io', iat: timestamp }, (subject !== null ? { sub: subject } : null)));
29
- };
30
- Token.parse = function (token) {
24
+ }, {
25
+ iss: 'croct.io',
26
+ aud: 'croct.io',
27
+ iat: timestamp,
28
+ ...(subject !== null ? { sub: subject } : null),
29
+ });
30
+ }
31
+ static parse(token) {
31
32
  if (token === '') {
32
33
  throw new Error('The token cannot be empty.');
33
34
  }
34
- var parts = token.split('.', 3);
35
+ const parts = token.split('.', 3);
35
36
  // This token is invalid
36
37
  if (parts.length < 2) {
37
38
  throw new Error('The token is malformed.');
38
39
  }
39
- var headers;
40
- var claims;
41
- var signature;
40
+ let headers;
41
+ let payload;
42
+ let signature;
42
43
  try {
43
44
  headers = JSON.parse((0, base64Url_1.base64UrlDecode)(parts[0]));
44
- claims = JSON.parse((0, base64Url_1.base64UrlDecode)(parts[1]));
45
+ payload = JSON.parse((0, base64Url_1.base64UrlDecode)(parts[1]));
45
46
  if (parts.length === 3) {
46
47
  signature = (0, base64Url_1.base64UrlDecode)(parts[2]);
47
48
  }
48
49
  }
49
- catch (_a) {
50
+ catch {
50
51
  throw new Error('The token is corrupted.');
51
52
  }
53
+ return Token.of(headers, payload, signature);
54
+ }
55
+ static of(headers, payload, signature = '') {
52
56
  try {
53
- schema_1.tokenSchema.validate({ headers: headers, claims: claims, signature: signature });
57
+ schema_1.tokenSchema.validate({
58
+ headers: headers,
59
+ payload: payload,
60
+ signature: signature,
61
+ });
54
62
  }
55
63
  catch (violation) {
56
- throw new Error("The token is invalid: ".concat((0, error_1.formatCause)(violation)));
64
+ throw new Error(`The token is invalid: ${(0, error_1.formatCause)(violation)}`);
57
65
  }
58
- return new Token(headers, claims, signature);
59
- };
60
- Token.prototype.getHeaders = function () {
61
- return tslib_1.__assign({}, this.headers);
62
- };
63
- Token.prototype.getClaims = function () {
64
- return tslib_1.__assign({}, this.claims);
65
- };
66
- Token.prototype.getSignature = function () {
66
+ return new Token(headers, payload, signature);
67
+ }
68
+ getHeaders() {
69
+ return { ...this.headers };
70
+ }
71
+ getPayload() {
72
+ return { ...this.payload };
73
+ }
74
+ getSignature() {
67
75
  return this.signature;
68
- };
69
- Token.prototype.isAnonymous = function () {
70
- return this.claims.sub === undefined;
71
- };
72
- Token.prototype.getSubject = function () {
73
- return this.claims.sub !== undefined ? this.claims.sub : null;
74
- };
75
- Token.prototype.getIssueTime = function () {
76
- return this.claims.iat;
77
- };
78
- Token.prototype.toJSON = function () {
76
+ }
77
+ isAnonymous() {
78
+ return this.payload.sub === undefined;
79
+ }
80
+ getSubject() {
81
+ return this.payload.sub !== undefined ? this.payload.sub : null;
82
+ }
83
+ getIssueTime() {
84
+ return this.payload.iat;
85
+ }
86
+ toJSON() {
79
87
  return this.toString();
80
- };
81
- Token.prototype.toString = function () {
82
- var headers = (0, base64Url_1.base64UrlEncode)(JSON.stringify(this.headers));
83
- var claims = (0, base64Url_1.base64UrlEncode)(JSON.stringify(this.claims));
84
- var signature = (0, base64Url_1.base64UrlEncode)(this.signature);
85
- return "".concat(headers, ".").concat(claims, ".").concat(signature);
86
- };
87
- return Token;
88
- }());
88
+ }
89
+ toString() {
90
+ const headers = (0, base64Url_1.base64UrlEncode)(JSON.stringify(this.headers));
91
+ const payload = (0, base64Url_1.base64UrlEncode)(JSON.stringify(this.payload));
92
+ const signature = (0, base64Url_1.base64UrlEncode)(this.signature);
93
+ return `${headers}.${payload}.${signature}`;
94
+ }
95
+ }
89
96
  exports.Token = Token;
90
- var FixedTokenProvider = /** @class */ (function () {
91
- function FixedTokenProvider(token) {
97
+ class FixedTokenProvider {
98
+ constructor(token) {
92
99
  this.token = token;
93
100
  }
94
- FixedTokenProvider.prototype.getToken = function () {
101
+ getToken() {
95
102
  return this.token;
96
- };
97
- return FixedTokenProvider;
98
- }());
103
+ }
104
+ }
99
105
  exports.FixedTokenProvider = FixedTokenProvider;
106
+ //# sourceMappingURL=token.js.map
package/tracker.d.ts CHANGED
@@ -4,19 +4,19 @@ import { OutputChannel } from './channel';
4
4
  import { TokenProvider } from './token';
5
5
  import { RetryPolicy } from './retry';
6
6
  import { Beacon, TrackingEvent, TrackingEventContext, PartialTrackingEvent } from './trackingEvents';
7
- declare type Options = {
7
+ type Options = {
8
8
  eventMetadata?: {
9
9
  [key: string]: string;
10
10
  };
11
11
  };
12
- export declare type Configuration = Options & {
12
+ export type Configuration = Options & {
13
13
  channel: OutputChannel<Beacon>;
14
14
  logger?: Logger;
15
15
  tab: Tab;
16
16
  tokenProvider: TokenProvider;
17
17
  inactivityRetryPolicy: RetryPolicy<number>;
18
18
  };
19
- export declare type EventInfo<T extends TrackingEvent = TrackingEvent> = {
19
+ export type EventInfo<T extends TrackingEvent = TrackingEvent> = {
20
20
  context: TrackingEventContext;
21
21
  event: T;
22
22
  timestamp: number;
@@ -36,7 +36,7 @@ export declare class Tracker {
36
36
  private readonly pending;
37
37
  private readonly state;
38
38
  private readonly inactivityTimer;
39
- constructor({ tab, tokenProvider, channel, logger, inactivityRetryPolicy, ...options }: Configuration);
39
+ constructor(config: Configuration);
40
40
  addListener(listener: EventListener): void;
41
41
  removeListener(listener: EventListener): void;
42
42
  get flushed(): Promise<void>;