UNPKG

@softchef/cdk-iot-device-management

Version:

IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.

65 lines (64 loc) 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateDiscoveredEndpointInCache = void 0; const requestQueue = {}; const updateDiscoveredEndpointInCache = async (config, options) => new Promise((resolve, reject) => { const { endpointCache } = config; const { cacheKey, commandName, identifiers } = options; const endpoints = endpointCache.get(cacheKey); if (endpoints && endpoints.length === 1 && endpoints[0].Address === "") { if (options.isDiscoveredEndpointRequired) { if (!requestQueue[cacheKey]) requestQueue[cacheKey] = []; requestQueue[cacheKey].push({ resolve, reject }); } else { resolve(); } } else if (endpoints && endpoints.length > 0) { resolve(); } else { const placeholderEndpoints = [{ Address: "", CachePeriodInMinutes: 1 }]; endpointCache.set(cacheKey, placeholderEndpoints); const command = new options.endpointDiscoveryCommandCtor({ Operation: commandName.substr(0, commandName.length - 7), Identifiers: identifiers, }); const handler = command.resolveMiddleware(options.clientStack, config, options.options); handler(command) .then((result) => { endpointCache.set(cacheKey, result.output.Endpoints); if (requestQueue[cacheKey]) { requestQueue[cacheKey].forEach(({ resolve }) => { resolve(); }); delete requestQueue[cacheKey]; } resolve(); }) .catch((error) => { var _a; if (error.name === "InvalidEndpointException" || ((_a = error.$metadata) === null || _a === void 0 ? void 0 : _a.httpStatusCode) === 421) { endpointCache.delete(cacheKey); } const errorToThrow = Object.assign(new Error(`The operation to discover endpoint failed.` + ` Please retry, or provide a custom endpoint and disable endpoint discovery to proceed.`), { reason: error }); if (requestQueue[cacheKey]) { requestQueue[cacheKey].forEach(({ reject }) => { reject(errorToThrow); }); delete requestQueue[cacheKey]; } if (options.isDiscoveredEndpointRequired) { reject(errorToThrow); } else { endpointCache.set(cacheKey, placeholderEndpoints); resolve(); } }); } }); exports.updateDiscoveredEndpointInCache = updateDiscoveredEndpointInCache;