UNPKG

14.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.temporal = exports.spatial = exports.error = exports.session = exports.types = exports.auth = exports.Neo4jError = exports.integer = exports.isDateTime = exports.isLocalDateTime = exports.isDate = exports.isTime = exports.isLocalTime = exports.isDuration = exports.isPoint = exports.isInt = exports.int = exports.driver = undefined;
7
8var _integer = require('./integer');
9
10var _graphTypes = require('./graph-types');
11
12var _error = require('./error');
13
14var _result = require('./result');
15
16var _result2 = _interopRequireDefault(_result);
17
18var _resultSummary = require('./result-summary');
19
20var _resultSummary2 = _interopRequireDefault(_resultSummary);
21
22var _record = require('./record');
23
24var _record2 = _interopRequireDefault(_record);
25
26var _driver = require('./driver');
27
28var _routingDriver = require('./routing-driver');
29
30var _routingDriver2 = _interopRequireDefault(_routingDriver);
31
32var _version = require('../version');
33
34var _version2 = _interopRequireDefault(_version);
35
36var _util = require('./internal/util');
37
38var _urlUtil = require('./internal/url-util');
39
40var _urlUtil2 = _interopRequireDefault(_urlUtil);
41
42var _httpDriver = require('./internal/http/http-driver');
43
44var _httpDriver2 = _interopRequireDefault(_httpDriver);
45
46var _spatialTypes = require('./spatial-types');
47
48var _temporalTypes = require('./temporal-types');
49
50function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
52/**
53 * @property {function(username: string, password: string, realm: ?string)} basic the function to create a
54 * basic authentication token.
55 * @property {function(base64EncodedTicket: string)} kerberos the function to create a Kerberos authentication token.
56 * Accepts a single string argument - base64 encoded Kerberos ticket.
57 * @property {function(principal: string, credentials: string, realm: string, scheme: string, parameters: ?object)} custom
58 * the function to create a custom authentication token.
59 */
60/**
61 * Copyright (c) 2002-2018 Neo4j Sweden AB [http://neo4j.com]
62 *
63 * This file is part of Neo4j.
64 *
65 * Licensed under the Apache License, Version 2.0 (the "License");
66 * you may not use this file except in compliance with the License.
67 * You may obtain a copy of the License at
68 *
69 * http://www.apache.org/licenses/LICENSE-2.0
70 *
71 * Unless required by applicable law or agreed to in writing, software
72 * distributed under the License is distributed on an "AS IS" BASIS,
73 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74 * See the License for the specific language governing permissions and
75 * limitations under the License.
76 */
77
78var auth = {
79 basic: function basic(username, password) {
80 var realm = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
81
82 if (realm) {
83 return { scheme: 'basic', principal: username, credentials: password, realm: realm };
84 } else {
85 return { scheme: 'basic', principal: username, credentials: password };
86 }
87 },
88 kerberos: function kerberos(base64EncodedTicket) {
89 return {
90 scheme: 'kerberos',
91 principal: '', // This empty string is required for backwards compatibility.
92 credentials: base64EncodedTicket
93 };
94 },
95 custom: function custom(principal, credentials, realm, scheme) {
96 var parameters = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined;
97
98 if (parameters) {
99 return {
100 scheme: scheme, principal: principal, credentials: credentials, realm: realm,
101 parameters: parameters
102 };
103 } else {
104 return { scheme: scheme, principal: principal, credentials: credentials, realm: realm };
105 }
106 }
107};
108var USER_AGENT = "neo4j-javascript/" + _version2.default;
109
110/**
111 * Construct a new Neo4j Driver. This is your main entry point for this
112 * library.
113 *
114 * ## Configuration
115 *
116 * This function optionally takes a configuration argument. Available configuration
117 * options are as follows:
118 *
119 * {
120 * // Encryption level: ENCRYPTION_ON or ENCRYPTION_OFF.
121 * encrypted: ENCRYPTION_ON|ENCRYPTION_OFF
122 *
123 * // Trust strategy to use if encryption is enabled. There is no mode to disable
124 * // trust other than disabling encryption altogether. The reason for
125 * // this is that if you don't know who you are talking to, it is easy for an
126 * // attacker to hijack your encrypted connection, rendering encryption pointless.
127 * //
128 * // TRUST_ALL_CERTIFICATES is the default choice for NodeJS deployments. It only requires
129 * // new host to provide a certificate and does no verification of the provided certificate.
130 * //
131 * // TRUST_ON_FIRST_USE is available for modern NodeJS deployments, and works
132 * // similarly to how `ssl` works - the first time we connect to a new host,
133 * // we remember the certificate they use. If the certificate ever changes, we
134 * // assume it is an attempt to hijack the connection and require manual intervention.
135 * // This means that by default, connections "just work" while still giving you
136 * // good encrypted protection.
137 * //
138 * // TRUST_CUSTOM_CA_SIGNED_CERTIFICATES is the classic approach to trust verification -
139 * // whenever we establish an encrypted connection, we ensure the host is using
140 * // an encryption certificate that is in, or is signed by, a certificate listed
141 * // as trusted. In the web bundle, this list of trusted certificates is maintained
142 * // by the web browser. In NodeJS, you configure the list with the next config option.
143 * //
144 * // TRUST_SYSTEM_CA_SIGNED_CERTIFICATES means that you trust whatever certificates
145 * // are in the default certificate chain of th
146 * trust: "TRUST_ALL_CERTIFICATES" | "TRUST_ON_FIRST_USE" | "TRUST_SIGNED_CERTIFICATES" |
147 * "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES" | "TRUST_SYSTEM_CA_SIGNED_CERTIFICATES",
148 *
149 * // List of one or more paths to trusted encryption certificates. This only
150 * // works in the NodeJS bundle, and only matters if you use "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES".
151 * // The certificate files should be in regular X.509 PEM format.
152 * // For instance, ['./trusted.pem']
153 * trustedCertificates: [],
154 *
155 * // Path to a file where the driver saves hosts it has seen in the past, this is
156 * // very similar to the ssl tool's known_hosts file. Each time we connect to a
157 * // new host, a hash of their certificate is stored along with the domain name and
158 * // port, and this is then used to verify the host certificate does not change.
159 * // This setting has no effect unless TRUST_ON_FIRST_USE is enabled.
160 * knownHosts:"~/.neo4j/known_hosts",
161 *
162 * // The max number of connections that are allowed idle in the pool at any time.
163 * // Connection will be destroyed if this threshold is exceeded.
164 * // <b>Deprecated:</b> please use <code>maxConnectionPoolSize</code> instead.
165 * connectionPoolSize: 100,
166 *
167 * // The maximum total number of connections allowed to be managed by the connection pool, per host.
168 * // This includes both in-use and idle connections. No maximum connection pool size is imposed
169 * // by default.
170 * maxConnectionPoolSize: 100,
171 *
172 * // The maximum allowed lifetime for a pooled connection in milliseconds. Pooled connections older than this
173 * // threshold will be closed and removed from the pool. Such discarding happens during connection acquisition
174 * // so that new session is never backed by an old connection. Setting this option to a low value will cause
175 * // a high connection churn and might result in a performance hit. It is recommended to set maximum lifetime
176 * // to a slightly smaller value than the one configured in network equipment (load balancer, proxy, firewall,
177 * // etc. can also limit maximum connection lifetime). No maximum lifetime limit is imposed by default. Zero
178 * // and negative values result in lifetime not being checked.
179 * maxConnectionLifetime: 60 * 60 * 1000, // 1 hour
180 *
181 * // The maximum amount of time to wait to acquire a connection from the pool (to either create a new
182 * // connection or borrow an existing one.
183 * connectionAcquisitionTimeout: 60000, // 1 minute
184 *
185 * // Specify the maximum time in milliseconds transactions are allowed to retry via
186 * // <code>Session#readTransaction()</code> and <code>Session#writeTransaction()</code> functions.
187 * // These functions will retry the given unit of work on `ServiceUnavailable`, `SessionExpired` and transient
188 * // errors with exponential backoff using initial delay of 1 second.
189 * // Default value is 30000 which is 30 seconds.
190 * maxTransactionRetryTime: 30000, // 30 seconds
191 *
192 * // Provide an alternative load balancing strategy for the routing driver to use.
193 * // Driver uses "least_connected" by default.
194 * // <b>Note:</b> We are experimenting with different strategies. This could be removed in the next minor
195 * // version.
196 * loadBalancingStrategy: "least_connected" | "round_robin",
197 *
198 * // Specify socket connection timeout in milliseconds. Numeric values are expected. Negative and zero values
199 * // result in no timeout being applied. Connection establishment will be then bound by the timeout configured
200 * // on the operating system level. Default value is 5000, which is 5 seconds.
201 * connectionTimeout: 5000, // 5 seconds
202 *
203 * // Make this driver always return native JavaScript numbers for integer values, instead of the
204 * // dedicated {@link Integer} class. Values that do not fit in native number bit range will be represented as
205 * // <code>Number.NEGATIVE_INFINITY</code> or <code>Number.POSITIVE_INFINITY</code>.
206 * // <b>Warning:</b> It is not always safe to enable this setting when JavaScript applications are not the only ones
207 * // interacting with the database. Stored numbers might in such case be not representable by native
208 * // {@link Number} type and thus driver will return lossy values. This might also happen when data was
209 * // initially imported using neo4j import tool and contained numbers larger than
210 * // <code>Number.MAX_SAFE_INTEGER</code>. Driver will then return positive infinity, which is lossy.
211 * // Default value for this option is <code>false</code> because native JavaScript numbers might result
212 * // in loss of precision in the general case.
213 * disableLosslessIntegers: false,
214 * }
215 *
216 * @param {string} url The URL for the Neo4j database, for instance "bolt://localhost"
217 * @param {Map<String,String>} authToken Authentication credentials. See {@link auth} for helpers.
218 * @param {Object} config Configuration object. See the configuration section above for details.
219 * @returns {Driver}
220 */
221function driver(url, authToken) {
222 var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
223
224 (0, _util.assertString)(url, 'Bolt URL');
225 var parsedUrl = _urlUtil2.default.parseDatabaseUrl(url);
226 if (parsedUrl.scheme === 'bolt+routing') {
227 return new _routingDriver2.default(parsedUrl.hostAndPort, parsedUrl.query, USER_AGENT, authToken, config);
228 } else if (parsedUrl.scheme === 'bolt') {
229 if (!(0, _util.isEmptyObjectOrNull)(parsedUrl.query)) {
230 throw new Error('Parameters are not supported with scheme \'bolt\'. Given URL: \'' + url + '\'');
231 }
232 return new _driver.Driver(parsedUrl.hostAndPort, USER_AGENT, authToken, config);
233 } else if (parsedUrl.scheme === 'http' || parsedUrl.scheme === 'https') {
234 return new _httpDriver2.default(parsedUrl, USER_AGENT, authToken, config);
235 } else {
236 throw new Error('Unknown scheme: ' + parsedUrl.scheme);
237 }
238}
239
240/**
241 * Object containing constructors for all neo4j types.
242 */
243var types = {
244 Node: _graphTypes.Node,
245 Relationship: _graphTypes.Relationship,
246 UnboundRelationship: _graphTypes.UnboundRelationship,
247 PathSegment: _graphTypes.PathSegment,
248 Path: _graphTypes.Path,
249 Result: _result2.default,
250 ResultSummary: _resultSummary2.default,
251 Record: _record2.default,
252 Point: _spatialTypes.Point,
253 Date: _temporalTypes.Date,
254 DateTime: _temporalTypes.DateTime,
255 Duration: _temporalTypes.Duration,
256 LocalDateTime: _temporalTypes.LocalDateTime,
257 LocalTime: _temporalTypes.LocalTime,
258 Time: _temporalTypes.Time
259};
260
261/**
262 * Object containing string constants representing session access modes.
263 */
264var session = {
265 READ: _driver.READ,
266 WRITE: _driver.WRITE
267};
268
269/**
270 * Object containing string constants representing predefined {@link Neo4jError} codes.
271 */
272var error = {
273 SERVICE_UNAVAILABLE: _error.SERVICE_UNAVAILABLE,
274 SESSION_EXPIRED: _error.SESSION_EXPIRED,
275 PROTOCOL_ERROR: _error.PROTOCOL_ERROR
276};
277
278/**
279 * Object containing functions to work with {@link Integer} objects.
280 */
281var integer = {
282 toNumber: _integer.toNumber,
283 toString: _integer.toString,
284 inSafeRange: _integer.inSafeRange
285};
286
287/**
288 * Object containing functions to work with spatial types, like {@link Point}.
289 */
290var spatial = {
291 isPoint: _spatialTypes.isPoint
292};
293
294/**
295 * Object containing functions to work with temporal types, like {@link Time} or {@link Duration}.
296 */
297var temporal = {
298 isDuration: _temporalTypes.isDuration,
299 isLocalTime: _temporalTypes.isLocalTime,
300 isTime: _temporalTypes.isTime,
301 isDate: _temporalTypes.isDate,
302 isLocalDateTime: _temporalTypes.isLocalDateTime,
303 isDateTime: _temporalTypes.isDateTime
304};
305
306/**
307 * @private
308 */
309var forExport = {
310 driver: driver,
311 int: _integer.int,
312 isInt: _integer.isInt,
313 isPoint: _spatialTypes.isPoint,
314 isDuration: _temporalTypes.isDuration,
315 isLocalTime: _temporalTypes.isLocalTime,
316 isTime: _temporalTypes.isTime,
317 isDate: _temporalTypes.isDate,
318 isLocalDateTime: _temporalTypes.isLocalDateTime,
319 isDateTime: _temporalTypes.isDateTime,
320 integer: integer,
321 Neo4jError: _error.Neo4jError,
322 auth: auth,
323 types: types,
324 session: session,
325 error: error,
326 spatial: spatial,
327 temporal: temporal
328};
329
330exports.driver = driver;
331exports.int = _integer.int;
332exports.isInt = _integer.isInt;
333exports.isPoint = _spatialTypes.isPoint;
334exports.isDuration = _temporalTypes.isDuration;
335exports.isLocalTime = _temporalTypes.isLocalTime;
336exports.isTime = _temporalTypes.isTime;
337exports.isDate = _temporalTypes.isDate;
338exports.isLocalDateTime = _temporalTypes.isLocalDateTime;
339exports.isDateTime = _temporalTypes.isDateTime;
340exports.integer = integer;
341exports.Neo4jError = _error.Neo4jError;
342exports.auth = auth;
343exports.types = types;
344exports.session = session;
345exports.error = error;
346exports.spatial = spatial;
347exports.temporal = temporal;
348exports.default = forExport;
\No newline at end of file