{"size": 1630, "ext": "js", "max_stars_repo_path": "threads-test.js", "max_stars_repo_name": "Kneelawk/Threads-Test", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "threads-test.js", "max_issues_repo_name": "Kneelawk/Threads-Test", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "threads-test.js", "max_forks_repo_name": "Kneelawk/Threads-Test", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.0769230769, "max_line_length": 85, "alphanum_fraction": 0.6564417178}
const fs = require('fs');
const express = require('express');
const body_parser = require('body-parser');
const Png = require('pngjs').PNG;
const threads_test_native = require('bindings')('threads_test_native');

if (!fs.existsSync('fractals')) {
  fs.mkdirSync('fractals');
}

let app = express();

app.use(body_parser.json());

let width, height;

threads_test_native.setDoneCallback((data) => {
  let buffer = new Buffer(data);
  let png = new Png({
    width,
    height,
    hasInputAlpha: true
  });
  png.data = buffer;
  png.pack().pipe(fs.createWriteStream('fractals/fractal.png'));
});

app.post('/api/generate', (req, res) => {
  let progress = threads_test_native.getProgress();
  if (!progress.generating) {
    threads_test_native.generateFractal(
      req.body.imageWidth || 300,
      req.body.imageHeight || 200,
      req.body.fractalWidth || 3,
      req.body.fractalHeight || 2,
      req.body.fractalX || 0,
      req.body.fractalY || 0,
      req.body.iterations || 100);

    width = req.body.imageWidth || 300;
    height = req.body.imageHeight || 200;

    return res.send(req.body);
  } else {
    return res.send(progress);
  }
});

app.get('/api/progress', (req, res) => {
  return res.send(threads_test_native.getProgress());
});

app.get('/api/result', (req, res) => {
  let progress = threads_test_native.getProgress();
  if (!progress.generating) {
    res.type('image/png');
    // node is single threaded, this will not happend while the file is being written
    return fs.createReadStream('fractals/fractal.png').pipe(res);
  } else {
    return res.send(progress);
  }
});

app.listen(8080);
