@n1xyz/nord-ts 0.3.2 → 0.3.4
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/dist/actions.js +184 -0
- package/dist/client/Nord.d.ts +9 -15
- package/dist/client/Nord.js +759 -0
- package/dist/client/NordAdmin.js +362 -0
- package/dist/client/NordUser.d.ts +3 -1
- package/dist/client/NordUser.js +752 -0
- package/dist/const.js +27 -0
- package/dist/error.js +51 -0
- package/dist/gen/nord_pb.d.ts +132 -114
- package/dist/gen/nord_pb.js +1068 -0
- package/dist/gen/openapi.d.ts +345 -72
- package/dist/gen/openapi.js +5 -0
- package/dist/index.browser.js +61342 -80207
- package/dist/index.common.js +59722 -87597
- package/dist/index.js +10 -0
- package/dist/nord/api/actions.d.ts +128 -0
- package/dist/nord/api/actions.js +396 -0
- package/dist/nord/api/core.d.ts +16 -0
- package/dist/nord/api/core.js +81 -0
- package/dist/nord/api/metrics.d.ts +67 -0
- package/dist/nord/api/metrics.js +229 -0
- package/dist/nord/api/triggers.d.ts +7 -0
- package/dist/nord/api/triggers.js +38 -0
- package/dist/nord/client/Nord.d.ts +387 -0
- package/dist/nord/client/Nord.js +747 -0
- package/dist/nord/client/NordAdmin.d.ts +226 -0
- package/dist/nord/client/NordAdmin.js +410 -0
- package/dist/nord/client/NordClient.d.ts +16 -0
- package/dist/nord/client/NordClient.js +28 -0
- package/dist/nord/client/NordUser.d.ts +379 -0
- package/dist/nord/client/NordUser.js +787 -0
- package/dist/nord/index.d.ts +8 -0
- package/dist/nord/index.js +34 -0
- package/dist/nord/models/Subscriber.d.ts +37 -0
- package/dist/nord/models/Subscriber.js +25 -0
- package/dist/nord/utils/NordError.d.ts +35 -0
- package/dist/nord/utils/NordError.js +49 -0
- package/dist/types.d.ts +15 -6
- package/dist/types.js +92 -0
- package/dist/utils.js +193 -0
- package/dist/websocket/NordWebSocketClient.d.ts +1 -0
- package/dist/websocket/NordWebSocketClient.js +242 -0
- package/dist/websocket/Subscriber.d.ts +7 -1
- package/dist/websocket/Subscriber.js +24 -0
- package/dist/websocket/events.d.ts +2 -1
- package/dist/websocket/events.js +1 -0
- package/dist/websocket/index.d.ts +1 -1
- package/dist/websocket/index.js +80 -0
- package/package.json +2 -2
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { AggregateMetrics, PeakTpsPeriodUnit } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* Time periods for metrics queries
|
|
4
|
+
*/
|
|
5
|
+
export declare enum MetricPeriod {
|
|
6
|
+
ONE_MINUTE = "1m",
|
|
7
|
+
FIVE_MINUTES = "5m",
|
|
8
|
+
FIFTEEN_MINUTES = "15m",
|
|
9
|
+
ONE_HOUR = "1h",
|
|
10
|
+
FOUR_HOURS = "4h",
|
|
11
|
+
ONE_DAY = "24h",
|
|
12
|
+
ONE_WEEK = "7d"
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Fetch aggregate metrics from the Nord API
|
|
16
|
+
*
|
|
17
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
18
|
+
* @param txPeakTpsPeriod - Period for peak TPS calculation
|
|
19
|
+
* @param txPeakTpsPeriodUnit - Unit for peak TPS period
|
|
20
|
+
* @returns Aggregate metrics
|
|
21
|
+
* @throws {NordError} If the request fails
|
|
22
|
+
*/
|
|
23
|
+
export declare function aggregateMetrics(webServerUrl: string, txPeakTpsPeriod?: number, txPeakTpsPeriodUnit?: PeakTpsPeriodUnit): Promise<AggregateMetrics>;
|
|
24
|
+
/**
|
|
25
|
+
* Get current transactions per second
|
|
26
|
+
*
|
|
27
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
28
|
+
* @param period - Time period for the query
|
|
29
|
+
* @returns Current TPS value
|
|
30
|
+
* @throws {NordError} If the request fails
|
|
31
|
+
*/
|
|
32
|
+
export declare function getCurrentTps(webServerUrl: string, period?: string): Promise<number>;
|
|
33
|
+
/**
|
|
34
|
+
* Get peak transactions per second
|
|
35
|
+
*
|
|
36
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
37
|
+
* @param period - Time period for the query
|
|
38
|
+
* @returns Peak TPS value
|
|
39
|
+
* @throws {NordError} If the request fails
|
|
40
|
+
*/
|
|
41
|
+
export declare function getPeakTps(webServerUrl: string, period?: string): Promise<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Get median transaction latency
|
|
44
|
+
*
|
|
45
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
46
|
+
* @param period - Time period for the query
|
|
47
|
+
* @returns Median latency in milliseconds
|
|
48
|
+
* @throws {NordError} If the request fails
|
|
49
|
+
*/
|
|
50
|
+
export declare function getMedianLatency(webServerUrl: string, period?: string): Promise<number>;
|
|
51
|
+
/**
|
|
52
|
+
* Get total transaction count
|
|
53
|
+
*
|
|
54
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
55
|
+
* @returns Total transaction count
|
|
56
|
+
* @throws {NordError} If the request fails
|
|
57
|
+
*/
|
|
58
|
+
export declare function getTotalTransactions(webServerUrl: string): Promise<number>;
|
|
59
|
+
/**
|
|
60
|
+
* Query Prometheus metrics
|
|
61
|
+
*
|
|
62
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
63
|
+
* @param params - Prometheus query parameters
|
|
64
|
+
* @returns Query result as a number
|
|
65
|
+
* @throws {NordError} If the request fails
|
|
66
|
+
*/
|
|
67
|
+
export declare function queryPrometheus(webServerUrl: string, params: string): Promise<number>;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MetricPeriod = void 0;
|
|
4
|
+
exports.aggregateMetrics = aggregateMetrics;
|
|
5
|
+
exports.getCurrentTps = getCurrentTps;
|
|
6
|
+
exports.getPeakTps = getPeakTps;
|
|
7
|
+
exports.getMedianLatency = getMedianLatency;
|
|
8
|
+
exports.getTotalTransactions = getTotalTransactions;
|
|
9
|
+
exports.queryPrometheus = queryPrometheus;
|
|
10
|
+
const types_1 = require("../../types");
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const NordError_1 = require("../utils/NordError");
|
|
13
|
+
/**
|
|
14
|
+
* Time periods for metrics queries
|
|
15
|
+
*/
|
|
16
|
+
var MetricPeriod;
|
|
17
|
+
(function (MetricPeriod) {
|
|
18
|
+
MetricPeriod["ONE_MINUTE"] = "1m";
|
|
19
|
+
MetricPeriod["FIVE_MINUTES"] = "5m";
|
|
20
|
+
MetricPeriod["FIFTEEN_MINUTES"] = "15m";
|
|
21
|
+
MetricPeriod["ONE_HOUR"] = "1h";
|
|
22
|
+
MetricPeriod["FOUR_HOURS"] = "4h";
|
|
23
|
+
MetricPeriod["ONE_DAY"] = "24h";
|
|
24
|
+
MetricPeriod["ONE_WEEK"] = "7d";
|
|
25
|
+
})(MetricPeriod || (exports.MetricPeriod = MetricPeriod = {}));
|
|
26
|
+
/**
|
|
27
|
+
* Fetch aggregate metrics from the Nord API
|
|
28
|
+
*
|
|
29
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
30
|
+
* @param txPeakTpsPeriod - Period for peak TPS calculation
|
|
31
|
+
* @param txPeakTpsPeriodUnit - Unit for peak TPS period
|
|
32
|
+
* @returns Aggregate metrics
|
|
33
|
+
* @throws {NordError} If the request fails
|
|
34
|
+
*/
|
|
35
|
+
async function aggregateMetrics(webServerUrl, txPeakTpsPeriod = 1, txPeakTpsPeriodUnit = types_1.PeakTpsPeriodUnit.Day) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/metrics?tx_peak_tps_period=${txPeakTpsPeriod}&tx_peak_tps_period_unit=${txPeakTpsPeriodUnit}`);
|
|
38
|
+
// Get the raw text response (Prometheus format)
|
|
39
|
+
const text = await response.text();
|
|
40
|
+
// Parse the Prometheus-formatted metrics text into an AggregateMetrics object
|
|
41
|
+
const metrics = {
|
|
42
|
+
blocks_total: 0,
|
|
43
|
+
tx_total: extractMetricValue(text, "nord_requests_ok_count"),
|
|
44
|
+
tx_tps: calculateTps(text),
|
|
45
|
+
tx_tps_peak: calculatePeakTps(text),
|
|
46
|
+
request_latency_average: extractLatency(text),
|
|
47
|
+
};
|
|
48
|
+
return metrics;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw new NordError_1.NordError("Failed to fetch aggregate metrics", { cause: error });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Extract a metric value from Prometheus-formatted text
|
|
56
|
+
*
|
|
57
|
+
* @param text - Prometheus-formatted metrics text
|
|
58
|
+
* @param metricName - Name of the metric to extract
|
|
59
|
+
* @returns The metric value as a number, or 0 if not found
|
|
60
|
+
*/
|
|
61
|
+
function extractMetricValue(text, metricName) {
|
|
62
|
+
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`, "m");
|
|
63
|
+
const match = text.match(regex);
|
|
64
|
+
return match ? parseFloat(match[1]) : 0;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Calculate TPS from Prometheus metrics
|
|
68
|
+
*
|
|
69
|
+
* @param text - Prometheus-formatted metrics text
|
|
70
|
+
* @returns Calculated TPS value
|
|
71
|
+
*/
|
|
72
|
+
function calculateTps(text) {
|
|
73
|
+
// Use the request count and latency to estimate TPS
|
|
74
|
+
const requestCount = extractMetricValue(text, "nord_requests_ok_count");
|
|
75
|
+
const latencySum = extractSummaryValue(text, "nord_requests_ok_latency_sum");
|
|
76
|
+
const latencyCount = extractSummaryValue(text, "nord_requests_ok_latency_count");
|
|
77
|
+
if (latencySum > 0 && latencyCount > 0) {
|
|
78
|
+
// Average latency in seconds
|
|
79
|
+
const avgLatency = latencySum / latencyCount;
|
|
80
|
+
// If we have valid latency data, estimate TPS as requests per second
|
|
81
|
+
return avgLatency > 0 ? requestCount / (latencyCount * avgLatency) : 0;
|
|
82
|
+
}
|
|
83
|
+
// Fallback: just return a small fraction of the total request count
|
|
84
|
+
return requestCount > 0 ? requestCount / 100 : 0;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Calculate peak TPS from Prometheus metrics
|
|
88
|
+
*
|
|
89
|
+
* @param text - Prometheus-formatted metrics text
|
|
90
|
+
* @returns Calculated peak TPS value
|
|
91
|
+
*/
|
|
92
|
+
function calculatePeakTps(text) {
|
|
93
|
+
// For peak TPS, we'll use a simple heuristic: 2x the current TPS estimate
|
|
94
|
+
// TODO: fix this
|
|
95
|
+
return calculateTps(text) * 2;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Extract latency from Prometheus metrics
|
|
99
|
+
*
|
|
100
|
+
* @param text - Prometheus-formatted metrics text
|
|
101
|
+
* @returns Average latency in seconds
|
|
102
|
+
*/
|
|
103
|
+
function extractLatency(text) {
|
|
104
|
+
// TODO: fix - using average for latency is kinda wack. ok to merge for now but should change.
|
|
105
|
+
const latencySum = extractSummaryValue(text, "nord_requests_ok_latency_sum");
|
|
106
|
+
const latencyCount = extractSummaryValue(text, "nord_requests_ok_latency_count");
|
|
107
|
+
return latencyCount > 0 ? latencySum / latencyCount : 0;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Extract a summary value from Prometheus-formatted text
|
|
111
|
+
*
|
|
112
|
+
* @param text - Prometheus-formatted metrics text
|
|
113
|
+
* @param metricName - Name of the metric to extract
|
|
114
|
+
* @returns The metric value as a number, or 0 if not found
|
|
115
|
+
*/
|
|
116
|
+
function extractSummaryValue(text, metricName) {
|
|
117
|
+
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`, "m");
|
|
118
|
+
const match = text.match(regex);
|
|
119
|
+
return match ? parseFloat(match[1]) : 0;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get current transactions per second
|
|
123
|
+
*
|
|
124
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
125
|
+
* @param period - Time period for the query
|
|
126
|
+
* @returns Current TPS value
|
|
127
|
+
* @throws {NordError} If the request fails
|
|
128
|
+
*/
|
|
129
|
+
async function getCurrentTps(webServerUrl, period = "1m") {
|
|
130
|
+
try {
|
|
131
|
+
// nord_tx_count doesn't exist in the metrics, use nord_requests_ok_count instead
|
|
132
|
+
return await queryPrometheus(webServerUrl, `sum(rate(nord_requests_ok_count[${period}]))`);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
throw new NordError_1.NordError(`Failed to get current TPS for period ${period}`, {
|
|
136
|
+
cause: error,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get peak transactions per second
|
|
142
|
+
*
|
|
143
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
144
|
+
* @param period - Time period for the query
|
|
145
|
+
* @returns Peak TPS value
|
|
146
|
+
* @throws {NordError} If the request fails
|
|
147
|
+
*/
|
|
148
|
+
async function getPeakTps(webServerUrl, period = "24h") {
|
|
149
|
+
try {
|
|
150
|
+
// nord_tx_count doesn't exist in the metrics, use nord_requests_ok_count instead
|
|
151
|
+
return await queryPrometheus(webServerUrl, `max_over_time(sum(rate(nord_requests_ok_count[1m]))[${period}:])`);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
throw new NordError_1.NordError(`Failed to get peak TPS for period ${period}`, {
|
|
155
|
+
cause: error,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get median transaction latency
|
|
161
|
+
*
|
|
162
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
163
|
+
* @param period - Time period for the query
|
|
164
|
+
* @returns Median latency in milliseconds
|
|
165
|
+
* @throws {NordError} If the request fails
|
|
166
|
+
*/
|
|
167
|
+
async function getMedianLatency(webServerUrl, period = "1m") {
|
|
168
|
+
try {
|
|
169
|
+
// nord_tx_latency_ms doesn't exist, use nord_requests_ok_latency instead
|
|
170
|
+
// which contains the latency data in the summary metric
|
|
171
|
+
return await queryPrometheus(webServerUrl, `quantile_over_time(0.5, nord_requests_ok_latency[${period}]) * 1000`);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
throw new NordError_1.NordError(`Failed to get median latency for period ${period}`, {
|
|
175
|
+
cause: error,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get total transaction count
|
|
181
|
+
*
|
|
182
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
183
|
+
* @returns Total transaction count
|
|
184
|
+
* @throws {NordError} If the request fails
|
|
185
|
+
*/
|
|
186
|
+
async function getTotalTransactions(webServerUrl) {
|
|
187
|
+
try {
|
|
188
|
+
// nord_tx_count doesn't exist, use nord_requests_ok_count instead
|
|
189
|
+
return await queryPrometheus(webServerUrl, "sum(nord_requests_ok_count)");
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
throw new NordError_1.NordError("Failed to get total transactions", { cause: error });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Query Prometheus metrics
|
|
197
|
+
*
|
|
198
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
199
|
+
* @param params - Prometheus query parameters
|
|
200
|
+
* @returns Query result as a number
|
|
201
|
+
* @throws {NordError} If the request fails
|
|
202
|
+
*/
|
|
203
|
+
async function queryPrometheus(webServerUrl, params) {
|
|
204
|
+
try {
|
|
205
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/prometheus?query=${encodeURIComponent(params)}`);
|
|
206
|
+
// Handle raw text response
|
|
207
|
+
const text = await response.text();
|
|
208
|
+
try {
|
|
209
|
+
// Try to parse as JSON first
|
|
210
|
+
const data = JSON.parse(text);
|
|
211
|
+
return data.data.result[0]?.value[1] || 0;
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
console.log("Prometheus query failed:", error);
|
|
215
|
+
// Try to find a number in the response
|
|
216
|
+
const numberMatch = text.match(/[\d.]+/);
|
|
217
|
+
if (numberMatch) {
|
|
218
|
+
return parseFloat(numberMatch[0]);
|
|
219
|
+
}
|
|
220
|
+
// Return 0 if no number is found
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
throw new NordError_1.NordError(`Failed to query Prometheus: ${params}`, {
|
|
226
|
+
cause: error,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { components } from "../../gen/openapi";
|
|
2
|
+
type AccountTriggerInfo = components["schemas"]["AccountTriggerInfo"];
|
|
3
|
+
type TriggerHistoryPage = components["schemas"]["PageResult_for_uint64_and_HistoryTriggerInfo"];
|
|
4
|
+
type HistoryTriggerQuery = components["schemas"]["AccountTriggersQuery"];
|
|
5
|
+
export type { AccountTriggerInfo, TriggerHistoryPage, HistoryTriggerQuery };
|
|
6
|
+
export declare function fetchAccountTriggers(serverUrl: string, accountId: number): Promise<AccountTriggerInfo[]>;
|
|
7
|
+
export declare function fetchAccountTriggerHistory(serverUrl: string, accountId: number, options: HistoryTriggerQuery): Promise<TriggerHistoryPage>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fetchAccountTriggers = fetchAccountTriggers;
|
|
7
|
+
exports.fetchAccountTriggerHistory = fetchAccountTriggerHistory;
|
|
8
|
+
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
9
|
+
async function fetchAccountTriggers(serverUrl, accountId) {
|
|
10
|
+
const client = (0, openapi_fetch_1.default)({ baseUrl: serverUrl });
|
|
11
|
+
const response = await client.GET("/account/{account_id}/triggers", {
|
|
12
|
+
params: {
|
|
13
|
+
path: { account_id: accountId },
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
if (response.data === undefined) {
|
|
17
|
+
throw new Error(`Failed to fetch triggers for account ${accountId}: HTTP ${response.response.status}`);
|
|
18
|
+
}
|
|
19
|
+
return response.data ?? [];
|
|
20
|
+
}
|
|
21
|
+
async function fetchAccountTriggerHistory(serverUrl, accountId, options) {
|
|
22
|
+
const client = (0, openapi_fetch_1.default)({ baseUrl: serverUrl });
|
|
23
|
+
const response = await client.GET("/account/{account_id}/triggers/history", {
|
|
24
|
+
params: {
|
|
25
|
+
path: { account_id: accountId },
|
|
26
|
+
query: {
|
|
27
|
+
since: options.since,
|
|
28
|
+
until: options.until,
|
|
29
|
+
pageSize: options.pageSize,
|
|
30
|
+
startInclusive: options.startInclusive,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
if (!response.data) {
|
|
35
|
+
throw new Error(`Failed to fetch trigger history for account ${accountId}: HTTP ${response.response.status}`);
|
|
36
|
+
}
|
|
37
|
+
return response.data;
|
|
38
|
+
}
|