Skip to content

fix: with 沙箱没有 Worker Proxy,且 Worker Proxy 没有处理 classic 模式 #1618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/proxies/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ const WorkerProxy = new Proxy<Worker>(originalWorker, {
}

if (url && !isSameOrigin(url)) {
let workerPath = scriptURL
// 如果 scriptURL 是跨域的,使用 Blob URL 加载并执行 worker
const script = `import "${scriptURL}";`
const workerPath = urlFromScript(script)
options.type = 'module'
// 应该遵总 worker 的 type 类型。classic worker
if (options.type === 'module') {
const script = `import "${scriptURL}";`
workerPath = urlFromScript(script)
} else {
const script = `importScripts("${scriptURL}");`
workerPath = urlFromScript(script)
}
return new Target(workerPath, options) as WorkerInstance
} else {
// 如果 scriptURL 是同源的,直接使用原生的 Worker 构造函数
Expand Down
10 changes: 10 additions & 0 deletions src/sandbox/with/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import {
appInstanceMap,
} from '../../create_app'
import WorkerProxy from '../../proxies/worker'

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

Object.defineProperty(microAppWindow, 'Worker', {
value: WorkerProxy,
configurable: true,
writable: true,
})

const proxyWindow = new Proxy(microAppWindow, {
get: (target: microAppWindowType, key: PropertyKey): unknown => {
throttleDeferForSetAppName(appName)
if (key === 'Worker') {
return WorkerProxy
}
if (
Reflect.has(target, key) ||
(isString(key) && /^__MICRO_APP_/.test(key)) ||
Expand Down