onnxruntime/js/web/lib/wasm/wasm-utils-import.ts
Ashrit Shetty df873177eb
Update win-ort-main to tip main 250116 (#23398)
### Description
This PR is to update the win-ort-main branch to the tip main
branch as of 2025-01-16.

### Motivation and Context
This update includes the OpenVino fix for debug builds.

---------

Signed-off-by: Liqun Fu <liqfu@microsoft.com>
Signed-off-by: Liqun Fu <liqun.fu@microsoft.com>
Signed-off-by: Junze Wu <junze.wu@intel.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Jianhui Dai <jianhui.j.dai@intel.com>
Co-authored-by: Yueqing Zhang <yuz75@Pitt.edu>
Co-authored-by: amancini-N <63410090+amancini-N@users.noreply.github.com>
Co-authored-by: Adrian Lizarraga <adlizarraga@microsoft.com>
Co-authored-by: liqun Fu <liqfu@microsoft.com>
Co-authored-by: Guenther Schmuelling <guschmue@microsoft.com>
Co-authored-by: Yifan Li <109183385+yf711@users.noreply.github.com>
Co-authored-by: yf711 <yifanl@microsoft.com>
Co-authored-by: Wanming Lin <wanming.lin@intel.com>
Co-authored-by: wejoncy <wejoncy@163.com>
Co-authored-by: wejoncy <wejoncy@.com>
Co-authored-by: Scott McKay <skottmckay@gmail.com>
Co-authored-by: Changming Sun <chasun@microsoft.com>
Co-authored-by: Jean-Michaël Celerier <jeanmichael.celerier+github@gmail.com>
Co-authored-by: Dmitry Deshevoy <mityada@gmail.com>
Co-authored-by: xhcao <xinghua.cao@intel.com>
Co-authored-by: Yueqing Zhang <yueqingz@amd.com>
Co-authored-by: Yulong Wang <7679871+fs-eire@users.noreply.github.com>
Co-authored-by: Jiajia Qin <jiajiaqin@microsoft.com>
Co-authored-by: Wu, Junze <junze.wu@intel.com>
Co-authored-by: Jian Chen <cjian@microsoft.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Matthieu Darbois <mayeut@users.noreply.github.com>
Co-authored-by: Prathik Rao <prathik.rao@gmail.com>
Co-authored-by: wonchung-microsoft <wonchung@microsoft.com>
Co-authored-by: Vincent Wang <wangwchpku@outlook.com>
Co-authored-by: PARK DongHa <luncliff@gmail.com>
Co-authored-by: Hector Li <hecli@microsoft.com>
Co-authored-by: Sam Webster <13457618+samwebster@users.noreply.github.com>
Co-authored-by: Adrian Lizarraga <adrianlm2@gmail.com>
Co-authored-by: Preetha Veeramalai <preetha.veeramalai@intel.com>
Co-authored-by: jatinwadhwa921 <jatin.wadhwa@intel.com>
Co-authored-by: Satya Kumar Jandhyala <satya.k.jandhyala@gmail.com>
Co-authored-by: Corentin Maravat <101636442+cocotdf@users.noreply.github.com>
Co-authored-by: Xiaoyu <85524621+xiaoyu-work@users.noreply.github.com>
Co-authored-by: Tianlei Wu <tlwu@microsoft.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jie Chen <jie.a.chen@intel.com>
Co-authored-by: Jianhui Dai <jianhui.j.dai@intel.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
Co-authored-by: Baiju Meswani <bmeswani@microsoft.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Ted Themistokleous <107195283+TedThemistokleous@users.noreply.github.com>
Co-authored-by: Jeff Daily <jeff.daily@amd.com>
Co-authored-by: Artur Wojcik <artur.wojcik@outlook.com>
Co-authored-by: Ted Themistokleous <tedthemistokleous@amd.com>
Co-authored-by: Xinya Zhang <Xinya.Zhang@amd.com>
Co-authored-by: ikalinic <ilija.kalinic@amd.com>
Co-authored-by: sstamenk <sstamenk@amd.com>
Co-authored-by: Yi-Hong Lyu <yilyu@microsoft.com>
Co-authored-by: Ti-Tai Wang <titaiwang@microsoft.com>
2025-01-16 15:20:25 -08:00

215 lines
8.2 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import type { OrtWasmModule } from './wasm-types';
import { isNode } from './wasm-utils-env';
/**
* The origin of the current location.
*
* In Node.js, this is undefined.
*/
const origin = isNode || typeof location === 'undefined' ? undefined : location.origin;
const getScriptSrc = (): string | undefined => {
// if Nodejs, return undefined
if (isNode) {
return undefined;
}
// if It's ESM, use import.meta.url
if (BUILD_DEFS.IS_ESM) {
// For ESM, if the import.meta.url is a file URL, this usually means the bundler rewrites `import.meta.url` to
// the file path at compile time. In this case, this file path cannot be used to determine the runtime URL.
//
// We need to use the URL constructor like this:
// ```js
// new URL('actual-bundle-name.js', import.meta.url).href
// ```
// So that bundler can preprocess the URL correctly.
if (BUILD_DEFS.ESM_IMPORT_META_URL?.startsWith('file:')) {
// if the rewritten URL is a relative path, we need to use the origin to resolve the URL.
return new URL(new URL(BUILD_DEFS.BUNDLE_FILENAME, BUILD_DEFS.ESM_IMPORT_META_URL).href, origin).href;
}
return BUILD_DEFS.ESM_IMPORT_META_URL;
}
return typeof document !== 'undefined'
? (document.currentScript as HTMLScriptElement)?.src
: // use `self.location.href` if available
typeof self !== 'undefined'
? self.location?.href
: undefined;
};
/**
* The classic script source URL. This is not always available in non ESModule environments.
*
* In Node.js, this is undefined.
*/
export const scriptSrc = getScriptSrc();
/**
* Infer the wasm path prefix from the script source URL.
*
* @returns The inferred wasm path prefix, or undefined if the script source URL is not available or is a blob URL.
*/
export const inferWasmPathPrefixFromScriptSrc = (): string | undefined => {
if (scriptSrc && !scriptSrc.startsWith('blob:')) {
return scriptSrc.substring(0, scriptSrc.lastIndexOf('/') + 1);
}
return undefined;
};
/**
* Check if the given filename with prefix is from the same origin.
*/
const isSameOrigin = (filename: string, prefixOverride?: string) => {
try {
const baseUrl = prefixOverride ?? scriptSrc;
const url = baseUrl ? new URL(filename, baseUrl) : new URL(filename);
return url.origin === origin;
} catch {
return false;
}
};
/**
* Normalize the inputs to an absolute URL with the given prefix override. If failed, return undefined.
*/
const normalizeUrl = (filename: string, prefixOverride?: string) => {
const baseUrl = prefixOverride ?? scriptSrc;
try {
const url = baseUrl ? new URL(filename, baseUrl) : new URL(filename);
return url.href;
} catch {
return undefined;
}
};
/**
* Create a fallback URL if an absolute URL cannot be created by the normalizeUrl function.
*/
const fallbackUrl = (filename: string, prefixOverride?: string) => `${prefixOverride ?? './'}${filename}`;
/**
* This helper function is used to preload a module from a URL.
*
* If the origin of the worker URL is different from the current origin, the worker cannot be loaded directly.
* See discussions in https://github.com/webpack-contrib/worker-loader/issues/154
*
* In this case, we will fetch the worker URL and create a new Blob URL with the same origin as a workaround.
*
* @param absoluteUrl - The absolute URL to preload.
*
* @returns - A promise that resolves to a new Blob URL
*/
const preload = async (absoluteUrl: string): Promise<string> => {
const response = await fetch(absoluteUrl, { credentials: 'same-origin' });
const blob = await response.blob();
return URL.createObjectURL(blob);
};
/**
* This helper function is used to dynamically import a module from a URL.
*
* The build script has special handling for this function to ensure that the URL is not bundled into the final output.
*
* @param url - The URL to import.
*
* @returns - A promise that resolves to the default export of the module.
*/
const dynamicImportDefault = async <T>(url: string): Promise<T> =>
(await import(/* webpackIgnore: true */ url)).default;
/**
* The proxy worker factory imported from the proxy worker module.
*
* This is only available when the WebAssembly proxy is not disabled.
*/
const createProxyWorker: ((urlOverride?: string) => Worker) | undefined =
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
BUILD_DEFS.DISABLE_WASM_PROXY ? undefined : require('./proxy-worker/main').default;
/**
* Import the proxy worker.
*
* This function will perform the following steps:
* 1. If a preload is needed, it will preload the module and return the object URL.
* 2. Use the proxy worker factory to create the proxy worker.
*
* @returns - A promise that resolves to a tuple of 2 elements:
* - The object URL of the preloaded module, or undefined if no preload is needed.
* - The proxy worker.
*/
export const importProxyWorker = async (): Promise<[undefined | string, Worker]> => {
if (!scriptSrc) {
throw new Error('Failed to load proxy worker: cannot determine the script source URL.');
}
// If the script source is from the same origin, we can use the embedded proxy module directly.
if (isSameOrigin(scriptSrc)) {
return [undefined, createProxyWorker!()];
}
// Otherwise, need to preload
const url = await preload(scriptSrc);
return [url, createProxyWorker!(url)];
};
/**
* The embedded WebAssembly module.
*
* This is only available in ESM and when embedding is not disabled.
*/
const embeddedWasmModule: EmscriptenModuleFactory<OrtWasmModule> | undefined =
BUILD_DEFS.IS_ESM && BUILD_DEFS.ENABLE_BUNDLE_WASM_JS
? // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
require(
!BUILD_DEFS.DISABLE_JSEP
? '../../dist/ort-wasm-simd-threaded.jsep.mjs'
: '../../dist/ort-wasm-simd-threaded.mjs',
).default
: undefined;
/**
* Import the WebAssembly module.
*
* This function will perform the following steps:
* 1. If the embedded module exists and no custom URL is specified, use the embedded module.
* 2. If a preload is needed, it will preload the module and return the object URL.
* 3. Otherwise, it will perform a dynamic import of the module.
*
* @returns - A promise that resolves to a tuple of 2 elements:
* - The object URL of the preloaded module, or undefined if no preload is needed.
* - The default export of the module, which is a factory function to create the WebAssembly module.
*/
export const importWasmModule = async (
urlOverride: string | undefined,
prefixOverride: string | undefined,
isMultiThreaded: boolean,
): Promise<[undefined | string, EmscriptenModuleFactory<OrtWasmModule>]> => {
if (!urlOverride && !prefixOverride && embeddedWasmModule && scriptSrc && isSameOrigin(scriptSrc)) {
return [undefined, embeddedWasmModule];
} else {
const wasmModuleFilename = !BUILD_DEFS.DISABLE_JSEP
? 'ort-wasm-simd-threaded.jsep.mjs'
: 'ort-wasm-simd-threaded.mjs';
const wasmModuleUrl = urlOverride ?? normalizeUrl(wasmModuleFilename, prefixOverride);
// need to preload if all of the following conditions are met:
// 1. not in Node.js.
// - Node.js does not have the same origin policy for creating workers.
// 2. multi-threaded is enabled.
// - If multi-threaded is disabled, no worker will be created. So we don't need to preload the module.
// 3. the absolute URL is available.
// - If the absolute URL is failed to be created, the origin cannot be determined. In this case, we will not
// preload the module.
// 4. the worker URL is not from the same origin.
// - If the worker URL is from the same origin, we can create the worker directly.
const needPreload = !isNode && isMultiThreaded && wasmModuleUrl && !isSameOrigin(wasmModuleUrl, prefixOverride);
const url = needPreload
? await preload(wasmModuleUrl)
: (wasmModuleUrl ?? fallbackUrl(wasmModuleFilename, prefixOverride));
return [needPreload ? url : undefined, await dynamicImportDefault<EmscriptenModuleFactory<OrtWasmModule>>(url)];
}
};