Skip to content

Commit

Permalink
Pass through topics headers (#2447)
Browse files Browse the repository at this point in the history
* Pass through topics headers

* Add PDS exception options, pass through headers

* Update packages/bsky/src/api/app/bsky/actor/getSuggestions.ts

Co-authored-by: devin ivy <devinivy@gmail.com>

* Feedback

---------

Co-authored-by: devin ivy <devinivy@gmail.com>
  • Loading branch information
estrattonbailey and devinivy authored Apr 26, 2024
1 parent 651d4c2 commit 65a9fd7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/bsky/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default function (server: Server, ctx: AppContext) {
const hydrateCtx = await ctx.hydrator.createContext({ viewer, labelers })
const headers = noUndefinedVals({
'accept-language': req.headers['accept-language'],
'x-bsky-topics': Array.isArray(req.headers['x-bsky-topics'])
? req.headers['x-bsky-topics'].join(',')
: req.headers['x-bsky-topics'],
})
const { resHeaders: resultHeaders, ...result } = await getSuggestions(
{ ...params, hydrateCtx, headers },
Expand Down
3 changes: 3 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export default function (server: Server, ctx: AppContext) {
const headers = noUndefinedVals({
authorization: req.headers['authorization'],
'accept-language': req.headers['accept-language'],
'x-bsky-topics': Array.isArray(req.headers['x-bsky-topics'])
? req.headers['x-bsky-topics'].join(',')
: req.headers['x-bsky-topics'],
})
// @NOTE feed cursors should not be affected by appview swap
const {
Expand Down
4 changes: 3 additions & 1 deletion packages/pds/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export default function (server: Server, ctx: AppContext) {
auth: ctx.authVerifier.access,
handler: async ({ req, auth }) => {
const requester = auth.credentials.did
return pipethrough(ctx, req, requester)
return pipethrough(ctx, req, requester, undefined, {
reqHeadersToForward: ['x-bsky-topics'],
})
},
})
}
4 changes: 3 additions & 1 deletion packages/pds/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export default function (server: Server, ctx: AppContext) {
{ feed: params.feed },
await ctx.appviewAuthHeaders(requester),
)
return pipethrough(ctx, req, requester, feed.view.did)
return pipethrough(ctx, req, requester, feed.view.did, {
reqHeadersToForward: ['x-bsky-topics'],
})
},
})
}
16 changes: 15 additions & 1 deletion packages/pds/src/pipethrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { httpLogger } from './logger'
import { getServiceEndpoint, noUndefinedVals } from '@atproto/common'
import AppContext from './context'

type PipethroughOptions = {
/**
* Request headers to pass-through, in addition to those defined in
* {@link REQ_HEADERS_TO_FORWARD}
*/
reqHeadersToForward?: string[]
}

const defaultService = (
ctx: AppContext,
path: string,
Expand Down Expand Up @@ -39,12 +47,14 @@ export const pipethrough = async (
req: express.Request,
requester?: string,
audOverride?: string,
options?: PipethroughOptions,
): Promise<HandlerPipeThrough> => {
const { url, headers } = await createUrlAndHeaders(
ctx,
req,
requester,
audOverride,
options,
)
const reqInit: RequestInit = {
headers,
Expand Down Expand Up @@ -106,6 +116,7 @@ export const createUrlAndHeaders = async (
req: express.Request,
requester?: string,
audOverride?: string,
options?: PipethroughOptions,
): Promise<{ url: URL; headers: { authorization?: string } }> => {
const proxyTo = await parseProxyHeader(ctx, req)
const defaultProxy = defaultService(ctx, req.path)
Expand All @@ -121,8 +132,11 @@ export const createUrlAndHeaders = async (
const headers = requester
? (await ctx.serviceAuthHeaders(requester, aud)).headers
: {}
const allowedHeaders = REQ_HEADERS_TO_FORWARD.concat(
options?.reqHeadersToForward ?? [],
)
// forward select headers to upstream services
for (const header of REQ_HEADERS_TO_FORWARD) {
for (const header of allowedHeaders) {
const val = req.headers[header]
if (val) {
headers[header] = val
Expand Down

0 comments on commit 65a9fd7

Please sign in to comment.