This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
187 lines (171 loc) · 3.58 KB
/
index.js
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
const b4a = require('b4a')
const { default: Ajv } = require('ajv')
const PROFILE_PATH = '/profile.json'
const PROFILE_SCHEMA = {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Profile',
type: 'object',
properties: {
name: {
type: 'string'
},
bio: {
type: 'string'
},
image: {
type: 'string'
},
links: {
type: 'array',
items: {
$ref: '#/definitions/Link'
}
}
},
definitions: {
Link: {
type: 'object',
properties: {
url: {
type: 'string'
},
title: {
type: 'string'
}
},
required: ['title', 'url']
}
}
}
const ajv = new Ajv({ allErrors: true })
const _validate = ajv.compile(PROFILE_SCHEMA)
class SlashtagsProfile {
/**
* @param {WebRelayClient} client
*/
constructor (client) {
this._client = client
}
/**
* Create a url of the profile file
*/
async createURL () {
return this._client.createURL(PROFILE_PATH)
}
/**
* Absolute for the profile path
*
* @returns {string}
*/
static get path () {
return PROFILE_PATH
}
/**
* Create or update a Profile file.
*
* @param {Profile} profile
* @param {Parameters<import('@synonymdev/web-relay').Client['put']>[2]} [options]
*
* @returns {Promise<void>}
*/
put (profile, options) {
validate(profile)
return this._client.put(PROFILE_PATH, encode(profile), options)
}
/**
* Delete Profile file.
* @param {Parameters<import('@synonymdev/web-relay').Client['del']>[1]} [options]
*
* @returns {Promise<void>}
*/
del (options) {
return this._client.del(PROFILE_PATH, options)
}
/**
* Return local Profile file
*
* @param {string} [url]
* @param {Parameters<import('@synonymdev/web-relay').Client['get']>[1]} [options]
*
* @returns {Promise<Profile | null>}
*/
async get (url, options) {
const buf = url
? await this._client.get(url, options)
: await this._client.get(PROFILE_PATH, options)
return buf && decode(buf)
}
/**
* Subscribe to updates to a local or remote profile file.
*
* @param {string} url
* @param {(curr: Profile) => any} onupdate
*
* @returns {() => void}
*/
subscribe (url, onupdate) {
return this._client.subscribe(url, (buf) => {
onupdate(buf && decode(buf))
})
}
/**
* Close core data instance
*
* @returns {Promise<void>}
*/
close () {
return this._client.close()
}
}
module.exports = SlashtagsProfile
/**
* Encode profile json into Uint8Array.
*
* @param {Profile} profile
*
* @returns {Uint8Array}
*/
function encode (profile) {
return b4a.from(JSON.stringify(profile))
}
/**
* Try to decode Uint8Array into profile json.
*
* @param{Uint8Array} buf
*
* @returns {Profile | null}
*/
function decode (buf) {
try {
return JSON.parse(b4a.toString(buf))
} catch {
return null
}
}
/**
* Validate profile json.
*
* @param {Profile} profile
*/
function validate (profile) {
const valid = _validate(profile)
if (!valid) {
const message = _validate.errors.map((error) => {
const name = error.instancePath === '' ? 'profile' : `Field '${error.instancePath.slice(1)}'`
return ` - ${name} ${error.message}`
})
.join('\n')
throw new Error('Invalid profile:\n' + message)
}
}
/**
* @typedef {{url: string, title: string}} Link
* @typedef {{
* name?: string;
* bio?: string;
* image?: string;
* links?: Array<Link>;
* }} Profile
*
* @typedef {import('@synonymdev/web-relay/types/lib/client/index')} WebRelayClient
*/