1.0.11 • Published 3 years ago

icebreaker-network v1.0.11

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

icebreaker-network

A network library for pull-streams to handle multiple protocols with the same interface.

Travis NPM

Usage

  var _ = require('icebreaker')
  var network = require('icebreaker-network')
 
  var listen = network.listen('tcp://127.0.0.1:8090')

  _(listen, network.on({
    ready: function (e) {
      console.log('socket is ready on port: '+e.address)
      //connect to localhost
      network.connect(e.address, function (err,connection) {
        // handle the client side pull-stream
        if(err)  throw err
        _('hello', connection, _.drain(function (d) {
          console.log(d.toString())
        }, function (err) {
          // close the socket
          listen.end()
        }))
      })
    },
    connection: function (s) {
      // handle the server side pull-stream
      _(s, _.map(function (d) {
        return d.toString() + ' world'
      }), s)
    },
    end: function () {
      console.log('socket closed')
    }
  }))

API

URL format for TCP, WS, UTP

{protocol}://{ip}:{port}/

URL format for TCP, WS unix sockets.

{protocol+unix}://{path}

URL format for protocols with secret-handshake

shs+{protocol}://{Base64 and URL encoded public key}@{ip}:{port}/{app_key}

URL format for TCP,WS unix sockets with secret-handshake

shs+{protocol}+unix://{Base64 and URL encoded public key}@{path}/{app_key}

Events

  • ready
  • message
  • connection
  • end
   _(
     network.listen('tcp://127.0.0.1:8090'),
     network.on({
      ready:function(e){
        console.log('tcp socket is ready on address: '+e.address)  
      },
      end:function(){

      }
    })
  )

network.on(events)

Helper to handle events on a listener.

  • Returns a sink if the end event is set, otherwise a through.
_(
  listener,
  network.on({
  ready:function(e){
    console.log(e.address)
  },
  connection:function(e){
    // handle the connection
  }
  end:function(err){ 
    if(err) console.log(err)
  }
}))

network.map(events)

Helper to map events on a listener.

  • Returns a through.
_(
  network.listen('tcp://127.0.0.1:8090'),
  network.map({
    connection:function(e){
      e.isSuper = true
      return e
    }
  }),
  network.on({
    connection:function(e){
      console.log(e)
    },
    end:function(err){
      console.log(err)
    }
  })
)

network.asyncMap(events)

The async version of map.

  • Returns a through.
_(
  network.listen('tcp://127.0.0.1:8090'),
  network.asyncMap({
    connection:function(e,done){
      e.isSuper = true
      done(e)
    }
  }),
  network.on({
    connection:function(e){
      console.log(e)
    },
    end:function(err){
      console.log(err)
    }
  })
)

network.connect(url,params,callback)

This connects to a specified url address.

  • Url to connect.
  • Parameters for the selected protocol is optional, when the selected protocols is not a shs protocol.
  • callback(error,connection) returns error or a connection duplex stream.
  • For shs protocols is params.keys.publicKey and params.keys.secretKey required.
  var cl = require('chloride')
  var keys = cl.crypto_sign_keypair()

network.listen(url,params)

This starts a server in the listening mode on the given url and parameters.

  • Returns a source with events
  • URL is required the format is protocol://host:port
  • Parameters for the selected protocol is optional, when the selected protocols is not a shs protocol.

listener.end

This closes the socket

listener.push(msg,address)

This sends a network message on message-oriented protocols to a destination.

network.combine(listen1,listen2,...)

This combines multiple network.listen to one source.

var os = require('os')

var listener = network.combine(
  listen('tcp://localhost:8089'),
  listen('udp://0.0.0.0:8089',{reuseAddr:true,loopback:true,multicast:'239.5.5.5'}),
  listen('tcp+unix://'+os.tmpdir()+'/test4.socket')
)

_(listener,network.on({
  // all listener ready
  ready:function(e){
    // sends a string message over udp
    listener.push('ready')
    // connects to localhost
    network.connect('tcp://localhost:8089',function(err,connection){
    
    })
  },
  // incomming tcp connection
  connection:function(connection){
    _(['hello'],connection,_.drain(function(item){
      console.log(item)
    },function(err){
        if(err) return console.log(err)
    }))
  },
  // incomming udp message
  message:function(e){
    console.log(e)
  },
  end:function(err){

  }
}))

network.protoNames()

  • Returns a list of the supported protocols.

network.register(name,connect,listen)

Register a custom protocol.

  • Name of the protocol for example 'custom:'.
  • For the custom connect and listen see in lib/tcp for example.

License

MIT

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago

0.9.0

5 years ago

0.8.6

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

6 years ago

0.7.17

6 years ago

0.7.16

6 years ago

0.7.15

6 years ago

0.7.14

6 years ago

0.7.13

6 years ago

0.7.12

6 years ago

0.7.11

6 years ago

0.7.10

6 years ago

0.7.9

6 years ago

0.7.8

6 years ago

0.7.7

7 years ago

0.7.6

7 years ago

0.7.5

7 years ago

0.7.4

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago