@vitest/web-worker 0.9.4 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/pure-263d3614.js +142 -0
- package/dist/pure.js +3 -142
- package/package.json +2 -2
package/dist/index.js
CHANGED
@@ -0,0 +1,142 @@
|
|
1
|
+
import { VitestRunner } from 'vitest/node';
|
2
|
+
import { toFilePath } from 'vite-node/utils';
|
3
|
+
|
4
|
+
function getWorkerState() {
|
5
|
+
return globalThis.__vitest_worker__;
|
6
|
+
}
|
7
|
+
class Bridge {
|
8
|
+
constructor() {
|
9
|
+
this.callbacks = {};
|
10
|
+
}
|
11
|
+
on(event, fn) {
|
12
|
+
var _a;
|
13
|
+
(_a = this.callbacks)[event] ?? (_a[event] = []);
|
14
|
+
this.callbacks[event].push(fn);
|
15
|
+
}
|
16
|
+
off(event, fn) {
|
17
|
+
if (this.callbacks[event])
|
18
|
+
this.callbacks[event] = this.callbacks[event].filter((f) => f !== fn);
|
19
|
+
}
|
20
|
+
removeEvents(event) {
|
21
|
+
this.callbacks[event] = [];
|
22
|
+
}
|
23
|
+
clear() {
|
24
|
+
this.callbacks = {};
|
25
|
+
}
|
26
|
+
emit(event, ...data) {
|
27
|
+
return (this.callbacks[event] || []).map((fn) => fn(...data));
|
28
|
+
}
|
29
|
+
}
|
30
|
+
class InlineWorkerRunner extends VitestRunner {
|
31
|
+
constructor(options, context) {
|
32
|
+
super(options);
|
33
|
+
this.context = context;
|
34
|
+
}
|
35
|
+
prepareContext(context) {
|
36
|
+
const ctx = super.prepareContext(context);
|
37
|
+
this.context.self.importScripts = () => {
|
38
|
+
};
|
39
|
+
return Object.assign(ctx, this.context, {
|
40
|
+
importScripts: () => {
|
41
|
+
}
|
42
|
+
});
|
43
|
+
}
|
44
|
+
}
|
45
|
+
function defineWebWorker() {
|
46
|
+
if ("Worker" in globalThis)
|
47
|
+
return;
|
48
|
+
const { config, rpc, mockMap, moduleCache } = getWorkerState();
|
49
|
+
const options = {
|
50
|
+
fetchModule(id) {
|
51
|
+
return rpc.fetch(id);
|
52
|
+
},
|
53
|
+
resolveId(id, importer) {
|
54
|
+
return rpc.resolveId(id, importer);
|
55
|
+
},
|
56
|
+
moduleCache,
|
57
|
+
mockMap,
|
58
|
+
interopDefault: config.deps.interopDefault ?? true,
|
59
|
+
root: config.root,
|
60
|
+
base: config.base
|
61
|
+
};
|
62
|
+
globalThis.Worker = class Worker {
|
63
|
+
constructor(url) {
|
64
|
+
this.inside = new Bridge();
|
65
|
+
this.outside = new Bridge();
|
66
|
+
this.messageQueue = [];
|
67
|
+
this.onmessage = null;
|
68
|
+
this.onmessageerror = null;
|
69
|
+
this.onerror = null;
|
70
|
+
const invalidates = [];
|
71
|
+
const context = {
|
72
|
+
onmessage: null,
|
73
|
+
dispatchEvent: (event) => {
|
74
|
+
this.inside.emit(event.type, event);
|
75
|
+
return true;
|
76
|
+
},
|
77
|
+
addEventListener: this.inside.on,
|
78
|
+
removeEventListener: this.inside.off,
|
79
|
+
postMessage: (data) => {
|
80
|
+
this.outside.emit("message", { data });
|
81
|
+
},
|
82
|
+
get self() {
|
83
|
+
return context;
|
84
|
+
},
|
85
|
+
get global() {
|
86
|
+
return context;
|
87
|
+
},
|
88
|
+
invalidates
|
89
|
+
};
|
90
|
+
this.inside.on("message", (e) => {
|
91
|
+
var _a;
|
92
|
+
(_a = context.onmessage) == null ? void 0 : _a.call(context, e);
|
93
|
+
});
|
94
|
+
this.outside.on("message", (e) => {
|
95
|
+
var _a;
|
96
|
+
(_a = this.onmessage) == null ? void 0 : _a.call(this, e);
|
97
|
+
});
|
98
|
+
const runner = new InlineWorkerRunner(options, context);
|
99
|
+
let id = url instanceof URL ? url.toString() : url;
|
100
|
+
id = id.replace("?worker_file", "").replace(/^file:\/+/, "/");
|
101
|
+
const fsPath = toFilePath(id, config.root);
|
102
|
+
invalidates.push(fsPath);
|
103
|
+
runner.executeFile(fsPath).then(() => {
|
104
|
+
invalidates.forEach((path) => {
|
105
|
+
moduleCache.delete(path);
|
106
|
+
moduleCache.delete(`${path}__mock`);
|
107
|
+
});
|
108
|
+
const q = this.messageQueue;
|
109
|
+
this.messageQueue = null;
|
110
|
+
if (q)
|
111
|
+
q.forEach(this.postMessage, this);
|
112
|
+
}).catch((e) => {
|
113
|
+
var _a;
|
114
|
+
this.outside.emit("error", e);
|
115
|
+
(_a = this.onerror) == null ? void 0 : _a.call(this, e);
|
116
|
+
console.error(e);
|
117
|
+
});
|
118
|
+
}
|
119
|
+
dispatchEvent(event) {
|
120
|
+
this.outside.emit(event.type, event);
|
121
|
+
return true;
|
122
|
+
}
|
123
|
+
addEventListener(event, fn) {
|
124
|
+
this.outside.on(event, fn);
|
125
|
+
}
|
126
|
+
removeEventListener(event, fn) {
|
127
|
+
this.outside.off(event, fn);
|
128
|
+
}
|
129
|
+
postMessage(data) {
|
130
|
+
if (this.messageQueue != null)
|
131
|
+
this.messageQueue.push(data);
|
132
|
+
else
|
133
|
+
this.inside.emit("message", { data });
|
134
|
+
}
|
135
|
+
terminate() {
|
136
|
+
this.outside.clear();
|
137
|
+
this.inside.clear();
|
138
|
+
}
|
139
|
+
};
|
140
|
+
}
|
141
|
+
|
142
|
+
export { defineWebWorker as d };
|
package/dist/pure.js
CHANGED
@@ -1,142 +1,3 @@
|
|
1
|
-
|
2
|
-
import
|
3
|
-
|
4
|
-
function getWorkerState() {
|
5
|
-
return globalThis.__vitest_worker__;
|
6
|
-
}
|
7
|
-
class Bridge {
|
8
|
-
constructor() {
|
9
|
-
this.callbacks = {};
|
10
|
-
}
|
11
|
-
on(event, fn) {
|
12
|
-
var _a;
|
13
|
-
(_a = this.callbacks)[event] ?? (_a[event] = []);
|
14
|
-
this.callbacks[event].push(fn);
|
15
|
-
}
|
16
|
-
off(event, fn) {
|
17
|
-
if (this.callbacks[event])
|
18
|
-
this.callbacks[event] = this.callbacks[event].filter((f) => f !== fn);
|
19
|
-
}
|
20
|
-
removeEvents(event) {
|
21
|
-
this.callbacks[event] = [];
|
22
|
-
}
|
23
|
-
clear() {
|
24
|
-
this.callbacks = {};
|
25
|
-
}
|
26
|
-
emit(event, ...data) {
|
27
|
-
return (this.callbacks[event] || []).map((fn) => fn(...data));
|
28
|
-
}
|
29
|
-
}
|
30
|
-
class InlineWorkerRunner extends VitestRunner {
|
31
|
-
constructor(options, context) {
|
32
|
-
super(options);
|
33
|
-
this.context = context;
|
34
|
-
}
|
35
|
-
prepareContext(context) {
|
36
|
-
const ctx = super.prepareContext(context);
|
37
|
-
this.context.self.importScripts = () => {
|
38
|
-
};
|
39
|
-
return Object.assign(ctx, this.context, {
|
40
|
-
importScripts: () => {
|
41
|
-
}
|
42
|
-
});
|
43
|
-
}
|
44
|
-
}
|
45
|
-
function defineWebWorker() {
|
46
|
-
if ("Worker" in globalThis)
|
47
|
-
return;
|
48
|
-
const { config, rpc, mockMap, moduleCache } = getWorkerState();
|
49
|
-
const options = {
|
50
|
-
fetchModule(id) {
|
51
|
-
return rpc.fetch(id);
|
52
|
-
},
|
53
|
-
resolveId(id, importer) {
|
54
|
-
return rpc.resolveId(id, importer);
|
55
|
-
},
|
56
|
-
moduleCache,
|
57
|
-
mockMap,
|
58
|
-
interopDefault: config.deps.interopDefault ?? true,
|
59
|
-
root: config.root,
|
60
|
-
base: config.base
|
61
|
-
};
|
62
|
-
globalThis.Worker = class Worker {
|
63
|
-
constructor(url) {
|
64
|
-
this.inside = new Bridge();
|
65
|
-
this.outside = new Bridge();
|
66
|
-
this.messageQueue = [];
|
67
|
-
this.onmessage = null;
|
68
|
-
this.onmessageerror = null;
|
69
|
-
this.onerror = null;
|
70
|
-
const invalidates = [];
|
71
|
-
const context = {
|
72
|
-
onmessage: null,
|
73
|
-
dispatchEvent: (event) => {
|
74
|
-
this.inside.emit(event.type, event);
|
75
|
-
return true;
|
76
|
-
},
|
77
|
-
addEventListener: this.inside.on,
|
78
|
-
removeEventListener: this.inside.off,
|
79
|
-
postMessage: (data) => {
|
80
|
-
this.outside.emit("message", { data });
|
81
|
-
},
|
82
|
-
get self() {
|
83
|
-
return context;
|
84
|
-
},
|
85
|
-
get global() {
|
86
|
-
return context;
|
87
|
-
},
|
88
|
-
invalidates
|
89
|
-
};
|
90
|
-
this.inside.on("message", (e) => {
|
91
|
-
var _a;
|
92
|
-
(_a = context.onmessage) == null ? void 0 : _a.call(context, e);
|
93
|
-
});
|
94
|
-
this.outside.on("message", (e) => {
|
95
|
-
var _a;
|
96
|
-
(_a = this.onmessage) == null ? void 0 : _a.call(this, e);
|
97
|
-
});
|
98
|
-
const runner = new InlineWorkerRunner(options, context);
|
99
|
-
let id = url instanceof URL ? url.toString() : url;
|
100
|
-
id = id.replace("?worker_file", "").replace(/^file:\/+/, "/");
|
101
|
-
const fsPath = toFilePath(id, config.root);
|
102
|
-
invalidates.push(fsPath);
|
103
|
-
runner.executeFile(fsPath).then(() => {
|
104
|
-
invalidates.forEach((path) => {
|
105
|
-
moduleCache.delete(path);
|
106
|
-
moduleCache.delete(`${path}__mock`);
|
107
|
-
});
|
108
|
-
const q = this.messageQueue;
|
109
|
-
this.messageQueue = null;
|
110
|
-
if (q)
|
111
|
-
q.forEach(this.postMessage, this);
|
112
|
-
}).catch((e) => {
|
113
|
-
var _a;
|
114
|
-
this.outside.emit("error", e);
|
115
|
-
(_a = this.onerror) == null ? void 0 : _a.call(this, e);
|
116
|
-
console.error(e);
|
117
|
-
});
|
118
|
-
}
|
119
|
-
dispatchEvent(event) {
|
120
|
-
this.outside.emit(event.type, event);
|
121
|
-
return true;
|
122
|
-
}
|
123
|
-
addEventListener(event, fn) {
|
124
|
-
this.outside.on(event, fn);
|
125
|
-
}
|
126
|
-
removeEventListener(event, fn) {
|
127
|
-
this.outside.off(event, fn);
|
128
|
-
}
|
129
|
-
postMessage(data) {
|
130
|
-
if (this.messageQueue != null)
|
131
|
-
this.messageQueue.push(data);
|
132
|
-
else
|
133
|
-
this.inside.emit("message", { data });
|
134
|
-
}
|
135
|
-
terminate() {
|
136
|
-
this.outside.clear();
|
137
|
-
this.inside.clear();
|
138
|
-
}
|
139
|
-
};
|
140
|
-
}
|
141
|
-
|
142
|
-
export { defineWebWorker };
|
1
|
+
export { d as defineWebWorker } from './pure-263d3614.js';
|
2
|
+
import 'vitest/node';
|
3
|
+
import 'vite-node/utils';
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/web-worker",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.10.0",
|
5
5
|
"description": "Web Worker support for testing in Vitest",
|
6
6
|
"main": "./dist/index.js",
|
7
7
|
"module": "./dist/index.js",
|
@@ -26,7 +26,7 @@
|
|
26
26
|
"vitest": "*"
|
27
27
|
},
|
28
28
|
"dependencies": {
|
29
|
-
"vite-node": "0.
|
29
|
+
"vite-node": "0.10.0"
|
30
30
|
},
|
31
31
|
"devDependencies": {
|
32
32
|
"rollup": "^2.70.2"
|