Skip to content

Commit

Permalink
Polish on entryway blob uploads and gets (#1814)
Browse files Browse the repository at this point in the history
* fix account creation to work between entryway and non-entryway

* stream blob uploads

* serve redirect to new pds on getBlob

* tidy did doc session tests
  • Loading branch information
devinivy authored Nov 4, 2023
1 parent 067eb64 commit 586cc06
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 178 deletions.
20 changes: 3 additions & 17 deletions packages/api/tests/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert'
import { defaultFetchHandler } from '@atproto/xrpc'
import {
AtpAgent,
Expand All @@ -7,17 +6,13 @@ import {
AtpSessionData,
} from '..'
import { TestNetworkNoAppView } from '@atproto/dev-env'
import { getPdsEndpoint, isValidDidDoc } from '@atproto/common-web'

describe('agent', () => {
let network: TestNetworkNoAppView

beforeAll(async () => {
network = await TestNetworkNoAppView.create({
dbPostgresSchema: 'api_agent',
pds: {
enableDidDocWithSession: true,
},
})
})

Expand Down Expand Up @@ -48,19 +43,16 @@ describe('agent', () => {
expect(agent.session?.did).toEqual(res.data.did)
expect(agent.session?.email).toEqual('[email protected]')
expect(agent.session?.emailConfirmed).toEqual(false)
assert(isValidDidDoc(res.data.didDoc))
expect(agent.api.xrpc.uri.origin).toEqual(getPdsEndpoint(res.data.didDoc))

const { data: sessionInfo } = await agent.api.com.atproto.server.getSession(
{},
)
expect(sessionInfo).toMatchObject({
expect(sessionInfo).toEqual({
did: res.data.did,
handle: res.data.handle,
email: '[email protected]',
emailConfirmed: false,
})
expect(isValidDidDoc(sessionInfo.didDoc)).toBe(true)

expect(events.length).toEqual(1)
expect(events[0]).toEqual('create')
Expand Down Expand Up @@ -98,18 +90,15 @@ describe('agent', () => {
expect(agent2.session?.did).toEqual(res1.data.did)
expect(agent2.session?.email).toEqual('[email protected]')
expect(agent2.session?.emailConfirmed).toEqual(false)
assert(isValidDidDoc(res1.data.didDoc))
expect(agent2.api.xrpc.uri.origin).toEqual(getPdsEndpoint(res1.data.didDoc))

const { data: sessionInfo } =
await agent2.api.com.atproto.server.getSession({})
expect(sessionInfo).toMatchObject({
expect(sessionInfo).toEqual({
did: res1.data.did,
handle: res1.data.handle,
email,
emailConfirmed: false,
})
expect(isValidDidDoc(sessionInfo.didDoc)).toBe(true)

expect(events.length).toEqual(2)
expect(events[0]).toEqual('create')
Expand Down Expand Up @@ -144,18 +133,15 @@ describe('agent', () => {
expect(agent2.hasSession).toEqual(true)
expect(agent2.session?.handle).toEqual(res1.data.handle)
expect(agent2.session?.did).toEqual(res1.data.did)
assert(isValidDidDoc(res1.data.didDoc))
expect(agent2.api.xrpc.uri.origin).toEqual(getPdsEndpoint(res1.data.didDoc))

const { data: sessionInfo } =
await agent2.api.com.atproto.server.getSession({})
expect(sessionInfo).toMatchObject({
expect(sessionInfo).toEqual({
did: res1.data.did,
handle: res1.data.handle,
email: res1.data.email,
emailConfirmed: false,
})
expect(isValidDidDoc(sessionInfo.didDoc)).toBe(true)

expect(events.length).toEqual(2)
expect(events[0]).toEqual('create')
Expand Down
23 changes: 13 additions & 10 deletions packages/pds/src/api/com/atproto/repo/uploadBlob.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { streamToBytes } from '@atproto/common'
import { Server } from '../../../../lexicon'
import AppContext from '../../../../context'
import {
authPassthru,
ensureThisPds,
getStreamingRequestInit,
proxy,
resultPassthru,
} from '../../../proxy'

export default function (server: Server, ctx: AppContext) {
Expand All @@ -16,14 +15,18 @@ export default function (server: Server, ctx: AppContext) {
ctx,
auth.credentials.audience,
async (agent) => {
const result = await agent.api.com.atproto.repo.uploadBlob(
await streamToBytes(input.body), // @TODO proxy streaming
{
...authPassthru(req),
encoding: input.encoding,
},
)
return resultPassthru(result)
const reqInit = getStreamingRequestInit(input.body)
reqInit.method = req.method
reqInit.headers = {
...authPassthru(req)?.headers,
'content-type':
req.headers['content-type'] || 'application/octet-stream',
}
const res = await fetch(`${agent.service.origin}${req.path}`, reqInit)
return {
encoding: 'application/json' as const,
body: await res.json(),
}
},
)
if (proxied !== null) {
Expand Down
Loading

0 comments on commit 586cc06

Please sign in to comment.