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

pcap-socket

v0.5.11

Published

Mock TCP socket based on pcap file data.

Downloads

8

Readme

pcap-socket

Mock TCP socket based on pcap file data.

This module is a pure JavaScript implementation built on top of the pcap-parser module.

Build Status

Example

Here is an example test case using a recorded HTTP request to verify that the typical node.js hello world server responds correctly.

'use strict';

var PcapSocket = require('pcap-socket');

var http = require('http');
var path = require('path');

module.exports.http = function(test) {
  test.expect(3);

  var msg = 'Hello World\n';

  // Setup an HTTP server to test
  var server = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(msg);
  });

  // Configure the pcap socket to provide real, recorded network data
  var file = path.join(__dirname, 'data', 'http-session-winxp.pcap');
  var psocket = new PcapSocket(file, '10.0.1.6');

  // When the server sends back a packet, validate that it makes sense
  psocket.output.on('readable', function() {
    var chunk = psocket.output.read();
    if (chunk) {
      var str = chunk.toString();

      test.ok(str.match(/HTTP\/1.1 200 OK/));
      test.ok(str.match(/Content-Type: text\/plain/));
      test.ok(str.match(new RegExp(msg)));

      test.done();
    }
  });
  psocket.output.read(0);

  // Supply the pcap socket to the HTTP server as a new connection
  server.emit('connection', psocket);
};

Limitations / TODO

  • Only supports IPv4 at the moment.
  • Do something more intelligent with duplicate and out-of-order TCP packets. Currently packets are delivered as they are seen by pcap. No attempt is made to de-duplicate or re-order packets.

Class PcapSocket

The PcapSocket class inherits from Duplex. Therefore it provides both streaming Readable and Writable interfaces.

Calling read() will return bytes sent to the configured address in the pcap file.

Calling write() will direct bytes to the output stream. This allows your test code to monitor the output stream to validate that your code is sending the correct values.

Note, while PcapSocket uses the new streams2 API provided in node 0.9.6 and greater, this class should still work in older versions of node. This backward compatibility is implemented using the readable-stream module.

var psock = new PcapSocket(pcapSource, address, opts)

  • pcapSource {String | Stream} If a String, pcapSource is interpreted as the name of a pcap file to read from. Otherwise pcapSource is treated as a stream providing pcap data.
  • address {String} An IPv4 address used in the pcap file. The socket will act as that IP address. Packets sent to this address will be available on the socket's read() method.
  • opts {Object | null} Optional parameters
    • localPort {Number | null} The TCP port associated with the address passed as the second argument. Packets sent to this port at the given address wil be available on the socket's read() method. If not provided then the port will be automatically set to the port used on the first TCP packet with data.
    • remoteAddress {String | null} The IPv4 address of the remote peer in the pcap file's TCP session. Only packets originating from this address will be available via read(). If not set, then the address will be automatically configured based on the first TCP packet with data.
    • remotePort {Number | null} The TCP port number of the remote pper in the pcap file's TCP session. Only packets originating from this port will be available via read(). If not set, then port will be automatically configured based on the first TCP packet with data.

psock.output

The output property provides a PassThrough stream. All data passed to the write() function will be directed into this stream. This allows test code to validate that the code using the socket writes out the correct values.

psock.address(), psock.localAddress, psock.localPort, psock.remoteAddress, psock.remotePort

These properties are provided in order to maintain compatibility with the net.Socket API.

If the localPort, remoteAddress, or remotePort are not set via the constructor options, then they will default to either the address '0.0.0.0' or port 0. Once a packet is processed they will then represent the selected TCP session addresses and ports.

If the properties changing is a problem for your code or tests, then make sure to set the addresses and ports via the constructor options.

psock.bytesRead, psock.bytesWritten

These properties are provided in order to maintain compatibility with the net.Socket API. They should work as expected.

pcock.setTimeout(), psock.setNoDelay(), psock.setKeepAlive(), psock.unref(), psock.ref()

These functions are provided in order to maintain compatibility with the net.Socket API. They are only stubs and effectively do nothing.