UNPKG

zephyr-agent

Version:
41 lines (40 loc) • 1.55 kB
import "node:module"; function stringifyId(value) { if ('number' == typeof value) return String(value); return 'string' == typeof value && value.trim() ? value.trim() : void 0; } function decodeJwtPayload(token) { const payload = token?.split('.')[1]; if (!payload) return; try { return JSON.parse(Buffer.from(toBase64(payload), 'base64').toString('utf8')); } catch { return; } } function getEmailClaims(payload, keys) { if (!payload) return []; return getEmails(keys.map((key)=>payload[key])); } function getStringClaim(payload, keys) { if (!payload) return; return keys.map((key)=>stringifyId(payload[key])).find((value)=>Boolean(value)); } function getEmails(values) { return Array.from(new Set(values.map((value)=>getEmail(value)).filter((email)=>Boolean(email)))); } function getEmail(value) { if ('string' != typeof value) return; const trimmed = value.trim(); return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed) ? trimmed : void 0; } function claimMatchesEnv(payload, claim, expected) { if (!expected || void 0 === payload[claim] || null === payload[claim]) return true; return String(payload[claim]) === expected; } function toBase64(base64Url) { const normalized = base64Url.replace(/-/g, '+').replace(/_/g, '/'); return normalized.padEnd(normalized.length + (4 - normalized.length % 4) % 4, '='); } export { claimMatchesEnv, decodeJwtPayload, getEmail, getEmailClaims, getEmails, getStringClaim, stringifyId }; //# sourceMappingURL=utils.mjs.map