@aws-amplify/analytics 7.0.1-api-v6-models.9351bcf.0 → 7.0.1-api-v6-models.891fe0d.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 (87) hide show
  1. package/lib/apis/disable.js +1 -1
  2. package/lib/apis/enable.js +1 -1
  3. package/lib/errors/AnalyticsError.js +7 -11
  4. package/lib/errors/assertValidationError.js +6 -6
  5. package/lib/errors/validation.js +10 -11
  6. package/lib/providers/kinesis/apis/flushEvents.js +20 -23
  7. package/lib/providers/kinesis/apis/record.js +24 -26
  8. package/lib/providers/kinesis/utils/getEventBuffer.js +39 -70
  9. package/lib/providers/kinesis/utils/resolveConfig.js +14 -13
  10. package/lib/providers/kinesis-firehose/apis/flushEvents.js +19 -22
  11. package/lib/providers/kinesis-firehose/apis/record.js +22 -24
  12. package/lib/providers/kinesis-firehose/utils/getEventBuffer.js +39 -68
  13. package/lib/providers/kinesis-firehose/utils/resolveConfig.js +14 -13
  14. package/lib/providers/personalize/apis/flushEvents.js +18 -21
  15. package/lib/providers/personalize/apis/record.js +55 -71
  16. package/lib/providers/personalize/utils/autoTrackMedia.js +96 -98
  17. package/lib/providers/personalize/utils/cachedSession.js +30 -42
  18. package/lib/providers/personalize/utils/getEventBuffer.js +35 -62
  19. package/lib/providers/personalize/utils/resolveConfig.js +14 -13
  20. package/lib/providers/pinpoint/apis/configureAutoTrack.js +6 -6
  21. package/lib/providers/pinpoint/apis/flushEvents.js +10 -13
  22. package/lib/providers/pinpoint/apis/identifyUser.js +18 -30
  23. package/lib/providers/pinpoint/apis/record.js +15 -16
  24. package/lib/providers/pinpoint/utils/resolveConfig.js +5 -6
  25. package/lib/providers/pinpoint/utils/resolveCredentials.js +7 -15
  26. package/lib/setupTests.js +1 -1
  27. package/lib/trackers/EventTracker.js +39 -45
  28. package/lib/trackers/PageViewTracker.js +41 -45
  29. package/lib/trackers/SessionTracker.js +25 -31
  30. package/lib/trackers/SessionTracker.native.js +25 -31
  31. package/lib/tsconfig.tsbuildinfo +1 -1
  32. package/lib/utils/eventBuffer/EventBuffer.js +34 -62
  33. package/lib/utils/groupBy.js +4 -7
  34. package/lib/utils/resolveCredentials.js +7 -15
  35. package/lib/utils/statusHelpers.js +4 -4
  36. package/lib/utils/trackerConfigHelpers.js +2 -2
  37. package/lib/utils/trackerConfigHelpers.native.js +2 -2
  38. package/lib/utils/trackerHelpers.js +5 -5
  39. package/lib/utils/userAgent.js +3 -3
  40. package/lib-esm/apis/disable.js +1 -1
  41. package/lib-esm/apis/enable.js +1 -1
  42. package/lib-esm/errors/AnalyticsError.js +6 -11
  43. package/lib-esm/errors/assertValidationError.js +4 -4
  44. package/lib-esm/errors/validation.js +10 -11
  45. package/lib-esm/providers/kinesis/apis/flushEvents.js +15 -18
  46. package/lib-esm/providers/kinesis/apis/record.js +18 -20
  47. package/lib-esm/providers/kinesis/utils/constants.js +1 -1
  48. package/lib-esm/providers/kinesis/utils/getEventBuffer.js +37 -68
  49. package/lib-esm/providers/kinesis/utils/resolveConfig.js +11 -10
  50. package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +15 -18
  51. package/lib-esm/providers/kinesis-firehose/apis/record.js +17 -19
  52. package/lib-esm/providers/kinesis-firehose/utils/constants.js +1 -1
  53. package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.js +37 -66
  54. package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.js +11 -10
  55. package/lib-esm/providers/personalize/apis/flushEvents.js +14 -17
  56. package/lib-esm/providers/personalize/apis/record.js +50 -66
  57. package/lib-esm/providers/personalize/utils/autoTrackMedia.js +94 -96
  58. package/lib-esm/providers/personalize/utils/cachedSession.js +28 -40
  59. package/lib-esm/providers/personalize/utils/constants.js +4 -4
  60. package/lib-esm/providers/personalize/utils/getEventBuffer.js +33 -60
  61. package/lib-esm/providers/personalize/utils/resolveConfig.js +11 -10
  62. package/lib-esm/providers/pinpoint/apis/configureAutoTrack.js +4 -4
  63. package/lib-esm/providers/pinpoint/apis/flushEvents.js +5 -8
  64. package/lib-esm/providers/pinpoint/apis/identifyUser.js +14 -26
  65. package/lib-esm/providers/pinpoint/apis/record.js +9 -10
  66. package/lib-esm/providers/pinpoint/utils/resolveConfig.js +3 -4
  67. package/lib-esm/providers/pinpoint/utils/resolveCredentials.js +5 -13
  68. package/lib-esm/setupTests.js +1 -1
  69. package/lib-esm/trackers/EventTracker.js +37 -44
  70. package/lib-esm/trackers/PageViewTracker.js +39 -44
  71. package/lib-esm/trackers/SessionTracker.js +22 -29
  72. package/lib-esm/trackers/SessionTracker.native.js +22 -29
  73. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  74. package/lib-esm/utils/eventBuffer/EventBuffer.js +33 -62
  75. package/lib-esm/utils/groupBy.js +4 -7
  76. package/lib-esm/utils/resolveCredentials.js +5 -13
  77. package/lib-esm/utils/statusHelpers.js +4 -4
  78. package/lib-esm/utils/trackerConfigHelpers.js +1 -1
  79. package/lib-esm/utils/trackerConfigHelpers.native.js +1 -1
  80. package/lib-esm/utils/trackerHelpers.js +4 -4
  81. package/lib-esm/utils/userAgent.js +2 -2
  82. package/package.json +6 -9
  83. package/src/providers/kinesis/utils/getEventBuffer.ts +6 -7
  84. package/src/providers/kinesis-firehose/utils/getEventBuffer.ts +6 -7
  85. package/src/providers/personalize/utils/getEventBuffer.ts +6 -7
  86. package/src/trackers/SessionTracker.native.ts +2 -3
  87. package/src/trackers/SessionTracker.ts +2 -3
@@ -3,89 +3,61 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.EventBuffer = void 0;
6
- var tslib_1 = require("tslib");
7
- var core_1 = require("@aws-amplify/core");
8
- var logger = new core_1.ConsoleLogger('EventBuffer');
9
- var EventBuffer = /** @class */ (function () {
10
- function EventBuffer(config, getAnalyticsClient) {
6
+ const core_1 = require("@aws-amplify/core");
7
+ const logger = new core_1.ConsoleLogger('EventBuffer');
8
+ class EventBuffer {
9
+ constructor(config, getAnalyticsClient) {
11
10
  this.list = [];
12
11
  this.config = config;
13
12
  this.getAnalyticsClient = getAnalyticsClient;
14
13
  this.startEventLoop();
15
14
  }
16
- EventBuffer.prototype.append = function () {
17
- var e_1, _a;
18
- var events = [];
19
- for (var _i = 0; _i < arguments.length; _i++) {
20
- events[_i] = arguments[_i];
21
- }
22
- try {
23
- for (var events_1 = tslib_1.__values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
24
- var event_1 = events_1_1.value;
25
- if (this.list.length + 1 > this.config.bufferSize) {
26
- logger.debug("Exceed ".concat(typeof event_1, " event buffer limits, event dropped"));
27
- continue;
28
- }
29
- this.list.push(event_1);
30
- }
31
- }
32
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
33
- finally {
34
- try {
35
- if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
15
+ append(...events) {
16
+ for (const event of events) {
17
+ if (this.list.length + 1 > this.config.bufferSize) {
18
+ logger.debug(`Exceed ${typeof event} event buffer limits, event dropped`);
19
+ continue;
36
20
  }
37
- finally { if (e_1) throw e_1.error; }
21
+ this.list.push(event);
38
22
  }
39
- };
40
- EventBuffer.prototype.flushAll = function () {
23
+ }
24
+ flushAll() {
41
25
  return this.submitEvents(this.list.length);
42
- };
43
- EventBuffer.prototype.release = function () {
26
+ }
27
+ release() {
44
28
  this.list = [];
45
29
  if (this.timer) {
46
30
  clearInterval(this.timer);
47
31
  }
48
- };
49
- Object.defineProperty(EventBuffer.prototype, "length", {
50
- get: function () {
51
- return this.list.length;
52
- },
53
- enumerable: false,
54
- configurable: true
55
- });
56
- EventBuffer.prototype.head = function (count) {
32
+ }
33
+ get length() {
34
+ return this.list.length;
35
+ }
36
+ head(count) {
57
37
  return this.list.splice(0, count);
58
- };
59
- EventBuffer.prototype.insertAtBeginning = function () {
60
- var _a;
61
- var data = [];
62
- for (var _i = 0; _i < arguments.length; _i++) {
63
- data[_i] = arguments[_i];
64
- }
65
- (_a = this.list).unshift.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(data), false));
66
- };
67
- EventBuffer.prototype.startEventLoop = function () {
68
- var _this = this;
38
+ }
39
+ insertAtBeginning(...data) {
40
+ this.list.unshift(...data);
41
+ }
42
+ startEventLoop() {
69
43
  if (this.timer) {
70
44
  clearInterval(this.timer);
71
45
  }
72
- var _a = this.config, flushSize = _a.flushSize, flushInterval = _a.flushInterval;
73
- setInterval(function () {
74
- _this.submitEvents(flushSize);
46
+ const { flushSize, flushInterval } = this.config;
47
+ setInterval(() => {
48
+ this.submitEvents(flushSize);
75
49
  }, flushInterval);
76
- };
77
- EventBuffer.prototype.submitEvents = function (count) {
78
- var _this = this;
79
- var events = this.head(count);
50
+ }
51
+ submitEvents(count) {
52
+ const events = this.head(count);
80
53
  if (events.length === 0) {
81
54
  return Promise.resolve();
82
55
  }
83
- return this.getAnalyticsClient()(events).then(function (result) {
56
+ return this.getAnalyticsClient()(events).then(result => {
84
57
  if (result.length > 0) {
85
- _this.insertAtBeginning.apply(_this, tslib_1.__spreadArray([], tslib_1.__read(result), false));
58
+ this.insertAtBeginning(...result);
86
59
  }
87
60
  });
88
- };
89
- return EventBuffer;
90
- }());
61
+ }
62
+ }
91
63
  exports.EventBuffer = EventBuffer;
@@ -3,13 +3,10 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.groupBy = void 0;
6
- var tslib_1 = require("tslib");
7
- var groupBy = function (getGroupId, list) {
8
- return list.reduce(function (result, current) {
9
- var _a;
10
- var _b;
11
- var groupId = getGroupId(current);
12
- return tslib_1.__assign(tslib_1.__assign({}, result), (_a = {}, _a[groupId] = tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(((_b = result[groupId]) !== null && _b !== void 0 ? _b : [])), false), [current], false), _a));
6
+ const groupBy = (getGroupId, list) => {
7
+ return list.reduce((result, current) => {
8
+ const groupId = getGroupId(current);
9
+ return { ...result, [groupId]: [...(result[groupId] ?? []), current] };
13
10
  }, {});
14
11
  };
15
12
  exports.groupBy = groupBy;
@@ -3,19 +3,11 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.resolveCredentials = void 0;
6
- var tslib_1 = require("tslib");
7
- var errors_1 = require("../errors");
8
- var core_1 = require("@aws-amplify/core");
9
- var resolveCredentials = function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
10
- var _a, credentials, identityId;
11
- return tslib_1.__generator(this, function (_b) {
12
- switch (_b.label) {
13
- case 0: return [4 /*yield*/, (0, core_1.fetchAuthSession)()];
14
- case 1:
15
- _a = _b.sent(), credentials = _a.credentials, identityId = _a.identityId;
16
- (0, errors_1.assertValidationError)(!!credentials, errors_1.AnalyticsValidationErrorCode.NoCredentials);
17
- return [2 /*return*/, { credentials: credentials, identityId: identityId }];
18
- }
19
- });
20
- }); };
6
+ const errors_1 = require("../errors");
7
+ const core_1 = require("@aws-amplify/core");
8
+ const resolveCredentials = async () => {
9
+ const { credentials, identityId } = await (0, core_1.fetchAuthSession)();
10
+ (0, errors_1.assertValidationError)(!!credentials, errors_1.AnalyticsValidationErrorCode.NoCredentials);
11
+ return { credentials, identityId };
12
+ };
21
13
  exports.resolveCredentials = resolveCredentials;
@@ -3,17 +3,17 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.isAnalyticsEnabled = exports.disableAnalytics = exports.enableAnalytics = void 0;
6
- var analyticsEnabled = true;
7
- var enableAnalytics = function () {
6
+ let analyticsEnabled = true;
7
+ const enableAnalytics = () => {
8
8
  analyticsEnabled = true;
9
9
  };
10
10
  exports.enableAnalytics = enableAnalytics;
11
- var disableAnalytics = function () {
11
+ const disableAnalytics = () => {
12
12
  analyticsEnabled = false;
13
13
  };
14
14
  exports.disableAnalytics = disableAnalytics;
15
15
  /**
16
16
  * Returns the current status of the Analytics category.
17
17
  */
18
- var isAnalyticsEnabled = function () { return analyticsEnabled; };
18
+ const isAnalyticsEnabled = () => analyticsEnabled;
19
19
  exports.isAnalyticsEnabled = isAnalyticsEnabled;
@@ -3,11 +3,11 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.validateTrackerConfiguration = void 0;
6
- var errors_1 = require("../errors");
6
+ const errors_1 = require("../errors");
7
7
  /**
8
8
  * Validates tracker configuration.
9
9
  */
10
- var validateTrackerConfiguration = function (input) {
10
+ const validateTrackerConfiguration = (input) => {
11
11
  (0, errors_1.assertValidationError)(input.type === 'event' ||
12
12
  input.type === 'pageView' ||
13
13
  input.type === 'session', errors_1.AnalyticsValidationErrorCode.InvalidTracker);
@@ -3,11 +3,11 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.validateTrackerConfiguration = void 0;
6
- var errors_1 = require("../errors");
6
+ const errors_1 = require("../errors");
7
7
  /**
8
8
  * Validates tracker configuration.
9
9
  */
10
- var validateTrackerConfiguration = function (input) {
10
+ const validateTrackerConfiguration = (input) => {
11
11
  // React Native only supports session tracking
12
12
  (0, errors_1.assertValidationError)(input.type === 'session', errors_1.AnalyticsValidationErrorCode.UnsupportedPlatform);
13
13
  };
@@ -3,17 +3,17 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.updateProviderTrackers = void 0;
6
- var trackers_1 = require("../trackers");
6
+ const trackers_1 = require("../trackers");
7
7
  /**
8
8
  * Updates a provider's trackers as appropriate for the provided auto-track configuration.
9
9
  *
10
10
  * @remark
11
11
  * This utility will mutate the provider's configured trackers via `providerTrackers`.
12
12
  */
13
- var updateProviderTrackers = function (input, providerEventRecorder, providerTrackers) {
14
- var trackerInstance;
15
- var trackerType = input.type;
16
- var currentTracker = providerTrackers[trackerType];
13
+ const updateProviderTrackers = (input, providerEventRecorder, providerTrackers) => {
14
+ let trackerInstance;
15
+ const trackerType = input.type;
16
+ const currentTracker = providerTrackers[trackerType];
17
17
  // Check if the tracker was disabled & should be cleaned up
18
18
  if (!input.enable) {
19
19
  if (currentTracker) {
@@ -3,18 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAnalyticsUserAgentString = exports.getAnalyticsUserAgent = void 0;
4
4
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
5
  // SPDX-License-Identifier: Apache-2.0
6
- var utils_1 = require("@aws-amplify/core/internals/utils");
6
+ const utils_1 = require("@aws-amplify/core/internals/utils");
7
7
  function getAnalyticsUserAgent(action) {
8
8
  return (0, utils_1.getAmplifyUserAgentObject)({
9
9
  category: utils_1.Category.Analytics,
10
- action: action,
10
+ action,
11
11
  });
12
12
  }
13
13
  exports.getAnalyticsUserAgent = getAnalyticsUserAgent;
14
14
  function getAnalyticsUserAgentString(action) {
15
15
  return (0, utils_1.getAmplifyUserAgent)({
16
16
  category: utils_1.Category.Analytics,
17
- action: action,
17
+ action,
18
18
  });
19
19
  }
20
20
  exports.getAnalyticsUserAgentString = getAnalyticsUserAgentString;
@@ -8,4 +8,4 @@ import { disableAnalytics } from '../utils';
8
8
  * When Analytics is disabled events will not be buffered or transmitted to your selected service. Any auto-tracking
9
9
  * behavior that you have configured via `configureAutoTrack` will not have any effect while Analytics is disabled.
10
10
  */
11
- export var disable = disableAnalytics;
11
+ export const disable = disableAnalytics;
@@ -7,4 +7,4 @@ import { enableAnalytics } from '../utils';
7
7
  * @note
8
8
  * Analytics is enabled by default. You do not need to call this API unless you have disabled Analytics.
9
9
  */
10
- export var enable = enableAnalytics;
10
+ export const enable = enableAnalytics;
@@ -1,20 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __extends } from "tslib";
4
3
  import { AmplifyError, } from '@aws-amplify/core/internals/utils';
5
4
  /**
6
5
  * @internal
7
6
  */
8
- var AnalyticsError = /** @class */ (function (_super) {
9
- __extends(AnalyticsError, _super);
10
- function AnalyticsError(params) {
11
- var _this = _super.call(this, params) || this;
7
+ export class AnalyticsError extends AmplifyError {
8
+ constructor(params) {
9
+ super(params);
12
10
  // Hack for making the custom error class work when transpiled to es5
13
11
  // TODO: Delete the following 2 lines after we change the build target to >= es2015
14
- _this.constructor = AnalyticsError;
15
- Object.setPrototypeOf(_this, AnalyticsError.prototype);
16
- return _this;
12
+ this.constructor = AnalyticsError;
13
+ Object.setPrototypeOf(this, AnalyticsError.prototype);
17
14
  }
18
- return AnalyticsError;
19
- }(AmplifyError));
20
- export { AnalyticsError };
15
+ }
@@ -6,12 +6,12 @@ import { validationErrorMap } from './validation';
6
6
  * @internal
7
7
  */
8
8
  export function assertValidationError(assertion, name, message) {
9
- var _a = validationErrorMap[name], defaultMessage = _a.message, recoverySuggestion = _a.recoverySuggestion;
9
+ const { message: defaultMessage, recoverySuggestion } = validationErrorMap[name];
10
10
  if (!assertion) {
11
11
  throw new AnalyticsError({
12
- name: name,
13
- message: message !== null && message !== void 0 ? message : defaultMessage,
14
- recoverySuggestion: recoverySuggestion,
12
+ name,
13
+ message: message ?? defaultMessage,
14
+ recoverySuggestion,
15
15
  });
16
16
  }
17
17
  }
@@ -1,6 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- var _a;
4
3
  export var AnalyticsValidationErrorCode;
5
4
  (function (AnalyticsValidationErrorCode) {
6
5
  AnalyticsValidationErrorCode["NoAppId"] = "NoAppId";
@@ -12,29 +11,29 @@ export var AnalyticsValidationErrorCode;
12
11
  AnalyticsValidationErrorCode["NoTrackingId"] = "NoTrackingId";
13
12
  AnalyticsValidationErrorCode["InvalidFlushSize"] = "InvalidFlushSize";
14
13
  })(AnalyticsValidationErrorCode || (AnalyticsValidationErrorCode = {}));
15
- export var validationErrorMap = (_a = {},
16
- _a[AnalyticsValidationErrorCode.NoAppId] = {
14
+ export const validationErrorMap = {
15
+ [AnalyticsValidationErrorCode.NoAppId]: {
17
16
  message: 'Missing application id.',
18
17
  },
19
- _a[AnalyticsValidationErrorCode.NoCredentials] = {
18
+ [AnalyticsValidationErrorCode.NoCredentials]: {
20
19
  message: 'Credentials should not be empty.',
21
20
  },
22
- _a[AnalyticsValidationErrorCode.NoEventName] = {
21
+ [AnalyticsValidationErrorCode.NoEventName]: {
23
22
  message: 'Events must specify a name.',
24
23
  },
25
- _a[AnalyticsValidationErrorCode.NoRegion] = {
24
+ [AnalyticsValidationErrorCode.NoRegion]: {
26
25
  message: 'Missing region.',
27
26
  },
28
- _a[AnalyticsValidationErrorCode.InvalidTracker] = {
27
+ [AnalyticsValidationErrorCode.InvalidTracker]: {
29
28
  message: 'Invalid tracker type specified.',
30
29
  },
31
- _a[AnalyticsValidationErrorCode.UnsupportedPlatform] = {
30
+ [AnalyticsValidationErrorCode.UnsupportedPlatform]: {
32
31
  message: 'Only session tracking is supported on React Native.',
33
32
  },
34
- _a[AnalyticsValidationErrorCode.InvalidFlushSize] = {
33
+ [AnalyticsValidationErrorCode.InvalidFlushSize]: {
35
34
  message: 'Invalid FlushSize, it should be smaller than BufferSize',
36
35
  },
37
- _a[AnalyticsValidationErrorCode.NoTrackingId] = {
36
+ [AnalyticsValidationErrorCode.NoTrackingId]: {
38
37
  message: 'A trackingId is required to use Amazon Personalize',
39
38
  },
40
- _a);
39
+ };
@@ -5,7 +5,7 @@ import { getAnalyticsUserAgentString, resolveCredentials, } from '../../../utils
5
5
  import { getEventBuffer } from '../utils/getEventBuffer';
6
6
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
7
7
  import { ConsoleLogger } from '@aws-amplify/core';
8
- var logger = new ConsoleLogger('Kinesis');
8
+ const logger = new ConsoleLogger('Kinesis');
9
9
  /**
10
10
  * Flushes all buffered Kinesis events to the service.
11
11
  *
@@ -13,22 +13,19 @@ var logger = new ConsoleLogger('Kinesis');
13
13
  * This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
14
14
  * this API may not be included in the flush.
15
15
  */
16
- export var flushEvents = function () {
17
- var _a = resolveConfig(), region = _a.region, flushSize = _a.flushSize, flushInterval = _a.flushInterval, bufferSize = _a.bufferSize, resendLimit = _a.resendLimit;
16
+ export const flushEvents = () => {
17
+ const { region, flushSize, flushInterval, bufferSize, resendLimit } = resolveConfig();
18
18
  resolveCredentials()
19
- .then(function (_a) {
20
- var credentials = _a.credentials, identityId = _a.identityId;
21
- return getEventBuffer({
22
- region: region,
23
- flushSize: flushSize,
24
- flushInterval: flushInterval,
25
- bufferSize: bufferSize,
26
- credentials: credentials,
27
- identityId: identityId,
28
- resendLimit: resendLimit,
29
- userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
30
- });
31
- })
32
- .then(function (eventBuffer) { return eventBuffer.flushAll(); })
33
- .catch(function (e) { return logger.warn('Failed to flush events.', e); });
19
+ .then(({ credentials, identityId }) => getEventBuffer({
20
+ region,
21
+ flushSize,
22
+ flushInterval,
23
+ bufferSize,
24
+ credentials,
25
+ identityId,
26
+ resendLimit,
27
+ userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
28
+ }))
29
+ .then(eventBuffer => eventBuffer.flushAll())
30
+ .catch(e => logger.warn('Failed to flush events.', e));
34
31
  };
@@ -6,38 +6,36 @@ import { getAnalyticsUserAgentString, isAnalyticsEnabled, resolveCredentials, }
6
6
  import { fromUtf8 } from '@smithy/util-utf8';
7
7
  import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
8
8
  import { ConsoleLogger } from '@aws-amplify/core';
9
- var logger = new ConsoleLogger('Kinesis');
10
- export var record = function (_a) {
11
- var streamName = _a.streamName, partitionKey = _a.partitionKey, data = _a.data;
9
+ const logger = new ConsoleLogger('Kinesis');
10
+ export const record = ({ streamName, partitionKey, data, }) => {
12
11
  if (!isAnalyticsEnabled()) {
13
12
  logger.debug('Analytics is disabled, event will not be recorded.');
14
13
  return;
15
14
  }
16
- var timestamp = Date.now();
17
- var _b = resolveConfig(), region = _b.region, bufferSize = _b.bufferSize, flushSize = _b.flushSize, flushInterval = _b.flushInterval, resendLimit = _b.resendLimit;
15
+ const timestamp = Date.now();
16
+ const { region, bufferSize, flushSize, flushInterval, resendLimit } = resolveConfig();
18
17
  resolveCredentials()
19
- .then(function (_a) {
20
- var credentials = _a.credentials, identityId = _a.identityId;
21
- var buffer = getEventBuffer({
22
- region: region,
23
- bufferSize: bufferSize,
24
- flushSize: flushSize,
25
- flushInterval: flushInterval,
26
- credentials: credentials,
27
- identityId: identityId,
28
- resendLimit: resendLimit,
18
+ .then(({ credentials, identityId }) => {
19
+ const buffer = getEventBuffer({
20
+ region,
21
+ bufferSize,
22
+ flushSize,
23
+ flushInterval,
24
+ credentials,
25
+ identityId,
26
+ resendLimit,
29
27
  userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
30
28
  });
31
29
  buffer.append({
32
- region: region,
33
- streamName: streamName,
34
- partitionKey: partitionKey,
30
+ region,
31
+ streamName,
32
+ partitionKey,
35
33
  event: ArrayBuffer.isView(data) ? data : fromUtf8(JSON.stringify(data)),
36
- timestamp: timestamp,
34
+ timestamp,
37
35
  retryCount: 0,
38
36
  });
39
37
  })
40
- .catch(function (e) {
38
+ .catch(e => {
41
39
  // An error occured while fetching credentials or persisting the event to the buffer
42
40
  logger.warn('Failed to record event.', e);
43
41
  });
@@ -1,6 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export var DEFAULT_KINESIS_CONFIG = {
3
+ export const DEFAULT_KINESIS_CONFIG = {
4
4
  bufferSize: 1000,
5
5
  flushSize: 100,
6
6
  flushInterval: 5000,
@@ -1,6 +1,5 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { __assign, __awaiter, __generator, __read, __values } from "tslib";
4
3
  import { EventBuffer, groupBy } from '../../../utils';
5
4
  import { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
6
5
  /**
@@ -11,88 +10,58 @@ import { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
11
10
  * Only one active session should exist at any given moment.
12
11
  * When a new session is initiated, the previous ones should be released.
13
12
  * */
14
- var eventBufferMap = {};
15
- var cachedClients = {};
16
- var createKinesisPutRecordsCommand = function (streamName, events) {
17
- return new PutRecordsCommand({
18
- StreamName: streamName,
19
- Records: events.map(function (event) { return ({
20
- PartitionKey: event.partitionKey,
21
- Data: event.event,
22
- }); }),
23
- });
13
+ const eventBufferMap = {};
14
+ const cachedClients = {};
15
+ const createKinesisPutRecordsCommand = (streamName, events) => new PutRecordsCommand({
16
+ StreamName: streamName,
17
+ Records: events.map(event => ({
18
+ PartitionKey: event.partitionKey,
19
+ Data: event.event,
20
+ })),
21
+ });
22
+ const submitEvents = async (events, client, resendLimit) => {
23
+ const groupedByStreamName = Object.entries(groupBy(event => event.streamName, events));
24
+ const requests = groupedByStreamName
25
+ .map(([streamName, events]) => createKinesisPutRecordsCommand(streamName, events))
26
+ .map(command => client.send(command));
27
+ const responses = await Promise.allSettled(requests);
28
+ const failedEvents = responses
29
+ .map((response, i) => response.status === 'rejected' ? groupedByStreamName[i][1] : [])
30
+ .flat();
31
+ return resendLimit
32
+ ? failedEvents
33
+ .filter(event => event.retryCount < resendLimit)
34
+ .map(event => ({ ...event, retryCount: event.retryCount + 1 }))
35
+ .sort((a, b) => a.timestamp - b.timestamp)
36
+ : [];
24
37
  };
25
- var submitEvents = function (events, client, resendLimit) { return __awaiter(void 0, void 0, void 0, function () {
26
- var groupedByStreamName, requests, responses, failedEvents;
27
- return __generator(this, function (_a) {
28
- switch (_a.label) {
29
- case 0:
30
- groupedByStreamName = Object.entries(groupBy(function (event) { return event.streamName; }, events));
31
- requests = groupedByStreamName
32
- .map(function (_a) {
33
- var _b = __read(_a, 2), streamName = _b[0], events = _b[1];
34
- return createKinesisPutRecordsCommand(streamName, events);
35
- })
36
- .map(function (command) { return client.send(command); });
37
- return [4 /*yield*/, Promise.allSettled(requests)];
38
- case 1:
39
- responses = _a.sent();
40
- failedEvents = responses
41
- .map(function (response, i) {
42
- return response.status === 'rejected' ? groupedByStreamName[i][1] : [];
43
- })
44
- .flat();
45
- return [2 /*return*/, resendLimit
46
- ? failedEvents
47
- .filter(function (event) { return event.retryCount < resendLimit; })
48
- .map(function (event) { return (__assign(__assign({}, event), { retryCount: event.retryCount + 1 })); })
49
- .sort(function (a, b) { return a.timestamp - b.timestamp; })
50
- : []];
51
- }
52
- });
53
- }); };
54
- export var getEventBuffer = function (_a) {
55
- var e_1, _b;
56
- var region = _a.region, flushInterval = _a.flushInterval, flushSize = _a.flushSize, bufferSize = _a.bufferSize, credentials = _a.credentials, identityId = _a.identityId, resendLimit = _a.resendLimit, userAgentValue = _a.userAgentValue;
57
- var sessionToken = credentials.sessionToken;
58
- var sessionIdentityKey = [region, sessionToken, identityId]
59
- .filter(function (x) { return !!x; })
60
- .join('-');
38
+ export const getEventBuffer = ({ region, flushInterval, flushSize, bufferSize, credentials, identityId, resendLimit, userAgentValue, }) => {
39
+ const sessionIdentityKey = [region, identityId].filter(x => !!x).join('-');
61
40
  if (!eventBufferMap[sessionIdentityKey]) {
62
- var getKinesisClient = function () {
41
+ const getKinesisClient = () => {
63
42
  if (!cachedClients[sessionIdentityKey]) {
64
43
  cachedClients[sessionIdentityKey] = new KinesisClient({
65
- credentials: credentials,
66
- region: region,
44
+ credentials,
45
+ region,
67
46
  customUserAgent: userAgentValue,
68
47
  });
69
48
  }
70
- return function (events) {
71
- return submitEvents(events, cachedClients[sessionIdentityKey], resendLimit);
72
- };
49
+ return events => submitEvents(events, cachedClients[sessionIdentityKey], resendLimit);
73
50
  };
74
51
  // create new session
75
52
  eventBufferMap[sessionIdentityKey] = new EventBuffer({
76
- flushInterval: flushInterval,
77
- flushSize: flushSize,
78
- bufferSize: bufferSize,
53
+ flushInterval,
54
+ flushSize,
55
+ bufferSize,
79
56
  }, getKinesisClient);
80
57
  // release other sessions
81
- var releaseSessionKeys = Object.keys(eventBufferMap).filter(function (x) { return x !== sessionIdentityKey; });
82
- try {
83
- for (var releaseSessionKeys_1 = __values(releaseSessionKeys), releaseSessionKeys_1_1 = releaseSessionKeys_1.next(); !releaseSessionKeys_1_1.done; releaseSessionKeys_1_1 = releaseSessionKeys_1.next()) {
84
- var releaseSessionKey = releaseSessionKeys_1_1.value;
58
+ const releaseSessionKeys = Object.keys(eventBufferMap).filter(x => x !== sessionIdentityKey);
59
+ for (const releaseSessionKey of releaseSessionKeys) {
60
+ eventBufferMap[releaseSessionKey].flushAll().finally(() => {
85
61
  eventBufferMap[releaseSessionKey].release();
86
62
  delete eventBufferMap[releaseSessionKey];
87
63
  delete cachedClients[releaseSessionKey];
88
- }
89
- }
90
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
91
- finally {
92
- try {
93
- if (releaseSessionKeys_1_1 && !releaseSessionKeys_1_1.done && (_b = releaseSessionKeys_1.return)) _b.call(releaseSessionKeys_1);
94
- }
95
- finally { if (e_1) throw e_1.error; }
64
+ });
96
65
  }
97
66
  }
98
67
  return eventBufferMap[sessionIdentityKey];