random-access-storage
Advanced tools
Comparing version 2.1.0 to 2.2.0
26
index.js
@@ -17,8 +17,9 @@ const EventEmitter = require('events') | ||
const DEL_OP = 2 | ||
const STAT_OP = 3 | ||
const TRUNCATE_OP = 3 | ||
const STAT_OP = 4 | ||
// BLOCKING_OPS | ||
const OPEN_OP = 4 | ||
const CLOSE_OP = 5 | ||
const DESTROY_OP = 6 | ||
const OPEN_OP = 5 | ||
const CLOSE_OP = 6 | ||
const DESTROY_OP = 7 | ||
@@ -42,2 +43,3 @@ module.exports = class RandomAccessStorage extends EventEmitter { | ||
if (opts.del) this._del = opts.del | ||
if (opts.truncate) this._truncate = opts.truncate | ||
if (opts.stat) this._stat = opts.stat | ||
@@ -50,2 +52,3 @@ if (opts.close) this._close = opts.close | ||
this.deletable = this._del !== RandomAccessStorage.prototype._del | ||
this.truncatable = this._truncate !== RandomAccessStorage.prototype._truncate || this.deletable | ||
this.statable = this._stat !== RandomAccessStorage.prototype._stat | ||
@@ -82,2 +85,13 @@ } | ||
truncate (offset, cb) { | ||
if (!cb) cb = noop | ||
openWritable(this) | ||
this.run(new Request(this, TRUNCATE_OP, offset, 0, null, false, cb)) | ||
} | ||
_truncate (req) { | ||
req.size = Infinity | ||
this._del(req) | ||
} | ||
stat (cb) { | ||
@@ -229,2 +243,6 @@ this.run(new Request(this, STAT_OP, 0, 0, null, false, cb)) | ||
case TRUNCATE_OP: | ||
if (this._openAndNotClosed()) ra._truncate(this) | ||
break | ||
case STAT_OP: | ||
@@ -231,0 +249,0 @@ if (this._openAndNotClosed()) ra._stat(this) |
{ | ||
"name": "random-access-storage", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Easily make random-access-storage instances", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -69,2 +69,3 @@ # random-access-storage | ||
del: fn, // sets ._del | ||
truncate: fn, // sets ._truncate | ||
stat: fn, // sets ._stat | ||
@@ -88,2 +89,6 @@ close: fn, // sets ._close | ||
#### `storage.truncatable` | ||
True if the storage implements `._truncate`. | ||
#### `storage.statable` | ||
@@ -167,3 +172,3 @@ | ||
Delete the specified amount of bytes as the specified offset. Optionally pass a callback that is called with `(err)` when the delete has completed. | ||
Delete the specified amount of bytes at the specified offset. Optionally pass a callback that is called with `(err)` when the delete has completed. | ||
@@ -181,2 +186,16 @@ #### `storage._del(req)` | ||
#### `storage.truncate(offset, [callback])` | ||
Truncate the storage at the specified offset. Optionally pass a callback that is called with `(err)` when the truncate has completed. | ||
#### `storage._truncate(req)` | ||
Implement storage truncate. Defaults to `storage._del(req)`. | ||
* `req.offset` contains the byte offset to truncate at. | ||
Call `req.callback(err)` when the truncate has completed. | ||
Note that this is guaranteed to run after the storage has been opened and not after it has been closed. | ||
#### `storage.stat(callback)` | ||
@@ -183,0 +202,0 @@ |
79
test.js
@@ -5,3 +5,3 @@ const test = require('brittle') | ||
test('basic read', function (t) { | ||
t.plan(2 * 4 + 4) | ||
t.plan(2 * 4 + 5) | ||
@@ -13,4 +13,4 @@ const expected = [Buffer.from('hi'), Buffer.from('ho')] | ||
process.nextTick(function () { | ||
t.alike(req.offset, 0) | ||
t.alike(req.size, 2) | ||
t.is(req.offset, 0) | ||
t.is(req.size, 2) | ||
req.callback(null, queued.shift()) | ||
@@ -24,2 +24,3 @@ }) | ||
t.absent(s.deletable) | ||
t.absent(s.truncatable) | ||
t.absent(s.statable) | ||
@@ -36,3 +37,3 @@ s.read(0, 2, ondata) | ||
test('basic write', function (t) { | ||
t.plan(2 * 2 + 4) | ||
t.plan(2 * 2 + 5) | ||
@@ -50,2 +51,3 @@ const expected = [Buffer.from('hi'), Buffer.from('ho')] | ||
t.absent(s.deletable) | ||
t.absent(s.truncatable) | ||
t.absent(s.statable) | ||
@@ -61,8 +63,8 @@ s.write(0, Buffer.from('hi'), onwrite) | ||
test('basic del', function (t) { | ||
t.plan(2 + 2 * 3 + 4) | ||
t.plan(2 + 2 * 3 + 5) | ||
const s = new RAS({ | ||
del: function (req) { | ||
t.alike(req.offset, 0) | ||
t.alike(req.size, 2) | ||
t.is(req.offset, 0) | ||
t.is(req.size, 2) | ||
req.callback(null) | ||
@@ -75,2 +77,3 @@ } | ||
t.ok(s.deletable) | ||
t.ok(s.truncatable) | ||
t.absent(s.statable) | ||
@@ -86,4 +89,54 @@ s.del(0, 2, ondelete) | ||
test('basic truncate', function (t) { | ||
t.plan(2 + 2 * 3 + 5) | ||
const s = new RAS({ | ||
truncate: function (req) { | ||
t.is(req.offset, 2) | ||
t.is(req.size, 0) | ||
req.callback(null) | ||
} | ||
}) | ||
t.absent(s.readable) | ||
t.absent(s.writable) | ||
t.absent(s.deletable) | ||
t.ok(s.truncatable) | ||
t.absent(s.statable) | ||
s.truncate(2, ontruncate) | ||
s.truncate(2, ontruncate) | ||
s.truncate(2) // cb is optional | ||
function ontruncate (err) { | ||
t.absent(err, 'no error') | ||
} | ||
}) | ||
test('basic truncate with del', function (t) { | ||
t.plan(2 + 2 * 3 + 5) | ||
const s = new RAS({ | ||
del: function (req) { | ||
t.is(req.offset, 2) | ||
t.is(req.size, Infinity) | ||
req.callback(null) | ||
} | ||
}) | ||
t.absent(s.readable) | ||
t.absent(s.writable) | ||
t.ok(s.deletable) | ||
t.ok(s.truncatable) | ||
t.absent(s.statable) | ||
s.truncate(2, ontruncate) | ||
s.truncate(2, ontruncate) | ||
s.truncate(2) // cb is optional | ||
function ontruncate (err) { | ||
t.absent(err, 'no error') | ||
} | ||
}) | ||
test('basic stat', function (t) { | ||
t.plan(2 * 2 + 4) | ||
t.plan(2 * 2 + 5) | ||
@@ -99,2 +152,3 @@ const s = new RAS({ | ||
t.absent(s.deletable) | ||
t.absent(s.truncatable) | ||
t.ok(s.statable) | ||
@@ -433,3 +487,3 @@ s.stat(onstat) | ||
t.ok(err) | ||
t.alike(err.message, 'Nope') | ||
t.is(err.message, 'Nope') | ||
}) | ||
@@ -439,3 +493,3 @@ | ||
t.ok(err) | ||
t.alike(err.message, 'Nope') | ||
t.is(err.message, 'Nope') | ||
}) | ||
@@ -484,2 +538,6 @@ | ||
_truncate (req) { | ||
req.callback(null) | ||
} | ||
_stat (req) { | ||
@@ -495,2 +553,3 @@ req.callback(null) | ||
t.ok(c.deletable) | ||
t.ok(c.truncatable) | ||
t.ok(c.statable) | ||
@@ -497,0 +556,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27271
732
242