UNPKG

lambda-live-debugger

Version:

Debug Lambda functions locally like it is running in the cloud

695 lines (683 loc) 25.4 kB
'use strict'; var uuid = require('@smithy/uuid'); const copyDocumentWithTransform = (source, schemaRef, transform = (_) => _) => source; const parseBoolean = (value) => { switch (value) { case "true": return true; case "false": return false; default: throw new Error(`Unable to parse boolean value "${value}"`); } }; const expectBoolean = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "number") { if (value === 0 || value === 1) { logger.warn(stackTraceWarning(`Expected boolean, got ${typeof value}: ${value}`)); } if (value === 0) { return false; } if (value === 1) { return true; } } if (typeof value === "string") { const lower = value.toLowerCase(); if (lower === "false" || lower === "true") { logger.warn(stackTraceWarning(`Expected boolean, got ${typeof value}: ${value}`)); } if (lower === "false") { return false; } if (lower === "true") { return true; } } if (typeof value === "boolean") { return value; } throw new TypeError(`Expected boolean, got ${typeof value}: ${value}`); }; const expectNumber = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "string") { const parsed = parseFloat(value); if (!Number.isNaN(parsed)) { if (String(parsed) !== String(value)) { logger.warn(stackTraceWarning(`Expected number but observed string: ${value}`)); } return parsed; } } if (typeof value === "number") { return value; } throw new TypeError(`Expected number, got ${typeof value}: ${value}`); }; const MAX_FLOAT = Math.ceil(2 ** 127 * (2 - 2 ** -23)); const expectFloat32 = (value) => { const expected = expectNumber(value); if (expected !== undefined && !Number.isNaN(expected) && expected !== Infinity && expected !== -Infinity) { if (Math.abs(expected) > MAX_FLOAT) { throw new TypeError(`Expected 32-bit float, got ${value}`); } } return expected; }; const expectLong = (value) => { if (value === null || value === undefined) { return undefined; } if (Number.isInteger(value) && !Number.isNaN(value)) { return value; } throw new TypeError(`Expected integer, got ${typeof value}: ${value}`); }; const expectInt = expectLong; const expectInt32 = (value) => expectSizedInt(value, 32); const expectShort = (value) => expectSizedInt(value, 16); const expectByte = (value) => expectSizedInt(value, 8); const expectSizedInt = (value, size) => { const expected = expectLong(value); if (expected !== undefined && castInt(expected, size) !== expected) { throw new TypeError(`Expected ${size}-bit integer, got ${value}`); } return expected; }; const castInt = (value, size) => { switch (size) { case 32: return Int32Array.of(value)[0]; case 16: return Int16Array.of(value)[0]; case 8: return Int8Array.of(value)[0]; } }; const expectNonNull = (value, location) => { if (value === null || value === undefined) { if (location) { throw new TypeError(`Expected a non-null value for ${location}`); } throw new TypeError("Expected a non-null value"); } return value; }; const expectObject = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "object" && !Array.isArray(value)) { return value; } const receivedType = Array.isArray(value) ? "array" : typeof value; throw new TypeError(`Expected object, got ${receivedType}: ${value}`); }; const expectString = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value === "string") { return value; } if (["boolean", "number", "bigint"].includes(typeof value)) { logger.warn(stackTraceWarning(`Expected string, got ${typeof value}: ${value}`)); return String(value); } throw new TypeError(`Expected string, got ${typeof value}: ${value}`); }; const expectUnion = (value) => { if (value === null || value === undefined) { return undefined; } const asObject = expectObject(value); const setKeys = Object.entries(asObject) .filter(([, v]) => v != null) .map(([k]) => k); if (setKeys.length === 0) { throw new TypeError(`Unions must have exactly one non-null member. None were found.`); } if (setKeys.length > 1) { throw new TypeError(`Unions must have exactly one non-null member. Keys ${setKeys} were not null.`); } return asObject; }; const strictParseDouble = (value) => { if (typeof value == "string") { return expectNumber(parseNumber(value)); } return expectNumber(value); }; const strictParseFloat = strictParseDouble; const strictParseFloat32 = (value) => { if (typeof value == "string") { return expectFloat32(parseNumber(value)); } return expectFloat32(value); }; const NUMBER_REGEX = /(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)|(-?Infinity)|(NaN)/g; const parseNumber = (value) => { const matches = value.match(NUMBER_REGEX); if (matches === null || matches[0].length !== value.length) { throw new TypeError(`Expected real number, got implicit NaN`); } return parseFloat(value); }; const limitedParseDouble = (value) => { if (typeof value == "string") { return parseFloatString(value); } return expectNumber(value); }; const handleFloat = limitedParseDouble; const limitedParseFloat = limitedParseDouble; const limitedParseFloat32 = (value) => { if (typeof value == "string") { return parseFloatString(value); } return expectFloat32(value); }; const parseFloatString = (value) => { switch (value) { case "NaN": return NaN; case "Infinity": return Infinity; case "-Infinity": return -Infinity; default: throw new Error(`Unable to parse float value: ${value}`); } }; const strictParseLong = (value) => { if (typeof value === "string") { return expectLong(parseNumber(value)); } return expectLong(value); }; const strictParseInt = strictParseLong; const strictParseInt32 = (value) => { if (typeof value === "string") { return expectInt32(parseNumber(value)); } return expectInt32(value); }; const strictParseShort = (value) => { if (typeof value === "string") { return expectShort(parseNumber(value)); } return expectShort(value); }; const strictParseByte = (value) => { if (typeof value === "string") { return expectByte(parseNumber(value)); } return expectByte(value); }; const stackTraceWarning = (message) => { return String(new TypeError(message).stack || message) .split("\n") .slice(0, 5) .filter((s) => !s.includes("stackTraceWarning")) .join("\n"); }; const logger = { warn: console.warn, }; const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; function dateToUtcString(date) { const year = date.getUTCFullYear(); const month = date.getUTCMonth(); const dayOfWeek = date.getUTCDay(); const dayOfMonthInt = date.getUTCDate(); const hoursInt = date.getUTCHours(); const minutesInt = date.getUTCMinutes(); const secondsInt = date.getUTCSeconds(); const dayOfMonthString = dayOfMonthInt < 10 ? `0${dayOfMonthInt}` : `${dayOfMonthInt}`; const hoursString = hoursInt < 10 ? `0${hoursInt}` : `${hoursInt}`; const minutesString = minutesInt < 10 ? `0${minutesInt}` : `${minutesInt}`; const secondsString = secondsInt < 10 ? `0${secondsInt}` : `${secondsInt}`; return `${DAYS[dayOfWeek]}, ${dayOfMonthString} ${MONTHS[month]} ${year} ${hoursString}:${minutesString}:${secondsString} GMT`; } const RFC3339 = new RegExp(/^(\d{4})-(\d{2})-(\d{2})[tT](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?[zZ]$/); const parseRfc3339DateTime = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value !== "string") { throw new TypeError("RFC-3339 date-times must be expressed as strings"); } const match = RFC3339.exec(value); if (!match) { throw new TypeError("Invalid RFC-3339 date-time value"); } const [_, yearStr, monthStr, dayStr, hours, minutes, seconds, fractionalMilliseconds] = match; const year = strictParseShort(stripLeadingZeroes(yearStr)); const month = parseDateValue(monthStr, "month", 1, 12); const day = parseDateValue(dayStr, "day", 1, 31); return buildDate(year, month, day, { hours, minutes, seconds, fractionalMilliseconds }); }; const RFC3339_WITH_OFFSET$1 = new RegExp(/^(\d{4})-(\d{2})-(\d{2})[tT](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(([-+]\d{2}\:\d{2})|[zZ])$/); const parseRfc3339DateTimeWithOffset = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value !== "string") { throw new TypeError("RFC-3339 date-times must be expressed as strings"); } const match = RFC3339_WITH_OFFSET$1.exec(value); if (!match) { throw new TypeError("Invalid RFC-3339 date-time value"); } const [_, yearStr, monthStr, dayStr, hours, minutes, seconds, fractionalMilliseconds, offsetStr] = match; const year = strictParseShort(stripLeadingZeroes(yearStr)); const month = parseDateValue(monthStr, "month", 1, 12); const day = parseDateValue(dayStr, "day", 1, 31); const date = buildDate(year, month, day, { hours, minutes, seconds, fractionalMilliseconds }); if (offsetStr.toUpperCase() != "Z") { date.setTime(date.getTime() - parseOffsetToMilliseconds(offsetStr)); } return date; }; const IMF_FIXDATE$1 = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/); const RFC_850_DATE$1 = new RegExp(/^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/); const ASC_TIME$1 = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( [1-9]|\d{2}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? (\d{4})$/); const parseRfc7231DateTime = (value) => { if (value === null || value === undefined) { return undefined; } if (typeof value !== "string") { throw new TypeError("RFC-7231 date-times must be expressed as strings"); } let match = IMF_FIXDATE$1.exec(value); if (match) { const [_, dayStr, monthStr, yearStr, hours, minutes, seconds, fractionalMilliseconds] = match; return buildDate(strictParseShort(stripLeadingZeroes(yearStr)), parseMonthByShortName(monthStr), parseDateValue(dayStr, "day", 1, 31), { hours, minutes, seconds, fractionalMilliseconds }); } match = RFC_850_DATE$1.exec(value); if (match) { const [_, dayStr, monthStr, yearStr, hours, minutes, seconds, fractionalMilliseconds] = match; return adjustRfc850Year(buildDate(parseTwoDigitYear(yearStr), parseMonthByShortName(monthStr), parseDateValue(dayStr, "day", 1, 31), { hours, minutes, seconds, fractionalMilliseconds, })); } match = ASC_TIME$1.exec(value); if (match) { const [_, monthStr, dayStr, hours, minutes, seconds, fractionalMilliseconds, yearStr] = match; return buildDate(strictParseShort(stripLeadingZeroes(yearStr)), parseMonthByShortName(monthStr), parseDateValue(dayStr.trimLeft(), "day", 1, 31), { hours, minutes, seconds, fractionalMilliseconds }); } throw new TypeError("Invalid RFC-7231 date-time value"); }; const parseEpochTimestamp = (value) => { if (value === null || value === undefined) { return undefined; } let valueAsDouble; if (typeof value === "number") { valueAsDouble = value; } else if (typeof value === "string") { valueAsDouble = strictParseDouble(value); } else if (typeof value === "object" && value.tag === 1) { valueAsDouble = value.value; } else { throw new TypeError("Epoch timestamps must be expressed as floating point numbers or their string representation"); } if (Number.isNaN(valueAsDouble) || valueAsDouble === Infinity || valueAsDouble === -Infinity) { throw new TypeError("Epoch timestamps must be valid, non-Infinite, non-NaN numerics"); } return new Date(Math.round(valueAsDouble * 1000)); }; const buildDate = (year, month, day, time) => { const adjustedMonth = month - 1; validateDayOfMonth(year, adjustedMonth, day); return new Date(Date.UTC(year, adjustedMonth, day, parseDateValue(time.hours, "hour", 0, 23), parseDateValue(time.minutes, "minute", 0, 59), parseDateValue(time.seconds, "seconds", 0, 60), parseMilliseconds(time.fractionalMilliseconds))); }; const parseTwoDigitYear = (value) => { const thisYear = new Date().getUTCFullYear(); const valueInThisCentury = Math.floor(thisYear / 100) * 100 + strictParseShort(stripLeadingZeroes(value)); if (valueInThisCentury < thisYear) { return valueInThisCentury + 100; } return valueInThisCentury; }; const FIFTY_YEARS_IN_MILLIS = 50 * 365 * 24 * 60 * 60 * 1000; const adjustRfc850Year = (input) => { if (input.getTime() - new Date().getTime() > FIFTY_YEARS_IN_MILLIS) { return new Date(Date.UTC(input.getUTCFullYear() - 100, input.getUTCMonth(), input.getUTCDate(), input.getUTCHours(), input.getUTCMinutes(), input.getUTCSeconds(), input.getUTCMilliseconds())); } return input; }; const parseMonthByShortName = (value) => { const monthIdx = MONTHS.indexOf(value); if (monthIdx < 0) { throw new TypeError(`Invalid month: ${value}`); } return monthIdx + 1; }; const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; const validateDayOfMonth = (year, month, day) => { let maxDays = DAYS_IN_MONTH[month]; if (month === 1 && isLeapYear(year)) { maxDays = 29; } if (day > maxDays) { throw new TypeError(`Invalid day for ${MONTHS[month]} in ${year}: ${day}`); } }; const isLeapYear = (year) => { return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); }; const parseDateValue = (value, type, lower, upper) => { const dateVal = strictParseByte(stripLeadingZeroes(value)); if (dateVal < lower || dateVal > upper) { throw new TypeError(`${type} must be between ${lower} and ${upper}, inclusive`); } return dateVal; }; const parseMilliseconds = (value) => { if (value === null || value === undefined) { return 0; } return strictParseFloat32("0." + value) * 1000; }; const parseOffsetToMilliseconds = (value) => { const directionStr = value[0]; let direction = 1; if (directionStr == "+") { direction = 1; } else if (directionStr == "-") { direction = -1; } else { throw new TypeError(`Offset direction, ${directionStr}, must be "+" or "-"`); } const hour = Number(value.substring(1, 3)); const minute = Number(value.substring(4, 6)); return direction * (hour * 60 + minute) * 60 * 1000; }; const stripLeadingZeroes = (value) => { let idx = 0; while (idx < value.length - 1 && value.charAt(idx) === "0") { idx++; } if (idx === 0) { return value; } return value.slice(idx); }; const LazyJsonString = function LazyJsonString(val) { const str = Object.assign(new String(val), { deserializeJSON() { return JSON.parse(String(val)); }, toString() { return String(val); }, toJSON() { return String(val); }, }); return str; }; LazyJsonString.from = (object) => { if (object && typeof object === "object" && (object instanceof LazyJsonString || "deserializeJSON" in object)) { return object; } else if (typeof object === "string" || Object.getPrototypeOf(object) === String.prototype) { return LazyJsonString(String(object)); } return LazyJsonString(JSON.stringify(object)); }; LazyJsonString.fromObject = LazyJsonString.from; function quoteHeader(part) { if (part.includes(",") || part.includes('"')) { part = `"${part.replace(/"/g, '\\"')}"`; } return part; } const ddd = `(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)(?:[ne|u?r]?s?day)?`; const mmm = `(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)`; const time = `(\\d?\\d):(\\d{2}):(\\d{2})(?:\\.(\\d+))?`; const date = `(\\d?\\d)`; const year = `(\\d{4})`; const RFC3339_WITH_OFFSET = new RegExp(/^(\d{4})-(\d\d)-(\d\d)[tT](\d\d):(\d\d):(\d\d)(\.(\d+))?(([-+]\d\d:\d\d)|[zZ])$/); const IMF_FIXDATE = new RegExp(`^${ddd}, ${date} ${mmm} ${year} ${time} GMT$`); const RFC_850_DATE = new RegExp(`^${ddd}, ${date}-${mmm}-(\\d\\d) ${time} GMT$`); const ASC_TIME = new RegExp(`^${ddd} ${mmm} ( [1-9]|\\d\\d) ${time} ${year}$`); const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const _parseEpochTimestamp = (value) => { if (value == null) { return void 0; } let num = NaN; if (typeof value === "number") { num = value; } else if (typeof value === "string") { if (!/^-?\d*\.?\d+$/.test(value)) { throw new TypeError(`parseEpochTimestamp - numeric string invalid.`); } num = Number.parseFloat(value); } else if (typeof value === "object" && value.tag === 1) { num = value.value; } if (isNaN(num) || Math.abs(num) === Infinity) { throw new TypeError("Epoch timestamps must be valid finite numbers."); } return new Date(Math.round(num * 1000)); }; const _parseRfc3339DateTimeWithOffset = (value) => { if (value == null) { return void 0; } if (typeof value !== "string") { throw new TypeError("RFC3339 timestamps must be strings"); } const matches = RFC3339_WITH_OFFSET.exec(value); if (!matches) { throw new TypeError(`Invalid RFC3339 timestamp format ${value}`); } const [, yearStr, monthStr, dayStr, hours, minutes, seconds, , ms, offsetStr] = matches; range(monthStr, 1, 12); range(dayStr, 1, 31); range(hours, 0, 23); range(minutes, 0, 59); range(seconds, 0, 60); const date = new Date(Date.UTC(Number(yearStr), Number(monthStr) - 1, Number(dayStr), Number(hours), Number(minutes), Number(seconds), Number(ms) ? Math.round(parseFloat(`0.${ms}`) * 1000) : 0)); date.setUTCFullYear(Number(yearStr)); if (offsetStr.toUpperCase() != "Z") { const [, sign, offsetH, offsetM] = /([+-])(\d\d):(\d\d)/.exec(offsetStr) || [void 0, "+", 0, 0]; const scalar = sign === "-" ? 1 : -1; date.setTime(date.getTime() + scalar * (Number(offsetH) * 60 * 60 * 1000 + Number(offsetM) * 60 * 1000)); } return date; }; const _parseRfc7231DateTime = (value) => { if (value == null) { return void 0; } if (typeof value !== "string") { throw new TypeError("RFC7231 timestamps must be strings."); } let day; let month; let year; let hour; let minute; let second; let fraction; let matches; if ((matches = IMF_FIXDATE.exec(value))) { [, day, month, year, hour, minute, second, fraction] = matches; } else if ((matches = RFC_850_DATE.exec(value))) { [, day, month, year, hour, minute, second, fraction] = matches; year = (Number(year) + 1900).toString(); } else if ((matches = ASC_TIME.exec(value))) { [, month, day, hour, minute, second, fraction, year] = matches; } if (year && second) { const timestamp = Date.UTC(Number(year), months.indexOf(month), Number(day), Number(hour), Number(minute), Number(second), fraction ? Math.round(parseFloat(`0.${fraction}`) * 1000) : 0); range(day, 1, 31); range(hour, 0, 23); range(minute, 0, 59); range(second, 0, 60); const date = new Date(timestamp); date.setUTCFullYear(Number(year)); return date; } throw new TypeError(`Invalid RFC7231 date-time value ${value}.`); }; function range(v, min, max) { const _v = Number(v); if (_v < min || _v > max) { throw new Error(`Value ${_v} out of range [${min}, ${max}]`); } } function splitEvery(value, delimiter, numDelimiters) { if (numDelimiters <= 0 || !Number.isInteger(numDelimiters)) { throw new Error("Invalid number of delimiters (" + numDelimiters + ") for splitEvery."); } const segments = value.split(delimiter); if (numDelimiters === 1) { return segments; } const compoundSegments = []; let currentSegment = ""; for (let i = 0; i < segments.length; i++) { if (currentSegment === "") { currentSegment = segments[i]; } else { currentSegment += delimiter + segments[i]; } if ((i + 1) % numDelimiters === 0) { compoundSegments.push(currentSegment); currentSegment = ""; } } if (currentSegment !== "") { compoundSegments.push(currentSegment); } return compoundSegments; } const splitHeader = (value) => { const z = value.length; const values = []; let withinQuotes = false; let prevChar = undefined; let anchor = 0; for (let i = 0; i < z; ++i) { const char = value[i]; switch (char) { case `"`: if (prevChar !== "\\") { withinQuotes = !withinQuotes; } break; case ",": if (!withinQuotes) { values.push(value.slice(anchor, i)); anchor = i + 1; } break; } prevChar = char; } values.push(value.slice(anchor)); return values.map((v) => { v = v.trim(); const z = v.length; if (z < 2) { return v; } if (v[0] === `"` && v[z - 1] === `"`) { v = v.slice(1, z - 1); } return v.replace(/\\"/g, '"'); }); }; const format = /^-?\d*(\.\d+)?$/; class NumericValue { string; type; constructor(string, type) { this.string = string; this.type = type; if (!format.test(string)) { throw new Error(`@smithy/core/serde - NumericValue must only contain [0-9], at most one decimal point ".", and an optional negation prefix "-".`); } } toString() { return this.string; } static [Symbol.hasInstance](object) { if (!object || typeof object !== "object") { return false; } const _nv = object; return NumericValue.prototype.isPrototypeOf(object) || (_nv.type === "bigDecimal" && format.test(_nv.string)); } } function nv(input) { return new NumericValue(String(input), "bigDecimal"); } exports.generateIdempotencyToken = uuid.v4; exports.LazyJsonString = LazyJsonString; exports.NumericValue = NumericValue; exports._parseEpochTimestamp = _parseEpochTimestamp; exports._parseRfc3339DateTimeWithOffset = _parseRfc3339DateTimeWithOffset; exports._parseRfc7231DateTime = _parseRfc7231DateTime; exports.copyDocumentWithTransform = copyDocumentWithTransform; exports.dateToUtcString = dateToUtcString; exports.expectBoolean = expectBoolean; exports.expectByte = expectByte; exports.expectFloat32 = expectFloat32; exports.expectInt = expectInt; exports.expectInt32 = expectInt32; exports.expectLong = expectLong; exports.expectNonNull = expectNonNull; exports.expectNumber = expectNumber; exports.expectObject = expectObject; exports.expectShort = expectShort; exports.expectString = expectString; exports.expectUnion = expectUnion; exports.handleFloat = handleFloat; exports.limitedParseDouble = limitedParseDouble; exports.limitedParseFloat = limitedParseFloat; exports.limitedParseFloat32 = limitedParseFloat32; exports.logger = logger; exports.nv = nv; exports.parseBoolean = parseBoolean; exports.parseEpochTimestamp = parseEpochTimestamp; exports.parseRfc3339DateTime = parseRfc3339DateTime; exports.parseRfc3339DateTimeWithOffset = parseRfc3339DateTimeWithOffset; exports.parseRfc7231DateTime = parseRfc7231DateTime; exports.quoteHeader = quoteHeader; exports.splitEvery = splitEvery; exports.splitHeader = splitHeader; exports.strictParseByte = strictParseByte; exports.strictParseDouble = strictParseDouble; exports.strictParseFloat = strictParseFloat; exports.strictParseFloat32 = strictParseFloat32; exports.strictParseInt = strictParseInt; exports.strictParseInt32 = strictParseInt32; exports.strictParseLong = strictParseLong; exports.strictParseShort = strictParseShort;