Skip to content

Commit

Permalink
mv proxy/module.js into services/package.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 15, 2014
1 parent d30260f commit aa135d2
Show file tree
Hide file tree
Showing 28 changed files with 1,576 additions and 1,029 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : false, // true: Require all defined variables be used
"unused" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"trailing" : false, // true: Prohibit trailing whitespaces
"maxparams" : false, // {int} Max number of formal params allowed per function
Expand Down
2 changes: 1 addition & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var config = {
// the storage engine for 'sqlite'
storage: path.join(root, 'database.sqlite'),

logging: false,
// logging: false,
},

// forward Compat with old style
Expand Down
10 changes: 5 additions & 5 deletions controllers/registry/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
* Module dependencies.
*/

var Module = require('../../proxy/module');
var Package = require('../../services/package');

module.exports = deprecateVersions;

/**
* @see https://github.com/cnpm/cnpmjs.org/issues/415
*/
function* deprecateVersions(next) {
function* deprecateVersions() {
var body = this.request.body;
var name = this.params.name || this.params[0];

var tasks = [];
for (var version in body.versions) {
tasks.push(Module.get(name, version));
tasks.push(Package.getModule(name, version));
}
var rs = yield tasks;

Expand All @@ -46,12 +46,12 @@ function* deprecateVersions(next) {
var data = body.versions[row.package.version];
if (typeof data.deprecated === 'string') {
row.package.deprecated = data.deprecated;
updateTasks.push(Module.updatePackage(row.id, row.package));
updateTasks.push(Package.updateModulePackage(row.id, row.package));
}
}
yield updateTasks;
// update last modified
yield* Module.updateLastModified(name);
yield* Package.updateModuleLastModified(name);

this.status = 201;
this.body = {
Expand Down
22 changes: 8 additions & 14 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@
*/

var debug = require('debug')('cnpmjs.org:controllers:registry:module');
var path = require('path');
var fs = require('fs');
var util = require('util');
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('../../services/total');
var nfs = require('../../common/nfs');
var common = require('../../lib/common');
Expand Down Expand Up @@ -325,7 +319,7 @@ exports.get = function* (next) {
this.body = {
error: 'not exist',
reason: 'version not found: ' + version
}
};
return;
}

Expand All @@ -338,7 +332,7 @@ exports.download = function *(next) {
var name = this.params.name || this.params[0];
var filename = this.params.filename || this.params[1];
var version = filename.slice(name.length + 1, -4);
var row = yield Module.get(name, version);
var row = yield* Package.getModule(name, version);
// can not get dist
var url = null;

Expand Down Expand Up @@ -374,7 +368,7 @@ exports.download = function *(next) {

_downloads[name] = (_downloads[name] || 0) + 1;

if (typeof dist.size === 'number') {
if (typeof dist.size === 'number' && dist.size > 0) {
this.length = dist.size;
}
this.type = mime.lookup(dist.key);
Expand Down Expand Up @@ -551,7 +545,7 @@ exports.addPackageAndDist = function *(next) {
debug('%s addPackageAndDist %s:%s, attachment size: %s, maintainers: %j, distTags: %j',
username, name, version, attachment.length, versionPackage.maintainers, distTags);

var exists = yield Module.get(name, version);
var exists = yield* Module.getModule(name, version);
var shasum;
if (exists) {
this.status = 403;
Expand Down Expand Up @@ -620,7 +614,7 @@ exports.addPackageAndDist = function *(next) {
mod.package.dist = dist;
_addDepsRelations(mod.package);

var addResult = yield Module.add(mod);
var addResult = yield* Package.addModule(mod);
debug('%s module: save file to %s, size: %d, sha1: %s, dist: %j, version: %s',
addResult.id, dist.tarball, dist.size, shasum, dist, version);

Expand Down Expand Up @@ -818,7 +812,7 @@ exports.removeWithVersions = function* (next) {
debug('no tag need to be remove');
}
// step 7: update last modified, make sure etag change
yield* Module.updateLastModified(name);
yield* Module.updateModuleLastModified(name);

this.status = 201;
this.body = { ok: true };
Expand Down Expand Up @@ -857,7 +851,7 @@ exports.removeTar = function* (next) {

var rs = yield [
Module.getById(id),
Module.get(name, version),
Package.getModule(name, version),
];
var revertTo = rs[0];
var mod = rs[1]; // module need to delete
Expand Down Expand Up @@ -1034,7 +1028,7 @@ exports.updateTag = function* () {
return;
}

var mod = yield Module.get(name, version);
var mod = yield* Package.getModule(name, version);
if (!mod) {
this.status = 403;
var reason = util.format('setting tag %s to unknown version: %s: %s/%s',
Expand Down
2 changes: 1 addition & 1 deletion controllers/registry/user_package.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.list = function* () {
var r = yield [
NpmModuleMaintainer.listByUsers(users),
// get the first user module by author field
Module.listNamesByAuthor(firstUser),
Package.listPublicModuleNamesByUser(firstUser),
];
var rows = r[0];
var firstUserModuleNames = r[1];
Expand Down
4 changes: 2 additions & 2 deletions docs/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ CREATE TABLE IF NOT EXISTS `dist_dir` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(200) NOT NULL COMMENT 'user name',
`name` varchar(200) NOT NULL COMMENT 'dir name',
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`date` varchar(20) COMMENT '02-May-2014 01:06',
PRIMARY KEY (`id`),
Expand All @@ -189,7 +189,7 @@ CREATE TABLE IF NOT EXISTS `dist_file` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(100) NOT NULL COMMENT 'user name',
`name` varchar(100) NOT NULL COMMENT 'file name',
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`date` varchar(20) COMMENT '02-May-2014 01:06',
`size` int(10) unsigned NOT NULL COMMENT 'file size' DEFAULT '0',
Expand Down
64 changes: 64 additions & 0 deletions models/dist_dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**!
* cnpmjs.org - models/dist_dir.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

/*
CREATE TABLE IF NOT EXISTS `dist_dir` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(200) NOT NULL COMMENT 'dir name',
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`date` varchar(20) COMMENT '02-May-2014 01:06',
PRIMARY KEY (`id`),
UNIQUE KEY `dist_dir_parent_name` (`parent`, `name`),
KEY `dist_dir_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dist dir info';
*/

module.exports = function (sequelize, DataTypes) {
return sequelize.define('DistDir', {
name: {
type: DataTypes.STRING(200),
allowNull: false,
comment: 'dir name',
},
parent: {
type: DataTypes.STRING(200),
allowNull: false,
defaultValue: '/',
comment: 'parent dir',
},
date: {
type: DataTypes.STRING(20),
allowNull: false,
comment: '02-May-2014 01:06'
}
}, {
tableName: 'dist_dir',
comment: 'dist dir info',
indexes: [
{
unique: true,
fields: ['parent', 'name']
},
{
fields: ['gmt_modified']
}
],
classMethods: {
}
});
};
82 changes: 82 additions & 0 deletions models/dist_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**!
* cnpmjs.org - models/dist_file.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

/*
CREATE TABLE IF NOT EXISTS `dist_file` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(100) NOT NULL COMMENT 'file name',
`parent` varchar(200) NOT NULL COMMENT 'parent dir' DEFAULT '/',
`date` varchar(20) COMMENT '02-May-2014 01:06',
`size` int(10) unsigned NOT NULL COMMENT 'file size' DEFAULT '0',
`sha1` varchar(40) COMMENT 'sha1 hex value',
`url` varchar(2048),
PRIMARY KEY (`id`),
UNIQUE KEY `dist_file_parent_name` (`parent`, `name`),
KEY `dist_file_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dist file info';
*/

module.exports = function (sequelize, DataTypes) {
return sequelize.define('DistFile', {
name: {
type: DataTypes.STRING(200),
allowNull: false,
comment: 'dir name',
},
parent: {
type: DataTypes.STRING(200),
allowNull: false,
defaultValue: '/',
comment: 'parent dir',
},
date: {
type: DataTypes.STRING(20),
allowNull: false,
comment: '02-May-2014 01:06'
},
size: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: 'file size'
},
sha1: {
type: DataTypes.STRING(40),
allowNull: false,
comment: 'sha1 hex value'
},
url: {
type: DataTypes.STRING(2048),
allowNull: false
}
}, {
tableName: 'dist_file',
comment: 'dist file info',
indexes: [
{
unique: true,
fields: ['parent', 'name']
},
{
fields: ['gmt_modified']
}
],
classMethods: {
}
});
};
5 changes: 4 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ function load(name) {
return sequelize.import(path.join(__dirname, name));
}

var models = module.exports = {
module.exports = {
sequelize: sequelize,
Module: load('module'),
ModuleKeyword: load('module_keyword'),
NpmModuleMaintainer: load('npm_module_maintainer'),
Tag: load('tag'),
User: load('user'),
Total: load('total'),
Download: load('download_total'),
Expand Down
8 changes: 5 additions & 3 deletions models/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* Module dependencies.
*/

var utility = require('utility');

/*
CREATE TABLE IF NOT EXISTS `module` (
`id` INTEGER NOT NULL auto_increment ,
Expand Down Expand Up @@ -100,7 +98,11 @@ module.exports = function (sequelize, DataTypes) {
}
],
classMethods: {

findByNameAndVersion: function* (name, version) {
return yield this.find({
where: { name: name, version: version }
});
}
}
});
};
Loading

0 comments on commit aa135d2

Please sign in to comment.