import { Readable } from 'stream' ; import { RequestHandlerWrapper , SavedObject , SavedObjectsExportResultDetails , } from 'src/core/server' ; import { createSplitStream , createMapStream , createFilterStream , createPromiseFromStreams , createListStream , createConcatStream , } from '@kbn/utils' ; import Boom from '@hapi/boom' ; 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 === undefined ) , 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 ( ', ' ) }` ; } } /**
 * Catches errors thrown by saved object route handlers and returns an error
 * with the payload and statusCode of the boom error.
 *
 * This is very close to the core `router.handleLegacyErrors` except that it
 * throws internal errors (statusCode: 500) so that the internal error's
 * message get logged by Core.
 *
 * TODO: Remove once https://github.com/elastic/kibana/issues/65291 is fixed.
 */ export const catchAndReturnBoomErrors = ( handler ) => { return async ( context , request , response ) => { try { return await handler ( context , request , response ) ; } catch ( e ) { if ( Boom . isBoom ( e ) && e . output . statusCode !== 500 ) { return response . customError ( { body : e . output . payload , statusCode : e . output . statusCode , headers : e . output . headers as { [ key ] : string ; } , } ) ; } throw e ; } } ; } ;