@certik/skynet 0.10.61 → 0.10.63

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,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.63
4
+
5
+ - Refactored: added verbose conclusive log in `exponentialRetry()`
6
+
7
+ ## 0.10.62
8
+
9
+ - Added: opensearch helpers
10
+
3
11
  ## 0.10.61
4
12
 
5
13
  - Added dns library
package/availability.js CHANGED
@@ -1,5 +1,5 @@
1
1
  async function wait(time) {
2
- return new Promise(resolve => {
2
+ return new Promise((resolve) => {
3
3
  setTimeout(resolve, time);
4
4
  });
5
5
  }
@@ -22,10 +22,7 @@ async function retry(times, verbose, func) {
22
22
  }
23
23
  }
24
24
 
25
- async function exponentialRetry(
26
- func,
27
- { maxRetry, initialDuration, growFactor, test, verbose }
28
- ) {
25
+ async function exponentialRetry(func, { maxRetry, initialDuration, growFactor, test, verbose }) {
29
26
  let retries = maxRetry;
30
27
  let duration = initialDuration || 5000;
31
28
  let growFactorFinal = growFactor || 2;
@@ -37,9 +34,7 @@ async function exponentialRetry(
37
34
  console.log("failed attempt result", result);
38
35
  }
39
36
 
40
- console.log(
41
- `sleep for ${duration}s after failed attempt, remaining ${retries} attempts`
42
- );
37
+ console.log(`sleep for ${duration}ms after failed attempt, remaining ${retries} attempts`);
43
38
  retries = retries - 1;
44
39
 
45
40
  await wait(duration);
@@ -48,11 +43,16 @@ async function exponentialRetry(
48
43
  duration = duration * growFactorFinal;
49
44
  }
50
45
 
46
+ if (verbose) {
47
+ console.log(`function to retry ends with status ${test(result)}, number of retries done: ${maxRetry - retries}}`);
48
+ console.log("final result", result);
49
+ }
50
+
51
51
  return result;
52
52
  }
53
53
 
54
54
  module.exports = {
55
55
  wait,
56
56
  retry,
57
- exponentialRetry
57
+ exponentialRetry,
58
58
  };
@@ -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.63",
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
+ }