fs-minipass
Advanced tools
Comparing version 2.0.1 to 2.1.0
76
index.js
@@ -49,2 +49,3 @@ 'use strict' | ||
const _defaultFlag = Symbol('_defaultFlag') | ||
const _errored = Symbol('_errored') | ||
@@ -56,2 +57,3 @@ class ReadStream extends MiniPass { | ||
this.readable = true | ||
this.writable = false | ||
@@ -62,2 +64,3 @@ | ||
this[_errored] = false | ||
this[_fd] = typeof opt.fd === 'number' ? opt.fd : null | ||
@@ -112,3 +115,4 @@ this[_path] = path | ||
/* istanbul ignore if */ | ||
if (buf.length === 0) return process.nextTick(() => this[_onread](null, 0, buf)) | ||
if (buf.length === 0) | ||
return process.nextTick(() => this[_onread](null, 0, buf)) | ||
fs.read(this[_fd], buf, 0, buf.length, null, (er, br, buf) => | ||
@@ -129,4 +133,5 @@ this[_onread](er, br, buf)) | ||
if (this[_autoClose] && typeof this[_fd] === 'number') { | ||
fs.close(this[_fd], _ => this.emit('close')) | ||
const fd = this[_fd] | ||
this[_fd] = null | ||
fs.close(fd, er => er ? this.emit('error', er) : this.emit('close')) | ||
} | ||
@@ -168,2 +173,8 @@ } | ||
case 'error': | ||
if (this[_errored]) | ||
return | ||
this[_errored] = true | ||
return super.emit(ev, data) | ||
default: | ||
@@ -195,3 +206,4 @@ return super.emit(ev, data) | ||
/* istanbul ignore next */ | ||
const br = buf.length === 0 ? 0 : fs.readSync(this[_fd], buf, 0, buf.length, null) | ||
const br = buf.length === 0 ? 0 | ||
: fs.readSync(this[_fd], buf, 0, buf.length, null) | ||
if (!this[_handleChunk](br, buf)) | ||
@@ -211,6 +223,5 @@ break | ||
if (this[_autoClose] && typeof this[_fd] === 'number') { | ||
try { | ||
fs.closeSync(this[_fd]) | ||
} catch (er) {} | ||
const fd = this[_fd] | ||
this[_fd] = null | ||
fs.closeSync(fd) | ||
this.emit('close') | ||
@@ -226,2 +237,4 @@ } | ||
this.readable = false | ||
this.writable = true | ||
this[_errored] = false | ||
this[_writing] = false | ||
@@ -247,2 +260,12 @@ this[_ended] = false | ||
emit (ev, data) { | ||
if (ev === 'error') { | ||
if (this[_errored]) | ||
return | ||
this[_errored] = true | ||
} | ||
return super.emit(ev, data) | ||
} | ||
get fd () { return this[_fd] } | ||
@@ -287,2 +310,3 @@ get path () { return this[_path] } | ||
this[_onwrite](null, 0) | ||
return this | ||
} | ||
@@ -354,4 +378,5 @@ | ||
if (this[_autoClose] && typeof this[_fd] === 'number') { | ||
fs.close(this[_fd], _ => this.emit('close')) | ||
const fd = this[_fd] | ||
this[_fd] = null | ||
fs.close(fd, er => er ? this.emit('error', er) : this.emit('close')) | ||
} | ||
@@ -364,13 +389,17 @@ } | ||
let fd | ||
try { | ||
// only wrap in a try{} block if we know we'll retry, to avoid | ||
// the rethrow obscuring the error's source frame in most cases. | ||
if (this[_defaultFlag] && this[_flags] === 'r+') { | ||
try { | ||
fd = fs.openSync(this[_path], this[_flags], this[_mode]) | ||
} catch (er) { | ||
if (er.code === 'ENOENT') { | ||
this[_flags] = 'w' | ||
return this[_open]() | ||
} else | ||
throw er | ||
} | ||
} else | ||
fd = fs.openSync(this[_path], this[_flags], this[_mode]) | ||
} catch (er) { | ||
if (this[_defaultFlag] && | ||
this[_flags] === 'r+' && | ||
er && er.code === 'ENOENT') { | ||
this[_flags] = 'w' | ||
return this[_open]() | ||
} else | ||
throw er | ||
} | ||
this[_onopen](null, fd) | ||
@@ -381,6 +410,5 @@ } | ||
if (this[_autoClose] && typeof this[_fd] === 'number') { | ||
try { | ||
fs.closeSync(this[_fd]) | ||
} catch (er) {} | ||
const fd = this[_fd] | ||
this[_fd] = null | ||
fs.closeSync(fd) | ||
this.emit('close') | ||
@@ -391,7 +419,11 @@ } | ||
[_write] (buf) { | ||
// throw the original, but try to close if it fails | ||
let threw = true | ||
try { | ||
this[_onwrite](null, | ||
fs.writeSync(this[_fd], buf, 0, buf.length, this[_pos])) | ||
} catch (er) { | ||
this[_onwrite](er, 0) | ||
threw = false | ||
} finally { | ||
if (threw) | ||
try { this[_close]() } catch (_) {} | ||
} | ||
@@ -398,0 +430,0 @@ } |
{ | ||
"name": "fs-minipass", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14089
367
0