diff --git a/packages/node_modules/pouchdb-route/lib/index.js b/packages/node_modules/pouchdb-route/lib/index.js index 00905b95..65a0db8c 100644 --- a/packages/node_modules/pouchdb-route/lib/index.js +++ b/packages/node_modules/pouchdb-route/lib/index.js @@ -14,20 +14,19 @@ limitations under the License. */ -"use strict"; +import PouchPluginError from "pouchdb-plugin-error"; +import extend from "extend"; +import querystring from "querystring"; +URLSearchParams() -var PouchPluginError = require("pouchdb-plugin-error"); -var extend = require("extend"); -var querystring = require("querystring"); - -module.exports = function route(PouchDB, req, options) { +export default function route(PouchDB, req, options) { //Mapping urls to PouchDB/plug-in functions. Based on: - //http://docs.couchdb.org/en/latest/http-api.html + //https://docs.couchdb.org/en/stable/api/index.html if (req.path[0] === "..") { throw404(); //coverage: ignore } if (req.query) { - for (var key in req.query) { + for (const key in req.query) { // Object returned by the `querystring` module on Node doesn't have the // `hasOwnProperty()` method for some reasons, so we can't just do // `req.query.hasOwnProperty()`. @@ -40,19 +39,19 @@ module.exports = function route(PouchDB, req, options) { } } } - var rootFunc = { + const rootFunc = { "_all_dbs": (PouchDB.allDbs || throw404).bind(PouchDB), "_replicate": callWithBody.bind(null, PouchDB, req, function (body) { return this.replicate(body.source, body.target, body); }), - "_session": function () { + ["_session"]() { if (!PouchDB.seamlessSession) { throw404(); } return ({ GET: PouchDB.seamlessSession.bind(PouchDB), - POST: function () { - var data = parseBody(req); + POST() { + const data = parseBody(req); return PouchDB.seamlessLogIn(data.name, data.password); }, DELETE: PouchDB.seamlessLogOut.bind(PouchDB) @@ -62,32 +61,33 @@ module.exports = function route(PouchDB, req, options) { if (rootFunc) { return rootFunc(); } - var db = new PouchDB(decodeURIComponent(req.path[0])); - var localCallWithBody = callWithBody.bind(null, db, req); + const db = new PouchDB(decodeURIComponent(req.path[0])); + const localCallWithBody = callWithBody.bind(null, db, req); + if (req.path.length === 1) { - var post = options.withValidation ? db.validatingPost : db.post; - var defaultDBFunc = db.info.bind(db); + const post = options.withValidation ? db.validatingPost : db.post; + const defaultDBFunc = db.info.bind(db); return ({ DELETE: db.destroy.bind(db), POST: localCallWithBody.bind(null, post, crudOpts(req, options)) }[req.method] || defaultDBFunc)(); } - var localRouteCRUD = routeCRUD.bind(null, db, req, options); - var defaultFunc = localRouteCRUD.bind(null, req.path[1], req.path.slice(2)); - var bulkDocs = options.withValidation ? db.validatingBulkDocs : db.bulkDocs; + const localRouteCRUD = routeCRUD.bind(null, db, req, options); + const defaultFunc = localRouteCRUD.bind(null, req.path[1], req.path.slice(2)); + const bulkDocs = options.withValidation ? db.validatingBulkDocs : db.bulkDocs; return ({ "_all_docs": db.allDocs.bind(db, req.query), "_bulk_docs": localCallWithBody.bind(null, bulkDocs, crudOpts(req, options)), "_changes": db.changes.bind(db, req.query), "_compact": db.compact.bind(db), - "_design": function () { - var url = req.path[2] + "/" + req.path.slice(4).join("/"); - var subDefaultFunc = localRouteCRUD.bind(null, "_design/" + req.path[2], req.path.slice(3)); + ["_design"]() { + const url = `${req.path[2]}/${req.path.slice(4).join("/")}`; + const subDefaultFunc = localRouteCRUD.bind(null, `_design/${req.path[2]}`, req.path.slice(3)); return ({ "_list": (db.list || throw404).bind(db, url, req), - "_rewrite": function () { - var newReq = extend({}, req); + ["_rewrite"]() { + const newReq = extend({}, req); delete newReq.path; return (db.rewrite || throw404).bind(db, url, newReq)(); }, @@ -98,95 +98,64 @@ module.exports = function route(PouchDB, req, options) { "_view": db.query.bind(db, url, req.query) }[req.path[3]] || subDefaultFunc)(); }, - "_local": localRouteCRUD.bind(null, "_local/" + req.path[2], req.path.slice(3)), + "_local": localRouteCRUD.bind(null, `_local/${req.path[2]}`, req.path.slice(3)), "_revs_diff": localCallWithBody.bind(null, db.revsDiff), - "_security": function () { - return ({ + ["_security"]: () => ({ GET: localCallWithBody.bind(null, db.getSecurity), PUT: localCallWithBody.bind(null, db.putSecurity) - }[req.method] || throw405.bind(null, req))(); - }, + }[req.method] || throw405.bind(null, req))();, "_temp_view": localCallWithBody.bind(null, db.query, req.query), "_view_cleanup": db.viewCleanup.bind(db, req.query) }[req.path[1]] || defaultFunc)(); }; -function crudOpts(req, options) { - return extend({}, req.query, options); +function crudOpts({query}, options) { + return extend({}, query, options); } +// call function with a json parsed body realy hackish +const callWithBody = (thisObj, req, func, ...args) => func.apply(thisObj, [parseBody(req),...args]); -function callWithBody(thisObj, req, func) { - var args = Array.prototype.slice.call(arguments, 3); - args.unshift(parseBody(req)); - return func.apply(thisObj, args); -} - -function parseBody(req) { +function parseBody({body}) { try { - return JSON.parse(req.body); + return JSON.parse(body); } catch (err) { - return querystring.parse(req.body); + return new URLSearchParams(body); } } +// Iterates over remainingPath function routeCRUD(db, req, options, docId, remainingPath) { - var opts = crudOpts(req, options); + const opts = crudOpts(req, options); docId = decodeURIComponent(docId); - function callAttachment(isPut) { - var funcs; - var args = [docId, remainingPath[0], req.query.rev]; - if (isPut) { - args.push(req.body); - args.push(req.headers["Content-Type"]); - funcs = { - true: db.validatingPutAttachment, - false: db.putAttachment - }; - } else { - funcs = { - true: db.validatingRemoveAttachment, - false: db.removeAttachment - }; - } - if (options.withValidation) { - args.push(opts); - } - return funcs[options.withValidation].apply(db, args); - } - - //document level - if (remainingPath.length === 0) { - var localCallWithBody = callWithBody.bind(null, db, req); - var put = options.withValidation ? db.validatingPut : db.put; - var remove = options.withValidation ? db.validatingRemove : db.remove; - return ({ - GET: function () { - return db.get(docId, opts); - }, - PUT: localCallWithBody.bind(null, put, opts), - DELETE: remove.bind(db, docId, opts.rev) + const callAttachment = (isPut) => (options.withValidation && isPut && db.validatingPutAttachment.apply(db,args.concat([req.body, req.headers["Content-Type"],opts])) || + !options.withValidation && !isPut && db.removeAttachment.apply(db,args) || + options.withValidation && !isPut && db.validatingRemoveAttachment.apply(db,args.concat(opts])) || + !options.withValidation && isPut && db.putAttachment.apply(db,args.concat([[req.body, req.headers["Content-Type"]]))); + + return remainingPath.length === 0 + //document level + ? ({ + GET: ()=>db.get(docId, opts), + PUT: callWithBody.bind(null, db, req).bind(null, (options.withValidation ? db.validatingPut : db.put), opts), + DELETE: (options.withValidation ? db.validatingRemove : db.remove).bind(db, docId, opts.rev), + }[req.method] || throw405.bind(null, req))() + //attachment level + : ({ + GET: ()=>db.getAttachment(docId, remainingPath.join("/"), opts), + PUT: callAttachment.bind(null, true), + DELETE: callAttachment.bind(null, false), }[req.method] || throw405.bind(null, req))(); - } - //attachment level - return ({ - GET: function () { - return db.getAttachment(docId, remainingPath.join("/"), opts); - }, - PUT: callAttachment.bind(null, true), - DELETE: callAttachment.bind(null, false), - - }[req.method] || throw405.bind(null, req))(); } function throw404() { throw new PouchPluginError({status: 404, name: "not_found", message: "missing"}); } -function throw405(req) { +function throw405({method}) { throw new PouchPluginError({ status: 405, name: "method_not_allowed", - message: "method '" + req.method + "' not allowed." + message: `method '${method}' not allowed.` }); }