import { createLogger } from '../../logger' ; import { TYPE_JSON , TYPE_PYTHON , TYPE_ZIP } from './constants' ; import type { ProtocolFile , ProtocolData , ProtocolType } from './types' ; const log = createLogger ( __filename ) ; const RE_JSON_EXT = /\.json$/i ; const RE_PY_EXT = /\.py$/i ; const RE_ZIP_EXT = /\.zip$/i ; export function filenameToType ( filename ) { if ( RE_JSON_EXT . test ( filename ) ) return TYPE_JSON ; if ( RE_PY_EXT . test ( filename ) ) return TYPE_PYTHON ; if ( RE_ZIP_EXT . test ( filename ) ) return TYPE_ZIP ; return null ; } export function fileToProtocolFile ( file ) { return { name : file . name , type : filenameToType ( file . name ) , lastModified : file . lastModified , } ; } export function parseProtocolData ( file , contents , metadata ? ) { if ( fileIsJson ( file ) ) { try { return JSON . parse ( contents ) ; } catch ( e ) { log . warn ( 'Failed to parse JSON' , { contents , message : e . message } ) ; } } else if ( metadata != null ) { return { metadata } ; } return null ; } export function fileIsPython ( file ) { return file . type === TYPE_PYTHON || file . type == null ; } export function fileIsJson ( file ) { return file . type === TYPE_JSON ; } export function fileIsBundle ( file ) { return file . type === TYPE_ZIP ; } export function fileIsBinary ( file ) { return fileIsBundle ( file ) ; }