Skip to content

Commit bdaedaa

Browse files
author
huangchenbj
committed
fix: with 沙箱没有 Worker Proxy,且 Worker Proxy 没有处理 classic 模式
1 parent aa0a559 commit bdaedaa

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/proxies/worker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ const WorkerProxy = new Proxy<Worker>(originalWorker, {
7878
}
7979

8080
if (url && !isSameOrigin(url)) {
81+
let workerPath = scriptURL
8182
// 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
82-
const script = `import "${scriptURL}";`
83-
const workerPath = urlFromScript(script)
84-
options.type = 'module'
83+
// 应该遵总 worker 的 type 类型。classic worker
84+
if (options.type === 'module') {
85+
const script = `import "${scriptURL}";`
86+
workerPath = urlFromScript(script)
87+
} else {
88+
const script = `importScripts("${scriptURL}");`
89+
workerPath = urlFromScript(script)
90+
}
8591
return new Target(workerPath, options) as WorkerInstance
8692
} else {
8793
// 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数

src/sandbox/with/window.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import {
2727
appInstanceMap,
2828
} from '../../create_app'
29+
import WorkerProxy from '../../proxies/worker'
2930

3031
/**
3132
* patch window of child app
@@ -87,9 +88,18 @@ function createProxyWindow (
8788
const rawWindow = globalEnv.rawWindow
8889
const descriptorTargetMap = new Map<PropertyKey, 'target' | 'rawWindow'>()
8990

91+
Object.defineProperty(microAppWindow, 'Worker', {
92+
value: WorkerProxy,
93+
configurable: true,
94+
writable: true,
95+
})
96+
9097
const proxyWindow = new Proxy(microAppWindow, {
9198
get: (target: microAppWindowType, key: PropertyKey): unknown => {
9299
throttleDeferForSetAppName(appName)
100+
if (key === 'Worker') {
101+
return WorkerProxy
102+
}
93103
if (
94104
Reflect.has(target, key) ||
95105
(isString(key) && /^__MICRO_APP_/.test(key)) ||

0 commit comments

Comments
 (0)