import JSONStream from 'JSONStream' ; import { LOCALE } from '../stores/SettingsStore' ; export interface Port { port : number ; protocol : string ; } param portString returns { { port : number , protocol } } export function normalizePort ( portString ) { if ( ! portString . includes ( '/' ) ) { throw new Error ( 'Invalid port string' ) ; } const [ port , protocol ] = portString . split ( '/' ) ; const parsedPort = parseInt ( port ) ; if ( isNaN ( parsedPort ) ) { throw new Error ( 'Port is NaN' ) ; } return { port : parseInt ( port ) , protocol } ; } param name returns { string } export function normalizeContainerName ( name ) { return name . replace ( '/' , '' ) ; } param imageId returns { string } export function normalizeImageId ( imageId ) { return imageId . replace ( 'sha256:' , '' ) ; } param repoTags returns { name export function parseRepoTags ( repoTags ) { if ( repoTags == null || repoTags . length === 0 ) { return null ; } repoTags = repoTags . map ( tag => tag . includes ( '/' ) ? tag . split ( '/' ) . pop ( ) : tag ) ; return { name : repoTags [ 0 ] . split ( ':' ) [ 0 ] , tags : repoTags . map ( repoTag => repoTag . split ( ':' ) [ 1 ] ) } ; } export function parseLocale ( locale ) { let [ language , country ] = LOCALE [ locale ] . split ( '_' ) ; return { country , language : language . toLowerCase ( ) , fullLocale : `${ language . toLowerCase ( ) }_${ country }` , } ; } export function dockerStream ( stream , onFinished , onProgress ) { var parser = JSONStream . parse ( ) , output = [ ] ; parser . on ( 'root' , onStreamEvent ) ; parser . on ( 'error' , onStreamError ) ; parser . on ( 'end' , onStreamEnd ) ; stream . pipe ( parser ) ; function onStreamEvent ( evt ) { if ( ! ( evt instanceof Object ) ) { evt = { } ; } output . push ( evt ) ; if ( evt . error ) { return onStreamError ( evt . error ) ; } if ( onProgress ) { onProgress ( evt ) ; } } function onStreamError ( err ) { parser . removeListener ( 'root' , onStreamEvent ) ; parser . removeListener ( 'error' , onStreamError ) ; parser . removeListener ( 'end' , onStreamEnd ) ; onFinished ( err , output ) ; } function onStreamEnd ( ) { onFinished ( null , output ) ; } } param bytes returns { { size : number , unit : ( string | string | string | string | string | string | string | string ) } } export function parseBytes ( bytes ) { let i = - 1 ; const byteUnits = [ 'kB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] ; do { bytes = bytes / 1024 ; i ++ ; } while ( bytes > 1024 ) ; return { size : parseFloat ( Math . max ( bytes , 0.1 ) . toFixed ( 1 ) ) , unit : byteUnits [ i ] } ; }