UNPKG

4.6 kBJavaScriptView Raw
1/*
2 * Copyright 2017-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13var __extends = (this && this.__extends) || (function () {
14 var extendStatics = function (d, b) {
15 extendStatics = Object.setPrototypeOf ||
16 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
17 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
18 return extendStatics(d, b);
19 };
20 return function (d, b) {
21 extendStatics(d, b);
22 function __() { this.constructor = d; }
23 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
24 };
25})();
26import { ConsoleLogger as Logger } from '@aws-amplify/core';
27import { AWSKinesisProvider } from './AWSKinesisProvider';
28import { PutRecordBatchCommand, FirehoseClient, } from '@aws-sdk/client-firehose';
29import { fromUtf8 } from '@aws-sdk/util-utf8-browser';
30var logger = new Logger('AWSKineisFirehoseProvider');
31var AWSKinesisFirehoseProvider = /** @class */ (function (_super) {
32 __extends(AWSKinesisFirehoseProvider, _super);
33 function AWSKinesisFirehoseProvider(config) {
34 return _super.call(this, config) || this;
35 }
36 /**
37 * get provider name of the plugin
38 */
39 AWSKinesisFirehoseProvider.prototype.getProviderName = function () {
40 return 'AWSKinesisFirehose';
41 };
42 AWSKinesisFirehoseProvider.prototype._sendEvents = function (group) {
43 var _this = this;
44 if (group.length === 0) {
45 return;
46 }
47 var _a = group[0], config = _a.config, credentials = _a.credentials;
48 var initClients = this._init(config, credentials);
49 if (!initClients)
50 return false;
51 var records = {};
52 group.map(function (params) {
53 // split by streamName
54 var evt = params.event;
55 var streamName = evt.streamName, data = evt.data;
56 if (records[streamName] === undefined) {
57 records[streamName] = [];
58 }
59 var bufferData = data && typeof data !== 'string' ? JSON.stringify(data) : data;
60 var Data = fromUtf8(bufferData);
61 var record = { Data: Data };
62 records[streamName].push(record);
63 });
64 Object.keys(records).map(function (streamName) {
65 logger.debug('putting records to kinesis', streamName, 'with records', records[streamName]);
66 _this._kinesisFirehose
67 .send(new PutRecordBatchCommand({
68 Records: records[streamName],
69 DeliveryStreamName: streamName,
70 }))
71 .then(function (res) { return logger.debug('Upload records to stream', streamName); })
72 .catch(function (err) { return logger.debug('Failed to upload records to Kinesis', err); });
73 });
74 };
75 AWSKinesisFirehoseProvider.prototype._init = function (config, credentials) {
76 logger.debug('init clients');
77 if (this._kinesisFirehose &&
78 this._config.credentials &&
79 this._config.credentials.sessionToken === credentials.sessionToken &&
80 this._config.credentials.identityId === credentials.identityId) {
81 logger.debug('no change for analytics config, directly return from init');
82 return true;
83 }
84 this._config.credentials = credentials;
85 var region = config.region;
86 return this._initFirehose(region, credentials);
87 };
88 AWSKinesisFirehoseProvider.prototype._initFirehose = function (region, credentials) {
89 logger.debug('initialize kinesis firehose with credentials', credentials);
90 this._kinesisFirehose = new FirehoseClient({
91 apiVersion: '2015-08-04',
92 region: region,
93 credentials: credentials,
94 });
95 return true;
96 };
97 return AWSKinesisFirehoseProvider;
98}(AWSKinesisProvider));
99export { AWSKinesisFirehoseProvider };
100/**
101 * @deprecated use named import
102 */
103export default AWSKinesisFirehoseProvider;
104//# sourceMappingURL=AWSKinesisFirehoseProvider.js.map
\No newline at end of file