This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
176 lines (156 loc) · 3.83 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
'use strict'
const { CID } = require('multiformats/cid')
const Client = require('../../lib/core')
const Service = require('./service')
const toUrlSearchParams = require('../../lib/to-url-search-params')
/**
* @typedef {import('../../types').Options} Options
* @typedef {import('ipfs-core-types/src/utils').AbortOptions} AbortOptions
* @typedef {import('ipfs-core-types/src/pin/remote').Pin} Pin
* @typedef {import('ipfs-core-types/src/pin/remote').AddOptions} AddOptions
* @typedef {import('ipfs-core-types/src/pin/remote').Query} Query
* @typedef {import('ipfs-core-types/src/pin/remote').Status} Status
* @typedef {import('../../types').HTTPClientExtraOptions} HTTPClientExtraOptions
* @typedef {import('ipfs-core-types/src/pin/remote').API<HTTPClientExtraOptions>} RemotePiningAPI
*/
class Remote {
/**
* @param {Options} options
*/
constructor (options) {
this.client = new Client(options)
/** @readonly */
this.service = new Service(options)
}
}
/**
* @type {RemotePiningAPI["add"]}
*/
Remote.prototype.add = async function add (cid, { timeout, signal, headers, ...query }) {
const response = await this.client.post('pin/remote/add', {
timeout,
signal,
headers,
searchParams: encodeAddParams({ cid, ...query })
})
return decodePin(await response.json())
}
/**
* @type {RemotePiningAPI["ls"]}
*/
Remote.prototype.ls = async function * ls ({ timeout, signal, headers, ...query }) {
const response = await this.client.post('pin/remote/ls', {
timeout,
signal,
headers,
searchParams: encodeQuery(query)
})
for await (const pin of response.ndjson()) {
yield decodePin(pin)
}
}
/**
* @type {RemotePiningAPI["rm"]}
*/
Remote.prototype.rm = async function rm ({ timeout, signal, headers, ...query }) {
await this.client.post('pin/remote/rm', {
timeout,
signal,
headers,
searchParams: encodeQuery({
...query,
all: false
})
})
}
/**
* @type {RemotePiningAPI["rmAll"]}
*/
Remote.prototype.rmAll = async function ({ timeout, signal, headers, ...query }) {
await this.client.post('pin/remote/rm', {
timeout,
signal,
headers,
searchParams: encodeQuery({
...query,
all: true
})
})
}
/**
* @param {Object} json
* @param {string} json.Name
* @param {string} json.Cid
* @param {Status} json.Status
* @returns {Pin}
*/
const decodePin = ({ Name: name, Status: status, Cid: cid }) => {
return {
cid: CID.parse(cid),
name,
status
}
}
/**
* @param {any} service
* @returns {string}
*/
const encodeService = (service) => {
if (typeof service === 'string' && service !== '') {
return service
} else {
throw new TypeError('service name must be passed')
}
}
/**
* @param {any} cid
* @returns {string}
*/
const encodeCID = (cid) => {
if (CID.asCID(cid)) {
return cid.toString()
} else {
throw new TypeError(`CID instance expected instead of ${typeof cid}`)
}
}
/**
* @param {Query & { all?: boolean }} query
* @returns {URLSearchParams}
*/
const encodeQuery = ({ service, cid, name, status, all }) => {
const query = toUrlSearchParams({
service: encodeService(service),
name,
force: all ? true : undefined
})
if (cid) {
for (const value of cid) {
query.append('cid', encodeCID(value))
}
}
if (status) {
for (const value of status) {
query.append('status', value)
}
}
return query
}
/**
* @param {AddOptions & {cid:CID}} options
* @returns {URLSearchParams}
*/
const encodeAddParams = ({ cid, service, background, name, origins }) => {
const params = toUrlSearchParams({
arg: encodeCID(cid),
service: encodeService(service),
name,
background: background ? true : undefined
})
if (origins) {
for (const origin of origins) {
params.append('origin', origin.toString())
}
}
return params
}
module.exports = Remote