random-access-file
Advanced tools
Comparing version 3.2.2 to 4.0.0
55
index.js
@@ -23,2 +23,27 @@ const RandomAccessStorage = require('random-access-storage') | ||
class Pool { | ||
constructor (maxSize) { | ||
this.maxSize = maxSize | ||
this.active = [] | ||
} | ||
_onactive (file) { | ||
// suspend a random one when the pool | ||
if (this.active.length >= this.maxSize) { | ||
const r = Math.floor(Math.random() * this.active.length) | ||
this.active[r].suspend() | ||
} | ||
file._pi = this.active.push(file) - 1 | ||
} | ||
_oninactive (file) { | ||
const head = this.active.pop() | ||
if (head !== file) { | ||
head._pi = file._pi | ||
this.active[head._pi] = head | ||
} | ||
} | ||
} | ||
module.exports = class RandomAccessFile extends RandomAccessStorage { | ||
@@ -28,3 +53,3 @@ constructor (filename, opts = {}) { | ||
super({ createAlways: size >= 0 }) | ||
super() | ||
@@ -44,2 +69,4 @@ if (opts.directory) filename = path.join(opts.directory, path.resolve('/', filename).replace(/^\w+:\\/, '')) | ||
this._pi = 0 // pool index | ||
this._pool = opts.pool || null | ||
this._size = size | ||
@@ -50,9 +77,15 @@ this._rmdir = !!opts.rmdir | ||
this._alloc = opts.alloc || Buffer.allocUnsafe | ||
this._alwaysCreate = size >= 0 | ||
} | ||
static createPool (maxSize) { | ||
return new Pool(maxSize) | ||
} | ||
_open (req) { | ||
const create = this._alwaysCreate || this.writing // .writing comes from RAS | ||
const self = this | ||
const mode = this.mode | (req.create ? CREAT : 0) | ||
const mode = this.mode | (create ? CREAT : 0) | ||
if (req.create) fs.mkdir(path.dirname(this.filename), { recursive: true }, ondir) | ||
if (create) fs.mkdir(path.dirname(this.filename), { recursive: true }, ondir) | ||
else ondir(null) | ||
@@ -62,5 +95,3 @@ | ||
if (err) return req.callback(err) | ||
if (self.fd) fs.close(self.fd, oncloseold) | ||
else fs.open(self.filename, mode, onopen) | ||
fs.open(self.filename, mode, onopen) | ||
} | ||
@@ -98,12 +129,5 @@ | ||
function oncloseold (err) { | ||
if (err) return onerrorafteropen(err) | ||
self.fd = 0 | ||
fs.open(self.filename, mode, onopen) | ||
} | ||
function ontruncate (err) { | ||
if (err) return onerrorafteropen(err) | ||
if (self._pool !== null) self._pool._onactive(self) | ||
req.callback(null) | ||
@@ -196,2 +220,3 @@ } | ||
if (err) return req.callback(err) | ||
if (self._pool !== null) self._pool._oninactive(self) | ||
self.fd = 0 | ||
@@ -202,3 +227,3 @@ req.callback(null) | ||
_destroy (req) { | ||
_unlink (req) { | ||
const self = this | ||
@@ -205,0 +230,0 @@ |
{ | ||
"name": "random-access-file", | ||
"version": "3.2.2", | ||
"version": "4.0.0", | ||
"description": "Continuous reading or writing to a file using random offsets and lengths", | ||
@@ -15,3 +15,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"random-access-storage": "^2.2.0" | ||
"random-access-storage": "^3.0.0" | ||
}, | ||
@@ -18,0 +18,0 @@ "optionalDependencies": { |
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
9628
202
+ Addedbare-events@2.5.0(transitive)
+ Addedrandom-access-storage@3.0.2(transitive)
- Removedevents@3.3.0(transitive)
- Removedrandom-access-storage@2.3.0(transitive)
Updatedrandom-access-storage@^3.0.0