Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(flat-server-api): choose server by room's region #1974

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flat-server-api/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class ServerRequestError extends Error {
public errorMessage: string;

public constructor(errorCode: RequestErrorCode) {
super(`request failed: ${errorCode}`);
super(`request failed: ${errorCode} (${RequestErrorCode[errorCode]})`);
this.name = this.constructor.name;
this.errorCode = errorCode;
this.errorMessage = RequestErrorMessage[errorCode];
Expand Down
54 changes: 46 additions & 8 deletions packages/flat-server-api/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FLAT_SERVER_BASE_URL_V1, FLAT_SERVER_BASE_URL_V2, Status } from "./constants";
import { FLAT_SERVER_BASE_URL_V1, FLAT_SERVER_BASE_URL_V2, Region, Status } from "./constants";
import { ServerRequestError, RequestErrorCode } from "./error";
import { v4 as uuidv4 } from "uuid";

Expand Down Expand Up @@ -26,6 +26,50 @@ export function setFlatAuthToken(token: string): void {
localStorage.setItem("FlatAuthToken", token);
}

const defaultRegion = FLAT_SERVER_BASE_URL_V1.includes("-sg") ? Region.SG : Region.CN_HZ;

export function chooseServer(payload: any, enableFlatServerV2?: boolean): string {
let server = enableFlatServerV2 ? FLAT_SERVER_BASE_URL_V2 : FLAT_SERVER_BASE_URL_V1;

let region = defaultRegion;
if (payload !== null && typeof payload === "object") {
// Please check all server api's payload to make sure roomUUID is from the fields: roomUUID, uuid
// the "uuid" is maybe an invite code
const uuid = payload.roomUUID || payload.uuid;

if (typeof uuid === "string") {
if ((uuid.length === 11 && uuid[0] === "1") || uuid.startsWith("CN-")) {
region = Region.CN_HZ;
}
if ((uuid.length === 11 && uuid[0] === "2") || uuid.startsWith("SG-")) {
region = Region.SG;
}
// Legacy room
if (uuid.length === 10) {
region = Region.CN_HZ;
}
}
}
if (region === defaultRegion) {
return server;
}

// replace the left most part of domain, currently we have "{api}" for cn-hz, "{api}-sg" for sg
server = server.replace(/^https:\/\/([-\w]+)/, (_, name: string) => {
// normalize
if (name.endsWith("-sg")) {
name = name.slice(0, -3);
}
// add suffix if needed
if (region === Region.SG) {
name += "-sg";
}
return `https://${name}`;
});

return server;
}

export async function requestFlatServer<TPayload, TResult>(
action: string,
payload?: TPayload,
Expand Down Expand Up @@ -58,13 +102,7 @@ export async function requestFlatServer<TPayload, TResult>(
headers.set("authorization", "Bearer " + token);
}

// TODO: if payload.roomUUID has '{REGION}-' prefix, select another server
const response = await fetch(
`${
enableFlatServerV2 === true ? FLAT_SERVER_BASE_URL_V2 : FLAT_SERVER_BASE_URL_V1
}/${action}`,
config,
);
const response = await fetch(`${chooseServer(payload, enableFlatServerV2)}/${action}`, config);

if (!response.ok) {
// @TODO create a timeout error code
Expand Down