Skip to content

Commit

Permalink
Add support for import() (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Sep 15, 2022
1 parent dbe0cc1 commit 5d38649
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions fixtures/fetch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fetch from 'node-fetch';

export default async function (port) {
const res = await fetch('http://localhost:' + port + '/');
return res.text();
}
4 changes: 4 additions & 0 deletions fixtures/jump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = async function (port) {
const fetch = await import('./fetch.mjs');
return fetch.default(port);
};
3 changes: 1 addition & 2 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ void Worker::Start(bool own_loop, bool own_microtaskqueue) {
{},
{},
static_cast<EnvironmentFlags::Flags>(
EnvironmentFlags::kTrackUnmanagedFds |
EnvironmentFlags::kNoRegisterESMLoader),
EnvironmentFlags::kTrackUnmanagedFds),
thread_id,
std::move(inspector_parent_handle));
assert(env_ != nullptr);
Expand Down
17 changes: 17 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import { join } from 'path';
import SynchronousWorker from '../';

describe('SynchronousWorker allows running Node.js code', () => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 5d38649

Please sign in to comment.