chunk_id large_stringlengths 36 36 | doc_type large_stringclasses 2
values | category large_stringclasses 17
values | document large_stringclasses 50
values | version_raw large_stringclasses 590
values | chunk_index int64 0 262 | text large_stringlengths 11 5.69k | file_path large_stringclasses 882
values |
|---|---|---|---|---|---|---|---|
6f53141a-e7c1-5e5c-917d-aa7fa59cc924 | docs | nodejs | nodejs_addons | 7x | 16 | ### Wrapping C++ objects[#](#addons_wrapping_c_objects)
It is also possible to wrap C++ objects/classes in a way that allows new
instances to be created using the JavaScript `new` operator:
```None
// addon.cc
#include <node.h>
#include "myobject.h"
namespace demo {
using v8::Local;
using v8::Object;
void InitAll(... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
115c5204-7ff0-5ec1-99e0-1c18a6947fa1 | docs | nodejs | nodejs_addons | 7x | 17 | } // namespace demo
#endif
```
In `myobject.cc`, implement the various methods that are to be exposed.
Below, the method `plusOne()` is exposed by adding it to the constructor's
prototype:
```None
// myobject.cc
#include "myobject.h"
namespace demo {
using v8::Context;
using v8::Function;
using v8::FunctionCallba... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
f1a860ca-ce30-541c-8c9c-36bd6709ef08 | docs | nodejs | nodejs_addons | 7x | 18 | constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "MyObject"),
tpl->GetFunction());
}
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.IsConstructCall()) {
// Invoked as constructor: `new MyO... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
49686d96-3b8d-55c2-8599-99d2dcab53a5 | docs | nodejs | nodejs_addons | 7x | 19 | Isolate* isolate = args.GetIsolate();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
obj->value_ += 1;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}
} // namespace demo
```
To build this example, the `myobject.cc` file must be added to the
`binding.gyp`:
```None
{
"target... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
d60f9050-7738-53de-aad7-e193fcb90b17 | docs | nodejs | nodejs_addons | 7x | 20 | First, the `createObject()` method is implemented in `addon.cc`:
```None
// addon.cc
#include <node.h>
#include "myobject.h"
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& ... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
f0695a90-a42c-56fc-b0d3-4d3d8b4d4f8d | docs | nodejs | nodejs_addons | 7x | 21 | static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
static v8::Persistent<v8::Function> ... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
7a76b20a-845c-5faf-9baf-7c3ba716e22c | docs | nodejs | nodejs_addons | 7x | 22 | Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
constructor.Reset(isolate, tpl->GetFunction());
}
void MyObje... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
9456bc0c-000a-5134-8b71-b7126b822835 | docs | nodejs | nodejs_addons | 7x | 23 | cons->NewInstance(context, argc, argv).ToLocalChecked();
args.GetReturnValue().Set(instance);
}
}
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const unsigned argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Loc... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
daf6681f-3369-51d1-b04c-a5678c56f273 | docs | nodejs | nodejs_addons | 7x | 24 | "addon.cc",
"myobject.cc"
]
}
]
}
```
Test it with:
```None
// test.js
const createObject = require('./build/Release/addon');
const obj = createObject(10);
console.log(obj.plusOne());
// Prints: 11
console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13
const ... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
de3513c1-dc79-536b-afe6-292d6a90fb0a | docs | nodejs | nodejs_addons | 7x | 25 | using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
MyObject::NewInstance(args);
}
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
04ec563f-fe9d-5f2f-a2a2-61ad9e769668 | docs | nodejs | nodejs_addons | 7x | 26 | class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Isolate* isolate);
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
inline double value() const { return value_; }
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::Functio... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
1214d5c4-3994-554f-858f-4db22bec3a42 | docs | nodejs | nodejs_addons | 7x | 27 | Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
constructor.Reset(isolate, tpl->GetFunction());
}
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate ... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
4d06ed05-5dd6-5442-a432-a642e9918c3a | docs | nodejs | nodejs_addons | 7x | 28 | args.GetReturnValue().Set(instance);
}
}
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const unsigned argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Context> context... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
97a31713-c96d-5beb-af13-69fd4585c1ac | docs | nodejs | nodejs_addons | 7x | 29 | #### void AtExit(callback, args)[#](#addons_void_atexit_callback_args)
* `callback`: `void (*)(void*)` - A pointer to the function to call at exit.
* `args`: `void*` - A pointer to pass to the callback at exit.
Registers exit hooks that run after the event loop has ended but before the VM
is killed.
AtExit takes two... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
061648ab-6680-5305-b261-51e0a8c19c02 | docs | nodejs | nodejs_addons | 7x | 30 | assert(!obj.IsEmpty()); // assert VM is still alive
assert(obj->IsObject());
at_exit_cb1_called++;
}
static void at_exit_cb2(void* arg) {
assert(arg == static_cast<void*>(cookie));
at_exit_cb2_called++;
}
static void sanity_check(void*) {
assert(at_exit_cb1_called == 1);
assert(at_exit_cb2_called == 2);... | large_dataset/raw/docs/nodejs/addons/addons_7x.md |
ef15a1f9-1bd1-58bb-aba2-348748023f9e | docs | nodejs | nodejs_addons | 8x | 0 | # C++ Addons#
Source: https://nodejs.org/docs/latest-v8.x/api/addons.html
Version: 8.x
Node.js Addons are dynamically-linked shared objects, written in C++, that
can be loaded into Node.js using the [`require()`](modules.html#modules_require) function, and used
just as if they were an ordinary Node.js module. They ar... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
d8cf118e-75ba-52a1-a5b2-2b3c950fca06 | docs | nodejs | nodejs_addons | 8x | 1 | threads and all of the asynchronous behaviors of the platform. It also
serves as a cross-platform abstraction library, giving easy, POSIX-like
access across all major operating systems to many common system tasks, such
as interacting with the filesystem, sockets, timers, and system events. libuv
also provides... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
dbb72775-d7cc-544a-8264-756060570141 | docs | nodejs | nodejs_addons | 8x | 2 | OpenSSL. These other libraries are located in the `deps/` directory in the
Node.js source tree. Only the libuv, OpenSSL, V8 and zlib symbols are
purposefully re-exported by Node.js and may be used to various extents by
Addons.
See [Linking to Node.js' own dependencies](#addons_linking_to_node_js_own_dependenc... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
89fab58c-8308-56c4-8085-e52e81ffe59c | docs | nodejs | nodejs_addons | 8x | 3 | Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, init)
} // namespace demo
```
Note that all Node.js Addons must export an initialization ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
e965c6b3-baba-591d-926b-d55f11e96796 | docs | nodejs | nodejs_addons | 8x | 4 | using a JSON-like format. This file is used by [node-gyp](https://github.com/nodejs/node-gyp) — a tool written
specifically to compile Node.js Addons.
```None
{
"targets": [
{
"target_name": "addon",
"sources": [ "hello.cc" ]
}
]
}
```
*Note*: A version of the `node-gyp` utility is bundled and... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
0321cc2e-987c-5452-aeec-efe898a0f09a | docs | nodejs | nodejs_addons | 8x | 5 | will generate either a `Makefile` (on Unix platforms) or a `vcxproj` file
(on Windows) in the `build/` directory.
Next, invoke the `node-gyp build` command to generate the compiled `addon.node`
file. This will be put into the `build/Release/` directory.
When using `npm install` to install a Node.js Addon, npm uses it... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
188ac85e-b020-599b-8f9c-3890cb592066 | docs | nodejs | nodejs_addons | 8x | 6 | it is compiled (i.e. sometimes it may be in `./build/Debug/`), Addons can use
the [bindings](https://github.com/TooTallNate/node-bindings) package to load the compiled module.
Note that while the `bindings` package implementation is more sophisticated
in how it locates Addon modules, it is essentially using a try-catc... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
d4634198-20ab-528c-a9b9-4a2f998e4e75 | docs | nodejs | nodejs_addons | 8x | 7 | * When `node-gyp` runs, it will detect the specific release version of Node.js
and download either the full source tarball or just the headers. If the full
source is downloaded, Addons will have complete access to the full set of
Node.js dependencies. However, if only the Node.js headers are downloaded, then
on... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
7b26eb9c-45ed-53c8-bc9e-5353d398be2a | docs | nodejs | nodejs_addons | 8x | 8 | omitted and Node.js will still find and initialize the Addon. One caveat,
however, is that Node.js will first attempt to locate and load modules or
JavaScript files that happen to share the same base name. For instance, if
there is a file `addon.js` in the same directory as the binary `addon.node`,
then [`require('addo... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
6902bb3c-0ffd-54e3-8793-a80965cef36d | docs | nodejs | nodejs_addons | 8x | 9 | changes but there is little that Node.js can do currently to ensure stability
of the V8 APIs.
The [Native Abstractions for Node.js](https://github.com/nodejs/nan) (or `nan`) provide a set of tools that
Addon developers are recommended to use to keep compatibility between past and
future releases of V8 and Node.js. See... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
ab3c22f3-fee1-50f9-979c-d124b1f94e8d | docs | nodejs | nodejs_addons | 8x | 10 | recompilation. Addons are built/packaged with the same approach/tools
outlined in this document (node-gyp, etc.). The only difference is the
set of APIs that are used by the native code. Instead of using the V8
or [Native Abstractions for Node.js](https://github.com/nodejs/nan) APIs, the functions available
in the N-AP... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
315e1259-2a32-5372-a3fc-469e29759cc2 | docs | nodejs | nodejs_addons | 8x | 11 | status = napi_set_named_property(env, exports, "hello", fn);
if (status != napi_ok) return nullptr;
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, init)
} // namespace demo
```
The functions available and how to use them are documented in the
section titled [C/C++ Addons - N-API](n-api.html).
## Addon e... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
027a6d5d-f199-5ad1-9cc7-5ec5bedb9c3f | docs | nodejs | nodejs_addons | 8x | 12 | filename to the `sources` array. For example:
```None
"sources": ["addon.cc", "myexample.cc"]
```
Once the `binding.gyp` file is ready, the example Addons can be configured and
built using `node-gyp`:
```None
$ node-gyp configure build
```
### Function arguments[#](#addons_function_arguments)
Addons will typically... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
5db601d2-3594-5535-8986-1172dc319f69 | docs | nodejs | nodejs_addons | 8x | 13 | void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
// Check the number of arguments passed.
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
20d86a06-c910-55af-a19c-693ed6b42e22 | docs | nodejs | nodejs_addons | 8x | 14 | Once compiled, the example Addon can be required and used from within Node.js:
```None
// test.js
const addon = require('./build/Release/addon');
console.log('This should be eight:', addon.add(3, 5));
```
### Callbacks[#](#addons_callbacks)
It is common practice within Addons to pass JavaScript functions to a C++
f... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
8401b934-24e7-5d42-8908-fc25841a3d24 | docs | nodejs | nodejs_addons | 8x | 15 | NODE_SET_METHOD(module, "exports", RunCallback);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} // namespace demo
```
Note that this example uses a two-argument form of `Init()` that receives
the full `module` object as the second argument. This allows the Addon
to completely overwrite `exports` with a single functi... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
001e31d0-a5de-529f-a0cf-830f2ec574c5 | docs | nodejs | nodejs_addons | 8x | 16 | using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
args.GetReturnValue().Set(obj)... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
600b7259-3935-5601-b1da-e7d9946e900f | docs | nodejs | nodejs_addons | 8x | 17 | using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void MyFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
}
void CreateFunction(const ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
04e81409-553d-58d9-bf66-8357124cd3de | docs | nodejs | nodejs_addons | 8x | 18 | ### Wrapping C++ objects[#](#addons_wrapping_c_objects)
It is also possible to wrap C++ objects/classes in a way that allows new
instances to be created using the JavaScript `new` operator:
```None
// addon.cc
#include <node.h>
#include "myobject.h"
namespace demo {
using v8::Local;
using v8::Object;
void InitAll(... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
6d0b968e-d590-5020-9fdd-52e98195b6eb | docs | nodejs | nodejs_addons | 8x | 19 | double value_;
};
} // namespace demo
#endif
```
In `myobject.cc`, implement the various methods that are to be exposed.
Below, the method `plusOne()` is exposed by adding it to the constructor's
prototype:
```None
// myobject.cc
#include "myobject.h"
namespace demo {
using v8::Context;
using v8::Function;
usi... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
6b0e118f-e262-586b-ad0f-629c0eaa0666 | docs | nodejs | nodejs_addons | 8x | 20 | constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "MyObject"),
tpl->GetFunction());
}
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.IsConstructCall()) {
// Invoked as constructor: `new MyO... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
7b4042e5-df2c-521f-9895-07b8008475be | docs | nodejs | nodejs_addons | 8x | 21 | Isolate* isolate = args.GetIsolate();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
obj->value_ += 1;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}
} // namespace demo
```
To build this example, the `myobject.cc` file must be added to the
`binding.gyp`:
```None
{
"target... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
b94ed868-250f-59e3-91d1-509166461276 | docs | nodejs | nodejs_addons | 8x | 22 | First, the `createObject()` method is implemented in `addon.cc`:
```None
// addon.cc
#include <node.h>
#include "myobject.h"
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
c0330893-4489-5575-9571-bf3015398379 | docs | nodejs | nodejs_addons | 8x | 23 | static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
static v8::Persistent<v8::Function> ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
abe3767b-6561-5ed5-8941-4ed7f8ce9e15 | docs | nodejs | nodejs_addons | 8x | 24 | Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
constructor.Reset(isolate, tpl->GetFunction());
}
void MyObje... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
82467c8b-041b-5a85-98c4-296b340d8fd1 | docs | nodejs | nodejs_addons | 8x | 25 | cons->NewInstance(context, argc, argv).ToLocalChecked();
args.GetReturnValue().Set(instance);
}
}
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const unsigned argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Loc... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
2876ca6d-98e8-579d-a558-1965c69f0c5b | docs | nodejs | nodejs_addons | 8x | 26 | "addon.cc",
"myobject.cc"
]
}
]
}
```
Test it with:
```None
// test.js
const createObject = require('./build/Release/addon');
const obj = createObject(10);
console.log(obj.plusOne());
// Prints: 11
console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13
const ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
08587953-009e-5e34-81c7-58d3109a15eb | docs | nodejs | nodejs_addons | 8x | 27 | using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
MyObject::NewInstance(args);
}
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
b6a05dae-385a-5687-94f9-60af30bb2530 | docs | nodejs | nodejs_addons | 8x | 28 | class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Isolate* isolate);
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
inline double value() const { return value_; }
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::Functio... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
cf369424-a786-5a2f-a568-333e1cc08e78 | docs | nodejs | nodejs_addons | 8x | 29 | Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
constructor.Reset(isolate, tpl->GetFunction());
}
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate ... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
480dbfaf-da41-522b-9cbd-153e9d1f6dd4 | docs | nodejs | nodejs_addons | 8x | 30 | args.GetReturnValue().Set(instance);
}
}
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const unsigned argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Context> context... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
88467cdb-ba05-5ceb-94f8-926bc3b63aa9 | docs | nodejs | nodejs_addons | 8x | 31 | #### void AtExit(callback, args)[#](#addons_void_atexit_callback_args)
* `callback` <void (\*)(void\*)> A pointer to the function to call at exit.
* `args` <void\*> A pointer to pass to the callback at exit.
Registers exit hooks that run after the event loop has ended but before the VM
is killed.
AtExit takes two pa... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
8a0989d6-4b9e-525f-a53a-ef7ee12dfcda | docs | nodejs | nodejs_addons | 8x | 32 | assert(!obj.IsEmpty()); // assert VM is still alive
assert(obj->IsObject());
at_exit_cb1_called++;
}
static void at_exit_cb2(void* arg) {
assert(arg == static_cast<void*>(cookie));
at_exit_cb2_called++;
}
static void sanity_check(void*) {
assert(at_exit_cb1_called == 1);
assert(at_exit_cb2_called == 2)... | large_dataset/raw/docs/nodejs/addons/addons_8x.md |
b975a691-f03e-5451-be07-d29a7162e54f | docs | nodejs | nodejs_addons | 9x | 0 | # C++ Addons#
Source: https://nodejs.org/docs/latest-v9.x/api/addons.html
Version: 9.x
Node.js Addons are dynamically-linked shared objects, written in C++, that
can be loaded into Node.js using the [`require()`](modules.html#modules_require) function, and used
just as if they were an ordinary Node.js module. They ar... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
69a7c1d7-f40a-5cba-9013-489adb42ef66 | docs | nodejs | nodejs_addons | 9x | 1 | threads and all of the asynchronous behaviors of the platform. It also
serves as a cross-platform abstraction library, giving easy, POSIX-like
access across all major operating systems to many common system tasks, such
as interacting with the filesystem, sockets, timers, and system events. libuv
also provides... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
712d0b3f-52e6-5721-9452-e530a3787a9d | docs | nodejs | nodejs_addons | 9x | 2 | OpenSSL. These other libraries are located in the `deps/` directory in the
Node.js source tree. Only the libuv, OpenSSL, V8 and zlib symbols are
purposefully re-exported by Node.js and may be used to various extents by
Addons.
See [Linking to Node.js' own dependencies](#addons_linking_to_node_js_own_dependenc... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
e7001c9b-f82b-513d-9210-0bd8b1d7daa4 | docs | nodejs | nodejs_addons | 9x | 3 | Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, init)
} // namespace demo
```
Note that all Node.js Addons must export an initialization ... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
2fc1f5d7-c124-5fa5-96e4-040183de1eb3 | docs | nodejs | nodejs_addons | 9x | 4 | using a JSON-like format. This file is used by [node-gyp](https://github.com/nodejs/node-gyp) — a tool written
specifically to compile Node.js Addons.
```None
{
"targets": [
{
"target_name": "addon",
"sources": [ "hello.cc" ]
}
]
}
```
A version of the `node-gyp` utility is bundled and distrib... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
9badce35-69cb-52fd-a602-fc273b6e991c | docs | nodejs | nodejs_addons | 9x | 5 | (on Windows) in the `build/` directory.
Next, invoke the `node-gyp build` command to generate the compiled `addon.node`
file. This will be put into the `build/Release/` directory.
When using `npm install` to install a Node.js Addon, npm uses its own bundled
version of `node-gyp` to perform this same set of actions, g... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
fd24faf6-1dcf-5376-bd04-abf1f276d0df | docs | nodejs | nodejs_addons | 9x | 6 | the [bindings](https://github.com/TooTallNate/node-bindings) package to load the compiled module.
Note that while the `bindings` package implementation is more sophisticated
in how it locates Addon modules, it is essentially using a try-catch pattern
similar to:
```None
try {
return require('./build/Release/addon.n... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
61b1f954-66d3-52d8-9503-415c828906af | docs | nodejs | nodejs_addons | 9x | 7 | and download either the full source tarball or just the headers. If the full
source is downloaded, Addons will have complete access to the full set of
Node.js dependencies. However, if only the Node.js headers are downloaded, then
only the symbols exported by Node.js will be available.
* `node-gyp` can be run u... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
59bb0aca-ce07-50a9-9101-54dfe2dfa8f8 | docs | nodejs | nodejs_addons | 9x | 8 | however, is that Node.js will first attempt to locate and load modules or
JavaScript files that happen to share the same base name. For instance, if
there is a file `addon.js` in the same directory as the binary `addon.node`,
then [`require('addon')`](modules.html#modules_require) will give precedence to the `addon.js`... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
31dc062f-d53f-5aa7-bbdc-3360581287ad | docs | nodejs | nodejs_addons | 9x | 9 | The [Native Abstractions for Node.js](https://github.com/nodejs/nan) (or `nan`) provide a set of tools that
Addon developers are recommended to use to keep compatibility between past and
future releases of V8 and Node.js. See the `nan` [examples](https://github.com/nodejs/nan/tree/master/examples/) for an
illustration ... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
0e4bd1ee-8b80-5455-af12-8fd4b2f3b2f7 | docs | nodejs | nodejs_addons | 9x | 10 | set of APIs that are used by the native code. Instead of using the V8
or [Native Abstractions for Node.js](https://github.com/nodejs/nan) APIs, the functions available
in the N-API are used.
To use N-API in the above "Hello world" example, replace the content of
`hello.cc` with the following. All other instructions re... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
a3687b5f-3d23-5a79-b477-89136954d75e | docs | nodejs | nodejs_addons | 9x | 11 | NAPI_MODULE(NODE_GYP_MODULE_NAME, init)
} // namespace demo
```
The functions available and how to use them are documented in the
section titled [C/C++ Addons - N-API](n-api.html).
## Addon examples[#](#addons_addon_examples)
Following are some example Addons intended to help developers get started. The
examples m... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
10cb88f3-6de4-50c5-8fda-cbc99f3debb2 | docs | nodejs | nodejs_addons | 9x | 12 | Once the `binding.gyp` file is ready, the example Addons can be configured and
built using `node-gyp`:
```None
$ node-gyp configure build
```
### Function arguments[#](#addons_function_arguments)
Addons will typically expose objects and functions that can be accessed from
JavaScript running within Node.js. When func... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
e4fbd1ae-c9c3-5605-9fd6-fa458a5eb169 | docs | nodejs | nodejs_addons | 9x | 13 | // Check the number of arguments passed.
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
// Check the argument types
if (!args[0]->IsNumber() |... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
aedd0ce5-4bc6-5ea3-9a87-e3e272c73e8e | docs | nodejs | nodejs_addons | 9x | 14 | const addon = require('./build/Release/addon');
console.log('This should be eight:', addon.add(3, 5));
```
### Callbacks[#](#addons_callbacks)
It is common practice within Addons to pass JavaScript functions to a C++
function and execute them from there. The following example illustrates how
to invoke such callbacks... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
a7adb04c-056b-55ed-9b8d-e8fcfa701d74 | docs | nodejs | nodejs_addons | 9x | 15 | Note that this example uses a two-argument form of `Init()` that receives
the full `module` object as the second argument. This allows the Addon
to completely overwrite `exports` with a single function instead of
adding the function as a property of `exports`.
To test it, run the following JavaScript:
```None
// test... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
2bde75c6-4975-50e9-834e-f56103dc1f19 | docs | nodejs | nodejs_addons | 9x | 16 | Isolate* isolate = args.GetIsolate();
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "msg"), args[0]->ToString());
args.GetReturnValue().Set(obj);
}
void Init(Local<Object> exports, Local<Object> module) {
NODE_SET_METHOD(module, "exports", CreateObject);
}
NODE_MODULE(NOD... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
4e709579-477e-5e0a-8f84-f882475a948f | docs | nodejs | nodejs_addons | 9x | 17 | void MyFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
}
void CreateFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<FunctionTemplate> tpl = Function... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
0acced78-6e28-5584-88e3-b3ad0693a2e2 | docs | nodejs | nodejs_addons | 9x | 18 | ### Wrapping C++ objects[#](#addons_wrapping_c_objects)
It is also possible to wrap C++ objects/classes in a way that allows new
instances to be created using the JavaScript `new` operator:
```None
// addon.cc
#include <node.h>
#include "myobject.h"
namespace demo {
using v8::Local;
using v8::Object;
void InitAll(... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
e23be2d0-31dc-5e60-8ae8-1b3e69db049a | docs | nodejs | nodejs_addons | 9x | 19 | double value_;
};
} // namespace demo
#endif
```
In `myobject.cc`, implement the various methods that are to be exposed.
Below, the method `plusOne()` is exposed by adding it to the constructor's
prototype:
```None
// myobject.cc
#include "myobject.h"
namespace demo {
using v8::Context;
using v8::Function;
usi... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
ff8978a6-e217-5aa7-9361-980c98cf7965 | docs | nodejs | nodejs_addons | 9x | 20 | constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "MyObject"),
tpl->GetFunction());
}
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.IsConstructCall()) {
// Invoked as constructor: `new MyO... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
6ac84c0a-ea7b-51f4-8822-b829a6cc224f | docs | nodejs | nodejs_addons | 9x | 21 | Isolate* isolate = args.GetIsolate();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
obj->value_ += 1;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
}
} // namespace demo
```
To build this example, the `myobject.cc` file must be added to the
`binding.gyp`:
```None
{
"target... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
0b5d8ae0-d87b-517d-8302-107f86db9a2b | docs | nodejs | nodejs_addons | 9x | 22 | First, the `createObject()` method is implemented in `addon.cc`:
```None
// addon.cc
#include <node.h>
#include "myobject.h"
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& ... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
101f94d8-1220-5d60-9505-386245a8cba0 | docs | nodejs | nodejs_addons | 9x | 23 | static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void PlusOne(const v8::FunctionCallbackInfo<v8::Value>& args);
static v8::Persistent<v8::Function> ... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
d81d8e5d-1c15-5a47-b933-2ba4f93e7200 | docs | nodejs | nodejs_addons | 9x | 24 | Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
constructor.Reset(isolate, tpl->GetFunction());
}
void MyObje... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
cb828be0-f537-58c4-83fd-f67444f35264 | docs | nodejs | nodejs_addons | 9x | 25 | cons->NewInstance(context, argc, argv).ToLocalChecked();
args.GetReturnValue().Set(instance);
}
}
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const unsigned argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Loc... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
3b75dba9-14ec-50c9-a971-c5062d1bf9fd | docs | nodejs | nodejs_addons | 9x | 26 | "addon.cc",
"myobject.cc"
]
}
]
}
```
Test it with:
```None
// test.js
const createObject = require('./build/Release/addon');
const obj = createObject(10);
console.log(obj.plusOne());
// Prints: 11
console.log(obj.plusOne());
// Prints: 12
console.log(obj.plusOne());
// Prints: 13
const ... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
0fdb6bb1-6ab8-501b-964f-85cebf63b1ec | docs | nodejs | nodejs_addons | 9x | 27 | using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
void CreateObject(const FunctionCallbackInfo<Value>& args) {
MyObject::NewInstance(args);
}
void Add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
MyObject* obj1 = node::ObjectWrap::Unwrap<MyObject>(
... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
230f6084-0fbb-517b-b3e7-fd3c6b3a157d | docs | nodejs | nodejs_addons | 9x | 28 | class MyObject : public node::ObjectWrap {
public:
static void Init(v8::Isolate* isolate);
static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
inline double value() const { return value_; }
private:
explicit MyObject(double value = 0);
~MyObject();
static void New(const v8::Functio... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
18aa3719-4b45-500e-9416-0059c329f611 | docs | nodejs | nodejs_addons | 9x | 29 | Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
constructor.Reset(isolate, tpl->GetFunction());
}
void MyObject::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate ... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
1597de82-b242-5f2d-b2fe-6a7da54adbaf | docs | nodejs | nodejs_addons | 9x | 30 | args.GetReturnValue().Set(instance);
}
}
void MyObject::NewInstance(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
const unsigned argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Context> context... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
d7aed417-5619-5eb6-8e6f-6be1a86dfddd | docs | nodejs | nodejs_addons | 9x | 31 | #### void AtExit(callback, args)[#](#addons_void_atexit_callback_args)
* `callback` <void (\*)(void\*)> A pointer to the function to call at exit.
* `args` <void\*> A pointer to pass to the callback at exit.
Registers exit hooks that run after the event loop has ended but before the VM
is killed.
AtExit takes two pa... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
49ac6a70-acba-5f2a-82c4-4bb59c0e76fd | docs | nodejs | nodejs_addons | 9x | 32 | assert(!obj.IsEmpty()); // assert VM is still alive
assert(obj->IsObject());
at_exit_cb1_called++;
}
static void at_exit_cb2(void* arg) {
assert(arg == static_cast<void*>(cookie));
at_exit_cb2_called++;
}
static void sanity_check(void*) {
assert(at_exit_cb1_called == 1);
assert(at_exit_cb2_called == 2)... | large_dataset/raw/docs/nodejs/addons/addons_9x.md |
00556060-c217-5bfe-8dea-d14228e4ac26 | docs | nodejs | nodejs_async_hooks | 10x | 0 | # Async Hooks#
Source: https://nodejs.org/docs/latest-v10.x/api/async_hooks.html
Version: 10.x
[Stability: 1](documentation.html#documentation_stability_index) - Experimental
The `async_hooks` module provides an API to register callbacks tracking the
lifetime of asynchronous resources created inside a Node.js applic... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
0c3ef9e9-a7e1-5db0-bb32-7ce715777364 | docs | nodejs | nodejs_async_hooks | 10x | 1 | interface, and each thread will use a new set of async IDs.
## Public API[#](#async_hooks_public_api)
### Overview[#](#async_hooks_overview)
Following is a simple overview of the public API.
```None
const async_hooks = require('async_hooks');
// Return the ID of the current execution context.
const eid = async_hoo... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
6a91736e-682d-5a3b-8eaf-829e887e64c7 | docs | nodejs | nodejs_async_hooks | 10x | 2 | // init is called during object construction. The resource may not have
// completed construction when this callback runs, therefore all fields of the
// resource referenced by "asyncId" may not have been populated.
function init(asyncId, type, triggerAsyncId, resource) { }
// before is called just before the resource... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
b8265ea8-8112-5c27-a101-6628986eb3e5 | docs | nodejs | nodejs_async_hooks | 10x | 3 | #### async\_hooks.createHook(callbacks)[[src]](https://github.com/nodejs/node/blob/5182a7ece0b71feeb9157f7aa348a15d53e32058/lib/async_hooks.js#L121)[#](#async_hooks_async_hooks_createhook_callbacks)
Added in: v8.1.0
* `callbacks` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Obje... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
214ce39d-5ba1-54a4-a575-b9a5db65e5c2 | docs | nodejs | nodejs_async_hooks | 10x | 4 | + `destroy` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) The [`destroy` callback](#async_hooks_destroy_asyncid).
* Returns: [<AsyncHook>](async_hooks.html#async_hooks_async_hooks_createhook_callbacks) Instance used for disabling and enabling hooks
Registers ... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
99eb9f8e-0ae6-5974-9d1b-8f03ad26d965 | docs | nodejs | nodejs_async_hooks | 10x | 5 | Note that the callbacks will be inherited via the prototype chain:
```None
class MyAsyncCallbacks {
init(asyncId, type, triggerAsyncId, resource) { }
destroy(asyncId) {}
}
class MyAddedCallbacks extends MyAsyncCallbacks {
before(asyncId) { }
after(asyncId) { }
}
const asyncHook = async_hooks.createHook(new M... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
a1228899-b56d-5a4c-bbb0-ac63ee98e2db | docs | nodejs | nodejs_async_hooks | 10x | 6 | class construction and destruction. Because of this, it is deemed necessary to
bring down the process quickly in order to prevent an unintentional abort in the
future. This is subject to change in the future if a comprehensive analysis is
performed to ensure an exception can follow the normal control flow without
unint... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
fcaab386-141b-5bd1-b1a6-94b672f66b85 | docs | nodejs | nodejs_async_hooks | 10x | 7 | // use a function like this one when debugging inside an AsyncHooks callback
fs.writeFileSync('log.out', `${util.format(...args)}\n`, { flag: 'a' });
}
```
If an asynchronous operation is needed for logging, it is possible to keep
track of what caused the asynchronous operation using the information
provided by As... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
a712ef62-09fa-597e-b3fd-24be9c0a1015 | docs | nodejs | nodejs_async_hooks | 10x | 8 | const hook = async_hooks.createHook(callbacks).enable();
```
#### asyncHook.disable()[#](#async_hooks_asynchook_disable)
* Returns: [<AsyncHook>](async_hooks.html#async_hooks_async_hooks_createhook_callbacks) A reference to `asyncHook`.
Disable the callbacks for a given `AsyncHook` instance from the global pool of
`... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
e4bbcdd9-cd87-5d47-8aca-351bf542e615 | docs | nodejs | nodejs_async_hooks | 10x | 9 | * `type` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The type of the async resource.
* `triggerAsyncId` [<number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) The unique ID of the async resource in whose
execution context thi... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
305b4507-bf83-50ea-bd95-fa442e1add20 | docs | nodejs | nodejs_async_hooks | 10x | 10 | // OR
clearTimeout(setTimeout(() => {}, 10));
```
Every new resource is assigned an ID that is unique within the scope of the
current Node.js instance.
###### `type`[#](#async_hooks_type)
The `type` is a string identifying the type of resource that caused
`init` to be called. Generally, it will correspond to the nam... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
b29d327c-be05-5603-a2be-0be5b76910a7 | docs | nodejs | nodejs_async_hooks | 10x | 11 | unique prefixes, such as the npm package name, to prevent collisions when
listening to the hooks.
###### `triggerAsyncId`[#](#async_hooks_triggerasyncid)
`triggerAsyncId` is the `asyncId` of the resource that caused (or "triggered")
the new resource to initialize and that caused `init` to call. This is different
from... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
3c7404dd-13d9-533d-a257-59e6f0297df9 | docs | nodejs | nodejs_async_hooks | 10x | 12 | The `TCPSERVERWRAP` is the server which receives the connections.
The `TCPWRAP` is the new connection from the client. When a new
connection is made, the `TCPWrap` instance is immediately constructed. This
happens outside of any JavaScript stack. (An `executionAsyncId()` of `0` means
that it is being executed from C++... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
3c60b59e-46ff-5b52-896d-d39956275cc2 | docs | nodejs | nodejs_async_hooks | 10x | 13 | currently not considered public, but using the Embedder API, users can provide
and document their own resource objects. For example, such a resource object
could contain the SQL query being executed.
In the case of Promises, the `resource` object will have `promise` property
that refers to the `Promise` that is being ... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
fe863453-cba4-56d1-8a96-c3e28b7dd6a6 | docs | nodejs | nodejs_async_hooks | 10x | 14 | callback to `listen()` will look like. The output formatting is slightly more
elaborate to make calling context easier to see.
```None
let indent = 0;
async_hooks.createHook({
init(asyncId, type, triggerAsyncId) {
const eid = async_hooks.executionAsyncId();
const indentStr = ' '.repeat(indent);
fs.writeS... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
b727c790-ba48-5b08-b2d1-73ff7cd8b625 | docs | nodejs | nodejs_async_hooks | 10x | 15 | require('net').createServer(() => {}).listen(8080, () => {
// Let's wait 10ms before logging the server started.
setTimeout(() => {
console.log('>>>', async_hooks.executionAsyncId());
}, 10);
});
```
Output from only starting the server:
```None
TCPSERVERWRAP(5): trigger: 1 execution: 1
TickObject(6): trigg... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
48bba41e-ad2b-5262-a667-e9a9d7894764 | docs | nodejs | nodejs_async_hooks | 10x | 16 | hostname is a *synchronous* operation, but to maintain a completely asynchronous
API the user's callback is placed in a `process.nextTick()`.
The graph only shows *when* a resource was created, not *why*, so to track
the *why* use `triggerAsyncId`.
##### before(asyncId)[#](#async_hooks_before_asyncid)
* `asyncId` [<... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
952176cb-523c-52e6-9518-96512520aae8 | docs | nodejs | nodejs_async_hooks | 10x | 17 | asynchronous resources like a TCP server will typically call the `before`
callback multiple times, while other operations like `fs.open()` will call
it only once.
##### after(asyncId)[#](#async_hooks_after_asyncid)
* `asyncId` [<number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_t... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
8524572e-2797-5d16-952e-369109073fbf | docs | nodejs | nodejs_async_hooks | 10x | 18 | made to the `resource` object passed to `init` it is possible that `destroy`
will never be called, causing a memory leak in the application. If the resource
does not depend on garbage collection, then this will not be an issue.
##### promiseResolve(asyncId)[#](#async_hooks_promiseresolve_asyncid)
* `asyncId` [<number... | large_dataset/raw/docs/nodejs/async_hooks/async_hooks_10x.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.