INNER CODE UNIT · JavaScript
jsonBytes
middyjs/middy · packages/cloudformation-response/index.js:87
const jsonBytes = (char) => Buffer.byteLength(JSON.stringify(char)) - 2;
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;