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

Dynamic imports #2

Merged
merged 1 commit into from
Nov 9, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## master

- dynamic ESM imports through `nodo.import`

## 1.6.3

- use Nodo.timeout for all JS calls
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ class FooBar < Nodo::Core
end
```

### Dynamic ESM imports

ES modules can be imported dynamically using `nodo.import()`:

```ruby
class DynamicFoo < Nodo::Core
function :v4, <<~JS
async () => {
const uuid = await nodo.import('uuid');
return await uuid.v4()
}
JS
end
```

Note that the availability of dynamic imports depends on your Node version.

### Alternate function definition syntax

JS code can also be supplied using the `code:` keyword argument:
Expand Down
10 changes: 7 additions & 3 deletions lib/nodo/nodo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = (function() {
const path = require('path');
const fs = require('fs');
const performance = require('perf_hooks').performance;

let server, closing;
const classes = {};
const contexts = {};
Expand Down Expand Up @@ -48,7 +48,11 @@ module.exports = (function() {
console.log(`[Nodo] ${message}`);
}
}


async function import_module(specifier) {
return await import(specifier);
}

const core = {
run: (socket) => {
debug('Starting up...');
Expand Down Expand Up @@ -135,5 +139,5 @@ module.exports = (function() {
}
};

return { core: core, debug: debug };
return { core: core, debug: debug, import: import_module };
})();
29 changes: 27 additions & 2 deletions test/nodo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ def test_evaluation_can_require_on_its_own
nodo = Class.new(Nodo::Core).new
nodo.evaluate('const uuid = require("uuid")')
uuid = nodo.evaluate('uuid.v4()')
assert_match /\A\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\z/, uuid
assert_uuid uuid
end

def test_evaluation_can_access_requires
nodo = Class.new(Nodo::Core) { require :uuid }
uuid = nodo.new.evaluate('uuid.v4()')
assert_match /\A\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\z/, uuid
assert_uuid uuid
end

def test_cannot_instantiate_core
Expand All @@ -239,6 +239,27 @@ def test_cannot_instantiate_core
end
end

def test_dynamic_imports_in_functions
klass = Class.new(Nodo::Core) do
function :v4, <<~JS
async () => {
const uuid = await nodo.import('uuid');
return await uuid.v4()
}
JS
end
nodo = klass.new
assert_uuid uuid_1 = nodo.v4
assert_uuid uuid_2 = nodo.v4
assert uuid_1 != uuid_2
end

def test_dynamic_imports_in_evaluation
nodo = Class.new(Nodo::Core)
uuid = nodo.new.evaluate("nodo.import('uuid').then((uuid) => uuid.v4()).catch((e) => null)")
assert_uuid uuid
end

private

def test_logger
Expand All @@ -256,5 +277,9 @@ def with_logger(logger)
ensure
Nodo.logger = prev_logger
end

def assert_uuid(obj)
assert_match /\A\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\z/, obj
end

end