@atproto/xrpc-server 0.10.14 → 0.10.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tests/_util.ts CHANGED
@@ -1,12 +1,21 @@
1
1
  import { once } from 'node:events'
2
2
  import * as http from 'node:http'
3
3
  import express from 'express'
4
- import * as xrpc from '../src'
5
- import { AuthRequiredError } from '../src'
4
+ import {
5
+ LexiconDocument,
6
+ LexiconIterableIndexer,
7
+ LexiconSchemaBuilder,
8
+ } from '@atproto/lex-document'
9
+ import { LexiconDoc } from '@atproto/lexicon'
10
+ import {
11
+ AuthRequiredError,
12
+ MethodConfigOrHandler,
13
+ Options,
14
+ Server,
15
+ StreamConfigOrHandler,
16
+ } from '../src'
6
17
 
7
- export async function createServer({
8
- router,
9
- }: xrpc.Server): Promise<http.Server> {
18
+ export async function createServer({ router }: Server): Promise<http.Server> {
10
19
  const app = express()
11
20
  app.use(router)
12
21
  const httpServer = app.listen(0)
@@ -53,3 +62,51 @@ export function basicAuthHeaders(creds: {
53
62
  Buffer.from(`${creds.username}:${creds.password}`).toString('base64'),
54
63
  }
55
64
  }
65
+
66
+ /**
67
+ * Builds a lexicon server based on an `@atproto/lexicon`
68
+ * {@link import('@atproto/lexicon').Lexicons} instance. Validation will be
69
+ * performed by {@link import('@atproto/lexicon').Lexicons}'s various assertion
70
+ * methods. This allows for testing the server's integration with
71
+ * `@atproto/lexicon`.
72
+ */
73
+ export async function buildMethodLexicons(
74
+ lexicons: LexiconDoc[],
75
+ handlers: Record<string, MethodConfigOrHandler | StreamConfigOrHandler>,
76
+ options?: Options,
77
+ ) {
78
+ const server = new Server(structuredClone(lexicons), options)
79
+ for (const [id, handler] of Object.entries(handlers)) {
80
+ const def = server.lex.getDef(id)
81
+ if (def?.type === 'subscription') {
82
+ server.addStreamMethod(id, handler as StreamConfigOrHandler)
83
+ } else {
84
+ server.method(id, handler as MethodConfigOrHandler)
85
+ }
86
+ }
87
+ return server
88
+ }
89
+
90
+ /**
91
+ * Builds a lexicon server based on `@atproto/lex`'s
92
+ * {@link import('@atproto/lex').Query},
93
+ * {@link import('@atproto/lex').Procedure}, and
94
+ * {@link import('@atproto/lex').Subscription} method definitions. Validation
95
+ * will be performed through built schema verifiers created by
96
+ * {@link LexiconSchemaBuilder}. This helper allows for testing the server's
97
+ * integration with `@atproto/lex`.
98
+ */
99
+ export async function buildAddLexicons(
100
+ lexicons: LexiconDocument[],
101
+ handlers: Record<string, MethodConfigOrHandler | StreamConfigOrHandler>,
102
+ options?: Options,
103
+ ) {
104
+ const server = new Server(undefined, options)
105
+ await using indexer = new LexiconIterableIndexer(structuredClone(lexicons))
106
+ await using builder = new LexiconSchemaBuilder(indexer)
107
+ for (const [id, handler] of Object.entries(handlers)) {
108
+ const schema = await builder.buildFullRef(`${id}#main`)
109
+ server.add(schema as any, handler as any)
110
+ }
111
+ return server
112
+ }