This is an automated email from the ASF dual-hosted git repository.
baodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-node.git
The following commit(s) were added to refs/heads/master by this push:
new a89b64f Use the certificate provided by the node.js (#301)
a89b64f is described below
commit a89b64fffc44cca2663b4caf954839e6cab2a3d8
Author: Baodi Shi <[email protected]>
AuthorDate: Sat Feb 25 15:26:53 2023 +0800
Use the certificate provided by the node.js (#301)
* Use the certificate provided by the node.js.
* Gen cert file when npm install.
* Certificate generation is performed only once
* Fix code reviews.
---
.gitignore | 1 +
index.js => GenCertFile.js | 27 ++----------------
index.js | 3 +-
package.json | 2 +-
src/Client.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 74 insertions(+), 27 deletions(-)
diff --git a/.gitignore b/.gitignore
index 8936209..b6ad8df 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,3 +26,4 @@ report/
*.tgz
pkg/linux/pulsar-cpp/
lib/
+src/cert.pem
diff --git a/index.js b/GenCertFile.js
similarity index 52%
copy from index.js
copy to GenCertFile.js
index 84449cf..99e816c 100644
--- a/index.js
+++ b/GenCertFile.js
@@ -17,29 +17,6 @@
* under the License.
*/
-const PulsarBinding = require('./src/pulsar-binding');
-const AuthenticationTls = require('./src/AuthenticationTls.js');
-const AuthenticationAthenz = require('./src/AuthenticationAthenz.js');
-const AuthenticationToken = require('./src/AuthenticationToken.js');
-const AuthenticationOauth2 = require('./src/AuthenticationOauth2.js');
+const Client = require('./src/Client.js');
-const LogLevel = {
- DEBUG: 0,
- INFO: 1,
- WARN: 2,
- ERROR: 3,
- toString: (level) => Object.keys(LogLevel).find((key) => LogLevel[key] ===
level),
-};
-
-const Pulsar = {
- Client: PulsarBinding.Client,
- Message: PulsarBinding.Message,
- MessageId: PulsarBinding.MessageId,
- AuthenticationTls,
- AuthenticationAthenz,
- AuthenticationToken,
- AuthenticationOauth2,
- LogLevel,
-};
-
-module.exports = Pulsar;
+Client.genCertFile();
diff --git a/index.js b/index.js
index 84449cf..2f5b9e0 100644
--- a/index.js
+++ b/index.js
@@ -22,6 +22,7 @@ const AuthenticationTls =
require('./src/AuthenticationTls.js');
const AuthenticationAthenz = require('./src/AuthenticationAthenz.js');
const AuthenticationToken = require('./src/AuthenticationToken.js');
const AuthenticationOauth2 = require('./src/AuthenticationOauth2.js');
+const Client = require('./src/Client.js');
const LogLevel = {
DEBUG: 0,
@@ -32,7 +33,7 @@ const LogLevel = {
};
const Pulsar = {
- Client: PulsarBinding.Client,
+ Client,
Message: PulsarBinding.Message,
MessageId: PulsarBinding.MessageId,
AuthenticationTls,
diff --git a/package.json b/package.json
index 9ee9783..b2d3c33 100644
--- a/package.json
+++ b/package.json
@@ -9,7 +9,7 @@
"example": "examples"
},
"scripts": {
- "install": "node-pre-gyp install --fallback-to-build",
+ "install": "node-pre-gyp install --fallback-to-build && node
GenCertFile.js",
"configure": "node-pre-gyp configure",
"build": "npm run format && node-pre-gyp build",
"build:debug": "npm run format && node-pre-gyp rebuild --debug",
diff --git a/src/Client.js b/src/Client.js
new file mode 100644
index 0000000..5932e13
--- /dev/null
+++ b/src/Client.js
@@ -0,0 +1,68 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+const fs = require('fs');
+const tls = require('tls');
+const os = require('os');
+const PulsarBinding = require('./pulsar-binding');
+
+const certsFilePath = `${__dirname}/cert.pem`;
+
+class Client {
+ constructor(params) {
+ if (!params.tlsTrustCertsFilePath) {
+ // eslint-disable-next-line no-param-reassign
+ params.tlsTrustCertsFilePath = certsFilePath;
+ }
+ this.client = new PulsarBinding.Client(params);
+ }
+
+ createProducer(params) {
+ return this.client.createProducer(params);
+ }
+
+ subscribe(params) {
+ return this.client.subscribe(params);
+ }
+
+ createReader(params) {
+ return this.client.createReader(params);
+ }
+
+ close() {
+ this.client.close();
+ }
+
+ static setLogHandler(params) {
+ PulsarBinding.Client.setLogHandler(params);
+ }
+
+ static genCertFile() {
+ fs.rmSync(certsFilePath, { force: true });
+ const fd = fs.openSync(certsFilePath, 'a');
+ try {
+ tls.rootCertificates.forEach((cert) => {
+ fs.appendFileSync(fd, cert + os.EOL);
+ });
+ } finally {
+ fs.closeSync(fd);
+ }
+ }
+}
+
+module.exports = Client;