├── .gitignore ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # random-access-stream 3 | 4 | Consume a [random-access storage](https://github.com/juliangruber/abstract-random-access) as a readable stream. 5 | 6 | ## Example 7 | 8 | ```js 9 | var Stream = require('random-access-stream') 10 | var ram = require('random-access-memory') 11 | 12 | var store = ram(Buffer('Hello cruel world')) 13 | var stream = Stream(store) 14 | stream.pipe(process.stdout) 15 | ``` 16 | 17 | ## API 18 | 19 | ### Stream(store[, opts]) 20 | 21 | Options: 22 | 23 | - `start=0` The byte offset at which to start reading 24 | - `end=Infinity` The byte offset at which to end reading 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const from = require('from2') 2 | 3 | module.exports = createReadStream 4 | 5 | function createReadStream (ras, opts) { 6 | if (!opts) opts = {} 7 | 8 | var start = opts.start || 0 9 | var end = opts.end !== undefined ? opts.end : -1 10 | var opened = false 11 | 12 | return from(read) 13 | 14 | function open (size, cb) { 15 | if (end !== -1) { 16 | opened = true 17 | return read(size, cb) 18 | } 19 | 20 | if (!ras.statable) return cb(new Error('Storage must be statable')) 21 | 22 | ras.stat(function (err, st) { 23 | if (err) return cb(err) 24 | end = st.size 25 | opened = true 26 | read(size, cb) 27 | }) 28 | } 29 | 30 | function read (size, cb) { 31 | if (!opened) return open(size, cb) 32 | if (start >= end) return cb(null, null) 33 | if (start + size > end) size = end - start 34 | const offset = start 35 | start += size 36 | ras.read(offset, size, cb) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-access-stream", 3 | "description": "Consume a random-access storage as a readable stream", 4 | "version": "2.0.0", 5 | "repository": "randoma-acces-storage/random-access-stream", 6 | "license": "MIT", 7 | "scripts": { 8 | "test": "tape test.js && standard" 9 | }, 10 | "devDependencies": { 11 | "random-access-memory": "^3.0.0", 12 | "tape": "^4.9.0" 13 | }, 14 | "dependencies": { 15 | "from2": "^2.3.0" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const tape = require('tape') 2 | const ram = require('random-access-memory') 3 | const createStream = require('./') 4 | 5 | tape('small buf', function (t) { 6 | const buf = Buffer.alloc(1000).fill('abc') 7 | const st = ram(buf) 8 | const output = [] 9 | 10 | createStream(st) 11 | .on('data', function (data) { 12 | output.push(data) 13 | }) 14 | .on('end', function () { 15 | t.same(buf, Buffer.concat(output)) 16 | t.end() 17 | }) 18 | }) 19 | 20 | tape('empty buf', function (t) { 21 | const buf = Buffer.alloc(0) 22 | const st = ram(buf) 23 | const output = [] 24 | 25 | createStream(st) 26 | .on('data', function (data) { 27 | output.push(data) 28 | }) 29 | .on('end', function () { 30 | t.same(buf, Buffer.concat(output)) 31 | t.end() 32 | }) 33 | }) 34 | tape('bigger buf', function (t) { 35 | const buf = Buffer.alloc(20 * 65536).fill('abc') 36 | const st = ram(buf) 37 | const output = [] 38 | 39 | createStream(st) 40 | .on('data', function (data) { 41 | output.push(data) 42 | }) 43 | .on('end', function () { 44 | t.same(buf, Buffer.concat(output)) 45 | t.end() 46 | }) 47 | }) 48 | 49 | tape('bigger buf and start', function (t) { 50 | const buf = Buffer.alloc(20 * 65536).fill('abc') 51 | const st = ram(buf) 52 | const output = [] 53 | 54 | createStream(st, {start: 5555}) 55 | .on('data', function (data) { 56 | output.push(data) 57 | }) 58 | .on('end', function () { 59 | t.same(buf.slice(5555), Buffer.concat(output)) 60 | t.end() 61 | }) 62 | }) 63 | 64 | tape('bigger buf and start and end', function (t) { 65 | const buf = Buffer.alloc(20 * 65536).fill('abc') 66 | const st = ram(buf) 67 | const output = [] 68 | 69 | createStream(st, {start: 5555, end: 6666}) 70 | .on('data', function (data) { 71 | output.push(data) 72 | }) 73 | .on('end', function () { 74 | t.same(buf.slice(5555, 6666), Buffer.concat(output)) 75 | t.end() 76 | }) 77 | }) 78 | --------------------------------------------------------------------------------