nacos-sdk-rust-binding-node 0.0.1-BETA → 0.0.1-PRE
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +1 -1
- package/README.md +8 -0
- package/example/config.js +6 -4
- package/example/config_decrypt.js +34 -0
- package/example/naming.js +6 -8
- package/index.d.ts +50 -24
- package/package.json +15 -15
- package/src/config.rs +41 -21
- package/src/lib.rs +4 -0
- package/src/naming.rs +49 -53
- package/src/plugin.rs +108 -0
package/Cargo.toml
CHANGED
|
@@ -15,7 +15,7 @@ crate-type = ["cdylib"]
|
|
|
15
15
|
|
|
16
16
|
[dependencies]
|
|
17
17
|
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
|
|
18
|
-
napi = { version = "2", default-features = false, features = ["napi4"] }
|
|
18
|
+
napi = { version = "2", default-features = false, features = ["napi4", "async"] }
|
|
19
19
|
napi-derive = "2"
|
|
20
20
|
|
|
21
21
|
# version = ^0.2
|
package/README.md
CHANGED
|
@@ -3,8 +3,16 @@ nacos-sdk-rust binding for NodeJs with napi.
|
|
|
3
3
|
|
|
4
4
|
Tip: nacos-sdk-nodejs 仓库暂未提供 2.x gRPC 交互模式,为了能升级它,故而通过 node addon 方式调用 nacos-sdk-rust
|
|
5
5
|
|
|
6
|
+
# Usage
|
|
7
|
+
**使用样例请看仓库内的 example 目录**
|
|
8
|
+
|
|
9
|
+
npm 包 -> https://www.npmjs.com/package/nacos-sdk-rust-binding-node
|
|
10
|
+
|
|
11
|
+
目前 GitHub Actions CI 仅能打出部分包,若未支持而需要的场景,请提 issue 看怎样提供。
|
|
12
|
+
|
|
6
13
|
# License
|
|
7
14
|
[Apache License Version 2.0](LICENSE)
|
|
8
15
|
|
|
9
16
|
# Acknowledgement
|
|
10
17
|
- binding for NodeJs with napi by [napi-rs](https://github.com/napi-rs/napi-rs.git)
|
|
18
|
+
- binding the [nacos-sdk-rust](https://github.com/nacos-group/nacos-sdk-rust.git)
|
package/example/config.js
CHANGED
|
@@ -11,11 +11,13 @@ const nacos_config_client = new NacosConfigClient({
|
|
|
11
11
|
|
|
12
12
|
try {
|
|
13
13
|
// If it fails, pay attention to err
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
nacos_config_client.getConfig('hongwen.properties', 'LOVE').then(data => {
|
|
15
|
+
console.log('getConfig => ' + data);
|
|
16
|
+
});
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
nacos_config_client.getConfigResp('hongwen.properties', 'LOVE').then(data => {
|
|
19
|
+
console.log('getConfigResp => ' + JSON.stringify(data));
|
|
20
|
+
});
|
|
19
21
|
} catch(e) {
|
|
20
22
|
console.log(e);
|
|
21
23
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NacosConfigClient, NacosConfigResponse } = require('../index')
|
|
4
|
+
|
|
5
|
+
// If it fails, pay attention to err
|
|
6
|
+
const nacos_config_client = new NacosConfigClient(
|
|
7
|
+
{
|
|
8
|
+
serverAddr: '127.0.0.1:8848',
|
|
9
|
+
namespace: "hongwen",
|
|
10
|
+
appName: "binding-node-example-app"
|
|
11
|
+
},
|
|
12
|
+
(err, config_req, config_resp) => {
|
|
13
|
+
// config_req or config_resp only one not null. e.g. you can do encrypt for config_req, decrypt for config_resp.
|
|
14
|
+
if (config_resp != null) {
|
|
15
|
+
config_resp.content = "func config_decrypt change it." // TODO by customize
|
|
16
|
+
}
|
|
17
|
+
return [config_req, config_resp];
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
// If it fails, pay attention to err
|
|
23
|
+
nacos_config_client.getConfig('hongwen.properties', 'LOVE').then(data => {
|
|
24
|
+
console.log('getConfig => ' + data);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
nacos_config_client.getConfigResp('hongwen.properties', 'LOVE').then((data) => {
|
|
28
|
+
console.log('getConfigResp => ' + JSON.stringify(data));
|
|
29
|
+
});
|
|
30
|
+
} catch(e) {
|
|
31
|
+
console.log(e);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
nacos_config_client.addListener('hongwen.properties', 'LOVE', (err, config_resp) => { console.log(config_resp) });
|
package/example/naming.js
CHANGED
|
@@ -12,10 +12,6 @@ const nacos_naming_client = new NacosNamingClient({
|
|
|
12
12
|
const instance1 = {
|
|
13
13
|
ip: '127.0.0.1',
|
|
14
14
|
port: 9090,
|
|
15
|
-
weight: 1.0,
|
|
16
|
-
healthy: true,
|
|
17
|
-
enabled: true,
|
|
18
|
-
ephemeral: true,
|
|
19
15
|
metadata: { 'application' : 'example-naming' },
|
|
20
16
|
}
|
|
21
17
|
|
|
@@ -51,8 +47,9 @@ function sleep(time){
|
|
|
51
47
|
await sleep(1000);
|
|
52
48
|
|
|
53
49
|
console.log('--------- get all instances 1 ------------');
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
nacos_naming_client.getAllInstances(serviceName, group).then(instance_arr => {
|
|
51
|
+
console.log(instance_arr);
|
|
52
|
+
});
|
|
56
53
|
await sleep(1000);
|
|
57
54
|
|
|
58
55
|
console.log('--------- batchRegisterInstance instance2 ------------');
|
|
@@ -60,8 +57,9 @@ function sleep(time){
|
|
|
60
57
|
await sleep(1000);
|
|
61
58
|
|
|
62
59
|
console.log('--------- get all instances 2 ------------');
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
nacos_naming_client.getAllInstances(serviceName, group).then(instance_arr => {
|
|
61
|
+
console.log(instance_arr);
|
|
62
|
+
});
|
|
65
63
|
|
|
66
64
|
await sleep(300000);
|
|
67
65
|
process.exit(0);
|
package/index.d.ts
CHANGED
|
@@ -37,50 +37,76 @@ export interface NacosServiceInstance {
|
|
|
37
37
|
ip: string
|
|
38
38
|
/** Port */
|
|
39
39
|
port: number
|
|
40
|
-
/** Weight */
|
|
41
|
-
weight
|
|
42
|
-
/** Healthy or not */
|
|
43
|
-
healthy
|
|
44
|
-
/** Enabled ot not */
|
|
45
|
-
enabled
|
|
46
|
-
/** Ephemeral or not */
|
|
47
|
-
ephemeral
|
|
48
|
-
/** Cluster Name */
|
|
40
|
+
/** Weight, default 1.0 */
|
|
41
|
+
weight?: number
|
|
42
|
+
/** Healthy or not, default true */
|
|
43
|
+
healthy?: boolean
|
|
44
|
+
/** Enabled ot not, default true */
|
|
45
|
+
enabled?: boolean
|
|
46
|
+
/** Ephemeral or not, default true */
|
|
47
|
+
ephemeral?: boolean
|
|
48
|
+
/** Cluster Name, default 'DEFAULT' */
|
|
49
49
|
clusterName?: string
|
|
50
50
|
/** Service Name */
|
|
51
51
|
serviceName?: string
|
|
52
|
-
/** Metadata */
|
|
53
|
-
metadata
|
|
52
|
+
/** Metadata, default '{}' */
|
|
53
|
+
metadata?: Record<string, string>
|
|
54
|
+
}
|
|
55
|
+
/** ConfigReq for [`ConfigFilter`] */
|
|
56
|
+
export interface NacosConfigReq {
|
|
57
|
+
/** DataId */
|
|
58
|
+
dataId: string
|
|
59
|
+
/** Group */
|
|
60
|
+
group: string
|
|
61
|
+
/** Namespace/Tenant */
|
|
62
|
+
namespace: string
|
|
63
|
+
/** Content */
|
|
64
|
+
content: string
|
|
65
|
+
/** Content's Encrypted Data Key. */
|
|
66
|
+
encryptedDataKey: string
|
|
67
|
+
}
|
|
68
|
+
/** ConfigResp for [`ConfigFilter`] */
|
|
69
|
+
export interface NacosConfigResp {
|
|
70
|
+
/** DataId */
|
|
71
|
+
dataId: string
|
|
72
|
+
/** Group */
|
|
73
|
+
group: string
|
|
74
|
+
/** Namespace/Tenant */
|
|
75
|
+
namespace: string
|
|
76
|
+
/** Content */
|
|
77
|
+
content: string
|
|
78
|
+
/** Content's Encrypted Data Key. */
|
|
79
|
+
encryptedDataKey: string
|
|
54
80
|
}
|
|
55
81
|
/** Client api of Nacos Config. */
|
|
56
82
|
export class NacosConfigClient {
|
|
57
83
|
/** Build a Config Client. */
|
|
58
|
-
constructor(clientOptions: ClientOptions)
|
|
84
|
+
constructor(clientOptions: ClientOptions, configFilter?: (err: Error | null, arg0?: NacosConfigReq | undefined | null, arg1?: NacosConfigResp | undefined | null) => any | undefined | null)
|
|
59
85
|
/**
|
|
60
86
|
* Get config's content.
|
|
61
87
|
* If it fails, pay attention to err
|
|
62
88
|
*/
|
|
63
|
-
getConfig(dataId: string, group: string): string
|
|
89
|
+
getConfig(dataId: string, group: string): Promise<string>
|
|
64
90
|
/**
|
|
65
91
|
* Get NacosConfigResponse.
|
|
66
92
|
* If it fails, pay attention to err
|
|
67
93
|
*/
|
|
68
|
-
getConfigResp(dataId: string, group: string): NacosConfigResponse
|
|
94
|
+
getConfigResp(dataId: string, group: string): Promise<NacosConfigResponse>
|
|
69
95
|
/**
|
|
70
96
|
* Publish config.
|
|
71
97
|
* If it fails, pay attention to err
|
|
72
98
|
*/
|
|
73
|
-
publishConfig(dataId: string, group: string, content: string): boolean
|
|
99
|
+
publishConfig(dataId: string, group: string, content: string): Promise<boolean>
|
|
74
100
|
/**
|
|
75
101
|
* Remove config.
|
|
76
102
|
* If it fails, pay attention to err
|
|
77
103
|
*/
|
|
78
|
-
removeConfig(dataId: string, group: string): boolean
|
|
104
|
+
removeConfig(dataId: string, group: string): Promise<boolean>
|
|
79
105
|
/**
|
|
80
106
|
* Add NacosConfigChangeListener callback func, which listen the config change.
|
|
81
107
|
* If it fails, pay attention to err
|
|
82
108
|
*/
|
|
83
|
-
addListener(dataId: string, group: string, listener: (err: Error | null, value: NacosConfigResponse) => any): void
|
|
109
|
+
addListener(dataId: string, group: string, listener: (err: Error | null, value: NacosConfigResponse) => any): Promise<void>
|
|
84
110
|
}
|
|
85
111
|
/** Client api of Nacos Naming. */
|
|
86
112
|
export class NacosNamingClient {
|
|
@@ -90,35 +116,35 @@ export class NacosNamingClient {
|
|
|
90
116
|
* Register instance.
|
|
91
117
|
* If it fails, pay attention to err
|
|
92
118
|
*/
|
|
93
|
-
registerInstance(serviceName: string, group: string, serviceInstance: NacosServiceInstance): void
|
|
119
|
+
registerInstance(serviceName: string, group: string, serviceInstance: NacosServiceInstance): Promise<void>
|
|
94
120
|
/**
|
|
95
121
|
* Deregister instance.
|
|
96
122
|
* If it fails, pay attention to err
|
|
97
123
|
*/
|
|
98
|
-
deregisterInstance(serviceName: string, group: string, serviceInstance: NacosServiceInstance): void
|
|
124
|
+
deregisterInstance(serviceName: string, group: string, serviceInstance: NacosServiceInstance): Promise<void>
|
|
99
125
|
/**
|
|
100
126
|
* Batch register instance, improve interaction efficiency.
|
|
101
127
|
* If it fails, pay attention to err
|
|
102
128
|
*/
|
|
103
|
-
batchRegisterInstance(serviceName: string, group: string, serviceInstances: Array<NacosServiceInstance>): void
|
|
129
|
+
batchRegisterInstance(serviceName: string, group: string, serviceInstances: Array<NacosServiceInstance>): Promise<void>
|
|
104
130
|
/**
|
|
105
131
|
* Get all instances by service and group. default cluster=[], subscribe=true.
|
|
106
132
|
* If it fails, pay attention to err
|
|
107
133
|
*/
|
|
108
|
-
getAllInstances(serviceName: string, group: string, clusters?: Array<string> | undefined | null, subscribe?: boolean = true): Array<NacosServiceInstance
|
|
134
|
+
getAllInstances(serviceName: string, group: string, clusters?: Array<string> | undefined | null, subscribe?: boolean = true): Promise<Array<NacosServiceInstance>>
|
|
109
135
|
/**
|
|
110
136
|
* Select instances whether healthy or not. default cluster=[], subscribe=true, healthy=true.
|
|
111
137
|
* If it fails, pay attention to err
|
|
112
138
|
*/
|
|
113
|
-
selectInstances(serviceName: string, group: string, clusters?: Array<string> | undefined | null, subscribe?: boolean = true, healthy?: boolean = true): Array<NacosServiceInstance
|
|
139
|
+
selectInstances(serviceName: string, group: string, clusters?: Array<string> | undefined | null, subscribe?: boolean = true, healthy?: boolean = true): Promise<Array<NacosServiceInstance>>
|
|
114
140
|
/**
|
|
115
141
|
* Select one healthy instance. default cluster=[], subscribe=true.
|
|
116
142
|
* If it fails, pay attention to err
|
|
117
143
|
*/
|
|
118
|
-
selectOneHealthyInstance(serviceName: string, group: string, clusters?: Array<string> | undefined | null, subscribe?: boolean = true): NacosServiceInstance
|
|
144
|
+
selectOneHealthyInstance(serviceName: string, group: string, clusters?: Array<string> | undefined | null, subscribe?: boolean = true): Promise<NacosServiceInstance>
|
|
119
145
|
/**
|
|
120
146
|
* Add NacosNamingEventListener callback func, which listen the instance change.
|
|
121
147
|
* If it fails, pay attention to err
|
|
122
148
|
*/
|
|
123
|
-
subscribe(serviceName: string, group: string, clusters: Array<string> | undefined | null, listener: (err: Error | null, value: Array<NacosServiceInstance>) => any): void
|
|
149
|
+
subscribe(serviceName: string, group: string, clusters: Array<string> | undefined | null, listener: (err: Error | null, value: Array<NacosServiceInstance>) => any): Promise<void>
|
|
124
150
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nacos-sdk-rust-binding-node",
|
|
3
|
-
"version": "0.0.1-
|
|
3
|
+
"version": "0.0.1-PRE",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"napi": {
|
|
@@ -43,19 +43,19 @@
|
|
|
43
43
|
},
|
|
44
44
|
"packageManager": "yarn@3.4.1",
|
|
45
45
|
"optionalDependencies": {
|
|
46
|
-
"nacos-sdk-rust-binding-node-win32-x64-msvc": "0.0.1-
|
|
47
|
-
"nacos-sdk-rust-binding-node-darwin-x64": "0.0.1-
|
|
48
|
-
"nacos-sdk-rust-binding-node-linux-x64-gnu": "0.0.1-
|
|
49
|
-
"nacos-sdk-rust-binding-node-darwin-arm64": "0.0.1-
|
|
50
|
-
"nacos-sdk-rust-binding-node-android-arm64": "0.0.1-
|
|
51
|
-
"nacos-sdk-rust-binding-node-linux-arm64-gnu": "0.0.1-
|
|
52
|
-
"nacos-sdk-rust-binding-node-linux-arm64-musl": "0.0.1-
|
|
53
|
-
"nacos-sdk-rust-binding-node-win32-arm64-msvc": "0.0.1-
|
|
54
|
-
"nacos-sdk-rust-binding-node-linux-arm-gnueabihf": "0.0.1-
|
|
55
|
-
"nacos-sdk-rust-binding-node-linux-x64-musl": "0.0.1-
|
|
56
|
-
"nacos-sdk-rust-binding-node-freebsd-x64": "0.0.1-
|
|
57
|
-
"nacos-sdk-rust-binding-node-win32-ia32-msvc": "0.0.1-
|
|
58
|
-
"nacos-sdk-rust-binding-node-android-arm-eabi": "0.0.1-
|
|
59
|
-
"nacos-sdk-rust-binding-node-darwin-universal": "0.0.1-
|
|
46
|
+
"nacos-sdk-rust-binding-node-win32-x64-msvc": "0.0.1-PRE",
|
|
47
|
+
"nacos-sdk-rust-binding-node-darwin-x64": "0.0.1-PRE",
|
|
48
|
+
"nacos-sdk-rust-binding-node-linux-x64-gnu": "0.0.1-PRE",
|
|
49
|
+
"nacos-sdk-rust-binding-node-darwin-arm64": "0.0.1-PRE",
|
|
50
|
+
"nacos-sdk-rust-binding-node-android-arm64": "0.0.1-PRE",
|
|
51
|
+
"nacos-sdk-rust-binding-node-linux-arm64-gnu": "0.0.1-PRE",
|
|
52
|
+
"nacos-sdk-rust-binding-node-linux-arm64-musl": "0.0.1-PRE",
|
|
53
|
+
"nacos-sdk-rust-binding-node-win32-arm64-msvc": "0.0.1-PRE",
|
|
54
|
+
"nacos-sdk-rust-binding-node-linux-arm-gnueabihf": "0.0.1-PRE",
|
|
55
|
+
"nacos-sdk-rust-binding-node-linux-x64-musl": "0.0.1-PRE",
|
|
56
|
+
"nacos-sdk-rust-binding-node-freebsd-x64": "0.0.1-PRE",
|
|
57
|
+
"nacos-sdk-rust-binding-node-win32-ia32-msvc": "0.0.1-PRE",
|
|
58
|
+
"nacos-sdk-rust-binding-node-android-arm-eabi": "0.0.1-PRE",
|
|
59
|
+
"nacos-sdk-rust-binding-node-darwin-universal": "0.0.1-PRE"
|
|
60
60
|
}
|
|
61
61
|
}
|
package/src/config.rs
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
#![deny(clippy::all)]
|
|
2
2
|
|
|
3
|
-
use napi::{bindgen_prelude::*, threadsafe_function
|
|
3
|
+
use napi::{bindgen_prelude::*, threadsafe_function::*, tokio::sync::Mutex};
|
|
4
4
|
use std::sync::Arc;
|
|
5
5
|
|
|
6
6
|
/// Client api of Nacos Config.
|
|
7
7
|
#[napi]
|
|
8
8
|
pub struct NacosConfigClient {
|
|
9
|
-
inner:
|
|
9
|
+
inner: Arc<Mutex<dyn nacos_sdk::api::config::ConfigService + Send + Sync + 'static>>,
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
#[napi]
|
|
13
13
|
impl NacosConfigClient {
|
|
14
14
|
/// Build a Config Client.
|
|
15
15
|
#[napi(constructor)]
|
|
16
|
-
pub fn new(
|
|
16
|
+
pub fn new(
|
|
17
|
+
client_options: crate::ClientOptions,
|
|
18
|
+
config_filter: Option<
|
|
19
|
+
ThreadsafeFunction<(
|
|
20
|
+
Option<crate::NacosConfigReq>,
|
|
21
|
+
Option<crate::NacosConfigResp>,
|
|
22
|
+
)>,
|
|
23
|
+
>,
|
|
24
|
+
) -> Result<NacosConfigClient> {
|
|
17
25
|
// print to console or file
|
|
18
26
|
crate::log_print_to_console_or_file();
|
|
19
27
|
|
|
@@ -43,45 +51,57 @@ impl NacosConfigClient {
|
|
|
43
51
|
nacos_sdk::api::config::ConfigServiceBuilder::new(props)
|
|
44
52
|
};
|
|
45
53
|
|
|
54
|
+
let config_service_builder = if config_filter.is_some() {
|
|
55
|
+
config_service_builder.add_config_filter(Box::new(crate::NacosConfigFilter {
|
|
56
|
+
func: Arc::new(config_filter.unwrap()),
|
|
57
|
+
}))
|
|
58
|
+
} else {
|
|
59
|
+
config_service_builder
|
|
60
|
+
};
|
|
61
|
+
|
|
46
62
|
let config_service = config_service_builder
|
|
47
63
|
.build()
|
|
48
64
|
.map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
|
|
49
65
|
|
|
50
66
|
Ok(NacosConfigClient {
|
|
51
|
-
inner:
|
|
67
|
+
inner: Arc::new(Mutex::new(config_service)),
|
|
52
68
|
})
|
|
53
69
|
}
|
|
54
70
|
|
|
55
71
|
/// Get config's content.
|
|
56
72
|
/// If it fails, pay attention to err
|
|
57
73
|
#[napi]
|
|
58
|
-
pub fn get_config(&
|
|
59
|
-
|
|
74
|
+
pub async fn get_config(&self, data_id: String, group: String) -> Result<String> {
|
|
75
|
+
let resp = self.get_config_resp(data_id, group).await?;
|
|
76
|
+
Ok(resp.content)
|
|
60
77
|
}
|
|
61
78
|
|
|
62
79
|
/// Get NacosConfigResponse.
|
|
63
80
|
/// If it fails, pay attention to err
|
|
64
81
|
#[napi]
|
|
65
|
-
pub fn get_config_resp(
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
pub async fn get_config_resp(
|
|
83
|
+
&self,
|
|
84
|
+
data_id: String,
|
|
85
|
+
group: String,
|
|
86
|
+
) -> Result<NacosConfigResponse> {
|
|
87
|
+
let mut inner = self.inner.lock().await;
|
|
88
|
+
let config_resp = inner
|
|
68
89
|
.get_config(data_id, group)
|
|
69
90
|
.map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
|
|
70
|
-
|
|
71
91
|
Ok(transfer_conf_resp(config_resp))
|
|
72
92
|
}
|
|
73
93
|
|
|
74
94
|
/// Publish config.
|
|
75
95
|
/// If it fails, pay attention to err
|
|
76
96
|
#[napi]
|
|
77
|
-
pub fn publish_config(
|
|
78
|
-
&
|
|
97
|
+
pub async fn publish_config(
|
|
98
|
+
&self,
|
|
79
99
|
data_id: String,
|
|
80
100
|
group: String,
|
|
81
101
|
content: String,
|
|
82
102
|
) -> Result<bool> {
|
|
83
|
-
self
|
|
84
|
-
|
|
103
|
+
let mut inner = self.inner.lock().await;
|
|
104
|
+
inner
|
|
85
105
|
.publish_config(data_id, group, content, None)
|
|
86
106
|
.map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))
|
|
87
107
|
}
|
|
@@ -89,9 +109,9 @@ impl NacosConfigClient {
|
|
|
89
109
|
/// Remove config.
|
|
90
110
|
/// If it fails, pay attention to err
|
|
91
111
|
#[napi]
|
|
92
|
-
pub fn remove_config(&
|
|
93
|
-
self
|
|
94
|
-
|
|
112
|
+
pub async fn remove_config(&self, data_id: String, group: String) -> Result<bool> {
|
|
113
|
+
let mut inner = self.inner.lock().await;
|
|
114
|
+
inner
|
|
95
115
|
.remove_config(data_id, group)
|
|
96
116
|
.map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))
|
|
97
117
|
}
|
|
@@ -99,14 +119,14 @@ impl NacosConfigClient {
|
|
|
99
119
|
/// Add NacosConfigChangeListener callback func, which listen the config change.
|
|
100
120
|
/// If it fails, pay attention to err
|
|
101
121
|
#[napi]
|
|
102
|
-
pub fn add_listener(
|
|
103
|
-
&
|
|
122
|
+
pub async fn add_listener(
|
|
123
|
+
&self,
|
|
104
124
|
data_id: String,
|
|
105
125
|
group: String,
|
|
106
126
|
listener: ThreadsafeFunction<NacosConfigResponse>,
|
|
107
127
|
) -> Result<()> {
|
|
108
|
-
self
|
|
109
|
-
|
|
128
|
+
let mut inner = self.inner.lock().await;
|
|
129
|
+
inner
|
|
110
130
|
.add_listener(
|
|
111
131
|
data_id,
|
|
112
132
|
group,
|
package/src/lib.rs
CHANGED
|
@@ -17,6 +17,7 @@ fn log_print_to_console_or_file() {
|
|
|
17
17
|
Ok(dir) => dir,
|
|
18
18
|
Err(_) => "/tmp".to_string(),
|
|
19
19
|
};
|
|
20
|
+
// FIXME log to file, now there are files but no content.
|
|
20
21
|
let file_appender = tracing_appender::rolling::daily(home_dir + "/logs/nacos", "nacos.log");
|
|
21
22
|
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
|
|
22
23
|
|
|
@@ -51,3 +52,6 @@ pub use config::*;
|
|
|
51
52
|
|
|
52
53
|
mod naming;
|
|
53
54
|
pub use naming::*;
|
|
55
|
+
|
|
56
|
+
mod plugin;
|
|
57
|
+
pub use plugin::*;
|
package/src/naming.rs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#![deny(clippy::all)]
|
|
2
2
|
|
|
3
|
-
use napi::{bindgen_prelude::*, threadsafe_function
|
|
3
|
+
use napi::{bindgen_prelude::*, threadsafe_function::*, tokio::sync::Mutex};
|
|
4
4
|
use std::sync::Arc;
|
|
5
5
|
|
|
6
6
|
/// Client api of Nacos Naming.
|
|
7
7
|
#[napi]
|
|
8
8
|
pub struct NacosNamingClient {
|
|
9
|
-
inner:
|
|
9
|
+
inner: Arc<Mutex<dyn nacos_sdk::api::naming::NamingService + Send + Sync + 'static>>,
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
#[napi]
|
|
@@ -48,21 +48,21 @@ impl NacosNamingClient {
|
|
|
48
48
|
.map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
|
|
49
49
|
|
|
50
50
|
Ok(NacosNamingClient {
|
|
51
|
-
inner:
|
|
51
|
+
inner: Arc::new(Mutex::new(naming_service)),
|
|
52
52
|
})
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/// Register instance.
|
|
56
56
|
/// If it fails, pay attention to err
|
|
57
57
|
#[napi]
|
|
58
|
-
pub fn register_instance(
|
|
58
|
+
pub async fn register_instance(
|
|
59
59
|
&self,
|
|
60
60
|
service_name: String,
|
|
61
61
|
group: String,
|
|
62
62
|
service_instance: NacosServiceInstance,
|
|
63
63
|
) -> Result<()> {
|
|
64
|
-
self
|
|
65
|
-
|
|
64
|
+
let inner = self.inner.lock().await;
|
|
65
|
+
inner
|
|
66
66
|
.register_service(
|
|
67
67
|
service_name,
|
|
68
68
|
Some(group),
|
|
@@ -74,14 +74,14 @@ impl NacosNamingClient {
|
|
|
74
74
|
/// Deregister instance.
|
|
75
75
|
/// If it fails, pay attention to err
|
|
76
76
|
#[napi]
|
|
77
|
-
pub fn deregister_instance(
|
|
77
|
+
pub async fn deregister_instance(
|
|
78
78
|
&self,
|
|
79
79
|
service_name: String,
|
|
80
80
|
group: String,
|
|
81
81
|
service_instance: NacosServiceInstance,
|
|
82
82
|
) -> Result<()> {
|
|
83
|
-
self
|
|
84
|
-
|
|
83
|
+
let inner = self.inner.lock().await;
|
|
84
|
+
inner
|
|
85
85
|
.deregister_instance(
|
|
86
86
|
service_name,
|
|
87
87
|
Some(group),
|
|
@@ -93,7 +93,7 @@ impl NacosNamingClient {
|
|
|
93
93
|
/// Batch register instance, improve interaction efficiency.
|
|
94
94
|
/// If it fails, pay attention to err
|
|
95
95
|
#[napi]
|
|
96
|
-
pub fn batch_register_instance(
|
|
96
|
+
pub async fn batch_register_instance(
|
|
97
97
|
&self,
|
|
98
98
|
service_name: String,
|
|
99
99
|
group: String,
|
|
@@ -104,8 +104,8 @@ impl NacosNamingClient {
|
|
|
104
104
|
.map(transfer_js_instance_to_rust)
|
|
105
105
|
.collect();
|
|
106
106
|
|
|
107
|
-
self
|
|
108
|
-
|
|
107
|
+
let inner = self.inner.lock().await;
|
|
108
|
+
inner
|
|
109
109
|
.batch_register_instance(service_name, Some(group), rust_instances)
|
|
110
110
|
.map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))
|
|
111
111
|
}
|
|
@@ -113,16 +113,15 @@ impl NacosNamingClient {
|
|
|
113
113
|
/// Get all instances by service and group. default cluster=[], subscribe=true.
|
|
114
114
|
/// If it fails, pay attention to err
|
|
115
115
|
#[napi]
|
|
116
|
-
pub fn get_all_instances(
|
|
116
|
+
pub async fn get_all_instances(
|
|
117
117
|
&self,
|
|
118
118
|
service_name: String,
|
|
119
119
|
group: String,
|
|
120
120
|
clusters: Option<Vec<String>>,
|
|
121
|
-
#[napi(ts_arg_type = "boolean = true")]
|
|
122
|
-
subscribe: Option<bool>,
|
|
121
|
+
#[napi(ts_arg_type = "boolean = true")] subscribe: Option<bool>,
|
|
123
122
|
) -> Result<Vec<NacosServiceInstance>> {
|
|
124
|
-
let
|
|
125
|
-
|
|
123
|
+
let inner = self.inner.lock().await;
|
|
124
|
+
let rust_instances = inner
|
|
126
125
|
.get_all_instances(
|
|
127
126
|
service_name,
|
|
128
127
|
Some(group),
|
|
@@ -142,18 +141,16 @@ impl NacosNamingClient {
|
|
|
142
141
|
/// Select instances whether healthy or not. default cluster=[], subscribe=true, healthy=true.
|
|
143
142
|
/// If it fails, pay attention to err
|
|
144
143
|
#[napi]
|
|
145
|
-
pub fn select_instances(
|
|
144
|
+
pub async fn select_instances(
|
|
146
145
|
&self,
|
|
147
146
|
service_name: String,
|
|
148
147
|
group: String,
|
|
149
148
|
clusters: Option<Vec<String>>,
|
|
150
|
-
#[napi(ts_arg_type = "boolean = true")]
|
|
151
|
-
|
|
152
|
-
#[napi(ts_arg_type = "boolean = true")]
|
|
153
|
-
healthy: Option<bool>,
|
|
149
|
+
#[napi(ts_arg_type = "boolean = true")] subscribe: Option<bool>,
|
|
150
|
+
#[napi(ts_arg_type = "boolean = true")] healthy: Option<bool>,
|
|
154
151
|
) -> Result<Vec<NacosServiceInstance>> {
|
|
155
|
-
let
|
|
156
|
-
|
|
152
|
+
let inner = self.inner.lock().await;
|
|
153
|
+
let rust_instances = inner
|
|
157
154
|
.select_instance(
|
|
158
155
|
service_name,
|
|
159
156
|
Some(group),
|
|
@@ -174,16 +171,15 @@ impl NacosNamingClient {
|
|
|
174
171
|
/// Select one healthy instance. default cluster=[], subscribe=true.
|
|
175
172
|
/// If it fails, pay attention to err
|
|
176
173
|
#[napi]
|
|
177
|
-
pub fn select_one_healthy_instance(
|
|
174
|
+
pub async fn select_one_healthy_instance(
|
|
178
175
|
&self,
|
|
179
176
|
service_name: String,
|
|
180
177
|
group: String,
|
|
181
178
|
clusters: Option<Vec<String>>,
|
|
182
|
-
#[napi(ts_arg_type = "boolean = true")]
|
|
183
|
-
subscribe: Option<bool>,
|
|
179
|
+
#[napi(ts_arg_type = "boolean = true")] subscribe: Option<bool>,
|
|
184
180
|
) -> Result<NacosServiceInstance> {
|
|
185
|
-
let
|
|
186
|
-
|
|
181
|
+
let inner = self.inner.lock().await;
|
|
182
|
+
let rust_instance = inner
|
|
187
183
|
.select_one_healthy_instance(
|
|
188
184
|
service_name,
|
|
189
185
|
Some(group),
|
|
@@ -198,15 +194,15 @@ impl NacosNamingClient {
|
|
|
198
194
|
/// Add NacosNamingEventListener callback func, which listen the instance change.
|
|
199
195
|
/// If it fails, pay attention to err
|
|
200
196
|
#[napi]
|
|
201
|
-
pub fn subscribe(
|
|
197
|
+
pub async fn subscribe(
|
|
202
198
|
&self,
|
|
203
199
|
service_name: String,
|
|
204
200
|
group: String,
|
|
205
201
|
clusters: Option<Vec<String>>,
|
|
206
202
|
listener: ThreadsafeFunction<Vec<NacosServiceInstance>>,
|
|
207
203
|
) -> Result<()> {
|
|
208
|
-
self
|
|
209
|
-
|
|
204
|
+
let inner = self.inner.lock().await;
|
|
205
|
+
inner
|
|
210
206
|
.subscribe(
|
|
211
207
|
service_name,
|
|
212
208
|
Some(group),
|
|
@@ -253,20 +249,20 @@ pub struct NacosServiceInstance {
|
|
|
253
249
|
pub ip: String,
|
|
254
250
|
/// Port
|
|
255
251
|
pub port: i32,
|
|
256
|
-
/// Weight
|
|
257
|
-
pub weight: f64
|
|
258
|
-
/// Healthy or not
|
|
259
|
-
pub healthy: bool
|
|
260
|
-
/// Enabled ot not
|
|
261
|
-
pub enabled: bool
|
|
262
|
-
/// Ephemeral or not
|
|
263
|
-
pub ephemeral: bool
|
|
264
|
-
/// Cluster Name
|
|
252
|
+
/// Weight, default 1.0
|
|
253
|
+
pub weight: Option<f64>,
|
|
254
|
+
/// Healthy or not, default true
|
|
255
|
+
pub healthy: Option<bool>,
|
|
256
|
+
/// Enabled ot not, default true
|
|
257
|
+
pub enabled: Option<bool>,
|
|
258
|
+
/// Ephemeral or not, default true
|
|
259
|
+
pub ephemeral: Option<bool>,
|
|
260
|
+
/// Cluster Name, default 'DEFAULT'
|
|
265
261
|
pub cluster_name: Option<String>,
|
|
266
262
|
/// Service Name
|
|
267
263
|
pub service_name: Option<String>,
|
|
268
|
-
/// Metadata
|
|
269
|
-
pub metadata: std::collections::HashMap<String, String
|
|
264
|
+
/// Metadata, default '{}'
|
|
265
|
+
pub metadata: Option<std::collections::HashMap<String, String>>,
|
|
270
266
|
}
|
|
271
267
|
|
|
272
268
|
fn transfer_js_instance_to_rust(
|
|
@@ -276,13 +272,13 @@ fn transfer_js_instance_to_rust(
|
|
|
276
272
|
instance_id: js_instance.instance_id.clone(),
|
|
277
273
|
ip: js_instance.ip.clone(),
|
|
278
274
|
port: js_instance.port,
|
|
279
|
-
weight: js_instance.weight,
|
|
280
|
-
healthy: js_instance.healthy,
|
|
281
|
-
enabled: js_instance.enabled,
|
|
282
|
-
ephemeral: js_instance.ephemeral,
|
|
275
|
+
weight: js_instance.weight.unwrap_or(1.0),
|
|
276
|
+
healthy: js_instance.healthy.unwrap_or(true),
|
|
277
|
+
enabled: js_instance.enabled.unwrap_or(true),
|
|
278
|
+
ephemeral: js_instance.ephemeral.unwrap_or(true),
|
|
283
279
|
cluster_name: js_instance.cluster_name.clone(),
|
|
284
280
|
service_name: js_instance.service_name.clone(),
|
|
285
|
-
metadata: js_instance.metadata.clone(),
|
|
281
|
+
metadata: js_instance.metadata.clone().unwrap_or_default(),
|
|
286
282
|
}
|
|
287
283
|
}
|
|
288
284
|
|
|
@@ -293,12 +289,12 @@ fn transfer_rust_instance_to_js(
|
|
|
293
289
|
instance_id: rust_instance.instance_id.clone(),
|
|
294
290
|
ip: rust_instance.ip.clone(),
|
|
295
291
|
port: rust_instance.port,
|
|
296
|
-
weight: rust_instance.weight,
|
|
297
|
-
healthy: rust_instance.healthy,
|
|
298
|
-
enabled: rust_instance.enabled,
|
|
299
|
-
ephemeral: rust_instance.ephemeral,
|
|
292
|
+
weight: Some(rust_instance.weight),
|
|
293
|
+
healthy: Some(rust_instance.healthy),
|
|
294
|
+
enabled: Some(rust_instance.enabled),
|
|
295
|
+
ephemeral: Some(rust_instance.ephemeral),
|
|
300
296
|
cluster_name: rust_instance.cluster_name.clone(),
|
|
301
297
|
service_name: rust_instance.service_name.clone(),
|
|
302
|
-
metadata: rust_instance.metadata.clone(),
|
|
298
|
+
metadata: Some(rust_instance.metadata.clone()),
|
|
303
299
|
}
|
|
304
300
|
}
|
package/src/plugin.rs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
use napi::threadsafe_function::*;
|
|
2
|
+
use std::sync::Arc;
|
|
3
|
+
|
|
4
|
+
/// [`config_filter`] It is an advanced feature that does not need to be used by default;
|
|
5
|
+
/// For example: 1. Encrypt ConfigReq.content value and then request; 2. Decrypt ConfigResp.content to get the value.
|
|
6
|
+
pub struct NacosConfigFilter {
|
|
7
|
+
pub(crate) func: Arc<ThreadsafeFunction<(Option<NacosConfigReq>, Option<NacosConfigResp>)>>,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl nacos_sdk::api::plugin::ConfigFilter for NacosConfigFilter {
|
|
11
|
+
fn filter(
|
|
12
|
+
&self,
|
|
13
|
+
config_req: Option<&mut nacos_sdk::api::plugin::ConfigReq>,
|
|
14
|
+
config_resp: Option<&mut nacos_sdk::api::plugin::ConfigResp>,
|
|
15
|
+
) {
|
|
16
|
+
if let Some(config_req) = config_req {
|
|
17
|
+
let js_config_req = NacosConfigReq {
|
|
18
|
+
data_id: config_req.data_id.clone(),
|
|
19
|
+
group: config_req.group.clone(),
|
|
20
|
+
namespace: config_req.namespace.clone(),
|
|
21
|
+
content: config_req.content.clone(),
|
|
22
|
+
encrypted_data_key: config_req.encrypted_data_key.clone(),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let (tx, rx) = std::sync::mpsc::channel();
|
|
26
|
+
self.func.clone().call_with_return_value(
|
|
27
|
+
Ok((Some(js_config_req), None)),
|
|
28
|
+
ThreadsafeFunctionCallMode::Blocking,
|
|
29
|
+
move |(after_js_config_req, _after_js_config_resp): (
|
|
30
|
+
Option<NacosConfigReq>,
|
|
31
|
+
Option<NacosConfigResp>,
|
|
32
|
+
)| {
|
|
33
|
+
let after_js_config_req = after_js_config_req.unwrap();
|
|
34
|
+
let _ = tx.send(after_js_config_req);
|
|
35
|
+
Ok(())
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
let ret = rx.recv().unwrap();
|
|
40
|
+
config_req.data_id = ret.data_id;
|
|
41
|
+
config_req.group = ret.group;
|
|
42
|
+
config_req.namespace = ret.namespace;
|
|
43
|
+
config_req.content = ret.content;
|
|
44
|
+
config_req.encrypted_data_key = ret.encrypted_data_key;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if let Some(config_resp) = config_resp {
|
|
48
|
+
let js_config_resp = NacosConfigResp {
|
|
49
|
+
data_id: config_resp.data_id.clone(),
|
|
50
|
+
group: config_resp.group.clone(),
|
|
51
|
+
namespace: config_resp.namespace.clone(),
|
|
52
|
+
content: config_resp.content.clone(),
|
|
53
|
+
encrypted_data_key: config_resp.encrypted_data_key.clone(),
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
let (tx, rx) = std::sync::mpsc::channel();
|
|
57
|
+
self.func.clone().call_with_return_value(
|
|
58
|
+
Ok((None, Some(js_config_resp))),
|
|
59
|
+
ThreadsafeFunctionCallMode::Blocking,
|
|
60
|
+
move |(_after_js_config_req, after_js_config_resp): (
|
|
61
|
+
Option<NacosConfigReq>,
|
|
62
|
+
Option<NacosConfigResp>,
|
|
63
|
+
)| {
|
|
64
|
+
let after_js_config_resp = after_js_config_resp.unwrap();
|
|
65
|
+
let _ = tx.send(after_js_config_resp);
|
|
66
|
+
Ok(())
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
let ret = rx.recv().unwrap();
|
|
71
|
+
config_resp.data_id = ret.data_id;
|
|
72
|
+
config_resp.group = ret.group;
|
|
73
|
+
config_resp.namespace = ret.namespace;
|
|
74
|
+
config_resp.content = ret.content;
|
|
75
|
+
config_resp.encrypted_data_key = ret.encrypted_data_key;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/// ConfigReq for [`ConfigFilter`]
|
|
81
|
+
#[napi(object)]
|
|
82
|
+
pub struct NacosConfigReq {
|
|
83
|
+
/// DataId
|
|
84
|
+
pub data_id: String,
|
|
85
|
+
/// Group
|
|
86
|
+
pub group: String,
|
|
87
|
+
/// Namespace/Tenant
|
|
88
|
+
pub namespace: String,
|
|
89
|
+
/// Content
|
|
90
|
+
pub content: String,
|
|
91
|
+
/// Content's Encrypted Data Key.
|
|
92
|
+
pub encrypted_data_key: String,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// ConfigResp for [`ConfigFilter`]
|
|
96
|
+
#[napi(object)]
|
|
97
|
+
pub struct NacosConfigResp {
|
|
98
|
+
/// DataId
|
|
99
|
+
pub data_id: String,
|
|
100
|
+
/// Group
|
|
101
|
+
pub group: String,
|
|
102
|
+
/// Namespace/Tenant
|
|
103
|
+
pub namespace: String,
|
|
104
|
+
/// Content
|
|
105
|
+
pub content: String,
|
|
106
|
+
/// Content's Encrypted Data Key.
|
|
107
|
+
pub encrypted_data_key: String,
|
|
108
|
+
}
|