nacos-sdk-rust-binding-node 0.0.1-ALPHA

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/.yarnrc.yml ADDED
@@ -0,0 +1,3 @@
1
+ nodeLinker: node-modules
2
+
3
+ yarnPath: .yarn/releases/yarn-3.4.1.cjs
package/Cargo.toml ADDED
@@ -0,0 +1,23 @@
1
+ [package]
2
+ edition = "2021"
3
+ name = "nacos-sdk-rust-binding-node"
4
+ version = "0.0.1"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib"]
8
+
9
+ [dependencies]
10
+ # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
11
+ napi = { version = "2", default-features = false, features = ["napi4"] }
12
+ napi-derive = "2"
13
+
14
+ # version = ^0.2
15
+ nacos-sdk = { git = "https://github.com/CherishCai/nacos-sdk-rust.git", branch = "chore_try_grpcio-sys_exlude_openssl"}
16
+ tracing-subscriber = "0.3"
17
+ tracing-appender = "0.2"
18
+
19
+ [build-dependencies]
20
+ napi-build = "2"
21
+
22
+ [profile.release]
23
+ lto = true
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # nacos-sdk-rust-binding-node
2
+ nacos-sdk-rust binding for NodeJs with napi.
3
+
4
+ Tip: nacos-sdk-nodejs 仓库暂未提供 2.x gRPC 交互模式,为了能升级它,故而通过 node addon 方式调用 nacos-sdk-rust
5
+
6
+ # License
7
+ [Apache License Version 2.0](LICENSE)
8
+
9
+ # Acknowledgement
10
+ - binding for NodeJs with napi by [napi-rs](https://github.com/napi-rs/napi-rs.git)
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ const { NacosConfigClient, NacosConfigResponse } = require('../index')
4
+
5
+ const nacos_config_client = new NacosConfigClient('0.0.0.0:8848', "hongwen", "binding-node-example-app");
6
+
7
+ var conf_resp = nacos_config_client.getConfig('hongwen.properties', 'LOVE');
8
+ console.log(conf_resp);
9
+ console.log(conf_resp.content);
10
+
11
+ nacos_config_client.addListener('hongwen.properties', 'LOVE', (err, conf_resp) => { console.log(conf_resp) });
package/build.rs ADDED
@@ -0,0 +1,5 @@
1
+ extern crate napi_build;
2
+
3
+ fn main() {
4
+ napi_build::setup();
5
+ }
package/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ export function sum(a: number, b: number): number
7
+ export interface NacosConfigResponse {
8
+ /** Namespace/Tenant */
9
+ namespace: string
10
+ /** DataId */
11
+ dataId: string
12
+ /** Group */
13
+ group: string
14
+ /** Content */
15
+ content: string
16
+ /** Content's Type; e.g. json,properties,xml,html,text,yaml */
17
+ contentType: string
18
+ /** Content's md5 */
19
+ md5: string
20
+ }
21
+ /** Client api of Nacos Config. */
22
+ export class NacosConfigClient {
23
+ constructor(serverAddr: string, namespace: string, appName: string, username?: string | undefined | null, password?: string | undefined | null)
24
+ /** Get a NacosConfigResponse from server. */
25
+ getConfig(dataId: string, group: string): NacosConfigResponse
26
+ /** Add a NacosConfigChangeListener callback func, which listen the config change. */
27
+ addListener(dataId: string, group: string, listener: (err: Error | null, value: NacosConfigResponse) => any): void
28
+ }
package/index.js ADDED
@@ -0,0 +1,252 @@
1
+ const { existsSync, readFileSync } = require('fs')
2
+ const { join } = require('path')
3
+
4
+ const { platform, arch } = process
5
+
6
+ let nativeBinding = null
7
+ let localFileExisted = false
8
+ let loadError = null
9
+
10
+ function isMusl() {
11
+ // For Node 10
12
+ if (!process.report || typeof process.report.getReport !== 'function') {
13
+ try {
14
+ const lddPath = require('child_process').execSync('which ldd').toString().trim();
15
+ return readFileSync(lddPath, 'utf8').includes('musl')
16
+ } catch (e) {
17
+ return true
18
+ }
19
+ } else {
20
+ const { glibcVersionRuntime } = process.report.getReport().header
21
+ return !glibcVersionRuntime
22
+ }
23
+ }
24
+
25
+ switch (platform) {
26
+ case 'android':
27
+ switch (arch) {
28
+ case 'arm64':
29
+ localFileExisted = existsSync(join(__dirname, 'nacos-sdk-rust-binding-node.android-arm64.node'))
30
+ try {
31
+ if (localFileExisted) {
32
+ nativeBinding = require('./nacos-sdk-rust-binding-node.android-arm64.node')
33
+ } else {
34
+ nativeBinding = require('nacos-sdk-rust-binding-node-android-arm64')
35
+ }
36
+ } catch (e) {
37
+ loadError = e
38
+ }
39
+ break
40
+ case 'arm':
41
+ localFileExisted = existsSync(join(__dirname, 'nacos-sdk-rust-binding-node.android-arm-eabi.node'))
42
+ try {
43
+ if (localFileExisted) {
44
+ nativeBinding = require('./nacos-sdk-rust-binding-node.android-arm-eabi.node')
45
+ } else {
46
+ nativeBinding = require('nacos-sdk-rust-binding-node-android-arm-eabi')
47
+ }
48
+ } catch (e) {
49
+ loadError = e
50
+ }
51
+ break
52
+ default:
53
+ throw new Error(`Unsupported architecture on Android ${arch}`)
54
+ }
55
+ break
56
+ case 'win32':
57
+ switch (arch) {
58
+ case 'x64':
59
+ localFileExisted = existsSync(
60
+ join(__dirname, 'nacos-sdk-rust-binding-node.win32-x64-msvc.node')
61
+ )
62
+ try {
63
+ if (localFileExisted) {
64
+ nativeBinding = require('./nacos-sdk-rust-binding-node.win32-x64-msvc.node')
65
+ } else {
66
+ nativeBinding = require('nacos-sdk-rust-binding-node-win32-x64-msvc')
67
+ }
68
+ } catch (e) {
69
+ loadError = e
70
+ }
71
+ break
72
+ case 'ia32':
73
+ localFileExisted = existsSync(
74
+ join(__dirname, 'nacos-sdk-rust-binding-node.win32-ia32-msvc.node')
75
+ )
76
+ try {
77
+ if (localFileExisted) {
78
+ nativeBinding = require('./nacos-sdk-rust-binding-node.win32-ia32-msvc.node')
79
+ } else {
80
+ nativeBinding = require('nacos-sdk-rust-binding-node-win32-ia32-msvc')
81
+ }
82
+ } catch (e) {
83
+ loadError = e
84
+ }
85
+ break
86
+ case 'arm64':
87
+ localFileExisted = existsSync(
88
+ join(__dirname, 'nacos-sdk-rust-binding-node.win32-arm64-msvc.node')
89
+ )
90
+ try {
91
+ if (localFileExisted) {
92
+ nativeBinding = require('./nacos-sdk-rust-binding-node.win32-arm64-msvc.node')
93
+ } else {
94
+ nativeBinding = require('nacos-sdk-rust-binding-node-win32-arm64-msvc')
95
+ }
96
+ } catch (e) {
97
+ loadError = e
98
+ }
99
+ break
100
+ default:
101
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
102
+ }
103
+ break
104
+ case 'darwin':
105
+ localFileExisted = existsSync(join(__dirname, 'nacos-sdk-rust-binding-node.darwin-universal.node'))
106
+ try {
107
+ if (localFileExisted) {
108
+ nativeBinding = require('./nacos-sdk-rust-binding-node.darwin-universal.node')
109
+ } else {
110
+ nativeBinding = require('nacos-sdk-rust-binding-node-darwin-universal')
111
+ }
112
+ break
113
+ } catch {}
114
+ switch (arch) {
115
+ case 'x64':
116
+ localFileExisted = existsSync(join(__dirname, 'nacos-sdk-rust-binding-node.darwin-x64.node'))
117
+ try {
118
+ if (localFileExisted) {
119
+ nativeBinding = require('./nacos-sdk-rust-binding-node.darwin-x64.node')
120
+ } else {
121
+ nativeBinding = require('nacos-sdk-rust-binding-node-darwin-x64')
122
+ }
123
+ } catch (e) {
124
+ loadError = e
125
+ }
126
+ break
127
+ case 'arm64':
128
+ localFileExisted = existsSync(
129
+ join(__dirname, 'nacos-sdk-rust-binding-node.darwin-arm64.node')
130
+ )
131
+ try {
132
+ if (localFileExisted) {
133
+ nativeBinding = require('./nacos-sdk-rust-binding-node.darwin-arm64.node')
134
+ } else {
135
+ nativeBinding = require('nacos-sdk-rust-binding-node-darwin-arm64')
136
+ }
137
+ } catch (e) {
138
+ loadError = e
139
+ }
140
+ break
141
+ default:
142
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
143
+ }
144
+ break
145
+ case 'freebsd':
146
+ if (arch !== 'x64') {
147
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
148
+ }
149
+ localFileExisted = existsSync(join(__dirname, 'nacos-sdk-rust-binding-node.freebsd-x64.node'))
150
+ try {
151
+ if (localFileExisted) {
152
+ nativeBinding = require('./nacos-sdk-rust-binding-node.freebsd-x64.node')
153
+ } else {
154
+ nativeBinding = require('nacos-sdk-rust-binding-node-freebsd-x64')
155
+ }
156
+ } catch (e) {
157
+ loadError = e
158
+ }
159
+ break
160
+ case 'linux':
161
+ switch (arch) {
162
+ case 'x64':
163
+ if (isMusl()) {
164
+ localFileExisted = existsSync(
165
+ join(__dirname, 'nacos-sdk-rust-binding-node.linux-x64-musl.node')
166
+ )
167
+ try {
168
+ if (localFileExisted) {
169
+ nativeBinding = require('./nacos-sdk-rust-binding-node.linux-x64-musl.node')
170
+ } else {
171
+ nativeBinding = require('nacos-sdk-rust-binding-node-linux-x64-musl')
172
+ }
173
+ } catch (e) {
174
+ loadError = e
175
+ }
176
+ } else {
177
+ localFileExisted = existsSync(
178
+ join(__dirname, 'nacos-sdk-rust-binding-node.linux-x64-gnu.node')
179
+ )
180
+ try {
181
+ if (localFileExisted) {
182
+ nativeBinding = require('./nacos-sdk-rust-binding-node.linux-x64-gnu.node')
183
+ } else {
184
+ nativeBinding = require('nacos-sdk-rust-binding-node-linux-x64-gnu')
185
+ }
186
+ } catch (e) {
187
+ loadError = e
188
+ }
189
+ }
190
+ break
191
+ case 'arm64':
192
+ if (isMusl()) {
193
+ localFileExisted = existsSync(
194
+ join(__dirname, 'nacos-sdk-rust-binding-node.linux-arm64-musl.node')
195
+ )
196
+ try {
197
+ if (localFileExisted) {
198
+ nativeBinding = require('./nacos-sdk-rust-binding-node.linux-arm64-musl.node')
199
+ } else {
200
+ nativeBinding = require('nacos-sdk-rust-binding-node-linux-arm64-musl')
201
+ }
202
+ } catch (e) {
203
+ loadError = e
204
+ }
205
+ } else {
206
+ localFileExisted = existsSync(
207
+ join(__dirname, 'nacos-sdk-rust-binding-node.linux-arm64-gnu.node')
208
+ )
209
+ try {
210
+ if (localFileExisted) {
211
+ nativeBinding = require('./nacos-sdk-rust-binding-node.linux-arm64-gnu.node')
212
+ } else {
213
+ nativeBinding = require('nacos-sdk-rust-binding-node-linux-arm64-gnu')
214
+ }
215
+ } catch (e) {
216
+ loadError = e
217
+ }
218
+ }
219
+ break
220
+ case 'arm':
221
+ localFileExisted = existsSync(
222
+ join(__dirname, 'nacos-sdk-rust-binding-node.linux-arm-gnueabihf.node')
223
+ )
224
+ try {
225
+ if (localFileExisted) {
226
+ nativeBinding = require('./nacos-sdk-rust-binding-node.linux-arm-gnueabihf.node')
227
+ } else {
228
+ nativeBinding = require('nacos-sdk-rust-binding-node-linux-arm-gnueabihf')
229
+ }
230
+ } catch (e) {
231
+ loadError = e
232
+ }
233
+ break
234
+ default:
235
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
236
+ }
237
+ break
238
+ default:
239
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
240
+ }
241
+
242
+ if (!nativeBinding) {
243
+ if (loadError) {
244
+ throw loadError
245
+ }
246
+ throw new Error(`Failed to load native binding`)
247
+ }
248
+
249
+ const { sum, NacosConfigClient } = nativeBinding
250
+
251
+ module.exports.sum = sum
252
+ module.exports.NacosConfigClient = NacosConfigClient
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "nacos-sdk-rust-binding-node",
3
+ "version": "0.0.1-ALPHA",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "napi": {
7
+ "name": "nacos-sdk-rust-binding-node",
8
+ "triples": {
9
+ "additional": [
10
+ "aarch64-apple-darwin",
11
+ "aarch64-linux-android",
12
+ "aarch64-unknown-linux-gnu",
13
+ "aarch64-unknown-linux-musl",
14
+ "aarch64-pc-windows-msvc",
15
+ "armv7-unknown-linux-gnueabihf",
16
+ "x86_64-unknown-linux-musl",
17
+ "x86_64-unknown-freebsd",
18
+ "i686-pc-windows-msvc",
19
+ "armv7-linux-androideabi",
20
+ "universal-apple-darwin"
21
+ ]
22
+ }
23
+ },
24
+ "license": "MIT",
25
+ "devDependencies": {
26
+ "@napi-rs/cli": "^2.14.6",
27
+ "ava": "^5.1.1"
28
+ },
29
+ "ava": {
30
+ "timeout": "3m"
31
+ },
32
+ "engines": {
33
+ "node": ">= 10"
34
+ },
35
+ "scripts": {
36
+ "artifacts": "napi artifacts",
37
+ "build": "napi build --platform --release",
38
+ "build:debug": "napi build --platform",
39
+ "prepublishOnly": "napi prepublish -t npm",
40
+ "test": "ava",
41
+ "universal": "napi universal",
42
+ "version": "napi version"
43
+ },
44
+ "packageManager": "yarn@3.4.1",
45
+ "optionalDependencies": {
46
+ "nacos-sdk-rust-binding-node-win32-x64-msvc": "0.0.1-ALPHA",
47
+ "nacos-sdk-rust-binding-node-darwin-x64": "0.0.1-ALPHA",
48
+ "nacos-sdk-rust-binding-node-linux-x64-gnu": "0.0.1-ALPHA",
49
+ "nacos-sdk-rust-binding-node-darwin-arm64": "0.0.1-ALPHA",
50
+ "nacos-sdk-rust-binding-node-android-arm64": "0.0.1-ALPHA",
51
+ "nacos-sdk-rust-binding-node-linux-arm64-gnu": "0.0.1-ALPHA",
52
+ "nacos-sdk-rust-binding-node-linux-arm64-musl": "0.0.1-ALPHA",
53
+ "nacos-sdk-rust-binding-node-win32-arm64-msvc": "0.0.1-ALPHA",
54
+ "nacos-sdk-rust-binding-node-linux-arm-gnueabihf": "0.0.1-ALPHA",
55
+ "nacos-sdk-rust-binding-node-linux-x64-musl": "0.0.1-ALPHA",
56
+ "nacos-sdk-rust-binding-node-freebsd-x64": "0.0.1-ALPHA",
57
+ "nacos-sdk-rust-binding-node-win32-ia32-msvc": "0.0.1-ALPHA",
58
+ "nacos-sdk-rust-binding-node-android-arm-eabi": "0.0.1-ALPHA",
59
+ "nacos-sdk-rust-binding-node-darwin-universal": "0.0.1-ALPHA"
60
+ }
61
+ }
package/src/lib.rs ADDED
@@ -0,0 +1,160 @@
1
+ #![deny(clippy::all)]
2
+
3
+ #[macro_use]
4
+ extern crate napi_derive;
5
+
6
+ use napi::{
7
+ bindgen_prelude::*, threadsafe_function::*, JsGlobal, JsNull, JsObject,
8
+ JsUndefined, Property,
9
+ };
10
+ use std::sync::Arc;
11
+
12
+ #[napi]
13
+ pub fn sum(a: i32, b: i32) -> i32 {
14
+ a + b
15
+ }
16
+
17
+ static INIT_ONCE: std::sync::Once = std::sync::Once::new();
18
+
19
+ /// Client api of Nacos Config.
20
+ #[napi]
21
+ pub struct NacosConfigClient {
22
+ inner: Box<dyn nacos_sdk::api::config::ConfigService>,
23
+ }
24
+
25
+ #[napi]
26
+ impl NacosConfigClient {
27
+ #[napi(constructor)]
28
+ pub fn new(
29
+ server_addr: String,
30
+ namespace: String,
31
+ app_name: String,
32
+ username: Option<String>,
33
+ password: Option<String>,
34
+ ) -> Result<NacosConfigClient> {
35
+ // print to console or file
36
+ INIT_ONCE.call_once(|| {
37
+ let home_dir = match std::env::var("HOME") {
38
+ Ok(dir) => dir,
39
+ Err(_) => "/tmp".to_string(),
40
+ };
41
+ let file_appender = tracing_appender::rolling::daily(home_dir + "/logs/nacos", "nacos.log");
42
+ let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
43
+
44
+ tracing_subscriber::fmt()
45
+ .with_writer(non_blocking)
46
+ .with_thread_names(true)
47
+ .with_file(true)
48
+ .with_level(true)
49
+ .with_line_number(true)
50
+ .with_thread_ids(true)
51
+ .with_max_level(tracing_subscriber::filter::LevelFilter::INFO)
52
+ .init();
53
+ });
54
+
55
+ // enable_auth_plugin_http with username & password
56
+ if username.is_some() && password.is_some() {
57
+ let props = nacos_sdk::api::props::ClientProps::new()
58
+ .server_addr(server_addr)
59
+ .namespace(namespace)
60
+ .app_name(app_name)
61
+ .auth_username(username.unwrap())
62
+ .auth_password(password.unwrap());
63
+
64
+ let config_service = nacos_sdk::api::config::ConfigServiceBuilder::new(props)
65
+ .enable_auth_plugin_http()
66
+ .build()
67
+ .map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
68
+
69
+ Ok(NacosConfigClient {
70
+ inner: Box::new(config_service),
71
+ })
72
+ } else {
73
+ let props = nacos_sdk::api::props::ClientProps::new()
74
+ .server_addr(server_addr)
75
+ .namespace(namespace)
76
+ .app_name(app_name);
77
+
78
+ let config_service = nacos_sdk::api::config::ConfigServiceBuilder::new(props)
79
+ .build()
80
+ .map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
81
+
82
+ Ok(NacosConfigClient {
83
+ inner: Box::new(config_service),
84
+ })
85
+ }
86
+ }
87
+
88
+ /// Get a NacosConfigResponse from server.
89
+ #[napi]
90
+ pub fn get_config(&mut self, data_id: String, group: String) -> Result<NacosConfigResponse> {
91
+ let config_resp = self.inner.get_config(data_id, group)
92
+ .map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
93
+
94
+ Ok(transfer_conf_resp(config_resp))
95
+ }
96
+
97
+ /// Add a NacosConfigChangeListener callback func, which listen the config change.
98
+ #[napi]
99
+ pub fn add_listener(
100
+ &mut self,
101
+ data_id: String,
102
+ group: String,
103
+ listener: ThreadsafeFunction<NacosConfigResponse>,
104
+ ) -> Result<()> {
105
+ self
106
+ .inner
107
+ .add_listener(
108
+ data_id,
109
+ group,
110
+ Arc::new(NacosConfigChangeListener {
111
+ func: Arc::new(listener),
112
+ }),
113
+ )
114
+ .map_err(|nacos_err| Error::from_reason(nacos_err.to_string()))?;
115
+ Ok(())
116
+ }
117
+ }
118
+
119
+ #[napi(object)]
120
+ pub struct NacosConfigResponse {
121
+ /// Namespace/Tenant
122
+ pub namespace: String,
123
+ /// DataId
124
+ pub data_id: String,
125
+ /// Group
126
+ pub group: String,
127
+ /// Content
128
+ pub content: String,
129
+ /// Content's Type; e.g. json,properties,xml,html,text,yaml
130
+ pub content_type: String,
131
+ /// Content's md5
132
+ pub md5: String,
133
+ }
134
+
135
+ pub struct NacosConfigChangeListener {
136
+ func: Arc<ThreadsafeFunction<NacosConfigResponse>>,
137
+ }
138
+
139
+ fn transfer_conf_resp(config_resp: nacos_sdk::api::config::ConfigResponse) -> NacosConfigResponse {
140
+ NacosConfigResponse {
141
+ namespace: config_resp.namespace().to_string(),
142
+ data_id: config_resp.data_id().to_string().to_string(),
143
+ group: config_resp.group().to_string(),
144
+ content: config_resp.content().to_string(),
145
+ content_type: config_resp.content_type().to_string(),
146
+ md5: config_resp.md5().to_string(),
147
+ }
148
+ }
149
+
150
+ impl nacos_sdk::api::config::ConfigChangeListener for NacosConfigChangeListener {
151
+ fn notify(&self, config_resp: nacos_sdk::api::config::ConfigResponse) {
152
+ let listen = self.func.clone();
153
+
154
+ let conf_resp = transfer_conf_resp(config_resp);
155
+
156
+ std::thread::spawn(move || {
157
+ listen.call(Ok(conf_resp), ThreadsafeFunctionCallMode::NonBlocking);
158
+ });
159
+ }
160
+ }