From aa1dd7e7a90b9938d6938f11e044d81617a40af0 Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Wed, 7 Feb 2024 16:46:02 -0500 Subject: [PATCH] apply pipethrough to most pds routes --- .../pds/src/api/app/bsky/feed/getActorFeeds.ts | 9 ++++----- packages/pds/src/api/app/bsky/feed/getFeed.ts | 14 ++++---------- .../src/api/app/bsky/feed/getFeedGenerator.ts | 9 ++++----- .../pds/src/api/app/bsky/graph/getFollows.ts | 9 ++++----- packages/pds/src/api/app/bsky/graph/getList.ts | 9 ++++----- .../pds/src/api/app/bsky/graph/getListBlocks.ts | 9 ++++----- .../pds/src/api/app/bsky/graph/getListMutes.ts | 9 ++++----- packages/pds/src/api/app/bsky/graph/getLists.ts | 9 ++++----- packages/pds/src/api/app/bsky/graph/getMutes.ts | 9 ++++----- .../app/bsky/graph/getSuggestedFollowsByActor.ts | 16 +++++++--------- .../api/app/bsky/notification/getUnreadCount.ts | 16 +++++++--------- .../app/bsky/notification/listNotifications.ts | 16 +++++++--------- packages/pds/src/pipethrough.ts | 9 ++++++--- 13 files changed, 63 insertions(+), 80 deletions(-) diff --git a/packages/pds/src/api/app/bsky/feed/getActorFeeds.ts b/packages/pds/src/api/app/bsky/feed/getActorFeeds.ts index 384d68500d4..ca5d8d5d48e 100644 --- a/packages/pds/src/api/app/bsky/feed/getActorFeeds.ts +++ b/packages/pds/src/api/app/bsky/feed/getActorFeeds.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.feed.getActorFeeds({ auth: ctx.authVerifier.access, handler: async ({ auth, params }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.feed.getActorFeeds( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.feed.getActorFeeds', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/feed/getFeed.ts b/packages/pds/src/api/app/bsky/feed/getFeed.ts index dd42e81fb94..ea63cf1bd47 100644 --- a/packages/pds/src/api/app/bsky/feed/getFeed.ts +++ b/packages/pds/src/api/app/bsky/feed/getFeed.ts @@ -1,6 +1,6 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' -import { noUndefinedVals } from '@atproto/common' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.feed.getFeed({ @@ -20,18 +20,12 @@ export default function (server: Server, ctx: AppContext) { // forward accept-language header to upstream services serviceAuthHeaders.headers['accept-language'] = req.headers['accept-language'] - const res = await ctx.appViewAgent.api.app.bsky.feed.getFeed( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.feed.getFeed', params, serviceAuthHeaders, ) - - return { - encoding: 'application/json', - body: res.data, - headers: noUndefinedVals({ - 'content-language': res.headers['content-language'], - }), - } }, }) } diff --git a/packages/pds/src/api/app/bsky/feed/getFeedGenerator.ts b/packages/pds/src/api/app/bsky/feed/getFeedGenerator.ts index 57c4731db53..deb5025f0fe 100644 --- a/packages/pds/src/api/app/bsky/feed/getFeedGenerator.ts +++ b/packages/pds/src/api/app/bsky/feed/getFeedGenerator.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.feed.getFeedGenerator({ auth: ctx.authVerifier.access, handler: async ({ params, auth }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.feed.getFeedGenerator( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.feed.getFeedGenerator', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getFollows.ts b/packages/pds/src/api/app/bsky/graph/getFollows.ts index 91955965ed6..2f3c2f2041a 100644 --- a/packages/pds/src/api/app/bsky/graph/getFollows.ts +++ b/packages/pds/src/api/app/bsky/graph/getFollows.ts @@ -1,6 +1,7 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' import { authPassthru } from '../../../proxy' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getFollows({ @@ -8,14 +9,12 @@ export default function (server: Server, ctx: AppContext) { handler: async ({ req, params, auth }) => { const requester = auth.credentials.type === 'access' ? auth.credentials.did : null - const res = await ctx.appViewAgent.api.app.bsky.graph.getFollows( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getFollows', params, requester ? await ctx.appviewAuthHeaders(requester) : authPassthru(req), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getList.ts b/packages/pds/src/api/app/bsky/graph/getList.ts index fb5776f5df2..60ea41cfd2f 100644 --- a/packages/pds/src/api/app/bsky/graph/getList.ts +++ b/packages/pds/src/api/app/bsky/graph/getList.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getList({ auth: ctx.authVerifier.access, handler: async ({ params, auth }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.graph.getList( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getList', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getListBlocks.ts b/packages/pds/src/api/app/bsky/graph/getListBlocks.ts index 376de0ba914..fed639e1dc8 100644 --- a/packages/pds/src/api/app/bsky/graph/getListBlocks.ts +++ b/packages/pds/src/api/app/bsky/graph/getListBlocks.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getListBlocks({ auth: ctx.authVerifier.access, handler: async ({ auth, params }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.graph.getListBlocks( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getListBlocks', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getListMutes.ts b/packages/pds/src/api/app/bsky/graph/getListMutes.ts index c489124642c..f3856a7fdc0 100644 --- a/packages/pds/src/api/app/bsky/graph/getListMutes.ts +++ b/packages/pds/src/api/app/bsky/graph/getListMutes.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getListMutes({ auth: ctx.authVerifier.access, handler: async ({ params, auth }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.graph.getListMutes( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getListMutes', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getLists.ts b/packages/pds/src/api/app/bsky/graph/getLists.ts index 61a1cb89079..4af580e280c 100644 --- a/packages/pds/src/api/app/bsky/graph/getLists.ts +++ b/packages/pds/src/api/app/bsky/graph/getLists.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getLists({ auth: ctx.authVerifier.access, handler: async ({ params, auth }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.graph.getLists( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getLists', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getMutes.ts b/packages/pds/src/api/app/bsky/graph/getMutes.ts index 0dc0e72412c..103c0bf4f2f 100644 --- a/packages/pds/src/api/app/bsky/graph/getMutes.ts +++ b/packages/pds/src/api/app/bsky/graph/getMutes.ts @@ -1,19 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getMutes({ auth: ctx.authVerifier.access, handler: async ({ auth, params }) => { const requester = auth.credentials.did - const res = await ctx.appViewAgent.api.app.bsky.graph.getMutes( + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getMutes', params, await ctx.appviewAuthHeaders(requester), ) - return { - encoding: 'application/json', - body: res.data, - } }, }) } diff --git a/packages/pds/src/api/app/bsky/graph/getSuggestedFollowsByActor.ts b/packages/pds/src/api/app/bsky/graph/getSuggestedFollowsByActor.ts index 0a11361f05c..b4422b7090e 100644 --- a/packages/pds/src/api/app/bsky/graph/getSuggestedFollowsByActor.ts +++ b/packages/pds/src/api/app/bsky/graph/getSuggestedFollowsByActor.ts @@ -1,20 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.graph.getSuggestedFollowsByActor({ auth: ctx.authVerifier.access, handler: async ({ auth, params }) => { const requester = auth.credentials.did - const res = - await ctx.appViewAgent.api.app.bsky.graph.getSuggestedFollowsByActor( - params, - await ctx.appviewAuthHeaders(requester), - ) - return { - encoding: 'application/json', - body: res.data, - } + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.graph.getSuggestedFollowsByActor', + params, + await ctx.appviewAuthHeaders(requester), + ) }, }) } diff --git a/packages/pds/src/api/app/bsky/notification/getUnreadCount.ts b/packages/pds/src/api/app/bsky/notification/getUnreadCount.ts index bca4bf3d46f..0827f47bb9e 100644 --- a/packages/pds/src/api/app/bsky/notification/getUnreadCount.ts +++ b/packages/pds/src/api/app/bsky/notification/getUnreadCount.ts @@ -1,20 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.notification.getUnreadCount({ auth: ctx.authVerifier.access, handler: async ({ auth, params }) => { const requester = auth.credentials.did - const res = - await ctx.appViewAgent.api.app.bsky.notification.getUnreadCount( - params, - await ctx.appviewAuthHeaders(requester), - ) - return { - encoding: 'application/json', - body: res.data, - } + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.notification.getUnreadCount', + params, + await ctx.appviewAuthHeaders(requester), + ) }, }) } diff --git a/packages/pds/src/api/app/bsky/notification/listNotifications.ts b/packages/pds/src/api/app/bsky/notification/listNotifications.ts index 4a514ae03f4..ad5d8635b41 100644 --- a/packages/pds/src/api/app/bsky/notification/listNotifications.ts +++ b/packages/pds/src/api/app/bsky/notification/listNotifications.ts @@ -1,20 +1,18 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' +import { pipethrough } from '../../../../pipethrough' export default function (server: Server, ctx: AppContext) { server.app.bsky.notification.listNotifications({ auth: ctx.authVerifier.access, handler: async ({ params, auth }) => { const requester = auth.credentials.did - const res = - await ctx.appViewAgent.api.app.bsky.notification.listNotifications( - params, - await ctx.appviewAuthHeaders(requester), - ) - return { - encoding: 'application/json', - body: res.data, - } + return pipethrough( + ctx.cfg.bskyAppView.url, + 'app.bsky.notification.listNotifications', + params, + await ctx.appviewAuthHeaders(requester), + ) }, }) } diff --git a/packages/pds/src/pipethrough.ts b/packages/pds/src/pipethrough.ts index 1e21d63caec..fd6f6235379 100644 --- a/packages/pds/src/pipethrough.ts +++ b/packages/pds/src/pipethrough.ts @@ -4,6 +4,7 @@ import { HandlerPipeThrough } from '@atproto/xrpc-server' import { CallOptions, ResponseType, XRPCError } from '@atproto/xrpc' import { lexicons } from './lexicon/lexicons' import { httpLogger } from './logger' +import { noUndefinedVals } from '@atproto/common' export const pipethrough = async ( serviceUrl: string, @@ -33,9 +34,11 @@ export const pipethrough = async ( } const encoding = res.headers.get('content-type') ?? 'application/json' const repoRevHeader = res.headers.get('atproto-repo-rev') - const headers = repoRevHeader - ? { ['atproto-repo-rev']: repoRevHeader } - : undefined + const contentLanguage = res.headers.get('content-language') + const headers = noUndefinedVals({ + ['atproto-repo-rev']: repoRevHeader ?? undefined, + ['content-language']: contentLanguage ?? undefined, + }) return { encoding, buffer, headers } }