<commit_before>var sniffer = require('mapbox-file-sniff');
var path = require('path');

var tilelive = require('tilelive');
var Vector = require('tilelive-vector');
var MBTiles = require('mbtiles');
var Omnivore = require('@mapbox/tilelive-omnivore');
var TileJSON = require('tilejson');
var Serialtiles = require('../lib/tilelive-serialtiles');
var Mapbox = require('../lib/tilelive-mapbox');
var invalid = require('./invalid');
var kmlValidator = require('./validators/kml.js');

Vector.registerProtocols(tilelive);
MBTiles.registerProtocols(tilelive);
Omnivore.registerProtocols(tilelive);
TileJSON.registerProtocols(tilelive);
Serialtiles.registerProtocols(tilelive);
Mapbox.registerProtocols(tilelive);

Vector.mapnik.register_fonts(path.dirname(require.resolve('mapbox-studio-default-fonts')), { recurse: true });
Vector.mapnik.register_fonts(path.dirname(require.resolve('mapbox-studio-pro-fonts')), { recurse: true });
if (process.env.MapboxUploadValidateFonts)
  Vector.mapnik.register_fonts(process.env.MapboxUploadValidateFonts, { recurse: true });

module.exports.filepath = function validateFilepath(filepath, callback) {
  filepath = path.resolve(filepath);
  sniffer.quaff(filepath, true, function(err, protocol) {
    if (err) return callback(invalid(err));
    sniffer.quaff(filepath, false, function(err, filetype) {
      if (err) return callback(invalid(err));
      return callback(null, {
        protocol: protocol,
        filetype: filetype
      });
    });
  });
};

module.exports.info = function validateInfo(info, limits, callback) {
  if (!callback) {
    callback = limits;
    limits = { max_metadata: 60 * 1024 };
  }

  // Lightweight KML validation before gdal/mapnik KML drivers
  // which never finish if KMLs have too many layers
  if (info.filetype === 'kml') {
    var kmlValid = kmlValidator(info.filepath);
    if (kmlValid !== true) {
      return callback(invalid(kmlValid));
    }
  }

  tilelive.info(info.uri, function(err, info) {
    if (err) return callback(invalid(err));
    if (info.prepare) return callback(invalid('Source cannot contain prepare key'));

    err = tilelive.verify(info);
    if (err) return callback(invalid(err));
<commit_msg>maxzoom validation workaround<commit_after>var sniffer = require('mapbox-file-sniff');
var path = require('path');

var tilelive = require('tilelive');
var Vector = require('tilelive-vector');
var MBTiles = require('mbtiles');
var Omnivore = require('@mapbox/tilelive-omnivore');
var TileJSON = require('tilejson');
var Serialtiles = require('../lib/tilelive-serialtiles');
var Mapbox = require('../lib/tilelive-mapbox');
var invalid = require('./invalid');
var kmlValidator = require('./validators/kml.js');

Vector.registerProtocols(tilelive);
MBTiles.registerProtocols(tilelive);
Omnivore.registerProtocols(tilelive);
TileJSON.registerProtocols(tilelive);
Serialtiles.registerProtocols(tilelive);
Mapbox.registerProtocols(tilelive);

Vector.mapnik.register_fonts(path.dirname(require.resolve('mapbox-studio-default-fonts')), { recurse: true });
Vector.mapnik.register_fonts(path.dirname(require.resolve('mapbox-studio-pro-fonts')), { recurse: true });
if (process.env.MapboxUploadValidateFonts)
  Vector.mapnik.register_fonts(process.env.MapboxUploadValidateFonts, { recurse: true });

module.exports.filepath = function validateFilepath(filepath, callback) {
  filepath = path.resolve(filepath);
  sniffer.quaff(filepath, true, function(err, protocol) {
    if (err) return callback(invalid(err));
    sniffer.quaff(filepath, false, function(err, filetype) {
      if (err) return callback(invalid(err));
      return callback(null, {
        protocol: protocol,
        filetype: filetype
      });
    });
  });
};

module.exports.info = function validateInfo(info, limits, callback) {
  if (!callback) {
    callback = limits;
    limits = { max_metadata: 60 * 1024 };
  }

  // Lightweight KML validation before gdal/mapnik KML drivers
  // which never finish if KMLs have too many layers
  if (info.filetype === 'kml') {
    var kmlValid = kmlValidator(info.filepath);
    if (kmlValid !== true) {
      return callback(invalid(kmlValid));
    }
  }

  tilelive.info(info.uri, function(err, info) {
    if (err) return callback(invalid(err));
    if (info.prepare) return callback(invalid('Source cannot contain prepare key'));
    if (info.maxzoom > 23) return callback(invalid('Maxzoom exceeds z23'));

    err = tilelive.verify(Object.assign({}, info, { maxzoom: 22 }));
    if (err) return callback(invalid(err));

{'fraction_non_alphanumeric': 0.14560318161732214, 'fraction_numerical': 0.003977021652673442, 'mean_word_length': 4.956578947368421, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2433346', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1401, 'n_words': 292}