From 5d38649c53705faeb7b344fd9f4989a9b0b3598f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 15 Sep 2022 15:01:31 +0200 Subject: [PATCH] Add support for import() (#2) Signed-off-by: Matteo Collina Signed-off-by: Matteo Collina --- fixtures/fetch.mjs | 6 ++++++ fixtures/jump.js | 4 ++++ src/binding.cc | 3 +-- test/index.ts | 17 +++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 fixtures/fetch.mjs create mode 100644 fixtures/jump.js diff --git a/fixtures/fetch.mjs b/fixtures/fetch.mjs new file mode 100644 index 0000000..d5991e7 --- /dev/null +++ b/fixtures/fetch.mjs @@ -0,0 +1,6 @@ +import fetch from 'node-fetch'; + +export default async function (port) { + const res = await fetch('http://localhost:' + port + '/'); + return res.text(); +} diff --git a/fixtures/jump.js b/fixtures/jump.js new file mode 100644 index 0000000..2b63b36 --- /dev/null +++ b/fixtures/jump.js @@ -0,0 +1,4 @@ +module.exports = async function (port) { + const fetch = await import('./fetch.mjs'); + return fetch.default(port); +}; diff --git a/src/binding.cc b/src/binding.cc index e0ab488..4173a82 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -258,8 +258,7 @@ void Worker::Start(bool own_loop, bool own_microtaskqueue) { {}, {}, static_cast( - EnvironmentFlags::kTrackUnmanagedFds | - EnvironmentFlags::kNoRegisterESMLoader), + EnvironmentFlags::kTrackUnmanagedFds), thread_id, std::move(inspector_parent_handle)); assert(env_ != nullptr); diff --git a/test/index.ts b/test/index.ts index 10f8735..300f295 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,4 +1,5 @@ import assert from 'assert'; +import { join } from 'path'; import SynchronousWorker from '../'; describe('SynchronousWorker allows running Node.js code', () => { @@ -208,4 +209,20 @@ describe('SynchronousWorker allows running Node.js code', () => { }); await w.stop(); }); + + it('support import', async() => { + const w = new SynchronousWorker({ sharedEventLoop: true, sharedMicrotaskQueue: true }); + const req = w.createRequire(__filename); + const httpServer = req('http').createServer((_, res) => { + res.writeHead(200); + res.end('Ok\n'); + }); + await new Promise((resolve) => { + httpServer.listen(0, resolve); + }); + const fetch = req(join(__dirname, '..', 'fixtures', 'jump.js')); + const res = await fetch(httpServer.address().port); + assert.strictEqual(res, 'Ok\n'); + httpServer.close(); + }); });