UNPKG

@aws-amplify/graphql-model-transformer

Version:
95 lines 3.97 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Retry = exports.safeHandler = exports.submitResponse = exports.MISSING_PHYSICAL_ID_MARKER = exports.CREATE_FAILED_PHYSICAL_ID_MARKER = void 0; const url = __importStar(require("url")); const outbound_1 = require("./outbound"); const util_1 = require("./util"); exports.CREATE_FAILED_PHYSICAL_ID_MARKER = 'AWSCDK::CustomResourceProviderFramework::CREATE_FAILED'; exports.MISSING_PHYSICAL_ID_MARKER = 'AWSCDK::CustomResourceProviderFramework::MISSING_PHYSICAL_ID'; async function submitResponse(status, event, options = {}) { const json = { Status: status, Reason: options.reason || status, StackId: event.StackId, RequestId: event.RequestId, PhysicalResourceId: event.PhysicalResourceId || exports.MISSING_PHYSICAL_ID_MARKER, LogicalResourceId: event.LogicalResourceId, NoEcho: options.noEcho, Data: event.Data, }; (0, util_1.log)('submit response to cloudformation', json); const responseBody = JSON.stringify(json); const parsedUrl = url.parse(event.ResponseURL); const retryOptions = { attempts: 5, sleep: 1000, }; await (0, util_1.withRetries)(retryOptions, outbound_1.httpRequest)({ hostname: parsedUrl.hostname, path: parsedUrl.path, method: 'PUT', headers: { 'content-type': '', 'content-length': Buffer.byteLength(responseBody, 'utf8'), }, }, responseBody); } exports.submitResponse = submitResponse; function safeHandler(block) { return async (event, context) => { if (event.RequestType === 'Delete' && event.PhysicalResourceId === exports.CREATE_FAILED_PHYSICAL_ID_MARKER) { console.log('ignoring DELETE event caused by a failed CREATE event'); await submitResponse('SUCCESS', event); return; } try { await block(event, context); } catch (e) { if (e instanceof Retry) { console.log('retry requested by handler'); throw e; } if (!event.PhysicalResourceId) { if (event.RequestType === 'Create') { console.log('CREATE failed, responding with a marker physical resource id so that the subsequent DELETE will be ignored'); event.PhysicalResourceId = exports.CREATE_FAILED_PHYSICAL_ID_MARKER; } else { console.log(`ERROR: Malformed event. "PhysicalResourceId" is required: ${JSON.stringify({ ...event, ResponseURL: '...' })}`); } } await submitResponse('FAILED', event, { reason: e.message, }); } }; } exports.safeHandler = safeHandler; class Retry extends Error { } exports.Retry = Retry; //# sourceMappingURL=cfn-response.js.map