Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(adapter-http) remove custom URL parsing #9039

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions packages/node_modules/pouchdb-adapter-http/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
adapterFun as coreAdapterFun,
explainError,
clone,
parseUri,
bulkGetShim,
nextTick
} from 'pouchdb-utils';
Expand Down Expand Up @@ -79,12 +78,14 @@ function hasUrlPrefix(opts) {
if (!opts.prefix) {
return false;
}
const protocol = parseUri(opts.prefix).protocol;
return protocol === 'http' || protocol === 'https';

const url = URL.parse(opts.prefix);
return url && (url.protocol === 'http:' || url.protocol === 'https:');
}

// Get all the information you possibly can about the URI given by name and
// return it as a suitable object.
// return it as an object compatible with then PouchDB constructor's options
// object.
function getHost(name, opts) {
// encode db name if opts.prefix is a url (#5574)
if (hasUrlPrefix(opts)) {
Expand All @@ -94,24 +95,34 @@ function getHost(name, opts) {
name = prefix + encodeURIComponent(dbName);
}

const uri = parseUri(name);
if (uri.user || uri.password) {
uri.auth = {username: uri.user, password: uri.password};
const url = URL.parse(name);
if (!url) {
return {};
}

const host = {
protocol: url.protocol.replace(/:$/, ''),
host: url.hostname,
port: url.port,
};

if (url.user || url.password) {
host.auth = {username: url.username, password: url.password};
}

// Split the path part of the URI into parts using '/' as the delimiter
// after removing any leading '/' and any trailing '/'
const parts = uri.path.replace(/(^\/|\/$)/g, '').split('/');
const parts = url.pathname.replace(/(^\/|\/$)/g, '').split('/');

uri.db = parts.pop();
host.db = parts.pop();
// Prevent double encoding of URI component
if (uri.db.indexOf('%') === -1) {
uri.db = encodeURIComponent(uri.db);
if (host.db.indexOf('%') === -1) {
host.db = encodeURIComponent(host.db);
}

uri.path = parts.join('/');
host.path = parts.join('/');

return uri;
return host;
}

// Generate a URL with the host data given by opts and the given path
Expand Down
2 changes: 0 additions & 2 deletions packages/node_modules/pouchdb-for-coverage/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import {
parseUri,
uuid,
rev,
clone,
Expand Down Expand Up @@ -49,7 +48,6 @@ import checkpointer from 'pouchdb-checkpointer';

export default {
blob,
parseUri,
uuid,
rev,
atob,
Expand Down
2 changes: 0 additions & 2 deletions packages/node_modules/pouchdb-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import nextTick from './nextTick';
import normalizeDdocFunctionName from './normalizeDdocFunctionName';
import once from './once';
import parseDdocFunctionName from './parseDdocFunctionName';
import parseUri from './parseUri';
import pick from './pick';
import scopeEval from './scopeEval';
import toPromise from './toPromise';
Expand All @@ -44,7 +43,6 @@ export {
normalizeDdocFunctionName,
once,
parseDdocFunctionName,
parseUri,
pick,
rev,
scopeEval,
Expand Down
35 changes: 0 additions & 35 deletions packages/node_modules/pouchdb-utils/src/parseUri.js

This file was deleted.

8 changes: 4 additions & 4 deletions tests/integration/test.replication.js
Original file line number Diff line number Diff line change
Expand Up @@ -3913,8 +3913,8 @@ adapters.forEach(function (adapters) {
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote, {
fetch: function (url, opts) {
var uri = testUtils.parseUri(url);
if (uri.path === '/') {
const { pathname } = new URL(url);
if (pathname === '/') {
// Network error, as returned by the Fetch API:
return Promise.reject(new TypeError('Failed to fetch'));
}
Expand Down Expand Up @@ -3944,8 +3944,8 @@ adapters.forEach(function (adapters) {
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote, {
fetch: function (url, opts) {
var uri = testUtils.parseUri(url);
if (uri.path === '/') {
const { pathname } = new URL(url);
if (pathname === '/') {
return PouchDB.fetch(url, opts).then(function (response) {
response.body = new Blob('this is not JSON');
return response;
Expand Down
9 changes: 4 additions & 5 deletions tests/integration/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ testUtils.putTree = function (db, tree, callback) {
};

function parseHostWithCreds(host) {
var uriObj = testUtils.parseUri(host);
var url = `${uriObj.protocol}://${uriObj.host}:${uriObj.port}${uriObj.path}`;
var { origin, pathname, username, password } = new URL(host);
var url = `${origin}${pathname}`;
var options = {};
if (uriObj.userInfo) {
if (username || password) {
options.headers = {};
options.headers['Authorization'] = 'Basic: ' + testUtils.btoa(uriObj.userInfo);
options.headers['Authorization'] = 'Basic: ' + testUtils.btoa(`${username}:${password}`);
}
return { url, options };
}
Expand Down Expand Up @@ -273,7 +273,6 @@ testUtils.atob = pouchUtils.atob;
testUtils.ajax = PouchForCoverage.ajax;
testUtils.uuid = pouchUtils.uuid;
testUtils.rev = pouchUtils.rev;
testUtils.parseUri = pouchUtils.parseUri;
testUtils.errors = PouchForCoverage.Errors;
testUtils.assign = pouchUtils.assign;
testUtils.generateReplicationId = pouchUtils.generateReplicationId;
Expand Down
6 changes: 3 additions & 3 deletions tests/mapreduce/test.mapreduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ function tests(suiteName, dbName, dbType, viewType) {

beforeEach(function () {
if (dbType === 'http') {
var uri = testUtils.parseUri(dbName);
var dbUrl = `${uri.protocol}://${uri.host}:${uri.port}${uri.path}`;
const url = new URL(dbName);
const dbUrl = `${url.origin}${url.pathname}`;
return PouchDB.fetch(dbUrl + '?q=1', {
method: 'PUT',
headers: { Authorization: 'Basic ' + testUtils.btoa(uri.userInfo) }
headers: { Authorization: 'Basic ' + testUtils.btoa(`${url.username}:${url.password}`) },
});
}
});
Expand Down
44 changes: 0 additions & 44 deletions tests/unit/test.parse-uri.js

This file was deleted.

Loading