Comparing version 2.0.6 to 3.0.0
193
index.js
@@ -5,6 +5,8 @@ /* global Bare */ | ||
const binding = require('./binding') | ||
const constants = require('./lib/constants') | ||
const errors = require('./lib/errors') | ||
const DEFAULT_READ_BUFFER = 65536 | ||
const defaultReadBufferSize = 65536 | ||
const Pipe = module.exports = class Pipe extends Duplex { | ||
const Pipe = module.exports = exports = class Pipe extends Duplex { | ||
constructor (path, opts = {}) { | ||
@@ -19,6 +21,10 @@ super({ mapWritable, eagerOpen: true }) | ||
const { | ||
readBufferSize = DEFAULT_READ_BUFFER, | ||
readBufferSize = defaultReadBufferSize, | ||
allowHalfOpen = true | ||
} = opts | ||
this._state = 0 | ||
this._allowHalfOpen = allowHalfOpen | ||
this._pendingOpen = null | ||
@@ -29,7 +35,2 @@ this._pendingWrite = null | ||
this._connected = typeof path !== 'string' | ||
this._reading = false | ||
this._closing = false | ||
this._allowHalfOpen = allowHalfOpen | ||
this._buffer = Buffer.alloc(readBufferSize) | ||
@@ -47,5 +48,5 @@ | ||
if (typeof path === 'number') { | ||
binding.open(this._handle, path) | ||
this.open(path) | ||
} else if (typeof path === 'string') { | ||
binding.connect(this._handle, path) | ||
this.connect(path) | ||
} | ||
@@ -56,2 +57,48 @@ | ||
get connecting () { | ||
return (this._state & constants.state.CONNECTING) !== 0 | ||
} | ||
get pending () { | ||
return (this._state & constants.state.CONNECTED) === 0 | ||
} | ||
open (fd, opts = {}, onconnect) { | ||
if (typeof fd !== 'number') { | ||
opts = fd || {} | ||
fd = opts.fd | ||
} else if (typeof opts === 'function') { | ||
onconnect = opts | ||
opts = {} | ||
} | ||
binding.open(this._handle, fd) | ||
this._state |= constants.state.CONNECTED | ||
if (onconnect) this.once('connect', onconnect) | ||
queueMicrotask(() => this.emit('connect')) | ||
return this | ||
} | ||
connect (path, opts = {}, onconnect) { | ||
if (typeof path !== 'string') { | ||
opts = path || {} | ||
path = opts.path | ||
} else if (typeof opts === 'function') { | ||
onconnect = opts | ||
opts = {} | ||
} | ||
binding.connect(this._handle, path) | ||
this._state |= constants.state.CONNECTING | ||
if (onconnect) this.once('connect', onconnect) | ||
return this | ||
} | ||
ref () { | ||
@@ -65,16 +112,13 @@ binding.ref(this._handle) | ||
static createServer (opts) { | ||
return new PipeServer(opts) | ||
} | ||
_open (cb) { | ||
if (this._state & constants.state.CONNECTED) return cb(null) | ||
this._pendingOpen = cb | ||
this._continueOpen(null) | ||
} | ||
_read (cb) { | ||
if (!this._reading) { | ||
this._reading = true | ||
if ((this._state & constants.state.READING) === 0) { | ||
this._state |= constants.state.READING | ||
binding.resume(this._handle) | ||
} | ||
cb(null) | ||
@@ -94,4 +138,4 @@ } | ||
_predestroy () { | ||
if (this._closing) return | ||
this._closing = true | ||
if (this._state & constants.state.CLOSING) return | ||
this._state |= constants.state.CLOSING | ||
binding.close(this._handle) | ||
@@ -102,4 +146,4 @@ Pipe._pipes.delete(this) | ||
_destroy (cb) { | ||
if (this._closing) return cb(null) | ||
this._closing = true | ||
if (this._state & constants.state.CLOSING) return cb(null) | ||
this._state |= constants.state.CLOSING | ||
this._pendingDestroy = cb | ||
@@ -111,3 +155,2 @@ binding.close(this._handle) | ||
_continueOpen (err) { | ||
if (!this._connected) return | ||
if (this._pendingOpen === null) return | ||
@@ -141,9 +184,12 @@ const cb = this._pendingOpen | ||
_onconnect (err) { | ||
if (!err) this.emit('connect') | ||
this._connected = true | ||
this._continueOpen(err) | ||
} | ||
if (err) { | ||
this.destroy(err) | ||
return | ||
} | ||
_onwrite (err) { | ||
this._continueWrite(err) | ||
this._state |= constants.state.CONNECTED | ||
this._state &= ~constants.state.CONNECTING | ||
this._continueOpen() | ||
this.emit('connect') | ||
} | ||
@@ -167,3 +213,3 @@ | ||
if (this.push(copy) === false && this.destroying === false) { | ||
this._reading = false | ||
this._state &= ~constants.state.READING | ||
binding.pause(this._handle) | ||
@@ -173,2 +219,6 @@ } | ||
_onwrite (err) { | ||
this._continueWrite(err) | ||
} | ||
_onfinal (err) { | ||
@@ -186,14 +236,23 @@ this._continueFinal(err) | ||
class PipeServer extends EventEmitter { | ||
constructor (opts = {}) { | ||
const Server = exports.Server = class PipeServer extends EventEmitter { | ||
constructor (opts = {}, onconnection) { | ||
if (typeof opts === 'function') { | ||
onconnection = opts | ||
opts = {} | ||
} | ||
super() | ||
const { | ||
readBufferSize = DEFAULT_READ_BUFFER, | ||
readBufferSize = defaultReadBufferSize, | ||
allowHalfOpen = true | ||
} = opts | ||
this._state = 0 | ||
this._readBufferSize = readBufferSize | ||
this._allowHalfOpen = allowHalfOpen | ||
this._connections = new Set() | ||
this._handle = binding.init(empty, this, | ||
@@ -208,5 +267,3 @@ this._onconnection, | ||
this.listening = false | ||
this.closing = false | ||
this.connections = new Set() | ||
if (onconnection) this.on('connection', onconnection) | ||
@@ -216,8 +273,30 @@ PipeServer._servers.add(this) | ||
listen (name, backlog = 511) { | ||
if (this.listening) throw new Error('Server is already bound') | ||
if (this.closing) throw new Error('Server is closed') | ||
get listening () { | ||
return (this._state & constants.state.LISTENING) !== 0 | ||
} | ||
binding.bind(this._handle, name, backlog) | ||
listen (name, backlog = 511, onlistening) { | ||
if (this._state & constants.state.CLOSING) { | ||
throw errors.SERVER_IS_CLOSED('Server is closed') | ||
} | ||
if (typeof backlog === 'function') { | ||
onlistening = backlog | ||
backlog = 511 | ||
} | ||
try { | ||
binding.bind(this._handle, name, backlog) | ||
this._state |= constants.state.LISTENING | ||
if (onlistening) this.once('listening', onlistening) | ||
queueMicrotask(() => this.emit('listening')) | ||
} catch (err) { | ||
queueMicrotask(() => { | ||
if ((this._state & constants.state.CLOSING) === 0) this.emit('error', err) | ||
}) | ||
} | ||
return this | ||
@@ -228,5 +307,4 @@ } | ||
if (onclose) this.once('close', onclose) | ||
if (this.closing) return | ||
this.closing = true | ||
if (this._state & constants.state.CLOSING) return | ||
this._state |= constants.state.CLOSING | ||
this._closeMaybe() | ||
@@ -244,3 +322,3 @@ } | ||
_closeMaybe () { | ||
if (this.closing && this.connections.size === 0) { | ||
if ((this._state & constants.state.CLOSING) && this._connections.size === 0) { | ||
binding.close(this._handle) | ||
@@ -257,3 +335,3 @@ PipeServer._servers.delete(this) | ||
if (this.closing) return | ||
if (this._state & constants.state.CLOSING) return | ||
@@ -268,6 +346,8 @@ const pipe = new Pipe({ | ||
this.connections.add(pipe) | ||
pipe._state |= constants.state.CONNECTED | ||
this._connections.add(pipe) | ||
pipe.on('close', () => { | ||
this.connections.delete(pipe) | ||
this._connections.delete(pipe) | ||
this._closeMaybe() | ||
@@ -292,2 +372,21 @@ }) | ||
exports.constants = constants | ||
exports.errors = errors | ||
exports.createConnection = function createConnection (path, opts, onconnect) { | ||
if (typeof path !== 'string') { | ||
opts = path || {} | ||
path = opts.path | ||
} else if (typeof opts === 'function') { | ||
onconnect = opts | ||
opts = {} | ||
} | ||
return new Pipe(opts).connect(path, opts, onconnect) | ||
} | ||
exports.createServer = function createServer (opts, onconnection) { | ||
return new Server(opts, onconnection) | ||
} | ||
Bare | ||
@@ -299,3 +398,3 @@ .on('exit', () => { | ||
for (const server of PipeServer._servers) { | ||
for (const server of Server._servers) { | ||
server.close() | ||
@@ -302,0 +401,0 @@ } |
{ | ||
"name": "bare-pipe", | ||
"version": "2.0.6", | ||
"version": "3.0.0", | ||
"description": "Native I/O pipes for JavaScript", | ||
"main": "index.js", | ||
"exports": { | ||
".": "./index.js", | ||
"./package": "./package.json", | ||
"./constants": "./lib/constants.js", | ||
"./errors": "./lib/errors.js" | ||
}, | ||
"files": [ | ||
@@ -11,2 +16,3 @@ "index.js", | ||
"CMakeLists.txt", | ||
"lib", | ||
"prebuilds" | ||
@@ -13,0 +19,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
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 5 instances 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
311074
14
325
5