UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

86 lines (84 loc) 3.87 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.getEventBuffer = void 0; const utils_1 = require("@aws-amplify/core/internals/utils"); const client_kinesis_1 = require("@aws-sdk/client-kinesis"); const utils_2 = require("../../../utils"); /** * These Records hold cached event buffers and AWS clients. * The hash key is determined by the region and session, * consisting of a combined value comprising [region, sessionToken, identityId]. * * Only one active session should exist at any given moment. * When a new session is initiated, the previous ones should be released. * */ const eventBufferMap = {}; const cachedClients = {}; const createKinesisPutRecordsCommand = (streamName, events) => new client_kinesis_1.PutRecordsCommand({ StreamName: streamName, Records: events.map(event => ({ PartitionKey: event.partitionKey, Data: event.event, })), }); const submitEvents = async (events, client, resendLimit) => { const groupedByStreamName = Object.entries((0, utils_2.groupBy)(event => event.streamName, events)); const requests = groupedByStreamName .map(([streamName, groupedEvents]) => createKinesisPutRecordsCommand(streamName, groupedEvents)) .map(command => client.send(command)); const responses = await Promise.allSettled(requests); const failedEvents = responses .map((response, i) => response.status === 'rejected' ? groupedByStreamName[i][1] : []) .flat(); return resendLimit ? failedEvents .filter(event => event.retryCount < resendLimit) .map(event => ({ ...event, retryCount: event.retryCount + 1 })) .sort((a, b) => a.timestamp - b.timestamp) : []; }; const getEventBuffer = ({ region, flushInterval, flushSize, bufferSize, credentials, identityId, resendLimit, userAgentValue, }) => { const sessionIdentityKey = [region, identityId].filter(x => !!x).join('-'); const [cachedClient, cachedCredentials] = cachedClients[sessionIdentityKey] ?? []; let credentialsHaveChanged = false; // Check if credentials have changed for the cached client if (cachedClient) { credentialsHaveChanged = (0, utils_1.haveCredentialsChanged)(cachedCredentials, credentials); } if (!eventBufferMap[sessionIdentityKey] || credentialsHaveChanged) { const getKinesisClient = () => { if (!cachedClient || credentialsHaveChanged) { cachedClients[sessionIdentityKey] = [ new client_kinesis_1.KinesisClient({ credentials, region, customUserAgent: userAgentValue, }), credentials, ]; } const [kinesisClient] = cachedClients[sessionIdentityKey]; return events => submitEvents(events, kinesisClient, resendLimit); }; // create new session eventBufferMap[sessionIdentityKey] = new utils_2.EventBuffer({ flushInterval, flushSize, bufferSize, }, getKinesisClient); // release other sessions const releaseSessionKeys = Object.keys(eventBufferMap).filter(x => x !== sessionIdentityKey); for (const releaseSessionKey of releaseSessionKeys) { eventBufferMap[releaseSessionKey].flushAll().finally(() => { eventBufferMap[releaseSessionKey].release(); delete eventBufferMap[releaseSessionKey]; delete cachedClients[releaseSessionKey]; }); } } return eventBufferMap[sessionIdentityKey]; }; exports.getEventBuffer = getEventBuffer; //# sourceMappingURL=getEventBuffer.js.map