@certik/skynet 0.10.61 → 0.10.62

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.62
4
+
5
+ - Added: opensearch helpers
6
+
3
7
  ## 0.10.61
4
8
 
5
9
  - Added dns library
@@ -0,0 +1,16 @@
1
+ import { Client, ClientOptions } from "@opensearch-project/opensearch";
2
+
3
+ export declare function newOpensearchClient(
4
+ endpoint: string,
5
+ key: string,
6
+ secret: string,
7
+ options?: ClientOptions
8
+ ): Client;
9
+
10
+ export declare async function sendToOpensearch(
11
+ client: Client,
12
+ appName: string,
13
+ index: string,
14
+ record: string,
15
+ indexSuffixes?: string[]
16
+ ): Promise<{ status: string; result: Object }>;
package/opensearch.js ADDED
@@ -0,0 +1,39 @@
1
+ const { Client } = require("@opensearch-project/opensearch");
2
+
3
+ // newOpensearchClient creates a new opensearch client with the given endpoint, key, secret and extra opensearch client options.
4
+ function newOpensearchClient(endpoint, key, secret, options = {}) {
5
+ return new Client({
6
+ node: endpoint,
7
+ auth: {
8
+ username: key,
9
+ password: secret,
10
+ },
11
+ ...options,
12
+ });
13
+ }
14
+
15
+ // sendToOpensearch sends a record object to the given index using the given opensearch client. An appName is also attached to the record
16
+ // as an identifier for who sends the record. The indexSuffixes is an array of strings that will be appended to the index name using `-`.
17
+ // A typical usage of indexSuffixes could be the date e.g. ["2023", "08"] so that new indexes are created on the monthly basis to prevent
18
+ // the index from growing too large.
19
+ // Returns an object with status and result properties. The status is either "ok" or "error". The result is the response from opensearch
20
+ // if the status is "ok" or the error message if the status is "error".
21
+ async function sendToOpensearch(client, appName, index, record, indexSuffixes = []) {
22
+ try {
23
+ const result = await client.index({
24
+ index: indexSuffixes ? [index, ...indexSuffixes].join("-") : index,
25
+ body: { app: appName, timestamp: new Date(), record },
26
+ });
27
+ if (result.statusCode === 201) {
28
+ return { status: "ok", result };
29
+ }
30
+ return { status: "error", result };
31
+ } catch (err) {
32
+ return { status: "error", result: err.stack ? err.stack : err };
33
+ }
34
+ }
35
+
36
+ module.exports = {
37
+ newOpensearchClient,
38
+ sendToOpensearch,
39
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.10.61",
3
+ "version": "0.10.62",
4
4
  "description": "Skynet Shared JS library",
5
5
  "main": "index.js",
6
6
  "author": "CertiK Engineering",
@@ -12,6 +12,7 @@
12
12
  "node": ">= 14"
13
13
  },
14
14
  "dependencies": {
15
+ "@opensearch-project/opensearch": "^2.3.1",
15
16
  "@slack/web-api": "^6.4.0",
16
17
  "ably": "^1.2.22",
17
18
  "abort-controller": "^3.0.0",
@@ -35,4 +36,4 @@
35
36
  "sinon": "^14.0.0"
36
37
  },
37
38
  "license": "MIT"
38
- }
39
+ }