Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: list all package versions by date (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Feb 23, 2020
1 parent 2cea5cd commit 3cb3fe0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 5 deletions.
37 changes: 37 additions & 0 deletions controllers/registry/package/list_versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const moment = require('moment');
const packageService = require('../../../services/package');

// GET /-/allversions?date={2020-02-20}
// List all packages versions sync at date(gmt_modified)

module.exports = function* () {
const query = this.query;
const date = moment(query.date, 'YYYY-MM-DD');
if (!date.isValid()) {
this.status = 400;
const error = '[query_parse_error] Invalid value for `date`, should be `YYYY-MM-DD` format.';
this.body = {
error,
reason: error,
};
return;
}

const today = date.format('YYYY-MM-DD');
const rows = yield packageService.findAllModuleAbbreviateds({
gmt_modified: {
$gte: `${today} 00:00:00`,
$lte: `${today} 23:59:59`,
},
});
this.body = rows.map(row => {
return {
name: row.name,
version: row.version,
publish_time: new Date(row.publish_time),
gmt_modified: row.gmt_modified,
};
});
};
4 changes: 4 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ exports.isLocalModule = function (mods) {
};

exports.isPrivateScopedPackage = function (name) {
if (!name) {
return false;
}

if (name[0] !== '@') {
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var unpublishable = require('../middleware/unpublishable');
var showTotal = require('../controllers/total');

var listAll = require('../controllers/registry/package/list_all');
var listAllPackageVersions = require('../controllers/registry/package/list_versions');
var listShorts = require('../controllers/registry/package/list_shorts');
var listSince = require('../controllers/registry/package/list_since');
var listAllVersions = require('../controllers/registry/package/list');
Expand Down Expand Up @@ -51,6 +52,8 @@ function routes(app) {
// get all module names, for auto completion
app.get('/-/short', listShorts);

app.get('/-/allversions', listAllPackageVersions);

// module
// scope package: params: [$name]
app.get(/^\/(@[\w\-\.]+\/[^\/]+)$/, syncByInstall, listAllVersions);
Expand Down
12 changes: 12 additions & 0 deletions services/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ exports.listModuleAbbreviatedsByName = function* (name) {
return rows;
};

exports.findAllModuleAbbreviateds = function* (where, order, limit, offset) {
const params = {
where,
order,
limit,
offset,
attributes: [ 'name', 'version', 'publish_time', 'gmt_modified' ],
};
const rows = yield models.ModuleAbbreviated.findAll(params);
return rows;
};

// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#abbreviated-version-object
exports.saveModuleAbbreviated = function* (mod) {
var pkg = JSON.stringify({
Expand Down
8 changes: 4 additions & 4 deletions test/controllers/registry/package/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe('test/controllers/registry/package/list.test.js', () => {
assert(Object.keys(data.versions).length > 0);
for (const v in data.versions) {
const pkg = data.versions[v];
assert('_hasShrinkwrap' in pkg);
// assert('_hasShrinkwrap' in pkg);
assert(pkg.publish_time && typeof pkg.publish_time === 'number');
assert(pkg._publish_on_cnpm === undefined);
}
Expand All @@ -344,7 +344,7 @@ describe('test/controllers/registry/package/list.test.js', () => {
assert(Object.keys(data.versions).length > 0);
for (const v in data.versions) {
const pkg = data.versions[v];
assert('_hasShrinkwrap' in pkg);
// assert('_hasShrinkwrap' in pkg);
assert(pkg.publish_time && typeof pkg.publish_time === 'number');
assert(pkg._publish_on_cnpm === undefined);
}
Expand Down Expand Up @@ -382,7 +382,7 @@ describe('test/controllers/registry/package/list.test.js', () => {
assert(Object.keys(data.versions).length > 0);
for (const v in data.versions) {
const pkg = data.versions[v];
assert('_hasShrinkwrap' in pkg);
// assert('_hasShrinkwrap' in pkg);
assert(pkg.publish_time && typeof pkg.publish_time === 'number');
assert(pkg._publish_on_cnpm === undefined);
}
Expand All @@ -407,7 +407,7 @@ describe('test/controllers/registry/package/list.test.js', () => {
assert(Object.keys(data.versions).length > 0);
for (const v in data.versions) {
const pkg = data.versions[v];
assert('_hasShrinkwrap' in pkg);
// assert('_hasShrinkwrap' in pkg);
assert(pkg.publish_time && typeof pkg.publish_time === 'number');
assert(pkg._publish_on_cnpm === undefined);
assert(pkg.dist.tarball.includes('.tgz?bucket=foo-us1&admin=1&other_urls=http'));
Expand Down
43 changes: 43 additions & 0 deletions test/controllers/registry/package/list_versions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const should = require('should');
const request = require('supertest');
const mm = require('mm');
const moment = require('moment');
const config = require('../../../../config');
const app = require('../../../../servers/registry');
const utils = require('../../../utils');

describe('test/controllers/registry/package/list_versions.test.js', function () {
afterEach(mm.restore);

before(function (done) {
utils.sync('pedding', done);
});

describe('GET /-/allversions', function () {
it('should get 200', function (done) {
mm(config, 'syncModel', 'all');
request(app)
.get('/-/allversions?date=' + moment().format('YYYY-MM-DD'))
.expect(200, function (err, res) {
should.not.exist(err);
console.log(res.body);
const rows = res.body;
rows.length.should.above(0);
done();
});
});

it('should get 404', function (done) {
mm(config, 'syncModel', 'all');
request(app)
.get('/-/allversions?date=notadsfwe')
.expect(400, function (err, res) {
should.not.exist(err);
res.body.reason.should.equal('[query_parse_error] Invalid value for `date`, should be `YYYY-MM-DD` format.');
done();
});
});
});
});
2 changes: 1 addition & 1 deletion test/controllers/sync_module_worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('test/controllers/sync_module_worker.test.js', () => {
console.log('get %d rows', rows.length);
rows.forEach(row => {
assert(row.package.deprecated);
assert(row.package._hasShrinkwrap === false);
// assert(row.package._hasShrinkwrap === false);
});

// mock deprecated missing
Expand Down

0 comments on commit 3cb3fe0

Please sign in to comment.