<commit_before>import * as path from 'path';
import * as defaultFs from 'fs';
import trumpet from 'trumpet';
import router from 'router';
import serveStatic from 'connect-gzip-static';

function injectConfig(transform, config) {
  transform.select('#u-wave-config')
    .createWriteStream()
    .end(JSON.stringify(config));
}

function injectTitle(transform, title) {
  transform.select('title')
    .createWriteStream()
    .end(title);
}

function injectResetKey(transform, key) {
  transform.select('#reset-data')
    .createWriteStream()
    .end(key);
}

export default function uwaveWebClient(uw, options = {}) {
  const {
    basePath = path.join(__dirname, '../public'),
    fs = defaultFs, // Should only be used by the dev server.
    title = 'üWave',
    ...clientOptions
  } = options;

  const clientRouter = router();

  return clientRouter
    .get('/', (req, res) => {
      const transform = trumpet();
      injectTitle(transform, title);
      injectConfig(transform, clientOptions);

      fs.createReadStream(path.join(basePath, 'index.html'), 'utf8')
        .pipe(transform)
        .pipe(res);
    })
    .get('/reset/:key', (req, res) => {
      const transform = trumpet();
      injectTitle(transform, title);
      injectConfig(transform, { apiUrl: clientOptions.apiUrl });
      injectResetKey(transform, req.params.key);

      fs.createReadStream(path.join(basePath, 'password-reset.html'), 'utf8')
        .pipe(transform)
        .pipe(res);
    })
    .use(serveStatic(basePath));
}<commit_msg>Expose client config as JSON (#774)<commit_after>import * as path from 'path';
import * as defaultFs from 'fs';
import trumpet from 'trumpet';
import router from 'router';
import serveStatic from 'connect-gzip-static';

function injectConfig(transform, config) {
  transform.select('#u-wave-config')
    .createWriteStream()
    .end(JSON.stringify(config));
}

function injectTitle(transform, title) {
  transform.select('title')
    .createWriteStream()
    .end(title);
}

function injectResetKey(transform, key) {
  transform.select('#reset-data')
    .createWriteStream()
    .end(key);
}

export default function uwaveWebClient(uw, options = {}) {
  const {
    basePath = path.join(__dirname, '../public'),
    fs = defaultFs, // Should only be used by the dev server.
    title = 'üWave',
    ...clientOptions
  } = options;

  const clientRouter = router();

  return clientRouter
    .get('/', (req, res) => {
      const transform = trumpet();
      injectTitle(transform, title);
      injectConfig(transform, clientOptions);

      fs.createReadStream(path.join(basePath, 'index.html'), 'utf8')
        .pipe(transform)
        .pipe(res);
    })
    .get('/reset/:key', (req, res) => {
      const transform = trumpet();
      injectTitle(transform, title);
      injectConfig(transform, { apiUrl: clientOptions.apiUrl });
      injectResetKey(transform, req.params.key);

      fs.createReadStream(path.join(basePath, 'password-reset.html'), 'utf8')
        .pipe(transform)
        .pipe(res);
    })
    .get('/u-wave-web-config.json', (req, res) => {
      res.json(clientOptions);
    })
    .use(serveStatic(basePath));
}
{'fraction_non_alphanumeric': 0.15362776025236594, 'fraction_numerical': 0.0022082018927444794, 'mean_word_length': 3.39196675900277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '6672942', 'n_tokens_mistral': 972, 'n_tokens_neox': 956, 'n_words': 213}