Skip to content

Commit

Permalink
Use Buffer instead of string concat (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fvdm committed Dec 17, 2014
1 parent ccfb11a commit d23148b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ cam.camera_params( console.log )
## snapshot
### ( [filename], callback )

Take a snapshot. Either receive the **binary JPEG** in the `callback` or specify a `filename` to store it on your computer.
Take a snapshot. Either receive the **binary JPEG** [`Buffer`](http://nodejs.org/api/buffer.html) in the `callback` or specify a `filename` to store it on your computer.

When a `filename` is provided the callback will return either the *filename* on success or *false* on faillure.

```js
// custom processing
cam.snapshot( function( jpeg ) {
// add binary processing here
// add binary Buffer processing here
})

// store locally
Expand Down
16 changes: 11 additions & 5 deletions foscam.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ app.snapshot = function( filepath, cb ) {
app.talk({
path: 'snapshot.cgi',
encoding: 'binary',
callback: function( bin ) {
callback: function( buf ) {
if( filepath ) {
fs.writeFile( filepath, bin, 'binary', function( err ) {
fs.writeFile( filepath, buf, 'binary', function( err ) {
if( err ) {
throw err
cb( false )
Expand Down Expand Up @@ -391,14 +391,20 @@ app.talk = function( props ) {

// response
response.setEncoding( props.encoding ? props.encoding : 'utf8' )
var data = ''
var data = []
var size = 0

response.on( 'data', function( chunk ) {
data.push( chunk )
size += chunk.length
})

response.on( 'data', function( chunk ) { data += chunk })
response.on( 'end', function() {
data = new Buffer.concat( data, size )

if( typeof props.callback == 'function' ) {
if( props.encoding !== 'binary' ) {
data = data.trim()
data = data.toString().trim()
}
props.callback( data )
}
Expand Down

0 comments on commit d23148b

Please sign in to comment.