import { get_existing_kernel } from "./jupyter" ; import { get_kernel_data } from "./kernel-data" ; export async function handle_request ( path , endpoint , query ? ) { switch ( endpoint ) { case "kernels" : return await get_kernel_data ( ) ; } const kernel = get_existing_kernel ( path ) ; if ( kernel == null ) { if ( endpoint == "signal" ) { return { } ; } throw Error ( `api endpoint ${ endpoint }: no kernel with path '${ path }'` ) ; } switch ( endpoint ) { case "save_ipynb_file" : await kernel . save_ipynb_file ( ) ; return { } ; case "signal" : kernel . signal ( query . signal ) ; return { } ; case "kernel_info" : return await kernel . kernel_info ( ) ; case "more_output" : return kernel . more_output ( query . id ) ; case "complete" : return await kernel . complete ( get_code_and_cursor_pos ( query ) ) ; case "introspect" : const { code , cursor_pos } = get_code_and_cursor_pos ( query ) ; let detail_level = 0 ; if ( query . level != null ) { try { detail_level = parseInt ( query . level ) ; if ( detail_level < 0 ) { detail_level = 0 ; } else if ( detail_level > 1 ) { detail_level = 1 ; } } catch ( err ) { } } return await kernel . introspect ( { code , cursor_pos , detail_level , } ) ; case "store" : const { key , value } = query ; if ( value === undefined ) { return kernel . store . get ( key ) ; } else if ( value === null ) { kernel . store . delete ( key ) ; return { } ; } else { kernel . store . set ( key , value ) ; return { } ; } case "comm" : const [ msg_id , comm_id , data ] = query ; return kernel . send_comm_message_to_kernel ( msg_id , comm_id , data ) ; default : throw Error ( `unknown endpoint "${ endpoint }"` ) ; } } function get_code_and_cursor_pos ( query ) { const code = query . code ; if ( ! code ) { throw Error ( "must specify code" ) ; } let cursor_pos ; if ( query . cursor_pos != null ) { try { cursor_pos = parseInt ( query . cursor_pos ) ; } catch ( error ) { cursor_pos = code . length ; } } else { cursor_pos = code . length ; } return { code , cursor_pos } ; }