Skip to content

Commit

Permalink
Make the data prefix optional in profile imports
Browse files Browse the repository at this point in the history
Make it possible to import base64 encoded profiles which don't have the
data prefix as the data prefix does not provide much practical value but
it does make debugging profile issues more difficult as decoding them
can't be done as easily with 3rd party tooling.

This is a forward looking change and does not impact anything in the
immediate future. Once this change has been rolled out, it's possible to
remove the prefix from the profile generation logic.
  • Loading branch information
MythicManiac committed Jan 4, 2024
1 parent 6f4d92d commit 58105d1
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/r2mm/mods/ProfileImportExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ const getDownloadDestination = async (): Promise<string> => {
return path.join(cacheDir, "import.r2z");
}

const isProfileDataValid = (profileData: string): boolean => {
return profileData.startsWith(PROFILE_DATA_PREFIX)

const B64_REGEX = /[A-Za-z0-9+/=]/;

const isValidBase64 = (profileData: string): boolean => {
return B64_REGEX.test(profileData);
}

async function saveDownloadedProfile(profileData: string): Promise<string> {
if (!isProfileDataValid(profileData)) {
const normalizeProfileData = (profileData: string): string => {
if (profileData.startsWith(PROFILE_DATA_PREFIX)) {
profileData = profileData.substring(PROFILE_DATA_PREFIX.length).trim();
}
if (!isValidBase64(profileData)) {
throw new Error("Invalid profile data");
}
return profileData;
}

async function saveDownloadedProfile(profileData: string): Promise<string> {
const fs = FsProvider.instance;
const b64 = profileData.substring(PROFILE_DATA_PREFIX.length).trim();
const b64 = normalizeProfileData(profileData);
const decoded = Buffer.from(b64, "base64");
const destination = await getDownloadDestination();
await fs.writeFile(destination, decoded);
Expand Down

0 comments on commit 58105d1

Please sign in to comment.