-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathindex.ts
192 lines (169 loc) · 5.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import http from 'http'
import chalk from 'chalk'
import crytpo from 'crypto'
import PDSServer, {
Database as PDSDatabase,
MemoryBlobStore,
ServerConfig as PDSServerConfig,
} from '@atproto/pds'
import * as plc from '@atproto/plc'
import * as crypto from '@atproto/crypto'
import AtpAgent from '@atproto/api'
import { ServerType, ServerConfig, StartParams } from './types.js'
import { HOUR } from '@atproto/common'
interface Startable {
start(): Promise<http.Server>
}
interface Destroyable {
destroy(): Promise<void>
}
export class DevEnvServer {
inst?: Destroyable
constructor(
private env: DevEnv,
public type: ServerType,
public port: number,
) {}
get name() {
return {
[ServerType.PersonalDataServer]: '🌞 Personal Data server',
[ServerType.DidPlaceholder]: '👤 DID Placeholder server',
}[this.type]
}
get description() {
return `[${chalk.bold(this.port)}] ${this.name}`
}
get url() {
return `http://localhost:${this.port}`
}
async start() {
if (this.inst) {
throw new Error('Already started')
}
const startServer = async (server: Startable): Promise<void> => {
try {
await server.start()
console.log(`${this.description} started ${chalk.gray(this.url)}`)
} catch (err) {
console.log(`${this.description} failed to start:`, err)
}
}
switch (this.type) {
case ServerType.PersonalDataServer: {
if (!this.env.plcUrl) {
throw new Error('Must be running a PLC server to start a PDS')
}
const db = await PDSDatabase.memory()
await db.migrateToLatestOrThrow()
const keypair = await crypto.EcdsaKeypair.create()
const blobstore = new MemoryBlobStore()
const plcClient = new plc.PlcClient(this.env.plcUrl)
const serverDid = await plcClient.createDid(
keypair,
keypair.did(),
'localhost',
`http://localhost:${this.port}`,
)
const pds = PDSServer.create({
db,
blobstore,
keypair,
config: new PDSServerConfig({
debugMode: true,
version: '0.0.0',
scheme: 'http',
hostname: 'localhost',
port: this.port,
didPlcUrl: this.env.plcUrl,
serverDid,
recoveryKey: keypair.did(),
jwtSecret: crytpo.randomBytes(8).toString('base64'),
availableUserDomains: ['.test'],
appUrlPasswordReset: 'app://password-reset',
// @TODO setup ethereal.email creds and set emailSmtpUrl here
emailNoReplyAddress: 'noreply@blueskyweb.xyz',
adminPassword: 'password',
inviteRequired: false,
imgUriSalt: '9dd04221f5755bce5f55f47464c27e1e',
imgUriKey:
'f23ecd142835025f42c3db2cf25dd813956c178392760256211f9d315f8ab4d8',
privacyPolicyUrl: 'https://example.com/privacy',
termsOfServiceUrl: 'https://example.com/tos',
maxSubscriptionBuffer: 200,
repoBackfillLimitMs: HOUR,
}),
})
await startServer(pds)
this.inst = pds
break
}
case ServerType.DidPlaceholder: {
const db = plc.Database.memory()
await db.migrateToLatestOrThrow()
const plcServer = plc.PlcServer.create({ db, port: this.port })
await startServer(plcServer)
this.inst = plcServer
break
}
default:
throw new Error(`Unsupported server type: ${this.type}`)
}
}
async close() {
if (this.inst) {
console.log(`Closing ${this.description}`)
await this.inst.destroy()
}
}
getClient(): AtpAgent {
return new AtpAgent({ service: `http://localhost:${this.port}` })
}
}
export class DevEnv {
plcUrl: string | undefined
servers: Map<number, DevEnvServer> = new Map()
static async create(params: StartParams): Promise<DevEnv> {
const devEnv = new DevEnv()
for (const cfg of params.servers || []) {
await devEnv.add(cfg)
}
return devEnv
}
async add(cfg: ServerConfig) {
if (this.servers.has(cfg.port)) {
throw new Error(`Port ${cfg.port} is in use`)
} else if (cfg.type === ServerType.DidPlaceholder && this.plcUrl) {
throw new Error('There should only be one plc server')
}
const server = new DevEnvServer(this, cfg.type, cfg.port)
await server.start()
this.servers.set(cfg.port, server)
if (cfg.type === ServerType.DidPlaceholder) {
this.plcUrl = `http://localhost:${cfg.port}`
}
}
async remove(server: number | DevEnvServer) {
const port = typeof server === 'number' ? server : server.port
const inst = this.servers.get(port)
if (inst) {
await inst.close()
this.servers.delete(port)
}
}
async shutdown() {
for (const server of this.servers.values()) {
await server.close()
}
}
hasType(type: ServerType) {
for (const s of this.servers.values()) {
if (s.type === type) {
return true
}
}
return false
}
listOfType(type: ServerType): DevEnvServer[] {
return Array.from(this.servers.values()).filter((s) => s.type === type)
}
}