chrome-net
Advanced tools
Comparing version 3.0.1 to 3.1.0
649
index.js
/*global chrome */ | ||
'use strict' | ||
@@ -12,5 +13,4 @@ /** | ||
var EventEmitter = require('events').EventEmitter | ||
var EventEmitter = require('events') | ||
var inherits = require('inherits') | ||
var is = require('core-util-is') | ||
var stream = require('stream') | ||
@@ -70,7 +70,7 @@ var deprecate = require('util').deprecate | ||
* @param {Object} options | ||
* @param {function} listener | ||
* @param {function} connectionListener | ||
* @return {Server} | ||
*/ | ||
exports.createServer = function (options, listener) { | ||
return new Server(options, listener) | ||
exports.createServer = function (options, connectionListener) { | ||
return new Server(options, connectionListener) | ||
} | ||
@@ -134,42 +134,39 @@ | ||
*/ | ||
function Server (/* [options], listener */) { | ||
var self = this | ||
if (!(self instanceof Server)) return new Server(arguments[0], arguments[1]) | ||
EventEmitter.call(self) | ||
function Server (options, connectionListener) { | ||
if (!(this instanceof Server)) return new Server(options, connectionListener) | ||
EventEmitter.call(this) | ||
var options | ||
if (is.isFunction(arguments[0])) { | ||
if (typeof options === 'function') { | ||
connectionListener = options | ||
options = {} | ||
self.on('connection', arguments[0]) | ||
this.on('connection', connectionListener) | ||
} else { | ||
options = arguments[0] || {} | ||
options = options || {} | ||
if (is.isFunction(arguments[1])) { | ||
self.on('connection', arguments[1]) | ||
if (typeof connectionListener === 'function') { | ||
this.on('connection', connectionListener) | ||
} | ||
} | ||
self._connections = 0 | ||
this._connections = 0 | ||
Object.defineProperty(self, 'connections', { | ||
get: deprecate(function () { | ||
return self._connections | ||
}, 'connections property is deprecated. Use getConnections() method'), | ||
set: deprecate(function (val) { | ||
return (self._connections = val) | ||
}, 'connections property is deprecated. Use getConnections() method'), | ||
Object.defineProperty(this, 'connections', { | ||
get: deprecate(() => this._connections, | ||
'Server.connections property is deprecated. ' + | ||
'Use Server.getConnections method instead.'), | ||
set: deprecate(val => (this._connections = val), | ||
'Server.connections property is deprecated.'), | ||
configurable: true, enumerable: false | ||
}) | ||
self.id = null // a number > 0 | ||
self._connecting = false | ||
this.id = null // a number > 0 | ||
this._connecting = false | ||
self.allowHalfOpen = options.allowHalfOpen || false | ||
self.pauseOnConnect = !!options.pauseOnConnect | ||
self._address = null | ||
this.allowHalfOpen = options.allowHalfOpen || false | ||
this.pauseOnConnect = !!options.pauseOnConnect | ||
this._address = null | ||
self._host = null | ||
self._port = null | ||
self._backlog = null | ||
this._host = null | ||
this._port = null | ||
this._backlog = null | ||
} | ||
@@ -199,7 +196,5 @@ exports.Server = Server | ||
Server.prototype.listen = function (/* variable arguments... */) { | ||
var self = this | ||
var lastArg = arguments[arguments.length - 1] | ||
if (is.isFunction(lastArg)) { | ||
self.once('listening', lastArg) | ||
if (typeof lastArg === 'function') { | ||
this.once('listening', lastArg) | ||
} | ||
@@ -215,3 +210,3 @@ | ||
if (is.isObject(arguments[0])) { | ||
if (arguments[0] !== null && typeof arguments[0] === 'object') { | ||
var h = arguments[0] | ||
@@ -222,3 +217,3 @@ | ||
} | ||
if (is.isNumber(h.fd) && h.fd >= 0) { | ||
if (typeof h.fd === 'number' && h.fd >= 0) { | ||
throw new Error('fd is not supported in Chrome Apps.') | ||
@@ -232,3 +227,6 @@ } | ||
if (is.isNumber(h.port)) { | ||
if (typeof h.port === 'number' || typeof h.port === 'string' || | ||
(typeof h.port === 'undefined' && 'port' in h)) { | ||
// Undefined is interpreted as zero (random port) for consistency | ||
// with net.connect(). | ||
address = h.host || null | ||
@@ -244,5 +242,5 @@ port = h.port | ||
throw new Error('Pipes are not supported in Chrome Apps.') | ||
} else if (is.isUndefined(arguments[1]) || | ||
is.isFunction(arguments[1]) || | ||
is.isNumber(arguments[1])) { | ||
} else if (arguments[1] === undefined || | ||
typeof arguments[1] === 'function' || | ||
typeof arguments[1] === 'number') { | ||
// The first argument is the port, no IP given. | ||
@@ -257,25 +255,25 @@ address = null | ||
if (self.id) { | ||
self.close() | ||
if (this.id) { | ||
this.close() | ||
} | ||
// If port is invalid or undefined, bind to a random port. | ||
self._port = port | 0 | ||
if (self._port < 0 || self._port > 65535) { // allow 0 for random port | ||
throw new RangeError('port should be >= 0 and < 65536: ' + self._port) | ||
if (typeof port !== 'undefined' && !isLegalPort(port)) { | ||
throw new RangeError('"port" option should be >= 0 and < 65536: ' + port) | ||
} | ||
this._port = port | 0 | ||
self._host = address | ||
this._host = address | ||
var isAny6 = !self._host | ||
var isAny6 = !this._host | ||
if (isAny6) { | ||
self._host = '::' | ||
this._host = '::' | ||
} | ||
self._backlog = is.isNumber(backlog) ? backlog : undefined | ||
this._backlog = typeof backlog === 'number' ? backlog : undefined | ||
self._connecting = true | ||
this._connecting = true | ||
chrome.sockets.tcpServer.create(function (createInfo) { | ||
if (!self._connecting || self.id) { | ||
chrome.sockets.tcpServer.create(createInfo => { | ||
if (!this._connecting || this.id) { | ||
ignoreLastError() | ||
@@ -286,41 +284,39 @@ chrome.sockets.tcpServer.close(createInfo.socketId) | ||
if (chrome.runtime.lastError) { | ||
self.emit('error', new Error(chrome.runtime.lastError.message)) | ||
this.emit('error', new Error(chrome.runtime.lastError.message)) | ||
return | ||
} | ||
var socketId = self.id = createInfo.socketId | ||
servers[self.id] = self | ||
var socketId = this.id = createInfo.socketId | ||
servers[this.id] = this | ||
function listen () { | ||
chrome.sockets.tcpServer.listen(self.id, self._host, self._port, | ||
self._backlog, function (result) { | ||
// callback may be after close | ||
if (self.id !== socketId) { | ||
ignoreLastError() | ||
return | ||
} | ||
if (result !== 0 && isAny6) { | ||
ignoreLastError() | ||
self._host = '0.0.0.0' // try IPv4 | ||
isAny6 = false | ||
return listen() | ||
} | ||
var listen = () => chrome.sockets.tcpServer.listen(this.id, this._host, | ||
this._port, this._backlog, | ||
result => { | ||
// callback may be after close | ||
if (this.id !== socketId) { | ||
ignoreLastError() | ||
return | ||
} | ||
if (result !== 0 && isAny6) { | ||
ignoreLastError() | ||
this._host = '0.0.0.0' // try IPv4 | ||
isAny6 = false | ||
return listen() | ||
} | ||
self._onListen(result) | ||
}) | ||
} | ||
this._onListen(result) | ||
}) | ||
listen() | ||
}) | ||
return self | ||
return this | ||
} | ||
Server.prototype._onListen = function (result) { | ||
var self = this | ||
self._connecting = false | ||
this._connecting = false | ||
if (result === 0) { | ||
var idBefore = self.id | ||
chrome.sockets.tcpServer.getInfo(self.id, function (info) { | ||
if (self.id !== idBefore) { | ||
var idBefore = this.id | ||
chrome.sockets.tcpServer.getInfo(this.id, info => { | ||
if (this.id !== idBefore) { | ||
ignoreLastError() | ||
@@ -330,7 +326,7 @@ return | ||
if (chrome.runtime.lastError) { | ||
self._onListen(-2) // net::ERR_FAILED | ||
this._onListen(-2) // net::ERR_FAILED | ||
return | ||
} | ||
self._address = { | ||
this._address = { | ||
port: info.localPort, | ||
@@ -341,9 +337,9 @@ family: info.localAddress && | ||
} | ||
self.emit('listening') | ||
this.emit('listening') | ||
}) | ||
} else { | ||
self.emit('error', errnoException(result, 'listen')) | ||
chrome.sockets.tcpServer.close(self.id) | ||
delete servers[self.id] | ||
self.id = null | ||
this.emit('error', exceptionWithHostPort(result, 'listen', this._host, this._port)) | ||
chrome.sockets.tcpServer.close(this.id) | ||
delete servers[this.id] | ||
this.id = null | ||
} | ||
@@ -353,7 +349,5 @@ } | ||
Server.prototype._onAccept = function (clientSocketId) { | ||
var self = this | ||
// Set the `maxConnections` property to reject connections when the server's | ||
// connection count gets high. | ||
if (self.maxConnections && self._connections >= self.maxConnections) { | ||
if (this.maxConnections && this._connections >= this.maxConnections) { | ||
chrome.sockets.tcp.close(clientSocketId) | ||
@@ -364,19 +358,16 @@ console.warn('Rejected connection - hit `maxConnections` limit') | ||
self._connections += 1 | ||
this._connections += 1 | ||
var acceptedSocket = new Socket({ | ||
server: self, | ||
server: this, | ||
id: clientSocketId, | ||
allowHalfOpen: self.allowHalfOpen, | ||
pauseOnCreate: self.pauseOnConnect | ||
allowHalfOpen: this.allowHalfOpen, | ||
pauseOnCreate: this.pauseOnConnect | ||
}) | ||
acceptedSocket.on('connect', function () { | ||
self.emit('connection', acceptedSocket) | ||
}) | ||
acceptedSocket.on('connect', () => this.emit('connection', acceptedSocket)) | ||
} | ||
Server.prototype._onAcceptError = function (resultCode) { | ||
var self = this | ||
self.emit('error', errnoException(resultCode, 'accept')) | ||
self.close() | ||
this.emit('error', errnoException(resultCode, 'accept')) | ||
this.close() | ||
} | ||
@@ -392,40 +383,36 @@ | ||
Server.prototype.close = function (callback) { | ||
var self = this | ||
if (callback) { | ||
if (!self.id) { | ||
self.once('close', function () { | ||
callback(new Error('Not running')) | ||
}) | ||
if (typeof callback === 'function') { | ||
if (!this.id) { | ||
this.once('close', () => callback(new Error('Not running'))) | ||
} else { | ||
self.once('close', callback) | ||
this.once('close', callback) | ||
} | ||
} | ||
if (self.id) { | ||
chrome.sockets.tcpServer.close(self.id) | ||
delete servers[self.id] | ||
self.id = null | ||
if (this.id) { | ||
chrome.sockets.tcpServer.close(this.id) | ||
delete servers[this.id] | ||
this.id = null | ||
} | ||
self._address = null | ||
self._connecting = false | ||
this._address = null | ||
this._connecting = false | ||
self._emitCloseIfDrained() | ||
this._emitCloseIfDrained() | ||
return self | ||
return this | ||
} | ||
Server.prototype._emitCloseIfDrained = function () { | ||
var self = this | ||
if (this.id || this._connecting || this._connections) { | ||
return | ||
} | ||
process.nextTick(emitCloseNT, this) | ||
} | ||
function emitCloseNT (self) { | ||
if (self.id || self._connecting || self._connections) { | ||
return | ||
} | ||
process.nextTick(function () { | ||
if (self.id || self._connecting || self._connections) { | ||
return | ||
} | ||
self.emit('close') | ||
}) | ||
self.emit('close') | ||
} | ||
@@ -444,8 +431,6 @@ | ||
Server.prototype.unref = function () { | ||
// No chrome.socket equivalent | ||
} | ||
Server.prototype.unref = | ||
Server.prototype.ref = function () { | ||
// No chrome.socket equivalent | ||
return this | ||
} | ||
@@ -462,6 +447,3 @@ | ||
Server.prototype.getConnections = function (callback) { | ||
var self = this | ||
process.nextTick(function () { | ||
callback(null, self._connections) | ||
}) | ||
process.nextTick(callback, null, this._connections) | ||
} | ||
@@ -536,8 +518,7 @@ | ||
function Socket (options) { | ||
var self = this | ||
if (!(self instanceof Socket)) return new Socket(options) | ||
if (!(this instanceof Socket)) return new Socket(options) | ||
if (is.isNumber(options)) { | ||
if (typeof options === 'number') { | ||
options = { fd: options } // Legacy interface. | ||
} else if (is.isUndefined(options)) { | ||
} else if (options === undefined) { | ||
options = {} | ||
@@ -548,3 +529,3 @@ } | ||
throw new Error('handle is not supported in Chrome Apps.') | ||
} else if (!is.isUndefined(options.fd)) { | ||
} else if (options.fd !== undefined) { | ||
throw new Error('fd is not supported in Chrome Apps.') | ||
@@ -555,29 +536,29 @@ } | ||
options.objectMode = false | ||
stream.Duplex.call(self, options) | ||
stream.Duplex.call(this, options) | ||
self.destroyed = false | ||
self._hadError = false // Used by _http_client.js | ||
self.id = null // a number > 0 | ||
self._parent = null | ||
self._host = null | ||
self._port = null | ||
self._pendingData = null | ||
this.destroyed = false | ||
this._hadError = false // Used by _http_client.js | ||
this.id = null // a number > 0 | ||
this._parent = null | ||
this._host = null | ||
this._port = null | ||
this._pendingData = null | ||
self.ondata = null | ||
self.onend = null | ||
this.ondata = null | ||
this.onend = null | ||
self._init() | ||
self._reset() | ||
this._init() | ||
this._reset() | ||
// default to *not* allowing half open sockets | ||
// Note: this is not possible in Chrome Apps, see https://crbug.com/124952 | ||
self.allowHalfOpen = options.allowHalfOpen || false | ||
this.allowHalfOpen = options.allowHalfOpen || false | ||
// shut down the socket when we're finished with it. | ||
self.on('finish', self.destroy) | ||
this.on('finish', this.destroy) | ||
if (options.server) { | ||
self.server = options.server | ||
self.id = options.id | ||
sockets[self.id] = self | ||
this.server = options.server | ||
this.id = options.id | ||
sockets[this.id] = this | ||
@@ -587,9 +568,9 @@ if (options.pauseOnCreate) { | ||
// (Already paused in Chrome version) | ||
self._readableState.flowing = false | ||
this._readableState.flowing = false | ||
} | ||
// For incoming sockets (from server), it's already connected. | ||
self._connecting = true | ||
self.writable = true | ||
self._onConnect() | ||
this._connecting = true | ||
this.writable = true | ||
this._onConnect() | ||
} | ||
@@ -601,8 +582,6 @@ } | ||
Socket.prototype._init = function () { | ||
var self = this | ||
// The amount of received bytes. | ||
self.bytesRead = 0 | ||
this.bytesRead = 0 | ||
self._bytesDispatched = 0 | ||
this._bytesDispatched = 0 | ||
} | ||
@@ -612,9 +591,7 @@ | ||
Socket.prototype._reset = function () { | ||
var self = this | ||
self.remoteAddress = self.remotePort = | ||
self.localAddress = self.localPort = null | ||
self.remoteFamily = 'IPv4' | ||
self.readable = self.writable = false | ||
self._connecting = false | ||
this.remoteAddress = this.remotePort = | ||
this.localAddress = this.localPort = null | ||
this.remoteFamily = 'IPv4' | ||
this.readable = this.writable = false | ||
this._connecting = false | ||
} | ||
@@ -647,3 +624,2 @@ | ||
Socket.prototype.connect = function () { | ||
var self = this | ||
var args = normalizeConnectArgs(arguments) | ||
@@ -657,39 +633,45 @@ var options = args[0] | ||
if (self.id) { | ||
if (this.id) { | ||
// already connected, destroy and connect again | ||
self.destroy() | ||
this.destroy() | ||
} | ||
if (self.destroyed) { | ||
self._readableState.reading = false | ||
self._readableState.ended = false | ||
self._readableState.endEmitted = false | ||
self._writableState.ended = false | ||
self._writableState.ending = false | ||
self._writableState.finished = false | ||
self._writableState.errorEmitted = false | ||
self._writableState.length = 0 | ||
self.destroyed = false | ||
if (this.destroyed) { | ||
this._readableState.reading = false | ||
this._readableState.ended = false | ||
this._readableState.endEmitted = false | ||
this._writableState.ended = false | ||
this._writableState.ending = false | ||
this._writableState.finished = false | ||
this._writableState.errorEmitted = false | ||
this._writableState.length = 0 | ||
this.destroyed = false | ||
} | ||
self._connecting = true | ||
self.writable = true | ||
this._connecting = true | ||
this.writable = true | ||
self._host = options.host || 'localhost' | ||
self._port = Number(options.port) | ||
this._host = options.host || 'localhost' | ||
this._port = options.port | ||
if (self._port < 0 || self._port > 65535 || isNaN(self._port)) { | ||
throw new RangeError('port should be >= 0 and < 65536: ' + options.port) | ||
if (typeof this._port !== 'undefined') { | ||
if (typeof this._port !== 'number' && typeof this._port !== 'string') { | ||
throw new TypeError('"port" option should be a number or string: ' + this._port) | ||
} | ||
if (!isLegalPort(this._port)) { | ||
throw new RangeError('"port" option should be >= 0 and < 65536: ' + this._port) | ||
} | ||
} | ||
this._port |= 0 | ||
self._init() | ||
this._init() | ||
self._unrefTimer() | ||
this._unrefTimer() | ||
if (is.isFunction(cb)) { | ||
self.once('connect', cb) | ||
if (typeof cb === 'function') { | ||
this.once('connect', cb) | ||
} | ||
chrome.sockets.tcp.create(function (createInfo) { | ||
if (!self._connecting || self.id) { | ||
chrome.sockets.tcp.create(createInfo => { | ||
if (!this._connecting || this.id) { | ||
ignoreLastError() | ||
@@ -700,14 +682,14 @@ chrome.sockets.tcp.close(createInfo.socketId) | ||
if (chrome.runtime.lastError) { | ||
self.destroy(new Error(chrome.runtime.lastError.message)) | ||
this.destroy(new Error(chrome.runtime.lastError.message)) | ||
return | ||
} | ||
self.id = createInfo.socketId | ||
sockets[self.id] = self | ||
this.id = createInfo.socketId | ||
sockets[this.id] = this | ||
chrome.sockets.tcp.setPaused(self.id, true) | ||
chrome.sockets.tcp.setPaused(this.id, true) | ||
chrome.sockets.tcp.connect(self.id, self._host, self._port, function (result) { | ||
chrome.sockets.tcp.connect(this.id, this._host, this._port, result => { | ||
// callback may come after call to destroy | ||
if (self.id !== createInfo.socketId) { | ||
if (this.id !== createInfo.socketId) { | ||
ignoreLastError() | ||
@@ -717,20 +699,18 @@ return | ||
if (result !== 0) { | ||
self.destroy(errnoException(result, 'connect')) | ||
this.destroy(exceptionWithHostPort(result, 'connect', this._host, this._port)) | ||
return | ||
} | ||
self._unrefTimer() | ||
self._onConnect() | ||
this._unrefTimer() | ||
this._onConnect() | ||
}) | ||
}) | ||
return self | ||
return this | ||
} | ||
Socket.prototype._onConnect = function () { | ||
var self = this | ||
var idBefore = self.id | ||
chrome.sockets.tcp.getInfo(self.id, function (result) { | ||
if (self.id !== idBefore) { | ||
var idBefore = this.id | ||
chrome.sockets.tcp.getInfo(this.id, result => { | ||
if (this.id !== idBefore) { | ||
ignoreLastError() | ||
@@ -740,21 +720,20 @@ return | ||
if (chrome.runtime.lastError) { | ||
self.destroy(new Error(chrome.runtime.lastError.message)) | ||
this.destroy(new Error(chrome.runtime.lastError.message)) | ||
return | ||
} | ||
self.remoteAddress = result.peerAddress | ||
self.remoteFamily = result.peerAddress && | ||
this.remoteAddress = result.peerAddress | ||
this.remoteFamily = result.peerAddress && | ||
result.peerAddress.indexOf(':') !== -1 ? 'IPv6' : 'IPv4' | ||
self.remotePort = result.peerPort | ||
self.localAddress = result.localAddress | ||
self.localPort = result.localPort | ||
this.remotePort = result.peerPort | ||
this.localAddress = result.localAddress | ||
this.localPort = result.localPort | ||
self._connecting = false | ||
self.readable = true | ||
this._connecting = false | ||
this.readable = true | ||
self.emit('connect') | ||
this.emit('connect') | ||
// start the first read, or get an immediate EOF. | ||
// this doesn't actually consume any bytes, because len=0 | ||
// TODO: replace _readableState.flowing with isPaused() after https://github.com/substack/node-browserify/issues/1341 | ||
if (self._readableState.flowing) self.read(0) | ||
if (!this.isPaused()) this.read(0) | ||
}) | ||
@@ -769,6 +748,5 @@ } | ||
get: function () { | ||
var self = this | ||
if (self.id) { | ||
if (this.id) { | ||
var bytes = this._writableState.length | ||
if (self._pendingData) bytes += self._pendingData.length | ||
if (this._pendingData) bytes += this._pendingData.length | ||
return bytes | ||
@@ -780,22 +758,18 @@ } | ||
Socket.prototype.end = function (data, encoding) { | ||
var self = this | ||
stream.Duplex.prototype.end.call(self, data, encoding) | ||
self.writable = false | ||
stream.Duplex.prototype.end.call(this, data, encoding) | ||
this.writable = false | ||
} | ||
Socket.prototype._write = function (chunk, encoding, callback) { | ||
var self = this | ||
if (!callback) callback = function () {} | ||
if (!callback) callback = () => {} | ||
if (self._connecting) { | ||
self._pendingData = chunk | ||
self.once('connect', function () { | ||
self._write(chunk, encoding, callback) | ||
}) | ||
if (this._connecting) { | ||
this._pendingData = chunk | ||
this.once('connect', () => this._write(chunk, encoding, callback)) | ||
return | ||
} | ||
self._pendingData = null | ||
this._pendingData = null | ||
if (!this.id) { | ||
callback(new Error('This socket is closed.')) | ||
callback(new Error('This socket is closed')) | ||
return | ||
@@ -810,5 +784,5 @@ } | ||
var idBefore = self.id | ||
chrome.sockets.tcp.send(self.id, buffer, function (sendInfo) { | ||
if (self.id !== idBefore) { | ||
var idBefore = this.id | ||
chrome.sockets.tcp.send(this.id, buffer, sendInfo => { | ||
if (this.id !== idBefore) { | ||
ignoreLastError() | ||
@@ -819,5 +793,5 @@ return | ||
if (sendInfo.resultCode < 0) { | ||
self.destroy(errnoException(sendInfo.resultCode, 'write'), callback) | ||
this.destroy(exceptionWithHostPort(sendInfo.resultCode, 'write', this.remoteAddress, this.remotePort), callback) | ||
} else { | ||
self._unrefTimer() | ||
this._unrefTimer() | ||
callback(null) | ||
@@ -827,17 +801,16 @@ } | ||
self._bytesDispatched += chunk.length | ||
this._bytesDispatched += chunk.length | ||
} | ||
Socket.prototype._read = function (bufferSize) { | ||
var self = this | ||
if (self._connecting || !self.id) { | ||
self.once('connect', self._read.bind(self, bufferSize)) | ||
if (this._connecting || !this.id) { | ||
this.once('connect', this._read.bind(this, bufferSize)) | ||
return | ||
} | ||
chrome.sockets.tcp.setPaused(self.id, false) | ||
chrome.sockets.tcp.setPaused(this.id, false) | ||
var idBefore = self.id | ||
chrome.sockets.tcp.getInfo(self.id, function (result) { | ||
if (self.id !== idBefore) { | ||
var idBefore = this.id | ||
chrome.sockets.tcp.getInfo(this.id, result => { | ||
if (this.id !== idBefore) { | ||
ignoreLastError() | ||
@@ -847,3 +820,3 @@ return | ||
if (chrome.runtime.lastError || !result.connected) { | ||
self._onReceiveError(-15) // workaround for https://crbug.com/518161 | ||
this._onReceiveError(-15) // workaround for https://crbug.com/518161 | ||
} | ||
@@ -854,16 +827,15 @@ }) | ||
Socket.prototype._onReceive = function (data) { | ||
var self = this | ||
// assuming buffer is browser implementation (`buffer` package on npm) | ||
var buffer = Buffer._augment(new Uint8Array(data)) | ||
var offset = self.bytesRead | ||
var offset = this.bytesRead | ||
self.bytesRead += buffer.length | ||
self._unrefTimer() | ||
this.bytesRead += buffer.length | ||
this._unrefTimer() | ||
if (self.ondata) { | ||
if (this.ondata) { | ||
console.error('socket.ondata = func is non-standard, use socket.on(\'data\', func)') | ||
self.ondata(buffer, offset, self.bytesRead) | ||
this.ondata(buffer, offset, this.bytesRead) | ||
} | ||
if (!self.push(buffer)) { // if returns false, then apply backpressure | ||
chrome.sockets.tcp.setPaused(self.id, true) | ||
if (!this.push(buffer)) { // if returns false, then apply backpressure | ||
chrome.sockets.tcp.setPaused(this.id, true) | ||
} | ||
@@ -873,12 +845,11 @@ } | ||
Socket.prototype._onReceiveError = function (resultCode) { | ||
var self = this | ||
if (resultCode === -100) { // net::ERR_CONNECTION_CLOSED | ||
if (self.onend) { | ||
if (this.onend) { | ||
console.error('socket.onend = func is non-standard, use socket.on(\'end\', func)') | ||
self.once('end', self.onend) | ||
this.once('end', this.onend) | ||
} | ||
self.push(null) | ||
self.destroy() | ||
this.push(null) | ||
this.destroy() | ||
} else if (resultCode < 0) { | ||
self.destroy(errnoException(resultCode, 'read')) | ||
this.destroy(errnoException(resultCode, 'read')) | ||
} | ||
@@ -893,4 +864,3 @@ } | ||
get: function () { | ||
var self = this | ||
if (self.id) return self._bytesDispatched + self.bufferSize | ||
if (this.id) return this._bytesDispatched + this.bufferSize | ||
} | ||
@@ -900,20 +870,15 @@ }) | ||
Socket.prototype.destroy = function (exception) { | ||
var self = this | ||
self._destroy(exception) | ||
this._destroy(exception) | ||
} | ||
Socket.prototype._destroy = function (exception, cb) { | ||
var self = this | ||
function fireErrorCallbacks () { | ||
var fireErrorCallbacks = () => { | ||
if (cb) cb(exception) | ||
if (exception && !self._writableState.errorEmitted) { | ||
process.nextTick(function () { | ||
self.emit('error', exception) | ||
}) | ||
self._writableState.errorEmitted = true | ||
if (exception && !this._writableState.errorEmitted) { | ||
process.nextTick(emitErrorNT, this, exception) | ||
this._writableState.errorEmitted = true | ||
} | ||
} | ||
if (self.destroyed) { | ||
if (this.destroyed) { | ||
// already destroyed, fire error callbacks | ||
@@ -924,13 +889,13 @@ fireErrorCallbacks() | ||
if (self.server) { | ||
self.server._connections -= 1 | ||
if (self.server._emitCloseIfDrained) self.server._emitCloseIfDrained() | ||
self.server = null | ||
if (this.server) { | ||
this.server._connections -= 1 | ||
if (this.server._emitCloseIfDrained) this.server._emitCloseIfDrained() | ||
this.server = null | ||
} | ||
self._reset() | ||
this._reset() | ||
for (var s = self; s !== null; s = s._parent) timers.unenroll(s) | ||
for (var s = this; s !== null; s = s._parent) timers.unenroll(s) | ||
self.destroyed = true | ||
this.destroyed = true | ||
@@ -940,10 +905,10 @@ // If _destroy() has been called before chrome.sockets.tcp.create() | ||
// or disconnect | ||
if (self.id) { | ||
delete sockets[self.id] | ||
chrome.sockets.tcp.close(self.id, function () { | ||
if (self.destroyed) { | ||
self.emit('close', !!exception) | ||
if (this.id) { | ||
delete sockets[this.id] | ||
chrome.sockets.tcp.close(this.id, () => { | ||
if (this.destroyed) { | ||
this.emit('close', !!exception) | ||
} | ||
}) | ||
self.id = null | ||
this.id = null | ||
} | ||
@@ -955,7 +920,5 @@ | ||
Socket.prototype.destroySoon = function () { | ||
var self = this | ||
if (this.writable) this.end() | ||
if (self.writable) self.end() | ||
if (self._writableState.finished) self.destroy() | ||
if (this._writableState.finished) this.destroy() | ||
} | ||
@@ -977,16 +940,16 @@ | ||
Socket.prototype.setTimeout = function (timeout, callback) { | ||
var self = this | ||
if (timeout === 0) { | ||
timers.unenroll(self) | ||
timers.unenroll(this) | ||
if (callback) { | ||
self.removeListener('timeout', callback) | ||
this.removeListener('timeout', callback) | ||
} | ||
} else { | ||
timers.enroll(self, timeout) | ||
timers._unrefActive(self) | ||
timers.enroll(this, timeout) | ||
timers._unrefActive(this) | ||
if (callback) { | ||
self.once('timeout', callback) | ||
this.once('timeout', callback) | ||
} | ||
} | ||
return this | ||
} | ||
@@ -1018,8 +981,12 @@ | ||
Socket.prototype.setNoDelay = function (noDelay, callback) { | ||
var self = this | ||
if (self.id) { | ||
// backwards compatibility: assume true when `enable` is omitted | ||
noDelay = is.isUndefined(noDelay) ? true : !!noDelay | ||
chrome.sockets.tcp.setNoDelay(self.id, noDelay, chromeCallbackWrap(callback)) | ||
if (!this.id) { | ||
this.once('connect', this.setNoDelay.bind(this, noDelay, callback)) | ||
return this | ||
} | ||
// backwards compatibility: assume true when `noDelay` is omitted | ||
noDelay = noDelay === undefined ? true : !!noDelay | ||
chrome.sockets.tcp.setNoDelay(this.id, noDelay, chromeCallbackWrap(callback)) | ||
return this | ||
} | ||
@@ -1046,7 +1013,11 @@ | ||
Socket.prototype.setKeepAlive = function (enable, initialDelay, callback) { | ||
var self = this | ||
if (self.id) { | ||
chrome.sockets.tcp.setKeepAlive(self.id, !!enable, ~~(initialDelay / 1000), | ||
chromeCallbackWrap(callback)) | ||
if (!this.id) { | ||
this.once('connect', this.setKeepAlive.bind(this, enable, initialDelay, callback)) | ||
return this | ||
} | ||
chrome.sockets.tcp.setKeepAlive(this.id, !!enable, ~~(initialDelay / 1000), | ||
chromeCallbackWrap(callback)) | ||
return this | ||
} | ||
@@ -1062,8 +1033,7 @@ | ||
Socket.prototype.address = function () { | ||
var self = this | ||
return { | ||
address: self.localAddress, | ||
port: self.localPort, | ||
family: self.localAddress && | ||
self.localAddress.indexOf(':') !== -1 ? 'IPv6' : 'IPv4' | ||
address: this.localAddress, | ||
port: this.localPort, | ||
family: this.localAddress && | ||
this.localAddress.indexOf(':') !== -1 ? 'IPv6' : 'IPv4' | ||
} | ||
@@ -1074,6 +1044,5 @@ } | ||
get: function () { | ||
var self = this | ||
if (self._connecting) { | ||
if (this._connecting) { | ||
return 'opening' | ||
} else if (self.readable && self.writable) { | ||
} else if (this.readable && this.writable) { | ||
return 'open' | ||
@@ -1086,8 +1055,6 @@ } else { | ||
Socket.prototype.unref = function () { | ||
// No chrome.socket equivalent | ||
} | ||
Socket.prototype.unref = | ||
Socket.prototype.ref = function () { | ||
// No chrome.socket equivalent | ||
return this | ||
} | ||
@@ -1121,3 +1088,3 @@ | ||
if (is.isObject(args[0])) { | ||
if (args[0] !== null && typeof args[0] === 'object') { | ||
// connect(options, [cb]) | ||
@@ -1131,3 +1098,3 @@ options = args[0] | ||
options.port = args[0] | ||
if (is.isString(args[1])) { | ||
if (typeof args[1] === 'string') { | ||
options.host = args[1] | ||
@@ -1138,3 +1105,3 @@ } | ||
var cb = args[args.length - 1] | ||
return is.isFunction(cb) ? [options, cb] : [options] | ||
return typeof cb === 'function' ? [options, cb] : [options] | ||
} | ||
@@ -1147,5 +1114,12 @@ | ||
function isPipeName (s) { | ||
return is.isString(s) && toNumber(s) === false | ||
return typeof s === 'string' && toNumber(s) === false | ||
} | ||
// Check that the port number is not NaN when coerced to a number, | ||
// is an integer and that it falls within the legal range of port numbers. | ||
function isLegalPort (port) { | ||
if (typeof port === 'string' && port.trim() === '') return false | ||
return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF | ||
} | ||
// This prevents "Unchecked runtime.lastError" errors | ||
@@ -1157,3 +1131,3 @@ function ignoreLastError () { | ||
function chromeCallbackWrap (callback) { | ||
return function () { | ||
return () => { | ||
var error | ||
@@ -1168,2 +1142,6 @@ if (chrome.runtime.lastError) { | ||
function emitErrorNT (self, err) { | ||
self.emit('error', err) | ||
} | ||
// Full list of possible error codes: https://code.google.com/p/chrome-browser/source/browse/trunk/src/net/base/net_error_list.h | ||
@@ -1209,1 +1187,20 @@ // TODO: Try to reproduce errors in both node & Chrome Apps and extend this list | ||
} | ||
function exceptionWithHostPort (err, syscall, address, port, additional) { | ||
var details | ||
if (port && port > 0) { | ||
details = address + ':' + port | ||
} else { | ||
details = address | ||
} | ||
if (additional) { | ||
details += ' - Local (' + additional + ')' | ||
} | ||
var ex = errnoException(err, syscall, details) | ||
ex.address = address | ||
if (port) { | ||
ex.port = port | ||
} | ||
return ex | ||
} |
{ | ||
"name": "chrome-net", | ||
"description": "Use the Node `net` API in Chrome Apps", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)", | ||
@@ -13,7 +13,6 @@ "bugs": { | ||
"dependencies": { | ||
"core-util-is": "~1.0.1", | ||
"inherits": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^11.0.1", | ||
"browserify": "^12.0.1", | ||
"chrome-dgram": "^2.0.6", | ||
@@ -20,0 +19,0 @@ "envify": "^3.2.0", |
@@ -0,0 +0,0 @@ # chrome-net [![npm](https://img.shields.io/npm/v/chrome-net.svg)](https://npmjs.org/package/chrome-net) [![downloads](https://img.shields.io/npm/dm/chrome-net.svg)](https://npmjs.org/package/chrome-net) |
@@ -0,0 +0,0 @@ /*global chrome */ |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ var test = require('tape') |
@@ -0,0 +0,0 @@ var test = require('tape') |
@@ -0,0 +0,0 @@ var test = require('tape') |
@@ -15,3 +15,3 @@ var test = require('tape') | ||
net.createServer().listen(65536) | ||
}, /port should be >= 0 and < 65536/, 'throws when using invalid port 65536') | ||
}, /"port" option should be >= 0 and < 65536/, 'throws when using invalid port 65536') | ||
t.end() | ||
@@ -26,3 +26,3 @@ }) | ||
net.connect(65536) | ||
}, /port should be >= 0 and < 65536/, 'throws when using invalid port 65536') | ||
}, /"port" option should be >= 0 and < 65536/, 'throws when using invalid port 65536') | ||
t.end() | ||
@@ -32,3 +32,3 @@ }) | ||
function isPaused (socket) { | ||
return !socket._readableState.flowing // TODO: replace with isPaused after https://github.com/substack/node-browserify/issues/1341 | ||
return socket.isPaused() | ||
} | ||
@@ -35,0 +35,0 @@ |
@@ -0,0 +0,0 @@ var net = require('net') |
@@ -0,0 +0,0 @@ var net = require('net') |
@@ -0,0 +0,0 @@ var dgram = require('dgram') |
@@ -0,0 +0,0 @@ var net = require('net') |
@@ -22,2 +22,3 @@ var browserify = require('browserify') | ||
builtins.dgram = require.resolve('chrome-dgram') | ||
builtins['function-bind'] = require.resolve('./function-bind.js') | ||
@@ -24,0 +25,0 @@ exports.browserify = function (filename, env, cb) { |
@@ -0,0 +0,0 @@ var chromeNet = require('../') |
@@ -0,0 +0,0 @@ var helper = require('./helper') |
@@ -0,0 +0,0 @@ var auto = require('run-auto') |
@@ -0,0 +0,0 @@ var helper = require('./helper') |
@@ -0,0 +0,0 @@ var auto = require('run-auto') |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
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
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
310333
1
23
9304
20
- Removedcore-util-is@~1.0.1
- Removedcore-util-is@1.0.3(transitive)