UNPKG

@goldstack/utils-aws-lambda

Version:

Utilities for deploying AWS Lambda functions

54 lines 1.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateFunctionName = void 0; const crypto_1 = __importDefault(require("crypto")); const INDEX_MARKER = '__index'; const DEFAULT_MARKER = '__default'; const sanitiseFunctionName = (input) => input .replace(/\//g, '-') .replace(/\$/g, '_') .replace(/\{/g, '_') .replace(/\}/g, '_') .replace(/\+/g, '_') .replace(/[^\w\-_]/gi, '_'); /** * Generates a valid function name for a route */ const generateFunctionName = (prefix, config) => { let name = config.name; if (name === '$default') { name = DEFAULT_MARKER; } if (name === '$index') { name = INDEX_MARKER; } name = sanitiseFunctionName(name); const segments = config.path.split('/').filter(Boolean); // remove empty segment from start let pathPrefix = ''; if (segments.length > 1) { pathPrefix = sanitiseFunctionName(segments.join('-')); } else if (segments.length === 1 && name === INDEX_MARKER) { // e.g. /health/$index.ts → ['health'] pathPrefix = sanitiseFunctionName(segments[0]); } if (pathPrefix && name === INDEX_MARKER) { pathPrefix += '-index'; } let fullName = [prefix, pathPrefix, name].filter(Boolean).join('-'); fullName = sanitiseFunctionName(fullName); // Lambda function name limit if (fullName.length > 64) { fullName = `${fullName.substring(0, 40)}-${crypto_1.default .createHash('md5') .update(fullName) .digest('hex') .substring(0, 20)}`; } return fullName; }; exports.generateFunctionName = generateFunctionName; //# sourceMappingURL=generateFunctionName.js.map