Node.js Inject: How to Conduct and Why to Use
This technique is not as popular as it should be

What Is Node JS Dependency Injection?
Dependency injection is a code wiring technique that presumes that the dependencies are input by an external entity (passed as parameters or references), not hardcoded (created or required) inside a module. Dependencies are the objects or services that a module can use.
Dependency injections are a great benefit of JavaScript and many other languages because this technique helps create independent, scalable, and reusable components. At the same time, dependency injections in Node.js web development are not as popular as you might expect.
Most would say that dependency injection in NodeJS is unnecessary because you can import dependencies to a module using the command “require”. Yet, such an approach creates a more complex code to maintain and test.
NodeJS dependency injection solves this problem.
Why to Use Node JS Dependency Injection?
JavaScript dependency injection offers four essential benefits:
1. Simple maintenance
The code becomes more reusable and easier to configure since the dependency injection technique uncouples an object and its dependency. Imagine that there are three components: X, Y, and Z. X depends on Y, and Y depends on Z. What if you need to add something to Y or Z? If you do not use dependency injection, you have to do a lot of additional coding and refactoring in X as well. If you use dependency injections, adding anything becomes much more straightforward. After all, X only cares about Y and not about Z or anything else they depend upon.
2. Simple testing
The unit testing becomes much easier to perform. If Y is directly referenced in X, you cannot test X without testing Y. If Y is injected in X, you can create a mocked or stubbed Y to test X. For instance, if Y is a web service response, you can substitute it with a hard-coded file, which would work perfectly for testing purposes.
3. Simple management
When a large team is working on a single project, sooner or later, their tasks will intersect. With Node.js dependency injections, team members isolate their components and become less dependent on others’ work. Thus, the work on a few parts can be conducted simultaneously, and compatibility errors are less likely to occur.
4. Single responsibility
Single responsibility is the first of the five principles of object-oriented design, called SOLID principles. Each component should be responsible for one thing. Otherwise, if your component solves two tasks, you will need to test both scenarios with each change in the code. JavaScript dependency injection is a way to implement the single responsibility principle.
How to Perform NodeJS Injection?
There are different ways of how dependency injection can be performed. You can find plenty of primitive examples on the web to understand the essence of this technique. Meanwhile, I would like to show you how to execute a dependency injection in an actual code with the help of the Frida framework.
Frida Framework
Frida by itself already has an example of code injection for Node.js, but it seems that it is a bit outdated and could work only with a particular Node.js version – GUIDE. It uses V8 embed code to execute a JS string. I have updated the code to Node.js v8.16.0 x64, but I will not elaborate on injection details here. You can easily find out more via the following link, so instead, let’s go to the injected code.At first, let’s see which Frida code we will use (you can get more details here).
- Module.findExportByName finds C exported function by its name. There is one little caveat – so-called “mangled names”. In short, when we investigate C++ compiled code, we could see a lot of names like [email protected]@v8@@[email protected]. First of our points is to find all required function names in a disassembler like IDA or OllyDbg and to get the memory address of it.
- NativeFunction(POINTER, RETURN_VALUE, ARGUMENTS) is used to define in-js bindings for a native function. Here, we need to describe correct arguments count and return values to call these functions later.
- NativeCallback is a code that will be executed after a native function call.
- WeakRef.bind is used to monitor specified pointer and the callback when it gets garbage-collected or on Frida detach.
- Memory.alloc(N) allocates N bytes in memory and returns pointer for this memory region.
Concepts and Functions Used
Here, I want to define some concepts that I am going to use:
- Isolate represents an isolated instance of the V8 engine. V8 isolates have completely separate states. Objects from one isolate cannot be used in another isolate. When V8 is initialized, a default isolate is implicitly created and entered. The embedder can create additional isolates and use them in parallel in multiple threads. An isolate can be entered by at most one thread at any given point in time. The Locker/Unlocker API must be used to synchronize. In short, it is a kind of sandbox, which contains its own states.
- Context stands for a sandboxed execution context with its own set of built-in objects and functions.
- HandleScope is a stack-allocated class, which governs a number of local handles. After a handle scope has been created, all local handles will be allocated within that handle scope until either the handle scope is deleted or another handle scope is created. If there is already a handle scope and a new one is created, all allocations will take place in the new handle scope until it is deleted. After that, new handles will again be allocated in the original handle scope. After the handle scope of a local handle has been deleted, the garbage collector will no longer track the object stored in the handle and may deallocate it. The behavior of accessing a handle for which the handle scope has been deleted is undefined.
- Script stands for a compiled JavaScript script.
- String is a JS string value.
- Value is a superclass of all JavaScript values and objects.
Next, I describe the functions of libuv that we will use – a lib that allows using an async, event-driven style of programming:
- uv_async_init initializes handle, specifies the callback that will be executed inside the event loop;
- uv_default_loop takes default event loop;
- uv_async_send calls the callback;
- uv_unref destroys the object created;
- uv_close requests handle to be closed.
We will also use some V8 functions (formatted as real method name –> name of binded js function). In particular:
- V8::Isolate::GetCurrent(v8_Isolate_GetCurrent) takes the instance of a current Isolate.
- V8::Isolate::GetCurrentContext(v8_Isolate_GetCurrentContext) gets context from the current Isolate.
- V8::Context::Enter(v8_Context_Enter) enters this context. After entering a context, all code compiled and run is compiled and run in this context. If another context is already entered, this old context is saved so it can be restored when the new context is exited.
- V8::HandleScope::Init(v8_HandleScope_init) initializes HandleScope.
- V8::String::NewFromUtf8(v8_String_NewFromUtf8) gets JS string from const char *.
- V8::Script::Compile(v8_Script_Compile) compiles the specified script (bound to current context).
- V8::Script::Run(v8_Script_Run) runs the script returning the resulting value. If the script is context-independent (created using ::New), it will be run in the currently entered context. If it is context-specific (created using ::Compile), it will be run in the context in which it was compiled.
- v8::Value::Int32Value(v8_Value_Int32Value) gets C int from JS value. Value has a lot of functions for different types.
Code Listing
Let’s go through the code listing step by step:
createFunc is a helper that is used to create JS bind with a specified signature to reuse later.
1 2 3 4 5 6 7 8 9 10 |
<span class="react-syntax-highlighter-line-number">1 </span><span class="react-syntax-highlighter-line-number">2 </span><span class="react-syntax-highlighter-line-number">3 </span><span class="react-syntax-highlighter-line-number">4 </span><span class="react-syntax-highlighter-line-number">5 </span></code><code style="color: black; background: none; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: 1.5; tab-size: 4; hyphens: none;"><span class="token" style="color: rgb(0, 119, 170);">try</span> <span class="token" style="color: rgb(153, 153, 153);">{</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> <span class="token function-variable" style="color: rgb(221, 74, 104);">createFunc</span> <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token parameter">name<span class="token" style="color: rgb(153, 153, 153);">,</span> retval<span class="token" style="color: rgb(153, 153, 153);">,</span> args</span><span class="token" style="color: rgb(153, 153, 153);">)</span> <span class="token arrow" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=></span> <span class="token" style="color: rgb(153, 153, 153);">{</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> _ptr <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token maybe-class-name">Module</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">findExportByName</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token null nil" style="color: rgb(0, 119, 170);">null</span><span class="token" style="color: rgb(153, 153, 153);">,</span> name<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">return</span> <span class="token" style="color: rgb(0, 119, 170);">new</span> <span class="token" style="color: rgb(221, 74, 104);">NativeFunction</span><span class="token" style="color: rgb(153, 153, 153);">(</span>_ptr<span class="token" style="color: rgb(153, 153, 153);">,</span> retval<span class="token" style="color: rgb(153, 153, 153);">,</span> args<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(153, 153, 153);">}</span> |
All of the calls like const uv_default_loop = createFunc(‘uv_default_loop’, ‘pointer’, []) are required to define JS bindings to native functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<span class="react-syntax-highlighter-line-number">1 </span><span class="react-syntax-highlighter-line-number">2 </span><span class="react-syntax-highlighter-line-number">3 </span><span class="react-syntax-highlighter-line-number">4 </span><span class="react-syntax-highlighter-line-number">5 </span><span class="react-syntax-highlighter-line-number">6 </span><span class="react-syntax-highlighter-line-number">7 </span><span class="react-syntax-highlighter-line-number">8 </span><span class="react-syntax-highlighter-line-number">9 </span><span class="react-syntax-highlighter-line-number">10 </span><span class="react-syntax-highlighter-line-number">11 </span><span class="react-syntax-highlighter-line-number">12 </span><span class="react-syntax-highlighter-line-number">13 </span><span class="react-syntax-highlighter-line-number">14 </span></code><code style="color: black; background: none; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: 1.5; tab-size: 4; hyphens: none;"> <span class="token" style="color: rgb(0, 119, 170);">const</span> uv_default_loop <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'uv_default_loop'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> uv_async_init <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'uv_async_init'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'int'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> uv_async_send <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'uv_async_send'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'int'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> uv_unref <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'uv_unref'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'void'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_Isolate_GetCurrent <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@v8@@[email protected]'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_Isolate_GetCurrentContext <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'?[email protected]@v8@@[email protected]@v8@@@[email protected]'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_Context_Enter <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@v8@@QEAAXXZ'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_HandleScope_init <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@@[email protected]@1@@Z'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'void'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_String_NewFromUtf8 <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@v8@@SA?AV?$Ma[email protected]@v8@@@[email protected]@[email protected]@[email protected]@Z'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'int'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'int'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_Script_Compile <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@v8@@[email protected]@v8@@@[email protected][email protected]@v8@@@[email protected]@[email protected]@12@@Z'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_Script_Run <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@v8@@[email protected]@v8@@@[email protected]'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> v8_Value_Int32Value <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">createFunc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'[email protected]@v8@@QEBAHXZ'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'int64'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> |
scriptToExecute is our injected code.
1 2 3 4 5 6 7 8 9 |
<span class="react-syntax-highlighter-line-number">1 </span><span class="react-syntax-highlighter-line-number">2 </span><span class="react-syntax-highlighter-line-number">3 </span><span class="react-syntax-highlighter-line-number">4 </span></code><code style="color: black; background: none; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: 1.5; tab-size: 4; hyphens: none;"><span class="token" style="color: rgb(0, 119, 170);">const</span> scriptToExecute <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token template-string"><span class="token template-punctuation" style="color: rgb(102, 153, 0);">`</span><span class="token" style="color: rgb(102, 153, 0);">((a, b)=>{ console.log("Hello from Frida", a, b); return a+b; })(5, 17)</span><span class="token template-punctuation" style="color: rgb(102, 153, 0);">`</span></span><span class="token" style="color: rgb(153, 153, 153);">;</span> |
We define uv async handler, then bind processPending to the default event loop, then “wake up” that event loop for a callback call. Inside the callback, we receive an Isolate instance, initialize new HandleScope, and take current context. Then, we convert the JS string to V8 string, compile, and run it. After all, we just extract C int value from the script execution result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<span class="react-syntax-highlighter-line-number">1 </span><span class="react-syntax-highlighter-line-number">2 </span><span class="react-syntax-highlighter-line-number">3 </span><span class="react-syntax-highlighter-line-number">4 </span><span class="react-syntax-highlighter-line-number">5 </span><span class="react-syntax-highlighter-line-number">6 </span><span class="react-syntax-highlighter-line-number">7 </span><span class="react-syntax-highlighter-line-number">8 </span><span class="react-syntax-highlighter-line-number">9 </span><span class="react-syntax-highlighter-line-number">10 </span><span class="react-syntax-highlighter-line-number">11 </span><span class="react-syntax-highlighter-line-number">12 </span><span class="react-syntax-highlighter-line-number">13 </span><span class="react-syntax-highlighter-line-number">14 </span><span class="react-syntax-highlighter-line-number">15 </span><span class="react-syntax-highlighter-line-number">16 </span><span class="react-syntax-highlighter-line-number">17 </span><span class="react-syntax-highlighter-line-number">18 </span><span class="react-syntax-highlighter-line-number">19 </span><span class="react-syntax-highlighter-line-number">20 </span><span class="react-syntax-highlighter-line-number">21 </span><span class="react-syntax-highlighter-line-number">22 </span><span class="react-syntax-highlighter-line-number">23 </span></code><code style="color: black; background: none; text-shadow: white 0px 1px; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; overflow-wrap: normal; line-height: 1.5; tab-size: 4; hyphens: none;"> <span class="token" style="color: rgb(0, 119, 170);">const</span> processPending <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(0, 119, 170);">new</span> <span class="token" style="color: rgb(221, 74, 104);">NativeCallback</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(0, 119, 170);">function</span> <span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 153, 153);">)</span> <span class="token" style="color: rgb(153, 153, 153);">{</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> isolate <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">v8_Isolate_GetCurrent</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> scope <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">alloc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 0, 85);">128</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(221, 74, 104);">v8_HandleScope_init</span><span class="token" style="color: rgb(153, 153, 153);">(</span>scope<span class="token" style="color: rgb(153, 153, 153);">,</span> isolate<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> opts <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">alloc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 0, 85);">128</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> context <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">v8_Isolate_GetCurrentContext</span><span class="token" style="color: rgb(153, 153, 153);">(</span>isolate<span class="token" style="color: rgb(153, 153, 153);">,</span> opts<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> item <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> scriptToExecute<span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> unkMem <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">alloc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 0, 85);">128</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> source <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">v8_String_NewFromUtf8</span><span class="token" style="color: rgb(153, 153, 153);">(</span>unkMem<span class="token" style="color: rgb(153, 153, 153);">,</span> isolate<span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">allocUtf8String</span><span class="token" style="color: rgb(153, 153, 153);">(</span>item<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 0, 85);">0</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">-</span><span class="token" style="color: rgb(153, 0, 85);">1</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> script <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">v8_Script_Compile</span><span class="token" style="color: rgb(153, 153, 153);">(</span>context<span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">readPointer</span><span class="token" style="color: rgb(153, 153, 153);">(</span>context<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">,</span> source<span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 0, 85);">NULL</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> result <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">v8_Script_Run</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">readPointer</span><span class="token" style="color: rgb(153, 153, 153);">(</span>context<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">,</span> context<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> intResult <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(221, 74, 104);">v8_Value_Int32Value</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">readPointer</span><span class="token" style="color: rgb(153, 153, 153);">(</span>result<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token console" style="color: rgb(221, 74, 104);">console</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">log</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">'Result'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> intResult<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(153, 153, 153);">}</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'void'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> onClose <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token" style="color: rgb(0, 119, 170);">new</span> <span class="token" style="color: rgb(221, 74, 104);">NativeCallback</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(0, 119, 170);">function</span> <span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 153, 153);">)</span> <span class="token" style="color: rgb(153, 153, 153);">{</span> <span class="token" style="color: rgb(153, 153, 153);">}</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(102, 153, 0);">'void'</span><span class="token" style="color: rgb(153, 153, 153);">,</span> <span class="token" style="color: rgb(153, 153, 153);">[</span><span class="token" style="color: rgb(102, 153, 0);">'pointer'</span><span class="token" style="color: rgb(153, 153, 153);">]</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(0, 119, 170);">const</span> async <span class="token" style="color: rgb(154, 110, 58); background: rgba(255, 255, 255, 0.5);">=</span> <span class="token maybe-class-name">Memory</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">alloc</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 0, 85);">24</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(221, 74, 104);">uv_async_init</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(221, 74, 104);">uv_default_loop</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">,</span> async<span class="token" style="color: rgb(153, 153, 153);">,</span> processPending<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(221, 74, 104);">uv_async_send</span><span class="token" style="color: rgb(153, 153, 153);">(</span>async<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(221, 74, 104);">uv_unref</span><span class="token" style="color: rgb(153, 153, 153);">(</span>async<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(153, 153, 153);">}</span> <span class="token" style="color: rgb(0, 119, 170);">catch</span> <span class="token" style="color: rgb(153, 153, 153);">(</span>ex<span class="token" style="color: rgb(153, 153, 153);">)</span> <span class="token" style="color: rgb(153, 153, 153);">{</span> <span class="token console" style="color: rgb(221, 74, 104);">console</span><span class="token" style="color: rgb(153, 153, 153);">.</span><span class="token method property-access" style="color: rgb(221, 74, 104);">log</span><span class="token" style="color: rgb(153, 153, 153);">(</span><span class="token" style="color: rgb(102, 153, 0);">"Injected code execution error"</span><span class="token" style="color: rgb(153, 153, 153);">,</span> ex<span class="token" style="color: rgb(153, 153, 153);">)</span><span class="token" style="color: rgb(153, 153, 153);">;</span> <span class="token" style="color: rgb(153, 153, 153);">}</span> |
To Wrap Up
Node js dependency injection is not as popular as it should be. It is designed to simplify and create a time-efficient coding experience, not vice versa.
There are different ways to perform this technique, and the one I offered you here is just one of them. You can find plenty of other examples on StackOverflow.
Yet, regardless of how you implement it, the benefits remain simple maintenance, testing, management, and single responsibility.
Do you have an idea for a Node JS app?
Does it seem to you that Node.js is the framework you need? Please, find out more about Node.js development services that we offer.
Written by Anton Trofimov, Full-stack JavaScript Developer, and Tania Matviiok, Content Manager for helping with the article @ Keenethics