@@ -2,20 +2,10 @@ import {
22 MessageChannel ,
33 type MessagePort as NodeMessagePort ,
44} from 'node:worker_threads'
5- import type { InlineWorkerContext , Procedure } from './types'
5+ import type { Procedure } from './types'
66import { InlineWorkerRunner } from './runner'
77import { debug , getFileIdFromUrl , getRunnerOptions } from './utils'
88
9- interface SharedInlineWorkerContext
10- extends Omit <
11- InlineWorkerContext ,
12- 'onmessage' | 'postMessage' | 'self' | 'global'
13- > {
14- onconnect : Procedure | null
15- self : SharedInlineWorkerContext
16- global : SharedInlineWorkerContext
17- }
18-
199function convertNodePortToWebPort ( port : NodeMessagePort ) : MessagePort {
2010 if ( ! ( 'addEventListener' in port ) ) {
2111 Object . defineProperty ( port , 'addEventListener' , {
@@ -79,33 +69,55 @@ export function createSharedWorkerConstructor(): typeof SharedWorker {
7969 super ( )
8070
8171 const name = typeof options === 'string' ? options : options ?. name
82-
83- // should be equal to SharedWorkerGlobalScope
84- const context : SharedInlineWorkerContext = {
85- onconnect : null ,
86- name,
72+ let selfProxy : typeof globalThis
73+
74+ const context = {
75+ onmessage : null ,
76+ onmessageerror : null ,
77+ onerror : null ,
78+ onlanguagechange : null ,
79+ onoffline : null ,
80+ ononline : null ,
81+ onrejectionhandled : null ,
82+ onrtctransform : null ,
83+ onunhandledrejection : null ,
84+ origin : typeof location !== 'undefined' ? location . origin : 'http://localhost:3000' ,
85+ importScripts : ( ) => {
86+ throw new Error (
87+ '[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.' ,
88+ )
89+ } ,
90+ crossOriginIsolated : false ,
91+ onconnect : null as ( ( e : MessageEvent ) => void ) | null ,
92+ name : name || '' ,
8793 close : ( ) => this . port . close ( ) ,
8894 dispatchEvent : ( event : Event ) => {
8995 return this . _vw_workerTarget . dispatchEvent ( event )
9096 } ,
91- addEventListener : ( ...args ) => {
92- return this . _vw_workerTarget . addEventListener ( ...args )
97+ addEventListener : ( ...args : any [ ] ) => {
98+ return this . _vw_workerTarget . addEventListener ( ...args as [ any , any ] )
9399 } ,
94100 removeEventListener : this . _vw_workerTarget . removeEventListener ,
95101 get self ( ) {
96- return context
97- } ,
98- get global ( ) {
99- return context
102+ return selfProxy
100103 } ,
101104 }
102105
106+ selfProxy = new Proxy ( context , {
107+ get ( target , prop , receiver ) {
108+ if ( Reflect . has ( target , prop ) ) {
109+ return Reflect . get ( target , prop , receiver )
110+ }
111+ return Reflect . get ( globalThis , prop , receiver )
112+ } ,
113+ } ) as any
114+
103115 const channel = new MessageChannel ( )
104116 this . port = convertNodePortToWebPort ( channel . port1 )
105117 this . _vw_workerPort = convertNodePortToWebPort ( channel . port2 )
106118
107119 this . _vw_workerTarget . addEventListener ( 'connect' , ( e ) => {
108- context . onconnect ?.( e )
120+ context . onconnect ?.( e as MessageEvent )
109121 } )
110122
111123 const runner = new InlineWorkerRunner ( runnerOptions , context )
0 commit comments