bl 6.1.6 → 7.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/.github/dependabot.yml +4 -0
- package/.github/workflows/test-and-release.yml +18 -21
- package/BufferList.js +399 -317
- package/BufferListStream.js +134 -0
- package/CHANGELOG.md +28 -0
- package/README.md +49 -63
- package/package.json +32 -18
- package/test/convert.js +3 -5
- package/test/indexOf.js +3 -5
- package/test/isBufferList.js +3 -5
- package/test/test.js +27 -27
- package/test/types.ts +89 -0
- package/test/verify-types.js +51 -0
- package/tsconfig.json +24 -0
- package/types/BufferList.d.ts +109 -0
- package/types/BufferList.d.ts.map +1 -0
- package/types/BufferListStream.d.ts +54 -0
- package/types/BufferListStream.d.ts.map +1 -0
- package/types/augment.d.ts +50 -0
- package/types/index.d.ts +4 -0
- package/BufferList.d.ts +0 -431
- package/bl.js +0 -84
- package/index.d.ts +0 -88
package/BufferList.js
CHANGED
|
@@ -1,421 +1,503 @@
|
|
|
1
|
-
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Buffer | BufferList | Uint8Array | Array<Buffer | BufferList | Uint8Array | string | number> | string | number} BufferListAcceptedTypes
|
|
5
|
+
*/
|
|
2
6
|
|
|
3
|
-
const { Buffer } = require('buffer')
|
|
4
7
|
const symbol = Symbol.for('BufferList')
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
export class BufferList {
|
|
10
|
+
/** @param {BufferListAcceptedTypes} [buf] */
|
|
11
|
+
constructor (buf) {
|
|
12
|
+
/** @type {Buffer[]} */
|
|
13
|
+
this._bufs = []
|
|
14
|
+
/** @type {number} */
|
|
15
|
+
this.length = 0
|
|
16
|
+
Object.defineProperty(this, symbol, { value: true })
|
|
17
|
+
if (buf) {
|
|
18
|
+
this.append(buf)
|
|
19
|
+
}
|
|
9
20
|
}
|
|
10
21
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
BufferList._init = function _init (buf) {
|
|
15
|
-
Object.defineProperty(this, symbol, { value: true })
|
|
16
|
-
|
|
17
|
-
this._bufs = []
|
|
18
|
-
this.length = 0
|
|
19
|
-
|
|
20
|
-
if (buf) {
|
|
21
|
-
this.append(buf)
|
|
22
|
+
/** @param {BufferListAcceptedTypes} [buf] */
|
|
23
|
+
_new (buf) {
|
|
24
|
+
return new BufferList(buf)
|
|
22
25
|
}
|
|
23
|
-
}
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
/**
|
|
28
|
+
* @param {number} offset
|
|
29
|
+
* @returns {[number, number]}
|
|
30
|
+
*/
|
|
31
|
+
_offset (offset) {
|
|
32
|
+
if (offset === 0 || this._bufs.length === 0) {
|
|
33
|
+
return [0, 0]
|
|
34
|
+
}
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
let tot = 0
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
for (let i = 0; i < this._bufs.length; i++) {
|
|
39
|
+
const _t = tot + this._bufs[i].length
|
|
40
|
+
if (offset < _t || i === this._bufs.length - 1) {
|
|
41
|
+
return [i, offset - tot]
|
|
42
|
+
}
|
|
43
|
+
tot = _t
|
|
40
44
|
}
|
|
41
|
-
tot = _t
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
BufferList.prototype._reverseOffset = function (blOffset) {
|
|
46
|
-
const bufferId = blOffset[0]
|
|
47
|
-
let offset = blOffset[1]
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
offset += this._bufs[i].length
|
|
46
|
+
return [0, 0]
|
|
51
47
|
}
|
|
52
48
|
|
|
53
|
-
|
|
54
|
-
}
|
|
49
|
+
/**
|
|
50
|
+
* @param {[number, number]} blOffset
|
|
51
|
+
* @returns {number}
|
|
52
|
+
*/
|
|
53
|
+
_reverseOffset (blOffset) {
|
|
54
|
+
const bufferId = blOffset[0]
|
|
55
|
+
let offset = blOffset[1]
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
57
|
+
for (let i = 0; i < bufferId; i++) {
|
|
58
|
+
offset += this._bufs[i].length
|
|
59
|
+
}
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
if (index > this.length || index < 0) {
|
|
62
|
-
return undefined
|
|
61
|
+
return offset
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
BufferList.prototype.slice = function slice (start, end) {
|
|
71
|
-
if (typeof start === 'number' && start < 0) {
|
|
72
|
-
start += this.length
|
|
64
|
+
/** @returns {Buffer[]} */
|
|
65
|
+
getBuffers () {
|
|
66
|
+
return this._bufs
|
|
73
67
|
}
|
|
74
68
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
69
|
+
/**
|
|
70
|
+
* @param {number} index
|
|
71
|
+
* @returns {number | undefined}
|
|
72
|
+
*/
|
|
73
|
+
get (index) {
|
|
74
|
+
if (index > this.length || index < 0) {
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
}
|
|
78
|
+
const offset = this._offset(index)
|
|
81
79
|
|
|
82
|
-
|
|
83
|
-
if (typeof srcStart !== 'number' || srcStart < 0) {
|
|
84
|
-
srcStart = 0
|
|
80
|
+
return this._bufs[offset[0]][offset[1]]
|
|
85
81
|
}
|
|
86
82
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
/**
|
|
84
|
+
* @param {number} [start]
|
|
85
|
+
* @param {number} [end]
|
|
86
|
+
* @returns {Buffer}
|
|
87
|
+
*/
|
|
88
|
+
slice (start, end) {
|
|
89
|
+
if (typeof start === 'number' && start < 0) {
|
|
90
|
+
start += this.length
|
|
91
|
+
}
|
|
90
92
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
if (typeof end === 'number' && end < 0) {
|
|
94
|
+
end += this.length
|
|
95
|
+
}
|
|
94
96
|
|
|
95
|
-
|
|
96
|
-
return dst || Buffer.alloc(0)
|
|
97
|
+
return this.copy(null, 0, start, end)
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// slice, but full concat if multiple buffers
|
|
110
|
-
return this._bufs.length === 1
|
|
111
|
-
? this._bufs[0]
|
|
112
|
-
: Buffer.concat(this._bufs, this.length)
|
|
100
|
+
/**
|
|
101
|
+
* @param {Buffer | null} dst
|
|
102
|
+
* @param {number} [dstStart]
|
|
103
|
+
* @param {number} [srcStart]
|
|
104
|
+
* @param {number} [srcEnd]
|
|
105
|
+
* @returns {Buffer}
|
|
106
|
+
*/
|
|
107
|
+
copy (dst, dstStart, srcStart, srcEnd) {
|
|
108
|
+
if (typeof srcStart !== 'number' || srcStart < 0) {
|
|
109
|
+
srcStart = 0
|
|
113
110
|
}
|
|
114
111
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this._bufs[i].copy(dst, bufoff)
|
|
118
|
-
bufoff += this._bufs[i].length
|
|
112
|
+
if (typeof srcEnd !== 'number' || srcEnd > this.length) {
|
|
113
|
+
srcEnd = this.length
|
|
119
114
|
}
|
|
120
115
|
|
|
121
|
-
|
|
122
|
-
|
|
116
|
+
if (srcStart >= this.length) {
|
|
117
|
+
return dst || Buffer.alloc(0)
|
|
118
|
+
}
|
|
123
119
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
|
|
128
|
-
: this._bufs[off[0]].slice(start, start + bytes)
|
|
129
|
-
}
|
|
120
|
+
if (srcEnd <= 0) {
|
|
121
|
+
return dst || Buffer.alloc(0)
|
|
122
|
+
}
|
|
130
123
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
124
|
+
const copy = !!dst
|
|
125
|
+
const off = this._offset(srcStart)
|
|
126
|
+
const len = srcEnd - srcStart
|
|
127
|
+
let bytes = len
|
|
128
|
+
let bufoff = (copy && dstStart) || 0
|
|
129
|
+
let start = off[1]
|
|
130
|
+
|
|
131
|
+
// copy/slice everything
|
|
132
|
+
if (srcStart === 0 && srcEnd === this.length) {
|
|
133
|
+
if (!copy) {
|
|
134
|
+
// slice, but full concat if multiple buffers
|
|
135
|
+
return this._bufs.length === 1
|
|
136
|
+
? this._bufs[0]
|
|
137
|
+
: Buffer.concat(/** @type {any} */ (this._bufs), this.length)
|
|
138
|
+
}
|
|
135
139
|
|
|
136
|
-
|
|
137
|
-
|
|
140
|
+
// copy, need to copy individual buffers
|
|
141
|
+
const d = /** @type {any} */ (dst)
|
|
142
|
+
for (let i = 0; i < this._bufs.length; i++) {
|
|
143
|
+
this._bufs[i].copy(d, bufoff)
|
|
144
|
+
bufoff += this._bufs[i].length
|
|
145
|
+
}
|
|
138
146
|
|
|
139
|
-
|
|
140
|
-
this._bufs[i].copy(dst, bufoff, start)
|
|
141
|
-
bufoff += l
|
|
142
|
-
} else {
|
|
143
|
-
this._bufs[i].copy(dst, bufoff, start, start + bytes)
|
|
144
|
-
bufoff += l
|
|
145
|
-
break
|
|
147
|
+
return d
|
|
146
148
|
}
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
// easy, cheap case where it's a subset of one of the buffers
|
|
151
|
+
if (bytes <= this._bufs[off[0]].length - start) {
|
|
152
|
+
if (!copy) {
|
|
153
|
+
return this._bufs[off[0]].slice(start, start + bytes)
|
|
154
|
+
}
|
|
155
|
+
this._bufs[off[0]].copy(/** @type {any} */ (dst), dstStart, start, start + bytes)
|
|
156
|
+
return /** @type {Buffer} */ (dst)
|
|
157
|
+
}
|
|
149
158
|
|
|
150
|
-
if (
|
|
151
|
-
|
|
159
|
+
if (!copy) {
|
|
160
|
+
// a slice, we need something to copy in to
|
|
161
|
+
dst = Buffer.allocUnsafe(len)
|
|
152
162
|
}
|
|
153
|
-
}
|
|
154
163
|
|
|
155
|
-
|
|
156
|
-
if (dst.length > bufoff) return dst.slice(0, bufoff)
|
|
164
|
+
const target = /** @type {any} */ (dst)
|
|
157
165
|
|
|
158
|
-
|
|
159
|
-
|
|
166
|
+
for (let i = off[0]; i < this._bufs.length; i++) {
|
|
167
|
+
const l = this._bufs[i].length - start
|
|
160
168
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
169
|
+
if (bytes > l) {
|
|
170
|
+
this._bufs[i].copy(target, bufoff, start)
|
|
171
|
+
bufoff += l
|
|
172
|
+
} else {
|
|
173
|
+
this._bufs[i].copy(target, bufoff, start, start + bytes)
|
|
174
|
+
bufoff += l
|
|
175
|
+
break
|
|
176
|
+
}
|
|
164
177
|
|
|
165
|
-
|
|
166
|
-
start += this.length
|
|
167
|
-
}
|
|
178
|
+
bytes -= l
|
|
168
179
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
180
|
+
if (start) {
|
|
181
|
+
start = 0
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// safeguard so that we don't return uninitialized memory
|
|
186
|
+
if (target.length > bufoff) return target.slice(0, bufoff)
|
|
172
187
|
|
|
173
|
-
|
|
174
|
-
return this._new()
|
|
188
|
+
return target
|
|
175
189
|
}
|
|
176
190
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
191
|
+
/**
|
|
192
|
+
* @param {number} [start]
|
|
193
|
+
* @param {number} [end]
|
|
194
|
+
* @returns {BufferList}
|
|
195
|
+
*/
|
|
196
|
+
shallowSlice (start, end) {
|
|
197
|
+
start = start || 0
|
|
198
|
+
end = typeof end !== 'number' ? this.length : end
|
|
180
199
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
|
|
185
|
-
}
|
|
200
|
+
if (start < 0) {
|
|
201
|
+
start += this.length
|
|
202
|
+
}
|
|
186
203
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
204
|
+
if (end < 0) {
|
|
205
|
+
end += this.length
|
|
206
|
+
}
|
|
190
207
|
|
|
191
|
-
|
|
192
|
-
|
|
208
|
+
if (start === end) {
|
|
209
|
+
return this._new()
|
|
210
|
+
}
|
|
193
211
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
212
|
+
const startOffset = this._offset(start)
|
|
213
|
+
const endOffset = this._offset(end)
|
|
214
|
+
const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
|
|
197
215
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
bytes = Math.trunc(bytes)
|
|
201
|
-
// do nothing if not a positive number
|
|
202
|
-
if (Number.isNaN(bytes) || bytes <= 0) return this
|
|
203
|
-
|
|
204
|
-
while (this._bufs.length) {
|
|
205
|
-
if (bytes >= this._bufs[0].length) {
|
|
206
|
-
bytes -= this._bufs[0].length
|
|
207
|
-
this.length -= this._bufs[0].length
|
|
208
|
-
this._bufs.shift()
|
|
216
|
+
if (endOffset[1] === 0) {
|
|
217
|
+
buffers.pop()
|
|
209
218
|
} else {
|
|
210
|
-
|
|
211
|
-
this.length -= bytes
|
|
212
|
-
break
|
|
219
|
+
buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
|
|
213
220
|
}
|
|
214
|
-
}
|
|
215
221
|
|
|
216
|
-
|
|
217
|
-
|
|
222
|
+
if (startOffset[1] !== 0) {
|
|
223
|
+
buffers[0] = buffers[0].slice(startOffset[1])
|
|
224
|
+
}
|
|
218
225
|
|
|
219
|
-
|
|
220
|
-
|
|
226
|
+
return this._new(buffers)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* @param {BufferEncoding} [encoding]
|
|
231
|
+
* @param {number} [start]
|
|
232
|
+
* @param {number} [end]
|
|
233
|
+
* @returns {string}
|
|
234
|
+
*/
|
|
235
|
+
toString (encoding, start, end) {
|
|
236
|
+
return this.slice(start, end).toString(encoding)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* @param {number} bytes
|
|
241
|
+
* @returns {this}
|
|
242
|
+
*/
|
|
243
|
+
consume (bytes) {
|
|
244
|
+
// first, normalize the argument, in accordance with how Buffer does it
|
|
245
|
+
bytes = Math.trunc(bytes)
|
|
246
|
+
// do nothing if not a positive number
|
|
247
|
+
if (Number.isNaN(bytes) || bytes <= 0) return this
|
|
248
|
+
|
|
249
|
+
while (this._bufs.length) {
|
|
250
|
+
if (bytes >= this._bufs[0].length) {
|
|
251
|
+
bytes -= this._bufs[0].length
|
|
252
|
+
this.length -= this._bufs[0].length
|
|
253
|
+
this._bufs.shift()
|
|
254
|
+
} else {
|
|
255
|
+
this._bufs[0] = this._bufs[0].slice(bytes)
|
|
256
|
+
this.length -= bytes
|
|
257
|
+
break
|
|
258
|
+
}
|
|
259
|
+
}
|
|
221
260
|
|
|
222
|
-
|
|
223
|
-
copy.append(this._bufs[i])
|
|
261
|
+
return this
|
|
224
262
|
}
|
|
225
263
|
|
|
226
|
-
|
|
227
|
-
|
|
264
|
+
/** @returns {BufferList} */
|
|
265
|
+
duplicate () {
|
|
266
|
+
const copy = this._new()
|
|
228
267
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
268
|
+
for (let i = 0; i < this._bufs.length; i++) {
|
|
269
|
+
copy.append(this._bufs[i])
|
|
270
|
+
}
|
|
232
271
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
272
|
+
return copy
|
|
273
|
+
}
|
|
236
274
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
275
|
+
/**
|
|
276
|
+
* @param {BufferListAcceptedTypes} buf
|
|
277
|
+
* @returns {this}
|
|
278
|
+
*/
|
|
279
|
+
append (buf) {
|
|
280
|
+
return this._attach(buf, BufferList.prototype._appendBuffer)
|
|
240
281
|
}
|
|
241
282
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
283
|
+
/**
|
|
284
|
+
* @param {BufferListAcceptedTypes} buf
|
|
285
|
+
* @returns {this}
|
|
286
|
+
*/
|
|
287
|
+
prepend (buf) {
|
|
288
|
+
return this._attach(buf, BufferList.prototype._prependBuffer, true)
|
|
289
|
+
}
|
|
247
290
|
|
|
248
|
-
|
|
249
|
-
|
|
291
|
+
/**
|
|
292
|
+
* @param {BufferListAcceptedTypes} buf
|
|
293
|
+
* @param {(this: BufferList, buf: Buffer) => void} attacher
|
|
294
|
+
* @param {boolean} [prepend]
|
|
295
|
+
* @returns {this}
|
|
296
|
+
*/
|
|
297
|
+
_attach (buf, attacher, prepend) {
|
|
298
|
+
if (buf == null) {
|
|
299
|
+
return this
|
|
250
300
|
}
|
|
251
|
-
} else if (this._isBufferList(buf)) {
|
|
252
|
-
// unwrap argument into individual BufferLists
|
|
253
|
-
const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1]
|
|
254
301
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
if (typeof buf === 'number') {
|
|
262
|
-
buf = buf.toString()
|
|
263
|
-
}
|
|
302
|
+
if (typeof buf === 'object' && 'buffer' in buf) {
|
|
303
|
+
// append/prepend a view of the underlying ArrayBuffer
|
|
304
|
+
const view = /** @type {Uint8Array} */ (buf)
|
|
305
|
+
attacher.call(this, Buffer.from(view.buffer, view.byteOffset, view.byteLength))
|
|
306
|
+
} else if (Array.isArray(buf)) {
|
|
307
|
+
const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1]
|
|
264
308
|
|
|
265
|
-
|
|
266
|
-
|
|
309
|
+
for (let i = starting; i >= 0 && i < buf.length; i += modifier) {
|
|
310
|
+
this._attach(buf[i], attacher, prepend)
|
|
311
|
+
}
|
|
312
|
+
} else if (this._isBufferList(buf)) {
|
|
313
|
+
// unwrap argument into individual BufferLists
|
|
314
|
+
const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1]
|
|
267
315
|
|
|
268
|
-
|
|
269
|
-
|
|
316
|
+
for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) {
|
|
317
|
+
this._attach(buf._bufs[i], attacher, prepend)
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
// coerce number arguments to strings, since Buffer(number) does
|
|
321
|
+
// uninitialized memory allocation
|
|
322
|
+
if (typeof buf === 'number') {
|
|
323
|
+
buf = buf.toString()
|
|
324
|
+
}
|
|
270
325
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
this.length += buf.length
|
|
274
|
-
}
|
|
326
|
+
attacher.call(this, Buffer.from(/** @type {string} */ (buf)))
|
|
327
|
+
}
|
|
275
328
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
this.length += buf.length
|
|
279
|
-
}
|
|
329
|
+
return this
|
|
330
|
+
}
|
|
280
331
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
332
|
+
/** @param {Buffer} buf */
|
|
333
|
+
_appendBuffer (buf) {
|
|
334
|
+
this._bufs.push(buf)
|
|
335
|
+
this.length += buf.length
|
|
285
336
|
}
|
|
286
337
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
} else if (typeof search === 'string') {
|
|
292
|
-
search = Buffer.from(search, encoding)
|
|
293
|
-
} else if (this._isBufferList(search)) {
|
|
294
|
-
search = search.slice()
|
|
295
|
-
} else if (Array.isArray(search.buffer)) {
|
|
296
|
-
search = Buffer.from(search.buffer, search.byteOffset, search.byteLength)
|
|
297
|
-
} else if (!Buffer.isBuffer(search)) {
|
|
298
|
-
search = Buffer.from(search)
|
|
338
|
+
/** @param {Buffer} buf */
|
|
339
|
+
_prependBuffer (buf) {
|
|
340
|
+
this._bufs.unshift(buf)
|
|
341
|
+
this.length += buf.length
|
|
299
342
|
}
|
|
300
343
|
|
|
301
|
-
|
|
344
|
+
/**
|
|
345
|
+
* @param {string | number | Buffer | BufferList | Uint8Array} search
|
|
346
|
+
* @param {number | string} [offset]
|
|
347
|
+
* @param {BufferEncoding} [encoding]
|
|
348
|
+
* @returns {number}
|
|
349
|
+
*/
|
|
350
|
+
indexOf (search, offset, encoding) {
|
|
351
|
+
if (encoding === undefined && typeof offset === 'string') {
|
|
352
|
+
encoding = /** @type {BufferEncoding} */ (offset)
|
|
353
|
+
offset = undefined
|
|
354
|
+
}
|
|
302
355
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
356
|
+
if (typeof search === 'function' || Array.isArray(search)) {
|
|
357
|
+
throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
|
|
358
|
+
} else if (typeof search === 'number') {
|
|
359
|
+
search = Buffer.from([search])
|
|
360
|
+
} else if (typeof search === 'string') {
|
|
361
|
+
search = Buffer.from(search, encoding)
|
|
362
|
+
} else if (this._isBufferList(search)) {
|
|
363
|
+
search = search.slice()
|
|
364
|
+
} else if (Array.isArray(/** @type {any} */ (search).buffer)) {
|
|
365
|
+
search = Buffer.from(/** @type {any} */ (search).buffer, /** @type {any} */ (search).byteOffset, /** @type {any} */ (search).byteLength)
|
|
366
|
+
} else if (!Buffer.isBuffer(search)) {
|
|
367
|
+
search = Buffer.from(/** @type {any} */ (search))
|
|
368
|
+
}
|
|
306
369
|
|
|
307
|
-
|
|
308
|
-
offset = this.length + offset
|
|
309
|
-
}
|
|
370
|
+
offset = Number(offset || 0)
|
|
310
371
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
372
|
+
if (isNaN(offset)) {
|
|
373
|
+
offset = 0
|
|
374
|
+
}
|
|
314
375
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
376
|
+
if (offset < 0) {
|
|
377
|
+
offset = this.length + offset
|
|
378
|
+
}
|
|
318
379
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
380
|
+
if (offset < 0) {
|
|
381
|
+
offset = 0
|
|
382
|
+
}
|
|
322
383
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
384
|
+
if (search.length === 0) {
|
|
385
|
+
return offset > this.length ? this.length : offset
|
|
386
|
+
}
|
|
326
387
|
|
|
327
|
-
|
|
328
|
-
|
|
388
|
+
const blOffset = this._offset(offset)
|
|
389
|
+
let blIndex = blOffset[0] // index of which internal buffer we're working on
|
|
390
|
+
let buffOffset = blOffset[1] // offset of the internal buffer we're working on
|
|
329
391
|
|
|
330
|
-
|
|
331
|
-
|
|
392
|
+
// scan over each buffer
|
|
393
|
+
for (; blIndex < this._bufs.length; blIndex++) {
|
|
394
|
+
const buff = this._bufs[blIndex]
|
|
332
395
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
396
|
+
while (buffOffset < buff.length) {
|
|
397
|
+
const availableWindow = buff.length - buffOffset
|
|
336
398
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const revOffset = this._reverseOffset([blIndex, buffOffset])
|
|
399
|
+
if (availableWindow >= search.length) {
|
|
400
|
+
const nativeSearchResult = buff.indexOf(/** @type {any} */ (search), buffOffset)
|
|
340
401
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
402
|
+
if (nativeSearchResult !== -1) {
|
|
403
|
+
return this._reverseOffset([blIndex, nativeSearchResult])
|
|
404
|
+
}
|
|
344
405
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
406
|
+
buffOffset = buff.length - search.length + 1 // end of native search window
|
|
407
|
+
} else {
|
|
408
|
+
const revOffset = this._reverseOffset([blIndex, buffOffset])
|
|
348
409
|
|
|
349
|
-
|
|
350
|
-
|
|
410
|
+
if (this._match(revOffset, search)) {
|
|
411
|
+
return revOffset
|
|
412
|
+
}
|
|
351
413
|
|
|
352
|
-
|
|
353
|
-
}
|
|
414
|
+
buffOffset++
|
|
415
|
+
}
|
|
416
|
+
}
|
|
354
417
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
418
|
+
buffOffset = 0
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return -1
|
|
358
422
|
}
|
|
359
423
|
|
|
360
|
-
|
|
361
|
-
|
|
424
|
+
/**
|
|
425
|
+
* @param {number} offset
|
|
426
|
+
* @param {Buffer | Uint8Array} search
|
|
427
|
+
* @returns {boolean}
|
|
428
|
+
*/
|
|
429
|
+
_match (offset, search) {
|
|
430
|
+
if (this.length - offset < search.length) {
|
|
362
431
|
return false
|
|
363
432
|
}
|
|
433
|
+
|
|
434
|
+
for (let searchOffset = 0; searchOffset < search.length; searchOffset++) {
|
|
435
|
+
if (this.get(offset + searchOffset) !== search[searchOffset]) {
|
|
436
|
+
return false
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return true
|
|
364
440
|
}
|
|
365
|
-
return true
|
|
366
|
-
}
|
|
367
441
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
readInt32BE: 4,
|
|
379
|
-
readInt32LE: 4,
|
|
380
|
-
readUInt32BE: 4,
|
|
381
|
-
readUInt32LE: 4,
|
|
382
|
-
readInt16BE: 2,
|
|
383
|
-
readInt16LE: 2,
|
|
384
|
-
readUInt16BE: 2,
|
|
385
|
-
readUInt16LE: 2,
|
|
386
|
-
readInt8: 1,
|
|
387
|
-
readUInt8: 1,
|
|
388
|
-
readIntBE: null,
|
|
389
|
-
readIntLE: null,
|
|
390
|
-
readUIntBE: null,
|
|
391
|
-
readUIntLE: null
|
|
442
|
+
// Used internally by the class and also as an indicator of this object being
|
|
443
|
+
// a `BufferList`. It's not possible to use `instanceof BufferList` in a browser
|
|
444
|
+
// environment because there could be multiple different copies of the
|
|
445
|
+
// BufferList class and some `BufferList`s might be `BufferList`s.
|
|
446
|
+
/**
|
|
447
|
+
* @param {any} b
|
|
448
|
+
* @returns {b is BufferList}
|
|
449
|
+
*/
|
|
450
|
+
_isBufferList (b) {
|
|
451
|
+
return b instanceof BufferList || BufferList.isBufferList(b)
|
|
392
452
|
}
|
|
393
453
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
} else {
|
|
401
|
-
BufferList.prototype[m] = function (offset = 0) {
|
|
402
|
-
return this.slice(offset, offset + methods[m])[m](0)
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
}(m))
|
|
454
|
+
/**
|
|
455
|
+
* @param {any} b
|
|
456
|
+
* @returns {boolean}
|
|
457
|
+
*/
|
|
458
|
+
static isBufferList (b) {
|
|
459
|
+
return b != null && b[symbol]
|
|
406
460
|
}
|
|
407
|
-
}())
|
|
408
|
-
|
|
409
|
-
// Used internally by the class and also as an indicator of this object being
|
|
410
|
-
// a `BufferList`. It's not possible to use `instanceof BufferList` in a browser
|
|
411
|
-
// environment because there could be multiple different copies of the
|
|
412
|
-
// BufferList class and some `BufferList`s might be `BufferList`s.
|
|
413
|
-
BufferList.prototype._isBufferList = function _isBufferList (b) {
|
|
414
|
-
return b instanceof BufferList || BufferList.isBufferList(b)
|
|
415
461
|
}
|
|
416
462
|
|
|
417
|
-
|
|
418
|
-
|
|
463
|
+
// Add all the Buffer read methods
|
|
464
|
+
/** @type {Record<string, number | null>} */
|
|
465
|
+
const methods = {
|
|
466
|
+
readDoubleBE: 8,
|
|
467
|
+
readDoubleLE: 8,
|
|
468
|
+
readFloatBE: 4,
|
|
469
|
+
readFloatLE: 4,
|
|
470
|
+
readBigInt64BE: 8,
|
|
471
|
+
readBigInt64LE: 8,
|
|
472
|
+
readBigUInt64BE: 8,
|
|
473
|
+
readBigUInt64LE: 8,
|
|
474
|
+
readInt32BE: 4,
|
|
475
|
+
readInt32LE: 4,
|
|
476
|
+
readUInt32BE: 4,
|
|
477
|
+
readUInt32LE: 4,
|
|
478
|
+
readInt16BE: 2,
|
|
479
|
+
readInt16LE: 2,
|
|
480
|
+
readUInt16BE: 2,
|
|
481
|
+
readUInt16LE: 2,
|
|
482
|
+
readInt8: 1,
|
|
483
|
+
readUInt8: 1,
|
|
484
|
+
readIntBE: null,
|
|
485
|
+
readIntLE: null,
|
|
486
|
+
readUIntBE: null,
|
|
487
|
+
readUIntLE: null
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
for (const m in methods) {
|
|
491
|
+
const size = methods[m]
|
|
492
|
+
if (size === null) {
|
|
493
|
+
/** @type {any} */ (BufferList.prototype)[m] = function (/** @type {number} */ offset, /** @type {number} */ byteLength) {
|
|
494
|
+
return /** @type {any} */ (this.slice(offset, offset + byteLength))[m](0, byteLength)
|
|
495
|
+
}
|
|
496
|
+
} else {
|
|
497
|
+
/** @type {any} */ (BufferList.prototype)[m] = function (/** @type {number} */ offset = 0) {
|
|
498
|
+
return /** @type {any} */ (this.slice(offset, offset + size))[m](0)
|
|
499
|
+
}
|
|
500
|
+
}
|
|
419
501
|
}
|
|
420
502
|
|
|
421
|
-
|
|
503
|
+
export default BufferList
|