Skip to content

Commit 98fdea2

Browse files
author
Zhen
committed
Adding parsing of url.query
1 parent 0375eb0 commit 98fdea2

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

src/v1/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Record from './record';
2626
import {Driver, READ, WRITE} from './driver';
2727
import RoutingDriver from './routing-driver';
2828
import VERSION from '../version';
29-
import {parseScheme, parseUrl} from "./internal/connector";
29+
import {parseScheme, parseUrl, parseRoutingContext} from "./internal/connector";
3030
import {assertString} from "./internal/util";
3131

3232

@@ -120,9 +120,14 @@ let USER_AGENT = "neo4j-javascript/" + VERSION;
120120
function driver(url, authToken, config = {}) {
121121
assertString(url, 'Bolt URL');
122122
const scheme = parseScheme(url);
123+
const routingContext = parseRoutingContext(url);
123124
if (scheme === "bolt+routing://") {
124-
return new RoutingDriver(parseUrl(url), USER_AGENT, authToken, config);
125+
return new RoutingDriver(parseUrl(url), routingContext, USER_AGENT, authToken, config);
125126
} else if (scheme === "bolt://") {
127+
if(routingContext.length > 0)
128+
{
129+
throw new Error("Routing context are not supported with scheme 'bolt'. Given URI: '" + url + "'");
130+
}
126131
return new Driver(parseUrl(url), USER_AGENT, authToken, config);
127132
} else {
128133
throw new Error("Unknown scheme: " + scheme);

src/v1/internal/connector.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ MAGIC_PREAMBLE = 0x6060B017,
6060
DEBUG = false;
6161

6262
let URLREGEX = new RegExp([
63-
"([^/]+//)?", // scheme
63+
"([^/]+//)?", // scheme
6464
"(([^:/?#]*)", // hostname
6565
"(?::([0-9]+))?)", // port (optional)
66-
".*"].join("")); // everything else
66+
"([^?]*)?", // everything else
67+
"(\\?(.+))?" // query
68+
].join(""));
6769

6870
function parseScheme( url ) {
6971
let scheme = url.match(URLREGEX)[1] || '';
@@ -82,6 +84,21 @@ function parsePort( url ) {
8284
return url.match( URLREGEX )[4];
8385
}
8486

87+
function parseRoutingContext(url) {
88+
const other = url.match(URLREGEX)[7] || '';
89+
const map = {};
90+
if (other.length !== 0) {
91+
other.split("&").forEach(val => {
92+
const keyValue = val.split("=");
93+
if (keyValue.length !== 2) {
94+
throw new Error("Invalid parameters: '" + keyValue + "' in url '" + url + "'.");
95+
}
96+
map[keyValue[0]] = keyValue[1];
97+
});
98+
}
99+
return map;
100+
}
101+
85102
/**
86103
* Very rudimentary log handling, should probably be replaced by something proper at some point.
87104
* @param actor the part that sent the message, 'S' for server and 'C' for client
@@ -495,5 +512,6 @@ export {
495512
parseUrl,
496513
parseHost,
497514
parsePort,
515+
parseRoutingContext,
498516
Connection
499517
}

src/v1/routing-driver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {LoadBalancer} from './internal/connection-providers';
2727
*/
2828
class RoutingDriver extends Driver {
2929

30-
constructor(url, userAgent, token = {}, config = {}) {
30+
constructor(url, routingContext, userAgent, token = {}, config = {}) {
3131
super(url, userAgent, token, RoutingDriver._validateConfig(config));
3232
}
3333

test/internal/host-name-resolvers.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,38 @@
1919

2020
import {DnsHostNameResolver, DummyHostNameResolver} from '../../src/v1/internal/host-name-resolvers';
2121
import hasFeature from '../../src/v1/internal/features';
22-
import {parseHost, parsePort, parseScheme} from '../../src/v1/internal/connector';
22+
import {parseHost, parsePort, parseScheme, parseRoutingContext} from '../../src/v1/internal/connector';
23+
24+
fdescribe('RoutingContextParser', ()=>{
25+
26+
it('should parse routing context', done => {
27+
const url = "bolt://localhost:7687/cat?name=molly&age=1&color=white";
28+
const context = parseRoutingContext(url);
29+
expect(context).toEqual({name:"molly", age:"1", color:"white"});
30+
31+
done();
32+
});
33+
34+
it('should return empty routing context', done =>{
35+
const url1 = "bolt://localhost:7687/cat?";
36+
const context1 = parseRoutingContext(url1);
37+
expect(context1).toEqual({});
38+
39+
const url2 = "bolt://localhost:7687/lalala";
40+
const context2 = parseRoutingContext(url2);
41+
expect(context2).toEqual({});
42+
43+
done();
44+
});
45+
46+
it('should error for unmatched pair', done=>{
47+
const url = "bolt://localhost?cat";
48+
expect(()=>parseRoutingContext(url)).toThrow(
49+
new Error("Invalid parameters: 'cat' in url 'bolt://localhost?cat'."));
50+
51+
done();
52+
});
53+
});
2354

2455
describe('DummyHostNameResolver', () => {
2556

0 commit comments

Comments
 (0)