<commit_before>#include "pcap_wrapper.hpp"
#include <cstring>
#include <node_buffer.h>
#include <sys/ioctl.h>

#define precondition(b) if (!(b)) return NanThrowError("Illegal arguments.")
#define check_handle_not_null(w) if ((w)->handle == NULL) \
  return NanThrowError("Inactive capture handle.")

using namespace v8;

// Async dispatcher.

class Dispatcher : public NanAsyncWorker {

public:

  Dispatcher(
    NanCallback *callback,
    pcap_t *pcap_handle,
    int batch_size,
    u_char *data,
    int break_len, // _After_ which dispatcher will break (-1 for no break).
    int len, // Throws an error if would write past this.
    std::vector<struct pcap_pkthdr> &headers,
    bool *running // Flag to signal end of dispatching.
  ) : NanAsyncWorker(callback) {

    _running = running;
    *_running = true;

    _batch_size = batch_size;
    _pcap_handle = pcap_handle;
    _cur = data;
    _break = break_len >= 0 ? data + break_len : NULL;
    _end = data + len;
    _broke = false;
    _headers = headers;

  }

  ~Dispatcher() {}

  void Execute() {

    int n = pcap_dispatch(
      _pcap_handle,
      _batch_size,
      on_packet,
      (u_char *) this
    );
    if (n == -2) {
      // Previous loop was broken, needed to clear flag. We try again.
      n = pcap_dispatch(
        _pcap_handle,
        _batch_size,
        on_packet,
        (u_char *) this
      );
    }

    // Note that we don't propagate the return value of the dispatch call. When
    // reading a file, we can't rely on the output value of it anyway so we
    // always rely on the length of the headers vector instead.

    if (n == -1) {
      SetErrorMessage(pcap_geterr(_pcap_handle));
    }

  }

  void HandleOKCallback() {

    NanScope();
    *_running = false;

    Local<Value> caplen_key = NanNew<String>("capLen");
    Local<Value> len_key = NanNew<String>("len");

    // Create packet header objects.
    int i;
    int l = _headers.size();
    Local<Array> headers = NanNew<Array>(l);
    for (i = 0; i < l; i++) {
      struct pcap_pkthdr hdr = _headers[i];
      Local<Object> obj = NanNew<Object>();
      // obj->Set(NanNew<String>("timeVal"), NanNew<Number>(hdr.ts)); TODO
      obj->Set(caplen_key, NanNew<Number>(hdr.caplen));
      obj->Set(len_key, NanNew<Number>(hdr.len));
      headers->Set(NanNew<Integer>(i), obj);
    }

    Local<Value> argv[] = {
      NanNull(), // Error.
      headers,
      NanNew<Boolean>(_broke)
    };

    callback->Call(3, argv);

  }

  void HandleErrorCallback() {
<commit_msg>Change CPP fetch API.<commit_after>#include "pcap_wrapper.hpp"
#include <cstring>
#include <node_buffer.h>
#include <sys/ioctl.h>

#define precondition(b) if (!(b)) return NanThrowError("Illegal arguments.")
#define check_handle_not_null(w) if ((w)->handle == NULL) \
  return NanThrowError("Inactive capture handle.")

using namespace v8;

// Async dispatcher.

class Dispatcher : public NanAsyncWorker {

public:

  Dispatcher(
    NanCallback *callback,
    pcap_t *pcap_handle,
    int batch_size,
    u_char *data,
    int break_len, // _After_ which dispatcher will break (-1 for no break).
    int len, // Throws an error if would write past this.
    std::vector<struct pcap_pkthdr> &headers,
    bool *running // Flag to signal end of dispatching.
  ) : NanAsyncWorker(callback) {

    _running = running;
    *_running = true;

    _batch_size = batch_size;
    _pcap_handle = pcap_handle;
    _cur = data;
    _break = break_len >= 0 ? data + break_len : NULL;
    _end = data + len;
    _broke = false;
    _headers = headers;

  }

  ~Dispatcher() {}

  void Execute() {

    int n = pcap_dispatch(
      _pcap_handle,
      _batch_size,
      on_packet,
      (u_char *) this
    );
    if (n == -2) {
      // Previous loop was broken, needed to clear flag. We try again.
      n = pcap_dispatch(
        _pcap_handle,
        _batch_size,
        on_packet,
        (u_char *) this
      );
    }

    // Note that we don't propagate the return value of the dispatch call. When
    // reading a file, we can't rely on the output value of it anyway so we
    // always rely on the length of the headers vector instead.

    if (n == -1) {
      SetErrorMessage(pcap_geterr(_pcap_handle));
    }

  }

  void HandleOKCallback() {

    NanScope();
    *_running = false;

    int offset = 0;
    int l = _headers.size();

    Local<Value> argv[] = {
      NanNull(), // Error.
      NanNew<Integer>(l), // Start offset (or total frames in first call).
      NanNew<Integer>(0), // End offset.
      NanFalse() // Overflow.
    };

    callback->Call(4, argv);

    // Create packet header objects.
    int i;
    for (i = 0; i < l; i++) {
      struct pcap_pkthdr hdr = _headers[i];
      argv[1] = argv[2];
      argv[2] = NanNew<Integer>(offset + hdr.caplen);
      argv[3] = hdr.len > hdr.caplen ? NanTrue() : NanFalse();
      callback->Call(4, argv);
      offset += hdr.caplen;
    }

  }

  void HandleErrorCallback() {

{'fraction_non_alphanumeric': 0.12653389660028164, 'fraction_numerical': 0.004224502112251056, 'mean_word_length': 2.6055112400290064, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7215269', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1645, 'n_words': 467}