Skip to content

Commit

Permalink
fix(Node): fix node check version for reconnecting state
Browse files Browse the repository at this point in the history
  • Loading branch information
Fab1o0107 committed Dec 27, 2023
1 parent e0f82ee commit 3dba326
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,23 @@ export class Node {
throw new Error("You can't re-use the same instance of a node once disconnected, please re-add the node again");
if (this.state === State.Connected) return undefined;

const version = await (
await fetch(`${this.url.replace("ws", "http")}/version`, {
headers: { Authorization: this.authorization, "User-Agent": this.manager.options.userAgent }
})
).text();
const versionRegex =
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;

if (Number(versionRegex.exec(version)?.at(1) ?? 0) !== 4)
throw new Error(`This node (${this.name}) is only supported for v4`);
let unSupportedVersion = false;

try {
const version = await (
await fetch(`${this.url.replace("ws", "http")}/version`, {
headers: { Authorization: this.authorization, "User-Agent": this.manager.options.userAgent }
})
).text();
const versionRegex =
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;

if (Number(versionRegex.exec(version)?.at(1) ?? 0) !== 4) unSupportedVersion = true;
} catch {
/* empty */
}

if (unSupportedVersion) throw new Error(`This node (${this.name}) is only supported for v4`);

this.state = State.Connecting;
this.sessionId = (await this.manager.redis?.get(RedisKey.NodeSession(this.name.toLowerCase()))) ?? null;
Expand Down

0 comments on commit 3dba326

Please sign in to comment.