Skip to content

Commit 92da1a0

Browse files
committed
Use nan to support newer node
1 parent 7287739 commit 92da1a0

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
build
3+
node_modules

binding.gyp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
],
1616
"defines": [
1717
"BZ_NO_STDIO"
18+
],
19+
"include_dirs" : [
20+
"<!(node -e \"require('nan')\")"
1821
]
1922
}
2023
]

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"gypfile": true,
1111
"author": "tdzl2003",
1212
"license": "ISC",
13-
"dependencies": {},
13+
"dependencies": {
14+
"nan": "^2.14.0"
15+
},
1416
"devDependencies": {},
1517
"repository": {
1618
"type": "git",

src/main.cc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
22
* Created by tdzl2003 on 2/28/16.
33
*/
4-
4+
#include <nan.h>
55
#include <node.h>
66
#include <node_buffer.h>
77

8-
#include <cmemory>
98
#include <cstdlib>
109

1110
using namespace std;
@@ -42,7 +41,10 @@ namespace bsdiffNode
4241
Local<Object> returnObj = node::Buffer::Copy(opaque->isolate, (const char*)buffer, size).ToLocalChecked();
4342

4443
Local<Value> argv[1] = { returnObj };
45-
opaque->cb->Call(Null(opaque->isolate), 1, argv);
44+
// opaque->cb->Call(Nan::GetCurrentContext()->Global(), Null(opaque->isolate), 1, argv);
45+
46+
47+
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), opaque->cb, 1, argv);
4648

4749
return 0;
4850
}
@@ -52,8 +54,7 @@ namespace bsdiffNode
5254
HandleScope scope(isolate);
5355

5456
if (!node::Buffer::HasInstance(args[0]) || !node::Buffer::HasInstance(args[1]) || !args[2]->IsFunction()) {
55-
isolate->ThrowException(Exception::TypeError(
56-
String::NewFromUtf8(isolate, "Invalid arguments.")));
57+
Nan::ThrowError("Invalid arguments.");
5758
}
5859

5960
char* oldData = node::Buffer::Data(args[0]);
@@ -75,8 +76,7 @@ namespace bsdiffNode
7576
stream.opaque = &streamOpaque;
7677

7778
if (bsdiff((const uint8_t*)oldData, oldLength, (const uint8_t*)newData, newLength, &stream)) {
78-
isolate->ThrowException(Exception::Error(
79-
String::NewFromUtf8(isolate, "Create bsdiff failed.")));
79+
Nan::ThrowError("Create bsdiff failed.");
8080
}
8181

8282
// args.GetReturnValue().Set(returnObj);
@@ -88,7 +88,7 @@ namespace bsdiffNode
8888
HandleScope scope(isolate);
8989

9090
if (!node::Buffer::HasInstance(args[0])) {
91-
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Invalid arguments.")));
91+
Nan::ThrowError("Invalid arguments.");
9292
}
9393

9494
char* Data = node::Buffer::Data(args[0]);
@@ -102,7 +102,7 @@ namespace bsdiffNode
102102

103103
int ret = BZ2_bzCompressInit ( &stream, 9, 0, 0 );
104104
if (ret != BZ_OK) {
105-
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Compress error")));
105+
Nan::ThrowError("Compress error.");
106106
}
107107

108108
// Local<Object> obj = node::Buffer::New(isolate, 4096).ToLocalChecked();
@@ -119,7 +119,9 @@ namespace bsdiffNode
119119
while (ret == BZ_FINISH_OK) {
120120
Local<Object> obj = node::Buffer::Copy(isolate, bufStart, stream.next_out - bufStart).ToLocalChecked();
121121
Local<Value> argv[1] = { obj };
122-
cb->Call(Null(isolate), 1, argv);
122+
// cb->Call(Null(isolate), 1, argv);
123+
124+
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), cb, 1, argv);
123125

124126
stream.next_out = bufStart;
125127
stream.avail_out = 4096;
@@ -128,12 +130,14 @@ namespace bsdiffNode
128130

129131
if (ret != BZ_STREAM_END) {
130132
free(bufStart);
131-
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Compress error")));
133+
Nan::ThrowError("Compress error.");
132134
}
133135

134136
Local<Object> obj = node::Buffer::Copy(isolate, bufStart, stream.next_out - bufStart).ToLocalChecked();
135137
Local<Value> argv[1] = { obj };
136-
cb->Call(Null(isolate), 1, argv);
138+
// cb->Call(Null(isolate), 1, argv);
139+
140+
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), cb, 1, argv);
137141

138142
BZ2_bzCompressEnd(&stream);
139143
free(bufStart);

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
nan@^2.14.0:
6+
version "2.14.0"
7+
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
8+
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==

0 commit comments

Comments
 (0)