Skip to content

Commit 57b6e9e

Browse files
committed
Added code sample for "Asynchronous C++ addons for Node.js with N-API" blog
1 parent 92be42c commit 57b6e9e

File tree

9 files changed

+127
-0
lines changed

9 files changed

+127
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <napi.h>
2+
3+
#include <DataProcessingAsyncWorker.h>
4+
5+
using namespace Napi;
6+
7+
void ProcessData(const CallbackInfo& info) {
8+
Buffer<uint8_t> data = info[0].As<Buffer<uint8_t>>();
9+
Function cb = info[1].As<Function>();
10+
11+
DataProcessingAsyncWorker *worker = new DataProcessingAsyncWorker(data, cb);
12+
worker->Queue();
13+
}
14+
15+
Object Init(Env env, Object exports) {
16+
exports.Set(String::New(env, "processData"),
17+
Function::New(env, ProcessData));
18+
return exports;
19+
}
20+
21+
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <DataProcessingAsyncWorker.h>
2+
3+
DataProcessingAsyncWorker::DataProcessingAsyncWorker(Buffer<uint8_t> &data,
4+
Function &callback) : AsyncWorker(callback),
5+
dataRef(ObjectReference::New(data, 1)),
6+
dataPtr(data.Data()),
7+
dataLength(data.Length())
8+
{
9+
}
10+
11+
void DataProcessingAsyncWorker::Execute()
12+
{
13+
for (size_t i = 0; i < dataLength; i++)
14+
{
15+
uint8_t value = *(dataPtr + i);
16+
*(dataPtr + i) = value * 2;
17+
}
18+
}
19+
20+
void DataProcessingAsyncWorker::OnOK()
21+
{
22+
Callback().Call({});
23+
24+
dataRef.Unref();
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <napi.h>
2+
3+
using namespace Napi;
4+
5+
class DataProcessingAsyncWorker : public AsyncWorker
6+
{
7+
public:
8+
DataProcessingAsyncWorker(Buffer<uint8_t> &data,
9+
Function &callback);
10+
11+
void Execute();
12+
13+
void OnOK();
14+
15+
private:
16+
ObjectReference dataRef;
17+
uint8_t *dataPtr;
18+
size_t dataLength;
19+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'addon',
5+
'sources': [ 'Addon.cc', 'DataProcessingAsyncWorker.cc' ],
6+
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")", "./"],
7+
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
8+
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
9+
}
10+
]
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('bindings')('addon.node');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
console.time('Program runtime');
2+
3+
const fs = require('fs');
4+
5+
const addon = require('./main');
6+
7+
const buf = fs.readFileSync('test-data');
8+
9+
console.time('Time spent in addon on main event loop thread');
10+
console.time('Data manipulation');
11+
12+
addon.processData(buf, () => {
13+
console.timeEnd('Data manipulation');
14+
15+
fs.writeFileSync('test-data-modified', buf);
16+
17+
console.timeEnd('Program runtime');
18+
});
19+
20+
console.timeEnd('Time spent in addon on main event loop thread');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "addon",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "main.js",
6+
"scripts": {
7+
},
8+
"author": "Codemerx",
9+
"license": "MIT",
10+
"gypfile": true,
11+
"dependencies": {
12+
"bindings": "^1.5.0",
13+
"node-addon-api": "^1.6.2"
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
console.time('Program runtime');
2+
3+
const fs = require('fs');
4+
5+
const buf = fs.readFileSync('test-data');
6+
7+
console.time('Data manipulation');
8+
for (let i = 0; i < buf.length; i++) {
9+
buf[i] *= 2;
10+
}
11+
console.timeEnd('Data manipulation');
12+
13+
fs.writeFileSync('test-data-modified', buf);
14+
15+
console.timeEnd('Program runtime');
84.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)