forked from spknetwork/video-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
139 lines (124 loc) · 6.9 KB
/
client.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
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'
import MPLEX from 'libp2p-mplex'
import { NOISE } from 'libp2p-noise'
import MulticastDNS from 'libp2p-mdns'
import Bootstrap from 'libp2p-bootstrap'
import DHT from 'libp2p-kad-dht'
import PeerId from 'peer-id'
import { CeramicClient } from '@ceramicnetwork/http-client'
import Pushable from 'it-pushable'
import {pipe} from 'it-pipe'
import {decode, encode} from './src/modules/libp2p/frame-codec.util'
import { MESSAGE_TYPES } from './src/modules/libp2p/messages.model'
import { TileDocument } from '@ceramicnetwork/stream-tile'
import { EncodeStatus } from './src/modules/encoder.model'
import logUpdate from 'log-update';
import cli from 'cli-ux'
const ClientKey =
{
id: 'Qmc9qsUzsCMyeCwxh5DPer5sNtUXXz1RxCf8BD7U1j2CuG',
privKey: 'CAASpwkwggSjAgEAAoIBAQDQb2mb/sBeJG6bjL9rR2t9YXUvlpb4b5m1vulNUydWlbeumJhg6foqiK3dC8x1g8h8LY5nP5HITc6r6L22/K1xYFj4+rRsJCf8xoB9eYUCjWAuKh07kRbj95JJGoi9AiW//XUEGmkPcKpb9aB3ocxwgyqTkPKtga1Xh+hkTaRg/Nwuc0CRnaxoyUFV/jbIEfLvvkURecAQOsxm80SVcHS4lZL8Kcq9BEu+Ui8Ien6EdkSGDtgwxum1UUn8DO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0UwxCFmuu3adOvbm09qmddZjbzEUbn2xVRJTBioIYK1y4YCB8kYjmD6skCgYEAsxSXWogFc1mCEdNUBNruRf0ZxPjgDj+qBKaaZ4P6Nr7qxAIv1O09xnIGc8XMtN9ywutyIsf7+YcofpC3bHqFmke8wysTW/Ggwb+6BTCSQwXaGUQFN0BmRyQEmuI4rkZ+0ynqgtz3lH5Kk5H6/pzxQ3PCM2as7OfFR7L0Dte6X/w=',
pubKey: 'CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQb2mb/sBeJG6bjL9rR2t9YXUvlpb4b5m1vulNUydWlbeumJhg6foqiK3dC8x1g8h8LY5nP5HITc6r6L22/K1xYFj4+rRsJCf8xoB9eYUCjWAuKh07kRbj95JJGoi9AiW//XUEGmkPcKpb9aB3ocxwgyqTkPKtga1Xh+hkTaRg/Nwuc0CRnaxoyUFV/jbIEfLvvkURecAQOsxm80SVcHS4lZL8Kcq9BEu+Ui8Ien6EdkSGDtgwxum1UUn8DOBNWN/geQ88n0ch6raVPk2YNWwVdcX32caXddwKd5RdXEiOHG5osvwFgUf8RG3M5AUU6Anh5Aong/erqf5zmjcbK+blAgMBAAE='
}
void (async () => {
console.log('P2P interface starting up')
const ceramic = new CeramicClient('https://ceramic-clay.3boxlabs.com') //Using the public node for now.
const idListener = await PeerId.createFromJSON(ClientKey)
const libp2p = await Libp2p.create({
peerId: idListener,
modules: {
transport: [TCP],
streamMuxer: [MPLEX],
connEncryption: [NOISE],
peerDiscovery: [MulticastDNS, Bootstrap],
dht: DHT
},
addresses: {
listen: ['/ip4/10.0.1.188/tcp/14446']
},
config: {
peerDiscovery: {
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
// The associated object, will be passed to the service when it is instantiated.
[MulticastDNS.tag]: {
interval: 1000,
enabled: true
},
[Bootstrap.tag]: {
list: [ // A list of bootstrap peers to connect to starting up the node
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
],
interval: 2000,
enabled: true
}
// .. other discovery module options.
},
dht: { // The DHT options (and defaults) can be found in its documentation
kBucketSize: 20,
enabled: true, // This flag is required for DHT to run (disabled by default)
randomWalk: {
enabled: true, // Allows to disable discovery (enabled by default)
interval: 300e3,
timeout: 10e3
}
}
}
})
// start libp2p
await libp2p.start()
console.log(libp2p.peerId.toJSON())
setInterval(() => {
//console.log(libp2p.connections.size)
}, 5000)
const handler = async ({ connection, stream, protocol }) => {
// use stream or connection according to the needs
console.log(connection, stream, protocol)
for await(let item of stream.source) {
console.log(item)
}
}
libp2p.handle('/spk-video-encoder/1.0.0', handler)
const output = await libp2p.dialProtocol("/ip4/10.0.1.188/tcp/14445/p2p/QmctF7GPunpcLu3Fn77Ypo657TA9GpTiipSDUUMoD2k5Kq", '/spk-video-encoder/1.0.0')
console.log(output)
cli.action.start('Encoding Video')
// do some action...
// stop the spinner
//cli.action.stop() // shows 'starting a process... done'
void (async () => {
let encodeId;
void (async () => {
for await(let item of output.stream.source) {
const decodedMessage = decode(item._bufs[0])
console.log(decodedMessage)
if(decodedMessage.type === MESSAGE_TYPES.RESPONE_ENCODE_JOB) {
encodeId = decodedMessage.streamId
pushable.push(encode({
type: MESSAGE_TYPES.SUBSCRIBE_UPDATE,
streamId: decodedMessage.streamId
}))
let encodeDoc = await TileDocument.load(ceramic, encodeId)
setInterval(async () => {
await encodeDoc.sync()
const contentData = encodeDoc.content as any;
if(contentData.status === EncodeStatus.COMPLETE) {
console.log(`Job complete, IPFS Hash is ${contentData.outCid}`)
cli.action.stop()
process.exit(0)
}
}, 1000)
}
}})()
})()
const pushable = Pushable()
pipe(pushable, output.stream)
pushable.push(encode({
type: MESSAGE_TYPES.REQUEST_ENCODE_JOB,
ipfsHash: 'Qma9ZjjtH7fdLWSrMU43tFihvSN59aQdes7f5KW6vGGk6e'
}))
//pushable.end()
//console.log(output.stream.close())
})()