|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [https://neo4j.com] |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import BoltProtocolV5x4 from './bolt-protocol-v5x4' |
| 18 | + |
| 19 | +import transformersFactories from './bolt-protocol-v5x5.transformer' |
| 20 | +import Transformer from './transformer' |
| 21 | +import RequestMessage from './request-message' |
| 22 | +import { LoginObserver, ResultStreamObserver } from './stream-observers' |
| 23 | + |
| 24 | +import { internal } from 'neo4j-driver-core' |
| 25 | + |
| 26 | +const { |
| 27 | + constants: { BOLT_PROTOCOL_V5_5, FETCH_ALL } |
| 28 | +} = internal |
| 29 | + |
| 30 | +const DEFAULT_DIAGNOSTIC_RECORD = Object.freeze({ |
| 31 | + OPERATION: '', |
| 32 | + OPERATION_CODE: '0', |
| 33 | + CURRENT_SCHEMA: '/' |
| 34 | +}) |
| 35 | + |
| 36 | +export default class BoltProtocol extends BoltProtocolV5x4 { |
| 37 | + get version () { |
| 38 | + return BOLT_PROTOCOL_V5_5 |
| 39 | + } |
| 40 | + |
| 41 | + get transformer () { |
| 42 | + if (this._transformer === undefined) { |
| 43 | + this._transformer = new Transformer(Object.values(transformersFactories).map(create => create(this._config, this._log))) |
| 44 | + } |
| 45 | + return this._transformer |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Initialize a connection with the server |
| 50 | + * |
| 51 | + * @param {Object} args The params |
| 52 | + * @param {string} args.userAgent The user agent |
| 53 | + * @param {any} args.authToken The auth token |
| 54 | + * @param {NotificationFilter} args.notificationFilter The notification filters. |
| 55 | + * @param {function(error)} args.onError On error callback |
| 56 | + * @param {function(onComplete)} args.onComplete On complete callback |
| 57 | + * @returns {LoginObserver} The Login observer |
| 58 | + */ |
| 59 | + initialize ({ userAgent, boltAgent, authToken, notificationFilter, onError, onComplete } = {}) { |
| 60 | + const state = {} |
| 61 | + const observer = new LoginObserver({ |
| 62 | + onError: error => this._onLoginError(error, onError), |
| 63 | + onCompleted: metadata => { |
| 64 | + state.metadata = metadata |
| 65 | + return this._onLoginCompleted(metadata) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + this.write( |
| 70 | + RequestMessage.hello5x5(userAgent, boltAgent, notificationFilter, this._serversideRouting), |
| 71 | + observer, |
| 72 | + false |
| 73 | + ) |
| 74 | + |
| 75 | + return this.logon({ |
| 76 | + authToken, |
| 77 | + onComplete: metadata => onComplete({ ...metadata, ...state.metadata }), |
| 78 | + onError, |
| 79 | + flush: true |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + beginTransaction ({ |
| 84 | + bookmarks, |
| 85 | + txConfig, |
| 86 | + database, |
| 87 | + mode, |
| 88 | + impersonatedUser, |
| 89 | + notificationFilter, |
| 90 | + beforeError, |
| 91 | + afterError, |
| 92 | + beforeComplete, |
| 93 | + afterComplete |
| 94 | + } = {}) { |
| 95 | + const observer = new ResultStreamObserver({ |
| 96 | + server: this._server, |
| 97 | + beforeError, |
| 98 | + afterError, |
| 99 | + beforeComplete, |
| 100 | + afterComplete |
| 101 | + }) |
| 102 | + observer.prepareToHandleSingleResponse() |
| 103 | + |
| 104 | + this.write( |
| 105 | + RequestMessage.begin5x5({ bookmarks, txConfig, database, mode, impersonatedUser, notificationFilter }), |
| 106 | + observer, |
| 107 | + true |
| 108 | + ) |
| 109 | + |
| 110 | + return observer |
| 111 | + } |
| 112 | + |
| 113 | + run ( |
| 114 | + query, |
| 115 | + parameters, |
| 116 | + { |
| 117 | + bookmarks, |
| 118 | + txConfig, |
| 119 | + database, |
| 120 | + mode, |
| 121 | + impersonatedUser, |
| 122 | + notificationFilter, |
| 123 | + beforeKeys, |
| 124 | + afterKeys, |
| 125 | + beforeError, |
| 126 | + afterError, |
| 127 | + beforeComplete, |
| 128 | + afterComplete, |
| 129 | + flush = true, |
| 130 | + reactive = false, |
| 131 | + fetchSize = FETCH_ALL, |
| 132 | + highRecordWatermark = Number.MAX_VALUE, |
| 133 | + lowRecordWatermark = Number.MAX_VALUE |
| 134 | + } = {} |
| 135 | + ) { |
| 136 | + const observer = new ResultStreamObserver({ |
| 137 | + server: this._server, |
| 138 | + reactive, |
| 139 | + fetchSize, |
| 140 | + moreFunction: this._requestMore.bind(this), |
| 141 | + discardFunction: this._requestDiscard.bind(this), |
| 142 | + beforeKeys, |
| 143 | + afterKeys, |
| 144 | + beforeError, |
| 145 | + afterError, |
| 146 | + beforeComplete, |
| 147 | + afterComplete, |
| 148 | + highRecordWatermark, |
| 149 | + lowRecordWatermark, |
| 150 | + enrichMetadata: BoltProtocol._enrichMetadata |
| 151 | + }) |
| 152 | + |
| 153 | + const flushRun = reactive |
| 154 | + this.write( |
| 155 | + RequestMessage.runWithMetadata5x5(query, parameters, { |
| 156 | + bookmarks, |
| 157 | + txConfig, |
| 158 | + database, |
| 159 | + mode, |
| 160 | + impersonatedUser, |
| 161 | + notificationFilter |
| 162 | + }), |
| 163 | + observer, |
| 164 | + flushRun && flush |
| 165 | + ) |
| 166 | + |
| 167 | + if (!reactive) { |
| 168 | + this.write(RequestMessage.pull({ n: fetchSize }), observer, flush) |
| 169 | + } |
| 170 | + |
| 171 | + return observer |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * |
| 176 | + * @param {object} metadata |
| 177 | + * @returns {object} |
| 178 | + */ |
| 179 | + static _enrichMetadata (metadata) { |
| 180 | + if (Array.isArray(metadata.statuses)) { |
| 181 | + metadata.statuses = metadata.statuses.map(status => ({ |
| 182 | + ...status, |
| 183 | + diagnostic_record: status.diagnostic_record !== null ? { ...DEFAULT_DIAGNOSTIC_RECORD, ...status.diagnostic_record } : null |
| 184 | + })) |
| 185 | + } |
| 186 | + |
| 187 | + return metadata |
| 188 | + } |
| 189 | +} |
0 commit comments