@certik/skynet 0.10.60 → 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,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.62
4
+
5
+ - Added: opensearch helpers
6
+
7
+ ## 0.10.61
8
+
9
+ - Added dns library
10
+
3
11
  ## 0.10.60
4
12
 
5
13
  - Fixed: print skynet API route error stack trace
package/dns.d.ts ADDED
@@ -0,0 +1 @@
1
+ export function hasTxtRecord(domainName: string, txtRecord: string): Promise<boolean>;
package/dns.js ADDED
@@ -0,0 +1,29 @@
1
+ // https://nodejs.org/api/dns.html#dns
2
+ const dns = require("dns").promises;
3
+
4
+ async function hasTxtRecord(domainName, txtRecord) {
5
+ let nsAddresses;
6
+ // get all nameservers for the domain
7
+ const nameservers = await dns.resolveNs(domainName);
8
+
9
+ // get default nameserver addresses
10
+ const defaultServers = dns.getServers();
11
+
12
+ // iterate over nameservers to see if they have the TXT record
13
+ for (const ns of nameservers) {
14
+ //reset serverAddresses to get nameserver addresses
15
+ dns.setServers(defaultServers);
16
+
17
+ // get nameserver addresses and use them to resolve domain's TXT record, in order to skip caches in DNS servers.
18
+ nsAddresses = await dns.resolve(ns);
19
+ dns.setServers(nsAddresses);
20
+ const txtRecords = await dns.resolveTxt(domainName);
21
+
22
+ if (txtRecords && txtRecords.length > 0 && txtRecords.find((txt) => txt.indexOf(txtRecord) > -1)) {
23
+ return true;
24
+ }
25
+ }
26
+ return false;
27
+ }
28
+
29
+ module.exports = { hasTxtRecord };
@@ -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.60",
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",
@@ -30,9 +31,9 @@
30
31
  },
31
32
  "devDependencies": {
32
33
  "ava": "^5.0.1",
33
- "eslint": "^8.18.0",
34
- "eslint-plugin-import": "^2.26.0",
34
+ "eslint": "^8.39.0",
35
+ "eslint-plugin-import": "^2.27.5",
35
36
  "sinon": "^14.0.0"
36
37
  },
37
38
  "license": "MIT"
38
- }
39
+ }