Skip to content

Commit

Permalink
Merge pull request #7 from JoshGlazebrook/typescript
Browse files Browse the repository at this point in the history
Typescript Rewrite (v2.0)
  • Loading branch information
JoshGlazebrook authored Jan 30, 2017
2 parents 0771caf + 4951108 commit 05b4c77
Show file tree
Hide file tree
Showing 13 changed files with 1,397 additions and 644 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ logs
results
node_modules
npm-debug.log
coverage
coverage
build
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.idea*
node_modules/
npm-debug.log
coverage
coverage
src
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
language: node_js
node_js:
- 0.10
- 0.12
- 4
- 6
- 7
- stable

before_script:
- npm install -g typescript
- tsc -p ./

script: "npm run coverage"
# Send coverage data to Coveralls
after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
10 changes: 10 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "./"],
"showOutput": "silent",
"problemMatcher": "$tsc"
}
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Change Log

## 2.0
> Relased 01/30/2017
### New Features:

* Entire package re-written in TypeScript (2.1)
* Backwards compatibility is preserved for now
* New factory methods for creating SmartBuffer instances
* SmartBuffer.fromSize()
* SmartBuffer.fromBuffer()
* SmartBuffer.fromOptions()
* New SmartBufferOptions constructor options
* Added additional tests

### Bug Fixes:
* Fixes a bug where reading null terminated strings may result in an exception.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Josh Glazebrook
Copyright (c) 2013-2017 Josh Glazebrook

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
76 changes: 52 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Key Features:
* Grows the internal Buffer as you add data to it.
* Useful string operations. (Null terminating strings)
* Allows for inserting values at specific points in the internal Buffer.
* Built in TypeScript
* Type Definitions Provided

Requirements:
* Node v4.0+ is supported at this time. (Versions prior to 2.0 will work on node 0.10)


#### Note:
smart-buffer can be used for writing to an underlying buffer as well as reading from it. It however does not function correctly if you're mixing both read and write operations with each other.
Expand All @@ -22,6 +28,10 @@ smart-buffer can be used for writing to an underlying buffer as well as reading

`npm install smart-buffer`

or

`yarn install smart-buffer`

## Using smart-buffer

### Example
Expand All @@ -34,7 +44,7 @@ To build this packet using the vanilla Buffer class, you would have to count up

```javascript
function createLoginPacket(username, password, age, country) {
var packet = new SmartBuffer();
let packet = new SmartBuffer();
packet.writeUInt16LE(0x0060); // Login Packet Type/ID
packet.writeStringNT(username);
packet.writeStringNT(password);
Expand All @@ -47,7 +57,7 @@ function createLoginPacket(username, password, age, country) {
```
With the above function, you now can do this:
```javascript
var login = createLoginPacket("Josh", "secret123", 22, "United States");
let login = createLoginPacket("Josh", "secret123", 22, "United States");

// <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>
```
Expand All @@ -56,9 +66,9 @@ Notice that the `[PacketLength:2]` part of the packet was inserted after we had
Reading back the packet we created above is just as easy:
```javascript

var reader = new SmartBuffer(login);
let reader = SmartBuffer.fromBuffer(login);

var logininfo = {
let logininfo = {
packetType: reader.readUInt16LE(),
packetLength: reader.readUInt16LE(),
username: reader.readStringNT(),
Expand All @@ -83,20 +93,43 @@ var logininfo = {

### Constructing a smart-buffer

smart-buffer has a few different constructor signatures you can use. By default, utf8 encoding is used, and the internal Buffer length will be 4096. When reading from a Buffer, smart-buffer does NOT make a copy of the Buffer. It reads from the Buffer it was given.
smart-buffer has a few different ways to construct an instance. Starting with version 2.0, the following factory methods are preffered.

```javascript
var SmartBuffer = require('smart-buffer');
let SmartBuffer = require('smart-buffer');

// Creating SmartBuffer from existing Buffer
let buff = SmartBuffer.fromBuffer(buffer); // Creates instance from buffer. (Uses default utf8 encoding)
let buff = SmartBuffer.fromBuffer(buffer, 'ascii'); // Creates instance from buffer with ascii encoding for Strings.

// Reading from an existing Buffer:
var reader = new SmartBuffer(buffer);
var reader = new SmartBuffer(buffer, 'ascii');
// Creating SmartBuffer with specified internal Buffer size.
let buff = SmartBuffer.fromSize(1024); // Creates instance with internal Buffer size of 1024.
let buff = SmartBuffer.fromSize(1024, 'utf8'); // Creates instance with intenral Buffer size of 1024, and utf8 encoding.

// Writing to a new Buffer:
var writer = new SmartBuffer(); // Defaults to utf8, 4096 length internal Buffer.
var writer = new SmartBuffer(1024); // Defaults to utf8, 1024 length internal Buffer.
var writer = new SmartBuffer('ascii'); // Sets to ascii encoding, 4096 length internal buffer.
var writer = new SmartBuffer(1024, 'ascii'); // Sets to ascii encoding, 1024 length internal buffer.
// Creating SmartBuffer with options object. This one specifies size and encoding.
let buff = SmartBuffer.fromOptions({
size: 1024,
encoding: 'ascii'
});

// Creating SmartBuffer with options object. This one specified an existing Buffer.
let buff = SmartBuffer.fromOptions({
buff: buffer
});

// Just want a regular SmartBuffer with all default options?
let buff = new SmartBuffer();
```

## Backwards Compatibility:

All constructors used prior to 2.0 still are supported. However it's not recommended to use these.

```javascript
let writer = new SmartBuffer(); // Defaults to utf8, 4096 length internal Buffer.
let writer = new SmartBuffer(1024); // Defaults to utf8, 1024 length internal Buffer.
let writer = new SmartBuffer('ascii'); // Sets to ascii encoding, 4096 length internal buffer.
let writer = new SmartBuffer(1024, 'ascii'); // Sets to ascii encoding, 1024 length internal buffer.
```

## Reading Data
Expand Down Expand Up @@ -124,24 +157,24 @@ Supported Operations:
* readDoubleLE

```javascript
var reader = new SmartBuffer(somebuffer);
var num = reader.readInt8();
let reader = new SmartBuffer(somebuffer);
let num = reader.readInt8();
```

## Reading String Values

When reading String values, you can either choose to read a null terminated string, or a string of a specified length.

### SmartBuffer.readStringNT( [encoding] )
> `String` **String encoding to use** - Defaults to the encoding set in the constructor, or utf8.
> `String` **String encoding to use** - Defaults to the encoding set in the constructor.
returns `String`

> Note: When readStringNT is called and there is no null character found, smart-buffer will read to the end of the internal Buffer.
### SmartBuffer.readString( [length], [encoding] )
### SmartBuffer.readString( [length] )
### SmartBuffer.readString( [encoding] )
### SmartBuffer.readString( [length], [encoding] )
> `Number` **Length of the string to read**
> `String` **String encoding to use** - Defaults to the encoding set in the constructor, or utf8.
Expand Down Expand Up @@ -274,7 +307,7 @@ Rewinds the read position backwards by the given value.

returns this

### SmartBuffer.skipTo( position )
### SmartBuffer.moveTo( position )
> `Number` **The point to skip the read position to**
Moves the read position to the given point.
Expand All @@ -291,11 +324,6 @@ returns `Buffer` A Buffer containing the contents of the internal Buffer.
returns `String` The internal Buffer in String representation.

### SmartBuffer.destroy()
Attempts to destroy the smart-buffer.

returns this

## Properties

### SmartBuffer.length
Expand Down
Loading

0 comments on commit 05b4c77

Please sign in to comment.