@atproto/tap 0.3.4 → 0.3.5

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.
@@ -1,343 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { l } from '@atproto/lex'
3
- import {
4
- CreateEvent,
5
- DeleteEvent,
6
- LexIndexer,
7
- UpdateEvent,
8
- } from '../src/lex-indexer.js'
9
- import { IdentityEvent, RecordEvent } from '../src/types.js'
10
- import {
11
- createIdentityEvent,
12
- createMockOpts,
13
- createRecordEvent as baseCreateRecordEvent,
14
- } from './_util.js'
15
-
16
- // Test lexicon definitions
17
- const postNsid = 'com.example.post'
18
- type Post = {
19
- $type: 'com.example.post'
20
- text: string
21
- }
22
- const post = {
23
- main: l.record<'tid', Post>('tid', postNsid, l.object({ text: l.string() })),
24
- }
25
-
26
- const likeNsid = 'com.example.like'
27
- type Like = {
28
- $type: 'com.example.like'
29
- subject: string
30
- }
31
- const like = {
32
- main: l.record<'tid', Like>(
33
- 'tid',
34
- likeNsid,
35
- l.object({ subject: l.string() }),
36
- ),
37
- }
38
-
39
- const createRecordEvent = (overrides: Partial<RecordEvent> = {}): RecordEvent =>
40
- baseCreateRecordEvent({
41
- collection: postNsid,
42
- record: { $type: postNsid, text: 'hello' },
43
- ...overrides,
44
- })
45
-
46
- describe('LexIndexer', () => {
47
- describe('handler registration', () => {
48
- it('registers create handler', async () => {
49
- const indexer = new LexIndexer()
50
- const received: CreateEvent<Post>[] = []
51
-
52
- indexer.create(post, async (evt) => {
53
- received.push(evt)
54
- })
55
-
56
- const opts = createMockOpts()
57
- await indexer.onEvent(createRecordEvent(), opts)
58
-
59
- expect(received).toHaveLength(1)
60
- expect(received[0].action).toBe('create')
61
- expect(received[0].record.text).toBe('hello')
62
- expect(received[0].cid).toBe(
63
- 'bafyreiclp443lavogvhj3d2ob2cxbfuscni2k5jk7bebjzg7khl3esabwq',
64
- )
65
- })
66
-
67
- it('registers update handler', async () => {
68
- const indexer = new LexIndexer()
69
- const received: UpdateEvent<Post>[] = []
70
-
71
- indexer.update(post, async (evt) => {
72
- received.push(evt)
73
- })
74
-
75
- const opts = createMockOpts()
76
- await indexer.onEvent(
77
- createRecordEvent({
78
- action: 'update',
79
- record: { $type: postNsid, text: 'updated' },
80
- }),
81
- opts,
82
- )
83
-
84
- expect(received).toHaveLength(1)
85
- expect(received[0].action).toBe('update')
86
- expect(received[0].record.text).toBe('updated')
87
- })
88
-
89
- it('registers delete handler', async () => {
90
- const indexer = new LexIndexer()
91
- const received: DeleteEvent[] = []
92
-
93
- indexer.delete(post, async (evt) => {
94
- received.push(evt)
95
- })
96
-
97
- const opts = createMockOpts()
98
- await indexer.onEvent(
99
- createRecordEvent({
100
- action: 'delete',
101
- record: undefined,
102
- cid: undefined,
103
- }),
104
- opts,
105
- )
106
-
107
- expect(received).toHaveLength(1)
108
- expect(received[0].action).toBe('delete')
109
- })
110
-
111
- it('registers put handler for both create and update', async () => {
112
- const indexer = new LexIndexer()
113
- const received: Array<{ action: string; text: string }> = []
114
-
115
- indexer.put(post, async (evt) => {
116
- received.push({ action: evt.action, text: evt.record.text })
117
- })
118
-
119
- const opts1 = createMockOpts()
120
- await indexer.onEvent(createRecordEvent({ action: 'create' }), opts1)
121
-
122
- const opts2 = createMockOpts()
123
- await indexer.onEvent(
124
- createRecordEvent({
125
- action: 'update',
126
- record: { $type: postNsid, text: 'updated' },
127
- }),
128
- opts2,
129
- )
130
-
131
- expect(received).toHaveLength(2)
132
- expect(received[0].action).toBe('create')
133
- expect(received[1].action).toBe('update')
134
- })
135
- })
136
-
137
- describe('handler routing', () => {
138
- it('routes to correct handler by collection', async () => {
139
- const indexer = new LexIndexer()
140
- const postEvents: CreateEvent<Post>[] = []
141
- const likeEvents: CreateEvent<Like>[] = []
142
-
143
- indexer.create(post, async (evt) => {
144
- postEvents.push(evt)
145
- })
146
- indexer.create(like, async (evt) => {
147
- likeEvents.push(evt)
148
- })
149
-
150
- const opts1 = createMockOpts()
151
- await indexer.onEvent(createRecordEvent(), opts1)
152
-
153
- const opts2 = createMockOpts()
154
- await indexer.onEvent(
155
- createRecordEvent({
156
- collection: likeNsid,
157
- record: { $type: likeNsid, subject: 'at://did:example:bob/post/123' },
158
- }),
159
- opts2,
160
- )
161
-
162
- expect(postEvents).toHaveLength(1)
163
- expect(likeEvents).toHaveLength(1)
164
- })
165
-
166
- it('routes to other handler for unregistered collections', async () => {
167
- const indexer = new LexIndexer()
168
- const otherEvents: RecordEvent[] = []
169
-
170
- indexer.create(post, async () => {})
171
- indexer.other(async (evt) => {
172
- otherEvents.push(evt)
173
- })
174
-
175
- const opts = createMockOpts()
176
- await indexer.onEvent(
177
- createRecordEvent({ collection: 'com.example.unknown' }),
178
- opts,
179
- )
180
-
181
- expect(otherEvents).toHaveLength(1)
182
- expect(otherEvents[0].collection).toBe('com.example.unknown')
183
- })
184
-
185
- it('routes to other handler for unregistered actions', async () => {
186
- const indexer = new LexIndexer()
187
- const otherEvents: RecordEvent[] = []
188
-
189
- indexer.create(post, async () => {})
190
- indexer.other(async (evt) => {
191
- otherEvents.push(evt)
192
- })
193
-
194
- const opts = createMockOpts()
195
- await indexer.onEvent(createRecordEvent({ action: 'delete' }), opts)
196
-
197
- expect(otherEvents).toHaveLength(1)
198
- expect(otherEvents[0].action).toBe('delete')
199
- })
200
-
201
- it('routes identity events to identity handler', async () => {
202
- const indexer = new LexIndexer()
203
- const received: IdentityEvent[] = []
204
-
205
- indexer.identity(async (evt) => {
206
- received.push(evt)
207
- })
208
-
209
- const opts = createMockOpts()
210
- await indexer.onEvent(createIdentityEvent(), opts)
211
-
212
- expect(received).toHaveLength(1)
213
- expect(received[0].handle).toBe('alice.test')
214
- })
215
- })
216
-
217
- describe('duplicate registration', () => {
218
- it('throws on duplicate create handler', () => {
219
- const indexer = new LexIndexer()
220
- indexer.create(post, async () => {})
221
-
222
- expect(() => indexer.create(post, async () => {})).toThrow(
223
- 'Handler already registered',
224
- )
225
- })
226
-
227
- it('throws on duplicate update handler', () => {
228
- const indexer = new LexIndexer()
229
- indexer.update(post, async () => {})
230
-
231
- expect(() => indexer.update(post, async () => {})).toThrow(
232
- 'Handler already registered',
233
- )
234
- })
235
-
236
- it('throws on duplicate delete handler', () => {
237
- const indexer = new LexIndexer()
238
- indexer.delete(post, async () => {})
239
-
240
- expect(() => indexer.delete(post, async () => {})).toThrow(
241
- 'Handler already registered',
242
- )
243
- })
244
-
245
- it('throws when put conflicts with create', () => {
246
- const indexer = new LexIndexer()
247
- indexer.create(post, async () => {})
248
-
249
- expect(() => indexer.put(post, async () => {})).toThrow(
250
- 'Handler already registered',
251
- )
252
- })
253
-
254
- it('throws when create conflicts with put', () => {
255
- const indexer = new LexIndexer()
256
- indexer.put(post, async () => {})
257
-
258
- expect(() => indexer.create(post, async () => {})).toThrow(
259
- 'Handler already registered',
260
- )
261
- })
262
- })
263
-
264
- describe('schema validation', () => {
265
- it('validates record on create', async () => {
266
- const indexer = new LexIndexer()
267
- indexer.create(post, async () => {})
268
-
269
- const opts = createMockOpts()
270
- await expect(
271
- indexer.onEvent(createRecordEvent({ record: { text: 123 } }), opts),
272
- ).rejects.toThrow('Record validation failed')
273
- })
274
-
275
- it('validates record on update', async () => {
276
- const indexer = new LexIndexer()
277
- indexer.update(post, async () => {})
278
-
279
- const opts = createMockOpts()
280
- await expect(
281
- indexer.onEvent(
282
- createRecordEvent({ action: 'update', record: { invalid: true } }),
283
- opts,
284
- ),
285
- ).rejects.toThrow('Record validation failed')
286
- })
287
- })
288
-
289
- describe('ack behavior', () => {
290
- it('calls ack after handler completes', async () => {
291
- const indexer = new LexIndexer()
292
- indexer.create(post, async () => {})
293
-
294
- const opts = createMockOpts()
295
- await indexer.onEvent(createRecordEvent(), opts)
296
-
297
- expect(opts.acked).toBe(true)
298
- })
299
-
300
- it('calls ack when routed to other handler', async () => {
301
- const indexer = new LexIndexer()
302
- indexer.other(async () => {})
303
-
304
- const opts = createMockOpts()
305
- await indexer.onEvent(createRecordEvent(), opts)
306
-
307
- expect(opts.acked).toBe(true)
308
- })
309
-
310
- it('calls ack even when no handler matches', async () => {
311
- const indexer = new LexIndexer()
312
-
313
- const opts = createMockOpts()
314
- await indexer.onEvent(createRecordEvent(), opts)
315
-
316
- expect(opts.acked).toBe(true)
317
- })
318
- })
319
-
320
- describe('error handling', () => {
321
- it('calls error handler when provided', () => {
322
- const indexer = new LexIndexer()
323
- const errors: Error[] = []
324
-
325
- indexer.error((err) => {
326
- errors.push(err)
327
- })
328
-
329
- const testError = new Error('test error')
330
- indexer.onError(testError)
331
-
332
- expect(errors).toHaveLength(1)
333
- expect(errors[0]).toBe(testError)
334
- })
335
-
336
- it('throws when no error handler is registered', () => {
337
- const indexer = new LexIndexer()
338
- const testError = new Error('test error')
339
-
340
- expect(() => indexer.onError(testError)).toThrow('test error')
341
- })
342
- })
343
- })
@@ -1,161 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { HandlerOpts } from '../src/channel.js'
3
- import { SimpleIndexer } from '../src/simple-indexer.js'
4
- import { IdentityEvent, RecordEvent } from '../src/types.js'
5
- import {
6
- createIdentityEvent,
7
- createMockOpts,
8
- createRecordEvent,
9
- } from './_util.js'
10
-
11
- describe('SimpleIndexer', () => {
12
- describe('event routing', () => {
13
- it('routes record events to record handler', async () => {
14
- const indexer = new SimpleIndexer()
15
- const receivedEvents: RecordEvent[] = []
16
-
17
- indexer.record(async (evt) => {
18
- receivedEvents.push(evt)
19
- })
20
-
21
- const opts = createMockOpts()
22
- await indexer.onEvent(createRecordEvent(), opts)
23
-
24
- expect(receivedEvents).toHaveLength(1)
25
- expect(receivedEvents[0].type).toBe('record')
26
- expect(receivedEvents[0].collection).toBe('com.example.post')
27
- })
28
-
29
- it('routes identity events to identity handler', async () => {
30
- const indexer = new SimpleIndexer()
31
- const receivedEvents: IdentityEvent[] = []
32
-
33
- indexer.identity(async (evt) => {
34
- receivedEvents.push(evt)
35
- })
36
-
37
- const opts = createMockOpts()
38
- await indexer.onEvent(createIdentityEvent(), opts)
39
-
40
- expect(receivedEvents).toHaveLength(1)
41
- expect(receivedEvents[0].type).toBe('identity')
42
- expect(receivedEvents[0].handle).toBe('alice.test')
43
- })
44
-
45
- it('does not call identity handler for record events', async () => {
46
- const indexer = new SimpleIndexer()
47
- let identityCalled = false
48
- let recordCalled = false
49
-
50
- indexer.identity(async () => {
51
- identityCalled = true
52
- })
53
- indexer.record(async () => {
54
- recordCalled = true
55
- })
56
-
57
- const opts = createMockOpts()
58
- await indexer.onEvent(createRecordEvent(), opts)
59
-
60
- expect(recordCalled).toBe(true)
61
- expect(identityCalled).toBe(false)
62
- })
63
-
64
- it('does not call record handler for identity events', async () => {
65
- const indexer = new SimpleIndexer()
66
- let identityCalled = false
67
- let recordCalled = false
68
-
69
- indexer.identity(async () => {
70
- identityCalled = true
71
- })
72
- indexer.record(async () => {
73
- recordCalled = true
74
- })
75
-
76
- const opts = createMockOpts()
77
- await indexer.onEvent(createIdentityEvent(), opts)
78
-
79
- expect(identityCalled).toBe(true)
80
- expect(recordCalled).toBe(false)
81
- })
82
- })
83
-
84
- describe('ack behavior', () => {
85
- it('calls ack after handler completes', async () => {
86
- const indexer = new SimpleIndexer()
87
- indexer.record(async () => {})
88
-
89
- const opts = createMockOpts()
90
- await indexer.onEvent(createRecordEvent(), opts)
91
-
92
- expect(opts.acked).toBe(true)
93
- })
94
-
95
- it('calls ack even when no handler is registered', async () => {
96
- const indexer = new SimpleIndexer()
97
- // No handlers registered
98
-
99
- const opts = createMockOpts()
100
- await indexer.onEvent(createRecordEvent(), opts)
101
-
102
- expect(opts.acked).toBe(true)
103
- })
104
- })
105
-
106
- describe('error handling', () => {
107
- it('calls error handler when provided', () => {
108
- const indexer = new SimpleIndexer()
109
- const errors: Error[] = []
110
-
111
- indexer.error((err) => {
112
- errors.push(err)
113
- })
114
-
115
- const testError = new Error('test error')
116
- indexer.onError(testError)
117
-
118
- expect(errors).toHaveLength(1)
119
- expect(errors[0]).toBe(testError)
120
- })
121
-
122
- it('throws when no error handler is registered', () => {
123
- const indexer = new SimpleIndexer()
124
- const testError = new Error('test error')
125
-
126
- expect(() => indexer.onError(testError)).toThrow('test error')
127
- })
128
- })
129
-
130
- describe('handler opts passthrough', () => {
131
- it('passes opts to record handler', async () => {
132
- const indexer = new SimpleIndexer()
133
- let receivedOpts: HandlerOpts | undefined
134
-
135
- indexer.record(async (_evt, opts) => {
136
- receivedOpts = opts
137
- })
138
-
139
- const opts = createMockOpts()
140
- await indexer.onEvent(createRecordEvent(), opts)
141
-
142
- expect(receivedOpts).toBeDefined()
143
- expect(receivedOpts?.signal).toBe(opts.signal)
144
- })
145
-
146
- it('passes opts to identity handler', async () => {
147
- const indexer = new SimpleIndexer()
148
- let receivedOpts: HandlerOpts | undefined
149
-
150
- indexer.identity(async (_evt, opts) => {
151
- receivedOpts = opts
152
- })
153
-
154
- const opts = createMockOpts()
155
- await indexer.onEvent(createIdentityEvent(), opts)
156
-
157
- expect(receivedOpts).toBeDefined()
158
- expect(receivedOpts?.signal).toBe(opts.signal)
159
- })
160
- })
161
- })
@@ -1,89 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import {
3
- assureAdminAuth,
4
- formatAdminAuthHeader,
5
- parseAdminAuthHeader,
6
- } from '../src/index.js'
7
-
8
- describe('util', () => {
9
- describe('formatAdminAuthHeader', () => {
10
- it('formats password as Basic auth header', () => {
11
- const header = formatAdminAuthHeader('secret')
12
- expect(header).toBe('Basic YWRtaW46c2VjcmV0')
13
- })
14
-
15
- it('uses admin as username', () => {
16
- const header = formatAdminAuthHeader('secret')
17
- const decoded = Buffer.from(
18
- header.replace('Basic ', ''),
19
- 'base64',
20
- ).toString()
21
- expect(decoded).toBe('admin:secret')
22
- })
23
- })
24
-
25
- describe('parseAdminAuthHeader', () => {
26
- it('parses Basic auth header and returns password', () => {
27
- const header = 'Basic YWRtaW46c2VjcmV0' // admin:secret
28
- const password = parseAdminAuthHeader(header)
29
- expect(password).toBe('secret')
30
- })
31
-
32
- it('handles header without Basic prefix', () => {
33
- const header = 'YWRtaW46c2VjcmV0' // admin:secret (no prefix)
34
- const password = parseAdminAuthHeader(header)
35
- expect(password).toBe('secret')
36
- })
37
-
38
- it('throws if username is not admin', () => {
39
- const header = 'Basic ' + Buffer.from('user:secret').toString('base64')
40
- expect(() => parseAdminAuthHeader(header)).toThrow(
41
- "Unexpected username in admin headers. Expected 'admin'",
42
- )
43
- })
44
- })
45
-
46
- describe('assureAdminAuth', () => {
47
- it('does not throw when password matches', () => {
48
- const header = formatAdminAuthHeader('secret')
49
- expect(() => assureAdminAuth('secret', header)).not.toThrow()
50
- })
51
-
52
- it('throws when password does not match', () => {
53
- const header = formatAdminAuthHeader('wrong')
54
- expect(() => assureAdminAuth('secret', header)).toThrow(
55
- 'Invalid admin password',
56
- )
57
- })
58
-
59
- it('throws when header has invalid username', () => {
60
- const header =
61
- 'Basic ' + Buffer.from('notadmin:secret').toString('base64')
62
- expect(() => assureAdminAuth('secret', header)).toThrow()
63
- })
64
-
65
- it('is timing-safe (does not leak password length)', () => {
66
- // This is a basic sanity check - true timing attack tests require statistical analysis
67
- const header = formatAdminAuthHeader('a')
68
- const start1 = performance.now()
69
- try {
70
- assureAdminAuth('b', header)
71
- } catch {
72
- // do nothing
73
- }
74
- const time1 = performance.now() - start1
75
-
76
- const longHeader = formatAdminAuthHeader('a'.repeat(1000))
77
- const start2 = performance.now()
78
- try {
79
- assureAdminAuth('b'.repeat(1000), longHeader)
80
- } catch {
81
- // do nothing
82
- }
83
- const time2 = performance.now() - start2
84
-
85
- // Times should be in the same order of magnitude (not a rigorous test)
86
- expect(Math.abs(time1 - time2)).toBeLessThan(50)
87
- })
88
- })
89
- })
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig/node.json",
3
- "compilerOptions": {
4
- "noImplicitAny": true,
5
- "rootDir": "./src",
6
- "outDir": "./dist",
7
- },
8
- "include": ["./src"],
9
- }
@@ -1 +0,0 @@
1
- {"version":"7.0.0-dev.20260614.1","root":["./src/channel.ts","./src/client.ts","./src/index.ts","./src/lex-indexer.ts","./src/simple-indexer.ts","./src/types.ts","./src/util.ts"]}
package/tsconfig.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "include": [],
3
- "references": [
4
- { "path": "./tsconfig.build.json" },
5
- { "path": "./tsconfig.tests.json" },
6
- ],
7
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig/vitest.json",
3
- "include": ["./tests", "./src/**/*.test.ts"],
4
- "compilerOptions": {
5
- "noImplicitAny": true,
6
- "rootDir": "./",
7
- },
8
- }
package/vitest.config.ts DELETED
@@ -1,5 +0,0 @@
1
- import { defineProject } from 'vitest/config'
2
-
3
- export default defineProject({
4
- test: {},
5
- })