INNER CODE UNIT · JavaScript

serialize

middyjs/middy · packages/cloudformation-response/index.js:89

const serialize = (response) => {
	let body = JSON.stringify(response);
	if (Buffer.byteLength(body) <= MAX_RESPONSE_BYTES) return body;
	if (typeof response.Reason === "string") {
		const reason = response.Reason;
		// Everything but the free text is fixed, so what is left of the cap
		// once the note is in place is the budget for whole characters. Walking
		// code points keeps multi-byte text and never splits a surrogate pair.
		response.Reason = TRUNCATED_NOTE;
		let budget =
			MAX_RESPONSE_BYTES - Buffer.byteLength(JSON.stringify(response));
		let kept = "";
		for (const char of reason) {
			const bytes = jsonBytes(char);
			if (bytes > budget) break;
			budget -= bytes;
			kept += char;
		}

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…