/
ipfs.ts
75 lines (69 loc) · 1.65 KB
/
ipfs.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
import { NativeModules } from 'react-native'
import { Buffer } from 'buffer'
const { IpfsBridge } = NativeModules
/**
* Get node's IPFS peerId.
* ```typescript
* Textile.ipfs.peerId();
* ```
*/
export async function peerId(): Promise<string> {
const result = await IpfsBridge.peerId()
return result as string
}
/**
* Open a new direct connection to a peer using an IPFS multiaddr
* ```typescript
* Textile.ipfs.connect(multiaddr);
* ```
*/
export async function connect(multiaddr: string): Promise<boolean> {
const result = await IpfsBridge.connect(multiaddr)
return result
}
/**
* Get raw file data by IPFS path. See `cat` method in IPFS.
* ```typescript
* Textile.ipfs.dataAtPath(path);
* ```
*/
export async function dataAtPath(
path: string
): Promise<{ data: Uint8Array; mediaType: string }> {
const { data, mediaType } = await IpfsBridge.dataAtPath(path)
return { data: Buffer.from(data, 'base64'), mediaType }
}
/**
* Publishes a message to a given pubsub topic
* ```typescript
* Textile.ipfs.pubsubPub(topic, data);
* ```
*/
export async function pubsubPub(
topic: string,
data: string | object
): Promise<string> {
return await IpfsBridge.pubsubPub(
topic,
typeof data === 'string' ? data : JSON.stringify(data)
)
}
/**
* Subscribes to messages on a given topic
* ```typescript
* Textile.ipfs.pubsubSub(topic);
* ```
*/
export async function pubsubSub(
topic: string
): Promise<{ queryId: string; queryHandle: { close: () => void } }> {
const queryId = await IpfsBridge.pubsubSub(topic)
return {
queryId,
queryHandle: {
close: () => {
IpfsBridge.cancelPubsubSub(queryId)
}
}
}
}