From 80ca0ee4fc5678baf88a257bd710bf1ac47f8225 Mon Sep 17 00:00:00 2001 From: samir paudyal <75193555+samir-byte@users.noreply.github.com> Date: Wed, 30 Oct 2024 16:57:52 +0545 Subject: [PATCH 1/2] perf: inline return promise --- frontend/actions/generate-user-stripe.ts | 2 +- .../blog/en/decrypt-verify-authjs-jwt-token.mdx | 2 +- .../blog/en/memfree-build-4-bun-gpt-4o-stream.mdx | 2 +- .../blog/zh/decrypt-verify-authjs-jwt-token.mdx | 2 +- .../blog/zh/memfree-build-4-bun-gpt-4o-stream.mdx | 2 +- frontend/hooks/use-upload-file.ts | 2 +- frontend/lib/tools/auto.ts | 2 +- frontend/lib/tools/hacker-news.ts | 2 +- vector/auth.ts | 4 ++-- vector/db.ts | 12 ++++++------ vector/util.ts | 6 +++--- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/frontend/actions/generate-user-stripe.ts b/frontend/actions/generate-user-stripe.ts index 0706b807..14a4890b 100644 --- a/frontend/actions/generate-user-stripe.ts +++ b/frontend/actions/generate-user-stripe.ts @@ -16,7 +16,7 @@ export type responseAction = { const billingUrl = absoluteUrl('/pricing'); async function createStripeSession(priceId, locale, session, params) { - return await stripe.checkout.sessions.create({ + return stripe.checkout.sessions.create({ success_url: billingUrl, cancel_url: billingUrl, billing_address_collection: 'auto', diff --git a/frontend/content/blog/en/decrypt-verify-authjs-jwt-token.mdx b/frontend/content/blog/en/decrypt-verify-authjs-jwt-token.mdx index 6481079f..7b41400b 100644 --- a/frontend/content/blog/en/decrypt-verify-authjs-jwt-token.mdx +++ b/frontend/content/blog/en/decrypt-verify-authjs-jwt-token.mdx @@ -53,7 +53,7 @@ So, I checked the source code of auth.js and found that it uses the jose library ```ts async function getDerivedEncryptionKey(enc: string, keyMaterial: Parameters[1], salt: Parameters[2]) { const length = enc === 'A256CBC-HS512' ? 64 : 32; - return await hkdf('sha256', keyMaterial, salt, `Auth.js Generated Encryption Key (${salt})`, length); + return hkdf('sha256', keyMaterial, salt, `Auth.js Generated Encryption Key (${salt})`, length); } ``` diff --git a/frontend/content/blog/en/memfree-build-4-bun-gpt-4o-stream.mdx b/frontend/content/blog/en/memfree-build-4-bun-gpt-4o-stream.mdx index f251c1b0..35ad3584 100644 --- a/frontend/content/blog/en/memfree-build-4-bun-gpt-4o-stream.mdx +++ b/frontend/content/blog/en/memfree-build-4-bun-gpt-4o-stream.mdx @@ -72,7 +72,7 @@ const openai = new OpenAI({ async function readImageFile(filePath: string) { const baseDir = dirname(fileURLToPath(import.meta.url)); - return await readFile(join(baseDir, filePath)); + return readFile(join(baseDir, filePath)); } export async function chatImage() { diff --git a/frontend/content/blog/zh/decrypt-verify-authjs-jwt-token.mdx b/frontend/content/blog/zh/decrypt-verify-authjs-jwt-token.mdx index 819084dd..fd0b9456 100644 --- a/frontend/content/blog/zh/decrypt-verify-authjs-jwt-token.mdx +++ b/frontend/content/blog/zh/decrypt-verify-authjs-jwt-token.mdx @@ -53,7 +53,7 @@ export const getAuthToken = async () => { ```ts async function getDerivedEncryptionKey(enc: string, keyMaterial: Parameters[1], salt: Parameters[2]) { const length = enc === 'A256CBC-HS512' ? 64 : 32; - return await hkdf('sha256', keyMaterial, salt, `Auth.js 生成的加密密钥 (${salt})`, length); + return hkdf('sha256', keyMaterial, salt, `Auth.js 生成的加密密钥 (${salt})`, length); } ``` diff --git a/frontend/content/blog/zh/memfree-build-4-bun-gpt-4o-stream.mdx b/frontend/content/blog/zh/memfree-build-4-bun-gpt-4o-stream.mdx index 1074bd8a..e0f3dc83 100644 --- a/frontend/content/blog/zh/memfree-build-4-bun-gpt-4o-stream.mdx +++ b/frontend/content/blog/zh/memfree-build-4-bun-gpt-4o-stream.mdx @@ -72,7 +72,7 @@ const openai = new OpenAI({ async function readImageFile(filePath: string) { const baseDir = dirname(fileURLToPath(import.meta.url)); - return await readFile(join(baseDir, filePath)); + return readFile(join(baseDir, filePath)); } export async function chatImage() { diff --git a/frontend/hooks/use-upload-file.ts b/frontend/hooks/use-upload-file.ts index efd36609..2a8b7f55 100644 --- a/frontend/hooks/use-upload-file.ts +++ b/frontend/hooks/use-upload-file.ts @@ -24,7 +24,7 @@ export function useUploadFile() { Token: `${token.data}`, }, }); - return await res.json(); + return res.json(); } catch (err) { console.error(err); toast.error(String(err)); diff --git a/frontend/lib/tools/auto.ts b/frontend/lib/tools/auto.ts index 2a886e30..3a164a0e 100644 --- a/frontend/lib/tools/auto.ts +++ b/frontend/lib/tools/auto.ts @@ -58,7 +58,7 @@ export async function autoAnswer( url: z.string().describe('the url to access'), }), execute: async ({ url }) => { - return await accessWebPage(url, onStream); + return accessWebPage(url, onStream); }, }), }, diff --git a/frontend/lib/tools/hacker-news.ts b/frontend/lib/tools/hacker-news.ts index 1d64cf27..9fe2999d 100644 --- a/frontend/lib/tools/hacker-news.ts +++ b/frontend/lib/tools/hacker-news.ts @@ -24,7 +24,7 @@ export async function get_top_stories(limit: number, onStream?: (...args: any[]) export async function get_story(id: number) { const response = await fetch(`https://hacker-news.firebaseio.com/v0/item/${id}.json`); - return await response.json(); + return response.json(); } export async function get_story_with_comments(id: number) { diff --git a/vector/auth.ts b/vector/auth.ts index 72d6947a..78cf16fa 100644 --- a/vector/auth.ts +++ b/vector/auth.ts @@ -23,7 +23,7 @@ export async function getToken(req: Request, isDev: boolean) { if (!tokenStr) { return null; } - return await decryptToken(tokenStr, isDev); + return decryptToken(tokenStr, isDev); } catch (error) { logError(error as Error, "getToken"); return null; @@ -47,7 +47,7 @@ async function getDerivedEncryptionKey( salt: Parameters[2] ) { const length = enc === "A256CBC-HS512" ? 64 : 32; - return await hkdf( + return hkdf( "sha256", keyMaterial, salt, diff --git a/vector/db.ts b/vector/db.ts index 0e2bc8fb..3bbef01a 100644 --- a/vector/db.ts +++ b/vector/db.ts @@ -27,7 +27,7 @@ const schema = new Schema([ async function getConnection() { const bucket = process.env.AWS_BUCKET || ""; if (bucket) { - return await lancedb.connect(bucket, { + return lancedb.connect(bucket, { storageOptions: { awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID || "", s3Express: "true", @@ -38,15 +38,15 @@ async function getConnection() { } else { // Let open source users could one click deploy const localDirectory = process.cwd(); - return await lancedb.connect(localDirectory); + return lancedb.connect(localDirectory); } } async function getTable(db: any, tableName: string): Promise { if ((await db.tableNames()).includes(tableName)) { - return await db.openTable(tableName); + return db.openTable(tableName); } else { - return await db.createEmptyTable(tableName, schema); + return db.createEmptyTable(tableName, schema); } } @@ -151,12 +151,12 @@ export async function dropTable(tableName: string) { export async function createEmptyTable(tableName: string) { const db = await getConnection(); - return await db.createEmptyTable(tableName, schema); + return db.createEmptyTable(tableName, schema); } export async function reCreateEmptyTable(tableName: string) { const db = await getConnection(); - return await db.createEmptyTable(tableName, schema, { mode: "overwrite" }); + return db.createEmptyTable(tableName, schema, { mode: "overwrite" }); } export async function size(tableName: string) { diff --git a/vector/util.ts b/vector/util.ts index 99bec13c..a5037433 100644 --- a/vector/util.ts +++ b/vector/util.ts @@ -13,7 +13,7 @@ export async function retryAsync( while (attempt < retries) { try { - return await fn(); + return fn(); } catch (error) { attempt++; if (attempt < retries) { @@ -110,11 +110,11 @@ export async function getMd(url: string, userId: string) { } try { - return await fetchWithRetry(primaryUrl, { headers }, 1, 1000); + return fetchWithRetry(primaryUrl, { headers }, 1, 1000); } catch (primaryError) { console.error("Primary URL failed:", primaryError); try { - return await fetchWithRetry(fallbackUrl, { headers }, 1, 1000); + return fetchWithRetry(fallbackUrl, { headers }, 1, 1000); } catch (fallbackError) { console.error("Fallback URL failed:", fallbackError); log({ From 5031e0b4561832ab8325bbdc9e0a22beba730ade Mon Sep 17 00:00:00 2001 From: samir paudyal <75193555+samir-byte@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:06:55 +0545 Subject: [PATCH 2/2] fix: handled error --- frontend/hooks/use-upload-file.ts | 2 +- vector/auth.ts | 2 +- vector/util.ts | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frontend/hooks/use-upload-file.ts b/frontend/hooks/use-upload-file.ts index 2a8b7f55..efd36609 100644 --- a/frontend/hooks/use-upload-file.ts +++ b/frontend/hooks/use-upload-file.ts @@ -24,7 +24,7 @@ export function useUploadFile() { Token: `${token.data}`, }, }); - return res.json(); + return await res.json(); } catch (err) { console.error(err); toast.error(String(err)); diff --git a/vector/auth.ts b/vector/auth.ts index 78cf16fa..52493dc5 100644 --- a/vector/auth.ts +++ b/vector/auth.ts @@ -23,7 +23,7 @@ export async function getToken(req: Request, isDev: boolean) { if (!tokenStr) { return null; } - return decryptToken(tokenStr, isDev); + return await decryptToken(tokenStr, isDev); } catch (error) { logError(error as Error, "getToken"); return null; diff --git a/vector/util.ts b/vector/util.ts index a5037433..99bec13c 100644 --- a/vector/util.ts +++ b/vector/util.ts @@ -13,7 +13,7 @@ export async function retryAsync( while (attempt < retries) { try { - return fn(); + return await fn(); } catch (error) { attempt++; if (attempt < retries) { @@ -110,11 +110,11 @@ export async function getMd(url: string, userId: string) { } try { - return fetchWithRetry(primaryUrl, { headers }, 1, 1000); + return await fetchWithRetry(primaryUrl, { headers }, 1, 1000); } catch (primaryError) { console.error("Primary URL failed:", primaryError); try { - return fetchWithRetry(fallbackUrl, { headers }, 1, 1000); + return await fetchWithRetry(fallbackUrl, { headers }, 1, 1000); } catch (fallbackError) { console.error("Fallback URL failed:", fallbackError); log({