-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathBufferListStream.js
More file actions
134 lines (113 loc) · 3 KB
/
Copy pathBufferListStream.js
File metadata and controls
134 lines (113 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Duplex } from 'node:stream'
import { BufferList } from './BufferList.js'
const symbol = Symbol.for('BufferList')
/**
* @typedef {import('./BufferList.js').BufferListAcceptedTypes} BufferListAcceptedTypes
*/
export class BufferListStream extends Duplex {
/**
* @param {((err: Error | null, buffer?: Buffer) => void) | BufferListAcceptedTypes} [callback]
*/
constructor (callback) {
super()
if (typeof callback === 'function') {
this._callback = callback
/** @param {Error} err */
const piper = (err) => {
if (this._callback) {
this._callback(err)
this._callback = undefined
}
}
this.on('pipe', (src) => {
src.on('error', piper)
})
this.on('unpipe', (src) => {
src.removeListener('error', piper)
})
callback = undefined
}
Object.defineProperty(this, symbol, { value: true })
/** @type {Buffer[]} */
this._bufs = []
/** @type {number} */
this.length = 0
if (callback) {
/** @type {any} */ (this).append(callback)
}
}
/**
* @param {any} buf
* @param {string} encoding
* @param {Function} callback
*/
_write (buf, encoding, callback) {
/** @type {any} */ (this)._appendBuffer(buf)
if (typeof callback === 'function') {
callback()
}
}
/** @param {number} size */
_read (size) {
if (!this.length) {
return this.push(null)
}
const self = /** @type {any} */ (this)
size = Math.min(size, this.length)
this.push(self.slice(0, size))
self.consume(size)
}
/**
* @param {any} [chunk]
* @param {any} [encoding]
* @param {any} [cb]
* @returns {this}
*/
end (chunk, encoding, cb) {
Duplex.prototype.end.call(this, chunk, encoding, cb)
if (this._callback) {
this._callback(null, /** @type {any} */ (this).slice())
this._callback = undefined
}
return this
}
/**
* @param {Error | null} err
* @param {Function} cb
*/
_destroy (err, cb) {
this._bufs.length = 0
this.length = 0
cb(err)
}
/** @param {any} [callback] */
_new (callback) {
return new BufferListStream(callback)
}
/**
* @param {any} b
* @returns {b is BufferList}
*/
_isBufferList (b) {
return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b)
}
/**
* @param {any} b
* @returns {boolean}
*/
static isBufferList (b) {
return BufferList.isBufferList(b)
}
}
// Copy BufferList prototype methods to BufferListStream prototype
for (const key of Object.getOwnPropertyNames(BufferList.prototype)) {
if (key === 'constructor' || key === '_new' || key === '_isBufferList') continue
const descriptor = Object.getOwnPropertyDescriptor(BufferList.prototype, key)
if (descriptor) {
Object.defineProperty(BufferListStream.prototype, key, descriptor)
}
}
export default BufferListStream
export { BufferList }
/** @type {(b: any) => boolean} */
export const isBufferList = BufferList.isBufferList