@libp2p/daemon-protocol 0.0.0 → 0.0.1

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/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  <a href="http://libp2p.io/"><img src="https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square" /></a>
4
4
  <a href="http://webchat.freenode.net/?channels=%23libp2p"><img src="https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square" /></a>
5
5
  <a href="https://discuss.libp2p.io"><img src="https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg" /></a>
6
- <a href="https://waffle.io/libp2p/libp2p"><img src="https://img.shields.io/badge/pm-waffle-yellow.svg?style=flat-square" /></a>
7
6
 
8
7
  > Contains the protobuf definitions of the communication protocol for libp2p daemons
9
8
 
@@ -13,18 +12,6 @@
13
12
  npm i @libp2p/daemon-protocol
14
13
  ```
15
14
 
16
- ## Usage
17
-
18
- For a full list of options, you can run help `jsp2pd --help`.
19
- Running the defaults, `jsp2pd`, will start the daemon and bind it to a local unix socket path.
20
- Daemon clients will be able to communicate with the daemon over that unix socket.
21
-
22
- As an alternative, you can use this daemon with a different version of libp2p as the one specified in `package.json`. You just need to define its path through an environment variable as follows:
23
-
24
- ```sh
25
- export LIBP2P_JS=./../../js-libp2p/src/index.js
26
- ```
27
-
28
15
  ## Contribute
29
16
 
30
17
  This module is actively under development. Please check out the issues and submit PRs!
@@ -0,0 +1,28 @@
1
+ import type { Duplex, Source } from 'it-stream-types';
2
+ export interface StreamHandlerOptions {
3
+ stream: Duplex<Uint8Array>;
4
+ maxLength?: number;
5
+ }
6
+ export declare class StreamHandler {
7
+ private readonly stream;
8
+ private readonly shake;
9
+ decoder: Source<Uint8Array>;
10
+ /**
11
+ * Create a stream handler for connection
12
+ */
13
+ constructor(opts: StreamHandlerOptions);
14
+ /**
15
+ * Read and decode message
16
+ */
17
+ read(): Promise<any>;
18
+ write(msg: Uint8Array): void;
19
+ /**
20
+ * Return the handshake rest stream and invalidate handler
21
+ */
22
+ rest(): Duplex<Uint8Array, Uint8Array, Promise<void>>;
23
+ /**
24
+ * Close the stream
25
+ */
26
+ close(): Promise<void>;
27
+ }
28
+ //# sourceMappingURL=stream-handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-handler.d.ts","sourceRoot":"","sources":["../../src/stream-handler.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAKrD,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAW;IAC1B,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAClC;;OAEG;gBACU,IAAI,EAAE,oBAAoB;IAQvC;;OAEG;IACG,IAAI;IAYV,KAAK,CAAE,GAAG,EAAE,UAAU;IAOtB;;OAEG;IACH,IAAI;IAKJ;;OAEG;IACG,KAAK;CAIZ"}
@@ -0,0 +1,47 @@
1
+ import * as lp from 'it-length-prefixed';
2
+ import { handshake } from 'it-handshake';
3
+ import { logger } from '@libp2p/logger';
4
+ const log = logger('libp2p:daemon-protocol:stream-handler');
5
+ export class StreamHandler {
6
+ /**
7
+ * Create a stream handler for connection
8
+ */
9
+ constructor(opts) {
10
+ const { stream, maxLength } = opts;
11
+ this.stream = stream;
12
+ this.shake = handshake(this.stream);
13
+ this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength ?? 4096 });
14
+ }
15
+ /**
16
+ * Read and decode message
17
+ */
18
+ async read() {
19
+ // @ts-expect-error decoder is really a generator
20
+ const msg = await this.decoder.next();
21
+ if (msg.value != null) {
22
+ return msg.value.slice();
23
+ }
24
+ log('read received no value, closing stream');
25
+ // End the stream, we didn't get data
26
+ await this.close();
27
+ }
28
+ write(msg) {
29
+ log('write message');
30
+ this.shake.write(lp.encode.single(msg).slice());
31
+ }
32
+ /**
33
+ * Return the handshake rest stream and invalidate handler
34
+ */
35
+ rest() {
36
+ this.shake.rest();
37
+ return this.shake.stream;
38
+ }
39
+ /**
40
+ * Close the stream
41
+ */
42
+ async close() {
43
+ log('closing the stream');
44
+ await this.rest().sink([]);
45
+ }
46
+ }
47
+ //# sourceMappingURL=stream-handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-handler.js","sourceRoot":"","sources":["../../src/stream-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAIvC,MAAM,GAAG,GAAG,MAAM,CAAC,uCAAuC,CAAC,CAAA;AAO3D,MAAM,OAAO,aAAa;IAIxB;;OAEG;IACH,YAAa,IAA0B;QACrC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAA;QAElC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACnC,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,aAAa,EAAE,SAAS,IAAI,IAAI,EAAE,CAAC,CAAA;IAC9F,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACR,iDAAiD;QACjD,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;QACrC,IAAI,GAAG,CAAC,KAAK,IAAI,IAAI,EAAE;YACrB,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;SACzB;QAED,GAAG,CAAC,wCAAwC,CAAC,CAAA;QAC7C,qCAAqC;QACrC,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;IACpB,CAAC;IAED,KAAK,CAAE,GAAe;QACpB,GAAG,CAAC,eAAe,CAAC,CAAA;QACpB,IAAI,CAAC,KAAK,CAAC,KAAK,CACd,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAC9B,CAAA;IACH,CAAC;IAED;;OAEG;IACH,IAAI;QACF,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAA;IAC1B,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAK;QACT,GAAG,CAAC,oBAAoB,CAAC,CAAA;QACzB,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAC5B,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ import type { Upgrader } from '@libp2p/interfaces/transport';
2
+ export declare const passThroughUpgrader: Upgrader;
3
+ //# sourceMappingURL=upgrader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upgrader.d.ts","sourceRoot":"","sources":["../../src/upgrader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAE5D,eAAO,MAAM,mBAAmB,EAAE,QAKjC,CAAA"}
@@ -0,0 +1,7 @@
1
+ export const passThroughUpgrader = {
2
+ // @ts-expect-error should return a connection
3
+ upgradeInbound: async (maConn) => maConn,
4
+ // @ts-expect-error should return a connection
5
+ upgradeOutbound: async (maConn) => maConn
6
+ };
7
+ //# sourceMappingURL=upgrader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upgrader.js","sourceRoot":"","sources":["../../src/upgrader.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,mBAAmB,GAAa;IAC3C,8CAA8C;IAC9C,cAAc,EAAE,KAAK,EAAC,MAAM,EAAC,EAAE,CAAC,MAAM;IACtC,8CAA8C;IAC9C,eAAe,EAAE,KAAK,EAAC,MAAM,EAAC,EAAE,CAAC,MAAM;CACxC,CAAA"}
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@libp2p/daemon-protocol",
3
- "version": "0.0.0",
4
- "description": "",
3
+ "version": "0.0.1",
4
+ "description": "Communication protocol between libp2p daemons and clients",
5
5
  "author": "",
6
6
  "license": "Apache-2.0 OR MIT",
7
- "homepage": "https://github.com/libp2p/js-libp2p-daemon-protocol#readme",
7
+ "homepage": "https://github.com/libp2p/js-libp2p-daemon/tree/master/packages/libp2p-daemon-protocol#readme",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "git+https://github.com/libp2p/js-libp2p-daemon-protocol.git"
10
+ "url": "git+https://github.com/libp2p/js-libp2p-daemon.git"
11
11
  },
12
12
  "bugs": {
13
- "url": "https://github.com/libp2p/js-libp2p-daemon-protocol/issues"
13
+ "url": "https://github.com/libp2p/js-libp2p-daemon/issues"
14
14
  },
15
15
  "engines": {
16
16
  "node": ">=16.0.0",
@@ -18,6 +18,22 @@
18
18
  },
19
19
  "type": "module",
20
20
  "types": "./dist/src/index.d.ts",
21
+ "typesVersions": {
22
+ "*": {
23
+ "*": [
24
+ "*",
25
+ "dist/*",
26
+ "dist/src/*",
27
+ "dist/src/*/index"
28
+ ],
29
+ "src/*": [
30
+ "*",
31
+ "dist/*",
32
+ "dist/src/*",
33
+ "dist/src/*/index"
34
+ ]
35
+ }
36
+ },
21
37
  "files": [
22
38
  "src",
23
39
  "dist/src",
@@ -27,13 +43,23 @@
27
43
  "exports": {
28
44
  ".": {
29
45
  "import": "./dist/src/index.js"
46
+ },
47
+ "./stream-handler": {
48
+ "import": "./dist/src/stream-handler.js"
49
+ },
50
+ "./upgrader": {
51
+ "import": "./dist/src/upgrader.js"
30
52
  }
31
53
  },
32
54
  "eslintConfig": {
33
55
  "extends": "ipfs",
34
56
  "parserOptions": {
35
57
  "sourceType": "module"
36
- }
58
+ },
59
+ "ignorePatterns": [
60
+ "*.d.ts",
61
+ "src/index.js"
62
+ ]
37
63
  },
38
64
  "release": {
39
65
  "branches": [
@@ -118,7 +144,7 @@
118
144
  },
119
145
  "scripts": {
120
146
  "lint": "aegir lint",
121
- "build": "mkdirp dist/src && cp src/*.js src/*.d.ts dist/src",
147
+ "build": "tsc && cp src/*.js src/*.d.ts dist/src",
122
148
  "generate": "npm run generate:proto && npm run generate:proto-types",
123
149
  "generate:proto": "pbjs -t static-module -w es6 -r libp2p-daemon --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/protocol/index.js ./src/protocol/index.proto",
124
150
  "generate:proto-types": "pbts -o src/protocol/index.d.ts src/protocol/index.js",
@@ -0,0 +1,66 @@
1
+ import * as lp from 'it-length-prefixed'
2
+ import { handshake } from 'it-handshake'
3
+ import { logger } from '@libp2p/logger'
4
+ import type { Duplex, Source } from 'it-stream-types'
5
+ import type { Handshake } from 'it-handshake'
6
+
7
+ const log = logger('libp2p:daemon-protocol:stream-handler')
8
+
9
+ export interface StreamHandlerOptions {
10
+ stream: Duplex<Uint8Array>
11
+ maxLength?: number
12
+ }
13
+
14
+ export class StreamHandler {
15
+ private readonly stream: Duplex<Uint8Array>
16
+ private readonly shake: Handshake
17
+ public decoder: Source<Uint8Array>
18
+ /**
19
+ * Create a stream handler for connection
20
+ */
21
+ constructor (opts: StreamHandlerOptions) {
22
+ const { stream, maxLength } = opts
23
+
24
+ this.stream = stream
25
+ this.shake = handshake(this.stream)
26
+ this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength ?? 4096 })
27
+ }
28
+
29
+ /**
30
+ * Read and decode message
31
+ */
32
+ async read () {
33
+ // @ts-expect-error decoder is really a generator
34
+ const msg = await this.decoder.next()
35
+ if (msg.value != null) {
36
+ return msg.value.slice()
37
+ }
38
+
39
+ log('read received no value, closing stream')
40
+ // End the stream, we didn't get data
41
+ await this.close()
42
+ }
43
+
44
+ write (msg: Uint8Array) {
45
+ log('write message')
46
+ this.shake.write(
47
+ lp.encode.single(msg).slice()
48
+ )
49
+ }
50
+
51
+ /**
52
+ * Return the handshake rest stream and invalidate handler
53
+ */
54
+ rest () {
55
+ this.shake.rest()
56
+ return this.shake.stream
57
+ }
58
+
59
+ /**
60
+ * Close the stream
61
+ */
62
+ async close () {
63
+ log('closing the stream')
64
+ await this.rest().sink([])
65
+ }
66
+ }
@@ -0,0 +1,8 @@
1
+ import type { Upgrader } from '@libp2p/interfaces/transport'
2
+
3
+ export const passThroughUpgrader: Upgrader = {
4
+ // @ts-expect-error should return a connection
5
+ upgradeInbound: async maConn => maConn,
6
+ // @ts-expect-error should return a connection
7
+ upgradeOutbound: async maConn => maConn
8
+ }