abstract-random-access
Advanced tools
Comparing version 1.0.2 to 1.1.0
148
index.js
var EventEmitter = require('events').EventEmitter | ||
var inherits = require('inherits') | ||
var assert = require('assert') | ||
@@ -10,59 +9,81 @@ var noop = function () {} | ||
function Abstract () { | ||
if (!(this instanceof Abstract)) return new Abstract() | ||
function Abstract (opts) { | ||
if (!(this instanceof Abstract)) return new Abstract(opts) | ||
EventEmitter.call(this) | ||
this.opened = false | ||
this._opening = null | ||
this._closing = null | ||
if (opts) { | ||
if (opts.read) this._read = opts.read | ||
if (opts.write) this._write = opts.write | ||
if (opts.open) this._open = opts.open | ||
if (opts.close) this._close = opts.close | ||
if (opts.end) this._end = opts.end | ||
if (opts.unlink) this._unlink = opts.unlink | ||
} | ||
} | ||
Abstract.prototype.open = function (callback) { | ||
var self = this | ||
if (!callback) callback = noop | ||
if (this.opened) return process.nextTick(callback) | ||
if (typeof this._open !== 'function') process.nextTick(next) | ||
else this._open(next) | ||
function next (err) { | ||
if (err) return callback(err) | ||
self.opened = true | ||
var self = this | ||
if (this._opening) { | ||
this._opening.push(callback) | ||
} else { | ||
this._opening = [callback] | ||
this._open(opened) | ||
} | ||
function opened (err) { | ||
if (!err) self.opened = true | ||
var cbs = self._opening | ||
self._opening = null | ||
self.emit('open') | ||
callback() | ||
for (var i = 0; i < cbs.length; i++) cbs[i](err) | ||
} | ||
} | ||
Abstract.prototype._open = function (callback) { | ||
process.nextTick(callback) | ||
} | ||
Abstract.prototype.write = function (offset, buffer, callback) { | ||
var self = this | ||
callback = callback || noop | ||
if (!callback) callback = noop | ||
assert(typeof offset === 'number', 'Scalar offset') | ||
assert(Buffer.isBuffer(buffer), 'Buffer') | ||
if (typeof offset !== 'number') throw new Error('Scalar offset') | ||
if (!Buffer.isBuffer(buffer)) throw new Error('Buffer') | ||
if (!this.opened) this.open(next) | ||
else next() | ||
if (!this.opened) return openAndWrite(this, offset, buffer, callback) | ||
this._write(offset, buffer, callback) | ||
} | ||
function next (err) { | ||
if (err) return callback(err) | ||
if (typeof self._write !== 'function') return process.nextTick(callback) | ||
self._write(offset, buffer, callback) | ||
} | ||
Abstract.prototype._write = function (offset, buffer, callback) { | ||
process.nextTick(function () { | ||
callback(new Error('Write not implemented')) | ||
}) | ||
} | ||
Abstract.prototype.read = function (offset, length, callback) { | ||
var self = this | ||
if (typeof offset !== 'number') throw new Error('Scalar offset') | ||
if (typeof length !== 'number') throw new Error('Scalar length') | ||
if (typeof callback !== 'function') throw new Error('Callback') | ||
assert(typeof offset === 'number', 'Scalar offset') | ||
assert(typeof length === 'number', 'Scalar length') | ||
assert(typeof callback === 'function', 'Callback') | ||
if (!this.opened) return openAndRead(this, offset, length, callback) | ||
this._read(offset, length, callback) | ||
} | ||
if (!this.opened) this.open(next) | ||
else next() | ||
function next (err) { | ||
if (err) return callback(err) | ||
if (typeof self._read !== 'function') return process.nextTick(callback) | ||
self._read(offset, length, callback) | ||
} | ||
Abstract.prototype._read = function (offset, length, callback) { | ||
process.nextTick(function () { | ||
callback(new Error('Read not implemented')) | ||
}) | ||
} | ||
Abstract.prototype.close = function (callback) { | ||
if (!callback) callback = noop | ||
var self = this | ||
callback = callback || noop | ||
@@ -74,21 +95,29 @@ if (!this.opened) this.open(next) | ||
if (err) return callback(err) | ||
if (typeof self._close !== 'function') process.nextTick(closed) | ||
self._close(closed) | ||
if (self._closing) { | ||
self._closing.push(callback) | ||
} else { | ||
self._closing = [callback] | ||
self._close(closed) | ||
} | ||
} | ||
function closed (err) { | ||
if (err) return callback(err) | ||
self.opened = false | ||
if (!err) self.opened = false | ||
var cbs = self._closing | ||
self._closing = null | ||
self.emit('close') | ||
callback() | ||
for (var i = 0; i < cbs.length; i++) cbs[i](err) | ||
} | ||
} | ||
Abstract.prototype._close = function (callback) { | ||
process.nextTick(callback) | ||
} | ||
Abstract.prototype.end = function (options, callback) { | ||
if (typeof opened === 'function') return this.end(null, options) | ||
if (!callback) callback = noop | ||
var self = this | ||
if (!options || typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
callback = callback || noop | ||
@@ -100,10 +129,14 @@ if (!this.opened) this.open(next) | ||
if (err) return callback(err) | ||
if (typeof self._end !== 'function') process.nextTick(callback) | ||
else self._end(options, callback) | ||
self._end(options, callback) | ||
} | ||
} | ||
Abstract.prototype._end = function (callback) { | ||
process.nextTick(callback) | ||
} | ||
Abstract.prototype.unlink = function (callback) { | ||
if (!callback) callback = noop | ||
var self = this | ||
callback = callback || noop | ||
@@ -115,5 +148,22 @@ if (!this.opened) this.open(next) | ||
if (err) return callback(err) | ||
if (typeof self._unlink !== 'function') process.nextTick(callback) | ||
else self._unlink(callback) | ||
self._unlink(callback) | ||
} | ||
} | ||
Abstract.prototype._unlink = function (callback) { | ||
process.nextTick(callback) | ||
} | ||
function openAndWrite (self, offset, buffer, callback) { | ||
self.open(function (err) { | ||
if (err) return callback(err) | ||
self.write(offset, buffer, callback) | ||
}) | ||
} | ||
function openAndRead (self, offset, length, callback) { | ||
self.open(function (err) { | ||
if (err) return callback(err) | ||
self.read(offset, length, callback) | ||
}) | ||
} |
{ | ||
"name": "abstract-random-access", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Base class for random access stores", | ||
@@ -8,3 +8,3 @@ "repository": "juliangruber/abstract-random-access", | ||
"scripts": { | ||
"test": "standard" | ||
"test": "standard && tape test.js" | ||
}, | ||
@@ -15,4 +15,5 @@ "dependencies": { | ||
"devDependencies": { | ||
"standard": "^7.1.2" | ||
"standard": "^7.1.2", | ||
"tape": "^4.6.0" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6572
5
209
2
1