-
Notifications
You must be signed in to change notification settings - Fork 119
/
interface.ts
286 lines (265 loc) · 7.17 KB
/
interface.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import type { UnixFSEntry } from 'ipfs-car/unpack'
import type { CID } from 'multiformats'
export type { CID, UnixFSEntry }
import type { CarReader } from '@ipld/car/api'
import type { BlockDecoder } from 'multiformats/codecs/interface'
import {
fetch as _fetch,
} from '../platform'
/**
* Define nominal type of U based on type of T. Similar to Opaque types in Flow
*/
export type Tagged<T, Tag> = T & { tag?: Tag }
export interface Service {
endpoint?: URL
token: string
rateLimiter?: RateLimiter
fetch?: typeof _fetch
}
/**
* CID in string representation.
*/
export type CIDString = Tagged<string, CID>
export interface API {
/**
* Stores files and returns a corresponding CID.
*/
put(
service: Service,
files: Iterable<Filelike>,
options?: PutOptions
): Promise<CIDString>
/**
* Uploads a CAR ([Content Addressed Archive](https://github.com/ipld/specs/blob/master/block-layer/content-addressable-archives.md)) file to web3.storage.
*/
putCar(
service: Service,
car: CarReader,
options?: PutCarOptions
): Promise<CIDString>
/**
* Get files for a root CID packed as a CAR file
*/
get(service: Service, cid: CIDString): Promise<Web3Response | null>
/**
* Remove a users record of an upload. Does not make CID unavailable.
*/
delete(service: Service, cid: CIDString, options?: RequestOptions): Promise<CIDString>
/**
* Get info on Filecoin deals and IPFS pins that a CID is replicated in.
*/
status(service: Service, cid: CIDString, options?: RequestOptions): Promise<Status | undefined>
/**
* Find all uploads for this account. Use a `for await...of` loop to fetch them all.
* @example
* Fetch all the uploads
* ```js
* const uploads = []
* for await (const item of client.list()) {
* uploads.push(item)
* }
* ```
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of}
*/
list(service: Service, options?: ListOptions): AsyncIterable<Upload>
}
export interface Filelike {
/**
* Name of the file. May include path information.
*/
name: string
/**
* Returns a ReadableStream which upon reading returns the data contained
* within the File.
*/
stream: () => ReadableStream
}
export type RequestOptions = {
/**
* A signal that can be used to abort the request.
*/
signal?: AbortSignal
}
export type PutOptions = RequestOptions & {
/**
* Callback called after the data has been assembled into a DAG, but before
* any upload requests begin. It is passed the CID of the root node of the
* graph.
*/
onRootCidReady?: (cid: CIDString) => void
/**
* Callback called after each chunk of data has been uploaded. By default,
* data is split into chunks of around 10MB. It is passed the actual chunk
* size in bytes.
*/
onStoredChunk?: (size: number) => void
/**
* Maximum times to retry a failed upload. Default: 5
*/
maxRetries?: number
/**
* Maximum chunk size to upload in bytes. Default: 10,485,760
*/
maxChunkSize?: number
/**
* Should input files be wrapped with a directory? Default: true
*
* It is enabled by default as it preserves the input filenames in DAG;
* the filenames become directory entries in the generated wrapping dir.
*
* The trade off is your root CID will be that of the wrapping dir,
* rather than the input file itself.
*
* For a single file e.g. `cat.png` it's IPFS path would be
* `<wrapping dir cid>/cat.png` rather than just `<cid for cat.png>`
*
* Wrapping with a directory is required when passing multiple files
* that do not share the same root.
*/
wrapWithDirectory?: boolean
/**
* Human readable name for this upload, for use in file listings.
*/
name?: string
}
export type PutCarOptions = RequestOptions & {
/**
* Human readable name for this upload, for use in file listings.
*/
name?: string
/**
* Callback called after each chunk of data has been uploaded. By default,
* data is split into chunks of around 10MB. It is passed the actual chunk
* size in bytes.
*/
onStoredChunk?: (size: number) => void
/**
* Maximum times to retry a failed upload. Default: 5
*/
maxRetries?: number
/**
* Maximum chunk size to upload in bytes. Default: 10,485,760
*/
maxChunkSize?: number
/**
* Additional IPLD block decoders. Used to interpret the data in the CAR file
* and split it into multiple chunks. Note these are only required if the CAR
* file was not encoded using the default encoders: `dag-pb`, `dag-cbor` and
* `raw`.
*/
decoders?: BlockDecoder<any, any>[]
}
export type ListOptions = RequestOptions & {
/**
* Return items uploaded before this ISO 8601 date string.
* Default: `new Date().toISOString()`.
*/
before?: string
/**
* Maximum number of results to return.
* Default: `Infinity`.
*/
maxResults?: number
}
export interface Web3File extends File {
/**
* Content Identifier for the file data.
*/
cid: CIDString
}
export interface Web3Response extends Response {
unixFsIterator: () => AsyncIterable<UnixFSEntry>
files: () => Promise<Array<Web3File>>
}
export interface Pin {
/**
* Libp2p peer ID of the node pinning the data.
*/
peerId: string
/**
* Human readable name for the peer pinning the data.
*/
peerName: string
/**
* Approximate geographical region of the node pinning the data.
*/
region: string
/**
* Pinning status on this peer.
*/
status: 'Pinned' | 'Pinning' | 'PinQueued'
/**
* Updated date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
*/
updated: string
}
export interface Deal {
/**
* On-chain ID of the deal.
*/
dealId: number
/**
* Address of the provider storing this data.
*/
storageProvider: string
/**
* Current deal status.
*/
status: 'Queued' | 'Published' | 'Active'
/**
* Filecoin [Piece CID](https://spec.filecoin.io/systems/filecoin_files/piece/) of the data in the deal.
*/
pieceCid: string
/**
* CID of the data aggregated in this deal.
*/
dataCid: string
/**
* Selector for extracting stored data from the aggregated data root.
*/
dataModelSelector: string
/**
* Date when the deal will become active in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
*/
activation: string
/**
* Creation date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
*/
created: string
/**
* Updated date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
*/
updated: string
}
export interface Status {
/**
* Content Identifier for the data.
*/
cid: CIDString
/**
* Total size of the DAG in bytes.
*/
dagSize: number
/**
* Creation date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
*/
created: string
/**
* IPFS peers this data is being pinned on.
*/
pins: Array<Pin>
/**
* Filecoin deals this data appears in.
*/
deals: Array<Deal>
}
export interface Upload extends Status {
name: string
}
/**
* RateLimiter returns a promise that resolves when it is safe to send a request
* that does not exceed the rate limit.
*/
export interface RateLimiter {
(): Promise<void>
}