import type { KibanaExecutionContext } from '../../types' ; export const BAGGAGE_HEADER = 'x-kbn-context' ; export function getParentContextFrom ( headers ) { const header = headers [ BAGGAGE_HEADER ] ; return parseHeader ( header ) ; } function parseHeader ( header ? ) { if ( ! header ) return undefined ; try { return JSON . parse ( decodeURIComponent ( header ) ) ; } catch ( e ) { return undefined ; } } export const BAGGAGE_MAX_PER_NAME_VALUE_PAIRS = 4096 ; const MAX_BAGGAGE_LENGTH = BAGGAGE_MAX_PER_NAME_VALUE_PAIRS / 4 ; function enforceMaxLength ( header ) { return header . slice ( 0 , MAX_BAGGAGE_LENGTH ) ; } public export interface IExecutionContextContainer { toString ( ) : string ; toJSON ( ) : Readonly < KibanaExecutionContext > ; } function stringify ( ctx ) { const stringifiedCtx = `${ ctx . type }:${ ctx . name }:${ encodeURIComponent ( ctx . id ) }` ; return ctx . parent ? `${ stringify ( ctx . parent ) };${ stringifiedCtx }` : stringifiedCtx ; } export class ExecutionContextContainer implements IExecutionContextContainer { readonly #context : Readonly < KibanaExecutionContext > ; constructor ( context , parent ? ) { this . #context = { parent : parent ?. toJSON ( ) , ... context } ; } toString ( ) { return enforceMaxLength ( stringify ( this . #context ) ) ; } toJSON ( ) { return this . #context ; } }