Skip to content

Commit

Permalink
Restructuring, much simpler now.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Mar 12, 2010
1 parent da82430 commit 7ccaf93
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 241 deletions.
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -18,13 +18,16 @@ Where `packages` is the hostname where you'll be running the thing, and `5984` i

Then drop this file in your `/etc/couchdb/local.d` folder. (If you're using Homebrew or MacPorts, this may be found underneath the package system prefix, either `/usr/local` or `/opt/local`, respectively.)

Now run the sync script.
Now Install use npm to install couchapp.

node sync.js
git clone git@github.com:mikeal/node.couchapp.js.git
cd node.couchapp.js
npm install .
npm activate couchapp 0.2.0

You can run it in dev mode like this to get file watching and error logging:
Now run the sync app.js from this repository.

node sync.js dev
couchapp --design app.js --sync --couch http://localhost:5984/jsregistry

# API

Expand Down
121 changes: 121 additions & 0 deletions app.js
@@ -0,0 +1,121 @@
var couchapp = require('couchapp');

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

ddoc.rewrites = [
{ from: "/:pkg", to: "/_show/package/:pkg", method: "GET" },
{ from: "/:pkg/:version", to: "_show/package/:pkg", method: "GET",
query: { version: ":version" }
},
{ from: "/:pkg", to: "/_update/package/:pkg", method: "PUT" },
{ from: "/:pkg/:version", to: "_update/package/:pkg", method: "PUT",
query: { version : ":version" }
},
]

ddoc.shows.package = function (doc, req) {
var code = 200,
headers = {"Content-Type":"application/json"},
body = null;
if (req.query.version) {
if (isNaN(parseInt(req.query.version[0]))) {
body = doc.versions[doc['dist-tags'][req.query.version]]
} else {
body = doc.versions[req.query.version]
}
if (!body) {
code = 404;
body = {"error" : "version not found: "+req.query.version};
}
} else {
body = doc;
}
return {
code : code,
body : toJSON(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-]*)?/;
function error (reason) {
return [{forbidden:reason}, JSON.stringify({forbidden:reason})];
}

if (doc) {
if (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));
if (!parsed) {
return error(
"setting tag "+req.query.version+
" to invalid version: "+req.body);
}
doc["dist-tags"][req.query.version] = JSON.parse(req.body);
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");
}
doc.versions[req.query.version] = JSON.parse(req.body);
return [doc, JSON.stringify({ok:"added version"})];
}

// update the package info
var newdoc = JSON.parse(req.body),
changed = false;
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") {
doc[i] = newdoc[i];
}
}
return [doc, JSON.stringify({ok:"updated package metadata"})];
} else {
// Create new package doc
doc = JSON.parse(req.body);
if (!doc.versions) doc.versions = {};
if (!doc['dist-tags']) doc['dist-tags'] = {};
return [doc, JSON.stringify({ok:"created new entry"})];
}
}

ddoc.validate_update_doc = function (newDoc, oldDoc, user) {
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};
}

// TODO: If the user doesn't have access to update this thing,
// then throw an unauthorized error

// 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};

// 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");

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]);
assert(newDoc["dist-tags"][i] in newDoc.versions,
"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");
}
}
1 change: 0 additions & 1 deletion deps/node-couchdb
Submodule node-couchdb deleted from 7ccbd5
19 changes: 0 additions & 19 deletions design/rewrites.json

This file was deleted.

23 changes: 0 additions & 23 deletions design/shows/package.js

This file was deleted.

51 changes: 0 additions & 51 deletions design/updates/package.js

This file was deleted.

29 changes: 0 additions & 29 deletions design/validate_doc_update.js

This file was deleted.

112 changes: 0 additions & 112 deletions sync.js

This file was deleted.

4 changes: 2 additions & 2 deletions test.js
Expand Up @@ -64,10 +64,10 @@ function assertStatus (code) {

requestQueue([
["/foo", "PUT", undefined, {_id:"foo", description:"new module"}, assertStatus(201)],
["/foo/0.1", "PUT", undefined,
["/foo/0.1.0", "PUT", undefined,
{_id:"foo", description:"new module", dist:{tarball:"http://path/to/tarball"}}, assertStatus(201)],
["/foo/stable", "PUT", undefined, "0.1", assertStatus(201)],
["/foo", "GET", undefined, "0.1", assertStatus(200)],
["/foo/0.1", "GET", undefined, "0.1", assertStatus(200)],
["/foo/0.1.0", "GET", undefined, "0.1", assertStatus(200)],
["/foo/stable", "GET", undefined, "0.1", assertStatus(200)],
], function () {sys.puts('done')})

0 comments on commit 7ccaf93

Please sign in to comment.