/usr/lib/nodejs/cradle/response.js is in libnode-cradle 0.5.5~20110308-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | //
// HTTP response wrapper
//
// It allows us to call array-like methods on documents
// with a 'row' attribute.
//
this.Response = function Response(json, response) {
var obj, headers;
// If there's rows, this is the result
// of a view function.
// We want to return this as an Array.
if (json.rows) {
obj = json.rows.slice(0);
obj.__proto__ = new(Array);
Object.keys(json).forEach(function (k) {
Object.defineProperty(obj.__proto__, k, {
value: json[k],
enumerable: false
});
});
} else if (json.results) {
obj = json.results.slice(0);
obj.__proto__ = new(Array);
obj.last_seq = json.last_seq;
} else if (json.uuids) {
obj = json.uuids;
obj.__proto__ = new(Array);
} else if (Array.isArray(json)) {
obj = json.slice(0);
obj.__proto__ = new(Array);
} else {
obj = {};
obj.__proto__ = new(Object);
Object.keys(json).forEach(function (k) {
obj[k] = json[k];
});
}
// If the response was originally a document,
// give access to it via the 'json' getter.
if (!Array.isArray(json) && !obj.json) {
Object.defineProperty(obj, 'json', {
value: json,
enumerable: false
});
}
if (response) {
headers = { status: response.statusCode };
Object.keys(response.headers).forEach(function (k) {
headers[k] = response.headers[k];
});
// Set the 'headers' special field, with the response's status code.
exports.extend(obj, 'headers' in obj ? { _headers: headers }
: { headers: headers });
}
// Alias '_rev' and '_id'
if (obj.id && obj.rev) {
exports.extend(obj, { _id: obj.id, _rev: obj.rev });
} else if (obj._id && obj._rev) {
exports.extend(obj, { id: obj._id, rev: obj._rev });
}
if (Array.isArray(obj) && json.rows) {
exports.extend(obj, exports.collectionPrototype);
}
exports.extend(obj, exports.basePrototype);
// Set the constructor to be this function
Object.defineProperty(obj, 'constructor', {
value: arguments.callee
});
return obj;
};
this.basePrototype = {
toJSON: function () {
return this;
},
toString: function () {
return JSON.stringify(this);
}
};
this.collectionPrototype = {
forEach: function (f) {
for (var i = 0, value; i < this.length; i++) {
value = this[i].doc || this[i].json || this[i].value || this[i];
if (f.length === 1) {
f.call(this[i], value);
} else {
f.call(this[i], this[i].key, value, this[i].id);
}
}
},
map: function (f) {
var ary = [];
if (f.length === 1) {
this.forEach(function (a) { ary.push(f.call(this, a)) });
} else {
this.forEach(function () { ary.push(f.apply(this, arguments)) });
}
return ary;
},
toArray: function () {
return this.map(function (k, v) { return v });
}
};
this.extend = function (obj, properties) {
var descriptor = Object.keys(properties).reduce(function (hash, k) {
hash[k] = {
value: properties[k],
enumerable: false
};
return hash;
}, {});
return Object.defineProperties(obj, descriptor);
};
|