Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
feat: sync from other mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jan 18, 2015
1 parent f92d2c3 commit a1646c1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
4 changes: 4 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ var config = {
// global sync interval
syncInterval: ms('5m'),

// sync by clone from other mirrors
cloneMode: true,
cloneUrl: 'http://cnpmjs.org/mirrors/apis',

// sync categories
categories: {
node: {
Expand Down
1 change: 1 addition & 0 deletions controllers/dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = function* () {
} else {
item.size = fmt('%s(%s)', item.size, bytes(item.size));
}
item.date = item.date || '';
return item;
});

Expand Down
26 changes: 19 additions & 7 deletions sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,39 @@ var logger = require('../common/logger');
var config = require('../config');
var co = require('co');
var GithubSyncer;
var MirrorsSyncer;

var syncers = config.categories;

for (var name in syncers) {
var syncer = syncers[name];
if (!config.enableSync) {
syncers[name].enable = false;
syncer.enable = false;
}

if (!syncers[name].enable) {
if (!syncer.enable) {
continue;
}

if (config.cloneMode) {
var baseUrl = config.cloneUrl.replace(/\/?$/, '/');
MirrorsSyncer = MirrorsSyncer || require('./mirrors');
syncer.Syncer = MirrorsSyncer;
syncer.syncing = false;
syncer.disturl = baseUrl + syncer.category;
continue;
}

// sync from github
if (syncers[name].githubRepo) {
if (syncer.githubRepo) {
GithubSyncer = GithubSyncer || require('./github');
syncers[name].Syncer = GithubSyncer;
syncers[name].syncing = false;
syncer.Syncer = GithubSyncer;
syncer.syncing = false;
continue;
}

syncers[name].Syncer = require('./' + name);
syncers[name].syncing = false;
syncer.Syncer = require('./' + name);
syncer.syncing = false;
}

Object.keys(syncers).forEach(function (name) {
Expand Down
62 changes: 62 additions & 0 deletions sync/mirrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**!
* mirrors - services/mirrors.js
*
* Authors:
* dead_horse <dead_horse@qq.com> (https://github.com/dead-horse)
*/

'use strict';

/**
* Module dependencies.
*/

var debug = require('debug')('mirrors:sync:mirrors');
var util = require('util');
var urllib = require('urllib');
var Syncer = require('./syncer');

module.exports = MirrorsSyncer;

function MirrorsSyncer(options) {
if (!(this instanceof MirrorsSyncer)) {
return new MirrorsSyncer(options);
}
Syncer.call(this, options);
}

util.inherits(MirrorsSyncer, Syncer);

var proto = MirrorsSyncer.prototype;

proto.check = function (checksums, info) {
if (!info.size) {
return true;
}
return checksums.size === info.size;
};

proto.listdir = function* (fullname) {
var url = this.disturl + fullname;

var res = yield urllib.requestThunk(url, {
timeout: 60 * 1000,
dataType: 'json'
});
debug('listdir %s got %s, %j', url, res.status, res.headers);
if (res.status !== 200) {
var msg = util.format('request %s error, got %s', url, res.status);
throw new Error(msg);
}

return res.data.map(function (file) {
return {
name: file.name,
size: file.size || '-',
date: file.date,
type: file.type === 'file' ? 'file' : 'dir',
parent: fullname,
downloadURL: file.url
};
});
};
2 changes: 1 addition & 1 deletion views/dist.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ <h1>Mirror index of <a target="_blank" href="<%= disturl %>"><%= disturl %></a><
<pre><a href="../">../</a><% for (var i = 0; i < items.length; i++) {
var item = items[i];
%>
<a href="<%= config.mount %><%= category %><%= item.parent || '' %><%= item.name || '' %>"><%= item.name %></a><%- padding(50, item.name.length, " ") %><%= item.date %><%- padding(50, item.size.length, " ") %><%= item.size %><% } %>
<a href="<%= config.mount %><%= category %><%= item.parent || '' %><%= item.name || '' %>"><%= item.name %></a><%- padding(50, item.name.length, " ") %><%= item.date %><%- padding(50, item.date.length, " ") %><%= item.size %><% } %>
</pre>
<hr>

0 comments on commit a1646c1

Please sign in to comment.