-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipfs.js
226 lines (188 loc) · 5.56 KB
/
ipfs.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
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
/* global Headers */
// IPFS Cluster IPNS API
export class IPFSPinningServiceAPI {
constructor (service, authToken = '') {
this.service = service
this.authToken = authToken
}
#prepReqestData () {
const url = new URL('./pins/', this.service)
const headers = new Headers()
if (url.username && url.password) {
const encoded = btoa(`${url.username}:${url.password}`)
const auth = `Basic ${encoded}`
headers.append('Authorization', auth)
url.username = ''
url.password = ''
} else if (url.password) {
// If we just have a password, it's for a bearer token
const token = url.password
const auth = `Bearer ${token}`
headers.append('Authorization', auth)
url.password = ''
}
url.searchParams.append('status', 'queued,pinning,pinned')
const requestURL = url.href
return { headers, requestURL }
}
async list () {
const { requestURL, headers } = this.#prepReqestData()
const response = await fetch(requestURL, {
headers
})
if (!response.ok) {
throw new Error(await response.text())
}
if (response.status === 204) return []
const { results } = await response.json()
if (!results) return []
return results.map(({ pin, requestid: id }) => {
const { cid, name } = pin
const url = `ipfs://${cid}`
return { cid, url, name, id }
})
}
async create (url, { name = url, origins, meta } = {}) {
const cid = urlToCID('ipfs', url)
const params = {
cid, name, origins, meta
}
console.log('Pinning', params)
const { requestURL, headers } = this.#prepReqestData()
headers.append('Content-Type', 'application/json')
const response = await fetch(requestURL, {
method: 'POST',
headers,
body: JSON.stringify(params)
})
if (!response.ok) {
throw new Error(await response.text())
}
const result = await response.json()
return result
}
async get (url) {
const getCID = urlToCID('ipfs', url)
const list = await this.list(url)
return list.find(({ cid }) => cid === getCID)
}
async delete (url) {
const { id } = await this.get(url)
const { requestURL, headers } = this.#prepReqestData()
const finalURL = new URL(`./${id}`, requestURL).href
const response = await fetch(finalURL, {
method: 'DELETE',
headers
})
if (!response.ok) {
throw new Error(await response.text())
}
return response.text()
}
}
// IPFS Cluster IPNS API
export class IPFSClusterAPI {
constructor (service, authToken = '') {
this.service = service
this.authToken = authToken
}
#prepReqestData (forURL = '') {
let url = new URL('./pins/', this.service)
const headers = new Headers()
if (url.username && url.password) {
const encoded = btoa(`${url.username}:${url.password}`)
const auth = `Basic ${encoded}`
headers.append('Authorization', auth)
url.username = ''
url.password = ''
}
if (forURL) {
const path = urlToPath(forURL)
url = new URL('.' + path, url.href)
}
const requestURL = url.href
return { headers, requestURL }
}
async list () {
const { requestURL, headers } = this.#prepReqestData()
const response = await fetch(requestURL, {
headers
})
if (!response.ok) {
throw new Error(await response.text())
}
// If no pins exist yet, return an empty list
if (response.status === 204) return []
// The reponse will be a JSON-ND format (which isn't documented?)
// i.e. there are JSON blobs separated by newlines
const raw = await response.text()
const results = raw
.split('\n')
.filter((chunk) => chunk.trim())
.map((chunk) => JSON.parse(chunk))
return results.map(({ cid, name, metadata }) => {
// TODO: IPNS pins?
const url = `ipfs://${cid}`
const id = cid
return { cid, url, name, id }
})
}
async create (url, { name = url, origins, meta } = {}) {
const { requestURL, headers } = this.#prepReqestData(url)
const postURL = new URL(requestURL)
if (name) postURL.searchParams.set('name', name)
if (origins) {
if (Array.isArrray(origins)) {
postURL.searchParams.set('origins', origins.join(','))
} else {
postURL.searchParams.set('origins', origins)
}
}
if (meta) {
for (const key of Object.keys(meta)) {
const value = meta[key]
postURL.searchParams.set(`meta-${key}`, value)
}
}
const finalURL = postURL.href
const response = await fetch(finalURL, {
method: 'POST',
headers
})
if (!response.ok) {
throw new Error(await response.text())
}
const result = await response.json()
return result
}
async get (url) {
const { requestURL, headers } = this.#prepReqestData(url)
const response = await fetch(requestURL, { headers })
if (!response.ok) {
throw new Error(await response.text())
}
return await response.json()
}
async delete (url) {
const { requestURL, headers } = this.#prepReqestData(url)
const response = await fetch(requestURL, {
headers,
method: 'DELETE'
})
if (!response.ok) {
throw new Error(await response.text())
}
return await response.json()
}
}
function urlToPath (url) {
return '/' + url.replace('://', '/')
}
function urlToCID (protocol, url) {
const prefix = `${protocol}://`
if (!url.startsWith(prefix)) {
throw new TypeError(`Unexpected URL, must start with ${prefix}`)
}
const cid = url.slice(prefix.length).split('/')[0]
return cid
}