|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology,"," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.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 | + |
| 20 | +import sharedNeo4j from './shared-neo4j'; |
| 21 | +import neo4j from '../../src/v1/index'; |
| 22 | + |
| 23 | +class UnsupportedBoltStub { |
| 24 | + |
| 25 | + start(script, port) { |
| 26 | + throw new Error('BoltStub: unable to start, unavailable on this platform'); |
| 27 | + } |
| 28 | + |
| 29 | + startWithTemplate(scriptTemplate, parameters, port) { |
| 30 | + throw new Error('BoltStub: unable to start with template, unavailable on this platform'); |
| 31 | + } |
| 32 | + |
| 33 | + run(callback) { |
| 34 | + throw new Error('BoltStub: unable to run, unavailable on this platform'); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +const verbose = false; // for debugging purposes |
| 39 | + |
| 40 | +class SupportedBoltStub extends UnsupportedBoltStub { |
| 41 | + |
| 42 | + constructor() { |
| 43 | + super(); |
| 44 | + this._childProcess = require('child_process'); |
| 45 | + this._mustache = require('mustache'); |
| 46 | + this._fs = require('fs'); |
| 47 | + this._tmp = require('tmp'); |
| 48 | + } |
| 49 | + |
| 50 | + static create() { |
| 51 | + try { |
| 52 | + return new SupportedBoltStub(); |
| 53 | + } catch (e) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + start(script, port) { |
| 59 | + const boltStub = this._childProcess.spawn('boltstub', ['-v', port, script]); |
| 60 | + |
| 61 | + if (verbose) { |
| 62 | + boltStub.stdout.on('data', (data) => { |
| 63 | + console.log(`${data}`); |
| 64 | + }); |
| 65 | + boltStub.stderr.on('data', (data) => { |
| 66 | + console.log(`${data}`); |
| 67 | + }); |
| 68 | + boltStub.on('end', data => { |
| 69 | + console.log(data); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + let exitCode = -1; |
| 74 | + boltStub.on('close', code => { |
| 75 | + exitCode = code; |
| 76 | + }); |
| 77 | + |
| 78 | + boltStub.on('error', error => { |
| 79 | + console.log('Failed to start child process:' + error); |
| 80 | + }); |
| 81 | + |
| 82 | + return new StubServer(() => exitCode); |
| 83 | + } |
| 84 | + |
| 85 | + startWithTemplate(scriptTemplate, parameters, port) { |
| 86 | + const template = this._fs.readFileSync(scriptTemplate, 'utf-8'); |
| 87 | + const scriptContents = this._mustache.render(template, parameters); |
| 88 | + const script = this._tmp.fileSync().name; |
| 89 | + this._fs.writeFileSync(script, scriptContents, 'utf-8'); |
| 90 | + return this.start(script, port); |
| 91 | + } |
| 92 | + |
| 93 | + run(callback) { |
| 94 | + // wait to make sure boltstub is started before running user code |
| 95 | + setTimeout(callback, 1000); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class StubServer { |
| 100 | + |
| 101 | + constructor(exitCodeSupplier) { |
| 102 | + this._exitCodeSupplier = exitCodeSupplier; |
| 103 | + this.exit.bind(this); |
| 104 | + } |
| 105 | + |
| 106 | + exit(callback) { |
| 107 | + // give process some time to exit |
| 108 | + setTimeout(() => { |
| 109 | + callback(this._exitCodeSupplier()); |
| 110 | + }, 1000); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function newDriver(url) { |
| 115 | + // boltstub currently does not support encryption, create driver with encryption turned off |
| 116 | + const config = { |
| 117 | + encrypted: 'ENCRYPTION_OFF' |
| 118 | + }; |
| 119 | + return neo4j.driver(url, sharedNeo4j.authToken, config); |
| 120 | +} |
| 121 | + |
| 122 | +const supportedStub = SupportedBoltStub.create(); |
| 123 | +const supported = supportedStub != null; |
| 124 | +const stub = supported ? supportedStub : new UnsupportedBoltStub(); |
| 125 | + |
| 126 | +export default { |
| 127 | + supported: supported, |
| 128 | + start: stub.start.bind(stub), |
| 129 | + startWithTemplate: stub.startWithTemplate.bind(stub), |
| 130 | + run: stub.run.bind(stub), |
| 131 | + newDriver: newDriver |
| 132 | +}; |
0 commit comments