From 6da8182530c4b3727476fc22d3e9c249801ff54e Mon Sep 17 00:00:00 2001 From: Mary <148872143+mary-ext@users.noreply.github.com> Date: Fri, 1 Dec 2023 06:51:07 +0700 Subject: [PATCH 1/5] fix(pds): include aspectRatio on read-sticky posts (#1824) * fix: include aspectRatio on read-sticky * chore: add missing alt text test case --- packages/pds/src/services/local/index.ts | 1 + packages/pds/tests/proxied/read-after-write.test.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/pds/src/services/local/index.ts b/packages/pds/src/services/local/index.ts index 83ef39798e8..0dfc2ca9355 100644 --- a/packages/pds/src/services/local/index.ts +++ b/packages/pds/src/services/local/index.ts @@ -220,6 +220,7 @@ export class LocalService { did, img.image.ref.toString(), ), + aspectRatio: img.aspectRatio, alt: img.alt, })) return { diff --git a/packages/pds/tests/proxied/read-after-write.test.ts b/packages/pds/tests/proxied/read-after-write.test.ts index 1e9e4125084..52507a2730a 100644 --- a/packages/pds/tests/proxied/read-after-write.test.ts +++ b/packages/pds/tests/proxied/read-after-write.test.ts @@ -141,6 +141,7 @@ describe('proxy read after write', () => { images: [ { image: img.image, + aspectRatio: { height: 2, width: 1 }, alt: 'alt text', }, ], @@ -190,6 +191,8 @@ describe('proxy read after write', () => { img.image.ref.toString(), ), ) + expect(imgs.images[0].aspectRatio).toEqual({ height: 2, width: 1 }) + expect(imgs.images[0].alt).toBe('alt text') expect(replies[0].replies?.length).toBe(1) // @ts-ignore expect(replies[0].replies[0].post.uri).toEqual(replyRes2.uri) From 3c0ef382c12a413cc971ae47ffb341236c545f60 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 30 Nov 2023 23:53:04 +0000 Subject: [PATCH 2/5] Don't create unnecessary error objects (#1908) * Don't create unnecessary error objects * add changeset --------- Co-authored-by: dholms --- .changeset/brave-swans-kiss.md | 5 +++++ packages/syntax/src/aturi_validation.ts | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 .changeset/brave-swans-kiss.md diff --git a/.changeset/brave-swans-kiss.md b/.changeset/brave-swans-kiss.md new file mode 100644 index 00000000000..e6e42a1bcb2 --- /dev/null +++ b/.changeset/brave-swans-kiss.md @@ -0,0 +1,5 @@ +--- +'@atproto/syntax': patch +--- + +prevent unnecessary throw/catch on uri syntax diff --git a/packages/syntax/src/aturi_validation.ts b/packages/syntax/src/aturi_validation.ts index a272b15a082..826dfa78b2a 100644 --- a/packages/syntax/src/aturi_validation.ts +++ b/packages/syntax/src/aturi_validation.ts @@ -38,13 +38,13 @@ export const ensureValidAtUri = (uri: string) => { } try { - ensureValidHandle(parts[2]) - } catch { - try { + if (parts[2].startsWith('did:')) { ensureValidDid(parts[2]) - } catch { - throw new Error('ATURI authority must be a valid handle or DID') + } else { + ensureValidHandle(parts[2]) } + } catch { + throw new Error('ATURI authority must be a valid handle or DID') } if (parts.length >= 4) { From 2fc6ca54c10c970ea813534e5eb0352c62a2572f Mon Sep 17 00:00:00 2001 From: Matthieu Sieben Date: Fri, 1 Dec 2023 00:53:16 +0100 Subject: [PATCH 3/5] perf(bsky): avoid re-creating auth functions on every request (#1822) perf(bsky): avoid re-creating auth utilities on every request Co-authored-by: Daniel Holmgren --- packages/bsky/src/auth.ts | 42 +++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/bsky/src/auth.ts b/packages/bsky/src/auth.ts index 220be08fc32..ba58638d4f9 100644 --- a/packages/bsky/src/auth.ts +++ b/packages/bsky/src/auth.ts @@ -7,37 +7,41 @@ import { ServerConfig } from './config' const BASIC = 'Basic ' const BEARER = 'Bearer ' -export const authVerifier = - (idResolver: IdResolver, opts: { aud: string | null }) => - async (reqCtx: { req: express.Request; res: express.Response }) => { +export const authVerifier = ( + idResolver: IdResolver, + opts: { aud: string | null }, +) => { + const getSigningKey = async ( + did: string, + forceRefresh: boolean, + ): Promise => { + const atprotoData = await idResolver.did.resolveAtprotoData( + did, + forceRefresh, + ) + return atprotoData.signingKey + } + + return async (reqCtx: { req: express.Request; res: express.Response }) => { const jwtStr = getJwtStrFromReq(reqCtx.req) if (!jwtStr) { throw new AuthRequiredError('missing jwt', 'MissingJwt') } - const payload = await verifyJwt( - jwtStr, - opts.aud, - async (did, forceRefresh) => { - const atprotoData = await idResolver.did.resolveAtprotoData( - did, - forceRefresh, - ) - return atprotoData.signingKey - }, - ) + const payload = await verifyJwt(jwtStr, opts.aud, getSigningKey) return { credentials: { did: payload.iss }, artifacts: { aud: opts.aud } } } +} export const authOptionalVerifier = ( idResolver: IdResolver, opts: { aud: string | null }, ) => { - const verify = authVerifier(idResolver, opts) + const verifyAccess = authVerifier(idResolver, opts) return async (reqCtx: { req: express.Request; res: express.Response }) => { if (!reqCtx.req.headers.authorization) { return { credentials: { did: null } } } - return verify(reqCtx) + return verifyAccess(reqCtx) } } @@ -131,9 +135,9 @@ export const buildBasicAuth = (username: string, password: string): string => { } export const getJwtStrFromReq = (req: express.Request): string | null => { - const { authorization = '' } = req.headers - if (!authorization.startsWith(BEARER)) { + const { authorization } = req.headers + if (!authorization?.startsWith(BEARER)) { return null } - return authorization.replace(BEARER, '').trim() + return authorization.slice(BEARER.length).trim() } From 2513bd1b26bdff51a0ee208fffd15563f93b8fe6 Mon Sep 17 00:00:00 2001 From: Matthieu Sieben Date: Fri, 1 Dec 2023 00:53:23 +0100 Subject: [PATCH 4/5] style(xrpc-server): avoid un-neccessary "if" statement (#1826) fix(xrpc-server): avoid un-neccessary "if" statement --- packages/xrpc-server/src/server.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/xrpc-server/src/server.ts b/packages/xrpc-server/src/server.ts index 1a258097d66..22281fc9740 100644 --- a/packages/xrpc-server/src/server.ts +++ b/packages/xrpc-server/src/server.ts @@ -248,11 +248,9 @@ export class Server { } // handle rate limits - if (consumeRateLimit) { - const result = await consumeRateLimit(reqCtx) - if (result instanceof RateLimitExceededError) { - return next(result) - } + const result = await consumeRateLimit(reqCtx) + if (result instanceof RateLimitExceededError) { + return next(result) } // run the handler From 691511b9b9ff5e6177b57febf9e3f097eb8d6128 Mon Sep 17 00:00:00 2001 From: Matthieu Sieben Date: Fri, 1 Dec 2023 00:55:20 +0100 Subject: [PATCH 5/5] fix(debug): properly type debugCatch wrapper result (#1817) --- packages/bsky/src/util/debug.ts | 4 ++-- packages/pds/src/util/debug.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/bsky/src/util/debug.ts b/packages/bsky/src/util/debug.ts index 0060fc98fa0..d83a3b6d1c5 100644 --- a/packages/bsky/src/util/debug.ts +++ b/packages/bsky/src/util/debug.ts @@ -1,7 +1,7 @@ export const debugCatch = any>(fn: Func) => { - return async (...args) => { + return async (...args: Parameters) => { try { - return await fn(...args) + return (await fn(...args)) as Awaited> } catch (err) { console.error(err) throw err diff --git a/packages/pds/src/util/debug.ts b/packages/pds/src/util/debug.ts index 0060fc98fa0..d83a3b6d1c5 100644 --- a/packages/pds/src/util/debug.ts +++ b/packages/pds/src/util/debug.ts @@ -1,7 +1,7 @@ export const debugCatch = any>(fn: Func) => { - return async (...args) => { + return async (...args: Parameters) => { try { - return await fn(...args) + return (await fn(...args)) as Awaited> } catch (err) { console.error(err) throw err