Skip to content

Commit 937775c

Browse files
committed
Merge branch 'Feature-2' of github.com:metcoder95/web-worker into metcoder95-Feature-2
2 parents 1fda46c + 4ce1305 commit 937775c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/node/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import URL, { fileURLToPath } from 'node:url';
17+
import URL, { fileURLToPath } from 'url';
18+
import fs from 'fs';
1819
import VM from 'vm';
1920
import threads from 'worker_threads';
2021

@@ -192,7 +193,7 @@ function workerThread() {
192193
evaluateDataUrl(mod, name);
193194
}
194195
else {
195-
require(mod);
196+
importScripts(mod);
196197
}
197198
}
198199
catch (err) {
@@ -221,3 +222,17 @@ function parseDataUrl(url) {
221222
}
222223
return { type, data };
223224
}
225+
226+
function importScripts() {
227+
for (let i = 0; i < arguments.length; i++) {
228+
const url = arguments[i];
229+
let code;
230+
if (/^data:/.test(url)) {
231+
code = parseDataUrl(url).data;
232+
}
233+
else {
234+
code = fs.readFileSync(url, 'utf-8');
235+
}
236+
VM.runInThisContext(code, { filename: url });
237+
}
238+
}

0 commit comments

Comments
 (0)