This repository has been 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
/
files.js
61 lines (53 loc) · 1.47 KB
/
files.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
/* eslint-env browser */
import { Client } from './client.js'
import { decodeCID } from 'ipfs-message-port-protocol/cid'
import { CID } from 'multiformats/cid'
/**
* @typedef {import('ipfs-message-port-server').FilesService} FilesService
* @typedef {import('ipfs-message-port-protocol/src/files').EncodedStat} EncodedStat
* @typedef {import('./client').MessageTransport} MessageTransport
* @typedef {import('./interface').MessagePortClientOptions} MessagePortClientOptions
* @typedef {import('ipfs-core-types/src/files').API<MessagePortClientOptions>} FilesAPI
*/
/**
* @class
* @extends {Client<FilesService>}
*/
export class FilesClient extends Client {
/**
* @param {MessageTransport} transport
*/
constructor (transport) {
super('files', ['stat'], transport)
}
}
/**
* @type {FilesAPI["stat"]}
*/
FilesClient.prototype.stat = async function stat (pathOrCID, options = {}) {
const { size, hash, withLocal, timeout, signal } = options
const { stat } = await this.remote.stat({
path: encodeLocation(pathOrCID),
size,
hash,
withLocal,
timeout,
signal
})
return decodeStat(stat)
}
/**
* Turns content address (path or CID) into path.
*
* @param {string|CID} pathOrCID
*/
const encodeLocation = pathOrCID => {
const cid = CID.asCID(pathOrCID)
return cid ? `/ipfs/${pathOrCID.toString()}` : pathOrCID.toString()
}
/**
* @param {EncodedStat} data
*/
const decodeStat = data => {
return { ...data, cid: decodeCID(data.cid) }
}