@libp2p/utils 6.7.2-da78fa851 → 6.7.2-e8398d97e

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,64 +0,0 @@
1
- import stream from 'node:stream'
2
- import { Uint8ArrayList } from 'uint8arraylist'
3
-
4
- export interface SocketWriter {
5
- /**
6
- * Write any available data into the socket, if the socket's internal write
7
- * buffer has available capacity
8
- */
9
- pull (): boolean
10
-
11
- /**
12
- * Write data into the socket, returns false if the socket's internal write
13
- * buffer is at capacity
14
- */
15
- write (data: Uint8Array | Uint8Array[] | Uint8ArrayList): boolean
16
- }
17
-
18
- /**
19
- * @deprecated delete if unused
20
- */
21
- export function socketWriter (socket: stream.Duplex): SocketWriter {
22
- const queue = new Uint8ArrayList()
23
-
24
- return {
25
- pull (): boolean {
26
- if (socket.writableNeedDrain) {
27
- return false
28
- }
29
-
30
- for (const buf of queue) {
31
- queue.consume(buf.byteLength)
32
-
33
- if (!socket.write(buf)) {
34
- // continue writing after drain event. this is a synchronous operation
35
- // so it will not interleave with the `this.writeToSocket()`
36
- // invocation in this.sendData so all data will be sent in-order
37
- if (queue.byteLength > 0) {
38
- socket.once('drain', () => {
39
- this.pull()
40
- })
41
- }
42
-
43
- return false
44
- }
45
- }
46
-
47
- return true
48
- },
49
-
50
- write (data: Uint8Array | Uint8Array[] | Uint8ArrayList): boolean {
51
- if (Array.isArray(data)) {
52
- queue.appendAll(data)
53
- } else {
54
- queue.append(data)
55
- }
56
-
57
- if (socket.writableNeedDrain) {
58
- return false
59
- }
60
-
61
- return this.pull()
62
- }
63
- }
64
- }