node-addon-api 1.7.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.travis.yml CHANGED
@@ -12,20 +12,17 @@ env:
12
12
  # https://github.com/jasongin/nvs/blob/master/doc/CI.md
13
13
  - NVS_VERSION=1.4.2
14
14
  matrix:
15
- - NODEJS_VERSION=node/4
16
15
  - NODEJS_VERSION=node/6
17
16
  - NODEJS_VERSION=node/8
18
- - NODEJS_VERSION=node/9
19
17
  - NODEJS_VERSION=node/10
20
- - NODEJS_VERSION=chakracore/8
21
- - NODEJS_VERSION=chakracore/10
18
+ - NODEJS_VERSION=node/12
19
+ - NODEJS_VERSION=node/13
22
20
  - NODEJS_VERSION=nightly
23
- - NODEJS_VERSION=chakracore-nightly
24
21
  matrix:
25
22
  fast_finish: true
26
23
  allow_failures:
27
24
  - env: NODEJS_VERSION=nightly
28
- - env: NODEJS_VERSION=chakracore-nightly
25
+ - env: NODEJS_VERSION=node/6
29
26
  sudo: false
30
27
  cache:
31
28
  directories:
@@ -60,6 +57,6 @@ script:
60
57
  # Travis CI sets NVM_NODEJS_ORG_MIRROR, but it makes node-gyp fail to download headers for nightly builds.
61
58
  - unset NVM_NODEJS_ORG_MIRROR
62
59
 
63
- - npm test $NPMOPT
60
+ - npm test
64
61
  after_success:
65
62
  - cpp-coveralls --gcov-options '\-lp' --build-root test/build --exclude test
package/CHANGELOG.md CHANGED
@@ -1,5 +1,104 @@
1
1
  # node-addon-api Changelog
2
2
 
3
+ ## 2020-07-01 Version 2.0.2, @NickNaso
4
+
5
+ ### Notable changes:
6
+
7
+ #### API
8
+
9
+ - `Napi::ObjectWrap`: avoid double-free on old Node.js.
10
+ - `Napi::ObjectWrap`: remove wrap only on failure.
11
+ - `Napi::ObjectWrap`: gracefully handle constructor exceptions
12
+ - `Napi::ObjectWrap`: call `napi_remove_wrap()` in destructor.
13
+
14
+ #### TEST
15
+
16
+ - Updated `Napi::BigInt` test for recent change in core.
17
+
18
+ ### Commmits
19
+
20
+ * [[`5abf60257d`](https://github.com/nodejs/node-addon-api/commit/5abf60257d)] - Merge pull request #723 from gabrielschulhof/backport-4e885069-pr-475 (Nicola Del Gobbo)
21
+ * [[`470f130666`](https://github.com/nodejs/node-addon-api/commit/470f130666)] - **objectwrap**: avoid double-free on old Node.js (Gabriel Schulhof)
22
+ * [[`81e2eac7ba`](https://github.com/nodejs/node-addon-api/commit/81e2eac7ba)] - **test**: update BigInt test for recent change in core (Michael Dawson) [#649](https://github.com/nodejs/node-addon-api/pull/649)
23
+ * [[`204f07252c`](https://github.com/nodejs/node-addon-api/commit/204f07252c)] - **objectwrap**: remove wrap only on failure (Gabriel Schulhof)
24
+ * [[`a552a384dd`](https://github.com/nodejs/node-addon-api/commit/a552a384dd)] - **src**: call `napi\_remove\_wrap()` in `ObjectWrap` dtor (Anna Henningsen) [#475](https://github.com/nodejs/node-addon-api/pull/475)
25
+ * [[`1a51067438`](https://github.com/nodejs/node-addon-api/commit/1a51067438)] - **objectwrap**: gracefully handle constructor exceptions (Gabriel Schulhof)
26
+
27
+ ## 2020-06-02 Version 2.0.1, @NickNaso
28
+
29
+ ### Notable changes:
30
+
31
+ #### API
32
+
33
+ - Fix memory corruption vulnerability
34
+
35
+ ### Commmits
36
+
37
+ * [[`265fea9edd`](https://github.com/nodejs/node-addon-api/commit/265fea9edd)] - **napi**: fix memory corruption vulnerability (Tobias Nießen )
38
+
39
+ ## 2019-11-21 Version 2.0.0, @NickNaso
40
+
41
+ ### Notable changes:
42
+
43
+ #### API
44
+
45
+ - Added `Napi::AsyncProgressWorker` api.
46
+ - Added error checking on `Napi::ThreadSafeFunction::GetContext`.
47
+ - Added copy constructor to `Napi::ThreadSafeFunction`.
48
+ - Added `Napi::ThreadSafeFunction::Ref` and `Napi::ThreadSafeFunction::Unref` to `Napi::ThreadSafeFunction`.
49
+ - Added `Napi::Object::AddFinalizer` method.
50
+ - Use `napi_add_finalizer()` to attach data when building against N-API 5.
51
+ - Added `Napi::Date` api.
52
+ - Added `Napi::ObjectWrap::Finalize` method.
53
+
54
+ #### Documentation
55
+
56
+ - Added documentation for `Napi::AsyncProgressWorker`.
57
+ - Improve `Napi::AsyncWorker` documentation.
58
+ - Added documentation for `Napi::Object::AddFinalizer` method.
59
+ - Improved documentation for `Napi::ThreadSafeFunction`.
60
+ - Improved documentation about the usage of CMake as build tool.
61
+ - Some minor corrections all over the documentation.
62
+
63
+ #### TEST
64
+
65
+ - Added test cases for `Napi::AsyncProgressWorker` api.
66
+ - Added test cases for `Napi::Date` api.
67
+ - Added test cases for new features added to `Napi::ThreadSafeFunction`.
68
+
69
+ ### Commmits
70
+
71
+ * [[`c881168d49`](https://github.com/nodejs/node-addon-api/commit/c881168d49)] - **tsfn**: add error checking on GetContext (#583) (Kevin Eady) [#583](https://github.com/nodejs/node-addon-api/pull/583)
72
+ * [[`24d75dd82f`](https://github.com/nodejs/node-addon-api/commit/24d75dd82f)] - Merge pull request #588 from NickNaso/add-asyncprogress-worker-readme (Nicola Del Gobbo)
73
+ * [[`aa79e37b62`](https://github.com/nodejs/node-addon-api/commit/aa79e37b62)] - Merge pull request #587 from timrach/patch-1 (Nicola Del Gobbo)
74
+ * [[`df75e08c2b`](https://github.com/nodejs/node-addon-api/commit/df75e08c2b)] - **tsfn**: support direct calls to underlying napi\_tsfn (Kevin Eady) [#58](https://github.com/nodejs/node-addon-api/pull/58)
75
+ * [[`2298dfae58`](https://github.com/nodejs/node-addon-api/commit/2298dfae58)] - **doc**: Added AsyncProgressWorker to readme (NickNaso)
76
+ * [[`b3609d33b6`](https://github.com/nodejs/node-addon-api/commit/b3609d33b6)] - Fix return type and declaration of setter callback (Tim Rach)
77
+ * [[`295e560f55`](https://github.com/nodejs/node-addon-api/commit/295e560f55)] - **test**: improve guards for experimental features (legendecas)
78
+ * [[`2e71842f63`](https://github.com/nodejs/node-addon-api/commit/2e71842f63)] - **tsfn**: Implement copy constructor (Kevin Eady) [#546](https://github.com/nodejs/node-addon-api/pull/546)
79
+ * [[`650562cab9`](https://github.com/nodejs/node-addon-api/commit/650562cab9)] - **src**: implement AsyncProgressWorker (legendecas) [#529](https://github.com/nodejs/node-addon-api/pull/529)
80
+ * [[`bdfd14101f`](https://github.com/nodejs/node-addon-api/commit/bdfd14101f)] - **src**: attach data with napi\_add\_finalizer (Gabriel Schulhof) [#577](https://github.com/nodejs/node-addon-api/pull/577)
81
+ * [[`9e955a802b`](https://github.com/nodejs/node-addon-api/commit/9e955a802b)] - **doc**: change node.js to Node.js per guideline (#579) (Tobias Nießen) [#579](https://github.com/nodejs/node-addon-api/pull/579)
82
+ * [[`b42e21e3a9`](https://github.com/nodejs/node-addon-api/commit/b42e21e3a9)] - **build**: move node/6 to travis allowed failures and add node/13 (#573) (Gabriel Schulhof)
83
+ * [[`8d6132f609`](https://github.com/nodejs/node-addon-api/commit/8d6132f609)] - **doc**: improve AsyncWorker docs (#571) (legendecas) [#571](https://github.com/nodejs/node-addon-api/pull/571)
84
+ * [[`bc8fc23627`](https://github.com/nodejs/node-addon-api/commit/bc8fc23627)] - **test**: do not run TSFN tests on NAPI\_VERSION \< 4 (legendecas) [#576](https://github.com/nodejs/node-addon-api/pull/576)
85
+ * [[`bcc1d58fc4`](https://github.com/nodejs/node-addon-api/commit/bcc1d58fc4)] - implement Object::AddFinalizer (Gabriel Schulhof)
86
+ * [[`e9a4bcd52a`](https://github.com/nodejs/node-addon-api/commit/e9a4bcd52a)] - **doc**: updates Make.js doc to current best practices (Jim Schlight) [#558](https://github.com/nodejs/node-addon-api/pull/558)
87
+ * [[`b513d1aa7a`](https://github.com/nodejs/node-addon-api/commit/b513d1aa7a)] - **doc**: fix return type of ArrayBuffer::Data (Tobias Nießen) [#552](https://github.com/nodejs/node-addon-api/pull/552)
88
+ * [[`34c11cf0a4`](https://github.com/nodejs/node-addon-api/commit/34c11cf0a4)] - **src**: disallow copying, double close of scopes (legendecas) [#566](https://github.com/nodejs/node-addon-api/pull/566)
89
+ * [[`ce139a05e8`](https://github.com/nodejs/node-addon-api/commit/ce139a05e8)] - **src**: make failure of closing scopes fatal (legendecas) [#566](https://github.com/nodejs/node-addon-api/pull/566)
90
+ * [[`740c79823e`](https://github.com/nodejs/node-addon-api/commit/740c79823e)] - **src**: add Env() to AsyncContext (Rolf Timmermans) [#568](https://github.com/nodejs/node-addon-api/pull/568)
91
+ * [[`ea9ce1c801`](https://github.com/nodejs/node-addon-api/commit/ea9ce1c801)] - **tsfn**: add wrappers for Ref and Unref (Kevin Eady) [#561](https://github.com/nodejs/node-addon-api/pull/561)
92
+ * [[`2e1769e1a3`](https://github.com/nodejs/node-addon-api/commit/2e1769e1a3)] - **error**: remove unnecessary if condition (legendecas) [#562](https://github.com/nodejs/node-addon-api/pull/562)
93
+ * [[`828f223a87`](https://github.com/nodejs/node-addon-api/commit/828f223a87)] - **doc**: fix spelling in ObjectWrap doc (#563) (Tobias Nießen) [#563](https://github.com/nodejs/node-addon-api/pull/563)
94
+ * [[`dd9fa8a4a8`](https://github.com/nodejs/node-addon-api/commit/dd9fa8a4a8)] - **doc**: move Arunesh and Taylor to Emeritus (#540) (Michael Dawson) [#540](https://github.com/nodejs/node-addon-api/pull/540)
95
+ * [[`cf8b8415df`](https://github.com/nodejs/node-addon-api/commit/cf8b8415df)] - **doc**: add Kevin to the list of collaborators (#539) (Michael Dawson) [#539](https://github.com/nodejs/node-addon-api/pull/539)
96
+ * [[`5d6aeae7b5`](https://github.com/nodejs/node-addon-api/commit/5d6aeae7b5)] - **build**: enable travis for fast PR check (legendecas)
97
+ * [[`6192e705cd`](https://github.com/nodejs/node-addon-api/commit/6192e705cd)] - **src**: add napi\_date (Mathias Küsel) [#497](https://github.com/nodejs/node-addon-api/pull/497)
98
+ * [[`7b1ee96d52`](https://github.com/nodejs/node-addon-api/commit/7b1ee96d52)] - **doc**: update prebuild\_tools.md (Nurbol Alpysbayev) [#527](https://github.com/nodejs/node-addon-api/pull/527)
99
+ * [[`0b4f3a5b8c`](https://github.com/nodejs/node-addon-api/commit/0b4f3a5b8c)] - **tsfn**: fix crash on releasing tsfn (legendecas) [#532](https://github.com/nodejs/node-addon-api/pull/532)
100
+ * [[`c3c8814d2f`](https://github.com/nodejs/node-addon-api/commit/c3c8814d2f)] - implement virutal ObjectWrap::Finalize (Michael Price) [#515](https://github.com/nodejs/node-addon-api/pull/515)
101
+
3
102
  ## 2019-07-23 Version 1.7.1, @NickNaso
4
103
 
5
104
  ### Notable changes:
package/README.md CHANGED
@@ -46,7 +46,7 @@ to ideas specified in the **ECMA262 Language Specification**.
46
46
  - **[Contributors](#contributors)**
47
47
  - **[License](#license)**
48
48
 
49
- ## **Current version: 1.7.1**
49
+ ## **Current version: 2.0.2**
50
50
 
51
51
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
52
52
 
@@ -75,6 +75,7 @@ The following is the documentation for node-addon-api.
75
75
  - [String](doc/string.md)
76
76
  - [Name](doc/basic_types.md#name)
77
77
  - [Number](doc/number.md)
78
+ - [Date](doc/date.md)
78
79
  - [BigInt](doc/bigint.md)
79
80
  - [Boolean](doc/boolean.md)
80
81
  - [Env](doc/env.md)
@@ -106,6 +107,7 @@ The following is the documentation for node-addon-api.
106
107
  - [Async Operations](doc/async_operations.md)
107
108
  - [AsyncWorker](doc/async_worker.md)
108
109
  - [AsyncContext](doc/async_context.md)
110
+ - [AsyncProgressWorker](doc/async_progress_worker.md)
109
111
  - [Thread-safe Functions](doc/threadsafe_function.md)
110
112
  - [Promises](doc/promises.md)
111
113
  - [Version management](doc/version_management.md)
@@ -178,20 +180,21 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for more details on our philosophy around
178
180
  | Name | GitHub Link |
179
181
  | ------------------- | ----------------------------------------------------- |
180
182
  | Anna Henningsen | [addaleax](https://github.com/addaleax) |
181
- | Arunesh Chandra | [aruneshchandra](https://github.com/aruneshchandra) |
182
183
  | Gabriel Schulhof | [gabrielschulhof](https://github.com/gabrielschulhof) |
183
184
  | Hitesh Kanwathirtha | [digitalinfinity](https://github.com/digitalinfinity) |
184
185
  | Jim Schlight | [jschlight](https://github.com/jschlight) |
185
186
  | Michael Dawson | [mhdawson](https://github.com/mhdawson) |
187
+ | Kevin Eady | [KevinEady](https://github.com/KevinEady)
186
188
  | Nicola Del Gobbo | [NickNaso](https://github.com/NickNaso) |
187
- | Taylor Woll | [boingoing](https://github.com/boingoing) |
188
189
 
189
190
  ### Emeritus
190
191
  | Name | GitHub Link |
191
192
  | ------------------- | ----------------------------------------------------- |
193
+ | Arunesh Chandra | [aruneshchandra](https://github.com/aruneshchandra) |
192
194
  | Benjamin Byholm | [kkoopa](https://github.com/kkoopa) |
193
195
  | Jason Ginchereau | [jasongin](https://github.com/jasongin) |
194
196
  | Sampson Gao | [sampsongao](https://github.com/sampsongao) |
197
+ | Taylor Woll | [boingoing](https://github.com/boingoing) |
195
198
 
196
199
  <a name="license"></a>
197
200
 
@@ -123,7 +123,7 @@ Returns the length of the wrapped data, in bytes.
123
123
  ### Data
124
124
 
125
125
  ```cpp
126
- T* Napi::ArrayBuffer::Data() const;
126
+ void* Napi::ArrayBuffer::Data() const;
127
127
  ```
128
128
 
129
129
  Returns a pointer the wrapped data.
@@ -45,6 +45,16 @@ The `Napi::AsyncContext` to be destroyed.
45
45
  virtual Napi::AsyncContext::~AsyncContext();
46
46
  ```
47
47
 
48
+ ### Env
49
+
50
+ Requests the environment in which the async context has been initially created.
51
+
52
+ ```cpp
53
+ Napi::Env Env() const;
54
+ ```
55
+
56
+ Returns the `Napi::Env` environment in which the async context has been created.
57
+
48
58
  ## Operator
49
59
 
50
60
  ```cpp
@@ -4,7 +4,7 @@ Node.js native add-ons often need to execute long running tasks and to avoid
4
4
  blocking the **event loop** they have to run them asynchronously from the
5
5
  **event loop**.
6
6
  In the Node.js model of execution the event loop thread represents the thread
7
- where JavaScript code is executing. The node.js guidance is to avoid blocking
7
+ where JavaScript code is executing. The Node.js guidance is to avoid blocking
8
8
  other work queued on the event loop thread. Therefore, we need to do this work on
9
9
  another thread.
10
10
 
@@ -0,0 +1,344 @@
1
+ # AsyncProgressWorker
2
+
3
+ `Napi::AsyncProgressWorker` is an abstract class which implements `Napi::AsyncWorker`
4
+ while extending `Napi::AsyncWorker` internally with `Napi::ThreadSafeFunction` for
5
+ moving work progress reports from worker thread(s) to event loop threads.
6
+
7
+ Like `Napi::AsyncWorker`, once created, execution is requested by calling
8
+ `Napi::AsyncProgressWorker::Queue`. When a thread is available for execution
9
+ the `Napi::AsyncProgressWorker::Execute` method will be invoked. During the
10
+ execution, `Napi::AsyncProgressWorker::ExecutionProgress::Send` can be used to
11
+ indicate execution process, which will eventually invoke `Napi::AsyncProgressWorker::OnProgress`
12
+ on the JavaScript thread to safely call into JavaScript. Once `Napi::AsyncProgressWorker::Execute`
13
+ completes either `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError`
14
+ will be invoked. Once the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError`
15
+ methods are complete the `Napi::AsyncProgressWorker` instance is destructed.
16
+
17
+ For the most basic use, only the `Napi::AsyncProgressWorker::Execute` and
18
+ `Napi::AsyncProgressWorker::OnProgress` method must be implemented in a subclass.
19
+
20
+ ## Methods
21
+
22
+ [`Napi::AsyncWorker`][] provides detailed descriptions for most methods.
23
+
24
+ ### Execute
25
+
26
+ This method is used to execute some tasks outside of the **event loop** on a libuv
27
+ worker thread. Subclasses must implement this method and the method is run on
28
+ a thread other than that running the main event loop. As the method is not
29
+ running on the main event loop, it must avoid calling any methods from node-addon-api
30
+ or running any code that might invoke JavaScript. Instead, once this method is
31
+ complete any interaction through node-addon-api with JavaScript should be implemented
32
+ in the `Napi::AsyncProgressWorker::OnOK` method and/or `Napi::AsyncProgressWorker::OnError`
33
+ which run on the main thread and are invoked when the `Napi::AsyncProgressWorker::Execute`
34
+ method completes.
35
+
36
+ ```cpp
37
+ virtual void Napi::AsyncProgressWorker::Execute(const ExecutionProgress& progress) = 0;
38
+ ```
39
+
40
+ ### OnOK
41
+
42
+ This method is invoked when the computation in the `Execute` method ends.
43
+ The default implementation runs the `Callback` optionally provided when the
44
+ `AsyncProgressWorker` class was created. The `Callback` will by default receive no
45
+ arguments. Arguments to the callback can be provided by overriding the `GetResult()`
46
+ method.
47
+
48
+ ```cpp
49
+ virtual void Napi::AsyncProgressWorker::OnOK();
50
+ ```
51
+
52
+ ### OnProgress
53
+
54
+ This method is invoked when the computation in the `Napi::AsyncProgressWorker::ExecutionProcess::Send`
55
+ method was called during worker thread execution.
56
+
57
+ ```cpp
58
+ virtual void Napi::AsyncProgressWorker::OnProgress(const T* data, size_t count)
59
+ ```
60
+
61
+ ### Constructor
62
+
63
+ Creates a new `Napi::AsyncProgressWorker`.
64
+
65
+ ```cpp
66
+ explicit Napi::AsyncProgressWorker(const Napi::Function& callback);
67
+ ```
68
+
69
+ - `[in] callback`: The function which will be called when an asynchronous
70
+ operations ends. The given function is called from the main event loop thread.
71
+
72
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
73
+ calling `Napi::AsyncWork::Queue`.
74
+
75
+ ### Constructor
76
+
77
+ Creates a new `Napi::AsyncProgressWorker`.
78
+
79
+ ```cpp
80
+ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* resource_name);
81
+ ```
82
+
83
+ - `[in] callback`: The function which will be called when an asynchronous
84
+ operations ends. The given function is called from the main event loop thread.
85
+ - `[in] resource_name`: Null-terminated string that represents the
86
+ identifier for the kind of resource that is being provided for diagnostic
87
+ information exposed by the async_hooks API.
88
+
89
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
90
+ calling `Napi::AsyncWork::Queue`.
91
+
92
+ ### Constructor
93
+
94
+ Creates a new `Napi::AsyncProgressWorker`.
95
+
96
+ ```cpp
97
+ explicit Napi::AsyncProgressWorker(const Napi::Function& callback, const char* resource_name, const Napi::Object& resource);
98
+ ```
99
+
100
+ - `[in] callback`: The function which will be called when an asynchronous
101
+ operations ends. The given function is called from the main event loop thread.
102
+ - `[in] resource_name`: Null-terminated string that represents the
103
+ identifier for the kind of resource that is being provided for diagnostic
104
+ information exposed by the async_hooks API.
105
+ - `[in] resource`: Object associated with the asynchronous operation that
106
+ will be passed to possible async_hooks.
107
+
108
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
109
+ calling `Napi::AsyncWork::Queue`.
110
+
111
+ ### Constructor
112
+
113
+ Creates a new `Napi::AsyncProgressWorker`.
114
+
115
+ ```cpp
116
+ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback);
117
+ ```
118
+
119
+ - `[in] receiver`: The `this` object passed to the called function.
120
+ - `[in] callback`: The function which will be called when an asynchronous
121
+ operations ends. The given function is called from the main event loop thread.
122
+
123
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
124
+ calling `Napi::AsyncWork::Queue`.
125
+
126
+ ### Constructor
127
+
128
+ Creates a new `Napi::AsyncProgressWorker`.
129
+
130
+ ```cpp
131
+ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name);
132
+ ```
133
+
134
+ - `[in] receiver`: The `this` object passed to the called function.
135
+ - `[in] callback`: The function which will be called when an asynchronous
136
+ operations ends. The given function is called from the main event loop thread.
137
+ - `[in] resource_name`: Null-terminated string that represents the
138
+ identifier for the kind of resource that is being provided for diagnostic
139
+ information exposed by the async_hooks API.
140
+
141
+ Returns a `Napi::AsyncWork` instance which can later be queued for execution by
142
+ calling `Napi::AsyncWork::Queue`.
143
+
144
+ ### Constructor
145
+
146
+ Creates a new `Napi::AsyncProgressWorker`.
147
+
148
+ ```cpp
149
+ explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name, const Napi::Object& resource);
150
+ ```
151
+
152
+ - `[in] receiver`: The `this` object to be passed to the called function.
153
+ - `[in] callback`: The function which will be called when an asynchronous
154
+ operations ends. The given function is called from the main event loop thread.
155
+ - `[in] resource_name`: Null-terminated string that represents the
156
+ identifier for the kind of resource that is being provided for diagnostic
157
+ information exposed by the async_hooks API.
158
+ - `[in] resource`: Object associated with the asynchronous operation that
159
+ will be passed to possible async_hooks.
160
+
161
+ Returns a `Napi::AsyncWork` instance which can later be queued for execution by
162
+ calling `Napi::AsyncWork::Queue`.
163
+
164
+ ### Constructor
165
+
166
+ Creates a new `Napi::AsyncProgressWorker`.
167
+
168
+ ```cpp
169
+ explicit Napi::AsyncProgressWorker(Napi::Env env);
170
+ ```
171
+
172
+ - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
173
+
174
+ Returns an `Napi::AsyncProgressWorker` instance which can later be queued for execution by calling
175
+ `Napi::AsyncProgressWorker::Queue`.
176
+
177
+ Available with `NAPI_VERSION` equal to or greater than 5.
178
+
179
+ ### Constructor
180
+
181
+ Creates a new `Napi::AsyncProgressWorker`.
182
+
183
+ ```cpp
184
+ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name);
185
+ ```
186
+
187
+ - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
188
+ - `[in] resource_name`: Null-terminated string that represents the
189
+ identifier for the kind of resource that is being provided for diagnostic
190
+ information exposed by the async_hooks API.
191
+
192
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
193
+ calling `Napi::AsyncProgressWorker::Queue`.
194
+
195
+ Available with `NAPI_VERSION` equal to or greater than 5.
196
+
197
+ ### Constructor
198
+
199
+ Creates a new `Napi::AsyncProgressWorker`.
200
+
201
+ ```cpp
202
+ explicit Napi::AsyncProgressWorker(Napi::Env env, const char* resource_name, const Napi::Object& resource);
203
+ ```
204
+
205
+ - `[in] env`: The environment in which to create the `Napi::AsyncProgressWorker`.
206
+ - `[in] resource_name`: Null-terminated string that represents the
207
+ identifier for the kind of resource that is being provided for diagnostic
208
+ information exposed by the async_hooks API.
209
+ - `[in] resource`: Object associated with the asynchronous operation that
210
+ will be passed to possible async_hooks.
211
+
212
+ Returns a `Napi::AsyncProgressWorker` instance which can later be queued for execution by
213
+ calling `Napi::AsyncProgressWorker::Queue`.
214
+
215
+ Available with `NAPI_VERSION` equal to or greater than 5.
216
+
217
+ ### Destructor
218
+
219
+ Deletes the created work object that is used to execute logic asynchronously and
220
+ release the internal `Napi::ThreadSafeFunction`, which will be aborted to prevent
221
+ unexpected upcoming thread safe calls.
222
+
223
+ ```cpp
224
+ virtual Napi::AsyncProgressWorker::~AsyncProgressWorker();
225
+ ```
226
+
227
+ # AsyncProgressWorker::ExecutionProcess
228
+
229
+ A bridge class created before the worker thread execution of `Napi::AsyncProgressWorker::Execute`.
230
+
231
+ ## Methods
232
+
233
+ ### Send
234
+
235
+ `Napi::AsyncProgressWorker::ExecutionProcess::Send` takes two arguments, a pointer
236
+ to a generic type of data, and a `size_t` to indicate how many items the pointer is
237
+ pointing to.
238
+
239
+ The data pointed to will be copied to internal slots of `Napi::AsyncProgressWorker` so
240
+ after the call to `Napi::AsyncProgressWorker::ExecutionProcess::Send` the data can
241
+ be safely released.
242
+
243
+ Note that `Napi::AsyncProgressWorker::ExecutionProcess::Send` merely guarantees
244
+ **eventual** invocation of `Napi::AsyncProgressWorker::OnProgress`, which means
245
+ multiple send might be coalesced into single invocation of `Napi::AsyncProgressWorker::OnProgress`
246
+ with latest data.
247
+
248
+ ```cpp
249
+ void Napi::AsyncProgressWorker::ExecutionProcess::Send(const T* data, size_t count) const;
250
+ ```
251
+
252
+ ## Example
253
+
254
+ The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that
255
+ inherits from it and implement the `Napi::AsyncProgressWorker::Execute` abstract method.
256
+ Typically input to the worker will be saved within the class' fields generally
257
+ passed in through its constructor.
258
+
259
+ During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute`
260
+ can be used to report the progress of the execution.
261
+
262
+ When the `Napi::AsyncProgressWorker::Execute` method completes without errors the
263
+ `Napi::AsyncProgressWorker::OnOK` function callback will be invoked. In this function the
264
+ results of the computation will be reassembled and returned back to the initial
265
+ JavaScript context.
266
+
267
+ `Napi::AsyncProgressWorker` ensures that all the code in the `Napi::AsyncProgressWorker::Execute`
268
+ function runs in the background out of the **event loop** thread and at the end
269
+ the `Napi::AsyncProgressWorker::OnOK` or `Napi::AsyncProgressWorker::OnError` function will be
270
+ called and are executed as part of the event loop.
271
+
272
+ The code below shows a basic example of the `Napi::AsyncProgressWorker` implementation:
273
+
274
+ ```cpp
275
+ #include<napi.h>
276
+
277
+ #include <chrono>
278
+ #include <thread>
279
+
280
+ use namespace Napi;
281
+
282
+ class EchoWorker : public AsyncProgressWorker<uint32_t> {
283
+ public:
284
+ EchoWorker(Function& callback, std::string& echo)
285
+ : AsyncProgressWorker(callback), echo(echo) {}
286
+
287
+ ~EchoWorker() {}
288
+ // This code will be executed on the worker thread
289
+ void Execute(const ExecutionProgress& progress) {
290
+ // Need to simulate cpu heavy task
291
+ for (uint32_t i = 0; i < 100; ++i) {
292
+ progress.Send(&i, 1)
293
+ std::this_thread::sleep_for(std::chrono::seconds(1));
294
+ }
295
+ }
296
+
297
+ void OnOK() {
298
+ HandleScope scope(Env());
299
+ Callback().Call({Env().Null(), String::New(Env(), echo)});
300
+ }
301
+
302
+ void OnProgress(const uint32_t* data, size_t /* count */) {
303
+ HandleScope scope(Env());
304
+ Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
305
+ }
306
+
307
+ private:
308
+ std::string echo;
309
+ };
310
+ ```
311
+
312
+ The `EchoWorker`'s constructor calls the base class' constructor to pass in the
313
+ callback that the `Napi::AsyncProgressWorker` base class will store persistently. When
314
+ the work on the `Napi::AsyncProgressWorker::Execute` method is done the
315
+ `Napi::AsyncProgressWorker::OnOk` method is called and the results are return back to
316
+ JavaScript when the stored callback is invoked with its associated environment.
317
+
318
+ The following code shows an example of how to create and use an `Napi::AsyncProgressWorker`
319
+
320
+ ```cpp
321
+ #include <napi.h>
322
+
323
+ // Include EchoWorker class
324
+ // ..
325
+
326
+ use namespace Napi;
327
+
328
+ Value Echo(const CallbackInfo& info) {
329
+ // We need to validate the arguments here
330
+ Function cb = info[1].As<Function>();
331
+ std::string in = info[0].As<String>();
332
+ EchoWorker* wk = new EchoWorker(cb, in);
333
+ wk->Queue();
334
+ return info.Env().Undefined();
335
+ }
336
+ ```
337
+
338
+ The implementation of a `Napi::AsyncProgressWorker` can be used by creating a
339
+ new instance and passing to its constructor the callback to execute when the
340
+ asynchronous task ends and other data needed for the computation. Once created,
341
+ the only other action needed is to call the `Napi::AsyncProgressWorker::Queue`
342
+ method that will queue the created worker for execution.
343
+
344
+ [`Napi::AsyncWorker`]: ./async_worker.md