@libp2p/http-utils 1.0.1 → 1.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.
- package/dist/index.min.js +16 -16
- package/dist/index.min.js.map +3 -3
- package/dist/src/stream-to-socket.d.ts +36 -1
- package/dist/src/stream-to-socket.d.ts.map +1 -1
- package/dist/src/stream-to-socket.js +90 -85
- package/dist/src/stream-to-socket.js.map +1 -1
- package/dist/typedoc-urls.json +1 -0
- package/package.json +1 -1
- package/src/stream-to-socket.ts +101 -90
|
@@ -1,4 +1,39 @@
|
|
|
1
|
+
import { Duplex } from 'node:stream';
|
|
1
2
|
import type { Connection, Stream } from '@libp2p/interface';
|
|
2
|
-
import type { Socket } from 'node:net';
|
|
3
|
+
import type { Socket, SocketConnectOpts, AddressInfo, SocketReadyState } from 'node:net';
|
|
4
|
+
export declare class Libp2pSocket extends Duplex {
|
|
5
|
+
#private;
|
|
6
|
+
readonly autoSelectFamilyAttemptedAddresses: never[];
|
|
7
|
+
readonly connecting = false;
|
|
8
|
+
readonly pending = false;
|
|
9
|
+
remoteAddress: string;
|
|
10
|
+
bytesRead: number;
|
|
11
|
+
bytesWritten: number;
|
|
12
|
+
timeout: number;
|
|
13
|
+
allowHalfOpen: boolean;
|
|
14
|
+
constructor();
|
|
15
|
+
setStream(stream: Stream, connection: Connection): void;
|
|
16
|
+
_write(chunk: Uint8Array, encoding: string, cb: (err?: Error) => void): void;
|
|
17
|
+
_read(size: number): void;
|
|
18
|
+
_destroy(err: Error, cb: (err?: Error) => void): void;
|
|
19
|
+
_final(cb: (err?: Error) => void): void;
|
|
20
|
+
get readyState(): SocketReadyState;
|
|
21
|
+
get bufferSize(): number;
|
|
22
|
+
destroySoon(): void;
|
|
23
|
+
connect(options: SocketConnectOpts, connectionListener?: () => void): this;
|
|
24
|
+
connect(port: number, host: string, connectionListener?: () => void): this;
|
|
25
|
+
connect(port: number, connectionListener?: () => void): this;
|
|
26
|
+
connect(path: string, connectionListener?: () => void): this;
|
|
27
|
+
setEncoding(encoding?: BufferEncoding): this;
|
|
28
|
+
resetAndDestroy(): this;
|
|
29
|
+
setTimeout(timeout: number, callback?: () => void): this;
|
|
30
|
+
setNoDelay(noDelay?: boolean): this;
|
|
31
|
+
setKeepAlive(enable?: boolean, initialDelay?: number): this;
|
|
32
|
+
address(): AddressInfo | Record<string, any>;
|
|
33
|
+
unref(): this;
|
|
34
|
+
ref(): this;
|
|
35
|
+
write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
|
|
36
|
+
write(str: Uint8Array | string, encoding?: BufferEncoding, cb?: (err?: Error) => void): boolean;
|
|
37
|
+
}
|
|
3
38
|
export declare function streamToSocket(stream: Stream, connection: Connection): Socket;
|
|
4
39
|
//# sourceMappingURL=stream-to-socket.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-to-socket.d.ts","sourceRoot":"","sources":["../../src/stream-to-socket.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream-to-socket.d.ts","sourceRoot":"","sources":["../../src/stream-to-socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE3D,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAIxF,qBAAa,YAAa,SAAQ,MAAM;;IACtC,SAAgB,kCAAkC,UAAK;IACvD,SAAgB,UAAU,SAAQ;IAClC,SAAgB,OAAO,SAAQ;IACxB,aAAa,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,SAAc;IACrB,aAAa,EAAE,OAAO,CAAA;;IAc7B,SAAS,CAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAA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EAAE,GAAG,CAAC;IAM7C,KAAK,IAAK,IAAI;IAMd,GAAG,IAAK,IAAI;IAMZ,KAAK,CAAE,MAAM,EAAE,UAAU,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,GAAG,OAAO;IACxE,KAAK,CAAE,GAAG,EAAE,UAAU,GAAG,MAAM,EAAE,QAAQ,CAAC,EAAE,cAAc,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,GAAG,OAAO;CAIjG;AAED,wBAAgB,cAAc,CAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,MAAM,CAK9E"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Duplex } from 'node:stream';
|
|
2
2
|
import { byteStream } from 'it-byte-stream';
|
|
3
3
|
const MAX_TIMEOUT = 2_147_483_647;
|
|
4
|
-
class Libp2pSocket extends Duplex {
|
|
4
|
+
export class Libp2pSocket extends Duplex {
|
|
5
5
|
autoSelectFamilyAttemptedAddresses = [];
|
|
6
6
|
connecting = false;
|
|
7
7
|
pending = false;
|
|
@@ -10,89 +10,92 @@ class Libp2pSocket extends Duplex {
|
|
|
10
10
|
bytesWritten;
|
|
11
11
|
timeout = MAX_TIMEOUT;
|
|
12
12
|
allowHalfOpen;
|
|
13
|
-
stream;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
super(
|
|
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
|
-
|
|
13
|
+
#stream;
|
|
14
|
+
#bytes;
|
|
15
|
+
constructor() {
|
|
16
|
+
super();
|
|
17
|
+
this.bytesRead = 0;
|
|
18
|
+
this.bytesWritten = 0;
|
|
19
|
+
this.allowHalfOpen = true;
|
|
20
|
+
this.remoteAddress = '';
|
|
21
|
+
}
|
|
22
|
+
setStream(stream, connection) {
|
|
23
|
+
this.#bytes = byteStream(stream);
|
|
24
|
+
this.#stream = stream;
|
|
25
|
+
this.remoteAddress = connection.remoteAddr.toString();
|
|
26
|
+
}
|
|
27
|
+
_write(chunk, encoding, cb) {
|
|
28
|
+
this.#stream?.log('write %d bytes', chunk.byteLength);
|
|
29
|
+
this.bytesWritten += chunk.byteLength;
|
|
30
|
+
this.#bytes?.write(chunk)
|
|
31
|
+
.then(() => {
|
|
32
|
+
cb();
|
|
33
|
+
}, err => {
|
|
34
|
+
cb(err);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
_read(size) {
|
|
38
|
+
this.#stream?.log('asked to read %d bytes', size);
|
|
39
|
+
void Promise.resolve().then(async () => {
|
|
40
|
+
try {
|
|
41
|
+
while (true) {
|
|
42
|
+
const chunk = await this.#bytes?.read({
|
|
43
|
+
signal: AbortSignal.timeout(this.timeout)
|
|
44
|
+
});
|
|
45
|
+
if (chunk == null) {
|
|
46
|
+
this.#stream?.log('socket readable end closed');
|
|
47
|
+
this.push(null);
|
|
48
|
+
return;
|
|
47
49
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
this.bytesRead += chunk.byteLength;
|
|
51
|
+
this.#stream?.log('socket read %d bytes', chunk.byteLength);
|
|
52
|
+
const more = this.push(chunk.subarray());
|
|
53
|
+
if (!more) {
|
|
54
|
+
break;
|
|
50
55
|
}
|
|
51
|
-
});
|
|
52
|
-
},
|
|
53
|
-
destroy: (err, cb) => {
|
|
54
|
-
this.stream.log('destroy with %d bytes buffered - %e', this.bufferSize, err);
|
|
55
|
-
if (err != null) {
|
|
56
|
-
bytes.unwrap().abort(err);
|
|
57
|
-
cb();
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
bytes.unwrap().close()
|
|
61
|
-
.then(() => {
|
|
62
|
-
cb();
|
|
63
|
-
})
|
|
64
|
-
.catch(err => {
|
|
65
|
-
stream.abort(err);
|
|
66
|
-
cb(err);
|
|
67
|
-
});
|
|
68
56
|
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
this.
|
|
72
|
-
bytes.unwrap().closeWrite()
|
|
73
|
-
.then(() => {
|
|
74
|
-
cb();
|
|
75
|
-
})
|
|
76
|
-
.catch(err => {
|
|
77
|
-
bytes.unwrap().abort(err);
|
|
78
|
-
cb(err);
|
|
79
|
-
});
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
this.destroy(err);
|
|
80
60
|
}
|
|
81
61
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
|
|
86
|
-
|
|
62
|
+
}
|
|
63
|
+
_destroy(err, cb) {
|
|
64
|
+
this.#stream?.log('destroy with %d bytes buffered - %e', this.bufferSize, err);
|
|
65
|
+
if (err != null) {
|
|
66
|
+
this.#bytes?.unwrap().abort(err);
|
|
67
|
+
cb();
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.#bytes?.unwrap().close()
|
|
71
|
+
.then(() => {
|
|
72
|
+
cb();
|
|
73
|
+
})
|
|
74
|
+
.catch(err => {
|
|
75
|
+
this.#stream?.abort(err);
|
|
76
|
+
cb(err);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
_final(cb) {
|
|
81
|
+
this.#stream?.log('final');
|
|
82
|
+
this.#bytes?.unwrap().closeWrite()
|
|
83
|
+
.then(() => {
|
|
84
|
+
cb();
|
|
85
|
+
})
|
|
86
|
+
.catch(err => {
|
|
87
|
+
this.#bytes?.unwrap().abort(err);
|
|
88
|
+
cb(err);
|
|
89
|
+
});
|
|
87
90
|
}
|
|
88
91
|
get readyState() {
|
|
89
|
-
if (this
|
|
92
|
+
if (this.#stream?.status === 'closed') {
|
|
90
93
|
return 'closed';
|
|
91
94
|
}
|
|
92
|
-
if (this
|
|
95
|
+
if (this.#stream?.writeStatus === 'closed' || this.#stream?.writeStatus === 'closing') {
|
|
93
96
|
return 'readOnly';
|
|
94
97
|
}
|
|
95
|
-
if (this
|
|
98
|
+
if (this.#stream?.readStatus === 'closed' || this.#stream?.readStatus === 'closing') {
|
|
96
99
|
return 'writeOnly';
|
|
97
100
|
}
|
|
98
101
|
return 'open';
|
|
@@ -101,24 +104,24 @@ class Libp2pSocket extends Duplex {
|
|
|
101
104
|
return this.writableLength;
|
|
102
105
|
}
|
|
103
106
|
destroySoon() {
|
|
104
|
-
this
|
|
107
|
+
this.#stream?.log('destroySoon with %d bytes buffered', this.bufferSize);
|
|
105
108
|
this.destroy();
|
|
106
109
|
}
|
|
107
110
|
connect(...args) {
|
|
108
|
-
this
|
|
111
|
+
this.#stream?.log('connect %o', args);
|
|
109
112
|
return this;
|
|
110
113
|
}
|
|
111
114
|
setEncoding(encoding) {
|
|
112
|
-
this
|
|
115
|
+
this.#stream?.log('setEncoding %s', encoding);
|
|
113
116
|
return this;
|
|
114
117
|
}
|
|
115
118
|
resetAndDestroy() {
|
|
116
|
-
this
|
|
117
|
-
this
|
|
119
|
+
this.#stream?.log('resetAndDestroy');
|
|
120
|
+
this.#stream?.abort(new Error('Libp2pSocket.resetAndDestroy'));
|
|
118
121
|
return this;
|
|
119
122
|
}
|
|
120
123
|
setTimeout(timeout, callback) {
|
|
121
|
-
this
|
|
124
|
+
this.#stream?.log('setTimeout %d', timeout);
|
|
122
125
|
if (callback != null) {
|
|
123
126
|
this.addListener('timeout', callback);
|
|
124
127
|
}
|
|
@@ -126,23 +129,23 @@ class Libp2pSocket extends Duplex {
|
|
|
126
129
|
return this;
|
|
127
130
|
}
|
|
128
131
|
setNoDelay(noDelay) {
|
|
129
|
-
this
|
|
132
|
+
this.#stream?.log('setNoDelay %b', noDelay);
|
|
130
133
|
return this;
|
|
131
134
|
}
|
|
132
135
|
setKeepAlive(enable, initialDelay) {
|
|
133
|
-
this
|
|
136
|
+
this.#stream?.log('setKeepAlive %b %d', enable, initialDelay);
|
|
134
137
|
return this;
|
|
135
138
|
}
|
|
136
139
|
address() {
|
|
137
|
-
this
|
|
140
|
+
this.#stream?.log('address');
|
|
138
141
|
return {};
|
|
139
142
|
}
|
|
140
143
|
unref() {
|
|
141
|
-
this
|
|
144
|
+
this.#stream?.log('unref');
|
|
142
145
|
return this;
|
|
143
146
|
}
|
|
144
147
|
ref() {
|
|
145
|
-
this
|
|
148
|
+
this.#stream?.log('ref');
|
|
146
149
|
return this;
|
|
147
150
|
}
|
|
148
151
|
write(chunk, encoding, cb) {
|
|
@@ -150,6 +153,8 @@ class Libp2pSocket extends Duplex {
|
|
|
150
153
|
}
|
|
151
154
|
}
|
|
152
155
|
export function streamToSocket(stream, connection) {
|
|
153
|
-
|
|
156
|
+
const socket = new Libp2pSocket();
|
|
157
|
+
socket.setStream(stream, connection);
|
|
158
|
+
return socket;
|
|
154
159
|
}
|
|
155
160
|
//# sourceMappingURL=stream-to-socket.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-to-socket.js","sourceRoot":"","sources":["../../src/stream-to-socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"stream-to-socket.js","sourceRoot":"","sources":["../../src/stream-to-socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAK3C,MAAM,WAAW,GAAG,aAAa,CAAA;AAEjC,MAAM,OAAO,YAAa,SAAQ,MAAM;I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,SAAS,CAAC,CAAA;QAE5B,OAAO,EAAE,CAAA;IACX,CAAC;IAED,KAAK;QACH,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;QAE1B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,GAAG;QACD,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;QAExB,OAAO,IAAI,CAAA;IACb,CAAC;IAID,KAAK,CAAE,KAAU,EAAE,QAAc,EAAE,EAAQ;QACzC,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,CAAA;IACzC,CAAC;CACF;AAED,MAAM,UAAU,cAAc,CAAE,MAAc,EAAE,UAAsB;IACpE,MAAM,MAAM,GAAG,IAAI,YAAY,EAAE,CAAA;IACjC,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IAEpC,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/dist/typedoc-urls.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
".:Middleware": "https://libp2p.github.io/js-libp2p-http/interfaces/_libp2p_http-utils.Middleware.html",
|
|
4
4
|
"MiddlewareOptions": "https://libp2p.github.io/js-libp2p-http/interfaces/_libp2p_http.index.MiddlewareOptions.html",
|
|
5
5
|
".:MiddlewareOptions": "https://libp2p.github.io/js-libp2p-http/interfaces/_libp2p_http-utils.MiddlewareOptions.html",
|
|
6
|
+
"Libp2pSocket": "https://libp2p.github.io/js-libp2p-http/classes/_libp2p_http-utils.Libp2pSocket.html",
|
|
6
7
|
"Request": "https://libp2p.github.io/js-libp2p-http/classes/_libp2p_http-utils.Request.html",
|
|
7
8
|
"Response": "https://libp2p.github.io/js-libp2p-http/classes/_libp2p_http-utils.Response.html",
|
|
8
9
|
"HeaderInfo": "https://libp2p.github.io/js-libp2p-http/interfaces/_libp2p_http-utils.HeaderInfo.html",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/http-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Shared utils and common code for HTTP modules",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-http/tree/main/packages/http-utils#readme",
|
package/src/stream-to-socket.ts
CHANGED
|
@@ -1,115 +1,123 @@
|
|
|
1
1
|
import { Duplex } from 'node:stream'
|
|
2
2
|
import { byteStream } from 'it-byte-stream'
|
|
3
3
|
import type { Connection, Stream } from '@libp2p/interface'
|
|
4
|
+
import type { ByteStream } from 'it-byte-stream'
|
|
4
5
|
import type { Socket, SocketConnectOpts, AddressInfo, SocketReadyState } from 'node:net'
|
|
5
6
|
|
|
6
7
|
const MAX_TIMEOUT = 2_147_483_647
|
|
7
8
|
|
|
8
|
-
class Libp2pSocket extends Duplex {
|
|
9
|
+
export class Libp2pSocket extends Duplex {
|
|
9
10
|
public readonly autoSelectFamilyAttemptedAddresses = []
|
|
10
11
|
public readonly connecting = false
|
|
11
12
|
public readonly pending = false
|
|
12
|
-
public
|
|
13
|
+
public remoteAddress: string
|
|
13
14
|
public bytesRead: number
|
|
14
15
|
public bytesWritten: number
|
|
15
16
|
public timeout = MAX_TIMEOUT
|
|
16
17
|
public allowHalfOpen: boolean
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
#stream?: Stream
|
|
20
|
+
#bytes?: ByteStream<Stream>
|
|
19
21
|
|
|
20
|
-
constructor (
|
|
21
|
-
|
|
22
|
+
constructor () {
|
|
23
|
+
super()
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
this.bytesRead = 0
|
|
26
|
+
this.bytesWritten = 0
|
|
27
|
+
this.allowHalfOpen = true
|
|
28
|
+
this.remoteAddress = ''
|
|
29
|
+
}
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
setStream (stream: Stream, connection: Connection): void {
|
|
32
|
+
this.#bytes = byteStream(stream)
|
|
33
|
+
this.#stream = stream
|
|
34
|
+
this.remoteAddress = connection.remoteAddr.toString()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_write (chunk: Uint8Array, encoding: string, cb: (err?: Error) => void): void {
|
|
38
|
+
this.#stream?.log('write %d bytes', chunk.byteLength)
|
|
39
|
+
|
|
40
|
+
this.bytesWritten += chunk.byteLength
|
|
41
|
+
this.#bytes?.write(chunk)
|
|
42
|
+
.then(() => {
|
|
43
|
+
cb()
|
|
44
|
+
}, err => {
|
|
45
|
+
cb(err)
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_read (size: number): void {
|
|
50
|
+
this.#stream?.log('asked to read %d bytes', size)
|
|
51
|
+
|
|
52
|
+
void Promise.resolve().then(async () => {
|
|
53
|
+
try {
|
|
54
|
+
while (true) {
|
|
55
|
+
const chunk = await this.#bytes?.read({
|
|
56
|
+
signal: AbortSignal.timeout(this.timeout)
|
|
33
57
|
})
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
while (true) {
|
|
41
|
-
const chunk = await bytes.read({
|
|
42
|
-
signal: AbortSignal.timeout(this.timeout)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
if (chunk == null) {
|
|
46
|
-
this.stream.log('socket readable end closed')
|
|
47
|
-
this.push(null)
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.bytesRead += chunk.byteLength
|
|
52
|
-
|
|
53
|
-
this.stream.log('socket read %d bytes', chunk.byteLength)
|
|
54
|
-
const more = this.push(chunk.subarray())
|
|
55
|
-
|
|
56
|
-
if (!more) {
|
|
57
|
-
break
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
} catch (err: any) {
|
|
61
|
-
this.destroy(err)
|
|
58
|
+
|
|
59
|
+
if (chunk == null) {
|
|
60
|
+
this.#stream?.log('socket readable end closed')
|
|
61
|
+
this.push(null)
|
|
62
|
+
return
|
|
62
63
|
}
|
|
63
|
-
})
|
|
64
|
-
},
|
|
65
|
-
destroy: (err, cb) => {
|
|
66
|
-
this.stream.log('destroy with %d bytes buffered - %e', this.bufferSize, err)
|
|
67
64
|
|
|
68
|
-
|
|
69
|
-
bytes.unwrap().abort(err)
|
|
70
|
-
cb()
|
|
71
|
-
} else {
|
|
72
|
-
bytes.unwrap().close()
|
|
73
|
-
.then(() => {
|
|
74
|
-
cb()
|
|
75
|
-
})
|
|
76
|
-
.catch(err => {
|
|
77
|
-
stream.abort(err)
|
|
78
|
-
cb(err)
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
final: (cb) => {
|
|
83
|
-
this.stream.log('final')
|
|
65
|
+
this.bytesRead += chunk.byteLength
|
|
84
66
|
|
|
85
|
-
|
|
86
|
-
.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
67
|
+
this.#stream?.log('socket read %d bytes', chunk.byteLength)
|
|
68
|
+
const more = this.push(chunk.subarray())
|
|
69
|
+
|
|
70
|
+
if (!more) {
|
|
71
|
+
break
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} catch (err: any) {
|
|
75
|
+
this.destroy(err)
|
|
93
76
|
}
|
|
94
77
|
})
|
|
78
|
+
}
|
|
95
79
|
|
|
96
|
-
|
|
97
|
-
this
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
80
|
+
_destroy (err: Error, cb: (err?: Error) => void): void {
|
|
81
|
+
this.#stream?.log('destroy with %d bytes buffered - %e', this.bufferSize, err)
|
|
82
|
+
|
|
83
|
+
if (err != null) {
|
|
84
|
+
this.#bytes?.unwrap().abort(err)
|
|
85
|
+
cb()
|
|
86
|
+
} else {
|
|
87
|
+
this.#bytes?.unwrap().close()
|
|
88
|
+
.then(() => {
|
|
89
|
+
cb()
|
|
90
|
+
})
|
|
91
|
+
.catch(err => {
|
|
92
|
+
this.#stream?.abort(err)
|
|
93
|
+
cb(err)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
_final (cb: (err?: Error) => void): void {
|
|
99
|
+
this.#stream?.log('final')
|
|
100
|
+
|
|
101
|
+
this.#bytes?.unwrap().closeWrite()
|
|
102
|
+
.then(() => {
|
|
103
|
+
cb()
|
|
104
|
+
})
|
|
105
|
+
.catch(err => {
|
|
106
|
+
this.#bytes?.unwrap().abort(err)
|
|
107
|
+
cb(err)
|
|
108
|
+
})
|
|
101
109
|
}
|
|
102
110
|
|
|
103
111
|
public get readyState (): SocketReadyState {
|
|
104
|
-
if (this
|
|
112
|
+
if (this.#stream?.status === 'closed') {
|
|
105
113
|
return 'closed'
|
|
106
114
|
}
|
|
107
115
|
|
|
108
|
-
if (this
|
|
116
|
+
if (this.#stream?.writeStatus === 'closed' || this.#stream?.writeStatus === 'closing') {
|
|
109
117
|
return 'readOnly'
|
|
110
118
|
}
|
|
111
119
|
|
|
112
|
-
if (this
|
|
120
|
+
if (this.#stream?.readStatus === 'closed' || this.#stream?.readStatus === 'closing') {
|
|
113
121
|
return 'writeOnly'
|
|
114
122
|
}
|
|
115
123
|
|
|
@@ -121,7 +129,7 @@ class Libp2pSocket extends Duplex {
|
|
|
121
129
|
}
|
|
122
130
|
|
|
123
131
|
destroySoon (): void {
|
|
124
|
-
this
|
|
132
|
+
this.#stream?.log('destroySoon with %d bytes buffered', this.bufferSize)
|
|
125
133
|
this.destroy()
|
|
126
134
|
}
|
|
127
135
|
|
|
@@ -130,24 +138,24 @@ class Libp2pSocket extends Duplex {
|
|
|
130
138
|
connect (port: number, connectionListener?: () => void): this
|
|
131
139
|
connect (path: string, connectionListener?: () => void): this
|
|
132
140
|
connect (...args: any[]): this {
|
|
133
|
-
this
|
|
141
|
+
this.#stream?.log('connect %o', args)
|
|
134
142
|
return this
|
|
135
143
|
}
|
|
136
144
|
|
|
137
145
|
setEncoding (encoding?: BufferEncoding): this {
|
|
138
|
-
this
|
|
146
|
+
this.#stream?.log('setEncoding %s', encoding)
|
|
139
147
|
return this
|
|
140
148
|
}
|
|
141
149
|
|
|
142
150
|
resetAndDestroy (): this {
|
|
143
|
-
this
|
|
144
|
-
this
|
|
151
|
+
this.#stream?.log('resetAndDestroy')
|
|
152
|
+
this.#stream?.abort(new Error('Libp2pSocket.resetAndDestroy'))
|
|
145
153
|
|
|
146
154
|
return this
|
|
147
155
|
}
|
|
148
156
|
|
|
149
157
|
setTimeout (timeout: number, callback?: () => void): this {
|
|
150
|
-
this
|
|
158
|
+
this.#stream?.log('setTimeout %d', timeout)
|
|
151
159
|
|
|
152
160
|
if (callback != null) {
|
|
153
161
|
this.addListener('timeout', callback)
|
|
@@ -159,31 +167,31 @@ class Libp2pSocket extends Duplex {
|
|
|
159
167
|
}
|
|
160
168
|
|
|
161
169
|
setNoDelay (noDelay?: boolean): this {
|
|
162
|
-
this
|
|
170
|
+
this.#stream?.log('setNoDelay %b', noDelay)
|
|
163
171
|
|
|
164
172
|
return this
|
|
165
173
|
}
|
|
166
174
|
|
|
167
175
|
setKeepAlive (enable?: boolean, initialDelay?: number): this {
|
|
168
|
-
this
|
|
176
|
+
this.#stream?.log('setKeepAlive %b %d', enable, initialDelay)
|
|
169
177
|
|
|
170
178
|
return this
|
|
171
179
|
}
|
|
172
180
|
|
|
173
181
|
address (): AddressInfo | Record<string, any> {
|
|
174
|
-
this
|
|
182
|
+
this.#stream?.log('address')
|
|
175
183
|
|
|
176
184
|
return {}
|
|
177
185
|
}
|
|
178
186
|
|
|
179
187
|
unref (): this {
|
|
180
|
-
this
|
|
188
|
+
this.#stream?.log('unref')
|
|
181
189
|
|
|
182
190
|
return this
|
|
183
191
|
}
|
|
184
192
|
|
|
185
193
|
ref (): this {
|
|
186
|
-
this
|
|
194
|
+
this.#stream?.log('ref')
|
|
187
195
|
|
|
188
196
|
return this
|
|
189
197
|
}
|
|
@@ -196,5 +204,8 @@ class Libp2pSocket extends Duplex {
|
|
|
196
204
|
}
|
|
197
205
|
|
|
198
206
|
export function streamToSocket (stream: Stream, connection: Connection): Socket {
|
|
199
|
-
|
|
207
|
+
const socket = new Libp2pSocket()
|
|
208
|
+
socket.setStream(stream, connection)
|
|
209
|
+
|
|
210
|
+
return socket
|
|
200
211
|
}
|