Skip to content

Commit

Permalink
correctly detect if DB exists before getting
Browse files Browse the repository at this point in the history
  • Loading branch information
Julli4n committed Oct 5, 2023
1 parent a4f0c79 commit 00edd20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scripts/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ await build({
package: {
name: "roblox-bat",
description: "A Deno/NodeJS module to generate Roblox BAT tokens for extensions",
version: "0.1.7",
version: "0.1.8",
homepage: "https://github.com/Julli4n/roblox-bat",
author: "Julli4n",
bugs: {
Expand Down
46 changes: 37 additions & 9 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,51 @@ export async function signWithKey(privateKey: CryptoKey, data: string): Promise<
const bufferResult = await crypto.subtle.sign(
TOKEN_SIGNATURE_ALGORITHM,
privateKey,
new TextEncoder().encode(data).buffer
new TextEncoder().encode(data).buffer,
);

return arrayBufferToBase64String(bufferResult);
}

export async function getCryptoKeyPairFromDB(dbName: string, dbObjectName: string, dbObjectChildId: string): Promise<CryptoKeyPair | null> {
export function doesDatabaseExist(dbName: string) {
return new Promise((resolve) => {
const db = indexedDB.open(dbName);
db.onsuccess = function () {
db.result.close();
resolve(true);
};

db.onupgradeneeded = function (evt) {
(evt.target as IDBRequest)?.transaction?.abort();
resolve(false);
};
});
}

export async function getCryptoKeyPairFromDB(
dbName: string,
dbObjectName: string,
dbObjectChildId: string,
): Promise<CryptoKeyPair | null> {
let targetVersion = 1;
// we want Roblox to create the DB on their end, so we do not want to interfere
const databases = await indexedDB.databases();
const database = databases.find(db => db.name === dbName);
if (!database) {
return null;
if ("databases" in indexedDB) {
const databases = await indexedDB.databases();
const database = databases.find((db) => db.name === dbName);
if (!database) {
return null;
}
if (database?.version) {
targetVersion = database.version;
}
} else {
if (!await doesDatabaseExist(dbName)) {
return null;
}
}

return new Promise((resolve, reject) => {

const request = indexedDB.open(dbName, database.version ?? 1);
const request = indexedDB.open(dbName, targetVersion);
request.onsuccess = () => {
try {
const db = request.result;
Expand All @@ -61,4 +89,4 @@ export async function getCryptoKeyPairFromDB(dbName: string, dbObjectName: strin
reject(request.error);
};
});
}
}

0 comments on commit 00edd20

Please # to comment.