<commit_before>/**!
 * cnpmjs.org - controllers/registry/module.js
 *
 * Copyright(c) cnpmjs.org and other contributors.
 * MIT Licensed
 *
 * Authors:
 *  dead_horse <<EMAIL>> (http://deadhorse.me)
 *  fengmk2 <<EMAIL>> (http://fengmk2.github.com)
 */

'use strict';

/**
 * Module dependencies.
 */

var debug = require('debug')('cnpmjs.org:controllers:registry:module');
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var utility = require('utility');
var coRead = require('co-read');
var coWrite = require('co-write');
var urlparse = require('url').parse;
var mime = require('mime');
var semver = require('semver');
var ms = require('ms');
var config = require('../../config');
var Module = require('../../proxy/module');
var Total = require('../../proxy/total');
var nfs = require('../../common/nfs');
var common = require('../../lib/common');
var Log = require('../../proxy/module_log');
var DownloadTotal = require('../../proxy/download');
var SyncModuleWorker = require('../../proxy/sync_module_worker');
var logger = require('../../common/logger');
var ModuleDeps = require('../../proxy/module_deps');

/**
 * show all version of a module
 */
exports.show = function *(next) {
  var name = this.params.name;

  var r = yield [Module.listTags(name), Module.listByName(name)];
  var tags = r[0];
  var rows = r[1];

  debug('show module, user: %s, allowSync: %s, isAdmin: %s',
    this.session.name, this.session.allowSync, this.session.isAdmin);
  // if module not exist in this registry,
  // sync the module backend and return package info from official registry
  if (rows.length === 0) {
    if (!this.session.allowSync) {
      this.status = 404;
      this.body = {
        error: 'not_found',
        reason: 'document not found'
      };
      return;
    }
    var username = (this.session && this.session.name) || 'anonymous';
    var result = yield SyncModuleWorker.sync(name, username);
    this.status = result.ok ? 200 : result.statusCode;
    this.body = result.pkg;
    return;
  }

  var nextMod = null;
  var latestMod = null;

  // set tags
  var distTags = {};
  for (var i = 0; i < tags.length; i++) {
    var t = tags[i];
    distTags[t.tag] = t.version;
  }

  // set versions and times
  var versions = {};
  var times = {};
  var attachments = {};
  for (var i = 0; i < rows.length; i++) {
    var row = rows[i];
    if (row.version === 'next') {
      nextMod = row;
      continue;
    }
    var pkg = row.package;
    common.setDownloadURL(pkg, this);
    pkg._cnpm_publish_time = row.publish_time;
    versions[pkg.version] = pkg;
    times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified;
    if ((!distTags.latest && !latestMod) || distTags.latest === row.version) {
      latestMod = row;
    }
  }

  if (!latestMod) {
    latestMod = nextMod || rows[0];
  }

  if (!nextMod) {
    nextMod = latestMod;
  }

  var rev = '';
  if (nextMod) {
    rev = String(nextMod.id);
  }

  var info = {
    _id: name,
    _rev: rev,
    name: name,
    description: latestMod.package.description,
    "dist-tags": distTags,
    maintainers: latestMod.package.maintainers,
    time: times,
    author: latestMod.package.author,
    repository: latestMod.package.repository,
    versions: versions,
    readme: latestMod.package.readme,
    _attachments: attachments,
  };

  debug('show module %s: %s, latest: %s', name, rev, latestMod.version);

  this.body = info;
};

/**<commit_msg>remove readme in versions<commit_after>/**!
 * cnpmjs.org - controllers/registry/module.js
 *
 * Copyright(c) cnpmjs.org and other contributors.
 * MIT Licensed
 *
 * Authors:
 *  dead_horse <<EMAIL>> (http://deadhorse.me)
 *  fengmk2 <<EMAIL>> (http://fengmk2.github.com)
 */

'use strict';

/**
 * Module dependencies.
 */

var debug = require('debug')('cnpmjs.org:controllers:registry:module');
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var utility = require('utility');
var coRead = require('co-read');
var coWrite = require('co-write');
var urlparse = require('url').parse;
var mime = require('mime');
var semver = require('semver');
var ms = require('ms');
var config = require('../../config');
var Module = require('../../proxy/module');
var Total = require('../../proxy/total');
var nfs = require('../../common/nfs');
var common = require('../../lib/common');
var Log = require('../../proxy/module_log');
var DownloadTotal = require('../../proxy/download');
var SyncModuleWorker = require('../../proxy/sync_module_worker');
var logger = require('../../common/logger');
var ModuleDeps = require('../../proxy/module_deps');

/**
 * show all version of a module
 */
exports.show = function *(next) {
  var name = this.params.name;

  var r = yield [Module.listTags(name), Module.listByName(name)];
  var tags = r[0];
  var rows = r[1];

  debug('show module, user: %s, allowSync: %s, isAdmin: %s',
    this.session.name, this.session.allowSync, this.session.isAdmin);
  // if module not exist in this registry,
  // sync the module backend and return package info from official registry
  if (rows.length === 0) {
    if (!this.session.allowSync) {
      this.status = 404;
      this.body = {
        error: 'not_found',
        reason: 'document not found'
      };
      return;
    }
    var username = (this.session && this.session.name) || 'anonymous';
    var result = yield SyncModuleWorker.sync(name, username);
    this.status = result.ok ? 200 : result.statusCode;
    this.body = result.pkg;
    return;
  }

  var nextMod = null;
  var latestMod = null;
  var readme;
  // set tags
  var distTags = {};
  for (var i = 0; i < tags.length; i++) {
    var t = tags[i];
    distTags[t.tag] = t.version;
  }

  // set versions and times
  var versions = {};
  var times = {};
  var attachments = {};
  for (var i = 0; i < rows.length; i++) {
    var row = rows[i];
    if (row.version === 'next') {
      nextMod = row;
      continue;
    }
    var pkg = row.package;
    common.setDownloadURL(pkg, this);
    pkg._cnpm_publish_time = row.publish_time;
    versions[pkg.version] = pkg;
    times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified;
    if ((!distTags.latest && !latestMod) || distTags.latest === row.version) {
      latestMod = row;
      readme = pkg.readme;
    }
    delete pkg.readme;
  }

  if (!latestMod) {
    latestMod = nextMod || rows[0];
  }

  if (!nextMod) {
    nextMod = latestMod;
  }

  var rev = '';
  if (nextMod) {
    rev = String(nextMod.id);
  }

  var info = {
    _id: name,
    _rev: rev,
    name: name,
    description: latestMod.package.description,
    "dist-tags": distTags,
    maintainers: latestMod.package.maintainers,
    time: times,
    author: latestMod.package.author,
    repository: latestMod.package.repository,
    versions: versions,
    readme: readme,
    _attachments: attachments,
  };

  debug('show module %s: %s, latest: %s', name, rev, latestMod.version);

  this.body = info;
};

/**
{'fraction_non_alphanumeric': 0.17314537946746406, 'fraction_numerical': 0.0039869001851060805, 'mean_word_length': 3.0367816091954025, 'pattern_counts': {'":': 2, '<': 15, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '945821', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2281, 'n_words': 587}