@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.
@@ -5,9 +5,14 @@ import { Readable } from 'node:stream'
5
5
  import { LexiconDoc } from '@atproto/lexicon'
6
6
  import { XrpcClient } from '@atproto/xrpc'
7
7
  import * as xrpcServer from '../src'
8
- import { closeServer, createServer } from './_util'
8
+ import {
9
+ buildAddLexicons,
10
+ buildMethodLexicons,
11
+ closeServer,
12
+ createServer,
13
+ } from './_util'
9
14
 
10
- const LEXICONS: LexiconDoc[] = [
15
+ const LEXICONS = [
11
16
  {
12
17
  lexicon: 1,
13
18
  id: 'io.example.pingOne',
@@ -81,18 +86,16 @@ const LEXICONS: LexiconDoc[] = [
81
86
  },
82
87
  },
83
88
  },
84
- ]
89
+ ] as const satisfies LexiconDoc[]
85
90
 
86
- describe('Procedures', () => {
87
- let s: http.Server
88
- const server = xrpcServer.createServer(LEXICONS)
89
- server.method('io.example.pingOne', (ctx) => {
91
+ const handlers = {
92
+ 'io.example.pingOne': (ctx: xrpcServer.HandlerContext) => {
90
93
  return { encoding: 'text/plain', body: ctx.params.message }
91
- })
92
- server.method('io.example.pingTwo', (ctx) => {
94
+ },
95
+ 'io.example.pingTwo': (ctx: xrpcServer.HandlerContext) => {
93
96
  return { encoding: 'text/plain', body: ctx.input?.body }
94
- })
95
- server.method('io.example.pingThree', async (ctx) => {
97
+ },
98
+ 'io.example.pingThree': async (ctx: xrpcServer.HandlerContext) => {
96
99
  assert(ctx.input?.body instanceof Readable, 'Input not readable')
97
100
  const buffers: Buffer[] = []
98
101
  for await (const data of ctx.input.body) {
@@ -102,56 +105,72 @@ describe('Procedures', () => {
102
105
  encoding: 'application/octet-stream',
103
106
  body: Buffer.concat(buffers),
104
107
  }
105
- })
106
- server.method('io.example.pingFour', (ctx) => {
108
+ },
109
+ 'io.example.pingFour': (ctx: xrpcServer.HandlerContext) => {
107
110
  return {
108
111
  encoding: 'application/json',
109
112
  body: { message: ctx.input?.body?.['message'] },
110
113
  }
111
- })
114
+ },
115
+ }
112
116
 
113
- let client: XrpcClient
114
- beforeAll(async () => {
115
- s = await createServer(server)
116
- const { port } = s.address() as AddressInfo
117
- client = new XrpcClient(`http://localhost:${port}`, LEXICONS)
118
- })
119
- afterAll(async () => {
120
- await closeServer(s)
121
- })
117
+ for (const buildServer of [buildMethodLexicons, buildAddLexicons]) {
118
+ describe(buildServer, () => {
119
+ let s: http.Server
120
+ let client: XrpcClient
121
+ let url: string
122
+ beforeAll(async () => {
123
+ const server = await buildServer(LEXICONS, handlers)
124
+ s = await createServer(server)
125
+ const { port } = s.address() as AddressInfo
126
+ url = `http://localhost:${port}`
127
+ client = new XrpcClient(url, LEXICONS)
128
+ })
129
+ afterAll(async () => {
130
+ if (s) await closeServer(s)
131
+ })
122
132
 
123
- it('serves requests', async () => {
124
- const res1 = await client.call('io.example.pingOne', {
125
- message: 'hello world',
133
+ test('io.example.pingOne', async () => {
134
+ const res = await client.call('io.example.pingOne', {
135
+ message: 'hello world',
136
+ })
137
+ expect(res.success).toBeTruthy()
138
+ expect(res.headers['content-type']).toBe('text/plain; charset=utf-8')
139
+ expect(res.data).toBe('hello world')
126
140
  })
127
- expect(res1.success).toBeTruthy()
128
- expect(res1.headers['content-type']).toBe('text/plain; charset=utf-8')
129
- expect(res1.data).toBe('hello world')
130
141
 
131
- const res2 = await client.call('io.example.pingTwo', {}, 'hello world', {
132
- encoding: 'text/plain',
142
+ test('io.example.pingTwo', async () => {
143
+ const res = await client.call('io.example.pingTwo', {}, 'hello world', {
144
+ encoding: 'text/plain',
145
+ })
146
+ expect(res.success).toBeTruthy()
147
+ expect(res.headers['content-type']).toBe('text/plain; charset=utf-8')
148
+ expect(res.data).toBe('hello world')
133
149
  })
134
- expect(res2.success).toBeTruthy()
135
- expect(res2.headers['content-type']).toBe('text/plain; charset=utf-8')
136
- expect(res2.data).toBe('hello world')
137
150
 
138
- const res3 = await client.call(
139
- 'io.example.pingThree',
140
- {},
141
- new TextEncoder().encode('hello world'),
142
- { encoding: 'application/octet-stream' },
143
- )
144
- expect(res3.success).toBeTruthy()
145
- expect(res3.headers['content-type']).toBe('application/octet-stream')
146
- expect(new TextDecoder().decode(res3.data)).toBe('hello world')
151
+ test('io.example.pingThree', async () => {
152
+ const res = await client.call(
153
+ 'io.example.pingThree',
154
+ {},
155
+ new TextEncoder().encode('hello world'),
156
+ { encoding: 'application/octet-stream' },
157
+ )
158
+ expect(res.success).toBeTruthy()
159
+ expect(res.headers['content-type']).toBe('application/octet-stream')
160
+ expect(new TextDecoder().decode(res.data)).toBe('hello world')
161
+ })
147
162
 
148
- const res4 = await client.call(
149
- 'io.example.pingFour',
150
- {},
151
- { message: 'hello world' },
152
- )
153
- expect(res4.success).toBeTruthy()
154
- expect(res4.headers['content-type']).toBe('application/json; charset=utf-8')
155
- expect(res4.data?.message).toBe('hello world')
163
+ test('io.example.pingFour', async () => {
164
+ const res = await client.call(
165
+ 'io.example.pingFour',
166
+ {},
167
+ { message: 'hello world' },
168
+ )
169
+ expect(res.success).toBeTruthy()
170
+ expect(res.headers['content-type']).toBe(
171
+ 'application/json; charset=utf-8',
172
+ )
173
+ expect(res.data?.message).toBe('hello world')
174
+ })
156
175
  })
157
- })
176
+ }
@@ -3,9 +3,14 @@ import { AddressInfo } from 'node:net'
3
3
  import { LexiconDoc } from '@atproto/lexicon'
4
4
  import { XrpcClient } from '@atproto/xrpc'
5
5
  import * as xrpcServer from '../src'
6
- import { closeServer, createServer } from './_util'
6
+ import {
7
+ buildAddLexicons,
8
+ buildMethodLexicons,
9
+ closeServer,
10
+ createServer,
11
+ } from './_util'
7
12
 
8
- const LEXICONS: LexiconDoc[] = [
13
+ const LEXICONS = [
9
14
  {
10
15
  lexicon: 1,
11
16
  id: 'io.example.pingOne',
@@ -65,59 +70,71 @@ const LEXICONS: LexiconDoc[] = [
65
70
  },
66
71
  },
67
72
  },
68
- ]
73
+ ] as const satisfies LexiconDoc[]
69
74
 
70
- describe('Queries', () => {
71
- let s: http.Server
72
- const server = xrpcServer.createServer(LEXICONS)
73
- server.method('io.example.pingOne', (ctx) => {
75
+ const handlers = {
76
+ 'io.example.pingOne': (ctx: xrpcServer.HandlerContext) => {
74
77
  return { encoding: 'text/plain', body: ctx.params.message }
75
- })
76
- server.method('io.example.pingTwo', (ctx) => {
78
+ },
79
+ 'io.example.pingTwo': (ctx: xrpcServer.HandlerContext) => {
77
80
  return {
78
81
  encoding: 'application/octet-stream',
79
82
  body: new TextEncoder().encode(String(ctx.params.message)),
80
83
  }
81
- })
82
- server.method('io.example.pingThree', (ctx) => {
84
+ },
85
+ 'io.example.pingThree': (ctx: xrpcServer.HandlerContext) => {
83
86
  return {
84
87
  encoding: 'application/json',
85
88
  body: { message: ctx.params.message },
86
89
  headers: { 'x-test-header-name': 'test-value' },
87
90
  }
88
- })
91
+ },
92
+ }
89
93
 
90
- let client: XrpcClient
91
- beforeAll(async () => {
92
- s = await createServer(server)
93
- const { port } = s.address() as AddressInfo
94
- client = new XrpcClient(`http://localhost:${port}`, LEXICONS)
95
- })
96
- afterAll(async () => {
97
- await closeServer(s)
98
- })
94
+ for (const buildServer of [buildMethodLexicons, buildAddLexicons]) {
95
+ describe(buildServer, () => {
96
+ let s: http.Server
97
+ let client: XrpcClient
98
+ let url: string
99
+ beforeAll(async () => {
100
+ const server = await buildServer(LEXICONS, handlers)
101
+ s = await createServer(server)
102
+ const { port } = s.address() as AddressInfo
103
+ url = `http://localhost:${port}`
104
+ client = new XrpcClient(url, LEXICONS)
105
+ })
106
+ afterAll(async () => {
107
+ if (s) await closeServer(s)
108
+ })
99
109
 
100
- it('serves requests', async () => {
101
- const res1 = await client.call('io.example.pingOne', {
102
- message: 'hello world',
110
+ test('io.example.pingOne', async () => {
111
+ const res = await client.call('io.example.pingOne', {
112
+ message: 'hello world',
113
+ })
114
+ expect(res.success).toBeTruthy()
115
+ expect(res.headers['content-type']).toBe('text/plain; charset=utf-8')
116
+ expect(res.data).toBe('hello world')
103
117
  })
104
- expect(res1.success).toBeTruthy()
105
- expect(res1.headers['content-type']).toBe('text/plain; charset=utf-8')
106
- expect(res1.data).toBe('hello world')
107
118
 
108
- const res2 = await client.call('io.example.pingTwo', {
109
- message: 'hello world',
119
+ test('io.example.pingTwo', async () => {
120
+ const res = await client.call('io.example.pingTwo', {
121
+ message: 'hello world',
122
+ })
123
+ expect(res.success).toBeTruthy()
124
+ expect(res.headers['content-type']).toBe('application/octet-stream')
125
+ expect(new TextDecoder().decode(res.data)).toBe('hello world')
110
126
  })
111
- expect(res2.success).toBeTruthy()
112
- expect(res2.headers['content-type']).toBe('application/octet-stream')
113
- expect(new TextDecoder().decode(res2.data)).toBe('hello world')
114
127
 
115
- const res3 = await client.call('io.example.pingThree', {
116
- message: 'hello world',
128
+ test('io.example.pingThree', async () => {
129
+ const res = await client.call('io.example.pingThree', {
130
+ message: 'hello world',
131
+ })
132
+ expect(res.success).toBeTruthy()
133
+ expect(res.headers['content-type']).toBe(
134
+ 'application/json; charset=utf-8',
135
+ )
136
+ expect(res.data?.message).toBe('hello world')
137
+ expect(res.headers['x-test-header-name']).toEqual('test-value')
117
138
  })
118
- expect(res3.success).toBeTruthy()
119
- expect(res3.headers['content-type']).toBe('application/json; charset=utf-8')
120
- expect(res3.data?.message).toBe('hello world')
121
- expect(res3.headers['x-test-header-name']).toEqual('test-value')
122
139
  })
123
- })
140
+ }