npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

connection

v1.5.0

Published

wrap for socket

Downloads

1,471

Readme

connection

connection socket wrapper

NPM version CI Test coverage Known Vulnerabilities npm download

Usage

Client Socket

const net = require('net');
const awaitFirst = require('await-first');
const Connection = require('connection');

const Decoder = require('sofa-bolt-node/lib/decoder');
const Encoder = require('sofa-bolt-node/lib/encoder');
// bolt protocol example
const protocol = {
  name: 'Rpc',
  encoder: opts => new Encoder(opts),
  decoder: opts => new Decoder(opts),
};

async function createConnection(hostname, port) {
  const socket = net.connect(port, hostname);
  await awaitFirst(socket, [ 'connect', 'error' ]);
  return new Connection({
    logger: console,
    socket,
    protocol,
  });
}

const conn = await createConnection('127.0.0.1', 12200);

conn.writeRequest({
  targetAppName: 'foo',
  args: [ 'peter' ],
  serverSignature: 'com.alipay.sofa.rpc.quickstart.HelloService:1.0',
  methodName: 'sayHello',
  methodArgSigs: [ 'java.lang.String' ],
  requestProps: null,
});

Server Socket

const Connection = require('connection');
const server = net.createServer();
server.listen(port);

server.on('connection', sock => {
  const conn = new Connection({
    logger: console,
    socket: sock,
    protocol,
  });
  
  conn.on('request', req => {
    conn.writeResponse(req, {
      error: null,
      appResponse: 'hello, peter',
      responseProps: null,
    });
  });
});

More example

API

  • oneway() - one way call
  • async writeRequest(req) - write request and wait response
  • async writeResponse(req, res) - write response
  • async close() - wait all pending request done and destroy the socket
  • async forceClose() - abort all pending request and destroy the socket
  • get protocolOptions() - encoder/decoder constructor options, can be overwrite when custom protocol

Protocol implement

interface Request {
  /**
   * If request is oneway, shoule set to true
   */
  oneway: boolean,
  /**
   * writeRequest will use the timeout to set the timer
   */
  timeout: number,
  /**
   * request packet type, request|heartbeat|response 
   */
  packetType: string,
}

interface Response {
  packetId: number,
}

interface Encoder extends Transform {
  /**
   * write request to socket
   * Connection#writeRequest and Connection#oneway will call the function.
   * @param {number} id - the request id
   * @param {Object} req - the request object should be encoded
   * @param {Function} cb - the encode callback
   */
  writeRequest(id: number, req: object, cb);
  /**
   * write response to socket
   * Connection#writeResponse will call the function.
   * @param {Object} req - the request object
   * @param {Object} res - the response object should be encoded
   * @param {Function} cb - the encode callback
   */
  writeResponse(req: object, res: object, cb);
}

interface Decoder extends Writable {
  // events
  // - request emit when have request packet
  // - heartbeat emit when have heartbeat packet
  // - response emit when have response packet
}

interface Protocol {
  name: string;
  encode(options: any): Encoder;
  decode(options: any): Decoder;
}

License

MIT

Contributors

|killagu|gxcsoccer|popomore|weijiafu14| | :---: | :---: | :---: | :---: |

This project follows the git-contributor spec, auto updated at Wed Jun 19 2024 17:29:25 GMT+0800.