import { Readable } from 'stream' ; import { SavedObject , SavedObjectsExportResultDetails } from 'src/core/server' ; import { createSplitStream , createMapStream , createFilterStream , createPromiseFromStreams , createListStream , createConcatStream , } from '../../utils/streams' ; export async function createSavedObjectsStreamFromNdJson ( ndJsonStream ) { const savedObjects = await createPromiseFromStreams ( [ ndJsonStream , createSplitStream ( '\n' ) , createMapStream ( ( str ) => { if ( str && str . trim ( ) !== '' ) { return JSON . parse ( str ) ; } } ) , createFilterStream < SavedObject | SavedObjectsExportResultDetails > ( ( obj ) => ! ! obj && ! ( obj as SavedObjectsExportResultDetails ) . exportedCount ) , createConcatStream ( [ ] ) , ] ) ; return createListStream ( savedObjects ) ; } export function validateTypes ( types , supportedTypes ) { const invalidTypes = types . filter ( ( t ) => ! supportedTypes . includes ( t ) ) ; if ( invalidTypes . length ) { return `Trying to export non-exportable type(s): ${ invalidTypes . join ( ', ' ) }` ; } } export function validateObjects ( objects , supportedTypes ) { const invalidObjects = objects . filter ( ( obj ) => ! supportedTypes . includes ( obj . type ) ) ; if ( invalidObjects . length ) { return `Trying to export object(s) with non-exportable types: ${ invalidObjects . map ( ( obj ) => `${ obj . type }:${ obj . id }` ) . join ( ', ' ) }` ; } }