Skip to content

Commit

Permalink
Replace neon by napi (#1578)
Browse files Browse the repository at this point in the history
* Replace neon by napi

* Remove Nodejs Storage trait test

* fmt

* Copyright

* Address review comments

* No quotes

* Missed one

* Rename error variant

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez committed Nov 6, 2023
1 parent a1715d4 commit 16d7f9a
Show file tree
Hide file tree
Showing 23 changed files with 5,657 additions and 5,045 deletions.
90 changes: 90 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
resolver = "2"
members = [
"bindings/core",
# TODO: issue #1424
#"bindings/nodejs",
"bindings/nodejs",
# TODO: issue #1423
#"bindings/python",
# TODO: issue #1425
Expand Down
13 changes: 7 additions & 6 deletions bindings/nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ crate-type = ["cdylib"]
doc = false

[dependencies]
async-trait = { version = "0.1.73", default-features = false }
iota-sdk-bindings-core = { path = "../core", default-features = false, features = [
"events",
"ledger_nano",
Expand All @@ -28,17 +29,17 @@ iota-sdk-bindings-core = { path = "../core", default-features = false, features
"mqtt",
"private_key_secret_manager",
] }

log = { version = "0.4.20", default-features = false }
neon = { version = "0.10.1", default-features = false, features = [
"napi-6",
"event-queue-api",
"promise-api",
] }
napi = { version = "2.13.3", default-features = false, features = ["async"] }
napi-derive = { version = "2.13.0", default-features = false }
once_cell = { version = "1.18.0", default-features = false }
serde_json = { version = "1.0.107", default-features = false }
thiserror = { version = "1.0.49", default-features = false }
tokio = { version = "1.33.0", default-features = false }

[build-dependencies]
napi-build = { version = "2.0.1", default-features = false }

[profile.production]
codegen-units = 1
inherits = "release"
Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ If you have already installed the project and only want to run the build, run th
npm run build
```

This command uses the [cargo-cp-artifact](https://github.com/neon-bindings/cargo-cp-artifact) utility to run the Rust
build and copy the built library into `./build/Release/index.node`.
This command uses the napi build utility to run the Rust build and copy the built library into `./build/Release/index.node`.
Prebuild requires that the binary is in `build/Release` as though it was built with node-gyp.

## Client Usage
Expand Down
8 changes: 8 additions & 0 deletions bindings/nodejs/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate napi_build;

fn main() {
napi_build::setup();
}
2 changes: 0 additions & 2 deletions bindings/nodejs/examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions bindings/nodejs/examples/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,6 @@ call-bind@^1.0.0, call-bind@^1.0.2:
function-bind "^1.1.1"
get-intrinsic "^1.0.2"

cargo-cp-artifact@^0.1.6:
version "0.1.8"
resolved "https://registry.yarnpkg.com/cargo-cp-artifact/-/cargo-cp-artifact-0.1.8.tgz#353814f49f6aa76601a4bcb3ea5f3071180b90de"
integrity sha512-3j4DaoTrsCD1MRkTF2Soacii0Nx7UHCce0EwUf4fHnggwiE4fbmF2AbnfzayR36DF8KGadfh7M/Yfy625kgPlA==

caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
Expand Down Expand Up @@ -888,9 +883,9 @@ [email protected]:
integrity sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==

detect-libc@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.1.tgz#e1897aa88fa6ad197862937fbc0441ef352ee0cd"
integrity sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w==
version "2.0.2"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.2.tgz#8ccf2ba9315350e1241b88d0ac3b0e1fbd99605d"
integrity sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==

diff@^4.0.1:
version "4.0.2"
Expand Down Expand Up @@ -1797,9 +1792,9 @@ next-tick@^1.1.0:
integrity sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==

node-abi@^3.3.0:
version "3.45.0"
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-3.45.0.tgz#f568f163a3bfca5aacfce1fbeee1fa2cc98441f5"
integrity sha512-iwXuFrMAcFVi/ZoZiqq8BzAdsLw9kxDfTC0HMyjXfSL/6CSDAGD5UmR7azrAgWV1zKYq7dUUMj4owusBWKLsiQ==
version "3.51.0"
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-3.51.0.tgz#970bf595ef5a26a271307f8a4befa02823d4e87d"
integrity sha512-SQkEP4hmNWjlniS5zdnfIXTk1x7Ome85RDzHlTbBtzE97Gfwz/Ipw4v/Ryk20DWIy3yCNVLVlGKApCnmvYoJbA==
dependencies:
semver "^7.3.5"

Expand Down Expand Up @@ -2129,9 +2124,9 @@ secp256k1@^4.0.1:
node-gyp-build "^4.2.0"

semver@^7.3.5:
version "7.5.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.2.tgz#5b851e66d1be07c1cdaf37dfc856f543325a2beb"
integrity sha512-SoftuTROv/cRjCze/scjGyiDtcUyxw1rgYQSZY7XTmtR5hX+dm76iDbTH8TkLPHCQmlbQVSSbNZCPM2hb0knnQ==
version "7.5.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e"
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==
dependencies:
lru-cache "^6.0.0"

Expand Down
75 changes: 4 additions & 71 deletions bindings/nodejs/lib/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import type { WalletEventType } from './types/wallet';
import { Event } from './types/wallet';
import type { WalletMethodHandler } from './wallet/wallet-method-handler';
import { __UtilsMethods__ } from './types/utils';
import type { SecretManagerMethodHandler } from './secret_manager/secret-manager-method-handler';
import type { ClientMethodHandler } from './client/client-method-handler';

// @ts-ignore: path is set to match runtime transpiled js path
import addon = require('../build/Release/index.node');
Expand All @@ -29,38 +24,6 @@ const {
migrateStrongholdSnapshotV2ToV3,
} = addon;

const callClientMethodAsync = (
method: string,
handler: ClientMethodHandler,
): Promise<string> =>
new Promise((resolve, reject) => {
callClientMethod(method, handler, (error: Error, result: string) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});

const callSecretManagerMethodAsync = (
method: string,
handler: SecretManagerMethodHandler,
): Promise<string> =>
new Promise((resolve, reject) => {
callSecretManagerMethod(
method,
handler,
(error: Error, result: string) => {
if (error) {
reject(error);
} else {
resolve(result);
}
},
);
});

const callUtilsMethod = (method: __UtilsMethods__): any => {
const response = JSON.parse(callUtilsMethodRust(JSON.stringify(method)));
if (response.type == 'error' || response.type == 'panic') {
Expand All @@ -70,48 +33,18 @@ const callUtilsMethod = (method: __UtilsMethods__): any => {
}
};

const listenWalletAsync = (
eventTypes: WalletEventType[],
callback: (error: Error, event: Event) => void,
handler: WalletMethodHandler,
): Promise<void> => {
listenWallet(
eventTypes,
function (err: any, data: string) {
const parsed = JSON.parse(data);
callback(err, new Event(parsed.accountIndex, parsed.event));
},
handler,
);
return Promise.resolve();
};

const callWalletMethodAsync = (
method: string,
handler: WalletMethodHandler,
): Promise<string> =>
new Promise((resolve, reject) => {
callWalletMethod(method, handler, (error: Error, result: string) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});

export {
initLogger,
createClient,
destroyClient,
createSecretManager,
createWallet,
callClientMethodAsync,
callSecretManagerMethodAsync,
callClientMethod,
callSecretManagerMethod,
callUtilsMethod,
callWalletMethodAsync,
callWalletMethod,
destroyWallet,
listenWalletAsync,
listenWallet,
getClientFromWallet,
getSecretManagerFromWallet,
listenMqtt,
Expand Down
9 changes: 3 additions & 6 deletions bindings/nodejs/lib/client/client-method-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import {
callClientMethodAsync,
callClientMethod,
createClient,
listenMqtt,
destroyClient,
Expand Down Expand Up @@ -38,10 +38,7 @@ export class ClientMethodHandler {
* @returns A promise that resolves to a JSON string response holding the result of the client method.
*/
async callMethod(method: __ClientMethods__): Promise<string> {
return callClientMethodAsync(
JSON.stringify(method),
this.methodHandler,
);
return callClientMethod(this.methodHandler, JSON.stringify(method));
}

/**
Expand All @@ -54,6 +51,6 @@ export class ClientMethodHandler {
topics: string[],
callback: (error: Error, result: string) => void,
): Promise<void> {
return listenMqtt(topics, callback, this.methodHandler);
return listenMqtt(this.methodHandler, topics, callback);
}
}
Loading

0 comments on commit 16d7f9a

Please sign in to comment.