alinea
Version:
Headless git-based CMS
37 lines (35 loc) • 1.21 kB
JavaScript
import "../chunks/chunk-NZLE2WMY.js";
// src/preview/ChunkCookieValue.ts
var MAX_COOKIE_LENGTH = 4096;
function chunkCookieValue(cookieName, cookieValue, maxLength = MAX_COOKIE_LENGTH) {
const res = [];
let position = 0;
let batch = 0;
while (position < cookieValue.length) {
const name = `${cookieName}-${batch}`;
const amount = maxLength - name.length;
if (amount <= 0) throw new Error("Max length is not sufficient");
const value = cookieValue.substring(position, position + amount);
position += amount;
batch++;
res.push({ name, value });
}
res.unshift({ name: cookieName, value: String(batch) });
return res;
}
function parseChunkedCookies(cookieName, allCookies) {
const amount = allCookies.find(({ name }) => name === cookieName);
if (!amount) return void 0;
const count = Number(amount.value);
const parts = allCookies.filter(({ name }) => name.startsWith(`${cookieName}-`)).sort((a, b) => {
return a.name.localeCompare(b.name);
});
const slices = parts.slice(0, count).map(({ value }) => value);
if (slices.length !== count) return void 0;
return slices.join("");
}
export {
MAX_COOKIE_LENGTH,
chunkCookieValue,
parseChunkedCookies
};