alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
40 lines (38 loc) • 1.25 kB
JavaScript
import "../chunks/chunk-U5RRZUYZ.js";
// src/preview/ChunkCookieValue.ts
var MAX_COOKIE_LENGTH = 4096;
function chunkCookieValue(cookieName, cookieValue, maxLength = MAX_COOKIE_LENGTH) {
const res = [];
let position = 0, batch = 0;
while (position < cookieValue.length) {
const name = `${cookieName}-${batch}`;
const prefix = `${name}=`;
const amount = maxLength - prefix.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
};