Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nodefony/elastic-bundle

Package Overview
Dependencies
Maintainers
1
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nodefony/elastic-bundle - npm Package Compare versions

Comparing version 5.0.9 to 5.1.0

tests/connectionsTest.js

6

package.json
{
"name": "@nodefony/elastic-bundle",
"version": "5.0.9",
"version": "5.1.0",
"description": "Nodefony Framework Bundle elasticsearch ",

@@ -15,7 +15,7 @@ "author": "admin admin@nodefony.com",

"dependencies": {
"elasticsearch": "^16.4.0",
"@elastic/elasticsearch": "^7.4.0",
"shortid": "^2.2.15"
},
"devDependencies": {
"node-pre-gyp": "^0.13.0"
"node-pre-gyp": "^0.14.0"
},

@@ -22,0 +22,0 @@ "license": "CECILL-B",

@@ -29,3 +29,3 @@ # Welcome to elastic-bundle

*
* options : https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html
* options : https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-configuration.html
*

@@ -35,5 +35,2 @@ */

elasticsearch: {
globalHostsOptions: {
protocol: "http"
},
globalOptions: {

@@ -48,12 +45,18 @@ ssl: {

main: {
hosts: [{
host: "192.168.100.181",
port: 9200
}],
sniffOnStart: true,
sniffInterval: 5000
name: "main",
nodes: ["http://localhost:9200"],
log: {
request: false,
response: false,
sniff: true,
resurrect: true
},
maxRetries:10,
sniffInterval: 5000,
pingTimeout: 5000,
requestTimeout:5000
}
}
}
}
},
```

@@ -63,29 +66,48 @@

#### <code>./controller/defaultController.js</code>
#### <code>./controller/elasticController.js</code>
```js
/**
* @Route ("/elastic")
* https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html
* @Route ("/test/elastic")
*/
module.exports = class defaultController extends nodefony.controller {
module.exports = class elasticController extends nodefony.controller {
constructor(container, context) {
super(container, context);
this.client = this.get("elastic").getClient("main");
this.elastic = this.get("elastic");
this.setJsonContext();
}
health(params = {}) {
return this.client.cluster.health(params);
async getClient() {
let conn = await this.elastic.getConnection("main");
return conn.client;
}
create() {
return this.client.indices.create({
index: 'nodefony'
async createIndex(index, id) {
if (!index) {
index = "nodefony";
}
const client = await this.getClient();
const {
body
} = await client.indices.exists({
index: index
});
let res = null;
if (!body) {
res = await client.index({
index: index,
id: id,
body: {
version: this.kernel.version,
package: this.kernel.package
}
});
} else {
this.log(`Index : ${index} Already exist !!! `);
}
return res;
}
ping() {
return this.client.ping({
requestTimeout: 30000,
});
async ping() {
return await this.client.ping();
}

@@ -95,18 +117,30 @@

* @Method ({"GET"})
* @Route (name="elastic-index")
* @Route ("/index/{id}" , name="test-elastic-search", defaults={"id" = 1},requirements={"id" = "\d+")
*
*/
indexAction() {
return this.ping()
.then(this.health.bind(this))
.then((ele) => {
return this.render("cci-bundle::index.html.twig", {
name: `Elastic Ping : ${ele}`
}).catch((e) => {
async indexAction(id) {
const client = await this.getClient();
await this.createIndex("nodefony", "id");
let get = null ;
try{
get = await client.get({
index: 'nodefony',
id: id
});
return this.renderJson(get)
.catch((e) => {
throw e;
});
}).catch((e) => {
throw e;
});
}catch(e){
if ( e && e.meta && e.meta.statusCode ){
return this.renderJson(e, e.meta.statusCode )
.catch((e) => {
throw e;
});
}
return this.renderJson(e )
.catch((e) => {
throw e;
});
}
}

@@ -116,3 +150,2 @@ };

## <a name="authors"></a>Authors

@@ -119,0 +152,0 @@

@@ -10,2 +10,2 @@ module.exports = {

}
};
};

@@ -1,4 +0,27 @@

const elasticsearch = require('elasticsearch');
const logger = require(path.resolve(__dirname, "..", "src", "elasticLogger.js"));
const elasticsearch = require('@elastic/elasticsearch');
const { Client } = require('@elastic/elasticsearch');
/*const {
ConnectionPool,
Connection
} = require('@elastic/elasticsearch');
class MyConnectionPool extends ConnectionPool {
markAlive(connection) {
// your code
kernel.log(connection, "DEBUG", "ELASTIC POOL CONNECTION");
super.markAlive(connection);
}
}
class MyConnection extends Connection {
request(params, callback) {
// your code
kernel.log(params, "DEBUG", "ELASTIC CONNECTION");
}
}*/
//const { events } = require('@elastic/elasticsearch')
//console.log(events)
//const { errors } = require('@elastic/elasticsearch');
//console.log(errors);
class elesticConnection extends nodefony.Service {

@@ -13,3 +36,7 @@ constructor(name, options = {}, service = null) {

if (!msgid) {
msgid = `\x1b[36mELASTICSEARCH CLIENT ${this.name} \x1b[0m`;
if (msg) {
msgid = `\x1b[36mELASTICSEARCH CLIENT ${this.name} ${msg} \x1b[0m`;
} else {
msgid = `\x1b[36mELASTICSEARCH CLIENT ${this.name}\x1b[0m`;
}
}

@@ -23,8 +50,46 @@ return super.logger(pci, severity, msgid, msg);

this.settings = nodefony.extend({}, this.settings, options);
if (!this.settings.log) {
this.settings.log = logger;
this.settings.log.prototype.logger = this.logger.bind(this);
this.client = new Client(this.settings);
if (this.settings.log) {
//this.settings.log = logger;
//this.settings.log.prototype.logger = this.logger.bind(this);
//this.settings.ConnectionPool = MyConnectionPool;
//this.settings.Connection = MyConnection;
if (this.settings.log.request) {
this.client.on('request', (err, result) => {
if (err) {
this.logger(err, "ERROR");
this.logger(err.meta, "ERROR");
return err;
}
this.logger(result.meta.request, "DEBUG", null, "REQUEST");
});
}
if (this.settings.log.response) {
this.client.on('response', (err, result) => {
if (err) {
this.logger(err, "ERROR");
return;
}
this.logger(result, "DEBUG", null, "RESPONSE");
});
}
if (this.settings.log.sniff) {
this.client.on('sniff', (err, result) => {
if (err) {
this.logger(err, "ERROR");
return;
}
this.logger(`${JSON.stringify(result.meta.sniff,null, '\t')}`, "DEBUG", null, "SNIFF");
});
}
if (this.settings.log.resurrect) {
this.client.on('resurrect', (err, result) => {
if (err) {
this.logger(err, "ERROR");
return;
}
this.logger(result, "INFO", null, "RESURRECT");
});
}
}
this.client = new this.service.engine.Client(this.settings);
this.hosts = this.client.transport._config.hosts;
return resolve(this.client);

@@ -47,7 +112,4 @@ } catch (e) {

let myOpt = {};
switch (nodefony.typeOf(connection.hosts)) {
switch (nodefony.typeOf(connection.nodes)) {
case "array":
for (let i = 0; i < connection.hosts.length; i++) {
connection.hosts[i] = nodefony.extend({}, this.globalHostsOptions, connection.hosts[i]);
}
nodefony.extend(myOpt, this.globalOptions, connection);

@@ -70,29 +132,60 @@ break;

readConfig() {
getConnection(name) {
return new Promise((resolve, reject) => {
if (name in this.connections) {
return resolve(super.getConnection(name));
} else {
this.on("connection", (connection) => {
if (connection) {
return resolve(connection);
}
return reject(new Error("No connection : " + name));
});
}
});
}
async readConfig() {
this.settings = this.bundle.settings.elasticsearch;
this.globalOptions = this.settings.globalOptions;
this.globalHostsOptions = this.settings.globalHostsOptions;
//this.globalHostsOptions = this.settings.globalHostsOptions;
for (let connection in this.settings.connections) {
let options = this.setOptions(this.settings.connections[connection]);
this.createConnection(connection, options)
.then((conn) => {
return conn.client.ping({
requestTimeout: 3000
})
.then(() => {
this.logger("PING OK", "INFO");
this.displayTable(conn, "INFO");
return conn;
})
.catch(e => {
this.logger(e, "ERROR");
throw e;
});
}).catch((e) => {
this.logger(e, "ERROR");
throw e;
});
try {
let options = this.setOptions(this.settings.connections[connection]);
let conn = await this.createConnection(connection, options);
this.logger(`Ping Elastic connection`, "INFO");
let ping = await conn.client.ping();
this.logger(ping, "DEBUG", );
this.logger(`Info Elastic connection`, "INFO");
let info = await conn.client.info();
this.logger(info, "DEBUG");
} catch (e) {
this.logger(e, "ERROR");
continue;
}
}
this.displayTable(this.connections, "INFO");
}
async displayTable(connections, severity = "DEBUG") {
let options = {
head: [
`${this.name.toUpperCase()} CONNECTIONS NAME`,
"HOST"
]
};
try {
let table = this.kernel.cli.displayTable(null, options);
let data = [];
for (let connection in connections) {
data.push(connection || "");
data.push(connections[connection].client.name || "");
}
table.push(data);
this.logger(`${this.name} Connections : \n${table.toString()}`, severity);
} catch (e) {
throw e;
}
}
readCertificates(ssl) {

@@ -134,2 +227,2 @@ let certif = nodefony.extend({}, ssl);

}
};
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc