You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. smart-buffer [![Build Status](https://travis-ci.org/JoshGlazebrook/smart-buffer.svg?branch=master)](https://travis-ci.org/JoshGlazebrook/smart-buffer) [![Coverage Status](https://coveralls.io/repos/github/JoshGlazebrook/smart-buffer/badge.svg?branch=master)](https://coveralls.io/github/JoshGlazebrook/smart-buffer?branch=master)
  2. =============
  3. smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
  4. ![stats](https://nodei.co/npm/smart-buffer.png?downloads=true&downloadRank=true&stars=true "stats")
  5. **Key Features**:
  6. * Proxies all of the Buffer write and read functions
  7. * Keeps track of read and write offsets automatically
  8. * Grows the internal Buffer as needed
  9. * Useful string operations. (Null terminating strings)
  10. * Allows for inserting values at specific points in the Buffer
  11. * Built in TypeScript
  12. * Type Definitions Provided
  13. * Browser Support (using Webpack/Browserify)
  14. * Full test coverage
  15. **Requirements**:
  16. * Node v4.0+ is supported at this time. (Versions prior to 2.0 will work on node 0.10)
  17. ## Breaking Changes in v4.0
  18. * Old constructor patterns have been completely removed. It's now required to use the SmartBuffer.fromXXX() factory constructors.
  19. * rewind(), skip(), moveTo() have been removed. (see [offsets](#offsets))
  20. * Internal private properties are now prefixed with underscores (_)
  21. * **All** writeXXX() methods that are given an offset will now **overwrite data** instead of insert. (see [write vs insert](#write-vs-insert))
  22. * insertXXX() methods have been added for when you want to insert data at a specific offset (this replaces the old behavior of writeXXX() when an offset was provided)
  23. ## Looking for v3 docs?
  24. Legacy documentation for version 3 and prior can be found [here](https://github.com/JoshGlazebrook/smart-buffer/blob/master/docs/README_v3.md).
  25. ## Installing:
  26. `yarn add smart-buffer`
  27. or
  28. `npm install smart-buffer`
  29. Note: The published NPM package includes the built javascript library.
  30. If you cloned this repo and wish to build the library manually use:
  31. `npm run build`
  32. ## Using smart-buffer
  33. ```javascript
  34. // Javascript
  35. const SmartBuffer = require('smart-buffer').SmartBuffer;
  36. // Typescript
  37. import { SmartBuffer, SmartBufferOptions} from 'smart-buffer';
  38. ```
  39. ### Simple Example
  40. Building a packet that uses the following protocol specification:
  41. `[PacketType:2][PacketLength:2][Data:XX]`
  42. To build this packet using the vanilla Buffer class, you would have to count up the length of the data payload beforehand. You would also need to keep track of the current "cursor" position in your Buffer so you write everything in the right places. With smart-buffer you don't have to do either of those things.
  43. ```javascript
  44. function createLoginPacket(username, password, age, country) {
  45. const packet = new SmartBuffer();
  46. packet.writeUInt16LE(0x0060); // Some packet type
  47. packet.writeStringNT(username);
  48. packet.writeStringNT(password);
  49. packet.writeUInt8(age);
  50. packet.writeStringNT(country);
  51. packet.insertUInt16LE(packet.length - 2, 2);
  52. return packet.toBuffer();
  53. }
  54. ```
  55. With the above function, you now can do this:
  56. ```javascript
  57. const login = createLoginPacket("Josh", "secret123", 22, "United States");
  58. // <Buffer 60 00 1e 00 4a 6f 73 68 00 73 65 63 72 65 74 31 32 33 00 16 55 6e 69 74 65 64 20 53 74 61 74 65 73 00>
  59. ```
  60. Notice that the `[PacketLength:2]` value (1e 00) was inserted at position 2.
  61. Reading back the packet we created above is just as easy:
  62. ```javascript
  63. const reader = SmartBuffer.fromBuffer(login);
  64. const logininfo = {
  65. packetType: reader.readUInt16LE(),
  66. packetLength: reader.readUInt16LE(),
  67. username: reader.readStringNT(),
  68. password: reader.readStringNT(),
  69. age: reader.readUInt8(),
  70. country: reader.readStringNT()
  71. };
  72. /*
  73. {
  74. packetType: 96, (0x0060)
  75. packetLength: 30,
  76. username: 'Josh',
  77. password: 'secret123',
  78. age: 22,
  79. country: 'United States'
  80. }
  81. */
  82. ```
  83. ## Write vs Insert
  84. In prior versions of SmartBuffer, .writeXXX(value, offset) calls would insert data when an offset was provided. In version 4, this will now overwrite the data at the offset position. To insert data there are now corresponding .insertXXX(value, offset) methods.
  85. **SmartBuffer v3**:
  86. ```javascript
  87. const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6]));
  88. buff.writeInt8(7, 2);
  89. console.log(buff.toBuffer())
  90. // <Buffer 01 02 07 03 04 05 06>
  91. ```
  92. **SmartBuffer v4**:
  93. ```javascript
  94. const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6]));
  95. buff.writeInt8(7, 2);
  96. console.log(buff.toBuffer());
  97. // <Buffer 01 02 07 04 05 06>
  98. ```
  99. To insert you instead should use:
  100. ```javascript
  101. const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6]));
  102. buff.insertInt8(7, 2);
  103. console.log(buff.toBuffer());
  104. // <Buffer 01 02 07 03 04 05 06>
  105. ```
  106. **Note:** Insert/Writing to a position beyond the currently tracked internal Buffer will zero pad to your offset.
  107. ## Constructing a smart-buffer
  108. There are a few different ways to construct a SmartBuffer instance.
  109. ```javascript
  110. // Creating SmartBuffer from existing Buffer
  111. const buff = SmartBuffer.fromBuffer(buffer); // Creates instance from buffer. (Uses default utf8 encoding)
  112. const buff = SmartBuffer.fromBuffer(buffer, 'ascii'); // Creates instance from buffer with ascii encoding for strings.
  113. // Creating SmartBuffer with specified internal Buffer size. (Note: this is not a hard cap, the internal buffer will grow as needed).
  114. const buff = SmartBuffer.fromSize(1024); // Creates instance with internal Buffer size of 1024.
  115. const buff = SmartBuffer.fromSize(1024, 'utf8'); // Creates instance with internal Buffer size of 1024, and utf8 encoding for strings.
  116. // Creating SmartBuffer with options object. This one specifies size and encoding.
  117. const buff = SmartBuffer.fromOptions({
  118. size: 1024,
  119. encoding: 'ascii'
  120. });
  121. // Creating SmartBuffer with options object. This one specified an existing Buffer.
  122. const buff = SmartBuffer.fromOptions({
  123. buff: buffer
  124. });
  125. // Creating SmartBuffer from a string.
  126. const buff = SmartBuffer.fromBuffer(Buffer.from('some string', 'utf8'));
  127. // Just want a regular SmartBuffer with all default options?
  128. const buff = new SmartBuffer();
  129. ```
  130. # Api Reference:
  131. **Note:** SmartBuffer is fully documented with Typescript definitions as well as jsdocs so your favorite editor/IDE will have intellisense.
  132. **Table of Contents**
  133. 1. [Constructing](#constructing)
  134. 2. **Numbers**
  135. 1. [Integers](#integers)
  136. 2. [Floating Points](#floating-point-numbers)
  137. 3. **Strings**
  138. 1. [Strings](#strings)
  139. 2. [Null Terminated Strings](#null-terminated-strings)
  140. 4. [Buffers](#buffers)
  141. 5. [Offsets](#offsets)
  142. 6. [Other](#other)
  143. ## Constructing
  144. ### constructor()
  145. ### constructor([options])
  146. - ```options``` *{SmartBufferOptions}* An optional options object to construct a SmartBuffer with.
  147. Examples:
  148. ```javascript
  149. const buff = new SmartBuffer();
  150. const buff = new SmartBuffer({
  151. size: 1024,
  152. encoding: 'ascii'
  153. });
  154. ```
  155. ### Class Method: fromBuffer(buffer[, encoding])
  156. - ```buffer``` *{Buffer}* The Buffer instance to wrap.
  157. - ```encoding``` *{string}* The string encoding to use. ```Default: 'utf8'```
  158. Examples:
  159. ```javascript
  160. const someBuffer = Buffer.from('some string');
  161. const buff = SmartBuffer.fromBuffer(someBuffer); // Defaults to utf8
  162. const buff = SmartBuffer.fromBuffer(someBuffer, 'ascii');
  163. ```
  164. ### Class Method: fromSize(size[, encoding])
  165. - ```size``` *{number}* The size to initialize the internal Buffer.
  166. - ```encoding``` *{string}* The string encoding to use. ```Default: 'utf8'```
  167. Examples:
  168. ```javascript
  169. const buff = SmartBuffer.fromSize(1024); // Defaults to utf8
  170. const buff = SmartBuffer.fromSize(1024, 'ascii');
  171. ```
  172. ### Class Method: fromOptions(options)
  173. - ```options``` *{SmartBufferOptions}* The Buffer instance to wrap.
  174. ```typescript
  175. interface SmartBufferOptions {
  176. encoding?: BufferEncoding; // Defaults to utf8
  177. size?: number; // Defaults to 4096
  178. buff?: Buffer;
  179. }
  180. ```
  181. Examples:
  182. ```javascript
  183. const buff = SmartBuffer.fromOptions({
  184. size: 1024
  185. };
  186. const buff = SmartBuffer.fromOptions({
  187. size: 1024,
  188. encoding: 'utf8'
  189. });
  190. const buff = SmartBuffer.fromOptions({
  191. encoding: 'utf8'
  192. });
  193. const someBuff = Buffer.from('some string', 'utf8');
  194. const buff = SmartBuffer.fromOptions({
  195. buffer: someBuff,
  196. encoding: 'utf8'
  197. });
  198. ```
  199. ## Integers
  200. ### readInt8([offset])
  201. - ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
  202. - Returns *{number}*
  203. Read a Int8 value.
  204. ### buff.readInt16BE([offset])
  205. ### buff.readInt16LE([offset])
  206. ### buff.readUInt16BE([offset])
  207. ### buff.readUInt16LE([offset])
  208. - ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
  209. - Returns *{number}*
  210. Read a 16 bit integer value.
  211. ### buff.readInt32BE([offset])
  212. ### buff.readInt32LE([offset])
  213. ### buff.readUInt32BE([offset])
  214. ### buff.readUInt32LE([offset])
  215. - ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
  216. - Returns *{number}*
  217. Read a 32 bit integer value.
  218. ### buff.writeInt8(value[, offset])
  219. ### buff.writeUInt8(value[, offset])
  220. - ```value``` *{number}* The value to write.
  221. - ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
  222. - Returns *{this}*
  223. Write a Int8 value.
  224. ### buff.insertInt8(value, offset)
  225. ### buff.insertUInt8(value, offset)
  226. - ```value``` *{number}* The value to insert.
  227. - ```offset``` *{number}* The offset to insert this data at.
  228. - Returns *{this}*
  229. Insert a Int8 value.
  230. ### buff.writeInt16BE(value[, offset])
  231. ### buff.writeInt16LE(value[, offset])
  232. ### buff.writeUInt16BE(value[, offset])
  233. ### buff.writeUInt16LE(value[, offset])
  234. - ```value``` *{number}* The value to write.
  235. - ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
  236. - Returns *{this}*
  237. Write a 16 bit integer value.
  238. ### buff.insertInt16BE(value, offset)
  239. ### buff.insertInt16LE(value, offset)
  240. ### buff.insertUInt16BE(value, offset)
  241. ### buff.insertUInt16LE(value, offset)
  242. - ```value``` *{number}* The value to insert.
  243. - ```offset``` *{number}* The offset to insert this data at.
  244. - Returns *{this}*
  245. Insert a 16 bit integer value.
  246. ### buff.writeInt32BE(value[, offset])
  247. ### buff.writeInt32LE(value[, offset])
  248. ### buff.writeUInt32BE(value[, offset])
  249. ### buff.writeUInt32LE(value[, offset])
  250. - ```value``` *{number}* The value to write.
  251. - ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
  252. - Returns *{this}*
  253. Write a 32 bit integer value.
  254. ### buff.insertInt32BE(value, offset)
  255. ### buff.insertInt32LE(value, offset)
  256. ### buff.insertUInt32BE(value, offset)
  257. ### buff.nsertUInt32LE(value, offset)
  258. - ```value``` *{number}* The value to insert.
  259. - ```offset``` *{number}* The offset to insert this data at.
  260. - Returns *{this}*
  261. Insert a 32 bit integer value.
  262. ## Floating Point Numbers
  263. ### buff.readFloatBE([offset])
  264. ### buff.readFloatLE([offset])
  265. - ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
  266. - Returns *{number}*
  267. Read a Float value.
  268. ### buff.eadDoubleBE([offset])
  269. ### buff.readDoubleLE([offset])
  270. - ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
  271. - Returns *{number}*
  272. Read a Double value.
  273. ### buff.writeFloatBE(value[, offset])
  274. ### buff.writeFloatLE(value[, offset])
  275. - ```value``` *{number}* The value to write.
  276. - ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
  277. - Returns *{this}*
  278. Write a Float value.
  279. ### buff.insertFloatBE(value, offset)
  280. ### buff.insertFloatLE(value, offset)
  281. - ```value``` *{number}* The value to insert.
  282. - ```offset``` *{number}* The offset to insert this data at.
  283. - Returns *{this}*
  284. Insert a Float value.
  285. ### buff.writeDoubleBE(value[, offset])
  286. ### buff.writeDoubleLE(value[, offset])
  287. - ```value``` *{number}* The value to write.
  288. - ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
  289. - Returns *{this}*
  290. Write a Double value.
  291. ### buff.insertDoubleBE(value, offset)
  292. ### buff.insertDoubleLE(value, offset)
  293. - ```value``` *{number}* The value to insert.
  294. - ```offset``` *{number}* The offset to insert this data at.
  295. - Returns *{this}*
  296. Insert a Double value.
  297. ## Strings
  298. ### buff.readString()
  299. ### buff.readString(size[, encoding])
  300. ### buff.readString(encoding)
  301. - ```size``` *{number}* The number of bytes to read. **Default:** ```Reads to the end of the Buffer.```
  302. - ```encoding``` *{string}* The string encoding to use. **Default:** ```utf8```.
  303. Read a string value.
  304. Examples:
  305. ```javascript
  306. const buff = SmartBuffer.fromBuffer(Buffer.from('hello there', 'utf8'));
  307. buff.readString(); // 'hello there'
  308. buff.readString(2); // 'he'
  309. buff.readString(2, 'utf8'); // 'he'
  310. buff.readString('utf8'); // 'hello there'
  311. ```
  312. ### buff.writeString(value)
  313. ### buff.writeString(value[, offset])
  314. ### buff.writeString(value[, encoding])
  315. ### buff.writeString(value[, offset[, encoding]])
  316. - ```value``` *{string}* The string value to write.
  317. - ```offset``` *{number}* The offset to write this value to. **Default:** ```Auto managed offset```
  318. - ```encoding``` *{string}* An optional string encoding to use. **Default:** ```utf8```
  319. Write a string value.
  320. Examples:
  321. ```javascript
  322. buff.writeString('hello'); // Auto managed offset
  323. buff.writeString('hello', 2);
  324. buff.writeString('hello', 'utf8') // Auto managed offset
  325. buff.writeString('hello', 2, 'utf8');
  326. ```
  327. ### buff.insertString(value, offset[, encoding])
  328. - ```value``` *{string}* The string value to write.
  329. - ```offset``` *{number}* The offset to write this value to.
  330. - ```encoding``` *{string}* An optional string encoding to use. **Default:** ```utf8```
  331. Insert a string value.
  332. Examples:
  333. ```javascript
  334. buff.insertString('hello', 2);
  335. buff.insertString('hello', 2, 'utf8');
  336. ```
  337. ## Null Terminated Strings
  338. ### buff.readStringNT()
  339. ### buff.readStringNT(encoding)
  340. - ```encoding``` *{string}* The string encoding to use. **Default:** ```utf8```.
  341. Read a null terminated string value. (If a null is not found, it will read to the end of the Buffer).
  342. Examples:
  343. ```javascript
  344. const buff = SmartBuffer.fromBuffer(Buffer.from('hello\0 there', 'utf8'));
  345. buff.readStringNT(); // 'hello'
  346. // If we called this again:
  347. buff.readStringNT(); // ' there'
  348. ```
  349. ### buff.writeStringNT(value)
  350. ### buff.writeStringNT(value[, offset])
  351. ### buff.writeStringNT(value[, encoding])
  352. ### buff.writeStringNT(value[, offset[, encoding]])
  353. - ```value``` *{string}* The string value to write.
  354. - ```offset``` *{number}* The offset to write this value to. **Default:** ```Auto managed offset```
  355. - ```encoding``` *{string}* An optional string encoding to use. **Default:** ```utf8```
  356. Write a null terminated string value.
  357. Examples:
  358. ```javascript
  359. buff.writeStringNT('hello'); // Auto managed offset <Buffer 68 65 6c 6c 6f 00>
  360. buff.writeStringNT('hello', 2); // <Buffer 00 00 68 65 6c 6c 6f 00>
  361. buff.writeStringNT('hello', 'utf8') // Auto managed offset
  362. buff.writeStringNT('hello', 2, 'utf8');
  363. ```
  364. ### buff.insertStringNT(value, offset[, encoding])
  365. - ```value``` *{string}* The string value to write.
  366. - ```offset``` *{number}* The offset to write this value to.
  367. - ```encoding``` *{string}* An optional string encoding to use. **Default:** ```utf8```
  368. Insert a null terminated string value.
  369. Examples:
  370. ```javascript
  371. buff.insertStringNT('hello', 2);
  372. buff.insertStringNT('hello', 2, 'utf8');
  373. ```
  374. ## Buffers
  375. ### buff.readBuffer([length])
  376. - ```length``` *{number}* The number of bytes to read into a Buffer. **Default:** ```Reads to the end of the Buffer```
  377. Read a Buffer of a specified size.
  378. ### buff.writeBuffer(value[, offset])
  379. - ```value``` *{Buffer}* The buffer value to write.
  380. - ```offset``` *{number}* An optional offset to write the value to. **Default:** ```Auto managed offset```
  381. ### buff.insertBuffer(value, offset)
  382. - ```value``` *{Buffer}* The buffer value to write.
  383. - ```offset``` *{number}* The offset to write the value to.
  384. ### buff.readBufferNT()
  385. Read a null terminated Buffer.
  386. ### buff.writeBufferNT(value[, offset])
  387. - ```value``` *{Buffer}* The buffer value to write.
  388. - ```offset``` *{number}* An optional offset to write the value to. **Default:** ```Auto managed offset```
  389. Write a null terminated Buffer.
  390. ### buff.insertBufferNT(value, offset)
  391. - ```value``` *{Buffer}* The buffer value to write.
  392. - ```offset``` *{number}* The offset to write the value to.
  393. Insert a null terminated Buffer.
  394. ## Offsets
  395. ### buff.readOffset
  396. ### buff.readOffset(offset)
  397. - ```offset``` *{number}* The new read offset value to set.
  398. - Returns: ```The current read offset```
  399. Gets or sets the current read offset.
  400. Examples:
  401. ```javascript
  402. const currentOffset = buff.readOffset; // 5
  403. buff.readOffset = 10;
  404. console.log(buff.readOffset) // 10
  405. ```
  406. ### buff.writeOffset
  407. ### buff.writeOffset(offset)
  408. - ```offset``` *{number}* The new write offset value to set.
  409. - Returns: ```The current write offset```
  410. Gets or sets the current write offset.
  411. Examples:
  412. ```javascript
  413. const currentOffset = buff.writeOffset; // 5
  414. buff.writeOffset = 10;
  415. console.log(buff.writeOffset) // 10
  416. ```
  417. ### buff.encoding
  418. ### buff.encoding(encoding)
  419. - ```encoding``` *{string}* The new string encoding to set.
  420. - Returns: ```The current string encoding```
  421. Gets or sets the current string encoding.
  422. Examples:
  423. ```javascript
  424. const currentEncoding = buff.encoding; // 'utf8'
  425. buff.encoding = 'ascii';
  426. console.log(buff.encoding) // 'ascii'
  427. ```
  428. ## Other
  429. ### buff.clear()
  430. Clear and resets the SmartBuffer instance.
  431. ### buff.remaining()
  432. - Returns ```Remaining data left to be read```
  433. Gets the number of remaining bytes to be read.
  434. ### buff.internalBuffer
  435. - Returns: *{Buffer}*
  436. Gets the internally managed Buffer (Includes unmanaged data).
  437. Examples:
  438. ```javascript
  439. const buff = SmartBuffer.fromSize(16);
  440. buff.writeString('hello');
  441. console.log(buff.InternalBuffer); // <Buffer 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00>
  442. ```
  443. ### buff.toBuffer()
  444. - Returns: *{Buffer}*
  445. Gets a sliced Buffer instance of the internally managed Buffer. (Only includes managed data)
  446. Examples:
  447. ```javascript
  448. const buff = SmartBuffer.fromSize(16);
  449. buff.writeString('hello');
  450. console.log(buff.toBuffer()); // <Buffer 68 65 6c 6c 6f>
  451. ```
  452. ### buff.toString([encoding])
  453. - ```encoding``` *{string}* The string encoding to use when converting to a string. **Default:** ```utf8```
  454. - Returns *{string}*
  455. Gets a string representation of all data in the SmartBuffer.
  456. ### buff.destroy()
  457. Destroys the SmartBuffer instance.
  458. ## License
  459. This work is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License).