forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexicons.ts
247 lines (228 loc) · 6.2 KB
/
lexicons.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import {
LexiconDoc,
LexRecord,
LexUserType,
LexiconDefNotFoundError,
InvalidLexiconError,
ValidationResult,
ValidationError,
isObj,
hasProp,
} from './types'
import {
assertValidRecord,
assertValidXrpcParams,
assertValidXrpcInput,
assertValidXrpcOutput,
assertValidXrpcMessage,
} from './validation'
import { toLexUri } from './util'
import * as ComplexValidators from './validators/complex'
/**
* A collection of compiled lexicons.
*/
export class Lexicons implements Iterable<LexiconDoc> {
docs: Map<string, LexiconDoc> = new Map()
defs: Map<string, LexUserType> = new Map()
constructor(docs?: Iterable<LexiconDoc>) {
if (docs) {
for (const doc of docs) {
this.add(doc)
}
}
}
/**
* @example clone a lexicon:
* ```ts
* const clone = new Lexicons(originalLexicon)
* ```
*
* @example get docs array:
* ```ts
* const docs = Array.from(lexicons)
* ```
*/
[Symbol.iterator](): Iterator<LexiconDoc> {
return this.docs.values()
}
/**
* Add a lexicon doc.
*/
add(doc: LexiconDoc): void {
const uri = toLexUri(doc.id)
if (this.docs.has(uri)) {
throw new Error(`${uri} has already been registered`)
}
// WARNING
// mutates the object
// -prf
resolveRefUris(doc, uri)
this.docs.set(uri, doc)
for (const [defUri, def] of iterDefs(doc)) {
this.defs.set(defUri, def)
}
}
/**
* Remove a lexicon doc.
*/
remove(uri: string) {
uri = toLexUri(uri)
const doc = this.docs.get(uri)
if (!doc) {
throw new Error(`Unable to remove "${uri}": does not exist`)
}
for (const [defUri, _def] of iterDefs(doc)) {
this.defs.delete(defUri)
}
this.docs.delete(uri)
}
/**
* Get a lexicon doc.
*/
get(uri: string): LexiconDoc | undefined {
uri = toLexUri(uri)
return this.docs.get(uri)
}
/**
* Get a definition.
*/
getDef(uri: string): LexUserType | undefined {
uri = toLexUri(uri)
return this.defs.get(uri)
}
/**
* Get a def, throw if not found. Throws on not found.
*/
getDefOrThrow<T extends LexUserType['type'] = LexUserType['type']>(
uri: string,
types?: readonly T[],
): Extract<LexUserType, { type: T }>
getDefOrThrow(
uri: string,
types?: readonly LexUserType['type'][],
): LexUserType {
const def = this.getDef(uri)
if (!def) {
throw new LexiconDefNotFoundError(`Lexicon not found: ${uri}`)
}
if (types && !types.includes(def.type)) {
throw new InvalidLexiconError(
`Not a ${types.join(' or ')} lexicon: ${uri}`,
)
}
return def
}
/**
* Validate a record or object.
*/
validate(lexUri: string, value: unknown): ValidationResult {
lexUri = toLexUri(lexUri)
const def = this.getDefOrThrow(lexUri, ['record', 'object'])
if (!isObj(value)) {
throw new ValidationError(`Value must be an object`)
}
if (def.type === 'record') {
return ComplexValidators.object(this, 'Record', def.record, value)
} else if (def.type === 'object') {
return ComplexValidators.object(this, 'Object', def, value)
} else {
// shouldn't happen
throw new InvalidLexiconError('Definition must be a record or object')
}
}
/**
* Validate a record and throw on any error.
*/
assertValidRecord(lexUri: string, value: unknown) {
lexUri = toLexUri(lexUri)
const def = this.getDefOrThrow(lexUri, ['record'])
if (!isObj(value)) {
throw new ValidationError(`Record must be an object`)
}
if (!hasProp(value, '$type') || typeof value.$type !== 'string') {
throw new ValidationError(`Record/$type must be a string`)
}
const $type = (value as Record<string, string>).$type || ''
if (toLexUri($type) !== lexUri) {
throw new ValidationError(
`Invalid $type: must be ${lexUri}, got ${$type}`,
)
}
return assertValidRecord(this, def as LexRecord, value)
}
/**
* Validate xrpc query params and throw on any error.
*/
assertValidXrpcParams(lexUri: string, value: unknown) {
lexUri = toLexUri(lexUri)
const def = this.getDefOrThrow(lexUri, [
'query',
'procedure',
'subscription',
])
return assertValidXrpcParams(this, def, value)
}
/**
* Validate xrpc input body and throw on any error.
*/
assertValidXrpcInput(lexUri: string, value: unknown) {
lexUri = toLexUri(lexUri)
const def = this.getDefOrThrow(lexUri, ['procedure'])
return assertValidXrpcInput(this, def, value)
}
/**
* Validate xrpc output body and throw on any error.
*/
assertValidXrpcOutput(lexUri: string, value: unknown) {
lexUri = toLexUri(lexUri)
const def = this.getDefOrThrow(lexUri, ['query', 'procedure'])
return assertValidXrpcOutput(this, def, value)
}
/**
* Validate xrpc subscription message and throw on any error.
*/
assertValidXrpcMessage<T = unknown>(lexUri: string, value: unknown): T {
lexUri = toLexUri(lexUri)
const def = this.getDefOrThrow(lexUri, ['subscription'])
return assertValidXrpcMessage(this, def, value) as T
}
/**
* Resolve a lex uri given a ref
*/
resolveLexUri(lexUri: string, ref: string) {
lexUri = toLexUri(lexUri)
return toLexUri(ref, lexUri)
}
}
function* iterDefs(doc: LexiconDoc): Generator<[string, LexUserType]> {
for (const defId in doc.defs) {
yield [`lex:${doc.id}#${defId}`, doc.defs[defId]]
if (defId === 'main') {
yield [`lex:${doc.id}`, doc.defs[defId]]
}
}
}
// WARNING
// this method mutates objects
// -prf
function resolveRefUris(obj: any, baseUri: string): any {
for (const k in obj) {
if (obj.type === 'ref') {
obj.ref = toLexUri(obj.ref, baseUri)
} else if (obj.type === 'union') {
obj.refs = obj.refs.map((ref) => toLexUri(ref, baseUri))
} else if (Array.isArray(obj[k])) {
obj[k] = obj[k].map((item: any) => {
if (typeof item === 'string') {
return item.startsWith('#') ? toLexUri(item, baseUri) : item
} else if (item && typeof item === 'object') {
return resolveRefUris(item, baseUri)
}
return item
})
} else if (obj[k] && typeof obj[k] === 'object') {
obj[k] = resolveRefUris(obj[k], baseUri)
}
}
return obj
}