Repository: dotnet/runtime
File: src/native/libs/Common/JavaScript/loader/assets.ts (TypeScript)

Code before:
        bytes = await fetchBytes(assetInternal);
    } finally {
        onDownloadedAsset(assetInternal);
    }
    if (bytes) {
        await nativeModulePromiseController.promise;
        dotnetBrowserHostExports.loadIcuData(bytes);
    }
}

export async function fetchAssembly(asset: AssemblyAsset): Promise<void> {
    const assetInternal = asset as AssetEntryInternal;
    totalAssetsToDownload++;
    dotnetAssert.check(assetInternal.virtualPath, "Assembly asset must have virtualPath");
    const assetNameForUrl = assetInternal.culture
        ? `${assetInternal.culture}/${assetInternal.name}`
        : assetInternal.name;
    if (assetNameForUrl && !asset.resolvedUrl) {
        asset.resolvedUrl = locateFile(assetNameForUrl);
    }
    assetInternal.virtualPath = assetInternal.virtualPath.startsWith("/")
        ? assetInternal.virtualPath
        : assetInternal.culture
            ? `${browserVirtualAppBase}${assetInternal.culture}/${assetInternal.virtualPath}`
            : browserVirtualAppBase + assetInternal.virtualPath;
    assetInternal.shortName = assetInternal.virtualPath.startsWith(browserVirtualAppBase)
        ? assetInternal.virtualPath.substring(browserVirtualAppBase.length)
        : assetInternal.virtualPath.substring(assetInternal.virtualPath.lastIndexOf("/") + 1);

    if (assetInternal.virtualPath.endsWith(".wasm")) {
        await fetchWebcil(assetInternal);
    } else {
        await fetchDll(assetInternal);
    }
}

async function fetchWebcil(assetInternal: AssetEntryInternal): Promise<void> {
    try {
        assetInternal.behavior = "webcil";
        const virtualPath = assetInternal.virtualPath = assetInternal.virtualPath!.replace(/\.wasm$/, ".dll");
        assetInternal.shortName = virtualPath.startsWith(browserVirtualAppBase)
            ? virtualPath.substring(browserVirtualAppBase.length)
            : virtualPath.substring(virtualPath.lastIndexOf("/") + 1);

        const webcilPromise = loadResource(assetInternal);

        const memory = await wasmMemoryPromiseController.promise;
        const instancePromise = dotnetBrowserHostExports.instantiateWebcilModule(webcilPromise, memory, virtualPath);
        await instancePromise;
    } finally {
        onDownloadedAsset(assetInternal);

Review comment (suggestion):
We're doing this block of code 3 times in fetchAssembly, fetchWebcil and fetchPdb.

```
function computeShortName(virtualPath: string): string {
    return virtualPath.startsWith(browserVirtualAppBase)
        ? virtualPath.substring(browserVirtualAppBase.length)
        : virtualPath.substring(virtualPath.lastIndexOf("/") + 1);
}
```

Code after:
        bytes = await fetchBytes(assetInternal);
    } finally {
        onDownloadedAsset(assetInternal);
    }
    if (bytes) {
        await nativeModulePromiseController.promise;
        dotnetBrowserHostExports.loadIcuData(bytes);
    }
}

export async function fetchAssembly(asset: AssemblyAsset): Promise<void> {
    const assetInternal = asset as AssetEntryInternal;
    totalAssetsToDownload++;
    dotnetAssert.check(assetInternal.virtualPath, "Assembly asset must have virtualPath");
    const assetNameForUrl = assetInternal.culture
        ? `${assetInternal.culture}/${assetInternal.name}`
        : assetInternal.name;
    if (assetNameForUrl && !asset.resolvedUrl) {
        asset.resolvedUrl = locateFile(assetNameForUrl);
    }

    normalizeVirtualPath(assetInternal);

    if (assetInternal.virtualPath.endsWith(".wasm")) {
        await fetchWebcil(assetInternal);
    } else {
        await fetchDll(assetInternal);
    }
}

async function fetchWebcil(assetInternal: AssetEntryInternal): Promise<void> {
    try {
        assetInternal.behavior = "webcil";

        normalizeVirtualPath(assetInternal);

        const webcilPromise = loadResource(assetInternal);

        const memory = await wasmMemoryPromiseController.promise;
        const instancePromise = dotnetBrowserHostExports.instantiateWebcilModule(webcilPromise, memory, assetInternal.virtualPath!);
        await instancePromise;
    } finally {
        onDownloadedAsset(assetInternal);
    }
}

async function fetchDll(assetInternal: AssetEntryInternal): Promise<void> {
    const virtualPath = assetInternal.virtualPath!;
    assetInternal.behavior = "assembly";
    let bytes;
    try {