gradio

Форк
0
/
cross-origin-worker.ts 
50 строк · 2.3 Кб
1
// A hack to load a worker script from a different origin.
2
// Vite's built-in Web Workers feature does not support inlining the worker code
3
// into the main bundle and always emits it to a separate file,
4
// which is not compatible with the cross-origin worker.
5
// So we use this hack to load the separate worker code from a domain different from the parent page.
6
// Vite deals with the special syntax `new Worker(new URL("worker.ts", import.meta.url))` for the worker build,
7
// so this `CrossOriginWorkerMaker` class must be defined in a separate file and
8
// be imported as the `Worker` alias into the file where the syntax is used to load the worker.
9
// This implementation was based on https://github.com/whitphx/stlite/blob/v0.34.0/packages/kernel/src/kernel.ts,
10
// and this technique was introduced originally for Webpack at https://github.com/webpack/webpack/discussions/14648#discussioncomment-1589272
11

12
// Caching is important not only for performance but also for ensuring that the same blob URL is used for the same requested URL.
13
const worker_blob_url_cache = new Map<string, string>();
14
function get_blob_url(url: URL): string {
15
	const cached_worker_blob_url = worker_blob_url_cache.get(url.toString());
16
	if (cached_worker_blob_url) {
17
		console.debug(`Reusing the cached worker blob URL for ${url.toString()}.`);
18
		return cached_worker_blob_url;
19
	}
20

21
	const worker_blob = new Blob([`importScripts("${url.toString()}");`], {
22
		type: "text/javascript"
23
	});
24
	const worker_blob_url = URL.createObjectURL(worker_blob);
25
	worker_blob_url_cache.set(url.toString(), worker_blob_url);
26
	return worker_blob_url;
27
}
28

29
export class CrossOriginWorkerMaker {
30
	public readonly worker: Worker | SharedWorker;
31

32
	constructor(url: URL, options?: WorkerOptions & { shared?: boolean }) {
33
		const { shared = false, ...workerOptions } = options ?? {};
34

35
		try {
36
			// This is the normal way to load a worker script, which is the best straightforward if possible.
37
			this.worker = shared
38
				? new SharedWorker(url, workerOptions)
39
				: new Worker(url, workerOptions);
40
		} catch (e) {
41
			console.debug(
42
				`Failed to load a worker script from ${url.toString()}. Trying to load a cross-origin worker...`
43
			);
44
			const worker_blob_url = get_blob_url(url);
45
			this.worker = shared
46
				? new SharedWorker(worker_blob_url, workerOptions)
47
				: new Worker(worker_blob_url, workerOptions);
48
		}
49
	}
50
}
51

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.