@blockcore/dns 0.0.5 → 0.0.8

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/README.md CHANGED
@@ -30,20 +30,18 @@ npm install @blockcore/dns
30
30
  ```ts
31
31
  import { BlockcoreDns } from '@blockcore/dns';
32
32
 
33
- let dns = new BlockcoreDns();
34
- let servers = await dns.getDnsServers();
33
+ let dns = new BlockcoreDns('https://ns.blockcore.net');
34
+ let indexers = await dns.getServicesByType('Indexer');
35
35
  ```
36
36
 
37
- After getting the servers, either you can loop all of them or pick a single one:
37
+ You can retrieve the public known nameservers and use those instead of manually specifying server:
38
38
 
39
39
  ```ts
40
- let server = servers[0];
41
-
42
- dns.setActiveServer(dnsServer.url);
40
+ let dnsServers = await BlockcoreDns.getDnsServers();
41
+ dns.setActiveServer(dnsServers[0].url);
43
42
 
44
43
  await dns.getServicesByType('Indexer');
45
44
  await dns.getServicesByNetwork('CITY');
46
45
  await dns.getServicesByTypeAndNetwork('Indexer', 'CITY');
47
46
  await dns.getExternalIP();
48
47
  ```
49
-
@@ -1,12 +1,16 @@
1
1
  import { DnsListEntry, ServiceListEntry } from './types.js';
2
+ /** The BlockcoreDns class will give you access to all known nameservers and all services registered with those nameservers. */
2
3
  export declare class BlockcoreDns {
3
- /** Call this to have data to provide an instance of the Blockcore DNS. */
4
- static getDnsServers(): Promise<DnsListEntry[]>;
5
- private activeServer?;
6
- constructor(nameserver: string);
7
- setActiveServer(url: string): void;
8
- getServicesByType(service: 'Indexer' | 'Explorer'): Promise<ServiceListEntry[]>;
9
- getServicesByTypeAndNetwork(service: 'Indexer' | 'Explorer', symbol: string): Promise<ServiceListEntry[]>;
10
- getServicesByNetwork(symbol: string): Promise<ServiceListEntry[]>;
11
- getExternalIP(): Promise<string>;
4
+ private nameservers;
5
+ private services;
6
+ private dns;
7
+ constructor();
8
+ private getDnsServers;
9
+ getNameServers(): DnsListEntry[];
10
+ getOnlineServicesByNetwork(network: string): ServiceListEntry[];
11
+ getServices(): ServiceListEntry[];
12
+ /** Attempts to load the latest status of all services from all known nameservers. This method can be called
13
+ * at intervals to ensure latest status is available.
14
+ */
15
+ load(): Promise<void>;
12
16
  }
@@ -1,37 +1,57 @@
1
1
  import { WebRequest } from './Request.js';
2
+ import { BlockcoreDnsClient } from './BlockcoreDnsClient.js';
3
+ /** The BlockcoreDns class will give you access to all known nameservers and all services registered with those nameservers. */
2
4
  export class BlockcoreDns {
3
- constructor(nameserver) {
4
- Object.defineProperty(this, "activeServer", {
5
+ constructor() {
6
+ Object.defineProperty(this, "nameservers", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: []
11
+ });
12
+ Object.defineProperty(this, "services", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: []
17
+ });
18
+ Object.defineProperty(this, "dns", {
5
19
  enumerable: true,
6
20
  configurable: true,
7
21
  writable: true,
8
22
  value: void 0
9
23
  });
10
- this.setActiveServer(nameserver);
24
+ this.dns = new BlockcoreDnsClient('');
11
25
  }
12
- /** Call this to have data to provide an instance of the Blockcore DNS. */
13
- static async getDnsServers() {
26
+ async getDnsServers() {
14
27
  const url = `https://chains.blockcore.net/services/DNS.json`;
15
28
  const servers = await WebRequest.fetchJson(url);
16
29
  return servers;
17
30
  }
18
- setActiveServer(url) {
19
- this.activeServer = url;
20
- }
21
- async getServicesByType(service) {
22
- const url = `${this.activeServer}/api/dns/services/service/${service}`;
23
- return await WebRequest.fetchJson(url);
31
+ getNameServers() {
32
+ return this.nameservers;
24
33
  }
25
- async getServicesByTypeAndNetwork(service, symbol) {
26
- const url = `${this.activeServer}/api/dns/services/symbol/${symbol}/service/${service}`;
27
- return await WebRequest.fetchJson(url);
34
+ getOnlineServicesByNetwork(network) {
35
+ return this.services.filter((s) => s.symbol === network && s.online === true);
28
36
  }
29
- async getServicesByNetwork(symbol) {
30
- const url = `${this.activeServer}/api/dns/services/symbol/${symbol}`;
31
- return await WebRequest.fetchJson(url);
37
+ getServices() {
38
+ return this.services;
32
39
  }
33
- async getExternalIP() {
34
- const url = `${this.activeServer}/api/dns/ipaddress`;
35
- return await WebRequest.fetchText(url);
40
+ /** Attempts to load the latest status of all services from all known nameservers. This method can be called
41
+ * at intervals to ensure latest status is available.
42
+ */
43
+ async load() {
44
+ this.nameservers = await this.getDnsServers();
45
+ const servicesMap = new Map();
46
+ for (let i = 0; i < this.nameservers.length; i++) {
47
+ const nameserver = this.nameservers[i];
48
+ if (!nameserver) {
49
+ continue;
50
+ }
51
+ this.dns.setActiveServer(nameserver.url);
52
+ const services = await this.dns.getServicesByType('Indexer');
53
+ services.forEach((item) => servicesMap.set(item.domain, { ...servicesMap.get(item.domain), ...item }));
54
+ }
55
+ this.services = Array.from(servicesMap.values());
36
56
  }
37
57
  }
@@ -0,0 +1,10 @@
1
+ import { ServiceListEntry } from './types.js';
2
+ export declare class BlockcoreDnsClient {
3
+ private activeServer?;
4
+ constructor(nameserver: string);
5
+ setActiveServer(url: string): void;
6
+ getServicesByType(service: 'Indexer' | 'Explorer'): Promise<ServiceListEntry[]>;
7
+ getServicesByTypeAndNetwork(service: 'Indexer' | 'Explorer', symbol: string): Promise<ServiceListEntry[]>;
8
+ getServicesByNetwork(symbol: string): Promise<ServiceListEntry[]>;
9
+ getExternalIP(): Promise<string>;
10
+ }
@@ -0,0 +1,31 @@
1
+ import { WebRequest } from './Request.js';
2
+ export class BlockcoreDnsClient {
3
+ constructor(nameserver) {
4
+ Object.defineProperty(this, "activeServer", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ this.setActiveServer(nameserver);
11
+ }
12
+ setActiveServer(url) {
13
+ this.activeServer = url;
14
+ }
15
+ async getServicesByType(service) {
16
+ const url = `${this.activeServer}/api/dns/services/service/${service}`;
17
+ return await WebRequest.fetchJson(url);
18
+ }
19
+ async getServicesByTypeAndNetwork(service, symbol) {
20
+ const url = `${this.activeServer}/api/dns/services/symbol/${symbol}/service/${service}`;
21
+ return await WebRequest.fetchJson(url);
22
+ }
23
+ async getServicesByNetwork(symbol) {
24
+ const url = `${this.activeServer}/api/dns/services/symbol/${symbol}`;
25
+ return await WebRequest.fetchJson(url);
26
+ }
27
+ async getExternalIP() {
28
+ const url = `${this.activeServer}/api/dns/ipaddress`;
29
+ return await WebRequest.fetchText(url);
30
+ }
31
+ }
package/lib/Request.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Response } from 'node-fetch';
2
1
  export declare class WebRequest {
3
2
  static fetchText(url: string): Promise<string>;
4
3
  static fetchJson<T>(url: string): Promise<T>;
package/lib/Request.js CHANGED
@@ -1,4 +1,3 @@
1
- import fetch from 'node-fetch';
2
1
  export class WebRequest {
3
2
  static async fetchText(url) {
4
3
  const response = await WebRequest.fetchUrl(url);
@@ -11,12 +10,9 @@ export class WebRequest {
11
10
  static async fetchUrl(url) {
12
11
  return await fetch(url, {
13
12
  method: 'GET',
14
- // mode: 'cors',
15
- // cache: 'no-cache',
16
- // credentials: 'same-origin',
17
- headers: {
18
- 'Content-Type': 'application/json',
19
- },
13
+ mode: 'cors',
14
+ cache: 'no-cache',
15
+ credentials: 'omit',
20
16
  redirect: 'follow',
21
17
  referrerPolicy: 'no-referrer',
22
18
  });
package/lib/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './BlockcoreDns.js';
2
+ export * from './Request.js';
2
3
  export * from './types.js';
package/lib/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './BlockcoreDns.js';
2
+ export * from './Request.js';
2
3
  export * from './types.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockcore/dns",
3
- "version": "0.0.5",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "exports": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -8,13 +8,14 @@
8
8
  "lib/**/*"
9
9
  ],
10
10
  "engines": {
11
- "node": ">=14.16"
11
+ "node": ">=18.*"
12
12
  },
13
13
  "scripts": {
14
14
  "build": "tsc",
15
15
  "lint": "eslint .",
16
16
  "test": "npm run lint && ava",
17
- "version": "node -p \"require('./package.json').version\""
17
+ "version": "node -p \"require('./package.json').version\"",
18
+ "changelog": "conventional-changelog -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
18
19
  },
19
20
  "repository": {
20
21
  "type": "git",
@@ -50,9 +51,5 @@
50
51
  "nodeArguments": [
51
52
  "--loader=ts-node/esm"
52
53
  ]
53
- },
54
- "dependencies": {
55
- "@blockcore/identity": "^0.0.1",
56
- "node-fetch": "^3.2.9"
57
54
  }
58
55
  }