@todesktop/client-comm-server
Advanced tools
Comparing version 0.48.0 to 0.49.0
@@ -21,5 +21,2 @@ export declare type HandleBroadcastFunc<Request extends Record<string, any>, Response> = (callback: (data: Request) => Response | Promise<Response>) => () => void; | ||
export declare function checkIfCommServerRunning(ports: number[]): Promise<boolean | undefined>; | ||
export declare class NotRunningError extends Error { | ||
code: string; | ||
} | ||
declare type SuccessResponse<T> = { | ||
@@ -26,0 +23,0 @@ success: true; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NotRunningError = exports.checkIfCommServerRunning = exports.createBroadcastService = void 0; | ||
exports.checkIfCommServerRunning = exports.createBroadcastService = void 0; | ||
const client_util_1 = require("@todesktop/client-util"); | ||
@@ -77,51 +77,14 @@ const client_core_1 = require("@todesktop/client-core"); | ||
exports.checkIfCommServerRunning = checkIfCommServerRunning; | ||
class NotRunningError extends Error { | ||
constructor() { | ||
super(...arguments); | ||
this.code = "ENOTRUNNING"; | ||
} | ||
} | ||
exports.NotRunningError = NotRunningError; | ||
const isValidResponse = (response) => { | ||
return (response !== null && typeof response === "object" && "success" in response); | ||
}; | ||
function fetchWithFallback(ports, path) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (const port of ports) { | ||
let response; | ||
let throwImmediately = false; | ||
try { | ||
response = yield fetch(new URL(path, `http://localhost:${port}`)); | ||
const response = yield fetch(new URL(path, `http://localhost:${port}`)); | ||
return response.json(); | ||
} | ||
catch (e) { | ||
// If the promise rejects then it's a network error. | ||
// The promise won’t reject on HTTP error status even if the response is an HTTP 404 or 500 | ||
break; | ||
// ignore | ||
} | ||
try { | ||
const json = yield response.json(); | ||
if (isValidResponse(json)) { | ||
if (json.success) { | ||
return json; | ||
} | ||
else if (!json.success && "error" in json) { | ||
throwImmediately = true; | ||
throw new Error(json.error); | ||
} | ||
} | ||
} | ||
catch (e) { | ||
if (throwImmediately) { | ||
// This looks like our server is running but it's responding with an error so throw immediately. | ||
throw e; | ||
} | ||
else if (ports.indexOf(port) === ports.length - 1) { | ||
// This is the last port in the list and it's throwing an unknown error (not a network error). | ||
// This usually means there is a different server running on the same port. | ||
throw e; | ||
} | ||
// Otherwise, we'll continue to the next port or throw `NotRunningError`. | ||
} | ||
} | ||
throw new NotRunningError("Communication server is not running"); | ||
throw new Error("Failed to fetch"); | ||
}); | ||
@@ -128,0 +91,0 @@ } |
{ | ||
"name": "@todesktop/client-comm-server", | ||
"version": "0.48.0", | ||
"version": "0.49.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -87,48 +87,15 @@ import { checkIfCompatibleWithPlugin } from "@todesktop/client-util"; | ||
export class NotRunningError extends Error { | ||
code = "ENOTRUNNING"; | ||
} | ||
const isValidResponse = <T>(response: unknown): response is ResponseData<T> => { | ||
return ( | ||
response !== null && typeof response === "object" && "success" in response | ||
); | ||
}; | ||
async function fetchWithFallback<T>( | ||
ports: number[], | ||
path: string | ||
): Promise<SuccessResponse<T>> { | ||
): FetchResponse<T> { | ||
for (const port of ports) { | ||
let response: Response; | ||
let throwImmediately = false; | ||
try { | ||
response = await fetch(new URL(path, `http://localhost:${port}`)); | ||
const response = await fetch(new URL(path, `http://localhost:${port}`)); | ||
return response.json(); | ||
} catch (e) { | ||
// If the promise rejects then it's a network error. | ||
// The promise won’t reject on HTTP error status even if the response is an HTTP 404 or 500 | ||
break; | ||
// ignore | ||
} | ||
try { | ||
const json = await response.json(); | ||
if (isValidResponse<T>(json)) { | ||
if (json.success) { | ||
return json; | ||
} else if (!json.success && "error" in json) { | ||
throwImmediately = true; | ||
throw new Error(json.error); | ||
} | ||
} | ||
} catch (e) { | ||
if (throwImmediately) { | ||
// This looks like our server is running but it's responding with an error so throw immediately. | ||
throw e; | ||
} else if (ports.indexOf(port) === ports.length - 1) { | ||
// This is the last port in the list and it's throwing an unknown error (not a network error). | ||
// This usually means there is a different server running on the same port. | ||
throw e; | ||
} | ||
// Otherwise, we'll continue to the next port or throw `NotRunningError`. | ||
} | ||
} | ||
throw new NotRunningError("Communication server is not running"); | ||
throw new Error("Failed to fetch"); | ||
} | ||
@@ -145,3 +112,2 @@ | ||
type ErrorResponse = { success: false; error: string }; | ||
type ResponseData<T> = SuccessResponse<T> | ErrorResponse; | ||
type FetchResponse<T> = Promise<ResponseData<T>>; | ||
type FetchResponse<T> = Promise<SuccessResponse<T> | ErrorResponse | undefined>; |
12110
228