Comparing version 1.1.2 to 1.2.0
153
index.js
@@ -0,1 +1,2 @@ | ||
const EventEmitter = require('events') | ||
const { Duplex } = require('streamx') | ||
@@ -6,3 +7,3 @@ const binding = require('./binding') | ||
module.exports = class Pipe extends Duplex { | ||
const Pipe = module.exports = class Pipe extends Duplex { | ||
constructor (path, opts = {}) { | ||
@@ -21,19 +22,15 @@ super({ mapWritable }) | ||
const slab = Buffer.alloc(binding.sizeofPipe + binding.sizeofWrite + readBufferSize) | ||
this._pendingOpen = null | ||
this._pendingWrite = null | ||
this._pendingFinal = null | ||
this._pendingDestroy = null | ||
this._handle = slab.subarray(0, binding.sizeofPipe) | ||
this._req = slab.subarray(binding.sizeofPipe, binding.sizeofPipe + binding.sizeofWrite) | ||
this._buffer = slab.subarray(binding.sizeofPipe + binding.sizeofWrite) | ||
this._openCallback = null | ||
this._writeCallback = null | ||
this._finalCallback = null | ||
this._destroyCallback = null | ||
this._connected = typeof path !== 'string' | ||
this._reading = false | ||
this._allowHalfOpen = allowHalfOpen | ||
this._maybeWriteonly = false | ||
binding.init(this._handle, this._buffer, this, | ||
this._buffer = Buffer.alloc(readBufferSize) | ||
this._handle = binding.init(this._buffer, this, | ||
noop, | ||
this._onconnect, | ||
@@ -47,4 +44,2 @@ this._onwrite, | ||
if (typeof path === 'number') { | ||
// reads are buffering in the bg, so in case not readable, ignore the errors | ||
this._maybeWriteonly = true | ||
binding.open(this._handle, path) | ||
@@ -56,4 +51,8 @@ } else if (typeof path === 'string') { | ||
static createServer (opts) { | ||
return new PipeServer(opts) | ||
} | ||
_open (cb) { | ||
this._openCallback = cb | ||
this._pendingOpen = cb | ||
this._continueOpen(null) | ||
@@ -65,8 +64,3 @@ } | ||
this._reading = true | ||
try { | ||
binding.resume(this._handle) | ||
} catch (err) { | ||
if (!this._maybeWriteonly) return cb(err) | ||
this.push(null) | ||
} | ||
binding.resume(this._handle) | ||
} | ||
@@ -77,8 +71,8 @@ cb(null) | ||
_writev (datas, cb) { | ||
this._writeCallback = cb | ||
binding.writev(this._req, this._handle, datas) | ||
this._pendingWrite = cb | ||
binding.writev(this._handle, datas) | ||
} | ||
_final (cb) { | ||
this._finalCallback = cb | ||
this._pendingFinal = cb | ||
binding.end(this._handle) | ||
@@ -88,3 +82,3 @@ } | ||
_destroy (cb) { | ||
this._destroyCallback = cb | ||
this._pendingDestroy = cb | ||
binding.close(this._handle) | ||
@@ -95,5 +89,5 @@ } | ||
if (!this._connected) return | ||
if (this._openCallback === null) return | ||
const cb = this._openCallback | ||
this._openCallback = null | ||
if (this._pendingOpen === null) return | ||
const cb = this._pendingOpen | ||
this._pendingOpen = null | ||
cb(err) | ||
@@ -103,5 +97,5 @@ } | ||
_continueWrite (err) { | ||
if (this._writeCallback === null) return | ||
const cb = this._writeCallback | ||
this._writeCallback = null | ||
if (this._pendingWrite === null) return | ||
const cb = this._pendingWrite | ||
this._pendingWrite = null | ||
cb(err) | ||
@@ -111,5 +105,5 @@ } | ||
_continueFinal (err) { | ||
if (this._finalCallback === null) return | ||
const cb = this._finalCallback | ||
this._finalCallback = null | ||
if (this._pendingFinal === null) return | ||
const cb = this._pendingFinal | ||
this._pendingFinal = null | ||
cb(err) | ||
@@ -119,5 +113,5 @@ } | ||
_continueDestroy () { | ||
if (this._destroyCallback === null) return | ||
const cb = this._destroyCallback | ||
this._destroyCallback = null | ||
if (this._pendingDestroy === null) return | ||
const cb = this._pendingDestroy | ||
this._pendingDestroy = null | ||
cb(null) | ||
@@ -166,4 +160,87 @@ } | ||
class PipeServer extends EventEmitter { | ||
constructor (opts = {}) { | ||
super() | ||
const { | ||
readBufferSize = DEFAULT_READ_BUFFER, | ||
allowHalfOpen = true | ||
} = opts | ||
this._readBufferSize = readBufferSize | ||
this._allowHalfOpen = allowHalfOpen | ||
this._handle = binding.init(empty, this, | ||
this._onconnection, | ||
noop, | ||
noop, | ||
noop, | ||
noop, | ||
this._onclose | ||
) | ||
this.listening = false | ||
this.closing = false | ||
this.connections = new Set() | ||
} | ||
listen (name, backlog = 511) { | ||
if (this.listening) throw new Error('Server is already bound') | ||
if (this.closing) throw new Error('Server is closed') | ||
binding.bind(this._handle, name, backlog) | ||
return this | ||
} | ||
close (onclose) { | ||
if (onclose) this.once('close', onclose) | ||
if (this.closing) return | ||
this.closing = true | ||
if (this.connections.size === 0) binding.close(this._handle) | ||
} | ||
_onconnection (err) { | ||
if (err) return // TODO: Propagate errors | ||
if (this.closing) return | ||
const pipe = new Pipe({ | ||
readBufferSize: this._readBufferSize, | ||
allowHalfOpen: this._allowHalfOpen | ||
}) | ||
try { | ||
binding.accept(this._handle, pipe._handle) | ||
this.connections.add(pipe) | ||
pipe.on('close', () => { | ||
this.connections.delete(pipe) | ||
if (this.closing && this.connections.size === 0) { | ||
binding.close(this._handle) | ||
} | ||
}) | ||
this.emit('connection', pipe) | ||
} catch (err) { // TODO: Propagate errors | ||
pipe.destroy() | ||
} | ||
} | ||
_onclose () { | ||
this._handle = null | ||
this.emit('close') | ||
} | ||
} | ||
const empty = Buffer.alloc(0) | ||
function noop () {} | ||
function mapWritable (buf) { | ||
return typeof buf === 'string' ? Buffer.from(buf) : buf | ||
} |
{ | ||
"name": "bare-pipe", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Native I/O pipes for JavaScript", | ||
@@ -14,5 +14,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "standard && bare test.js", | ||
"install": "bare-dev rebuild", | ||
"prebuild": "bare-dev prebuild" | ||
"test": "standard && bare test.js" | ||
}, | ||
@@ -19,0 +17,0 @@ "repository": { |
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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
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
33423
185
0
0