import * as uuid from "uuid" ; export const ASSET_SERVING_PREFIX = "/assets/" ; example const recordedAssets = { } ; returns export function recordAsset ( filePath ) { const extensionIndex = filePath . lastIndexOf ( "." ) ; if ( extensionIndex === - 1 ) { throw new Error ( `Unsupported asset with no extension: ${ filePath }` ) ; } const extension = filePath . substr ( extensionIndex + 1 ) ; const generatedName = `${ uuid . v4 ( ) }.${ extension }` ; recordedAssets [ generatedName ] = filePath ; return `${ ASSET_SERVING_PREFIX }${ generatedName }` ; } param servedPath returns export function getAssetFilename ( servedPath ) { if ( ! servedPath . startsWith ( ASSET_SERVING_PREFIX ) ) { throw new Error ( `Invalid asset path: ${ servedPath } (wrong prefix)` ) ; } const generatedName = servedPath . substr ( ASSET_SERVING_PREFIX . length ) ; const filePath = recordedAssets [ generatedName ] ; if ( ! filePath ) { throw new Error ( `Invalid asset path: ${ servedPath } (never recorded)` ) ; } return filePath ; }