Skip to content

Commit 433ff89

Browse files
feat: added integrated TypeScript typings (#108)
* Added integrated typescript types * Added @types/readable-stream as devDependency * Fixed default export of BufferListStream on require * Renamed BufeerList import, removed BufferList all-export, removed export on BufferListStream * Fixed issue with module.exports equivalent: removed es6 default export and moved BufferList and BufferListStream to be BufferListStreamConstructor (static) properties * Fixed types of Buffer-inherited methods (readDouble, readFloat, ...) * Fixed BufferListInitData (renamed as BufferListAcceptedTypes) to accept Uint8Array and an array of itself * Added number to BufferList accepted types * Setup typescript test running on unit tests * Test: renamed BufferList import into BufferListStream (as the default exported class) * Changed tests to add typings with JSDoc / TSDoc * Added ts-check directive for typescript typechecking in editors * Added prototype declaration on BufferList and BufferListStream * Renamed test:typings in test:types * Removed npx from test for types * Changed version selector for typescript to accept patches only * Added missing semi-colon (auto format) Co-authored-by: Rod Vagg <rod@vagg.org>
1 parent 078bfe3 commit 433ff89

File tree

4 files changed

+599
-82
lines changed

4 files changed

+599
-82
lines changed

BufferList.d.ts

+382
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
export type BufferListAcceptedTypes =
2+
| Buffer
3+
| BufferList
4+
| Uint8Array
5+
| BufferListAcceptedTypes[]
6+
| string
7+
| number;
8+
9+
export interface BufferListConstructor {
10+
new (initData?: BufferListAcceptedTypes): BufferList;
11+
(initData?: BufferListAcceptedTypes): BufferList;
12+
13+
/**
14+
* Determines if the passed object is a BufferList. It will return true
15+
* if the passed object is an instance of BufferList or BufferListStream
16+
* and false otherwise.
17+
*
18+
* N.B. this won't return true for BufferList or BufferListStream instances
19+
* created by versions of this library before this static method was added.
20+
*
21+
* @param other
22+
*/
23+
24+
isBufferList(other: unknown): boolean;
25+
}
26+
27+
interface BufferList {
28+
prototype: Object
29+
30+
/**
31+
* Get the length of the list in bytes. This is the sum of the lengths
32+
* of all of the buffers contained in the list, minus any initial offset
33+
* for a semi-consumed buffer at the beginning. Should accurately
34+
* represent the total number of bytes that can be read from the list.
35+
*/
36+
37+
length: number;
38+
39+
/**
40+
* Adds an additional buffer or BufferList to the internal list.
41+
* this is returned so it can be chained.
42+
*
43+
* @param buffer
44+
*/
45+
46+
append(buffer: BufferListAcceptedTypes): this;
47+
48+
/**
49+
* Will return the byte at the specified index.
50+
* @param index
51+
*/
52+
53+
get(index: number): number;
54+
55+
/**
56+
* Returns a new Buffer object containing the bytes within the
57+
* range specified. Both start and end are optional and will
58+
* default to the beginning and end of the list respectively.
59+
*
60+
* If the requested range spans a single internal buffer then a
61+
* slice of that buffer will be returned which shares the original
62+
* memory range of that Buffer. If the range spans multiple buffers
63+
* then copy operations will likely occur to give you a uniform Buffer.
64+
*
65+
* @param start
66+
* @param end
67+
*/
68+
69+
slice(start?: number, end?: number): Buffer;
70+
71+
/**
72+
* Returns a new BufferList object containing the bytes within the
73+
* range specified. Both start and end are optional and will default
74+
* to the beginning and end of the list respectively.
75+
*
76+
* No copies will be performed. All buffers in the result share
77+
* memory with the original list.
78+
*
79+
* @param start
80+
* @param end
81+
*/
82+
83+
shallowSlice(start?: number, end?: number): this;
84+
85+
/**
86+
* Copies the content of the list in the `dest` buffer, starting from
87+
* `destStart` and containing the bytes within the range specified
88+
* with `srcStart` to `srcEnd`.
89+
*
90+
* `destStart`, `start` and `end` are optional and will default to the
91+
* beginning of the dest buffer, and the beginning and end of the
92+
* list respectively.
93+
*
94+
* @param dest
95+
* @param destStart
96+
* @param srcStart
97+
* @param srcEnd
98+
*/
99+
100+
copy(
101+
dest: Buffer,
102+
destStart?: number,
103+
srcStart?: number,
104+
srcEnd?: number
105+
): Buffer;
106+
107+
/**
108+
* Performs a shallow-copy of the list. The internal Buffers remains the
109+
* same, so if you change the underlying Buffers, the change will be
110+
* reflected in both the original and the duplicate.
111+
*
112+
* This method is needed if you want to call consume() or pipe() and
113+
* still keep the original list.
114+
*
115+
* @example
116+
*
117+
* ```js
118+
* var bl = new BufferListStream();
119+
* bl.append('hello');
120+
* bl.append(' world');
121+
* bl.append('\n');
122+
* bl.duplicate().pipe(process.stdout, { end: false });
123+
*
124+
* console.log(bl.toString())
125+
* ```
126+
*/
127+
128+
duplicate(): this;
129+
130+
/**
131+
* Will shift bytes off the start of the list. The number of bytes
132+
* consumed don't need to line up with the sizes of the internal
133+
* Buffers—initial offsets will be calculated accordingly in order
134+
* to give you a consistent view of the data.
135+
*
136+
* @param bytes
137+
*/
138+
139+
consume(bytes?: number): void;
140+
141+
/**
142+
* Will return a string representation of the buffer. The optional
143+
* `start` and `end` arguments are passed on to `slice()`, while
144+
* the encoding is passed on to `toString()` of the resulting Buffer.
145+
*
146+
* See the [`Buffer#toString()`](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end)
147+
* documentation for more information.
148+
*
149+
* @param encoding
150+
* @param start
151+
* @param end
152+
*/
153+
154+
toString(encoding?: string, start?: number, end?: number): string;
155+
156+
/**
157+
* Will return the byte at the specified index. indexOf() method
158+
* returns the first index at which a given element can be found
159+
* in the BufferList, or -1 if it is not present.
160+
*
161+
* @param value
162+
* @param byteOffset
163+
* @param encoding
164+
*/
165+
166+
indexOf(
167+
value: string | number | Uint8Array | BufferList | Buffer,
168+
byteOffset?: number,
169+
encoding?: string
170+
): number;
171+
172+
/**
173+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
174+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
175+
*
176+
* @param offset
177+
*/
178+
179+
readDoubleBE: Buffer['readDoubleBE'];
180+
181+
/**
182+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
183+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
184+
*
185+
* @param offset
186+
*/
187+
188+
readDoubleLE: Buffer['readDoubleLE'];
189+
190+
/**
191+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
192+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
193+
*
194+
* @param offset
195+
*/
196+
197+
readFloatBE: Buffer['readFloatBE'];
198+
199+
/**
200+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
201+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
202+
*
203+
* @param offset
204+
*/
205+
206+
readFloatLE: Buffer['readFloatLE'];
207+
208+
/**
209+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
210+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
211+
*
212+
* @param offset
213+
*/
214+
215+
readInt32BE: Buffer['readInt32BE'];
216+
217+
/**
218+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
219+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
220+
*
221+
* @param offset
222+
*/
223+
224+
readInt32LE: Buffer['readInt32LE'];
225+
226+
/**
227+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
228+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
229+
*
230+
* @param offset
231+
*/
232+
233+
readUInt32BE: Buffer['readUInt32BE'];
234+
235+
/**
236+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
237+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
238+
*
239+
* @param offset
240+
*/
241+
242+
readUInt32LE: Buffer['readUInt32LE'];
243+
244+
/**
245+
* All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.
246+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html) documentation for how these work.
247+
*
248+
* @param offset
249+
*/
250+
251+
readInt16BE: Buffer['readInt16BE'];
252+
253+
/**
254+
* All of the standard byte-reading methods of the Buffer interface are
255+
* implemented and will operate across internal Buffer boundaries transparently.
256+
*
257+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
258+
* documentation for how these work.
259+
*
260+
* @param offset
261+
*/
262+
263+
readInt16LE: Buffer['readInt16LE'];
264+
265+
/**
266+
* All of the standard byte-reading methods of the Buffer interface are
267+
* implemented and will operate across internal Buffer boundaries transparently.
268+
*
269+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
270+
* documentation for how these work.
271+
*
272+
* @param offset
273+
*/
274+
275+
readUInt16BE: Buffer['readUInt16BE'];
276+
277+
/**
278+
* All of the standard byte-reading methods of the Buffer interface are
279+
* implemented and will operate across internal Buffer boundaries transparently.
280+
*
281+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
282+
* documentation for how these work.
283+
*
284+
* @param offset
285+
*/
286+
287+
readUInt16LE: Buffer['readUInt16LE'];
288+
289+
/**
290+
* All of the standard byte-reading methods of the Buffer interface are
291+
* implemented and will operate across internal Buffer boundaries transparently.
292+
*
293+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
294+
* documentation for how these work.
295+
*
296+
* @param offset
297+
*/
298+
299+
readInt8: Buffer['readInt8'];
300+
301+
/**
302+
* All of the standard byte-reading methods of the Buffer interface are
303+
* implemented and will operate across internal Buffer boundaries transparently.
304+
*
305+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
306+
* documentation for how these work.
307+
*
308+
* @param offset
309+
*/
310+
311+
readUInt8: Buffer['readUInt8'];
312+
313+
/**
314+
* All of the standard byte-reading methods of the Buffer interface are
315+
* implemented and will operate across internal Buffer boundaries transparently.
316+
*
317+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
318+
* documentation for how these work.
319+
*
320+
* @param offset
321+
*/
322+
323+
readIntBE: Buffer['readIntBE'];
324+
325+
/**
326+
* All of the standard byte-reading methods of the Buffer interface are
327+
* implemented and will operate across internal Buffer boundaries transparently.
328+
*
329+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
330+
* documentation for how these work.
331+
*
332+
* @param offset
333+
*/
334+
335+
readIntLE: Buffer['readIntLE'];
336+
337+
/**
338+
* All of the standard byte-reading methods of the Buffer interface are
339+
* implemented and will operate across internal Buffer boundaries transparently.
340+
*
341+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
342+
* documentation for how these work.
343+
*
344+
* @param offset
345+
*/
346+
347+
readUIntBE: Buffer['readUIntBE'];
348+
349+
/**
350+
* All of the standard byte-reading methods of the Buffer interface are
351+
* implemented and will operate across internal Buffer boundaries transparently.
352+
*
353+
* See the [Buffer](http://nodejs.org/docs/latest/api/buffer.html)
354+
* documentation for how these work.
355+
*
356+
* @param offset
357+
*/
358+
359+
readUIntLE: Buffer['readUIntLE'];
360+
}
361+
362+
/**
363+
* No arguments are required for the constructor, but you can initialise
364+
* the list by passing in a single Buffer object or an array of Buffer
365+
* objects.
366+
*
367+
* `new` is not strictly required, if you don't instantiate a new object,
368+
* it will be done automatically for you so you can create a new instance
369+
* simply with:
370+
*
371+
* ```js
372+
* const { BufferList } = require('bl')
373+
* const bl = BufferList()
374+
*
375+
* // equivalent to:
376+
*
377+
* const { BufferList } = require('bl')
378+
* const bl = new BufferList()
379+
* ```
380+
*/
381+
382+
declare const BufferList: BufferListConstructor;

0 commit comments

Comments
 (0)