Skip to content

Commit

Permalink
apply pipethrough to most pds routes
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Feb 7, 2024
1 parent a737327 commit aa1dd7e
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 80 deletions.
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/feed/getActorFeeds.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
14 changes: 4 additions & 10 deletions packages/pds/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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'],
}),
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/feed/getFeedGenerator.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/graph/getFollows.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
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({
auth: ctx.authVerifier.accessOrRole,
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,
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/graph/getList.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/graph/getListBlocks.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/graph/getListMutes.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/graph/getLists.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
9 changes: 4 additions & 5 deletions packages/pds/src/api/app/bsky/graph/getMutes.ts
Original file line number Diff line number Diff line change
@@ -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,
}
},
})
}
Original file line number Diff line number Diff line change
@@ -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),
)
},
})
}
16 changes: 7 additions & 9 deletions packages/pds/src/api/app/bsky/notification/getUnreadCount.ts
Original file line number Diff line number Diff line change
@@ -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),
)
},
})
}
16 changes: 7 additions & 9 deletions packages/pds/src/api/app/bsky/notification/listNotifications.ts
Original file line number Diff line number Diff line change
@@ -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),
)
},
})
}
9 changes: 6 additions & 3 deletions packages/pds/src/pipethrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
}

Expand Down

0 comments on commit aa1dd7e

Please sign in to comment.