Comparing version 3.3.8 to 4.0.0
165
index.js
@@ -10,4 +10,4 @@ /* global Bare */ | ||
const Pipe = module.exports = exports = class Pipe extends Duplex { | ||
constructor (path, opts = {}) { | ||
module.exports = exports = class Pipe extends Duplex { | ||
constructor(path, opts = {}) { | ||
super({ eagerOpen: true }) | ||
@@ -20,6 +20,4 @@ | ||
const { | ||
readBufferSize = defaultReadBufferSize, | ||
allowHalfOpen = true | ||
} = opts | ||
const { readBufferSize = defaultReadBufferSize, allowHalfOpen = true } = | ||
opts | ||
@@ -40,3 +38,5 @@ this._state = 0 | ||
this._handle = binding.init(this._buffer, this, | ||
this._handle = binding.init( | ||
this._buffer, | ||
this, | ||
noop, | ||
@@ -55,16 +55,17 @@ this._onconnect, | ||
} | ||
Pipe._pipes.add(this) | ||
} | ||
get connecting () { | ||
get connecting() { | ||
return (this._state & constants.state.CONNECTING) !== 0 | ||
} | ||
get pending () { | ||
get pending() { | ||
return (this._state & constants.state.CONNECTED) === 0 | ||
} | ||
get readyState () { | ||
if (this._state & constants.state.READABLE && this._state & constants.state.WRITABLE) { | ||
get readyState() { | ||
if ( | ||
this._state & constants.state.READABLE && | ||
this._state & constants.state.WRITABLE | ||
) { | ||
return 'open' | ||
@@ -84,3 +85,3 @@ } | ||
open (fd, opts = {}, onconnect) { | ||
open(fd, opts = {}, onconnect) { | ||
if (typeof opts === 'function') { | ||
@@ -127,4 +128,7 @@ onconnect = opts | ||
connect (path, opts = {}, onconnect) { | ||
if (this._state & constants.state.CONNECTING || this._state & constants.state.CONNECTED) { | ||
connect(path, opts = {}, onconnect) { | ||
if ( | ||
this._state & constants.state.CONNECTING || | ||
this._state & constants.state.CONNECTED | ||
) { | ||
throw errors.PIPE_ALREADY_CONNECTED('Pipe is already connected') | ||
@@ -161,11 +165,11 @@ } | ||
ref () { | ||
ref() { | ||
binding.ref(this._handle) | ||
} | ||
unref () { | ||
unref() { | ||
binding.unref(this._handle) | ||
} | ||
_open (cb) { | ||
_open(cb) { | ||
if (this._state & constants.state.CONNECTED) return cb(null) | ||
@@ -175,3 +179,3 @@ this._pendingOpen = cb | ||
_read () { | ||
_read() { | ||
if ((this._state & constants.state.READING) === 0) { | ||
@@ -183,9 +187,15 @@ this._state |= constants.state.READING | ||
_writev (batch, cb) { | ||
_writev(batch, cb) { | ||
this._pendingWrite = [cb, batch] | ||
binding.writev(this._handle, batch.map(({ chunk }) => chunk)) | ||
binding.writev( | ||
this._handle, | ||
batch.map(({ chunk }) => chunk) | ||
) | ||
} | ||
_final (cb) { | ||
if (this._state & constants.state.READABLE && this._state & constants.state.WRITABLE) { | ||
_final(cb) { | ||
if ( | ||
this._state & constants.state.READABLE && | ||
this._state & constants.state.WRITABLE | ||
) { | ||
this._pendingFinal = cb | ||
@@ -198,10 +208,9 @@ binding.end(this._handle) | ||
_predestroy () { | ||
_predestroy() { | ||
if (this._state & constants.state.CLOSING) return | ||
this._state |= constants.state.CLOSING | ||
binding.close(this._handle) | ||
Pipe._pipes.delete(this) | ||
} | ||
_destroy (err, cb) { | ||
_destroy(err, cb) { | ||
if (this._state & constants.state.CLOSING) return cb(err) | ||
@@ -211,6 +220,5 @@ this._state |= constants.state.CLOSING | ||
binding.close(this._handle) | ||
Pipe._pipes.delete(this) | ||
} | ||
_continueOpen (err) { | ||
_continueOpen(err) { | ||
if (this._pendingOpen === null) return | ||
@@ -222,3 +230,3 @@ const cb = this._pendingOpen | ||
_continueWrite (err) { | ||
_continueWrite(err) { | ||
if (this._pendingWrite === null) return | ||
@@ -230,3 +238,3 @@ const cb = this._pendingWrite[0] | ||
_continueFinal (err) { | ||
_continueFinal(err) { | ||
if (this._pendingFinal === null) return | ||
@@ -238,3 +246,3 @@ const cb = this._pendingFinal | ||
_continueDestroy () { | ||
_continueDestroy() { | ||
if (this._pendingDestroy === null) return | ||
@@ -246,3 +254,3 @@ const cb = this._pendingDestroy | ||
_onconnect (err) { | ||
_onconnect(err) { | ||
if (err) { | ||
@@ -254,3 +262,6 @@ if (this._pendingOpen) this._continueOpen(err) | ||
this._state |= constants.state.CONNECTED | constants.state.READABLE | constants.state.WRITABLE | ||
this._state |= | ||
constants.state.CONNECTED | | ||
constants.state.READABLE | | ||
constants.state.WRITABLE | ||
this._state &= ~constants.state.CONNECTING | ||
@@ -262,3 +273,3 @@ this._continueOpen() | ||
_onread (err, read) { | ||
_onread(err, read) { | ||
if (err) { | ||
@@ -284,11 +295,11 @@ this.destroy(err) | ||
_onwrite (err) { | ||
_onwrite(err) { | ||
this._continueWrite(err) | ||
} | ||
_onfinal (err) { | ||
_onfinal(err) { | ||
this._continueFinal(err) | ||
} | ||
_onclose () { | ||
_onclose() { | ||
this._handle = null | ||
@@ -298,3 +309,3 @@ this._continueDestroy() | ||
_onspawn (readable, writable) { | ||
_onspawn(readable, writable) { | ||
this._state |= constants.state.CONNECTED | ||
@@ -316,4 +327,2 @@ | ||
} | ||
static _pipes = new Set() | ||
} | ||
@@ -323,8 +332,8 @@ | ||
exports.pipe = function pipe () { | ||
exports.pipe = function pipe() { | ||
return binding.pipe() | ||
} | ||
const Server = exports.Server = class PipeServer extends EventEmitter { | ||
constructor (opts = {}, onconnection) { | ||
exports.Server = class PipeServer extends EventEmitter { | ||
constructor(opts = {}, onconnection) { | ||
if (typeof opts === 'function') { | ||
@@ -337,6 +346,4 @@ onconnection = opts | ||
const { | ||
readBufferSize = defaultReadBufferSize, | ||
allowHalfOpen = true | ||
} = opts | ||
const { readBufferSize = defaultReadBufferSize, allowHalfOpen = true } = | ||
opts | ||
@@ -355,11 +362,9 @@ this._state = 0 | ||
if (onconnection) this.on('connection', onconnection) | ||
PipeServer._servers.add(this) | ||
} | ||
get listening () { | ||
get listening() { | ||
return (this._state & constants.state.BOUND) !== 0 | ||
} | ||
address () { | ||
address() { | ||
if ((this._state & constants.state.BOUND) === 0) { | ||
@@ -372,4 +377,7 @@ return null | ||
listen (path, backlog = 511, opts = {}, onlistening) { | ||
if (this._state & constants.state.BINDING || this._state & constants.state.BOUND) { | ||
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') | ||
@@ -398,3 +406,5 @@ } | ||
this._handle = binding.init(empty, this, | ||
this._handle = binding.init( | ||
empty, | ||
this, | ||
this._onconnection, | ||
@@ -429,3 +439,3 @@ noop, | ||
close (onclose) { | ||
close(onclose) { | ||
if (onclose) this.once('close', onclose) | ||
@@ -437,3 +447,3 @@ if (this._state & constants.state.CLOSING) return | ||
ref () { | ||
ref() { | ||
this._state &= ~constants.state.UNREFED | ||
@@ -443,3 +453,3 @@ if (this._handle !== null) binding.ref(this._handle) | ||
unref () { | ||
unref() { | ||
this._state |= constants.state.UNREFED | ||
@@ -449,11 +459,10 @@ if (this._handle !== null) binding.unref(this._handle) | ||
_closeMaybe () { | ||
if ((this._state & constants.state.CLOSING) && this._connections.size === 0) { | ||
_closeMaybe() { | ||
if (this._state & constants.state.CLOSING && this._connections.size === 0) { | ||
if (this._handle !== null) binding.close(this._handle) | ||
else queueMicrotask(() => this.emit('close')) | ||
PipeServer._servers.delete(this) | ||
} | ||
} | ||
_onconnection (err) { | ||
_onconnection(err) { | ||
if (err) { | ||
@@ -466,3 +475,3 @@ this.emit('error', err) | ||
const pipe = new Pipe({ | ||
const pipe = new exports.Pipe({ | ||
readBufferSize: this._readBufferSize, | ||
@@ -476,3 +485,6 @@ allowHalfOpen: this._allowHalfOpen | ||
pipe._path = this._path | ||
pipe._state |= constants.state.CONNECTED | constants.state.READABLE | constants.state.WRITABLE | ||
pipe._state |= | ||
constants.state.CONNECTED | | ||
constants.state.READABLE | | ||
constants.state.WRITABLE | ||
@@ -494,3 +506,3 @@ this._connections.add(pipe) | ||
_onclose () { | ||
_onclose() { | ||
const err = this._error | ||
@@ -505,4 +517,2 @@ | ||
} | ||
static _servers = new Set() | ||
} | ||
@@ -513,3 +523,3 @@ | ||
exports.createConnection = function createConnection (path, opts, onconnect) { | ||
exports.createConnection = function createConnection(path, opts, onconnect) { | ||
if (typeof opts === 'function') { | ||
@@ -525,22 +535,11 @@ onconnect = opts | ||
return new Pipe(opts).connect(path, opts, onconnect) | ||
return new exports.Pipe(opts).connect(path, opts, onconnect) | ||
} | ||
exports.createServer = function createServer (opts, onconnection) { | ||
return new Server(opts, onconnection) | ||
exports.createServer = function createServer(opts, onconnection) { | ||
return new exports.Server(opts, onconnection) | ||
} | ||
Bare | ||
.on('exit', () => { | ||
for (const pipe of Pipe._pipes) { | ||
pipe.destroy() | ||
} | ||
for (const server of Server._servers) { | ||
server.close() | ||
} | ||
}) | ||
const empty = Buffer.alloc(0) | ||
function noop () {} | ||
function noop() {} |
module.exports = class PipeError extends Error { | ||
constructor (msg, code, fn = PipeError) { | ||
constructor(msg, code, fn = PipeError) { | ||
super(`${code}: ${msg}`) | ||
@@ -11,17 +11,25 @@ this.code = code | ||
get name () { | ||
get name() { | ||
return 'PipeError' | ||
} | ||
static PIPE_ALREADY_CONNECTED (msg) { | ||
return new PipeError(msg, 'PIPE_ALREADY_CONNECTED', PipeError.PIPE_ALREADY_CONNECTED) | ||
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_ALREADY_LISTENING(msg) { | ||
return new PipeError( | ||
msg, | ||
'SERVER_ALREADY_LISTENING', | ||
PipeError.SERVER_ALREADY_LISTENING | ||
) | ||
} | ||
static SERVER_IS_CLOSED (msg) { | ||
static SERVER_IS_CLOSED(msg) { | ||
return new PipeError(msg, 'SERVER_IS_CLOSED', PipeError.SERVER_IS_CLOSED) | ||
} | ||
} |
{ | ||
"name": "bare-pipe", | ||
"version": "3.3.8", | ||
"version": "4.0.0", | ||
"description": "Native I/O pipes for JavaScript", | ||
@@ -21,3 +21,3 @@ "exports": { | ||
"scripts": { | ||
"test": "standard && bare test.js" | ||
"test": "prettier . --check && bare test.js" | ||
}, | ||
@@ -34,2 +34,5 @@ "repository": { | ||
"homepage": "https://github.com/holepunchto/bare-pipe#readme", | ||
"engines": { | ||
"bare": ">=1.7.0" | ||
}, | ||
"dependencies": { | ||
@@ -43,4 +46,5 @@ "bare-events": "^2.0.0", | ||
"cmake-bare": "^1.1.6", | ||
"standard": "^17.0.0" | ||
"prettier": "^3.4.1", | ||
"prettier-config-standard": "^7.0.0" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # bare-pipe | ||
``` js | ||
```js | ||
const Pipe = require('bare-pipe') | ||
@@ -14,0 +14,0 @@ |
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
547520
443
5