[browser] Internalize assetUniqueQuery as modulesUniqueQuery (#88329)
authorPavel Savara <pavel.savara@gmail.com>
Mon, 10 Jul 2023 09:57:33 +0000 (11:57 +0200)
committerGitHub <noreply@github.com>
Mon, 10 Jul 2023 09:57:33 +0000 (11:57 +0200)
src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/SecondRuntimeTest.js
src/mono/wasm/runtime/dotnet.d.ts
src/mono/wasm/runtime/loader/assets.ts
src/mono/wasm/runtime/loader/blazor/_Integration.ts
src/mono/wasm/runtime/loader/config.ts
src/mono/wasm/runtime/loader/polyfills.ts
src/mono/wasm/runtime/snapshot.ts
src/mono/wasm/runtime/types/index.ts
src/mono/wasm/runtime/types/internal.ts
src/mono/wasm/test-main.js

index 4230d51..7302b1c 100644 (file)
@@ -2,9 +2,6 @@ export async function runSecondRuntimeAndTestStaticState() {
     const { dotnet: dotnet2 } = await import('./_framework/dotnet.js?instance=2');
     const runtime2 = await dotnet2
         .withStartupMemoryCache(false)
-        .withConfig({
-            assetUniqueQuery: "?instance=2",
-        })
         .create();
 
     const increment1 = await getIncrementStateFunction(App.runtime);
index b715912..dcc03f9 100644 (file)
@@ -149,10 +149,6 @@ type MonoConfig = {
      * application environment
      */
     applicationEnvironment?: string;
-    /**
-     * query string to be used for asset loading
-     */
-    assetUniqueQuery?: string;
 };
 interface ResourceRequest {
     name: string;
index da53c32..0f6ab16 100644 (file)
@@ -12,14 +12,20 @@ let throttlingPromise: PromiseAndController<void> | undefined;
 // in order to prevent net::ERR_INSUFFICIENT_RESOURCES if we start downloading too many files at same time
 let parallel_count = 0;
 
-// don't `fetch` javaScript files
-const skipDownloadsByAssetTypes: {
+const jsModulesAssetTypes: {
     [k: string]: boolean
 } = {
     "js-module-threads": true,
     "js-module-runtime": true,
     "js-module-native": true,
     "js-module-dotnet": true,
+};
+
+// don't `fetch` javaScript and wasm files
+const skipDownloadsByAssetTypes: {
+    [k: string]: boolean
+} = {
+    ...jsModulesAssetTypes,
     "dotnetwasm": true,
 };
 
@@ -39,10 +45,7 @@ const containedInSnapshotByAssetTypes: {
     "pdb": true,
     "heap": true,
     "icu": true,
-    "js-module-threads": true,
-    "js-module-runtime": true,
-    "js-module-native": true,
-    "js-module-dotnet": true,
+    ...jsModulesAssetTypes,
     "dotnetwasm": true,
 };
 
@@ -50,10 +53,7 @@ const containedInSnapshotByAssetTypes: {
 const skipInstantiateByAssetTypes: {
     [k: string]: boolean
 } = {
-    "js-module-threads": true,
-    "js-module-runtime": true,
-    "js-module-native": true,
-    "js-module-dotnet": true,
+    ...jsModulesAssetTypes,
     "dotnetwasm": true,
     "symbols": true,
 };
@@ -368,10 +368,7 @@ function resolve_path(asset: AssetEntry, sourcePrefix: string): string {
         } else {
             attemptUrl = sourcePrefix + asset.name;
         }
-        attemptUrl = loaderHelpers.locateFile(attemptUrl);
-        if (loaderHelpers.assetUniqueQuery) {
-            attemptUrl = attemptUrl + loaderHelpers.assetUniqueQuery;
-        }
+        attemptUrl = appendUniqueQuery(loaderHelpers.locateFile(attemptUrl), asset.behavior);
     }
     else {
         attemptUrl = asset.resolvedUrl;
@@ -380,6 +377,17 @@ function resolve_path(asset: AssetEntry, sourcePrefix: string): string {
     return attemptUrl;
 }
 
+export function appendUniqueQuery(attemptUrl: string, behavior: AssetBehaviours): string {
+    // apply unique query to js modules to make the module state independent of the other runtime instances
+    if (loaderHelpers.modulesUniqueQuery && jsModulesAssetTypes[behavior]) {
+        attemptUrl = attemptUrl + loaderHelpers.modulesUniqueQuery;
+    }
+
+    return attemptUrl;
+}
+
+
+
 function download_resource(request: ResourceRequest): LoadingResource {
     try {
         if (typeof loaderHelpers.downloadResource === "function") {
index 5bf57fe..c6ded22 100644 (file)
@@ -10,6 +10,7 @@ import { BootConfigResult } from "./BootConfig";
 import { WebAssemblyResourceLoader } from "./WebAssemblyResourceLoader";
 import { hasDebuggingEnabled } from "./_Polyfill";
 import { ICUDataMode } from "../../types/blazor";
+import { appendUniqueQuery } from "../assets";
 
 let resourceLoader: WebAssemblyResourceLoader;
 
@@ -77,14 +78,6 @@ export function setupModuleForBlazor(module: DotnetModuleInternal) {
     loaderHelpers.downloadResource = downloadResource; // polyfills were already assigned
 }
 
-function appendUniqueQuery(attemptUrl: string): string {
-    if (loaderHelpers.assetUniqueQuery) {
-        attemptUrl = attemptUrl + loaderHelpers.assetUniqueQuery;
-    }
-
-    return attemptUrl;
-}
-
 export function mapBootConfigToMonoConfig(moduleConfig: MonoConfigInternal, applicationEnvironment: string) {
     const resources = resourceLoader.bootConfig.resources;
 
@@ -133,13 +126,13 @@ export function mapBootConfigToMonoConfig(moduleConfig: MonoConfigInternal, appl
     for (const name in resources.runtimeAssets) {
         const asset = resources.runtimeAssets[name] as AssetEntry;
         asset.name = name;
-        asset.resolvedUrl = appendUniqueQuery(loaderHelpers.locateFile(name));
+        asset.resolvedUrl = appendUniqueQuery(loaderHelpers.locateFile(name), asset.behavior);
         assets.push(asset);
     }
     for (const name in resources.assembly) {
         const asset: AssetEntry = {
             name,
-            resolvedUrl: appendUniqueQuery(loaderHelpers.locateFile(name)),
+            resolvedUrl: appendUniqueQuery(loaderHelpers.locateFile(name), "assembly"),
             hash: resources.assembly[name],
             behavior: "assembly",
         };
@@ -149,7 +142,7 @@ export function mapBootConfigToMonoConfig(moduleConfig: MonoConfigInternal, appl
         for (const name in resources.pdb) {
             const asset: AssetEntry = {
                 name,
-                resolvedUrl: appendUniqueQuery(loaderHelpers.locateFile(name)),
+                resolvedUrl: appendUniqueQuery(loaderHelpers.locateFile(name), "pdb"),
                 hash: resources.pdb[name],
                 behavior: "pdb",
             };
@@ -177,7 +170,7 @@ export function mapBootConfigToMonoConfig(moduleConfig: MonoConfigInternal, appl
             continue;
         }
 
-        const resolvedUrl = appendUniqueQuery(loaderHelpers.locateFile(name));
+        const resolvedUrl = appendUniqueQuery(loaderHelpers.locateFile(name), behavior);
         const asset: AssetEntry = {
             name,
             resolvedUrl,
@@ -206,7 +199,7 @@ export function mapBootConfigToMonoConfig(moduleConfig: MonoConfigInternal, appl
         if (config === "appsettings.json" || config === `appsettings.${applicationEnvironment}.json`) {
             assets.push({
                 name: config,
-                resolvedUrl: appendUniqueQuery((document ? document.baseURI : "/") + config),
+                resolvedUrl: appendUniqueQuery((document ? document.baseURI : "/") + config, "vfs"),
                 behavior: "vfs",
             });
         }
@@ -216,7 +209,7 @@ export function mapBootConfigToMonoConfig(moduleConfig: MonoConfigInternal, appl
         for (const name in resources.vfs[virtualPath]) {
             const asset: AssetEntry = {
                 name,
-                resolvedUrl: appendUniqueQuery(loaderHelpers.locateFile(name)),
+                resolvedUrl: appendUniqueQuery(loaderHelpers.locateFile(name), "vfs"),
                 hash: resources.vfs[virtualPath][name],
                 behavior: "vfs",
                 virtualPath
index 4d997a1..e8ccaea 100644 (file)
@@ -53,7 +53,6 @@ export function normalizeConfig() {
         config.diagnosticTracing = true;
     }
     runtimeHelpers.diagnosticTracing = loaderHelpers.diagnosticTracing = !!config.diagnosticTracing;
-    loaderHelpers.assetUniqueQuery = config.assetUniqueQuery;
     runtimeHelpers.waitForDebugger = config.waitForDebugger;
     config.startupMemoryCache = !!config.startupMemoryCache;
     if (config.startupMemoryCache && runtimeHelpers.waitForDebugger) {
index ae59010..0b5e2be 100644 (file)
@@ -7,7 +7,12 @@ let node_url: any | undefined = undefined;
 
 export async function detect_features_and_polyfill(module: DotnetModuleInternal): Promise<void> {
 
-    loaderHelpers.scriptUrl = normalizeFileUrl(/* webpackIgnore: true */import.meta.url);
+    const scriptUrlQuery =/* webpackIgnore: true */import.meta.url;
+    const queryIndex = scriptUrlQuery.indexOf("?");
+    if (queryIndex > 0) {
+        loaderHelpers.modulesUniqueQuery = scriptUrlQuery.substring(queryIndex);
+    }
+    loaderHelpers.scriptUrl = normalizeFileUrl(scriptUrlQuery);
     loaderHelpers.scriptDirectory = normalizeDirectoryUrl(loaderHelpers.scriptUrl);
     loaderHelpers.locateFile = (path) => {
         if (isPathAbsolute(path)) return path;
index abe4a8d..6bb77f5 100644 (file)
@@ -176,7 +176,6 @@ async function getCacheKey(): Promise<string | null> {
     delete inputs.maxParallelDownloads;
     delete inputs.enableDownloadRetry;
     delete inputs.exitAfterSnapshot;
-    delete inputs.assetUniqueQuery;
 
     inputs.GitHash = GitHash;
     inputs.ProductVersion = ProductVersion;
index 1d0a721..2979576 100644 (file)
@@ -83,10 +83,6 @@ export type MonoConfig = {
      * application environment
      */
     applicationEnvironment?: string,
-    /**
-     * query string to be used for asset loading
-     */
-    assetUniqueQuery?: string,
 };
 
 export interface ResourceRequest {
index 735e7c4..37f77e9 100644 (file)
@@ -104,7 +104,7 @@ export type LoaderHelpers = {
     _loaded_files: { url: string, file: string }[];
     scriptDirectory: string
     scriptUrl: string
-    assetUniqueQuery?: string
+    modulesUniqueQuery?: string
     preferredIcuAsset: string | null,
     invariantMode: boolean,
 
index a6cd146..e26b35c 100644 (file)
@@ -308,7 +308,6 @@ async function dry_run(runArgs) {
             appendElementOnExit: false,
             logExitCode: false,
             pthreadPoolSize: 0,
-            assetUniqueQuery: "?dry_run=true",
             // this just means to not continue startup after the snapshot is taken. 
             // If there was previously a matching snapshot, it will be used.
             exitAfterSnapshot: true