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/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() } 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/read-after-write/viewer.ts b/packages/pds/src/read-after-write/viewer.ts index 74621790c82..a3da0e5eb88 100644 --- a/packages/pds/src/read-after-write/viewer.ts +++ b/packages/pds/src/read-after-write/viewer.ts @@ -232,6 +232,7 @@ export class LocalViewer { const images = embed.images.map((img) => ({ thumb: this.getImageUrl('feed_thumbnail', img.image.ref.toString()), fullsize: this.getImageUrl('feed_fullsize', img.image.ref.toString()), + aspectRatio: img.aspectRatio, alt: img.alt, })) return { 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 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) 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) { diff --git a/packages/xrpc-server/src/server.ts b/packages/xrpc-server/src/server.ts index a9539545866..9a666488c1c 100644 --- a/packages/xrpc-server/src/server.ts +++ b/packages/xrpc-server/src/server.ts @@ -251,11 +251,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