@croct/sdk 0.10.0 → 0.11.0-alpha

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 (122) hide show
  1. package/activeRecord.js +32 -36
  2. package/cache/fallbackCache.js +15 -32
  3. package/cache/inMemoryCache.js +9 -10
  4. package/cache/index.js +1 -1
  5. package/cache/localStorageCache.js +24 -25
  6. package/channel/beaconSocketChannel.d.ts +1 -1
  7. package/channel/beaconSocketChannel.js +49 -79
  8. package/channel/channel.d.ts +1 -1
  9. package/channel/encodedChannel.js +8 -10
  10. package/channel/guaranteedChannel.d.ts +4 -4
  11. package/channel/guaranteedChannel.js +41 -43
  12. package/channel/index.js +1 -1
  13. package/channel/queuedChannel.js +35 -64
  14. package/channel/retryChannel.d.ts +1 -1
  15. package/channel/retryChannel.js +44 -77
  16. package/channel/sandboxChannel.js +17 -18
  17. package/channel/socketChannel.d.ts +4 -4
  18. package/channel/socketChannel.js +77 -79
  19. package/cid/cachedAssigner.js +15 -27
  20. package/cid/fixedAssigner.js +5 -6
  21. package/cid/index.js +1 -1
  22. package/cid/remoteAssigner.js +23 -36
  23. package/constants.d.ts +3 -2
  24. package/constants.js +6 -5
  25. package/container.d.ts +13 -6
  26. package/container.js +152 -168
  27. package/contentFetcher.d.ts +59 -0
  28. package/contentFetcher.js +129 -0
  29. package/context.d.ts +3 -3
  30. package/context.js +36 -38
  31. package/error.js +2 -2
  32. package/evaluator.d.ts +33 -24
  33. package/evaluator.js +126 -117
  34. package/eventManager.d.ts +1 -1
  35. package/eventManager.js +14 -15
  36. package/facade/contentFetcherFacade.d.ts +27 -0
  37. package/facade/contentFetcherFacade.js +40 -0
  38. package/facade/evaluatorFacade.d.ts +13 -3
  39. package/facade/evaluatorFacade.js +57 -72
  40. package/facade/sdkFacade.d.ts +10 -3
  41. package/facade/sdkFacade.js +129 -141
  42. package/facade/sessionFacade.js +6 -7
  43. package/facade/sessionPatch.js +9 -13
  44. package/facade/trackerFacade.js +32 -38
  45. package/facade/userFacade.js +10 -11
  46. package/facade/userPatch.js +9 -13
  47. package/index.js +2 -2
  48. package/logging/consoleLogger.js +18 -35
  49. package/logging/index.js +1 -1
  50. package/logging/namespacedLogger.js +14 -15
  51. package/logging/nullLogger.js +10 -13
  52. package/namespacedStorage.js +30 -47
  53. package/package.json +13 -16
  54. package/patch.d.ts +1 -1
  55. package/queue/capacityRestrictedQueue.js +17 -18
  56. package/queue/inMemoryQueue.js +22 -28
  57. package/queue/index.js +1 -1
  58. package/queue/monitoredQueue.d.ts +2 -2
  59. package/queue/monitoredQueue.js +39 -40
  60. package/queue/persistentQueue.js +33 -38
  61. package/retry/arbitraryPolicy.js +8 -10
  62. package/retry/backoffPolicy.d.ts +1 -1
  63. package/retry/backoffPolicy.js +11 -13
  64. package/retry/index.js +1 -1
  65. package/retry/maxAttemptsPolicy.js +7 -8
  66. package/retry/neverPolicy.js +6 -9
  67. package/schema/attributeSchema.js +1 -1
  68. package/schema/contentFetcherSchemas.d.ts +2 -0
  69. package/schema/contentFetcherSchemas.js +22 -0
  70. package/schema/contentSchemas.js +1 -1
  71. package/schema/contextSchemas.js +1 -1
  72. package/schema/ecommerceSchemas.js +1 -1
  73. package/schema/evaluatorSchemas.d.ts +2 -0
  74. package/schema/{evaluationSchemas.js → evaluatorSchemas.js} +3 -3
  75. package/schema/eventSchemas.js +5 -7
  76. package/schema/index.d.ts +2 -1
  77. package/schema/index.js +3 -2
  78. package/schema/loggerSchema.js +1 -1
  79. package/schema/operationSchemas.js +8 -8
  80. package/schema/sdkFacadeSchemas.js +9 -6
  81. package/schema/sdkSchemas.js +8 -5
  82. package/schema/tokenSchema.js +5 -4
  83. package/schema/userSchema.js +2 -2
  84. package/sdk.d.ts +9 -3
  85. package/sdk.js +81 -127
  86. package/sdkEvents.d.ts +3 -3
  87. package/sourceLocation.d.ts +3 -3
  88. package/sourceLocation.js +13 -14
  89. package/tab.d.ts +5 -5
  90. package/tab.js +50 -80
  91. package/token/cachedTokenStore.js +9 -10
  92. package/token/inMemoryTokenStore.js +7 -8
  93. package/token/index.js +1 -1
  94. package/token/replicatedTokenStore.js +7 -8
  95. package/token/token.d.ts +9 -5
  96. package/token/token.js +63 -57
  97. package/tracker.d.ts +4 -4
  98. package/tracker.js +145 -122
  99. package/trackingEvents.d.ts +36 -36
  100. package/trackingEvents.js +12 -6
  101. package/transformer.js +1 -1
  102. package/utilityTypes.d.ts +2 -2
  103. package/uuid.js +9 -7
  104. package/validation/arrayType.d.ts +2 -2
  105. package/validation/arrayType.js +29 -27
  106. package/validation/booleanType.js +11 -15
  107. package/validation/functionType.js +11 -15
  108. package/validation/index.js +1 -1
  109. package/validation/jsonType.d.ts +2 -2
  110. package/validation/jsonType.js +61 -80
  111. package/validation/mixedSchema.js +4 -7
  112. package/validation/nullType.js +11 -15
  113. package/validation/numberType.d.ts +1 -1
  114. package/validation/numberType.js +23 -22
  115. package/validation/objectType.d.ts +1 -1
  116. package/validation/objectType.js +61 -72
  117. package/validation/schema.js +6 -10
  118. package/validation/stringType.d.ts +1 -1
  119. package/validation/stringType.js +36 -47
  120. package/validation/unionType.js +27 -77
  121. package/validation/violation.js +1 -2
  122. package/schema/evaluationSchemas.d.ts +0 -2
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.tokenSchema = void 0;
4
- var validation_1 = require("../validation");
4
+ const validation_1 = require("../validation");
5
5
  exports.tokenSchema = new validation_1.ObjectType({
6
- required: ['headers', 'claims'],
6
+ required: ['headers', 'payload'],
7
7
  properties: {
8
8
  headers: new validation_1.ObjectType({
9
- required: ['typ', 'alg', 'appId'],
9
+ required: ['typ', 'alg'],
10
10
  properties: {
11
11
  typ: new validation_1.StringType(),
12
12
  alg: new validation_1.StringType(),
@@ -16,7 +16,7 @@ exports.tokenSchema = new validation_1.ObjectType({
16
16
  }),
17
17
  },
18
18
  }),
19
- claims: new validation_1.ObjectType({
19
+ payload: new validation_1.ObjectType({
20
20
  required: ['iss', 'aud', 'iat'],
21
21
  properties: {
22
22
  iss: new validation_1.StringType(),
@@ -34,6 +34,7 @@ exports.tokenSchema = new validation_1.ObjectType({
34
34
  format: 'uuid',
35
35
  }),
36
36
  },
37
+ additionalProperties: true,
37
38
  }),
38
39
  signature: new validation_1.StringType(),
39
40
  },
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.userProfileSchema = void 0;
4
- var validation_1 = require("../validation");
5
- var attributeSchema_1 = require("./attributeSchema");
4
+ const validation_1 = require("../validation");
5
+ const attributeSchema_1 = require("./attributeSchema");
6
6
  exports.userProfileSchema = new validation_1.ObjectType({
7
7
  properties: {
8
8
  firstName: new validation_1.StringType({
package/sdk.d.ts CHANGED
@@ -6,15 +6,18 @@ import { SdkEventMap } from './sdkEvents';
6
6
  import { EventManager } from './eventManager';
7
7
  import { CidAssigner } from './cid';
8
8
  import { UrlSanitizer } from './tab';
9
- export declare type Configuration = {
9
+ import { ContentFetcher } from './contentFetcher';
10
+ import { TokenStore } from './token';
11
+ export type Configuration = {
10
12
  appId: string;
11
13
  tokenScope: TokenScope;
12
14
  debug: boolean;
13
15
  test: boolean;
14
- cid?: string;
16
+ clientId?: string;
15
17
  trackerEndpointUrl?: string;
16
18
  evaluationEndpointUrl?: string;
17
- bootstrapEndpointUrl?: string;
19
+ contentEndpointUrl?: string;
20
+ cidAssignerEndpointUrl?: string;
18
21
  beaconQueueSize?: number;
19
22
  urlSanitizer?: UrlSanitizer;
20
23
  logger?: Logger;
@@ -29,9 +32,12 @@ export declare class Sdk {
29
32
  static init(configuration: Configuration): Sdk;
30
33
  get appId(): string;
31
34
  get cidAssigner(): CidAssigner;
35
+ get previewTokenStore(): TokenStore;
36
+ get userTokenStore(): TokenStore;
32
37
  get context(): Context;
33
38
  get tracker(): Tracker;
34
39
  get evaluator(): Evaluator;
40
+ get contentFetcher(): ContentFetcher;
35
41
  get eventManager(): EventManager<SdkEventMap>;
36
42
  getLogger(...namespace: string[]): Logger;
37
43
  getTabStorage(namespace: string, ...subnamespace: string[]): Storage;
package/sdk.js CHANGED
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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");
4
+ const container_1 = require("./container");
5
+ const constants_1 = require("./constants");
6
+ const schema_1 = require("./schema");
7
+ const error_1 = require("./error");
9
8
  function validateConfiguration(configuration) {
10
9
  if (typeof configuration !== 'object' || configuration === null) {
11
10
  throw new Error('The configuration must be a key-value map.');
@@ -14,37 +13,34 @@ function validateConfiguration(configuration) {
14
13
  schema_1.sdkConfigurationSchema.validate(configuration);
15
14
  }
16
15
  catch (violation) {
17
- throw new Error("Invalid configuration: ".concat((0, error_1.formatCause)(violation)));
16
+ throw new Error(`Invalid configuration: ${(0, error_1.formatCause)(violation)}`);
18
17
  }
19
18
  }
20
- var Sdk = /** @class */ (function () {
21
- function Sdk(container) {
19
+ class Sdk {
20
+ constructor(container) {
22
21
  this.container = container;
23
22
  }
24
- Sdk.init = function (configuration) {
25
- var e_1, _a;
26
- var _b, _c, _d, _e;
23
+ static init(configuration) {
24
+ var _a, _b, _c, _d, _e;
27
25
  validateConfiguration(configuration);
28
- var _f = configuration.eventMetadata, customMetadata = _f === void 0 ? {} : _f, containerConfiguration = tslib_1.__rest(configuration, ["eventMetadata"]);
29
- var eventMetadata = {
26
+ const { eventMetadata: customMetadata = {}, ...containerConfiguration } = configuration;
27
+ const eventMetadata = {
30
28
  sdkVersion: constants_1.VERSION,
31
29
  };
32
- try {
33
- for (var _g = tslib_1.__values(Object.keys(customMetadata)), _h = _g.next(); !_h.done; _h = _g.next()) {
34
- var metadata = _h.value;
35
- eventMetadata["custom_".concat(metadata)] = customMetadata[metadata];
36
- }
30
+ for (const metadata of Object.keys(customMetadata)) {
31
+ eventMetadata[`custom_${metadata}`] = customMetadata[metadata];
37
32
  }
38
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
39
- finally {
40
- try {
41
- if (_h && !_h.done && (_a = _g.return)) _a.call(_g);
42
- }
43
- finally { if (e_1) throw e_1.error; }
44
- }
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 }));
46
- var logger = container.getLogger();
47
- var _j = container.getConfiguration(), appId = _j.appId, tokenScope = _j.tokenScope;
33
+ const container = new container_1.Container({
34
+ ...containerConfiguration,
35
+ evaluationEndpointUrl: (_a = containerConfiguration.evaluationEndpointUrl) !== null && _a !== void 0 ? _a : constants_1.EVALUATION_ENDPOINT_URL,
36
+ contentEndpointUrl: (_b = containerConfiguration.contentEndpointUrl) !== null && _b !== void 0 ? _b : constants_1.CONTENT_ENDPOINT_URL,
37
+ trackerEndpointUrl: (_c = containerConfiguration.trackerEndpointUrl) !== null && _c !== void 0 ? _c : constants_1.TRACKER_ENDPOINT_URL,
38
+ cidAssignerEndpointUrl: (_d = containerConfiguration.cidAssignerEndpointUrl) !== null && _d !== void 0 ? _d : constants_1.CID_ASSIGNER_ENDPOINT_URL,
39
+ beaconQueueSize: (_e = containerConfiguration.beaconQueueSize) !== null && _e !== void 0 ? _e : 100,
40
+ eventMetadata: eventMetadata,
41
+ });
42
+ const logger = container.getLogger();
43
+ const { appId, tokenScope } = container.getConfiguration();
48
44
  logger.debug('\n\n'
49
45
  + ' ██████ ██████  ██████  ██████ ████████ \n'
50
46
  + '██      ██   ██ ██    ██ ██         ██    \n'
@@ -52,106 +48,64 @@ var Sdk = /** @class */ (function () {
52
48
  + '██  ██   ██ ██  ██ ██  ██    \n'
53
49
  + ' ██████ ██  ██  ██████   ██████  ██    \n'
54
50
  + '\n');
55
- logger.info("Initializing SDK v".concat(constants_1.VERSION, "..."));
56
- logger.debug("App ID: ".concat(appId));
57
- var context = container.getContext();
58
- var tab = context.getTab();
59
- var user = context.getUser();
60
- logger.debug("".concat(tab.isNew ? 'New' : 'Current', " tab: ").concat(tab.id));
61
- logger.debug("Token scope: ".concat(tokenScope));
62
- logger.debug("Current user: ".concat(user !== null ? user : 'anonymous'));
63
- logger.debug("Test mode: ".concat(containerConfiguration.test));
51
+ logger.info(`Initializing SDK v${constants_1.VERSION}...`);
52
+ logger.debug(`App ID: ${appId}`);
53
+ const context = container.getContext();
54
+ const tab = context.getTab();
55
+ const user = context.getUser();
56
+ logger.debug(`${tab.isNew ? 'New' : 'Current'} tab: ${tab.id}`);
57
+ logger.debug(`Token scope: ${tokenScope}`);
58
+ logger.debug(`Current user: ${user !== null ? user : 'anonymous'}`);
59
+ logger.debug(`Test mode: ${containerConfiguration.test}`);
64
60
  logger.info('⚡ Croct SDK is ready!');
65
61
  return new Sdk(container);
66
- };
67
- Object.defineProperty(Sdk.prototype, "appId", {
68
- get: function () {
69
- var appId = this.container.getConfiguration().appId;
70
- return appId;
71
- },
72
- enumerable: false,
73
- configurable: true
74
- });
75
- Object.defineProperty(Sdk.prototype, "cidAssigner", {
76
- get: function () {
77
- return this.container.getCidAssigner();
78
- },
79
- enumerable: false,
80
- configurable: true
81
- });
82
- Object.defineProperty(Sdk.prototype, "context", {
83
- get: function () {
84
- return this.container.getContext();
85
- },
86
- enumerable: false,
87
- configurable: true
88
- });
89
- Object.defineProperty(Sdk.prototype, "tracker", {
90
- get: function () {
91
- return this.container.getTracker();
92
- },
93
- enumerable: false,
94
- configurable: true
95
- });
96
- Object.defineProperty(Sdk.prototype, "evaluator", {
97
- get: function () {
98
- return this.container.getEvaluator();
99
- },
100
- enumerable: false,
101
- configurable: true
102
- });
103
- Object.defineProperty(Sdk.prototype, "eventManager", {
104
- get: function () {
105
- return this.container.getEventManager();
106
- },
107
- enumerable: false,
108
- configurable: true
109
- });
110
- Sdk.prototype.getLogger = function () {
111
- var _a;
112
- var namespace = [];
113
- for (var _i = 0; _i < arguments.length; _i++) {
114
- namespace[_i] = arguments[_i];
115
- }
116
- return (_a = this.container).getLogger.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(namespace), false));
117
- };
118
- Sdk.prototype.getTabStorage = function (namespace) {
119
- var _a;
120
- var subnamespace = [];
121
- for (var _i = 1; _i < arguments.length; _i++) {
122
- subnamespace[_i - 1] = arguments[_i];
123
- }
124
- return (_a = this.container).getTabStorage.apply(_a, tslib_1.__spreadArray([namespace], tslib_1.__read(subnamespace), false));
125
- };
126
- Sdk.prototype.getBrowserStorage = function (namespace) {
127
- var _a;
128
- var subnamespace = [];
129
- for (var _i = 1; _i < arguments.length; _i++) {
130
- subnamespace[_i - 1] = arguments[_i];
62
+ }
63
+ get appId() {
64
+ const { appId } = this.container.getConfiguration();
65
+ return appId;
66
+ }
67
+ get cidAssigner() {
68
+ return this.container.getCidAssigner();
69
+ }
70
+ get previewTokenStore() {
71
+ return this.container.getPreviewTokenStore();
72
+ }
73
+ get userTokenStore() {
74
+ return this.container.getUserTokenStore();
75
+ }
76
+ get context() {
77
+ return this.container.getContext();
78
+ }
79
+ get tracker() {
80
+ return this.container.getTracker();
81
+ }
82
+ get evaluator() {
83
+ return this.container.getEvaluator();
84
+ }
85
+ get contentFetcher() {
86
+ return this.container.getContentFetcher();
87
+ }
88
+ get eventManager() {
89
+ return this.container.getEventManager();
90
+ }
91
+ getLogger(...namespace) {
92
+ return this.container.getLogger(...namespace);
93
+ }
94
+ getTabStorage(namespace, ...subnamespace) {
95
+ return this.container.getTabStorage(namespace, ...subnamespace);
96
+ }
97
+ getBrowserStorage(namespace, ...subnamespace) {
98
+ return this.container.getBrowserStorage(namespace, ...subnamespace);
99
+ }
100
+ async close() {
101
+ if (this.closed) {
102
+ return;
131
103
  }
132
- return (_a = this.container).getBrowserStorage.apply(_a, tslib_1.__spreadArray([namespace], tslib_1.__read(subnamespace), false));
133
- };
134
- Sdk.prototype.close = function () {
135
- return tslib_1.__awaiter(this, void 0, void 0, function () {
136
- var logger;
137
- return tslib_1.__generator(this, function (_a) {
138
- switch (_a.label) {
139
- case 0:
140
- if (this.closed) {
141
- return [2 /*return*/];
142
- }
143
- logger = this.getLogger();
144
- logger.debug('Closing SDK...');
145
- this.closed = true;
146
- return [4 /*yield*/, this.container.dispose()];
147
- case 1:
148
- _a.sent();
149
- logger.info('SDK closed.');
150
- return [2 /*return*/];
151
- }
152
- });
153
- });
154
- };
155
- return Sdk;
156
- }());
104
+ const logger = this.getLogger();
105
+ logger.debug('Closing SDK...');
106
+ this.closed = true;
107
+ await this.container.dispose();
108
+ logger.info('SDK closed.');
109
+ }
110
+ }
157
111
  exports.Sdk = Sdk;
package/sdkEvents.d.ts CHANGED
@@ -3,8 +3,8 @@ export interface TokenChanged {
3
3
  oldToken: Token | null;
4
4
  newToken: Token | null;
5
5
  }
6
- export declare type SdkEventMap = Record<string, Record<string, unknown>> & {
6
+ export type SdkEventMap = Record<string, Record<string, unknown>> & {
7
7
  tokenChanged: TokenChanged;
8
8
  };
9
- export declare type SdkEventType = keyof SdkEventMap;
10
- export declare type SdkEvent<T extends SdkEventType = SdkEventType> = T extends SdkEventType ? SdkEventMap[T] : SdkEventMap[SdkEventType];
9
+ export type SdkEventType = keyof SdkEventMap;
10
+ export type SdkEvent<T extends SdkEventType = SdkEventType> = T extends SdkEventType ? SdkEventMap[T] : SdkEventMap[SdkEventType];
@@ -1,12 +1,12 @@
1
- export declare type Position = {
1
+ export type Position = {
2
2
  line: number;
3
3
  column: number;
4
4
  index: number;
5
5
  };
6
- export declare type Location = {
6
+ export type Location = {
7
7
  start: Position;
8
8
  end: Position;
9
9
  };
10
10
  export declare function getLength(input: string): number;
11
- export declare function getPosition(input: string, index: number): Position;
12
11
  export declare function getLocation(input: string, startIndex: number, endIndex: number): Location;
12
+ export declare function getPosition(input: string, index: number): Position;
package/sourceLocation.js CHANGED
@@ -1,15 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getLocation = exports.getPosition = exports.getLength = void 0;
4
- var tslib_1 = require("tslib");
3
+ exports.getPosition = exports.getLocation = exports.getLength = void 0;
5
4
  function getLength(input) {
6
- return tslib_1.__spreadArray([], tslib_1.__read(input), false).length;
5
+ return [...input].length;
7
6
  }
8
7
  exports.getLength = getLength;
9
- function getPosition(input, index) {
10
- return getLocation(input, index, index).start;
11
- }
12
- exports.getPosition = getPosition;
13
8
  function getLocation(input, startIndex, endIndex) {
14
9
  if (startIndex < 0) {
15
10
  throw Error('The start index cannot be negative.');
@@ -17,13 +12,13 @@ function getLocation(input, startIndex, endIndex) {
17
12
  if (endIndex < startIndex) {
18
13
  throw new Error('The end index must greater than or equal to the start index.');
19
14
  }
20
- var start;
21
- var end;
22
- var chars = tslib_1.__spreadArray([], tslib_1.__read(input), false);
23
- var line = 1;
24
- var column = 0;
25
- for (var offset = 0; offset < chars.length; offset++) {
26
- var char = chars[offset];
15
+ let start;
16
+ let end;
17
+ const chars = [...input];
18
+ let line = 1;
19
+ let column = 0;
20
+ for (let offset = 0; offset < chars.length; offset++) {
21
+ const char = chars[offset];
27
22
  if (offset === startIndex) {
28
23
  start = {
29
24
  index: offset,
@@ -67,3 +62,7 @@ function getLocation(input, startIndex, endIndex) {
67
62
  };
68
63
  }
69
64
  exports.getLocation = getLocation;
65
+ function getPosition(input, index) {
66
+ return getLocation(input, index, index).start;
67
+ }
68
+ exports.getPosition = getPosition;
package/tab.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { EventListener } from './eventManager';
2
- export declare type TabEvent<T = Record<string, unknown>> = CustomEvent<{
2
+ export type TabEvent<T = Record<string, unknown>> = CustomEvent<{
3
3
  tab: Tab;
4
4
  } & T>;
5
- export declare type TabVisibilityChangeEvent = TabEvent<{
5
+ export type TabVisibilityChangeEvent = TabEvent<{
6
6
  visible: boolean;
7
7
  }>;
8
- export declare type TabUrlChangeEvent = TabEvent<{
8
+ export type TabUrlChangeEvent = TabEvent<{
9
9
  url: string;
10
10
  }>;
11
- export declare type UrlSanitizer = (url: string) => URL;
12
- declare type TabEventMap = {
11
+ export type UrlSanitizer = (url: string) => URL;
12
+ type TabEventMap = {
13
13
  focus: TabEvent;
14
14
  blur: TabEvent;
15
15
  load: TabEvent;
package/tab.js CHANGED
@@ -1,125 +1,95 @@
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;
@@ -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,13 @@ 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;
@@ -1,16 +1,15 @@
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;