Skip to content

Commit 5d38649

Browse files
authored
Add support for import() (#2)
Signed-off-by: Matteo Collina <[email protected]> Signed-off-by: Matteo Collina <[email protected]>
1 parent dbe0cc1 commit 5d38649

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

fixtures/fetch.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fetch from 'node-fetch';
2+
3+
export default async function (port) {
4+
const res = await fetch('http://localhost:' + port + '/');
5+
return res.text();
6+
}

fixtures/jump.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = async function (port) {
2+
const fetch = await import('./fetch.mjs');
3+
return fetch.default(port);
4+
};

src/binding.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ void Worker::Start(bool own_loop, bool own_microtaskqueue) {
258258
{},
259259
{},
260260
static_cast<EnvironmentFlags::Flags>(
261-
EnvironmentFlags::kTrackUnmanagedFds |
262-
EnvironmentFlags::kNoRegisterESMLoader),
261+
EnvironmentFlags::kTrackUnmanagedFds),
263262
thread_id,
264263
std::move(inspector_parent_handle));
265264
assert(env_ != nullptr);

test/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from 'assert';
2+
import { join } from 'path';
23
import SynchronousWorker from '../';
34

45
describe('SynchronousWorker allows running Node.js code', () => {
@@ -208,4 +209,20 @@ describe('SynchronousWorker allows running Node.js code', () => {
208209
});
209210
await w.stop();
210211
});
212+
213+
it('support import', async() => {
214+
const w = new SynchronousWorker({ sharedEventLoop: true, sharedMicrotaskQueue: true });
215+
const req = w.createRequire(__filename);
216+
const httpServer = req('http').createServer((_, res) => {
217+
res.writeHead(200);
218+
res.end('Ok\n');
219+
});
220+
await new Promise((resolve) => {
221+
httpServer.listen(0, resolve);
222+
});
223+
const fetch = req(join(__dirname, '..', 'fixtures', 'jump.js'));
224+
const res = await fetch(httpServer.address().port);
225+
assert.strictEqual(res, 'Ok\n');
226+
httpServer.close();
227+
});
211228
});

0 commit comments

Comments
 (0)