@nuxt/nitro-server
Version:
39 lines (38 loc) • 1.08 kB
JavaScript
//#region src/runtime/utils/island-props.ts
/** @internal */
const MAX_ISLAND_BODY_BYTES = 65536;
/** @internal */
const MAX_ISLAND_PROP_DEPTH = 64;
/**
* Whether the bracket nesting of a JSON-ish string exceeds `maxDepth`, in a single linear
* pass. Brackets inside string values are ignored.
*
* @internal
*/
function exceedsMaxDepth(raw, maxDepth = 64) {
let depth = 0;
let inString = false;
let escaped = false;
for (let i = 0; i < raw.length; i++) {
const ch = raw[i];
if (inString) {
if (escaped) escaped = false;
else if (ch === "\\") escaped = true;
else if (ch === "\"") inString = false;
continue;
}
if (ch === "\"") inString = true;
else if (ch === "{" || ch === "[") {
if (++depth > maxDepth) return true;
} else if (ch === "}" || ch === "]") {
if (depth > 0) depth--;
}
}
return false;
}
/** @internal */
function exceedsMaxBytes(raw, maxBytes = MAX_ISLAND_BODY_BYTES) {
return Buffer.byteLength(raw, "utf8") > maxBytes;
}
//#endregion
export { MAX_ISLAND_BODY_BYTES, MAX_ISLAND_PROP_DEPTH, exceedsMaxBytes, exceedsMaxDepth };