Socket
Socket
Sign inDemoInstall

chrome-net

Package Overview
Dependencies
2
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

test/ip.js

157

index.js

@@ -12,9 +12,5 @@ /* global chrome */

* TODO (unimplemented):
*
* net.isIP(input)
* net.isIPv4(input)
* net.isIPv6(input)
* Socket.prototype.bufferSize
*/
var bops = require('bops')
var EventEmitter = require('events').EventEmitter

@@ -24,2 +20,3 @@ var is = require('core-util-is')

var util = require('util')
var ipaddr = require('ipaddr.js')

@@ -51,31 +48,2 @@ /**

/**
* Convert given data to a Uint8Array. The underlying ArrayBuffer is
* guaranteed to be the same size as the Uint8Array.
*
* @param {Uint8Array|ArrayBuffer|string} data
* @return {Uint8Array} [description]
*/
function toBuffer (data) {
if (bops.is(data)) {
if (data.length === data.buffer.length) {
return data
} else {
// If data is a Uint8Array (TypedArrayView) AND its underlying ArrayBuffer
// has a different size (larger) then we create a new Uint8Array and
// underlying ArrayBuffer that are the exact same size. This is necessary
// because Chrome's `sendTo` consumes the underlying ArrayBuffer.
var newBuf = bops.create(data.length)
bops.copy(data, newBuf, 0, 0, data.length)
return newBuf
}
} else if (typeof data === 'string') {
return bops.from(data)
} else if (data instanceof ArrayBuffer) {
return new Uint8Array(data)
} else {
throw new Error('Cannot convert data to ArrayBuffer type')
}
}
/**
* Creates a new TCP server. The connectionListener argument is automatically

@@ -185,3 +153,3 @@ * set as a listener for the 'connection' event.

*
* @return {[type]} [description]
* @return {Socket}
*/

@@ -274,33 +242,2 @@ Server.prototype.listen = function (/* variable arguments... */) {

var self = this
// function fireErrorCallbacks () {
// if (cb) cb(exception)
// if (exception && !self.errorEmitted) {
// process.nextTick(function () {
// self.emit('error', exception)
// })
// self.errorEmitted = true
// }
// }
// if (self.destroyed) {
// // already destroyed, fire error callbacks
// fireErrorCallbacks()
// return
// }
// self._connecting = false
// this.readable = this.writable = false
// chrome.socket.disconnect(self.id)
// chrome.socket.destroy(self.id)
// self.emit('close', !!exception)
// fireErrorCallbacks()
// self.destroyed = true
// if (this.server) {
// this.server._connections -= 1
// }
}

@@ -425,18 +362,2 @@

// let's make this a stream of Uint8Array objects.
//
// TODO: Consider defunctzombie's approach:
//
// defunctzombie: https://github.com/alexgorbatchev/node-browser-builtins/tree/master/builtin
// defunctzombie: take the stream files
// defunctzombie: stream.js
// defunctzombie: and the __stream shit
// defunctzombie: put them all into a repo
// defunctzombie: maybe called bops-stream I dunno
// defunctzombie: and update the uses of Buffer('foobar') or whatever to use bops
// defunctzombie: if the bops was API compatible you could just change require('buffer') to require('bops');
// feross: right
// defunctzombie: then in your module just require('bops-stream'); and you will be set
// feross: and use it in place of require('stream')?
// defunctzombie: this will be nice since others will be able to use your module too
// defunctzombie: yep
options.objectMode = true

@@ -488,5 +409,4 @@

* @param {Object} options
* @param {function} cb
* @param {[type]} [connectListener] [description]
* @return {[type]} [description]
* @param {function} [connectListener]
* @return {Socket} this socket (for chaining)
*/

@@ -501,2 +421,6 @@ Socket.prototype.connect = function (options, cb) {

if (is.isFunction(cb)) {
self.once('connect', cb)
}
chrome.socket.create('tcp', {}, function (createInfo) {

@@ -507,7 +431,7 @@ self.id = createInfo.socketId

options.host,
options.port,
Number(options.port),
function (result) {
if (result < 0) {
self.destroy('error', new Error('Socket ' + self.id +
' connect error ' + result))
self.destroy(new Error('Socket ' + self.id + ' connect error ' +
result))
return

@@ -536,3 +460,2 @@ }

self.emit('connect')
// start the first read, or get an immediate EOF.

@@ -545,4 +468,2 @@ // this doesn't actually consume any bytes, because len=0

// Socket.prototype.bufferSize
/**

@@ -559,3 +480,3 @@ * Sends data on the socket. The second parameter specifies the encoding in

*
* @param {ArrayBuffer|TypedArray|string} chunk
* @param {Buffer|Arrayish|string} chunk
* @param {string} [encoding]

@@ -566,6 +487,6 @@ * @param {function} [callback]

Socket.prototype.write = function (chunk, encoding, callback) {
var buffer = toBuffer(chunk)
if (!Buffer.isBuffer(chunk)) chunk = new Buffer(chunk)
// The stream is in "object mode" so it will accept a Uint8Array object
return stream.Duplex.prototype.write.call(this, buffer, encoding, callback)
return stream.Duplex.prototype.write.call(this, chunk, encoding, callback)
}

@@ -588,3 +509,3 @@

chrome.socket.write(self.id, buffer.buffer, function (writeInfo) {
chrome.socket.write(self.id, buffer.toArrayBuffer(), function (writeInfo) {
if (writeInfo.bytesWritten < 0) {

@@ -625,3 +546,5 @@ var err = new Error('Socket ' + self.id + ' write error ' +

} else {
var buffer = toBuffer(readInfo.data)
var buffer = readInfo.data
buffer = new Buffer(new Uint8Array(buffer))
self.bytesRead += buffer.length

@@ -643,11 +566,11 @@

self._writableState.buffer.forEach(function (el) {
if (bops.is(el.chunk))
self._writableState.toArrayBuffer().forEach(function (el) {
if (Buffer.isBuffer(el.chunk))
bytes += el.chunk.length
else
bytes += bops.from(el.chunk, el.encoding).length
bytes += new Buffer(el.chunk, el.encoding).length
})
if (self._pendingData) {
if (bops.is(self._pendingData))
if (Buffer.isBuffer(self._pendingData))
bytes += self._pendingData.length

@@ -661,5 +584,10 @@ else

Socket.prototype.destroy = function (exception, cb) {
Socket.prototype.destroy = function (exception) {
var self = this
self._destroy(exception)
}
Socket.prototype._destroy = function (exception, cb) {
var self = this
function fireErrorCallbacks () {

@@ -784,1 +712,28 @@ if (cb) cb(exception)

}
exports.isIP = function (input) {
try {
ipaddr.parse(input)
} catch (e) {
return false
}
return true
}
exports.isIPv4 = function (input) {
try {
var parsed = ipaddr.parse(input)
return (parsed.kind() === 'ipv4')
} catch (e) {
return false
}
}
exports.isIPv6 = function (input) {
try {
var parsed = ipaddr.parse(input)
return (parsed.kind() === 'ipv6')
} catch (e) {
return false
}
}
{
"name": "chrome-net",
"version": "1.0.0",
"version": "1.1.0",
"description": "Use the Node `net` API in Chrome Apps",
"main": "index.js",
"dependencies": {
"bops": "0.x",
"core-util-is": "1.x"
"core-util-is": "1.x",
"ipaddr.js": "0.x"
},

@@ -20,5 +20,2 @@ "devDependencies": {

},
"browser": {
"buffer": false
},
"scripts": {

@@ -25,0 +22,0 @@ "test": "tape test/*.js"

@@ -1,2 +0,1 @@

var bops = require('bops')
var net = require('../../')

@@ -17,3 +16,3 @@

client.on('data', function (data) {
if (bops.to(data) === 'boop') {
if (data.toString() === 'boop') {
client.write('pass')

@@ -20,0 +19,0 @@ } else {

@@ -1,2 +0,1 @@

var bops = require('bops')
var dgram = require('chrome-dgram')

@@ -34,3 +33,5 @@ var net = require('../../')

sock.on('data', function (data) {
if (bops.to(data) === 'beep') {
console.log('data')
console.log(data.toString())
if (data.toString() === 'beep') {
sock.write('boop')

@@ -37,0 +38,0 @@ } else {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc