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 pathdag.js
74 lines (65 loc) · 1.93 KB
/
dag.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
import { Client } from './client.js'
import { encodeCID, decodeCID } from 'ipfs-message-port-protocol/cid'
import { encodeNode, decodeNode } from 'ipfs-message-port-protocol/dag'
/**
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('ipfs-message-port-protocol/src/cid').EncodedCID} EncodedCID
* @typedef {import('ipfs-message-port-server').DAGService} DagService
* @typedef {import('./client').MessageTransport} MessageTransport
* @typedef {import('./interface').MessagePortClientOptions} MessagePortClientOptions
* @typedef {import('ipfs-core-types/src/dag').API<MessagePortClientOptions>} DAGAPI
*/
/**
* @class
* @extends {Client<DagService>}
*/
export class DAGClient extends Client {
/**
* @param {MessageTransport} transport
*/
constructor (transport) {
super('dag', ['put', 'get', 'resolve'], transport)
}
}
/**
* @type {DAGAPI["put"]}
*/
DAGClient.prototype.put = async function put (dagNode, options = {}) {
const encodedCID = await this.remote.put({
...options,
dagNode: encodeNode(dagNode, options.transfer)
})
return decodeCID(encodedCID)
}
/**
* @type {DAGAPI["get"]}
*/
DAGClient.prototype.get = async function get (cid, options = {}) {
const { value, remainderPath } = await this.remote.get({
...options,
cid: encodeCID(cid, options.transfer)
})
return { value: decodeNode(value), remainderPath }
}
/**
* @type {DAGAPI["resolve"]}
*/
DAGClient.prototype.resolve = async function resolve (cid, options = {}) {
const { cid: encodedCID, remainderPath } = await this.remote.resolve({
...options,
cid: encodeCIDOrPath(cid, options.transfer)
})
return { cid: decodeCID(encodedCID), remainderPath }
}
/**
* @param {string|CID} input
* @param {Set<Transferable>} [transfer]
* @returns {string|EncodedCID}
*/
const encodeCIDOrPath = (input, transfer) => {
if (typeof input === 'string') {
return input
} else {
return encodeCID(input, transfer)
}
}