You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2020/02/12 11:25:49 UTC
[couchdb-nano] 01/15: use new url.URL instead of
url.parse/url.reslove
This is an automated email from the ASF dual-hosted git repository.
glynnbird pushed a commit to branch jest
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git
commit 29773e156e11101dffcf24bdc8c99e21183ed6be
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Wed Feb 5 14:13:19 2020 +0000
use new url.URL instead of url.parse/url.reslove
---
lib/nano.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 51 insertions(+), 15 deletions(-)
diff --git a/lib/nano.js b/lib/nano.js
index d73e634..6e6e535 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -10,9 +10,7 @@
// License for the specific language governing permissions and limitations under
// the License.
-'use strict'
-
-const u = require('url')
+const URL = require('url').URL
const assert = require('assert')
const querystring = require('querystring')
const request = require('request')
@@ -63,8 +61,8 @@ module.exports = exports = function dbScope (cfg) {
return
}
- const path = u.parse(cfg.url)
- let pathArray = path.pathname.split('/').filter(function (e) { return e })
+ const path = new URL(cfg.url)
+ const pathArray = path.pathname.split('/').filter(function (e) { return e })
const db = pathArray.pop()
const rootPath = path.pathname.replace(/\/?$/, '/..')
@@ -144,7 +142,7 @@ module.exports = exports = function dbScope (cfg) {
req.headers.cookie = 'XXXXXXX'
}
- let errors = errs.merge({
+ const errors = errs.merge({
message: 'couch returned ' + responseHeaders.statusCode,
scope: 'couch',
statusCode: responseHeaders.statusCode,
@@ -238,7 +236,7 @@ module.exports = exports = function dbScope (cfg) {
}
if (opts.accept) {
- req.headers['accept'] = opts.accept
+ req.headers.accept = opts.accept
}
// http://guide.couchdb.org/draft/security.html#cookies
@@ -332,17 +330,26 @@ module.exports = exports = function dbScope (cfg) {
// http://docs.couchdb.org/en/latest/api/database/common.html#put--db
function createDb (dbName, qs0, callback0) {
+ if (!dbName) {
+ throw new Error('missing dbName')
+ }
const { opts, callback } = getCallback(qs0, callback0)
return relax({ db: dbName, method: 'PUT', qs: opts }, callback)
}
// http://docs.couchdb.org/en/latest/api/database/common.html#delete--db
function destroyDb (dbName, callback) {
+ if (!dbName) {
+ throw new Error('missing dbName')
+ }
return relax({ db: dbName, method: 'DELETE' }, callback)
}
// http://docs.couchdb.org/en/latest/api/database/common.html#get--db
function getDb (dbName, callback) {
+ if (!dbName) {
+ throw new Error('missing dbName')
+ }
return relax({ db: dbName }, callback)
}
@@ -358,6 +365,9 @@ module.exports = exports = function dbScope (cfg) {
// http://docs.couchdb.org/en/latest/api/database/compact.html#post--db-_compact
function compactDb (dbName, ddoc, callback) {
+ if (!dbName) {
+ throw new Error('missing dbName')
+ }
if (typeof ddoc === 'function') {
callback = ddoc
ddoc = null
@@ -373,6 +383,9 @@ module.exports = exports = function dbScope (cfg) {
// http://docs.couchdb.org/en/latest/api/database/changes.html#get--db-_changes
function changesDb (dbName, qs0, callback0) {
const { opts, callback } = getCallback(qs0, callback0)
+ if (!dbName) {
+ throw new Error('missing dbName')
+ }
return relax({ db: dbName, path: '_changes', qs: opts }, callback)
}
@@ -395,10 +408,12 @@ module.exports = exports = function dbScope (cfg) {
if (typeof db === 'object' && db.config && db.config.url && db.config.db) {
return urlResolveFix(db.config.url, encodeURIComponent(db.config.db))
} else {
- const parsed = u.parse(db)
- if (parsed.protocol) {
- return db
- } else {
+ try {
+ // if it parses, return it
+ const parsed = new URL(db)
+ return parsed.toString()
+ } catch (e) {
+ // otherwise treat it as a database name
return urlResolveFix(cfg.url, encodeURIComponent(db))
}
}
@@ -407,6 +422,12 @@ module.exports = exports = function dbScope (cfg) {
// http://docs.couchdb.org/en/latest/api/server/common.html#post--_replicate
function replicateDb (source, target, opts0, callback0) {
const { opts, callback } = getCallback(opts0, callback0)
+ if (!source) {
+ throw new Error('missing source')
+ }
+ if (!target) {
+ throw new Error('missing target')
+ }
// _replicate
opts.source = _serializeAsUrl(source)
@@ -427,6 +448,12 @@ module.exports = exports = function dbScope (cfg) {
// http://guide.couchdb.org/draft/replication.html
function enableReplication (source, target, opts0, callback0) {
const { opts, callback } = getCallback(opts0, callback0)
+ if (!source) {
+ throw new Error('missing source')
+ }
+ if (!target) {
+ throw new Error('missing target')
+ }
// _replicator
opts.source = _serializeAsUrl(source)
@@ -438,12 +465,21 @@ module.exports = exports = function dbScope (cfg) {
// http://guide.couchdb.org/draft/replication.html
function queryReplication (id, opts0, callback0) {
const { opts, callback } = getCallback(opts0, callback0)
+ if (!id) {
+ throw new Error('missing id')
+ }
return relax({ db: '_replicator', method: 'GET', path: id, qs: opts }, callback)
}
// http://guide.couchdb.org/draft/replication.html
function disableReplication (id, rev, opts0, callback0) {
const { opts, callback } = getCallback(opts0, callback0)
+ if (!id) {
+ throw new Error('missing id')
+ }
+ if (!rev) {
+ throw new Error('missing rev')
+ }
const req = {
db: '_replicator',
method: 'DELETE',
@@ -547,7 +583,7 @@ module.exports = exports = function dbScope (cfg) {
db: dbName,
doc: docSrc,
method: 'COPY',
- headers: { 'Destination': docDest }
+ headers: { Destination: docDest }
}
if (opts.overwrite) {
@@ -593,7 +629,7 @@ module.exports = exports = function dbScope (cfg) {
// http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs
function fetchDocs (docNames, qs0, callback0) {
const { opts, callback } = getCallback(qs0, callback0)
- opts['include_docs'] = true
+ opts.include_docs = true
return relax({
db: dbName,
@@ -750,7 +786,7 @@ module.exports = exports = function dbScope (cfg) {
length: Buffer.isBuffer(att.data)
? att.data.length : Buffer.byteLength(att.data),
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
- 'content_type': att.content_type
+ content_type: att.content_type
}
multipart.push({ body: att.data })
})
@@ -1004,5 +1040,5 @@ function urlResolveFix (couchUrl, dbName) {
if (/[^/]$/.test(couchUrl)) {
couchUrl += '/'
}
- return u.resolve(couchUrl, dbName)
+ return new URL(dbName, couchUrl).toString()
}