Skip to content

Commit

Permalink
Some fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
meaku committed Oct 26, 2015
1 parent 288cb04 commit 01b4d7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
59 changes: 40 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"use strict";

const koa = require("koa");
const app = koa();
const stats = require("./lib");
const cash = require("koa-cash");
const stats = require("./lib");

var cache = require('lru-cache')({
let cache = require("lru-cache")({
maxAge: 30000 // global max age
});

app.use(require('koa-cash')({
const app = koa();

app.use(require("koa-cash")({
get: function* (key, maxAge) {
return cache.get(key)
},
Expand All @@ -19,7 +20,7 @@ app.use(require('koa-cash')({
}));

//error handler; see https://github.com/koajs/koa/wiki/Error-Handling
app.use(function *(next) {
app.use(function* (next) {
try {
yield next;
} catch (err) {
Expand All @@ -37,47 +38,61 @@ app.use(function* (next) {
yield next;

This comment has been minimized.

Copy link
@jhnns

jhnns Oct 26, 2015

Member

Just being curious: I'd expect it to be yield next(). Why don't you call the method?

This comment has been minimized.

Copy link
@meaku

meaku Oct 26, 2015

Author Member

That's the proposed koa way at the moment. next is not a function. But to be honest, i tried to find out how it works internally and didn't find it yet. But there is a proposal of koa 2.0 which uses asyc/await replacing next.

See koajs/koa#415, koajs/koa#533

This comment has been minimized.

Copy link
@jhnns

jhnns Oct 26, 2015

Member

next is probably a promise?

This comment has been minimized.

Copy link
@meaku

meaku Oct 26, 2015

Author Member

Yeah. Probably.

});

app.use(function *(next) {
app.use(function* (next) {

let modules;

if(this.query.user) {
if (this.query.user) {
this.state.modules = yield stats.modulesByUser(this.query.user);
}

if(this.query.modules && !this.state.user) {
this.state.modules = this.query.modules.split(",");
if (this.query.modules) {
this.state.modules = this.state.modules || [];

modules = this.query.modules.split(",");

if (!Array.isArray(modules)) {
modules = [modules];
}

this.state.modules = this.state.modules.concat(modules);
}

if(!this.state.modules) {
throw new Error("You have to pass modules via query args");
if (!this.state.modules) {
throw new Error("You have to pass modules via query args: ?user=peerigon&modules=less-loader");
}

yield next;
});

app.use(function *(next) {
app.use(function* fetchModules(next) {

this.state.modules = yield stats.modules(this.state.modules);

yield next;
});

app.use(function *stripFields(next) {
app.use(function* stripFields(next) {

if(this.query.fields) {
if (this.query.fields) {
this.state.fields = this.query.fields.split(",");
}

let fields = this.state.fields || ["name", "description", "downloads"];
this.state.downloads = 0;

this.state.modules = this.state.modules.map(function(module) {
this.state.modules = this.state.modules.map((module) => {

let res = {};

fields.forEach(function(field) {
fields.forEach(function (field) {
res[field] = module[field];
});

if (module.downloads && module.downloads.downloads) {
this.state.downloads += module.downloads.downloads;
}

return res;

});
Expand All @@ -86,9 +101,15 @@ app.use(function *stripFields(next) {

});

app.use(function *() {
this.body = this.state.modules;

app.use(function* respond() {
this.body = {
downloads: this.state.downloads,
modules: this.state.modules
};
});


app.listen(9000);
app.listen(9000, function() {
console.log("npm peerigon stats listening on port 9000");
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"example": "example"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "node app.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 01b4d7d

Please sign in to comment.