<commit_before>// @flow

import net from 'net';
import type { Socket } from 'net';
import { attachHandlers, setEncoding } from './socket';

// List of currently active sockets
let socketCounter = 0;
const activeSockets: { [string]: Socket } = {};

// A queue for startup events while server is not yet ready
const startUpQueue = [];

// Check if ip is localhost
function isValidIP(address: any): boolean {
  return (
    address === '127.0.0.1' ||
    address === '::ffff:127.0.0.1'
  );
}

// Create local socket server for communication between R and Node.js
const server = net.createServer((socket: Socket) => {
  // Kill all connections that are not from localhost
  if (!isValidIP(socket.remoteAddress)) {
    socket.destroy();
  }

  // Set socket encoding
  setEncoding(socket);

  // Add the new socket to active sockets list
  socketCounter += 1;
  const socketId = `s${socketCounter}`;
  activeSockets[socketId] = socket;

  // Add close listener to socket so we can remove it once it closes
  socket.on('close', () => delete activeSockets[socketId]);

  // Attach event handlers to the socket
  attachHandlers(socket);
});

// Clear statup queue after we get ready
server.on('listening', () => {
  while (startUpQueue.length > 0) {
    startUpQueue.shift()();
  }
});

// Start the local socket server
export const listen = (port: number) => server.listen(port);

// Check if server is listening
export const whenReady = (callback: () => void) => (
  server.listening ? callback() : startUpQueue.push(callback)
);

// Stop server
export const closeServer = (): Promise<void> => new Promise((resolve) => {
  server.close(() => resolve());
  Object.keys(activeSockets).forEach((socketId) => {
    activeSockets[socketId].destroy();
    delete activeSockets[socketId];
  });
});
<commit_msg>Made server close method also unref it so process can exit<commit_after>// @flow

import net from 'net';
import type { Socket } from 'net';
import { attachHandlers, setEncoding } from './socket';

// List of currently active sockets
let socketCounter = 0;
const activeSockets: { [string]: Socket } = {};

// A queue for startup events while server is not yet ready
const startUpQueue = [];

// Check if ip is localhost
function isValidIP(address: any): boolean {
  return (
    address === '127.0.0.1' ||
    address === '::ffff:127.0.0.1'
  );
}

// Create local socket server for communication between R and Node.js
const server = net.createServer((socket: Socket) => {
  // Kill all connections that are not from localhost
  if (!isValidIP(socket.remoteAddress)) {
    socket.destroy();
  }

  // Set socket encoding
  setEncoding(socket);

  // Add the new socket to active sockets list
  socketCounter += 1;
  const socketId = `s${socketCounter}`;
  activeSockets[socketId] = socket;

  // Add close listener to socket so we can remove it once it closes
  socket.on('close', () => delete activeSockets[socketId]);

  // Attach event handlers to the socket
  attachHandlers(socket);
});

// Clear statup queue after we get ready
server.on('listening', () => {
  while (startUpQueue.length > 0) {
    startUpQueue.shift()();
  }
});

// Start the local socket server
export const listen = (port: number) => server.listen(port);

// Check if server is listening
export const whenReady = (callback: () => void) => (
  server.listening ? callback() : startUpQueue.push(callback)
);

// Stop server
export const closeServer = (): Promise<void> => new Promise((resolve) => {
  server.close(() => {
    server.unref();
    resolve();
  });
  Object.keys(activeSockets).forEach((socketId) => {
    activeSockets[socketId].destroy();
    delete activeSockets[socketId];
  });
});

{'fraction_non_alphanumeric': 0.14180729450190527, 'fraction_numerical': 0.008165487207403375, 'mean_word_length': 3.791395045632334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '6385207', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1074, 'n_words': 355}