@libp2p/daemon-server 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,165 @@
1
+ {
2
+ "name": "@libp2p/daemon-server",
3
+ "version": "0.0.0",
4
+ "description": "API server for libp2p-daemon instances",
5
+ "license": "Apache-2.0 OR MIT",
6
+ "homepage": "https://github.com/libp2p/js-libp2p-daemon/tree/master/packages/libp2p-daemon#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/libp2p/js-libp2p-daemon.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/libp2p/js-libp2p-daemon/issues"
13
+ },
14
+ "keywords": [
15
+ "libp2p"
16
+ ],
17
+ "engines": {
18
+ "node": ">=16.0.0",
19
+ "npm": ">=7.0.0"
20
+ },
21
+ "bin": {
22
+ "jsp2pd": "src/cli/bin.js"
23
+ },
24
+ "type": "module",
25
+ "types": "./dist/src/index.d.ts",
26
+ "files": [
27
+ "src",
28
+ "dist/src",
29
+ "!dist/test",
30
+ "!**/*.tsbuildinfo"
31
+ ],
32
+ "exports": {
33
+ ".": {
34
+ "import": "./dist/src/index.js"
35
+ }
36
+ },
37
+ "eslintConfig": {
38
+ "extends": "ipfs",
39
+ "parserOptions": {
40
+ "sourceType": "module"
41
+ },
42
+ "ignorePatterns": [
43
+ "*.d.ts",
44
+ "src/profocol/index.js"
45
+ ]
46
+ },
47
+ "release": {
48
+ "branches": [
49
+ "master"
50
+ ],
51
+ "plugins": [
52
+ [
53
+ "@semantic-release/commit-analyzer",
54
+ {
55
+ "preset": "conventionalcommits",
56
+ "releaseRules": [
57
+ {
58
+ "breaking": true,
59
+ "release": "major"
60
+ },
61
+ {
62
+ "revert": true,
63
+ "release": "patch"
64
+ },
65
+ {
66
+ "type": "feat",
67
+ "release": "minor"
68
+ },
69
+ {
70
+ "type": "fix",
71
+ "release": "patch"
72
+ },
73
+ {
74
+ "type": "chore",
75
+ "release": "patch"
76
+ },
77
+ {
78
+ "type": "docs",
79
+ "release": "patch"
80
+ },
81
+ {
82
+ "type": "test",
83
+ "release": "patch"
84
+ },
85
+ {
86
+ "scope": "no-release",
87
+ "release": false
88
+ }
89
+ ]
90
+ }
91
+ ],
92
+ [
93
+ "@semantic-release/release-notes-generator",
94
+ {
95
+ "preset": "conventionalcommits",
96
+ "presetConfig": {
97
+ "types": [
98
+ {
99
+ "type": "feat",
100
+ "section": "Features"
101
+ },
102
+ {
103
+ "type": "fix",
104
+ "section": "Bug Fixes"
105
+ },
106
+ {
107
+ "type": "chore",
108
+ "section": "Trivial Changes"
109
+ },
110
+ {
111
+ "type": "docs",
112
+ "section": "Trivial Changes"
113
+ },
114
+ {
115
+ "type": "test",
116
+ "section": "Tests"
117
+ }
118
+ ]
119
+ }
120
+ }
121
+ ],
122
+ "@semantic-release/changelog",
123
+ "@semantic-release/npm",
124
+ "@semantic-release/github",
125
+ "@semantic-release/git"
126
+ ]
127
+ },
128
+ "scripts": {
129
+ "lint": "aegir lint",
130
+ "build": "tsc",
131
+ "pretest": "npm run build",
132
+ "test": "aegir test -t node -f test/*.js test/**/*.js",
133
+ "test:node": "aegir test -t node -f test/*.js test/**/*.js",
134
+ "release": "semantic-release"
135
+ },
136
+ "dependencies": {
137
+ "@chainsafe/libp2p-noise": "^6.0.0",
138
+ "@libp2p/daemon-protocol": "^0.0.0",
139
+ "@libp2p/interfaces": "^1.3.17",
140
+ "@multiformats/multiaddr": "^10.1.8",
141
+ "it-all": "^1.0.6",
142
+ "it-drain": "^1.0.5",
143
+ "it-buffer": "^0.1.3",
144
+ "it-handshake": "^3.0.1",
145
+ "it-length-prefixed": "^7.0.1",
146
+ "it-pipe": "^2.0.3",
147
+ "it-pushable": "^2.0.1",
148
+ "multiformats": "^9.4.2",
149
+ "promisify-es6": "^1.0.3",
150
+ "protobufjs": "^6.10.2",
151
+ "stream-to-it": "^0.2.0",
152
+ "streaming-iterables": "^6.0.0",
153
+ "uint8arrays": "^3.0.0",
154
+ "yargs": "^17.3.1",
155
+ "yargs-promise": "^1.1.0"
156
+ },
157
+ "devDependencies": {
158
+ "aegir": "^36.0.0",
159
+ "delay": "^5.0.0",
160
+ "it-pair": "^2.0.2",
161
+ "mocha": "^9.1.1",
162
+ "p-defer": "^4.0.0",
163
+ "sinon": "^13.0.1"
164
+ }
165
+ }
package/src/client.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { TCP } from '@libp2p/tcp'
2
+ import { passThroughUpgrader } from './util/index.js'
3
+ import type { Multiaddr } from '@multiformats/multiaddr'
4
+ import type { Connection } from '@libp2p/interfaces/connection'
5
+ import type { ConnectionHandler, Listener } from '@libp2p/interfaces/transport'
6
+
7
+ export class Client {
8
+ private multiaddr: Multiaddr
9
+ private tcp: TCP
10
+ private listener?: Listener
11
+
12
+ constructor (addr: Multiaddr) {
13
+ this.multiaddr = addr
14
+ this.tcp = new TCP()
15
+ }
16
+
17
+ /**
18
+ * Connects to a daemon at the unix socket path the client
19
+ * was created with
20
+ */
21
+ connect (): Promise<Connection> {
22
+ return this.tcp.dial(this.multiaddr, {
23
+ upgrader: passThroughUpgrader
24
+ })
25
+ }
26
+
27
+ /**
28
+ * Starts a server listening at `socketPath`. New connections
29
+ * will be sent to the `connectionHandler`.
30
+ */
31
+ async start (addr: Multiaddr, handler: ConnectionHandler): Promise<void> {
32
+ if (this.listener != null) {
33
+ await this.close()
34
+ }
35
+
36
+ this.listener = this.tcp.createListener({
37
+ handler,
38
+ upgrader: passThroughUpgrader
39
+ })
40
+
41
+ await this.listener.listen(addr)
42
+ }
43
+
44
+ /**
45
+ * Closes the socket
46
+ *
47
+ * @returns {Promise}
48
+ */
49
+ async close () {
50
+ if (this.listener != null) {
51
+ await this.listener.close()
52
+ }
53
+
54
+ this.listener = undefined
55
+ }
56
+ }
package/src/dht.ts ADDED
@@ -0,0 +1,153 @@
1
+ /* eslint max-depth: ["error", 6] */
2
+
3
+ import {
4
+ DHTResponse,
5
+ } from '@libp2p/daemon-protocol'
6
+ import { ErrorResponse, OkResponse } from './responses.js'
7
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
8
+ import type { DualDHT } from '@libp2p/interfaces/dht'
9
+ import type { CID } from 'multiformats/cid'
10
+ import drain from 'it-drain'
11
+
12
+ export interface DHTOperationsInit {
13
+ dht: DualDHT
14
+ }
15
+
16
+ export class DHTOperations {
17
+ private dht: DualDHT
18
+
19
+ constructor (init: DHTOperationsInit) {
20
+ const { dht } = init
21
+
22
+ this.dht = dht
23
+ }
24
+
25
+ async * provide (cid: CID) {
26
+ await this.dht.provide(cid)
27
+ yield OkResponse()
28
+ }
29
+
30
+ async * getClosestPeers (key: Uint8Array) {
31
+ yield OkResponse({
32
+ dht: {
33
+ type: DHTResponse.Type.BEGIN
34
+ }
35
+ })
36
+
37
+ for await (const event of this.dht.getClosestPeers(key)) {
38
+ if (event.name === 'PEER_RESPONSE') {
39
+ yield * event.closer.map(peer => DHTResponse.encode({
40
+ type: DHTResponse.Type.VALUE,
41
+ value: peer.id.toBytes()
42
+ }).finish())
43
+ }
44
+ }
45
+
46
+ yield DHTResponse.encode({
47
+ type: DHTResponse.Type.END
48
+ }).finish()
49
+ }
50
+
51
+ async * getPublicKey (peerId: PeerId) {
52
+ yield ErrorResponse(new Error('FIX ME: not implemented'))
53
+
54
+ /*
55
+ const pubKey = await this.dht.getPublicKey(peerId)
56
+
57
+ yield OkResponse({
58
+ dht: {
59
+ type: DHTResponse.Type.VALUE,
60
+ value: pubKey.bytes
61
+ }
62
+ })
63
+ */
64
+ }
65
+
66
+ async * getValue (key: Uint8Array) {
67
+ try {
68
+ for await (const event of this.dht.getClosestPeers(key)) {
69
+ if (event.name === 'VALUE') {
70
+ yield OkResponse({
71
+ dht: {
72
+ type: DHTResponse.Type.VALUE,
73
+ value: event.value
74
+ }
75
+ })
76
+ }
77
+ }
78
+ } catch (err: any) {
79
+ yield ErrorResponse(err.message)
80
+ }
81
+ }
82
+
83
+ async * putValue (key: Uint8Array, value: Uint8Array) {
84
+ try {
85
+ await drain(this.dht.put(key, value))
86
+
87
+ yield OkResponse()
88
+ } catch (err: any) {
89
+ yield ErrorResponse(err.message)
90
+ }
91
+ }
92
+
93
+ async * findPeer (peerId: PeerId) {
94
+ try {
95
+ for await (const event of this.dht.findPeer(peerId)) {
96
+ if (event.name === 'FINAL_PEER') {
97
+ yield OkResponse({
98
+ dht: {
99
+ type: DHTResponse.Type.VALUE,
100
+ peer: {
101
+ id: event.peer.id.toBytes(),
102
+ addrs: event.peer.multiaddrs.map(m => m.bytes)
103
+ }
104
+ }
105
+ })
106
+ }
107
+ }
108
+
109
+ throw new Error('Peer not found')
110
+ } catch (err: any) {
111
+ yield ErrorResponse(err)
112
+ }
113
+ }
114
+
115
+ async * findProviders (cid: CID, count: number) {
116
+ yield OkResponse({
117
+ dht: {
118
+ type: DHTResponse.Type.BEGIN
119
+ }
120
+ })
121
+
122
+ try {
123
+ const maxNumProviders = count
124
+ let found = 0
125
+
126
+ for await (const event of this.dht.findProviders(cid)) {
127
+ if (event.name === 'PEER_RESPONSE') {
128
+ for (const provider of event.providers) {
129
+ found++
130
+
131
+ yield DHTResponse.encode({
132
+ type: DHTResponse.Type.VALUE,
133
+ peer: {
134
+ id: provider.id.toBytes(),
135
+ addrs: (provider.multiaddrs || []).map(m => m.bytes)
136
+ }
137
+ }).finish()
138
+ }
139
+
140
+ if (maxNumProviders === found) {
141
+ return
142
+ }
143
+ }
144
+ }
145
+ } catch (err: any) {
146
+ yield ErrorResponse(err)
147
+ }
148
+
149
+ yield DHTResponse.encode({
150
+ type: DHTResponse.Type.END
151
+ }).finish()
152
+ }
153
+ }