Skip to content

Commit

Permalink
add ram(buf) constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Aug 2, 2016
1 parent 19d8504 commit e0f01be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ file.write(0, Buffer('hello'), function () {
})
```

You can also initialize a `ram` instance with a `Buffer`:

```js
var file = ram(Buffer('hello world'))
```

## License

MIT
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var AbstractRandomAccess = require('abstract-random-access')

module.exports = RandomAccessMemory

function RandomAccessMemory () {
function RandomAccessMemory (buf) {
if (!(this instanceof RandomAccessMemory)) return new RandomAccessMemory()
AbstractRandomAccess.call(this)
this.buffer = Buffer(0)
this.length = 0
this.buffer = buf || Buffer(0)
this.length = this.buffer.length
}

inherits(RandomAccessMemory, AbstractRandomAccess)
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ tape('random access write and read', function (t) {
})
})
})

tape('buffer constructor', function (t) {
var file = ram(Buffer('contents'))
file.read(0, 7, function (err, buf) {
t.error(err)
t.deepEqual(buf, Buffer('content'))
t.end()
})
})

0 comments on commit e0f01be

Please sign in to comment.