@libp2p/http-utils 1.0.0 → 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 +17 -17
- package/dist/index.min.js.map +4 -4
- package/dist/src/constants.d.ts +0 -7
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +0 -12
- package/dist/src/constants.js.map +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +11 -10
- package/dist/src/index.js.map +1 -1
- 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 -7
- package/package.json +1 -1
- package/src/constants.ts +0 -12
- package/src/index.ts +13 -11
- package/src/stream-to-socket.ts +101 -90
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
|
}
|