UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

84 lines (82 loc) 3.8 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_firehose_1 = require("@aws-sdk/client-firehose"); 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 createPutRecordsBatchCommand = (streamName, events) => new client_firehose_1.PutRecordBatchCommand({ DeliveryStreamName: streamName, Records: events.map(event => ({ 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]) => createPutRecordsBatchCommand(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, credentials, identityId, bufferSize, flushSize, flushInterval, resendLimit, userAgentValue, }) => { const sessionIdentityKey = [region, identityId].filter(id => !!id).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 getClient = () => { if (!cachedClient || credentialsHaveChanged) { cachedClients[sessionIdentityKey] = [ new client_firehose_1.FirehoseClient({ region, credentials, customUserAgent: userAgentValue, }), credentials, ]; } const [firehoseClient] = cachedClients[sessionIdentityKey]; return events => submitEvents(events, firehoseClient, resendLimit); }; eventBufferMap[sessionIdentityKey] = new utils_2.EventBuffer({ bufferSize, flushSize, flushInterval, }, getClient); const releaseSessionKeys = Object.keys(eventBufferMap).filter(key => key !== 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