@atproto/common-web 0.2.1 → 0.2.2
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/CHANGELOG.md +6 -0
- package/dist/async.d.ts +1 -0
- package/dist/did-doc.d.ts +91 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
- package/src/async.ts +22 -0
- package/src/did-doc.ts +133 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
package/src/async.ts
CHANGED
|
@@ -124,3 +124,25 @@ export class AsyncBufferFullError extends Error {
|
|
|
124
124
|
super(`ReachedMaxBufferSize: ${maxSize}`)
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
export const handleAllSettledErrors = (
|
|
129
|
+
results: PromiseSettledResult<unknown>[],
|
|
130
|
+
) => {
|
|
131
|
+
const errors = results.filter(isRejected).map((res) => res.reason)
|
|
132
|
+
if (errors.length === 0) {
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
if (errors.length === 1) {
|
|
136
|
+
throw errors[0]
|
|
137
|
+
}
|
|
138
|
+
throw new AggregateError(
|
|
139
|
+
errors,
|
|
140
|
+
'Multiple errors: ' + errors.map((err) => err?.message).join('\n'),
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const isRejected = (
|
|
145
|
+
result: PromiseSettledResult<unknown>,
|
|
146
|
+
): result is PromiseRejectedResult => {
|
|
147
|
+
return result.status === 'rejected'
|
|
148
|
+
}
|
package/src/did-doc.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
// Parsing atproto data
|
|
4
|
+
// --------
|
|
5
|
+
|
|
6
|
+
export const isValidDidDoc = (doc: unknown): doc is DidDocument => {
|
|
7
|
+
return didDocument.safeParse(doc).success
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const getDid = (doc: DidDocument): string => {
|
|
11
|
+
const id = doc.id
|
|
12
|
+
if (typeof id !== 'string') {
|
|
13
|
+
throw new Error('No `id` on document')
|
|
14
|
+
}
|
|
15
|
+
return id
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const getHandle = (doc: DidDocument): string | undefined => {
|
|
19
|
+
const aka = doc.alsoKnownAs
|
|
20
|
+
if (!aka) return undefined
|
|
21
|
+
const found = aka.find((name) => name.startsWith('at://'))
|
|
22
|
+
if (!found) return undefined
|
|
23
|
+
// strip off at:// prefix
|
|
24
|
+
return found.slice(5)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// @NOTE we parse to type/publicKeyMultibase to avoid the dependency on @atproto/crypto
|
|
28
|
+
export const getSigningKey = (
|
|
29
|
+
doc: DidDocument,
|
|
30
|
+
): { type: string; publicKeyMultibase: string } | undefined => {
|
|
31
|
+
const did = getDid(doc)
|
|
32
|
+
let keys = doc.verificationMethod
|
|
33
|
+
if (!keys) return undefined
|
|
34
|
+
if (typeof keys !== 'object') return undefined
|
|
35
|
+
if (!Array.isArray(keys)) {
|
|
36
|
+
keys = [keys]
|
|
37
|
+
}
|
|
38
|
+
const found = keys.find(
|
|
39
|
+
(key) => key.id === '#atproto' || key.id === `${did}#atproto`,
|
|
40
|
+
)
|
|
41
|
+
if (!found?.publicKeyMultibase) return undefined
|
|
42
|
+
return {
|
|
43
|
+
type: found.type,
|
|
44
|
+
publicKeyMultibase: found.publicKeyMultibase,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const getPdsEndpoint = (doc: DidDocument): string | undefined => {
|
|
49
|
+
return getServiceEndpoint(doc, {
|
|
50
|
+
id: '#atproto_pds',
|
|
51
|
+
type: 'AtprotoPersonalDataServer',
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const getFeedGenEndpoint = (doc: DidDocument): string | undefined => {
|
|
56
|
+
return getServiceEndpoint(doc, {
|
|
57
|
+
id: '#bsky_fg',
|
|
58
|
+
type: 'BskyFeedGenerator',
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const getNotifEndpoint = (doc: DidDocument): string | undefined => {
|
|
63
|
+
return getServiceEndpoint(doc, {
|
|
64
|
+
id: '#bsky_notif',
|
|
65
|
+
type: 'BskyNotificationService',
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const getServiceEndpoint = (
|
|
70
|
+
doc: DidDocument,
|
|
71
|
+
opts: { id: string; type: string },
|
|
72
|
+
) => {
|
|
73
|
+
const did = getDid(doc)
|
|
74
|
+
let services = doc.service
|
|
75
|
+
if (!services) return undefined
|
|
76
|
+
if (typeof services !== 'object') return undefined
|
|
77
|
+
if (!Array.isArray(services)) {
|
|
78
|
+
services = [services]
|
|
79
|
+
}
|
|
80
|
+
const found = services.find(
|
|
81
|
+
(service) => service.id === opts.id || service.id === `${did}${opts.id}`,
|
|
82
|
+
)
|
|
83
|
+
if (!found) return undefined
|
|
84
|
+
if (found.type !== opts.type) {
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
if (typeof found.serviceEndpoint !== 'string') {
|
|
88
|
+
return undefined
|
|
89
|
+
}
|
|
90
|
+
return validateUrl(found.serviceEndpoint)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Check protocol and hostname to prevent potential SSRF
|
|
94
|
+
const validateUrl = (urlStr: string): string | undefined => {
|
|
95
|
+
let url
|
|
96
|
+
try {
|
|
97
|
+
url = new URL(urlStr)
|
|
98
|
+
} catch {
|
|
99
|
+
return undefined
|
|
100
|
+
}
|
|
101
|
+
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
102
|
+
return undefined
|
|
103
|
+
} else if (!url.hostname) {
|
|
104
|
+
return undefined
|
|
105
|
+
} else {
|
|
106
|
+
return urlStr
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Types
|
|
111
|
+
// --------
|
|
112
|
+
|
|
113
|
+
const verificationMethod = z.object({
|
|
114
|
+
id: z.string(),
|
|
115
|
+
type: z.string(),
|
|
116
|
+
controller: z.string(),
|
|
117
|
+
publicKeyMultibase: z.string().optional(),
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
const service = z.object({
|
|
121
|
+
id: z.string(),
|
|
122
|
+
type: z.string(),
|
|
123
|
+
serviceEndpoint: z.union([z.string(), z.record(z.unknown())]),
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
export const didDocument = z.object({
|
|
127
|
+
id: z.string(),
|
|
128
|
+
alsoKnownAs: z.array(z.string()).optional(),
|
|
129
|
+
verificationMethod: z.array(verificationMethod).optional(),
|
|
130
|
+
service: z.array(service).optional(),
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
export type DidDocument = z.infer<typeof didDocument>
|
package/src/index.ts
CHANGED