Skip to content

Commit

Permalink
On napi-rewrite: save
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiask88 committed Mar 10, 2024
3 parents 008b1f5 + 534db8c + 7b387d6 commit ab44857
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 41 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
build/
prebuilds/
node_modules/
test/
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test/
.travis.yml
appveyor.yml
.github/
.npmignore
.gitignore
13 changes: 9 additions & 4 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"targets": [{
"target_name": "node_snap7",
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")",
"./src"
],
"sources": [
Expand All @@ -11,15 +10,21 @@
"./src/node_snap7_server.cpp",
"./src/snap7.cpp"
],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
"defines": ["NODE_ADDON_API_DISABLE_DEPRECATED"],
"conditions": [
["OS=='win'", {
"libraries": ["-lws2_32.lib", "-lwinmm.lib"],
"defines": ["_WINSOCK_DEPRECATED_NO_WARNINGS", "_HAS_EXCEPTIONS=0"] # Make sure the STL doesn't try to use exceptions
"defines": ["_WINSOCK_DEPRECATED_NO_WARNINGS"] # Make sure the STL doesn't try to use exceptions
}],
['OS=="mac"', {
'cflags+': ['-fvisibility=hidden'],
'xcode_settings': {
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
}
}]
],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")",
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api",
"snap7"
]
}, {
Expand Down
8 changes: 4 additions & 4 deletions lib/node-snap7.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* MIT License <https://github.com/mathiask88/node-snap7/blob/master/LICENSE>
*/

var events = require('events');
const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;

module.exports = snap7 = require('../binding');

inherits(snap7.S7Server, EventEmitter);

snap7.S7Client.prototype.DBRead = async function (dbNumber, start, size) {
return this.ReadArea(this.S7AreaDB, dbNumber, start, size, this.S7WLByte);
}
Expand Down Expand Up @@ -54,6 +57,3 @@ snap7.S7Client.prototype.CTRead = async function (start, size) {
snap7.S7Client.prototype.CTWrite = async function (start, size, buf) {
return this.WriteArea(this.S7AreaCT, 0, start, size, this.S7WLCounter, buf);
}

snap7.S7Server.super_ = events.EventEmitter;
Object.setPrototypeOf(snap7.S7Server.prototype, events.EventEmitter.prototype);
53 changes: 49 additions & 4 deletions src/node_snap7_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ Napi::Value S7Client::ConnectTo(const Napi::CallbackInfo &info) {

if (info.Length() < 3) {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

if (!info[0].IsString() || !info[1].IsNumber() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

std::string* remAddress = new std::string(info[0].As<Napi::String>().Utf8Value());
Expand All @@ -263,6 +265,7 @@ Napi::Value S7Client::SetConnectionParams(const Napi::CallbackInfo &info) {
if (!info[0].IsString() || !info[1].IsNumber() ||
!info[2].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

std::string remAddress = info[0].As<Napi::String>().Utf8Value();
Expand All @@ -282,6 +285,7 @@ Napi::Value S7Client::SetConnectionType(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

word type = info[0].As<Napi::Number>().Uint32Value();;
Expand All @@ -302,6 +306,7 @@ Napi::Value S7Client::GetParam(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int pData;
Expand All @@ -322,6 +327,7 @@ Napi::Value S7Client::SetParam(const Napi::CallbackInfo &info) {

if (!(info[0].IsNumber() || info[1].IsNumber())) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int pData = info[1].As<Napi::Number>().Int32Value();
Expand Down Expand Up @@ -674,13 +680,17 @@ void IOWorker::OnOK() {
Napi::Value S7Client::ReadArea(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

if (info.Length() < 5)
if (info.Length() < 5) {
Napi::TypeError::New(env, "Wrong number of Arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

if (!info[0].IsNumber() || !info[1].IsNumber() ||
!info[2].IsNumber() || !info[3].IsNumber() ||
!info[4].IsNumber())
!info[4].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int amount = info[3].As<Napi::Number>().Int32Value();
int byteCount = GetByteCountFromWordLen(info[4].As<Napi::Number>().Int32Value());
Expand All @@ -701,13 +711,17 @@ Napi::Value S7Client::ReadArea(const Napi::CallbackInfo &info) {
Napi::Value S7Client::WriteArea(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

if (info.Length() < 6)
if (info.Length() < 6) {
Napi::TypeError::New(env, "Wrong number of Arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

if (!info[0].IsNumber() || !info[1].IsNumber() ||
!info[2].IsNumber() || !info[3].IsNumber() ||
!info[4].IsNumber() || !info[5].IsBuffer())
!info[4].IsNumber() || !info[5].IsBuffer()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

IOWorker* worker = new IOWorker(env, this, DataIOFunction::WRITEAREA
, info[5].As<Napi::Buffer<char>>().Data()
Expand All @@ -726,41 +740,49 @@ Napi::Value S7Client::ReadMultiVars(const Napi::CallbackInfo &info) {

if (info.Length() < 1) {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

if (!info[0].IsArray()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

const Napi::Array data_arr = info[0].As<Napi::Array>();
uint32_t len = data_arr.Length();
if (len == 0) {
Napi::TypeError::New(env, "Array needs at least 1 item").ThrowAsJavaScriptException();
return env.Undefined();
} else if (len > MaxVars) {
std::stringstream err;
err << "Array exceeds max variables (" << MaxVars
<< ") that can be transferred with ReadMultiVars()";
Napi::TypeError::New(env, err.str()).ThrowAsJavaScriptException();
return env.Undefined();
}

for (uint32_t i = 0; i < len; i++) {
if (!data_arr[i].IsObject()) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
} else {
Napi::Object data_obj = data_arr[i].As<Napi::Object>();
if (!data_obj.Has("Area") ||
!data_obj.Has("WordLen") ||
!data_obj.Has("Start") ||
!data_obj.Has("Amount")) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
} else if (!data_obj.Get("Area").IsNumber() ||
!data_obj.Get("WordLen").IsNumber() ||
!data_obj.Get("Start").IsNumber() ||
!data_obj.Get("Amount").IsNumber()) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
} else if (data_obj.Get("Area").As<Napi::Number>().Int32Value() == S7AreaDB) {
if (!data_obj.Has("DBNumber")) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
}
} else {
data_obj.Set("DBNumber", Napi::Number::New(env, 0));
Expand Down Expand Up @@ -834,26 +856,31 @@ Napi::Value S7Client::WriteMultiVars(const Napi::CallbackInfo &info) {

if (info.Length() < 1) {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

if (!info[0].IsArray()) {
Napi::TypeError::New(info.Env(), "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

const Napi::Array data_arr = info[0].As<Napi::Array>();
uint32_t len = data_arr.Length();
if (len == 0) {
Napi::TypeError::New(env, "Array needs at least 1 item").ThrowAsJavaScriptException();
return env.Undefined();
} else if (len > MaxVars) {
std::stringstream err;
err << "Array exceeds max variables (" << MaxVars
<< ") that can be transferred with WriteMultiVars()";
Napi::TypeError::New(env, err.str()).ThrowAsJavaScriptException();
return env.Undefined();
}

for (uint32_t i = 0; i < len; i++) {
if (!data_arr[i].IsObject()) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
} else {
Napi::Object data_obj = data_arr[i].As<Napi::Object>();
if (!data_obj.Has("Area") ||
Expand All @@ -862,15 +889,18 @@ Napi::Value S7Client::WriteMultiVars(const Napi::CallbackInfo &info) {
!data_obj.Has("Amount") ||
!data_obj.Has("Data")) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
} else if (!data_obj.Get("Area").IsNumber() ||
!data_obj.Get("WordLen").IsNumber() ||
!data_obj.Get("Start").IsNumber() ||
!data_obj.Get("Amount").IsNumber() ||
!data_obj.Get("Data").IsBuffer()) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
} else if (data_obj.Get("Area").As<Napi::Number>().Int32Value() == S7AreaDB) {
if (!data_obj.Has("DBNumber")) {
Napi::TypeError::New(env, "Wrong argument structure").ThrowAsJavaScriptException();
return env.Undefined();
}
} else {
data_obj.Set("DBNumber", Napi::Number::New(env, 0));
Expand Down Expand Up @@ -933,6 +963,7 @@ Napi::Value S7Client::GetAgBlockInfo(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

PS7BlockInfo BlockInfo = new TS7BlockInfo;
Expand All @@ -948,6 +979,7 @@ Napi::Value S7Client::GetPgBlockInfo(const Napi::CallbackInfo &info) {

if (!info[0].IsBuffer()) {
Napi::TypeError::New(env, "Argument should be a Buffer").ThrowAsJavaScriptException();
return env.Undefined();
}

PS7BlockInfo BlockInfo = new TS7BlockInfo;
Expand Down Expand Up @@ -994,6 +1026,7 @@ Napi::Value S7Client::ListBlocksOfType(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int BlockNum = sizeof(TS7BlocksOfType) / sizeof(PS7BlocksOfType);
Expand Down Expand Up @@ -1025,6 +1058,7 @@ Napi::Value S7Client::Upload(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int size = info[2].As<Napi::Number>().Int32Value();
Expand All @@ -1042,6 +1076,7 @@ Napi::Value S7Client::FullUpload(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int size = info[2].As<Napi::Number>().Int32Value();
Expand All @@ -1059,6 +1094,7 @@ Napi::Value S7Client::Download(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber() || info[1].IsBuffer()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

Napi::Buffer<char> buffer = info[1].As<Napi::Buffer<char>>();
Expand All @@ -1076,6 +1112,7 @@ Napi::Value S7Client::Delete(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

IOWorker* worker = new IOWorker(env, this, DataIOFunction::DELETEBLOCK
Expand All @@ -1091,6 +1128,7 @@ Napi::Value S7Client::DBGet(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int size = 65536;
Expand All @@ -1107,6 +1145,7 @@ Napi::Value S7Client::DBFill(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber() || !(info[1].IsNumber() || info[1].IsString())) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

int fill;
Expand Down Expand Up @@ -1141,6 +1180,7 @@ Napi::Value S7Client::SetPlcDateTime(const Napi::CallbackInfo &info) {

if (!(info[0].IsObject() || info[0].IsDate())) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

tm *DateTime = new tm;
Expand Down Expand Up @@ -1249,6 +1289,7 @@ Napi::Value S7Client::ReadSZL(const Napi::CallbackInfo &info) {

if (!(info[0].IsNumber() || info[1].IsNumber())) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

PS7SZL SZL = new TS7SZL;
Expand Down Expand Up @@ -1317,6 +1358,7 @@ Napi::Value S7Client::CopyRamToRom(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

IOWorker* worker = new IOWorker(env, this, DataIOFunction::COPYRAMTOROM
Expand All @@ -1331,6 +1373,7 @@ Napi::Value S7Client::Compress(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

IOWorker* worker = new IOWorker(env, this, DataIOFunction::COMPRESS
Expand Down Expand Up @@ -1372,6 +1415,7 @@ Napi::Value S7Client::SetSessionPassword(const Napi::CallbackInfo &info) {

if (!info[0].IsString()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

std::string *password = new std::string(info[0].As<Napi::String>().Utf8Value());
Expand Down Expand Up @@ -1453,6 +1497,7 @@ Napi::Value S7Client::ErrorText(const Napi::CallbackInfo &info) {

if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Undefined();
}

return Napi::String::New(env,
Expand Down
Loading

0 comments on commit ab44857

Please sign in to comment.