All files / core/blong-browser/src/lib sessionKey.ts

100% Statements 3/3
100% Branches 6/6
100% Functions 2/2
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                              79x         39x     79x    
import type {PermissionMap} from '../types/permission.js';
 
/**
 * What the session may do, as one string.
 *
 * A cookie minted from a session is only good while that session's authority holds: a reader whose
 * role changed must be given a new one, and one whose session merely re-rendered must not be given a
 * second. Comparing two strings is the cheapest way to say which happened — the actor plus the names
 * it was granted, sorted so the same set always spells the same thing (`@@D-836`).
 */
export function sessionKey(
    actorId: string | number | undefined,
    permissions: PermissionMap,
): string {
    const granted =
        typeof permissions === 'boolean'
            ? permissions
                ? '*'
                : ''
            : Object.keys(permissions)
                  .filter(name => permissions[name])
                  .sort()
                  .join(',');
    return `${actorId ?? ''}|${granted}`;
}