@libp2p/daemon-server 0.0.1 → 1.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/dist/src/dht.d.ts +1 -1
- package/dist/src/dht.d.ts.map +1 -1
- package/dist/src/dht.js +23 -22
- package/dist/src/dht.js.map +1 -1
- package/dist/src/index.d.ts +19 -29
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +131 -123
- package/dist/src/index.js.map +1 -1
- package/dist/src/pubsub.d.ts +1 -1
- package/dist/src/pubsub.d.ts.map +1 -1
- package/dist/src/pubsub.js +44 -22
- package/dist/src/pubsub.js.map +1 -1
- package/dist/src/responses.d.ts +2 -2
- package/dist/src/responses.d.ts.map +1 -1
- package/dist/src/responses.js +5 -3
- package/dist/src/responses.js.map +1 -1
- package/package.json +16 -21
- package/src/dht.ts +24 -24
- package/src/index.ts +172 -164
- package/src/pubsub.ts +46 -27
- package/src/responses.ts +7 -5
- package/dist/src/client.d.ts +0 -26
- package/dist/src/client.d.ts.map +0 -1
- package/dist/src/client.js +0 -43
- package/dist/src/client.js.map +0 -1
- package/dist/src/stream-handler.d.ts +0 -28
- package/dist/src/stream-handler.d.ts.map +0 -1
- package/dist/src/stream-handler.js +0 -47
- package/dist/src/stream-handler.js.map +0 -1
- package/dist/src/util/index.d.ts +0 -13
- package/dist/src/util/index.d.ts.map +0 -1
- package/dist/src/util/index.js +0 -26
- package/dist/src/util/index.js.map +0 -1
- package/src/client.ts +0 -56
- package/src/stream-handler.ts +0 -65
- package/src/util/index.ts +0 -30
package/dist/src/client.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
2
|
-
import type { Connection } from '@libp2p/interfaces/connection';
|
|
3
|
-
import type { ConnectionHandler } from '@libp2p/interfaces/transport';
|
|
4
|
-
export declare class Client {
|
|
5
|
-
private multiaddr;
|
|
6
|
-
private tcp;
|
|
7
|
-
private listener?;
|
|
8
|
-
constructor(addr: Multiaddr);
|
|
9
|
-
/**
|
|
10
|
-
* Connects to a daemon at the unix socket path the client
|
|
11
|
-
* was created with
|
|
12
|
-
*/
|
|
13
|
-
connect(): Promise<Connection>;
|
|
14
|
-
/**
|
|
15
|
-
* Starts a server listening at `socketPath`. New connections
|
|
16
|
-
* will be sent to the `connectionHandler`.
|
|
17
|
-
*/
|
|
18
|
-
start(addr: Multiaddr, handler: ConnectionHandler): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* Closes the socket
|
|
21
|
-
*
|
|
22
|
-
* @returns {Promise}
|
|
23
|
-
*/
|
|
24
|
-
close(): Promise<void>;
|
|
25
|
-
}
|
|
26
|
-
//# sourceMappingURL=client.d.ts.map
|
package/dist/src/client.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAY,MAAM,8BAA8B,CAAA;AAE/E,qBAAa,MAAM;IACjB,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,GAAG,CAAK;IAChB,OAAO,CAAC,QAAQ,CAAC,CAAU;gBAEd,IAAI,EAAE,SAAS;IAK5B;;;OAGG;IACH,OAAO,IAAK,OAAO,CAAC,UAAU,CAAC;IAM/B;;;OAGG;IACG,KAAK,CAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAaxE;;;;OAIG;IACG,KAAK;CAOZ"}
|
package/dist/src/client.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { TCP } from '@libp2p/tcp';
|
|
2
|
-
import { passThroughUpgrader } from './util/index.js';
|
|
3
|
-
export class Client {
|
|
4
|
-
constructor(addr) {
|
|
5
|
-
this.multiaddr = addr;
|
|
6
|
-
this.tcp = new TCP();
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Connects to a daemon at the unix socket path the client
|
|
10
|
-
* was created with
|
|
11
|
-
*/
|
|
12
|
-
connect() {
|
|
13
|
-
return this.tcp.dial(this.multiaddr, {
|
|
14
|
-
upgrader: passThroughUpgrader
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Starts a server listening at `socketPath`. New connections
|
|
19
|
-
* will be sent to the `connectionHandler`.
|
|
20
|
-
*/
|
|
21
|
-
async start(addr, handler) {
|
|
22
|
-
if (this.listener != null) {
|
|
23
|
-
await this.close();
|
|
24
|
-
}
|
|
25
|
-
this.listener = this.tcp.createListener({
|
|
26
|
-
handler,
|
|
27
|
-
upgrader: passThroughUpgrader
|
|
28
|
-
});
|
|
29
|
-
await this.listener.listen(addr);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Closes the socket
|
|
33
|
-
*
|
|
34
|
-
* @returns {Promise}
|
|
35
|
-
*/
|
|
36
|
-
async close() {
|
|
37
|
-
if (this.listener != null) {
|
|
38
|
-
await this.listener.close();
|
|
39
|
-
}
|
|
40
|
-
this.listener = undefined;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
//# sourceMappingURL=client.js.map
|
package/dist/src/client.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAKrD,MAAM,OAAO,MAAM;IAKjB,YAAa,IAAe;QAC1B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;QACrB,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,EAAE,CAAA;IACtB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnC,QAAQ,EAAE,mBAAmB;SAC9B,CAAC,CAAA;IACJ,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAK,CAAE,IAAe,EAAE,OAA0B;QACtD,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,EAAE;YACzB,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;SACnB;QAED,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC;YACtC,OAAO;YACP,QAAQ,EAAE,mBAAmB;SAC9B,CAAC,CAAA;QAEF,MAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK;QACT,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,EAAE;YACzB,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAA;SAC5B;QAED,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAA;IAC3B,CAAC;CACF"}
|
|
@@ -1,28 +0,0 @@
|
|
|
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 stream;
|
|
8
|
-
private 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(): void;
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=stream-handler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,MAAM,CAAoB;IAClC,OAAO,CAAC,KAAK,CAAW;IACjB,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IAClC;;OAEG;gBACU,IAAI,EAAE,oBAAoB;IAQvC;;OAEG;IACG,IAAI;IAWV,KAAK,CAAE,GAAG,EAAE,UAAU;IAOtB;;OAEG;IACH,IAAI;IAKJ;;OAEG;IACH,KAAK;CAIN"}
|
|
@@ -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"}
|
package/dist/src/util/index.d.ts
DELETED
|
@@ -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"}
|
package/dist/src/util/index.js
DELETED
|
@@ -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
|
-
}
|
package/src/stream-handler.ts
DELETED
|
@@ -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'
|