Skip to content

Commit

Permalink
Dev-env introspection server (#2470)
Browse files Browse the repository at this point in the history
* add dev-env introspection server

* Update packages/dev-env/src/introspect.ts

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

* lost a bracket

---------

Co-authored-by: devin ivy <devinivy@gmail.com>
  • Loading branch information
dholms and devinivy authored May 8, 2024
1 parent 5226cbc commit c812902
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/dev-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"get-port": "^5.1.1",
"multiformats": "^9.9.0",
"uint8arrays": "3.0.0"
},
"devDependencies": {
"@types/express": "^4.17.13"
}
}
6 changes: 6 additions & 0 deletions packages/dev-env/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ const run = async () => {
publicUrl: 'http://localhost:2584',
},
plc: { port: 2582 },
introspect: { port: 2581 },
})
mockMailer(network.pds)
await generateMockSetup(network)

if (network.introspect) {
console.log(
`🔍 Dev-env introspection server started http://localhost:${network.introspect.port}`,
)
}
console.log(
`👤 DID Placeholder server started http://localhost:${network.plc.port}`,
)
Expand Down
54 changes: 54 additions & 0 deletions packages/dev-env/src/introspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import http from 'node:http'
import events from 'node:events'
import express from 'express'
import { TestPlc } from './plc'
import { TestPds } from './pds'
import { TestBsky } from './bsky'
import { TestOzone } from './ozone'

export class IntrospectServer {
constructor(
public port: number,
public server: http.Server,
) {}

static async start(
port: number,
plc: TestPlc,
pds: TestPds,
bsky: TestBsky,
ozone: TestOzone,
) {
const app = express()
app.get('/', (_req, res) => {
res.status(200).send({
plc: {
url: plc.url,
},
pds: {
url: pds.url,
did: pds.ctx.cfg.service.did,
},
bsky: {
url: bsky.url,
did: bsky.ctx.cfg.serverDid,
},
ozone: {
url: ozone.url,
did: ozone.ctx.cfg.service.did,
},
db: {
url: ozone.ctx.cfg.db.postgresUrl,
},
})
})
const server = app.listen(port)
await events.once(server, 'listening')
return new IntrospectServer(port, server)
}

async close() {
this.server.close()
await events.once(this.server, 'close')
}
}
16 changes: 15 additions & 1 deletion packages/dev-env/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { OzoneServiceProfile } from './ozone-service-profile'
import { mockNetworkUtilities } from './util'
import { TestNetworkNoAppView } from './network-no-appview'
import { EXAMPLE_LABELER } from './const'
import { IntrospectServer } from './introspect'

const ADMIN_USERNAME = 'admin'
const ADMIN_PASSWORD = 'admin-pass'
Expand All @@ -22,6 +23,7 @@ export class TestNetwork extends TestNetworkNoAppView {
public pds: TestPds,
public bsky: TestBsky,
public ozone: TestOzone,
public introspect?: IntrospectServer,
) {
super(plc, pds)
}
Expand Down Expand Up @@ -118,7 +120,18 @@ export class TestNetwork extends TestNetworkNoAppView {
await bsky.sub.background.processAll()
await thirdPartyPds.close()

return new TestNetwork(plc, pds, bsky, ozone)
let introspect: IntrospectServer | undefined = undefined
if (params.introspect?.port) {
introspect = await IntrospectServer.start(
params.introspect.port,
plc,
pds,
bsky,
ozone,
)
}

return new TestNetwork(plc, pds, bsky, ozone, introspect)
}

async processFullSubscription(timeout = 5000) {
Expand Down Expand Up @@ -177,5 +190,6 @@ export class TestNetwork extends TestNetworkNoAppView {
await this.bsky.close()
await this.pds.close()
await this.plc.close()
await this.introspect?.close()
}
}
5 changes: 5 additions & 0 deletions packages/dev-env/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import * as bsync from '@atproto/bsync'
import * as ozone from '@atproto/ozone'
import { ExportableKeypair, Keypair } from '@atproto/crypto'

export type IntrospectConfig = {
port?: number
}

export type PlcConfig = {
port?: number
version?: string
Expand Down Expand Up @@ -45,6 +49,7 @@ export type TestServerParams = {
plc: Partial<PlcConfig>
bsky: Partial<BskyConfig>
ozone: Partial<OzoneConfig>
introspect: Partial<IntrospectConfig>
}

export type DidAndKey = {
Expand Down
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c812902

Please sign in to comment.