|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2018 "Neo4j," |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import RequestMessage from './request-message'; |
| 20 | +import * as v1 from './packstream-v1'; |
| 21 | + |
| 22 | +export default class BoltProtocol { |
| 23 | + |
| 24 | + /** |
| 25 | + * @constructor |
| 26 | + * @param {Connection} connection the connection. |
| 27 | + * @param {Chunker} chunker the chunker. |
| 28 | + * @param {boolean} disableLosslessIntegers if this connection should convert all received integers to native JS numbers. |
| 29 | + */ |
| 30 | + constructor(connection, chunker, disableLosslessIntegers) { |
| 31 | + this._connection = connection; |
| 32 | + this._packer = this._createPacker(chunker); |
| 33 | + this._unpacker = this._createUnpacker(disableLosslessIntegers); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the packer. |
| 38 | + * @return {Packer} the protocol's packer. |
| 39 | + */ |
| 40 | + packer() { |
| 41 | + return this._packer; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the unpacker. |
| 46 | + * @return {Unpacker} the protocol's unpacker. |
| 47 | + */ |
| 48 | + unpacker() { |
| 49 | + return this._unpacker; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Perform initialization and authentication of the underlying connection. |
| 54 | + * @param {string} clientName the client name. |
| 55 | + * @param {object} authToken the authentication token. |
| 56 | + * @param {StreamObserver} observer the response observer. |
| 57 | + */ |
| 58 | + initialize(clientName, authToken, observer) { |
| 59 | + const message = RequestMessage.init(clientName, authToken); |
| 60 | + this._connection.write(message, observer, true); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Begin an explicit transaction. |
| 65 | + * @param {Bookmark} bookmark the bookmark. |
| 66 | + * @param {StreamObserver} observer the response observer. |
| 67 | + */ |
| 68 | + beginTransaction(bookmark, observer) { |
| 69 | + const runMessage = RequestMessage.run('BEGIN', bookmark.asBeginTransactionParameters()); |
| 70 | + const pullAllMessage = RequestMessage.pullAll(); |
| 71 | + |
| 72 | + this._connection.write(runMessage, observer, false); |
| 73 | + this._connection.write(pullAllMessage, observer, false); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Commit the explicit transaction. |
| 78 | + * @param {StreamObserver} observer the response observer. |
| 79 | + */ |
| 80 | + commitTransaction(observer) { |
| 81 | + this.run('COMMIT', {}, observer); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Rollback the explicit transaction. |
| 86 | + * @param {StreamObserver} observer the response observer. |
| 87 | + */ |
| 88 | + rollbackTransaction(observer) { |
| 89 | + this.run('ROLLBACK', {}, observer); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Send a Cypher statement through the underlying connection. |
| 94 | + * @param {string} statement the cypher statement. |
| 95 | + * @param {object} parameters the statement parameters. |
| 96 | + * @param {StreamObserver} observer the response observer. |
| 97 | + */ |
| 98 | + run(statement, parameters, observer) { |
| 99 | + const runMessage = RequestMessage.run(statement, parameters); |
| 100 | + const pullAllMessage = RequestMessage.pullAll(); |
| 101 | + |
| 102 | + this._connection.write(runMessage, observer, false); |
| 103 | + this._connection.write(pullAllMessage, observer, true); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Send a RESET through the underlying connection. |
| 108 | + * @param {StreamObserver} observer the response observer. |
| 109 | + */ |
| 110 | + reset(observer) { |
| 111 | + const message = RequestMessage.reset(); |
| 112 | + this._connection.write(message, observer, true); |
| 113 | + } |
| 114 | + |
| 115 | + _createPacker(chunker) { |
| 116 | + return new v1.Packer(chunker); |
| 117 | + } |
| 118 | + |
| 119 | + _createUnpacker(disableLosslessIntegers) { |
| 120 | + return new v1.Unpacker(disableLosslessIntegers); |
| 121 | + } |
| 122 | +} |
0 commit comments