<commit_before>import path from 'path';
import compile, {compiler} from '../compile.js';

/**
 * Provides a middleware that can compile Cap'n Proto schema files into their binary representation and return them to
 * the client.
 *
 * Will return a `501` error if the Cap'n Proto compiler cannot be found on the server.
 * @param {String} root the root location of the schema files to return
 * @returns {Function} a middleware function that can handle HTTP requests
 */
export default function(root) {
  return function(request, response, next) {
    const {accept} = request.headers;
    if (['application/x-capnp-schema-binary', 'application/*', '*/*'].indexOf(accept) === -1) {
      response.writeHead(406, {'Content-Type': 'application/json'});
      response.end(JSON.stringify({error: `Invalid accepts: ${accept}`}));
      return;
    }

    const url = new URL(`${location.origin}${request.url}`);
    compiler()
      .then(executable => {
        return compile(path.join(root, url.pathname), executable)
          .then(schema => {
            response.writeHead(200, {'Content-Type': 'application/x-capnp-schema-binary'});
            response.end(Buffer.from(schema, 0, schema.length));
          })
          .catch(err => {
            response.writeHead(500, {'Content-Type': 'application/json'});
            response.end(JSON.stringify({error: `Failed to compile schema: ${err.message}`}));
          });
      }, err => {
        response.writeHead(501, {'Content-Type': 'application/json'});
        response.end(JSON.stringify({error: `Failed to find compiler: ${err}`}));<commit_msg>Use the byte length of the buffer<commit_after>import path from 'path';
import compile, {compiler} from '../compile.js';

/**
 * Provides a middleware that can compile Cap'n Proto schema files into their binary representation and return them to
 * the client.
 *
 * Will return a `501` error if the Cap'n Proto compiler cannot be found on the server.
 * @param {String} root the root location of the schema files to return
 * @returns {Function} a middleware function that can handle HTTP requests
 */
export default function(root) {
  return function(request, response, next) {
    const {accept} = request.headers;
    if (['application/x-capnp-schema-binary', 'application/*', '*/*'].indexOf(accept) === -1) {
      response.writeHead(406, {'Content-Type': 'application/json'});
      response.end(JSON.stringify({error: `Invalid accepts: ${accept}`}));
      return;
    }

    const url = new URL(`${location.origin}${request.url}`);
    compiler()
      .then(executable => {
        return compile(path.join(root, url.pathname), executable)
          .then(schema => {
            response.writeHead(200, {'Content-Type': 'application/x-capnp-schema-binary'});
            response.end(Buffer.from(schema, 0, schema.byteLength));
          })
          .catch(err => {
            response.writeHead(500, {'Content-Type': 'application/json'});
            response.end(JSON.stringify({error: `Failed to compile schema: ${err.message}`}));
          });
      }, err => {
        response.writeHead(501, {'Content-Type': 'application/json'});
        response.end(JSON.stringify({error: `Failed to find compiler: ${err}`}));
{'fraction_non_alphanumeric': 0.15451174289245984, 'fraction_numerical': 0.010506798516687269, 'mean_word_length': 3.2874172185430464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6782666', 'n_tokens_mistral': 922, 'n_tokens_neox': 901, 'n_words': 261}