├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deferred-random-access 2 | 3 | A random-access instance that wraps another instance that is created async. 4 | 5 | ``` 6 | npm install deferred-random-access 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` js 12 | var dra = require('deferred-random-access') 13 | var ram = require('random-access-memory') 14 | 15 | var storage = dra(function (cb) { 16 | process.nextTick(function () { 17 | console.log('creating instance') 18 | cb(null, ram()) 19 | }) 20 | }) 21 | 22 | storage.write(0, Buffer.from('hi'), function () { 23 | console.log('totally wrote it', storage.length) 24 | storage.read(0, 2, console.log) 25 | }) 26 | ``` 27 | 28 | ## License 29 | 30 | MIT 31 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var dra = require('./') 2 | var ram = require('random-access-memory') 3 | 4 | var storage = dra(function (cb) { 5 | process.nextTick(function () { 6 | console.log('woooot') 7 | cb(null, ram()) 8 | }) 9 | }) 10 | 11 | storage.write(0, Buffer.from('hi'), function () { 12 | console.log('totally wrote it', storage.length) 13 | storage.read(0, 2, console.log) 14 | }) 15 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var thunky = require('thunky') 2 | 3 | module.exports = DRA 4 | 5 | function DRA (open) { 6 | if (!(this instanceof DRA)) return new DRA(open) 7 | 8 | var self = this 9 | 10 | this._instance = null 11 | this.open = thunky(function (cb) { 12 | open(function (err, instance) { 13 | if (err) return cb(err) 14 | self._instance = instance 15 | cb() 16 | }) 17 | }) 18 | 19 | this.open() 20 | } 21 | 22 | Object.defineProperty(DRA.prototype, 'length', { 23 | get: function () { 24 | return this._instance ? this._instance.length : 0 25 | } 26 | }) 27 | 28 | DRA.prototype.read = function (offset, length, cb) { 29 | if (!cb) cb = noop 30 | if (this._instance) return this._instance.read(offset, length, cb) 31 | this._waitAndRead(offset, length, cb) 32 | } 33 | 34 | DRA.prototype._waitAndRead = function (offset, length, cb) { 35 | var self = this 36 | 37 | this.open(function (err) { 38 | if (err) return cb(err) 39 | self._instance.read(offset, length, cb) 40 | }) 41 | } 42 | 43 | DRA.prototype.write = function (offset, buffer, cb) { 44 | if (!cb) cb = noop 45 | if (this._instance) return this._instance.write(offset, buffer, cb) 46 | this._waitAndWrite(offset, buffer, cb) 47 | } 48 | 49 | DRA.prototype._waitAndWrite = function (offset, buffer, cb) { 50 | var self = this 51 | 52 | this.open(function (err) { 53 | if (err) return cb(err) 54 | self._instance.write(offset, buffer, cb) 55 | }) 56 | } 57 | 58 | DRA.prototype.del = function (offset, length, cb) { 59 | if (!cb) cb = noop 60 | if (this._instance) return this._instance.read(offset, length, cb) 61 | this._waitAndDel(offset, length, cb) 62 | } 63 | 64 | DRA.prototype._waitAndDel = function (offset, length, cb) { 65 | var self = this 66 | 67 | this.open(function (err) { 68 | if (err) return cb(err) 69 | if (self._instance.del) self._instance.del(offset, length, cb) 70 | else cb(null) 71 | }) 72 | } 73 | 74 | DRA.prototype.close = function (cb) { 75 | if (!cb) cb = noop 76 | 77 | var self = this 78 | 79 | this.open(function (err) { 80 | if (err) return cb(err) 81 | if (self._instance.close) self._instance.close(cb) 82 | else cb() 83 | }) 84 | } 85 | 86 | function noop () {} 87 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "deferred-random-access", 3 | "version": "1.0.0", 4 | "description": "A random-access instance that wraps another instance that is created async", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "standard": "^10.0.1", 9 | "tape": "^4.6.3" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/mafintosh/deferred-random-access.git" 14 | }, 15 | "scripts": { 16 | "test": "standard && tape test.js" 17 | }, 18 | "author": "Mathias Buus (@mafintosh)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/mafintosh/deferred-random-access/issues" 22 | }, 23 | "homepage": "https://github.com/mafintosh/deferred-random-access" 24 | } 25 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var dra = require('./') 3 | var ram = require('random-access-memory') 4 | 5 | tape('basic', function (t) { 6 | t.plan(5) 7 | 8 | var storage = dra(function (cb) { 9 | process.nextTick(function () { 10 | t.pass('called async storage function') 11 | cb(null, ram()) 12 | }) 13 | }) 14 | 15 | storage.write(0, Buffer.from('hi'), function (err) { 16 | t.error(err, 'no error') 17 | t.same(storage.length, 2) 18 | storage.read(0, 2, function (err, buf) { 19 | t.error(err, 'no error') 20 | t.same(buf, Buffer.from('hi')) 21 | }) 22 | }) 23 | }) 24 | 25 | tape('parallel', function (t) { 26 | t.plan(13) 27 | 28 | var storage = dra(function (cb) { 29 | process.nextTick(function () { 30 | t.pass('called async storage function') 31 | cb(null, ram()) 32 | }) 33 | }) 34 | 35 | storage.write(0, Buffer.from('hi'), function (err) { 36 | t.error(err, 'no error') 37 | t.same(storage.length, 2) 38 | storage.read(0, 2, function (err, buf) { 39 | t.error(err, 'no error') 40 | t.same(buf, Buffer.from('hi')) 41 | }) 42 | }) 43 | 44 | storage.write(0, Buffer.from('hi'), function (err) { 45 | t.error(err, 'no error') 46 | t.same(storage.length, 2) 47 | storage.read(0, 2, function (err, buf) { 48 | t.error(err, 'no error') 49 | t.same(buf, Buffer.from('hi')) 50 | }) 51 | }) 52 | 53 | storage.write(0, Buffer.from('hi'), function (err) { 54 | t.error(err, 'no error') 55 | t.same(storage.length, 2) 56 | storage.read(0, 2, function (err, buf) { 57 | t.error(err, 'no error') 58 | t.same(buf, Buffer.from('hi')) 59 | }) 60 | }) 61 | }) 62 | --------------------------------------------------------------------------------