@fibscope/agent 0.1.0
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/.env.example +7 -0
- package/PUBLISH.md +27 -0
- package/README.md +123 -0
- package/index.mjs +3244 -0
- package/lib/bridge/recommendation.mjs +311 -0
- package/lib/fnn-discovery.mjs +293 -0
- package/lib/observe/collector.mjs +1039 -0
- package/lib/pulse/engine.mjs +21 -0
- package/lib/pulse/index.mjs +6 -0
- package/lib/pulse/intelligence.mjs +154 -0
- package/lib/pulse/providers.mjs +321 -0
- package/lib/pulse/server.mjs +54 -0
- package/lib/pulse/snapshot.mjs +155 -0
- package/lib/route/route.mjs +1053 -0
- package/package.json +29 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { FiberCliProvider, FiberProviderError, FiberRpcProvider } from "./providers.mjs";
|
|
2
|
+
|
|
3
|
+
export async function collectFiberSnapshot(providerOrOptions, maybeOptions = {}) {
|
|
4
|
+
const provider = isProvider(providerOrOptions) ? providerOrOptions : undefined;
|
|
5
|
+
const options = isProvider(providerOrOptions) ? maybeOptions : (providerOrOptions ?? maybeOptions);
|
|
6
|
+
const selected = provider ? { provider, failures: [] } : await selectActiveFiberProvider(options);
|
|
7
|
+
if (!selected.provider) return offlineSnapshot(selected.failures);
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const timestamp = Date.now();
|
|
11
|
+
const [nodeInfo, peers, channels] = await Promise.all([
|
|
12
|
+
selected.provider.getNodeInfo(),
|
|
13
|
+
selected.provider.getPeers(),
|
|
14
|
+
selected.provider.getChannels(),
|
|
15
|
+
]);
|
|
16
|
+
const node = normalizeCollectedNode(nodeInfo, peers, channels, timestamp);
|
|
17
|
+
const snapshot = {
|
|
18
|
+
timestamp,
|
|
19
|
+
provider: providerInfo(selected.provider),
|
|
20
|
+
node,
|
|
21
|
+
peers,
|
|
22
|
+
channels,
|
|
23
|
+
payments: [],
|
|
24
|
+
collectionErrors: [],
|
|
25
|
+
};
|
|
26
|
+
snapshot.payments = await collectPayments(selected.provider, options, snapshot);
|
|
27
|
+
return snapshot;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return offlineSnapshot([{ provider: selected.provider.label, message: errorMessage(error) }]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function collectNode(provider) {
|
|
34
|
+
return provider.getNodeInfo();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function collectPeers(provider) {
|
|
38
|
+
return provider.getPeers();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function collectChannels(provider) {
|
|
42
|
+
return provider.getChannels();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function collectPayments(provider, options = {}, snapshot = {}) {
|
|
46
|
+
if (typeof options.paymentProvider === "function") {
|
|
47
|
+
return (await options.paymentProvider(snapshot)) ?? [];
|
|
48
|
+
}
|
|
49
|
+
if (typeof provider.getPayments === "function") {
|
|
50
|
+
try {
|
|
51
|
+
return (await provider.getPayments()) ?? [];
|
|
52
|
+
} catch (error) {
|
|
53
|
+
snapshot.collectionErrors?.push?.({ provider: provider.label, collector: "payments", message: errorMessage(error) });
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function selectActiveFiberProvider(options = {}) {
|
|
61
|
+
if (options.provider) return { provider: options.provider, failures: [] };
|
|
62
|
+
const candidates = buildProviderCandidates(options);
|
|
63
|
+
const failures = [];
|
|
64
|
+
for (const provider of candidates) {
|
|
65
|
+
try {
|
|
66
|
+
await provider.getNodeInfo();
|
|
67
|
+
return { provider, failures };
|
|
68
|
+
} catch (error) {
|
|
69
|
+
failures.push({ provider: provider.label, message: errorMessage(error) });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { failures };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function buildProviderCandidates(options = {}) {
|
|
76
|
+
const providers = [];
|
|
77
|
+
const timeoutMs = options.timeoutMs ?? 1_500;
|
|
78
|
+
const rpcToken = options.rpcToken ?? process.env.PULSE_FIBER_RPC_TOKEN;
|
|
79
|
+
const rpcHeaders = {
|
|
80
|
+
...(options.rpcHeaders ?? {}),
|
|
81
|
+
...(rpcToken ? { authorization: `Bearer ${rpcToken}` } : {}),
|
|
82
|
+
};
|
|
83
|
+
const rpcUrls = unique([
|
|
84
|
+
options.rpcUrl,
|
|
85
|
+
process.env.PULSE_FIBER_RPC_URL,
|
|
86
|
+
process.env.FIBER_RPC_URL,
|
|
87
|
+
...(options.rpcUrls ?? []),
|
|
88
|
+
"http://127.0.0.1:8227",
|
|
89
|
+
"http://127.0.0.1:8237",
|
|
90
|
+
].filter(Boolean));
|
|
91
|
+
providers.push(...rpcUrls.map((rpcUrl) => new FiberRpcProvider({
|
|
92
|
+
rpcUrl,
|
|
93
|
+
headers: rpcHeaders,
|
|
94
|
+
timeoutMs,
|
|
95
|
+
})));
|
|
96
|
+
const cliBins = options.cliBin || process.env.PULSE_FNN_CLI || process.env.FNN_CLI
|
|
97
|
+
? [options.cliBin ?? process.env.PULSE_FNN_CLI ?? process.env.FNN_CLI ?? "fnn-cli"]
|
|
98
|
+
: ["fnn-cli", "fiber-cli"];
|
|
99
|
+
providers.push(...cliBins.map((bin) => new FiberCliProvider({
|
|
100
|
+
bin,
|
|
101
|
+
baseArgs: options.baseArgs,
|
|
102
|
+
timeoutMs,
|
|
103
|
+
})));
|
|
104
|
+
return providers;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function offlineSnapshot(failures = []) {
|
|
108
|
+
const timestamp = Date.now();
|
|
109
|
+
return {
|
|
110
|
+
timestamp,
|
|
111
|
+
provider: null,
|
|
112
|
+
node: {
|
|
113
|
+
nodeId: "",
|
|
114
|
+
version: "",
|
|
115
|
+
online: false,
|
|
116
|
+
peerCount: 0,
|
|
117
|
+
channelCount: 0,
|
|
118
|
+
lastUpdated: timestamp,
|
|
119
|
+
},
|
|
120
|
+
peers: [],
|
|
121
|
+
channels: [],
|
|
122
|
+
payments: [],
|
|
123
|
+
collectionErrors: failures,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeCollectedNode(node, peers, channels, timestamp) {
|
|
128
|
+
return {
|
|
129
|
+
...node,
|
|
130
|
+
online: true,
|
|
131
|
+
peerCount: peers.length,
|
|
132
|
+
channelCount: channels.length,
|
|
133
|
+
lastUpdated: timestamp,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function providerInfo(provider) {
|
|
138
|
+
return {
|
|
139
|
+
kind: provider.kind ?? "unknown",
|
|
140
|
+
label: provider.label ?? "unknown",
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function isProvider(value) {
|
|
145
|
+
return Boolean(value && typeof value === "object" && "getNodeInfo" in value && "getPeers" in value && "getChannels" in value);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function errorMessage(error) {
|
|
149
|
+
if (error instanceof FiberProviderError) return error.message;
|
|
150
|
+
return error instanceof Error ? error.message : String(error);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function unique(values) {
|
|
154
|
+
return [...new Set(values)];
|
|
155
|
+
}
|