Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for import() #2

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});