Skip to content

Commit

Permalink
chore: relax requirements for web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Dec 8, 2022
1 parent e9dc57b commit a54f807
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/web-worker/src/pure.ts
Expand Up @@ -58,12 +58,13 @@ function createClonedMessageEvent(data: any, transferOrOptions: StructuredSerial
const transfer = Array.isArray(transferOrOptions) ? transferOrOptions : transferOrOptions?.transfer

debug('clone worker message %o', data)
const origin = typeof location === 'undefined' ? undefined : location.origin

if (typeof structuredClone === 'function' && clone === 'native') {
debug('create message event, using native structured clone')
return new MessageEvent('message', {
data: structuredClone(data, { transfer }),
origin: window.location.origin,
origin,
})
}
if (clone !== 'none') {
Expand All @@ -76,13 +77,13 @@ function createClonedMessageEvent(data: any, transferOrOptions: StructuredSerial
)
return new MessageEvent('message', {
data: ponyfillStructuredClone(data, { lossy: true }),
origin: window.location.origin,
origin,
})
}
debug('create message event without cloning an object')
return new MessageEvent('message', {
data,
origin: window.location.origin,
origin,
})
}

Expand All @@ -102,10 +103,8 @@ export function defineWebWorker(options?: DefineWorkerOptions) {
if (typeof Worker !== 'undefined' && '__VITEST_WEB_WORKER__' in globalThis.Worker)
return

assertGlobalExists('window')
assertGlobalExists('EventTarget')
assertGlobalExists('MessageEvent')
assertGlobalExists('ErrorEvent')

const { config, rpc, mockMap, moduleCache } = getWorkerState()

Expand Down Expand Up @@ -204,7 +203,8 @@ export function defineWebWorker(options?: DefineWorkerOptions) {
debug('worker %s successfully initialized', this._vw_name)
}).catch((e) => {
debug('worker %s failed to initialize: %o', this._vw_name, e)
const error = new ErrorEvent('error', {
const EventConstructor = globalThis.ErrorEvent || globalThis.Event
const error = new EventConstructor('error', {
error: e,
message: e.message,
})
Expand Down

0 comments on commit a54f807

Please sign in to comment.