import fs from "fs" ; import path from "path" ; import { GenericKey } from "./types" ; import slugify from "slugify" ; param export const parseQuerystring = ( url ) : { [ id ] : string ; } => { let options = { } ; const questionMark = url . indexOf ( "?" ) ; if ( questionMark !== - 1 ) { options = url . substring ( questionMark + 1 ) . split ( "&" ) . map ( pair => pair . split ( "=" ) ) . reduce ( ( acc , val ) => { acc [ val [ 0 ] ] = val [ 1 ] ; return acc ; } , { } ) ; } return options ; } ; param url export const parseUrl = ( url ) : { type : string ; part1 : string ; part2 : string ; part3 : string ; bucketName : string ; queryString : { [ key ] : string ; } ; } => { if ( url === "" || typeof url === "undefined" ) { throw new Error ( "please provide a configuration url" ) ; } const type = url . substring ( 0 , url . indexOf ( "://" ) ) ; let config = url . substring ( url . indexOf ( "://" ) + 3 ) ; const at = config . indexOf ( "@" ) ; const questionMark = config . indexOf ( "?" ) ; const colon = config . indexOf ( ":" ) ; let part1 = "" ; let part2 = "" ; let part3 = "" ; let bucketName = "" ; const queryString = parseQuerystring ( url ) ; if ( questionMark !== - 1 ) { config = config . substring ( 0 , questionMark ) ; } let bucketString = "" ; if ( at !== - 1 ) { bucketString = config . substring ( at + 1 ) ; const slash = bucketString . indexOf ( "/" ) ; if ( slash !== - 1 ) { bucketName = bucketString . substring ( slash + 1 ) ; part3 = bucketString . substring ( 0 , slash ) ; } else { bucketName = bucketString ; } config = config . substring ( 0 , at ) ; } if ( colon !== - 1 ) { [ part1 , part2 ] = config . split ( ":" ) ; } else { part1 = config ; } return { type , part1 , part2 , part3 , bucketName , queryString } ; } ; param s export const parseIntFromString = ( s ) : number => { if ( s . startsWith ( "0o" ) ) { return parseInt ( s , 8 ) ; } if ( s . startsWith ( "0x" ) || s . startsWith ( "0X" ) ) { return parseInt ( s , 16 ) ; } if ( s . startsWith ( "0b" ) || s . startsWith ( "0B" ) ) { return parseInt ( s , 2 ) ; } return parseInt ( s ) ; } ; export const parseMode = ( s ) : number | string => { if ( typeof s === "number" ) { if ( s < 0 ) { throw new Error ( `The argument 'mode' must be a 32-bit unsigned integer or an octal string. Received ${ s }` ) ; } return s ; } if ( s . startsWith ( "0o" ) ) { return s . substring ( 2 ) ; } return s ; } ; param export const getProtocol = ( url ) : string => { return ; } ; param p param settings export const generateSlugPath = ( p , slug ) : string => { if ( slug === "true" || slug === true || slug == 1 ) { const paths = p . split ( "/" ) . map ( d => slugify ( d ) ) ; return path . join ( ... paths ) ; } return p ; } ; param url param doSlug export const generateSlug = ( url , slug ) : string => { if ( ! url || url === "null" || url === "undefined" ) { return "" ; } if ( url . indexOf ( "/" ) !== - 1 ) { return generateSlugPath ( url , slug ) ; } if ( slug === "true" || slug === true || slug == 1 ) { const s = slugify ( url ) ; return s ; } return url ; } ; param name export const validateName = ( name ) : string => { if ( name === null ) { return "Can not use `null` as bucket name" ; } if ( name === "null" ) { return 'Can not use "null" as bucket name' ; } if ( name === "undefined" ) { return 'Can not use "undefined" as bucket name' ; } if ( name === "" || typeof name === "undefined" ) { return "Please provide a bucket name" ; } return null ; } ;