Comparing version 3.2.1 to 3.3.0
141
index.js
@@ -28,2 +28,5 @@ /* global Bare */ | ||
this._fd = -1 | ||
this._path = null | ||
this._pendingOpen = null | ||
@@ -62,2 +65,18 @@ this._pendingWrite = null | ||
get readyState () { | ||
if (this._state & constants.state.READABLE && this._state & constants.state.WRITABLE) { | ||
return 'open' | ||
} | ||
if (this._state & constants.state.READABLE) { | ||
return 'readOnly' | ||
} | ||
if (this._state & constants.state.WRITABLE) { | ||
return 'writeOnly' | ||
} | ||
return 'opening' | ||
} | ||
open (fd, opts = {}, onconnect) { | ||
@@ -74,10 +93,27 @@ if (typeof opts === 'function') { | ||
binding.open(this._handle, fd) | ||
try { | ||
const status = binding.open(this._handle, fd) | ||
this._state |= constants.state.CONNECTED | ||
this._state |= constants.state.CONNECTED | ||
this._fd = fd | ||
if (onconnect) this.once('connect', onconnect) | ||
if (status & binding.READABLE) { | ||
this._state |= constants.state.READABLE | ||
} else { | ||
this.push(null) | ||
} | ||
queueMicrotask(() => this.emit('connect')) | ||
if (status & binding.WRITABLE) { | ||
this._state |= constants.state.WRITABLE | ||
} else { | ||
this.end() | ||
} | ||
if (onconnect) this.once('connect', onconnect) | ||
queueMicrotask(() => this.emit('connect')) | ||
} catch (err) { | ||
queueMicrotask(() => this.destroy(err)) | ||
} | ||
return this | ||
@@ -87,2 +123,8 @@ } | ||
connect (path, opts = {}, onconnect) { | ||
if (this._state & constants.state.CONNECTING || this._state & constants.state.CONNECTED) { | ||
throw errors.PIPE_ALREADY_CONNECTED('Pipe is already connected') | ||
} | ||
this._state |= constants.state.CONNECTING | ||
if (typeof opts === 'function') { | ||
@@ -98,7 +140,11 @@ onconnect = opts | ||
binding.connect(this._handle, path) | ||
try { | ||
binding.connect(this._handle, path) | ||
this._state |= constants.state.CONNECTING | ||
this._path = path | ||
if (onconnect) this.once('connect', onconnect) | ||
if (onconnect) this.once('connect', onconnect) | ||
} catch (err) { | ||
queueMicrotask(() => this.destroy(err)) | ||
} | ||
@@ -134,4 +180,8 @@ return this | ||
_final (cb) { | ||
this._pendingFinal = cb | ||
binding.end(this._handle) | ||
if (this._state & constants.state.READABLE && this._state & constants.state.WRITABLE) { | ||
this._pendingFinal = cb | ||
binding.end(this._handle) | ||
} else { | ||
cb(null) | ||
} | ||
} | ||
@@ -189,3 +239,3 @@ | ||
this._state |= constants.state.CONNECTED | ||
this._state |= constants.state.CONNECTED | constants.state.READABLE | constants.state.WRITABLE | ||
this._state &= ~constants.state.CONNECTING | ||
@@ -231,5 +281,18 @@ this._continueOpen() | ||
_onspawn (readable, writable) { | ||
this._state |= constants.state.CONNECTED | ||
if (readable) this._state |= constants.state.READABLE | ||
if (writable) this._state |= constants.state.WRITABLE | ||
} | ||
static _pipes = new Set() | ||
} | ||
exports.Pipe = exports | ||
exports.pipe = function pipe () { | ||
return binding.pipe() | ||
} | ||
const Server = exports.Server = class PipeServer extends EventEmitter { | ||
@@ -257,10 +320,4 @@ constructor (opts = {}, onconnection) { | ||
this._handle = binding.init(empty, this, | ||
this._onconnection, | ||
noop, | ||
noop, | ||
noop, | ||
noop, | ||
this._onclose | ||
) | ||
this._error = null | ||
this._handle = null | ||
@@ -273,7 +330,7 @@ if (onconnection) this.on('connection', onconnection) | ||
get listening () { | ||
return (this._state & constants.state.LISTENING) !== 0 | ||
return (this._state & constants.state.BOUND) !== 0 | ||
} | ||
address () { | ||
if ((this._state & constants.state.LISTENING) === 0) { | ||
if ((this._state & constants.state.BOUND) === 0) { | ||
return null | ||
@@ -286,2 +343,6 @@ } | ||
listen (path, backlog = 511, opts = {}, onlistening) { | ||
if (this._state & constants.state.BINDING || this._state & constants.state.BOUND) { | ||
throw errors.SERVER_ALREADY_LISTENING('Server is already listening') | ||
} | ||
if (this._state & constants.state.CLOSING) { | ||
@@ -291,2 +352,4 @@ throw errors.SERVER_IS_CLOSED('Server is closed') | ||
this._state |= constants.state.BINDING | ||
if (typeof backlog === 'function') { | ||
@@ -306,2 +369,13 @@ onlistening = backlog | ||
this._handle = binding.init(empty, this, | ||
this._onconnection, | ||
noop, | ||
noop, | ||
noop, | ||
noop, | ||
this._onclose | ||
) | ||
if (this._state & constants.state.UNREFED) binding.unref(this._handle) | ||
try { | ||
@@ -311,3 +385,4 @@ binding.bind(this._handle, path, backlog) | ||
this._path = path | ||
this._state |= constants.state.LISTENING | ||
this._state |= constants.state.BOUND | ||
this._state &= ~constants.state.BINDING | ||
@@ -318,5 +393,5 @@ if (onlistening) this.once('listening', onlistening) | ||
} catch (err) { | ||
queueMicrotask(() => { | ||
if ((this._state & constants.state.CLOSING) === 0) this.emit('error', err) | ||
}) | ||
this._error = err | ||
binding.close(this._handle) | ||
} | ||
@@ -335,7 +410,9 @@ | ||
ref () { | ||
binding.ref(this._handle) | ||
this._state &= ~constants.state.UNREFED | ||
if (this._handle !== null)binding.ref(this._handle) | ||
} | ||
unref () { | ||
binding.unref(this._handle) | ||
this._state |= constants.state.UNREFED | ||
if (this._handle !== null)binding.unref(this._handle) | ||
} | ||
@@ -345,3 +422,4 @@ | ||
if ((this._state & constants.state.CLOSING) && this._connections.size === 0) { | ||
binding.close(this._handle) | ||
if (this._handle !== null)binding.close(this._handle) | ||
else queueMicrotask(() => this.emit('close')) | ||
PipeServer._servers.delete(this) | ||
@@ -368,2 +446,3 @@ } | ||
pipe._state |= constants.state.CONNECTED | ||
pipe._path = this._path | ||
@@ -386,4 +465,10 @@ this._connections.add(pipe) | ||
_onclose () { | ||
const err = this._error | ||
this._state &= ~constants.state.BINDING | ||
this._error = null | ||
this._handle = null | ||
this.emit('close') | ||
if (err) this.emit('error', err) | ||
else this.emit('close') | ||
} | ||
@@ -390,0 +475,0 @@ |
@@ -5,6 +5,9 @@ module.exports = { | ||
CONNECTED: 0x2, | ||
LISTENING: 0x4, | ||
READING: 0x8, | ||
CLOSING: 0x10 | ||
BINDING: 0x4, | ||
BOUND: 0x8, | ||
READING: 0x10, | ||
CLOSING: 0x20, | ||
READABLE: 0x40, | ||
WRITABLE: 0x80 | ||
} | ||
} |
@@ -15,2 +15,10 @@ module.exports = class PipeError extends Error { | ||
static PIPE_ALREADY_CONNECTED (msg) { | ||
return new PipeError(msg, 'PIPE_ALREADY_CONNECTED', PipeError.PIPE_ALREADY_CONNECTED) | ||
} | ||
static SERVER_ALREADY_LISTENING (msg) { | ||
return new PipeError(msg, 'SERVER_ALREADY_LISTENING', PipeError.SERVER_ALREADY_LISTENING) | ||
} | ||
static SERVER_IS_CLOSED (msg) { | ||
@@ -17,0 +25,0 @@ return new PipeError(msg, 'SERVER_IS_CLOSED', PipeError.SERVER_IS_CLOSED) |
{ | ||
"name": "bare-pipe", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "Native I/O pipes for JavaScript", | ||
@@ -5,0 +5,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
312861
415