@libp2p/daemon-server 0.0.1 → 0.0.2

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.
@@ -1,47 +0,0 @@
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-client: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) {
22
- return msg.value.slice();
23
- }
24
- log('read received no value, closing stream');
25
- // End the stream, we didn't get data
26
- 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
- close() {
43
- log('closing the stream');
44
- this.rest().sink([]);
45
- }
46
- }
47
- //# sourceMappingURL=stream-handler.js.map
@@ -1 +0,0 @@
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,qCAAqC,CAAC,CAAA;AAOzD,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,EAAE;YACb,OAAO,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;SACzB;QACD,GAAG,CAAC,wCAAwC,CAAC,CAAA;QAC7C,qCAAqC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAA;IACd,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;QACH,GAAG,CAAC,oBAAoB,CAAC,CAAA;QACzB,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACtB,CAAC;CACF"}
@@ -1,13 +0,0 @@
1
- import type { Upgrader } from '@libp2p/interfaces/transport';
2
- import type { Multiaddr } from '@multiformats/multiaddr';
3
- export declare const passThroughUpgrader: Upgrader;
4
- /**
5
- * Converts the multiaddr to a nodejs NET compliant option
6
- * for .connect or .listen
7
- *
8
- * @param {Multiaddr} addr
9
- * @returns {string|object} A nodejs NET compliant option
10
- */
11
- export declare function multiaddrToNetConfig(addr: Multiaddr): string | import("@multiformats/multiaddr").NodeAddress;
12
- export declare const isWindows: boolean;
13
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/util/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAIxD,eAAO,MAAM,mBAAmB,EAAE,QAKjC,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAE,IAAI,EAAE,SAAS,0DAQpD;AAED,eAAO,MAAM,SAAS,SAA4B,CAAA"}
@@ -1,26 +0,0 @@
1
- import { resolve } from 'path';
2
- import os from 'os';
3
- export const passThroughUpgrader = {
4
- // @ts-expect-error
5
- upgradeInbound: async (maConn) => maConn,
6
- // @ts-expect-error
7
- upgradeOutbound: async (maConn) => maConn
8
- };
9
- /**
10
- * Converts the multiaddr to a nodejs NET compliant option
11
- * for .connect or .listen
12
- *
13
- * @param {Multiaddr} addr
14
- * @returns {string|object} A nodejs NET compliant option
15
- */
16
- export function multiaddrToNetConfig(addr) {
17
- const listenPath = addr.getPath();
18
- // unix socket listening
19
- if (listenPath) {
20
- return resolve(listenPath);
21
- }
22
- // tcp listening
23
- return addr.nodeAddress();
24
- }
25
- export const isWindows = os.platform() === 'win32';
26
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/util/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAC9B,OAAO,EAAE,MAAM,IAAI,CAAA;AAEnB,MAAM,CAAC,MAAM,mBAAmB,GAAa;IAC3C,mBAAmB;IACnB,cAAc,EAAE,KAAK,EAAC,MAAM,EAAC,EAAE,CAAC,MAAM;IACtC,mBAAmB;IACnB,eAAe,EAAE,KAAK,EAAC,MAAM,EAAC,EAAE,CAAC,MAAM;CACxC,CAAA;AAED;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAE,IAAe;IACnD,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;IACjC,wBAAwB;IACxB,IAAI,UAAU,EAAE;QACd,OAAO,OAAO,CAAC,UAAU,CAAC,CAAA;KAC3B;IACD,gBAAgB;IAChB,OAAO,IAAI,CAAC,WAAW,EAAE,CAAA;AAC3B,CAAC;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,EAAE,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAA"}
package/src/client.ts DELETED
@@ -1,56 +0,0 @@
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
- }
@@ -1,65 +0,0 @@
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-client:stream-handler')
8
-
9
- export interface StreamHandlerOptions {
10
- stream: Duplex<Uint8Array>
11
- maxLength?: number
12
- }
13
-
14
- export class StreamHandler {
15
- private stream: Duplex<Uint8Array>
16
- private 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) {
36
- return msg.value.slice()
37
- }
38
- log('read received no value, closing stream')
39
- // End the stream, we didn't get data
40
- this.close()
41
- }
42
-
43
- write (msg: Uint8Array) {
44
- log('write message')
45
- this.shake.write(
46
- lp.encode.single(msg).slice()
47
- )
48
- }
49
-
50
- /**
51
- * Return the handshake rest stream and invalidate handler
52
- */
53
- rest () {
54
- this.shake.rest()
55
- return this.shake.stream
56
- }
57
-
58
- /**
59
- * Close the stream
60
- */
61
- close () {
62
- log('closing the stream')
63
- this.rest().sink([])
64
- }
65
- }
package/src/util/index.ts DELETED
@@ -1,30 +0,0 @@
1
- import type { Upgrader } from '@libp2p/interfaces/transport'
2
- import type { Multiaddr } from '@multiformats/multiaddr'
3
- import { resolve } from 'path'
4
- import os from 'os'
5
-
6
- export const passThroughUpgrader: Upgrader = {
7
- // @ts-expect-error
8
- upgradeInbound: async maConn => maConn,
9
- // @ts-expect-error
10
- upgradeOutbound: async maConn => maConn
11
- }
12
-
13
- /**
14
- * Converts the multiaddr to a nodejs NET compliant option
15
- * for .connect or .listen
16
- *
17
- * @param {Multiaddr} addr
18
- * @returns {string|object} A nodejs NET compliant option
19
- */
20
- export function multiaddrToNetConfig (addr: Multiaddr) {
21
- const listenPath = addr.getPath()
22
- // unix socket listening
23
- if (listenPath) {
24
- return resolve(listenPath)
25
- }
26
- // tcp listening
27
- return addr.nodeAddress()
28
- }
29
-
30
- export const isWindows = os.platform() === 'win32'