1import { Platform } from 'expo-modules-core'; 2import { PixelRatio } from 'react-native'; 3// Returns the Metro dev server-specific asset location. 4function getScaledAssetPath(asset) { 5 const scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get()); 6 const scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; 7 const type = !asset.type ? '' : `.${asset.type}`; 8 return asset.httpServerLocation + '/' + asset.name + scaleSuffix + type; 9} 10export default class AssetSourceResolver { 11 serverUrl; 12 // where the jsbundle is being run from 13 // NOTE(EvanBacon): Never defined on web. 14 jsbundleUrl; 15 // the asset to resolve 16 asset; 17 constructor(serverUrl, jsbundleUrl, asset) { 18 if (!serverUrl) { 19 throw new Error('Web assets require a server URL'); 20 } 21 this.serverUrl = serverUrl; 22 this.jsbundleUrl = null; 23 this.asset = asset; 24 } 25 // Always true for web runtimes 26 isLoadedFromServer() { 27 return true; 28 } 29 // Always false for web runtimes 30 isLoadedFromFileSystem() { 31 return false; 32 } 33 defaultAsset() { 34 return this.assetServerURL(); 35 } 36 /** 37 * @returns absolute remote URL for the hosted asset. 38 */ 39 assetServerURL() { 40 const fromUrl = new URL(getScaledAssetPath(this.asset), this.serverUrl); 41 fromUrl.searchParams.set('platform', Platform.OS); 42 fromUrl.searchParams.set('hash', this.asset.hash); 43 return this.fromSource(fromUrl.toString()); 44 } 45 fromSource(source) { 46 return { 47 __packager_asset: true, 48 width: this.asset.width ?? undefined, 49 height: this.asset.height ?? undefined, 50 uri: source, 51 scale: AssetSourceResolver.pickScale(this.asset.scales, PixelRatio.get()), 52 }; 53 } 54 static pickScale(scales, deviceScale) { 55 for (let i = 0; i < scales.length; i++) { 56 if (scales[i] >= deviceScale) { 57 return scales[i]; 58 } 59 } 60 return scales[scales.length - 1] || 1; 61 } 62} 63//# sourceMappingURL=AssetSourceResolver.web.js.map