Skip to content

Commit

Permalink
update metadata on package publish. also, down with semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 24, 2010
1 parent b4e7b80 commit ef26a97
Showing 1 changed file with 44 additions and 42 deletions.
86 changes: 44 additions & 42 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var couchapp = require('couchapp');
var couchapp = require('couchapp')

var ddoc = {_id:'_design/app', shows:{}, updates:{}, views:{}, lists:{}};
exports.app = ddoc;
var ddoc = {_id:'_design/app', shows:{}, updates:{}, views:{}, lists:{}}
exports.app = ddoc

ddoc.rewrites =
[ { from: "/", to:"_list/index/listAll", method: "GET" }
Expand Down Expand Up @@ -45,6 +45,7 @@ ddoc.lists.index = function (head, req) {
}
p.versions = {}
for (var i in row.value.versions) {
if (row.value.versions[i].repository) p.repository = row.value.versions[i].repository
p.versions[i] = "http://"+req.headers.Host+"/"+row.value.name+"/"+i
}
p.url = "http://"+req.headers.Host+"/"+encodeURIComponent(row.value.name)+"/"
Expand Down Expand Up @@ -76,7 +77,7 @@ ddoc.shows.package = function (doc, req) {
body = {"error" : "version not found: "+req.query.version}
}
} else {
body = doc;
body = doc
delete body._revisions
delete body._attachments
}
Expand All @@ -87,11 +88,11 @@ ddoc.shows.package = function (doc, req) {
code : code,
body : body,
headers : headers,
};
}
}

ddoc.updates.package = function (doc, req) {
var semver = /v?([0-9]+)\.([0-9]+)\.([0-9]+)([a-zA-Z-][a-zA-Z0-9-]*)?/;
var semver = /v?([0-9]+)\.([0-9]+)\.([0-9]+)([a-zA-Z-][a-zA-Z0-9-]*)?/
function toISOString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
Expand All @@ -100,38 +101,41 @@ ddoc.updates.package = function (doc, req) {
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'}
var now = toISOString(new Date());
var now = toISOString(new Date())
function error (reason) {
return [{forbidden:reason}, JSON.stringify({forbidden:reason})];
return [{forbidden:reason}, JSON.stringify({forbidden:reason})]
}

if (doc) {
if (req.query.version) {
var parsed = semver(req.query.version);
var parsed = semver(req.query.version)
if (!parsed) {
// it's a tag.
var tag = req.query.version;
parsed = semver(JSON.parse(req.body));
var tag = req.query.version
parsed = semver(JSON.parse(req.body))
if (!parsed) {
return error(
"setting tag "+req.query.version+
" to invalid version: "+req.body);
" to invalid version: "+req.body)
}
doc["dist-tags"][req.query.version] = JSON.parse(req.body);
doc.mtime = now;
return [doc, JSON.stringify({ok:"updated tag"})];
doc["dist-tags"][req.query.version] = JSON.parse(req.body)
doc.mtime = now
return [doc, JSON.stringify({ok:"updated tag"})]
}
// adding a new version.
if (req.query.version in doc.versions) {
// attempting to overwrite an existing version.
// not supported at this time.
return error("cannot modify existing version");
return error("cannot modify existing version")
}
var body = JSON.parse(req.body)
for (var i in body) if (typeof body[i] === "string") {
doc[i] = body[i]
}
var body = JSON.parse(req.body);
body.ctime = body.mtime = doc.mtime = now;
doc["dist-tags"].latest = body.version;
doc.versions[req.query.version] = body;
return [doc, JSON.stringify({ok:"added version"})];
body.ctime = body.mtime = doc.mtime = now
doc["dist-tags"].latest = body.version
doc.versions[req.query.version] = body
return [doc, JSON.stringify({ok:"added version"})]
}

// update the package info
Expand All @@ -140,43 +144,41 @@ ddoc.updates.package = function (doc, req) {
if (doc._rev && doc._rev !== newdoc._rev) {
return error( "must supply latest _rev to update existing package" )
}
for (var i in newdoc) {
if (typeof newdoc[i] === "string" || i === "maintainers") {
doc[i] = newdoc[i];
}
for (var i in newdoc) if (typeof newdoc[i] === "string" || i === "maintainers") {
doc[i] = newdoc[i]
}
if (newdoc.versions) {
doc.versions = newdoc.versions
doc["dist-tags"] = newdoc["dist-tags"]
}
doc.mtime = now;
return [doc, JSON.stringify({ok:"updated package metadata"})];
doc.mtime = now
return [doc, JSON.stringify({ok:"updated package metadata"})]
} else {
// Create new package doc
doc = JSON.parse(req.body);
if (!doc.versions) doc.versions = {};
doc = JSON.parse(req.body)
if (!doc.versions) doc.versions = {}
var latest
for (var v in doc.versions) {
doc.versions[v].ctime = doc.versions[v].mtime = now;
doc.versions[v].ctime = doc.versions[v].mtime = now
latest = v
}
if (latest) doc["dist-tags"].latest = latest;
if (!doc['dist-tags']) doc['dist-tags'] = {};
doc.ctime = doc.mtime = now;
return [doc, JSON.stringify({ok:"created new entry"})];
if (latest) doc["dist-tags"].latest = latest
if (!doc['dist-tags']) doc['dist-tags'] = {}
doc.ctime = doc.mtime = now
return [doc, JSON.stringify({ok:"created new entry"})]
}
}

ddoc.validate_doc_update = function (newDoc, oldDoc, user) {
var semver = /v?([0-9]+)\.([0-9]+)\.([0-9]+)([a-zA-Z-][a-zA-Z0-9-]*)?/;
var semver = /v?([0-9]+)\.([0-9]+)\.([0-9]+)([a-zA-Z-][a-zA-Z0-9-]*)?/

function assert (ok, message) {
if (!ok) throw {forbidden:message};
if (!ok) throw {forbidden:message}
}

// if the newDoc is an {error:"blerg"}, then throw that right out.
// something detected in the _updates/package script.
if (newDoc.forbidden) throw {forbidden:newDoc.forbidden || newDoc.error};
if (newDoc.forbidden) throw {forbidden:newDoc.forbidden || newDoc.error}

function validUser () {
if ( !oldDoc || !oldDoc.maintainers ) return true
Expand All @@ -200,17 +202,17 @@ ddoc.validate_doc_update = function (newDoc, oldDoc, user) {
}

// make sure all the dist-tags and versions are valid semver
assert(newDoc["dist-tags"], "must have dist-tags");
assert(newDoc.versions, "must have versions");
assert(newDoc["dist-tags"], "must have dist-tags")
assert(newDoc.versions, "must have versions")

for (var i in newDoc["dist-tags"]) {
assert(semver(newDoc["dist-tags"][i]),
"dist-tag "+i+" is not a valid version: "+newDoc["dist-tags"][i]);
"dist-tag "+i+" is not a valid version: "+newDoc["dist-tags"][i])
assert(newDoc["dist-tags"][i] in newDoc.versions,
"dist-tag "+i+" refers to non-existent version: "+newDoc["dist-tags"][i]);
"dist-tag "+i+" refers to non-existent version: "+newDoc["dist-tags"][i])
}
for (var i in newDoc.versions) {
assert(semver(i),
"version "+i+" is not a valid version");
"version "+i+" is not a valid version")
}
}

0 comments on commit ef26a97

Please sign in to comment.