{"size": 1288, "ext": "js", "max_stars_repo_path": "examples/multipart-parser.js", "max_stars_repo_name": "akindr/formidable", "max_stars_repo_stars_event_min_datetime": "2020-02-25T12:52:03.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-30T21:35:19.000Z", "max_issues_repo_path": "examples/multipart-parser.js", "max_issues_repo_name": "Acidburn0zzz/formidable", "max_issues_repo_issues_event_min_datetime": "2020-02-25T14:05:18.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T15:07:45.000Z", "max_forks_repo_path": "examples/multipart-parser.js", "max_forks_repo_name": "Acidburn0zzz/formidable", "max_forks_repo_forks_event_min_datetime": "2020-02-25T13:56:15.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T15:08:02.000Z"}
{"max_stars_count": 1042, "max_issues_count": 258, "max_forks_count": 100, "avg_line_length": 42.9333333333, "max_line_length": 478, "alphanum_fraction": 0.6871118012}
'use strict';

const { MultipartParser } = require('../src/index');

// hand crafted multipart
const boundary = '--abcxyz';
const next = '\r\n';
const formData = 'Content-Disposition: form-data; ';
const bufferToWrite = Buffer.from(
  `${boundary}${next}${formData}name="text"${next}${next}some text ...${next}${next}${boundary}${next}${formData}name="z"${next}${next}text inside z${next}${next}${boundary}${next}${formData}name="file1"; filename="a.txt"${next}Content-Type: text/plain${next}${next}Content of a.txt.${next}${next}${boundary}${next}${formData}name="file2"; filename="a.html"${next}Content-Type: text/html${next}${next}<!DOCTYPE html><title>Content of a.html.</title>${next}${next}${boundary}--`,
);

const multipartParser = new MultipartParser();
multipartParser.on('data', ({ name, buffer, start, end }) => {
  console.log(`${name}:`);
  if (buffer && start && end) {
    console.log(String(buffer.slice(start, end)));
  }
  console.log();
});
multipartParser.on('error', (error) => {
  console.error(error);
});

multipartParser.initWithBoundary(boundary.substring(2)); // todo make better error message when it is forgotten
// const shouldWait = !multipartParser.write(buffer);
multipartParser.write(bufferToWrite);
multipartParser.end();
// multipartParser.destroy();
