-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
|
@@ -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) { | ||
|
@@ -37,47 +38,61 @@ app.use(function* (next) { | |
yield next; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
meaku
Author
Member
|
||
}); | ||
|
||
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; | ||
|
||
}); | ||
|
@@ -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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Just being curious: I'd expect it to be
yield next()
. Why don't you call the method?