<commit_before>import http from 'http';
import FormData from 'form-data/lib/form_data';
import fs from 'fs';

export type RequestOptions = {
  hostname: string,
  method: string,
  port: number,
  headers?: {
    'Content-Type': string,
  },
};

export type RequestPayload = {
  application: string,
  version: string,
  build: string,
  os: string,
  logs: Array<string>,
  date: string,
  magic: number,
  type: {
    type : string,
    email: string,
    subject: string,
    problem: string,
  }
};

function typedHttpRequest<Response>(
  httpOptions: RequestOptions, requestPayload?: RequestPayload
): Promise<Response> {
  return new Promise((resolve, reject) => {
    const options: RequestOptions = Object.assign({}, httpOptions);
    const payload: RequestPayload = Object.assign({}, requestPayload);
    // Prepare multipart/form-data
    const formData = new FormData();
    formData.append('payload', JSON.stringify(payload));

    // prepare file stream (attachment)
    if (payload.compressedLog) {
      const stream = fs.createReadStream(payload.compressedLog);
      formData.append('logs.zip', stream);
    }

    options.headers = formData.getHeaders();
    const httpRequest = http.request(options);

    // Attach form-data to the request
    formData.pipe(httpRequest);

    httpRequest.on('response', (response) => {
      if (response.statusCode !== 200) {
        return reject();
      }
      response.on('data', (chunk) => {
        console.debug('response.data', chunk);
      });
      response.on('error', (error) => {
        console.debug('response.error', error);
        reject(error);
      });
      response.on('end', () => {
        console.debug('response.end');
        resolve();
      });
    });
    httpRequest.on('error', (error) => reject(error));
  });
}

export const request = typedHttpRequest;
<commit_msg>[DDW-5] Remove console logging<commit_after>import http from 'http';
import FormData from 'form-data/lib/form_data';
import fs from 'fs';

export type RequestOptions = {
  hostname: string,
  method: string,
  port: number,
  headers?: {
    'Content-Type': string,
  },
};

export type RequestPayload = {
  application: string,
  version: string,
  build: string,
  os: string,
  logs: Array<string>,
  date: string,
  magic: number,
  type: {
    type : string,
    email: string,
    subject: string,
    problem: string,
  }
};

function typedHttpRequest<Response>(
  httpOptions: RequestOptions, requestPayload?: RequestPayload
): Promise<Response> {
  return new Promise((resolve, reject) => {
    const options: RequestOptions = Object.assign({}, httpOptions);
    const payload: RequestPayload = Object.assign({}, requestPayload);
    // Prepare multipart/form-data
    const formData = new FormData();
    formData.append('payload', JSON.stringify(payload));

    // prepare file stream (attachment)
    if (payload.compressedLog) {
      const stream = fs.createReadStream(payload.compressedLog);
      formData.append('logs.zip', stream);
    }

    options.headers = formData.getHeaders();
    const httpRequest = http.request(options);

    // Attach form-data to the request
    formData.pipe(httpRequest);

    httpRequest.on('response', (response) => {
      if (response.statusCode !== 200) {
        return reject();
      }
      response.on('data', () => {});
      response.on('error', (error) => {
        reject(error);
      });
      response.on('end', () => {
        resolve();
      });
    });
    httpRequest.on('error', (error) => reject(error));
  });
}

export const request = typedHttpRequest;

{'fraction_non_alphanumeric': 0.14672245467224546, 'fraction_numerical': 0.0019525801952580196, 'mean_word_length': 3.243786982248521, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '637349', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1041, 'n_words': 251}