Skip to content

Commit

Permalink
Merge pull request hyperledger-indy#2389 from TimoGlastra/update-node…
Browse files Browse the repository at this point in the history
…js-wrapper

NodeJS wrapper updates
  • Loading branch information
WadeBarnes authored May 25, 2021
2 parents 8993d0b + 503e8bc commit e80e709
Show file tree
Hide file tree
Showing 25 changed files with 408 additions and 402 deletions.
2 changes: 1 addition & 1 deletion libindy/ci/ubuntu.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RUN apt-get update && \
jq

# install nodejs and npm
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs

RUN pip3 install -U \
Expand Down
2 changes: 1 addition & 1 deletion libindy/ci/ubuntu18.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
RUN apt-get update && apt-get install -y maven

# install nodejs and npm
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs

RUN apt-get install -y wget
Expand Down
2 changes: 1 addition & 1 deletion wrappers/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This module has a native compile step. It compiles C++ code and dynamically link

You will need:

* C++ build tools and Python 2. See [this](https://github.com/nodejs/node-gyp#installation) for platform recommendations.
* C++ build tools and Python 3.6+. See [this](https://github.com/nodejs/node-gyp#installation) for platform recommendations.
* `libindy` v1.6+ in your system library path. (i.e. `/usr/lib/libindy.so` for linux)

Then you can install via npm:
Expand Down
13 changes: 8 additions & 5 deletions wrappers/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"main": "src/index.js",
"gypfile": true,
"engines": {
"node": ">=8"
"node": ">=10"
},
"files": [
"src",
Expand All @@ -30,13 +30,16 @@
"dependencies": {
"bindings": "^1.3.1",
"nan": "^2.11.1",
"node-gyp": "^4.0.0"
"node-gyp": "^8.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^3.15.0",
"cuid": "^2.1.4",
"home-dir": "^1.0.0",
"standard": "^12.0.1",
"tempy": "^0.3.0"
"standard": "^16.0.3",
"tempy": "^1.0.0"
},
"ava": {
"timeout": "2m"
}
}
49 changes: 25 additions & 24 deletions wrappers/nodejs/src/IndyError.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var util = require('util')
var capi = require('./indyBinding')
const capi = require('./indyBinding')

var errors = {
const errors = {
100: 'CommonInvalidParam1',
101: 'CommonInvalidParam2',
102: 'CommonInvalidParam3',
Expand Down Expand Up @@ -61,29 +60,31 @@ var errors = {
706: 'TransactionNotAllowedError'
}

function IndyError (err) {
Error.call(this)
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
if (errors.hasOwnProperty(err)) {
this.message = errors[err]
this.indyCode = err
this.indyName = errors[err]
try {
this.indyCurrentErrorJson = capi.getCurrentError()
var details = JSON.parse(this.indyCurrentErrorJson)
if (typeof details.message === 'string') {
this.indyMessage = details.message
}
if (typeof details.backtrace === 'string') {
this.indyBacktrace = details.backtrace
}
} catch (e) {
class IndyError extends Error {
constructor (err) {
super()

this.name = this.constructor.name
Error.captureStackTrace(this, IndyError)

if (Object.prototype.hasOwnProperty.call(errors, err)) {
this.message = errors[err]
this.indyCode = err
this.indyName = errors[err]
try {
this.indyCurrentErrorJson = capi.getCurrentError()
const details = JSON.parse(this.indyCurrentErrorJson)
if (typeof details.message === 'string') {
this.indyMessage = details.message
}
if (typeof details.backtrace === 'string') {
this.indyBacktrace = details.backtrace
}
} catch (e) {}
} else {
this.message = err + ''
}
} else {
this.message = (err + '')
}
}
util.inherits(IndyError, Error)

module.exports = IndyError
16 changes: 8 additions & 8 deletions wrappers/nodejs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var capi = require('./indyBinding')
var wrapIndyCallback = require('./wrapIndyCallback')
var IndyError = require('./IndyError')
const capi = require('./indyBinding')
const wrapIndyCallback = require('./wrapIndyCallback')
const IndyError = require('./IndyError')

function toJson (val) {
if (val === null || val === void 0) {
if (val === null || val === undefined) {
return null
}
if (typeof val === 'string') {
Expand All @@ -22,26 +22,26 @@ function fromJson (val) {
return val
}

var indy = {}
const indy = {}

indy.capi = capi // if you want to skip the json dance, IndyError, and promise support

indy.setRuntimeConfig = function setRuntimeConfig (config) {
var err = capi.setRuntimeConfig(toJson(config))
const err = capi.setRuntimeConfig(toJson(config))
if (err !== 0) {
throw new IndyError(err)
}
}

indy.setDefaultLogger = function setDefaultLogger (pattern) {
var err = capi.setDefaultLogger(pattern)
const err = capi.setDefaultLogger(pattern)
if (err !== 0) {
throw new IndyError(err)
}
}

indy.setLogger = function setLogger (logFn) {
var err = capi.setLogger(logFn)
const err = capi.setLogger(logFn)
if (err !== 0) {
throw new IndyError(err)
}
Expand Down
30 changes: 16 additions & 14 deletions wrappers/nodejs/src/indy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class IndyCallback : public Nan::AsyncResource {
IndyCallback* icb = static_cast<IndyCallback*>(async->data);
icbmap.erase(icb->handle);

v8::Local<v8::Context> context = Nan::GetCurrentContext();

v8::Local<v8::Array> tuple;
v8::Local<v8::Value> argv[2];
argv[0] = Nan::New<v8::Number>(icb->err);
Expand All @@ -231,46 +233,46 @@ class IndyCallback : public Nan::AsyncResource {
break;
case CB_HANDLE_U32:
tuple = Nan::New<v8::Array>();
tuple->Set(0, Nan::New<v8::Number>(icb->handle0));
tuple->Set(1, Nan::New<v8::Number>(icb->u32int0));
(void)tuple->Set(context, 0, Nan::New<v8::Number>(icb->handle0));
(void)tuple->Set(context, 1, Nan::New<v8::Number>(icb->u32int0));
argv[1] = tuple;
break;
case CB_I32:
argv[1] = Nan::New<v8::Number>(icb->i32int0);
break;
case CB_STRING_I64:
tuple = Nan::New<v8::Array>();
tuple->Set(0, toJSString(icb->str0));
tuple->Set(1, Nan::New<v8::Number>(icb->i64int0));
(void)tuple->Set(context, 0, toJSString(icb->str0));
(void)tuple->Set(context, 1, Nan::New<v8::Number>(icb->i64int0));
argv[1] = tuple;
break;
case CB_BUFFER:
argv[1] = Nan::NewBuffer(icb->buffer0data, icb->buffer0len).ToLocalChecked();
break;
case CB_STRING_BUFFER:
tuple = Nan::New<v8::Array>();
tuple->Set(0, toJSString(icb->str0));
tuple->Set(1, Nan::NewBuffer(icb->buffer0data, icb->buffer0len).ToLocalChecked());
(void)tuple->Set(context, 0, toJSString(icb->str0));
(void)tuple->Set(context, 1, Nan::NewBuffer(icb->buffer0data, icb->buffer0len).ToLocalChecked());
argv[1] = tuple;
break;
case CB_STRING_STRING:
tuple = Nan::New<v8::Array>();
tuple->Set(0, toJSString(icb->str0));
tuple->Set(1, toJSString(icb->str1));
(void)tuple->Set(context, 0, toJSString(icb->str0));
(void)tuple->Set(context, 1, toJSString(icb->str1));
argv[1] = tuple;
break;
case CB_STRING_STRING_TIMESTAMP:
tuple = Nan::New<v8::Array>();
tuple->Set(0, toJSString(icb->str0));
tuple->Set(1, toJSString(icb->str1));
tuple->Set(2, Nan::New<v8::Number>(icb->timestamp0));
(void)tuple->Set(context, 0, toJSString(icb->str0));
(void)tuple->Set(context, 1, toJSString(icb->str1));
(void)tuple->Set(context, 2, Nan::New<v8::Number>(icb->timestamp0));
argv[1] = tuple;
break;
case CB_STRING_STRING_STRING:
tuple = Nan::New<v8::Array>();
tuple->Set(0, toJSString(icb->str0));
tuple->Set(1, toJSString(icb->str1));
tuple->Set(2, toJSString(icb->str2));
(void)tuple->Set(context, 0, toJSString(icb->str0));
(void)tuple->Set(context, 1, toJSString(icb->str1));
(void)tuple->Set(context, 2, toJSString(icb->str2));
argv[1] = tuple;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions wrappers/nodejs/src/wrapIndyCallback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var IndyError = require('./IndyError')
const IndyError = require('./IndyError')

function wrapIndyCallback (cb, mapResponse) {
var promise
let promise
if (!cb) {
promise = new Promise(function (resolve, reject) {
cb = function cb (err, data) {
Expand Down
Loading

0 comments on commit e80e709

Please sign in to comment.