MCPcopy Index your code
hub / github.com/librespeed/speedtest / applyServerListJSON

Function applyServerListJSON

frontend/javascript/index.js:141–195  ·  view source on GitHub ↗

* Load server list from the configured source and populate the dropdown

()

Source from the content-addressed store, hash-verified

139 * Load server list from the configured source and populate the dropdown
140 */
141async function applyServerListJSON() {
142 try {
143 const serverSource =
144 typeof globalThis.SPEEDTEST_SERVERS !== "undefined"
145 ? globalThis.SPEEDTEST_SERVERS
146 : "server-list.json";
147 const servers = Array.isArray(serverSource)
148 ? serverSource
149 : await fetch(serverSource).then((response) => response.json());
150 if (!servers || !Array.isArray(servers) || servers.length === 0) {
151 return console.error("Server list is empty or malformed");
152 }
153
154 testState.servers = servers;
155
156 // If there's only one server, just show it. No reachability checks needed.
157 if (servers.length === 1) {
158 populateDropdown(servers);
159 return;
160 }
161
162 // For multiple servers: first run the built-in selection (which pings servers
163 // and annotates them with pingT). Only then populate the dropdown so that
164 // dead servers don't appear.
165 testState.speedtest.addTestPoints(servers);
166 testState.speedtest.selectServer((bestServer) => {
167 const aliveServers = testState.servers.filter((s) => {
168 // Keep servers that responded to ping (pingT !== -1).
169 if (s.pingT !== -1) return true;
170 // Also keep protocol-relative servers ("//...") as a defensive fallback.
171 // LibreSpeed normalizes them to the page protocol before pinging, so they
172 // are normally treated like any other server and get a real pingT value.
173 return typeof s.server === "string" && s.server.startsWith("//");
174 });
175
176 // Prefer to show only reachable servers, but if none are reachable,
177 // fall back to the full list so users can still pick a server manually.
178 if (aliveServers.length > 0) {
179 testState.servers = aliveServers;
180 }
181 populateDropdown(testState.servers);
182
183
184 if (bestServer) {
185 selectServer(bestServer);
186 } else {
187 alert(
188 "Can't reach any of the speedtest servers! But you're on this page. Something weird is going on with your network."
189 );
190 }
191 });
192 } catch (error) {
193 console.error("Failed to load server list:", error);
194 }
195}
196
197/**
198 * Add all the servers to the server selection dropdown and make it actually

Callers 1

index.jsFile · 0.85

Calls 2

populateDropdownFunction · 0.85
selectServerFunction · 0.85

Tested by

no test coverage detected