import { StorageListOptions , StorageListResult , StoredKey , base64Decode , base64Encode , nonCircularClone , } from "@miniflare/shared" ; export function cloneMetadata < Meta > ( metadata ? ) { return ( metadata && nonCircularClone ( metadata ) ) as Meta | undefined ; } const collator = new Intl . Collator ( ) ; export function listFilterMatch ( options , name ) { return ! ( ( options ?. prefix && ! name . startsWith ( options . prefix ) ) || ( options ?. start && collator . compare ( name , options . start ) < 0 ) || ( options ?. end && collator . compare ( name , options . end ) >= 0 ) ) ; } export function listPaginate < Key extends StoredKey > ( options , keys ) { const direction = options ?. reverse ? - 1 : 1 ; keys . sort ( ( a , b ) => direction * collator . compare ( a . name , b . name ) ) ; const startAfter = options ?. cursor ? base64Decode ( options . cursor ) : "" ; let startIndex = 0 ; if ( startAfter !== "" ) { startIndex = keys . findIndex ( ( { name } ) => name === startAfter ) ; if ( startIndex === - 1 ) startIndex = keys . length ; startIndex ++ ; } const endIndex = options ?. limit === undefined ? keys . length : startIndex + options . limit ; const nextCursor = endIndex < keys . length ? base64Encode ( keys [ endIndex - 1 ] . name ) : "" ; keys = keys . slice ( startIndex , endIndex ) ; return { keys , cursor : nextCursor } ; }