@croct/sdk 0.5.0 → 0.7.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.
Files changed (187) hide show
  1. package/activeRecord.d.ts +1 -1
  2. package/activeRecord.js +15 -12
  3. package/base64Url.js +7 -2
  4. package/cache/cache.d.ts +12 -0
  5. package/cache/cache.js +2 -0
  6. package/cache/fallbackCache.d.ts +2 -2
  7. package/cache/fallbackCache.js +6 -3
  8. package/cache/inMemoryCache.d.ts +2 -2
  9. package/cache/inMemoryCache.js +5 -2
  10. package/cache/index.d.ts +4 -12
  11. package/cache/index.js +11 -0
  12. package/cache/localStorageCache.d.ts +2 -2
  13. package/cache/localStorageCache.js +4 -1
  14. package/channel/beaconSocketChannel.d.ts +3 -3
  15. package/channel/beaconSocketChannel.js +12 -9
  16. package/channel/channel.d.ts +15 -0
  17. package/channel/channel.js +2 -0
  18. package/channel/encodedChannel.d.ts +2 -2
  19. package/channel/encodedChannel.js +4 -1
  20. package/channel/guaranteedChannel.d.ts +1 -1
  21. package/channel/guaranteedChannel.js +10 -7
  22. package/channel/index.d.ts +8 -15
  23. package/channel/index.js +19 -0
  24. package/channel/queuedChannel.d.ts +2 -2
  25. package/channel/queuedChannel.js +10 -7
  26. package/channel/retryChannel.d.ts +3 -3
  27. package/channel/retryChannel.js +18 -15
  28. package/channel/sandboxChannel.d.ts +2 -2
  29. package/channel/sandboxChannel.js +4 -1
  30. package/channel/socketChannel.d.ts +1 -1
  31. package/channel/socketChannel.js +12 -9
  32. package/cid/assigner.d.ts +3 -0
  33. package/cid/assigner.js +2 -0
  34. package/cid/cachedAssigner.d.ts +5 -5
  35. package/cid/cachedAssigner.js +9 -6
  36. package/cid/fixedAssigner.d.ts +6 -0
  37. package/cid/fixedAssigner.js +13 -0
  38. package/cid/index.d.ts +4 -3
  39. package/cid/index.js +11 -0
  40. package/cid/remoteAssigner.d.ts +3 -3
  41. package/cid/remoteAssigner.js +11 -8
  42. package/constants.js +8 -5
  43. package/container.d.ts +5 -5
  44. package/container.js +49 -55
  45. package/context.d.ts +4 -4
  46. package/context.js +14 -13
  47. package/error.d.ts +1 -1
  48. package/error.js +7 -2
  49. package/evaluator.d.ts +2 -2
  50. package/evaluator.js +26 -23
  51. package/eventManager.d.ts +1 -1
  52. package/eventManager.js +5 -2
  53. package/facade/evaluatorFacade.d.ts +3 -3
  54. package/facade/evaluatorFacade.js +13 -10
  55. package/facade/index.d.ts +7 -0
  56. package/facade/index.js +19 -0
  57. package/facade/sdkFacade.d.ts +9 -9
  58. package/facade/sdkFacade.js +35 -32
  59. package/facade/sessionFacade.d.ts +3 -3
  60. package/facade/sessionFacade.js +6 -3
  61. package/facade/sessionPatch.d.ts +3 -3
  62. package/facade/sessionPatch.js +8 -5
  63. package/facade/trackerFacade.d.ts +2 -2
  64. package/facade/trackerFacade.js +21 -17
  65. package/facade/userFacade.d.ts +4 -4
  66. package/facade/userFacade.js +6 -3
  67. package/facade/userPatch.d.ts +3 -3
  68. package/facade/userPatch.js +8 -5
  69. package/index.d.ts +2 -2
  70. package/index.js +7 -3
  71. package/json.js +2 -0
  72. package/logging/consoleLogger.d.ts +2 -2
  73. package/logging/consoleLogger.js +8 -5
  74. package/logging/index.d.ts +4 -9
  75. package/logging/index.js +11 -0
  76. package/logging/logger.d.ts +9 -0
  77. package/logging/logger.js +2 -0
  78. package/logging/namespacedLogger.d.ts +2 -2
  79. package/logging/namespacedLogger.js +4 -1
  80. package/logging/nullLogger.d.ts +2 -2
  81. package/logging/nullLogger.js +4 -1
  82. package/namespacedStorage.d.ts +1 -1
  83. package/namespacedStorage.js +7 -4
  84. package/package.json +21 -18
  85. package/patch.js +2 -0
  86. package/queue/capacityRestrictedQueue.d.ts +2 -2
  87. package/queue/capacityRestrictedQueue.js +4 -1
  88. package/queue/inMemoryQueue.d.ts +2 -2
  89. package/queue/inMemoryQueue.js +6 -3
  90. package/queue/index.d.ts +5 -9
  91. package/queue/index.js +13 -0
  92. package/queue/monitoredQueue.d.ts +6 -6
  93. package/queue/monitoredQueue.js +6 -3
  94. package/queue/persistentQueue.d.ts +2 -2
  95. package/queue/persistentQueue.js +5 -2
  96. package/queue/queue.d.ts +9 -0
  97. package/queue/queue.js +2 -0
  98. package/retry/arbitraryPolicy.d.ts +2 -2
  99. package/retry/arbitraryPolicy.js +6 -3
  100. package/retry/backoffPolicy.d.ts +2 -2
  101. package/retry/backoffPolicy.js +4 -1
  102. package/retry/index.d.ts +5 -4
  103. package/retry/index.js +13 -0
  104. package/retry/maxAttemptsPolicy.d.ts +2 -2
  105. package/retry/maxAttemptsPolicy.js +4 -1
  106. package/retry/neverPolicy.d.ts +2 -2
  107. package/retry/neverPolicy.js +4 -1
  108. package/retry/policy.d.ts +4 -0
  109. package/retry/policy.js +2 -0
  110. package/schema/attributeSchema.d.ts +2 -0
  111. package/schema/attributeSchema.js +8 -0
  112. package/schema/contentSchemas.d.ts +2 -0
  113. package/schema/contentSchemas.js +46 -0
  114. package/schema/contextSchemas.d.ts +1 -1
  115. package/schema/contextSchemas.js +5 -2
  116. package/schema/ecommerceSchemas.d.ts +1 -1
  117. package/schema/ecommerceSchemas.js +60 -60
  118. package/schema/evaluationSchemas.d.ts +1 -1
  119. package/schema/evaluationSchemas.js +7 -6
  120. package/schema/eventSchemas.d.ts +3 -2
  121. package/schema/eventSchemas.js +53 -48
  122. package/schema/index.d.ts +10 -0
  123. package/schema/index.js +13 -0
  124. package/schema/loggerSchema.d.ts +1 -1
  125. package/schema/loggerSchema.js +9 -7
  126. package/schema/operationSchemas.d.ts +1 -1
  127. package/schema/operationSchemas.js +26 -24
  128. package/schema/sdkFacadeSchemas.d.ts +2 -2
  129. package/schema/sdkFacadeSchemas.js +22 -24
  130. package/schema/sdkSchemas.d.ts +2 -2
  131. package/schema/sdkSchemas.js +21 -22
  132. package/schema/tokenSchema.d.ts +2 -3
  133. package/schema/tokenSchema.js +18 -17
  134. package/schema/userSchema.d.ts +1 -1
  135. package/schema/userSchema.js +47 -61
  136. package/sdk.d.ts +5 -5
  137. package/sdk.js +27 -24
  138. package/sdkEvents.d.ts +2 -2
  139. package/sdkEvents.js +2 -0
  140. package/sourceLocation.js +12 -6
  141. package/tab.d.ts +2 -2
  142. package/tab.js +12 -9
  143. package/token/cachedTokenStore.d.ts +3 -3
  144. package/token/cachedTokenStore.js +6 -3
  145. package/token/inMemoryTokenStore.d.ts +2 -2
  146. package/token/inMemoryTokenStore.js +4 -1
  147. package/token/index.d.ts +4 -41
  148. package/token/index.js +11 -96
  149. package/token/replicatedTokenStore.d.ts +2 -2
  150. package/token/replicatedTokenStore.js +4 -1
  151. package/token/token.d.ts +41 -0
  152. package/token/token.js +99 -0
  153. package/tracker.d.ts +3 -3
  154. package/tracker.js +29 -26
  155. package/trackingEvents.d.ts +29 -14
  156. package/trackingEvents.js +20 -14
  157. package/transformer.js +5 -1
  158. package/utilityTypes.js +2 -0
  159. package/uuid.js +5 -1
  160. package/validation/arrayType.d.ts +2 -2
  161. package/validation/arrayType.js +13 -9
  162. package/validation/booleanType.d.ts +2 -2
  163. package/validation/booleanType.js +7 -3
  164. package/validation/functionType.d.ts +2 -2
  165. package/validation/functionType.js +7 -3
  166. package/validation/index.d.ts +12 -18
  167. package/validation/index.js +29 -30
  168. package/validation/jsonType.d.ts +2 -1
  169. package/validation/jsonType.js +25 -14
  170. package/validation/mixedSchema.d.ts +2 -2
  171. package/validation/mixedSchema.js +4 -1
  172. package/validation/nullType.d.ts +2 -2
  173. package/validation/nullType.js +7 -3
  174. package/validation/numberType.d.ts +2 -2
  175. package/validation/numberType.js +13 -9
  176. package/validation/objectType.d.ts +2 -2
  177. package/validation/objectType.js +23 -18
  178. package/validation/schema.d.ts +16 -0
  179. package/validation/schema.js +15 -0
  180. package/validation/stringType.d.ts +2 -2
  181. package/validation/stringType.js +16 -12
  182. package/validation/unionType.d.ts +2 -2
  183. package/validation/unionType.js +14 -10
  184. package/validation/violation.d.ts +2 -0
  185. package/validation/violation.js +24 -0
  186. package/cid/fixedCidAssigner.d.ts +0 -6
  187. package/cid/fixedCidAssigner.js +0 -10
package/sdk.js CHANGED
@@ -1,17 +1,20 @@
1
- import { __assign, __awaiter, __generator, __read, __rest, __spread, __values } from "tslib";
2
- import { Container } from './container';
3
- import { BOOTSTRAP_ENDPOINT_URL, EVALUATION_ENDPOINT_URL, TRACKER_ENDPOINT_URL, VERSION } from './constants';
4
- import { configurationSchema } from './schema/sdkSchemas';
5
- import { formatCause } from './error';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Sdk = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var container_1 = require("./container");
6
+ var constants_1 = require("./constants");
7
+ var schema_1 = require("./schema");
8
+ var error_1 = require("./error");
6
9
  function validateConfiguration(configuration) {
7
10
  if (typeof configuration !== 'object' || configuration === null) {
8
11
  throw new Error('The configuration must be a key-value map.');
9
12
  }
10
13
  try {
11
- configurationSchema.validate(configuration);
14
+ schema_1.sdkConfigurationSchema.validate(configuration);
12
15
  }
13
16
  catch (violation) {
14
- throw new Error("Invalid configuration: " + formatCause(violation));
17
+ throw new Error("Invalid configuration: " + error_1.formatCause(violation));
15
18
  }
16
19
  }
17
20
  var Sdk = /** @class */ (function () {
@@ -22,12 +25,12 @@ var Sdk = /** @class */ (function () {
22
25
  var e_1, _a;
23
26
  var _b, _c, _d, _e;
24
27
  validateConfiguration(configuration);
25
- var _f = configuration.eventMetadata, customMetadata = _f === void 0 ? {} : _f, containerConfiguration = __rest(configuration, ["eventMetadata"]);
28
+ var _f = configuration.eventMetadata, customMetadata = _f === void 0 ? {} : _f, containerConfiguration = tslib_1.__rest(configuration, ["eventMetadata"]);
26
29
  var eventMetadata = {
27
- sdkVersion: VERSION,
30
+ sdkVersion: constants_1.VERSION,
28
31
  };
29
32
  try {
30
- for (var _g = __values(Object.keys(customMetadata)), _h = _g.next(); !_h.done; _h = _g.next()) {
33
+ for (var _g = tslib_1.__values(Object.keys(customMetadata)), _h = _g.next(); !_h.done; _h = _g.next()) {
31
34
  var metadata = _h.value;
32
35
  eventMetadata["custom_" + metadata] = customMetadata[metadata];
33
36
  }
@@ -39,7 +42,7 @@ var Sdk = /** @class */ (function () {
39
42
  }
40
43
  finally { if (e_1) throw e_1.error; }
41
44
  }
42
- var container = new Container(__assign(__assign({}, containerConfiguration), { evaluationEndpointUrl: (_b = containerConfiguration.evaluationEndpointUrl, (_b !== null && _b !== void 0 ? _b : EVALUATION_ENDPOINT_URL)), trackerEndpointUrl: (_c = containerConfiguration.trackerEndpointUrl, (_c !== null && _c !== void 0 ? _c : TRACKER_ENDPOINT_URL)), bootstrapEndpointUrl: (_d = containerConfiguration.bootstrapEndpointUrl, (_d !== null && _d !== void 0 ? _d : BOOTSTRAP_ENDPOINT_URL)), beaconQueueSize: (_e = containerConfiguration.beaconQueueSize, (_e !== null && _e !== void 0 ? _e : 100)), eventMetadata: eventMetadata }));
45
+ var container = new container_1.Container(tslib_1.__assign(tslib_1.__assign({}, containerConfiguration), { evaluationEndpointUrl: (_b = containerConfiguration.evaluationEndpointUrl) !== null && _b !== void 0 ? _b : constants_1.EVALUATION_ENDPOINT_URL, trackerEndpointUrl: (_c = containerConfiguration.trackerEndpointUrl) !== null && _c !== void 0 ? _c : constants_1.TRACKER_ENDPOINT_URL, bootstrapEndpointUrl: (_d = containerConfiguration.bootstrapEndpointUrl) !== null && _d !== void 0 ? _d : constants_1.BOOTSTRAP_ENDPOINT_URL, beaconQueueSize: (_e = containerConfiguration.beaconQueueSize) !== null && _e !== void 0 ? _e : 100, eventMetadata: eventMetadata }));
43
46
  var logger = container.getLogger();
44
47
  var _j = container.getConfiguration(), appId = _j.appId, tokenScope = _j.tokenScope;
45
48
  logger.debug('\n\n'
@@ -49,7 +52,7 @@ var Sdk = /** @class */ (function () {
49
52
  + '██  ██   ██ ██  ██ ██  ██    \n'
50
53
  + ' ██████ ██  ██  ██████   ██████  ██    \n'
51
54
  + '\n');
52
- logger.info("Initializing SDK v" + VERSION + "...");
55
+ logger.info("Initializing SDK v" + constants_1.VERSION + "...");
53
56
  logger.debug("App ID: " + appId);
54
57
  var context = container.getContext();
55
58
  var tab = context.getTab();
@@ -65,42 +68,42 @@ var Sdk = /** @class */ (function () {
65
68
  var appId = this.container.getConfiguration().appId;
66
69
  return appId;
67
70
  },
68
- enumerable: true,
71
+ enumerable: false,
69
72
  configurable: true
70
73
  });
71
74
  Object.defineProperty(Sdk.prototype, "cidAssigner", {
72
75
  get: function () {
73
76
  return this.container.getCidAssigner();
74
77
  },
75
- enumerable: true,
78
+ enumerable: false,
76
79
  configurable: true
77
80
  });
78
81
  Object.defineProperty(Sdk.prototype, "context", {
79
82
  get: function () {
80
83
  return this.container.getContext();
81
84
  },
82
- enumerable: true,
85
+ enumerable: false,
83
86
  configurable: true
84
87
  });
85
88
  Object.defineProperty(Sdk.prototype, "tracker", {
86
89
  get: function () {
87
90
  return this.container.getTracker();
88
91
  },
89
- enumerable: true,
92
+ enumerable: false,
90
93
  configurable: true
91
94
  });
92
95
  Object.defineProperty(Sdk.prototype, "evaluator", {
93
96
  get: function () {
94
97
  return this.container.getEvaluator();
95
98
  },
96
- enumerable: true,
99
+ enumerable: false,
97
100
  configurable: true
98
101
  });
99
102
  Object.defineProperty(Sdk.prototype, "eventManager", {
100
103
  get: function () {
101
104
  return this.container.getEventManager();
102
105
  },
103
- enumerable: true,
106
+ enumerable: false,
104
107
  configurable: true
105
108
  });
106
109
  Sdk.prototype.getLogger = function () {
@@ -109,7 +112,7 @@ var Sdk = /** @class */ (function () {
109
112
  for (var _i = 0; _i < arguments.length; _i++) {
110
113
  namespace[_i] = arguments[_i];
111
114
  }
112
- return (_a = this.container).getLogger.apply(_a, __spread(namespace));
115
+ return (_a = this.container).getLogger.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(namespace)));
113
116
  };
114
117
  Sdk.prototype.getTabStorage = function (namespace) {
115
118
  var _a;
@@ -117,7 +120,7 @@ var Sdk = /** @class */ (function () {
117
120
  for (var _i = 1; _i < arguments.length; _i++) {
118
121
  subnamespace[_i - 1] = arguments[_i];
119
122
  }
120
- return (_a = this.container).getTabStorage.apply(_a, __spread([namespace], subnamespace));
123
+ return (_a = this.container).getTabStorage.apply(_a, tslib_1.__spreadArray([namespace], tslib_1.__read(subnamespace)));
121
124
  };
122
125
  Sdk.prototype.getBrowserStorage = function (namespace) {
123
126
  var _a;
@@ -125,12 +128,12 @@ var Sdk = /** @class */ (function () {
125
128
  for (var _i = 1; _i < arguments.length; _i++) {
126
129
  subnamespace[_i - 1] = arguments[_i];
127
130
  }
128
- return (_a = this.container).getBrowserStorage.apply(_a, __spread([namespace], subnamespace));
131
+ return (_a = this.container).getBrowserStorage.apply(_a, tslib_1.__spreadArray([namespace], tslib_1.__read(subnamespace)));
129
132
  };
130
133
  Sdk.prototype.close = function () {
131
- return __awaiter(this, void 0, void 0, function () {
134
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
132
135
  var logger;
133
- return __generator(this, function (_a) {
136
+ return tslib_1.__generator(this, function (_a) {
134
137
  switch (_a.label) {
135
138
  case 0:
136
139
  if (this.closed) {
@@ -150,4 +153,4 @@ var Sdk = /** @class */ (function () {
150
153
  };
151
154
  return Sdk;
152
155
  }());
153
- export default Sdk;
156
+ exports.Sdk = Sdk;
package/sdkEvents.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import Token from './token/index';
1
+ import { Token } from './token';
2
2
  export interface TokenChanged {
3
3
  oldToken: Token | null;
4
4
  newToken: Token | null;
5
5
  }
6
- export declare type SdkEventMap = Record<string, object> & {
6
+ export declare type SdkEventMap = Record<string, Record<string, unknown>> & {
7
7
  tokenChanged: TokenChanged;
8
8
  };
9
9
  export declare type SdkEventType = keyof SdkEventMap;
package/sdkEvents.js CHANGED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/sourceLocation.js CHANGED
@@ -1,11 +1,16 @@
1
- import { __read, __spread } from "tslib";
2
- export function getLength(input) {
3
- return __spread(input).length;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLocation = exports.getPosition = exports.getLength = void 0;
4
+ var tslib_1 = require("tslib");
5
+ function getLength(input) {
6
+ return tslib_1.__spreadArray([], tslib_1.__read(input)).length;
4
7
  }
5
- export function getPosition(input, index) {
8
+ exports.getLength = getLength;
9
+ function getPosition(input, index) {
6
10
  return getLocation(input, index, index).start;
7
11
  }
8
- export function getLocation(input, startIndex, endIndex) {
12
+ exports.getPosition = getPosition;
13
+ function getLocation(input, startIndex, endIndex) {
9
14
  if (startIndex < 0) {
10
15
  throw Error('The start index cannot be negative.');
11
16
  }
@@ -14,7 +19,7 @@ export function getLocation(input, startIndex, endIndex) {
14
19
  }
15
20
  var start;
16
21
  var end;
17
- var chars = __spread(input);
22
+ var chars = tslib_1.__spreadArray([], tslib_1.__read(input));
18
23
  var line = 1;
19
24
  var column = 0;
20
25
  for (var offset = 0; offset < chars.length; offset++) {
@@ -61,3 +66,4 @@ export function getLocation(input, startIndex, endIndex) {
61
66
  end: end,
62
67
  };
63
68
  }
69
+ exports.getLocation = getLocation;
package/tab.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { EventListener } from './eventManager';
2
- export declare type TabEvent<T = {}> = CustomEvent<{
2
+ export declare type TabEvent<T = Record<string, unknown>> = CustomEvent<{
3
3
  tab: Tab;
4
4
  } & T>;
5
5
  export declare type TabVisibilityChangeEvent = TabEvent<{
@@ -17,7 +17,7 @@ declare type TabEventMap = {
17
17
  visibilityChange: TabVisibilityChangeEvent;
18
18
  urlChange: TabUrlChangeEvent;
19
19
  };
20
- export default class Tab {
20
+ export declare class Tab {
21
21
  readonly id: string;
22
22
  readonly isNew: boolean;
23
23
  readonly urlSanitizer?: UrlSanitizer;
package/tab.js CHANGED
@@ -1,4 +1,7 @@
1
- import { SynchronousEventManager } from './eventManager';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tab = void 0;
4
+ var eventManager_1 = require("./eventManager");
2
5
  var EventMap = {
3
6
  focus: 'focus',
4
7
  blur: 'blur',
@@ -8,7 +11,7 @@ var EventMap = {
8
11
  };
9
12
  var Tab = /** @class */ (function () {
10
13
  function Tab(id, isNew, urlSanitizer) {
11
- this.eventManager = new SynchronousEventManager();
14
+ this.eventManager = new eventManager_1.SynchronousEventManager();
12
15
  this.id = id;
13
16
  this.isNew = isNew;
14
17
  this.urlSanitizer = urlSanitizer;
@@ -39,35 +42,35 @@ var Tab = /** @class */ (function () {
39
42
  get: function () {
40
43
  return this.sanitizeUrl(window.location.href);
41
44
  },
42
- enumerable: true,
45
+ enumerable: false,
43
46
  configurable: true
44
47
  });
45
48
  Object.defineProperty(Tab.prototype, "title", {
46
49
  get: function () {
47
50
  return document.title;
48
51
  },
49
- enumerable: true,
52
+ enumerable: false,
50
53
  configurable: true
51
54
  });
52
55
  Object.defineProperty(Tab.prototype, "referrer", {
53
56
  get: function () {
54
- return this.sanitizeUrl(document.referrer);
57
+ return document.referrer === '' ? '' : this.sanitizeUrl(document.referrer);
55
58
  },
56
- enumerable: true,
59
+ enumerable: false,
57
60
  configurable: true
58
61
  });
59
62
  Object.defineProperty(Tab.prototype, "isVisible", {
60
63
  get: function () {
61
64
  return document.visibilityState === 'visible';
62
65
  },
63
- enumerable: true,
66
+ enumerable: false,
64
67
  configurable: true
65
68
  });
66
69
  Object.defineProperty(Tab.prototype, "document", {
67
70
  get: function () {
68
71
  return document;
69
72
  },
70
- enumerable: true,
73
+ enumerable: false,
71
74
  configurable: true
72
75
  });
73
76
  Tab.prototype.addListener = function (type, listener) {
@@ -119,4 +122,4 @@ var Tab = /** @class */ (function () {
119
122
  };
120
123
  return Tab;
121
124
  }());
122
- export default Tab;
125
+ exports.Tab = Tab;
@@ -1,6 +1,6 @@
1
- import Token, { TokenStore } from './index';
2
- import Cache from '../cache/index';
3
- export default class CachedTokenStore implements TokenStore {
1
+ import { Token, TokenStore } from './token';
2
+ import { Cache } from '../cache';
3
+ export declare class CachedTokenStore implements TokenStore {
4
4
  private readonly cache;
5
5
  constructor(cache: Cache);
6
6
  getToken(): Token | null;
@@ -1,4 +1,7 @@
1
- import Token from './index';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CachedTokenStore = void 0;
4
+ var token_1 = require("./token");
2
5
  var CachedTokenStore = /** @class */ (function () {
3
6
  function CachedTokenStore(cache) {
4
7
  this.cache = cache;
@@ -9,7 +12,7 @@ var CachedTokenStore = /** @class */ (function () {
9
12
  return null;
10
13
  }
11
14
  try {
12
- return Token.parse(data);
15
+ return token_1.Token.parse(data);
13
16
  }
14
17
  catch (error) {
15
18
  return null;
@@ -24,4 +27,4 @@ var CachedTokenStore = /** @class */ (function () {
24
27
  };
25
28
  return CachedTokenStore;
26
29
  }());
27
- export default CachedTokenStore;
30
+ exports.CachedTokenStore = CachedTokenStore;
@@ -1,5 +1,5 @@
1
- import Token, { TokenStore } from './index';
2
- export default class InMemoryTokenStore implements TokenStore {
1
+ import { Token, TokenStore } from './token';
2
+ export declare class InMemoryTokenStore implements TokenStore {
3
3
  private token;
4
4
  getToken(): Token | null;
5
5
  setToken(token: Token | null): void;
@@ -1,3 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryTokenStore = void 0;
1
4
  var InMemoryTokenStore = /** @class */ (function () {
2
5
  function InMemoryTokenStore() {
3
6
  this.token = null;
@@ -10,4 +13,4 @@ var InMemoryTokenStore = /** @class */ (function () {
10
13
  };
11
14
  return InMemoryTokenStore;
12
15
  }());
13
- export default InMemoryTokenStore;
16
+ exports.InMemoryTokenStore = InMemoryTokenStore;
package/token/index.d.ts CHANGED
@@ -1,41 +1,4 @@
1
- export declare type Headers = {
2
- typ: string;
3
- alg: string;
4
- kid?: string;
5
- appId: string;
6
- };
7
- export declare type Claims = {
8
- iss: string;
9
- aud: string;
10
- iat: number;
11
- exp?: number;
12
- sub?: string;
13
- jid?: string;
14
- };
15
- export default class Token {
16
- private readonly headers;
17
- private readonly claims;
18
- private readonly signature;
19
- private constructor();
20
- static issue(appId: string, subject?: string | null, timestamp?: number): Token;
21
- static parse(token: string): Token;
22
- getHeaders(): Headers;
23
- getClaims(): Claims;
24
- getSignature(): string;
25
- isAnonymous(): boolean;
26
- getSubject(): string | null;
27
- getIssueTime(): number;
28
- toJSON(): string;
29
- toString(): string;
30
- }
31
- export interface TokenProvider {
32
- getToken(): Token | null;
33
- }
34
- export interface TokenStore extends TokenProvider {
35
- setToken(token: Token | null): void;
36
- }
37
- export declare class FixedTokenProvider implements TokenProvider {
38
- private readonly token;
39
- constructor(token: Token | null);
40
- getToken(): Token | null;
41
- }
1
+ export * from './token';
2
+ export { CachedTokenStore } from './cachedTokenStore';
3
+ export { InMemoryTokenStore } from './inMemoryTokenStore';
4
+ export { ReplicatedTokenStore } from './replicatedTokenStore';
package/token/index.js CHANGED
@@ -1,96 +1,11 @@
1
- import { __assign } from "tslib";
2
- import { base64UrlDecode, base64UrlEncode } from '../base64Url';
3
- import tokenSchema from '../schema/tokenSchema';
4
- import { formatCause } from '../error';
5
- var Token = /** @class */ (function () {
6
- function Token(headers, claims, signature) {
7
- if (signature === void 0) { signature = ''; }
8
- this.headers = headers;
9
- this.claims = claims;
10
- this.signature = signature;
11
- }
12
- Token.issue = function (appId, subject, timestamp) {
13
- if (subject === void 0) { subject = null; }
14
- if (timestamp === void 0) { timestamp = Math.floor(Date.now() / 1000); }
15
- if (timestamp < 0) {
16
- throw new Error('The timestamp must be non-negative.');
17
- }
18
- if (subject === '') {
19
- throw new Error('The subject must be non-empty.');
20
- }
21
- return new Token({
22
- typ: 'JWT',
23
- alg: 'none',
24
- appId: appId,
25
- }, __assign({ iss: 'croct.io', aud: 'croct.io', iat: timestamp }, (subject !== null ? { sub: subject } : null)));
26
- };
27
- Token.parse = function (token) {
28
- if (token === '') {
29
- throw new Error('The token cannot be empty.');
30
- }
31
- var parts = token.split('.', 3);
32
- // This token is invalid
33
- if (parts.length < 2) {
34
- throw new Error('The token is malformed.');
35
- }
36
- var headers;
37
- var claims;
38
- var signature;
39
- try {
40
- headers = JSON.parse(base64UrlDecode(parts[0]));
41
- claims = JSON.parse(base64UrlDecode(parts[1]));
42
- if (parts.length === 3) {
43
- signature = base64UrlDecode(parts[2]);
44
- }
45
- }
46
- catch (_a) {
47
- throw new Error('The token is corrupted.');
48
- }
49
- try {
50
- tokenSchema.validate({ headers: headers, claims: claims, signature: signature });
51
- }
52
- catch (violation) {
53
- throw new Error("The token is invalid: " + formatCause(violation));
54
- }
55
- return new Token(headers, claims, signature);
56
- };
57
- Token.prototype.getHeaders = function () {
58
- return __assign({}, this.headers);
59
- };
60
- Token.prototype.getClaims = function () {
61
- return __assign({}, this.claims);
62
- };
63
- Token.prototype.getSignature = function () {
64
- return this.signature;
65
- };
66
- Token.prototype.isAnonymous = function () {
67
- return this.claims.sub === undefined;
68
- };
69
- Token.prototype.getSubject = function () {
70
- return this.claims.sub !== undefined ? this.claims.sub : null;
71
- };
72
- Token.prototype.getIssueTime = function () {
73
- return this.claims.iat;
74
- };
75
- Token.prototype.toJSON = function () {
76
- return this.toString();
77
- };
78
- Token.prototype.toString = function () {
79
- var headers = base64UrlEncode(JSON.stringify(this.headers));
80
- var claims = base64UrlEncode(JSON.stringify(this.claims));
81
- var signature = base64UrlEncode(this.signature);
82
- return headers + "." + claims + "." + signature;
83
- };
84
- return Token;
85
- }());
86
- export default Token;
87
- var FixedTokenProvider = /** @class */ (function () {
88
- function FixedTokenProvider(token) {
89
- this.token = token;
90
- }
91
- FixedTokenProvider.prototype.getToken = function () {
92
- return this.token;
93
- };
94
- return FixedTokenProvider;
95
- }());
96
- export { FixedTokenProvider };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReplicatedTokenStore = exports.InMemoryTokenStore = exports.CachedTokenStore = void 0;
4
+ var tslib_1 = require("tslib");
5
+ tslib_1.__exportStar(require("./token"), exports);
6
+ var cachedTokenStore_1 = require("./cachedTokenStore");
7
+ Object.defineProperty(exports, "CachedTokenStore", { enumerable: true, get: function () { return cachedTokenStore_1.CachedTokenStore; } });
8
+ var inMemoryTokenStore_1 = require("./inMemoryTokenStore");
9
+ Object.defineProperty(exports, "InMemoryTokenStore", { enumerable: true, get: function () { return inMemoryTokenStore_1.InMemoryTokenStore; } });
10
+ var replicatedTokenStore_1 = require("./replicatedTokenStore");
11
+ Object.defineProperty(exports, "ReplicatedTokenStore", { enumerable: true, get: function () { return replicatedTokenStore_1.ReplicatedTokenStore; } });
@@ -1,5 +1,5 @@
1
- import Token, { TokenStore } from './index';
2
- export default class ReplicatedTokenStore implements TokenStore {
1
+ import { Token, TokenStore } from './token';
2
+ export declare class ReplicatedTokenStore implements TokenStore {
3
3
  private primary;
4
4
  private secondary;
5
5
  constructor(primary: TokenStore, secondary: TokenStore);
@@ -1,3 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReplicatedTokenStore = void 0;
1
4
  var ReplicatedTokenStore = /** @class */ (function () {
2
5
  function ReplicatedTokenStore(primary, secondary) {
3
6
  this.primary = primary;
@@ -12,4 +15,4 @@ var ReplicatedTokenStore = /** @class */ (function () {
12
15
  };
13
16
  return ReplicatedTokenStore;
14
17
  }());
15
- export default ReplicatedTokenStore;
18
+ exports.ReplicatedTokenStore = ReplicatedTokenStore;
@@ -0,0 +1,41 @@
1
+ export declare type Headers = {
2
+ typ: string;
3
+ alg: string;
4
+ kid?: string;
5
+ appId: string;
6
+ };
7
+ export declare type Claims = {
8
+ iss: string;
9
+ aud: string;
10
+ iat: number;
11
+ exp?: number;
12
+ sub?: string;
13
+ jid?: string;
14
+ };
15
+ export declare class Token {
16
+ private readonly headers;
17
+ private readonly claims;
18
+ private readonly signature;
19
+ private constructor();
20
+ static issue(appId: string, subject?: string | null, timestamp?: number): Token;
21
+ static parse(token: string): Token;
22
+ getHeaders(): Headers;
23
+ getClaims(): Claims;
24
+ getSignature(): string;
25
+ isAnonymous(): boolean;
26
+ getSubject(): string | null;
27
+ getIssueTime(): number;
28
+ toJSON(): string;
29
+ toString(): string;
30
+ }
31
+ export interface TokenProvider {
32
+ getToken(): Token | null;
33
+ }
34
+ export interface TokenStore extends TokenProvider {
35
+ setToken(token: Token | null): void;
36
+ }
37
+ export declare class FixedTokenProvider implements TokenProvider {
38
+ private readonly token;
39
+ constructor(token: Token | null);
40
+ getToken(): Token | null;
41
+ }
package/token/token.js ADDED
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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 = ''; }
11
+ this.headers = headers;
12
+ this.claims = claims;
13
+ this.signature = signature;
14
+ }
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); }
18
+ if (timestamp < 0) {
19
+ throw new Error('The timestamp must be non-negative.');
20
+ }
21
+ if (subject === '') {
22
+ throw new Error('The subject must be non-empty.');
23
+ }
24
+ return new Token({
25
+ typ: 'JWT',
26
+ alg: 'none',
27
+ 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) {
31
+ if (token === '') {
32
+ throw new Error('The token cannot be empty.');
33
+ }
34
+ var parts = token.split('.', 3);
35
+ // This token is invalid
36
+ if (parts.length < 2) {
37
+ throw new Error('The token is malformed.');
38
+ }
39
+ var headers;
40
+ var claims;
41
+ var signature;
42
+ try {
43
+ headers = JSON.parse(base64Url_1.base64UrlDecode(parts[0]));
44
+ claims = JSON.parse(base64Url_1.base64UrlDecode(parts[1]));
45
+ if (parts.length === 3) {
46
+ signature = base64Url_1.base64UrlDecode(parts[2]);
47
+ }
48
+ }
49
+ catch (_a) {
50
+ throw new Error('The token is corrupted.');
51
+ }
52
+ try {
53
+ schema_1.tokenSchema.validate({ headers: headers, claims: claims, signature: signature });
54
+ }
55
+ catch (violation) {
56
+ throw new Error("The token is invalid: " + error_1.formatCause(violation));
57
+ }
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 () {
67
+ 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 () {
79
+ return this.toString();
80
+ };
81
+ Token.prototype.toString = function () {
82
+ var headers = base64Url_1.base64UrlEncode(JSON.stringify(this.headers));
83
+ var claims = base64Url_1.base64UrlEncode(JSON.stringify(this.claims));
84
+ var signature = base64Url_1.base64UrlEncode(this.signature);
85
+ return headers + "." + claims + "." + signature;
86
+ };
87
+ return Token;
88
+ }());
89
+ exports.Token = Token;
90
+ var FixedTokenProvider = /** @class */ (function () {
91
+ function FixedTokenProvider(token) {
92
+ this.token = token;
93
+ }
94
+ FixedTokenProvider.prototype.getToken = function () {
95
+ return this.token;
96
+ };
97
+ return FixedTokenProvider;
98
+ }());
99
+ exports.FixedTokenProvider = FixedTokenProvider;