Use ES6 syntax - Use class definitions - Use const and let - Computed property names in object literals
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/87e02146 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/87e02146 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/87e02146 Branch: refs/heads/TINKERPOP-1489 Commit: 87e0214600077401fbfab22fc33056d2c0d45f3c Parents: c7652cc Author: Jorge Bay Gondra <jorgebaygon...@gmail.com> Authored: Thu Nov 30 14:45:22 2017 +0100 Committer: Jorge Bay Gondra <jorgebaygon...@gmail.com> Committed: Mon Jan 8 11:30:48 2018 +0100 ---------------------------------------------------------------------- .../glv/GraphTraversalSource.template | 155 +- gremlin-javascript/glv/TraversalSource.template | 247 +- .../lib/driver/driver-remote-connection.js | 279 +- .../lib/driver/remote-connection.js | 86 +- .../gremlin-javascript/lib/process/bytecode.js | 125 +- .../lib/process/graph-traversal.js | 2439 +++++++++--------- .../lib/process/traversal-strategy.js | 99 +- .../gremlin-javascript/lib/process/traversal.js | 363 +-- .../gremlin-javascript/lib/structure/graph.js | 180 +- .../lib/structure/io/graph-serializer.js | 535 +--- .../lib/structure/io/type-serializers.js | 288 +++ .../javascript/gremlin-javascript/lib/utils.js | 7 +- .../test/integration/remote-connection-tests.js | 2 +- .../test/integration/traversal-test.js | 2 +- .../test/unit/exports-test.js | 4 +- .../test/unit/graphson-test.js | 38 +- .../test/unit/traversal-test.js | 26 +- 17 files changed, 2451 insertions(+), 2424 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87e02146/gremlin-javascript/glv/GraphTraversalSource.template ---------------------------------------------------------------------- diff --git a/gremlin-javascript/glv/GraphTraversalSource.template b/gremlin-javascript/glv/GraphTraversalSource.template index e0fb453..812cfcf 100644 --- a/gremlin-javascript/glv/GraphTraversalSource.template +++ b/gremlin-javascript/glv/GraphTraversalSource.template @@ -22,93 +22,96 @@ */ 'use strict'; -var t = require('./traversal.js'); -var remote = require('../driver/remote-connection'); -var utils = require('../utils'); -var Bytecode = require('./bytecode'); -var TraversalStrategies = require('./traversal-strategy').TraversalStrategies; -var inherits = utils.inherits; -var parseArgs = utils.parseArgs; +const Traversal = require('./traversal').Traversal; +const remote = require('../driver/remote-connection'); +const utils = require('../utils'); +const Bytecode = require('./bytecode'); +const TraversalStrategies = require('./traversal-strategy').TraversalStrategies; +const parseArgs = utils.parseArgs; -/** - * - * @param {Graph} graph - * @param {TraversalStrategies} traversalStrategies - * @param {Bytecode} [bytecode] - * @constructor - */ -function GraphTraversalSource(graph, traversalStrategies, bytecode) { - this.graph = graph; - this.traversalStrategies = traversalStrategies; - this.bytecode = bytecode || new Bytecode(); -} /** - * @param remoteConnection - * @returns {GraphTraversalSource} + * Represents the primary DSL of the Gremlin traversal machine. */ -GraphTraversalSource.prototype.withRemote = function (remoteConnection) { - var traversalStrategy = new TraversalStrategies(this.traversalStrategies); - traversalStrategy.addStrategy(new remote.RemoteStrategy(remoteConnection)); - return new GraphTraversalSource(this.graph, traversalStrategy, new Bytecode(this.bytecode)); -}; +class GraphTraversalSource { + /** + * @param {Graph} graph + * @param {TraversalStrategies} traversalStrategies + * @param {Bytecode} [bytecode] + */ + constructor(graph, traversalStrategies, bytecode) { + this.graph = graph; + this.traversalStrategies = traversalStrategies; + this.bytecode = bytecode || new Bytecode(); + } -/** - * Returns the string representation of the GraphTraversalSource. - * @returns {string} - */ -GraphTraversalSource.prototype.toString = function () { - return 'graphtraversalsource[' + this.graph.toString() + ']'; -}; -<% sourceStepMethods.each{ method -> %> -/** - * Graph Traversal Source <%= method %> method. - * @param {...Object} args - * @returns {GraphTraversalSource} - */ -GraphTraversalSource.prototype.<%= toJs.call(method) %> = function (args) { - var b = new Bytecode(this.bytecode).addSource('<%= method %>', parseArgs.apply(null, arguments)); - return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b); -}; -<% + /** + * @param remoteConnection + * @returns {GraphTraversalSource} + */ + withRemote(remoteConnection) { + const traversalStrategy = new TraversalStrategies(this.traversalStrategies); + traversalStrategy.addStrategy(new remote.RemoteStrategy(remoteConnection)); + return new GraphTraversalSource(this.graph, traversalStrategy, new Bytecode(this.bytecode)); + } + + /** + * Returns the string representation of the GraphTraversalSource. + * @returns {string} + */ + toString() { + return 'graphtraversalsource[' + this.graph.toString() + ']'; + } + <% sourceStepMethods.each{ method -> %> + /** + * Graph Traversal Source <%= method %> method. + * @param {...Object} args + * @returns {GraphTraversalSource} + */ + <%= toJs.call(method) %>(args) { + const b = new Bytecode(this.bytecode).addSource('<%= method %>', parseArgs.apply(null, arguments)); + return new GraphTraversalSource(this.graph, new TraversalStrategies(this.traversalStrategies), b); + } + <% + } + sourceSpawnMethods.each{ method -> %> + /** + * <%= method %> GraphTraversalSource step method. + * @param {...Object} args + * @returns {GraphTraversal} + */ + <%= toJs.call(method) %>(args) { + const b = new Bytecode(this.bytecode).addStep('<%= method %>', parseArgs.apply(null, arguments)); + return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b); + } + <% } %> } -sourceSpawnMethods.each{ method -> %> -/** - * <%= method %> GraphTraversalSource step method. - * @param {...Object} args - * @returns {GraphTraversal} - */ -GraphTraversalSource.prototype.<%= toJs.call(method) %> = function (args) { - var b = new Bytecode(this.bytecode).addStep('<%= method %>', parseArgs.apply(null, arguments)); - return new GraphTraversal(this.graph, new TraversalStrategies(this.traversalStrategies), b); -}; -<% } %> + /** * Represents a graph traversal. - * @extends Traversal - * @constructor */ -function GraphTraversal(graph, traversalStrategies, bytecode) { - t.Traversal.call(this, graph, traversalStrategies, bytecode); +class GraphTraversal extends Traversal { + constructor(graph, traversalStrategies, bytecode) { + super(graph, traversalStrategies, bytecode); + } + <% graphStepMethods.each{ method -> %> + /** + * Graph traversal <%= method %> method. + * @param {...Object} args + * @returns {GraphTraversal} + */ + <%= toJs.call(method) %>(args) { + this.bytecode.addStep('<%= method %>', parseArgs.apply(null, arguments)); + return this; + } + <% } %> } -inherits(GraphTraversal, t.Traversal); -<% graphStepMethods.each{ method -> %> -/** - * Graph traversal <%= method %> method. - * @param {...Object} args - * @returns {GraphTraversal} - */ -GraphTraversal.prototype.<%= toJs.call(method) %> = function (args) { - this.bytecode.addStep('<%= method %>', parseArgs.apply(null, arguments)); - return this; -}; -<% } %> /** * Contains the static method definitions * @type {Object} */ -var statics = {}; +const statics = {}; <% anonStepMethods.each{ method -> %> /** * <%= method %>() static method @@ -116,12 +119,12 @@ var statics = {}; * @returns {GraphTraversal} */ statics.<%= toJs.call(method) %> = function (args) { - var g = new GraphTraversal(null, null, new Bytecode()); + const g = new GraphTraversal(null, null, new Bytecode()); return g.<%= toJs.call(method) %>.apply(g, arguments); }; <% } %> module.exports = { - GraphTraversal: GraphTraversal, - GraphTraversalSource: GraphTraversalSource, - statics: statics + GraphTraversal, + GraphTraversalSource, + statics }; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87e02146/gremlin-javascript/glv/TraversalSource.template ---------------------------------------------------------------------- diff --git a/gremlin-javascript/glv/TraversalSource.template b/gremlin-javascript/glv/TraversalSource.template index 0d45e90..d707a42 100644 --- a/gremlin-javascript/glv/TraversalSource.template +++ b/gremlin-javascript/glv/TraversalSource.template @@ -26,142 +26,149 @@ const utils = require('../utils'); const parseArgs = utils.parseArgs; const itemDone = Object.freeze({ value: null, done: true }); -function Traversal(graph, traversalStrategies, bytecode) { - this.graph = graph; - this.traversalStrategies = traversalStrategies; - this.bytecode = bytecode; - /** @type {Array<Traverser>} */ - this.traversers = null; - this.sideEffects = null; - this._traversalStrategiesPromise = null; - this._traversersIteratorIndex = 0; -} +class Traversal { + constructor(graph, traversalStrategies, bytecode) { + this.graph = graph; + this.traversalStrategies = traversalStrategies; + this.bytecode = bytecode; + /** @type {Array<Traverser>} */ + this.traversers = null; + this.sideEffects = null; + this._traversalStrategiesPromise = null; + this._traversersIteratorIndex = 0; + } -/** @returns {Bytecode} */ -Traversal.prototype.getBytecode = function () { - return this.bytecode; -}; + /** @returns {Bytecode} */ + getBytecode() { + return this.bytecode; + } -/** - * Returns an Array containing the traverser objects. - * @returns {Promise.<Array>} - */ -Traversal.prototype.toList = function () { - return this._applyStrategies().then(() => { - const result = []; - let it; - while ((it = this._getNext()) && !it.done) { - result.push(it.value); - } - return result; - }); -}; + /** + * Returns an Array containing the traverser objects. + * @returns {Promise.<Array>} + */ + toList() { + return this._applyStrategies().then(() => { + const result = []; + let it; + while ((it = this._getNext()) && !it.done) { + result.push(it.value); + } + return result; + }); + }; + + /** + * Iterates all Traverser instances in the traversal. + * @returns {Promise} + */ + iterate() { + return this._applyStrategies().then(() => { + let it; + while ((it = this._getNext()) && !it.done) { + } + }); + } -/** - * Iterates all Traverser instances in the traversal. - * @returns {Promise} - */ -Traversal.prototype.iterate = function () { - return this._applyStrategies().then(() => { - let it; - while ((it = this._getNext()) && !it.done) { - } - }); -}; + /** + * Async iterator method implementation. + * Returns a promise containing an iterator item. + * @returns {Promise.<{value, done}>} + */ + next() { + return this._applyStrategies().then(() => this._getNext()); + } -/** - * Async iterator method implementation. - * Returns a promise containing an iterator item. - * @returns {Promise.<{value, done}>} - */ -Traversal.prototype.next = function () { - return this._applyStrategies().then(() => this._getNext()); -}; + /** + * Synchronous iterator of traversers including + * @private + */ + _getNext() { + while (this.traversers && this._traversersIteratorIndex < this.traversers.length) { + let traverser = this.traversers[this._traversersIteratorIndex]; + if (traverser.bulk > 0) { + traverser.bulk--; + return { value: traverser.object, done: false }; + } + this._traversersIteratorIndex++; + } + return itemDone; + } -/** - * Synchronous iterator of traversers including - * @private - */ -Traversal.prototype._getNext = function () { - while (this.traversers && this._traversersIteratorIndex < this.traversers.length) { - let traverser = this.traversers[this._traversersIteratorIndex]; - if (traverser.bulk > 0) { - traverser.bulk--; - return { value: traverser.object, done: false }; + _applyStrategies() { + if (this._traversalStrategiesPromise) { + // Apply strategies only once + return this._traversalStrategiesPromise; } - this._traversersIteratorIndex++; + return this._traversalStrategiesPromise = this.traversalStrategies.applyStrategies(this); } - return itemDone; -}; -Traversal.prototype._applyStrategies = function () { - if (this._traversalStrategiesPromise) { - // Apply strategies only once - return this._traversalStrategiesPromise; + /** + * Returns the Bytecode JSON representation of the traversal + * @returns {String} + */ + toString() { + return this.bytecode.toString(); + }; +} + +class P { + /** + * Represents an operation. + * @constructor + */ + constructor(operator, value, other) { + this.operator = operator; + this.value = value; + this.other = other; } - return this._traversalStrategiesPromise = this.traversalStrategies.applyStrategies(this); -}; -/** - * Returns the Bytecode JSON representation of the traversal - * @returns {String} - */ -Traversal.prototype.toString = function () { - return this.bytecode.toString(); -}; + /** + * Returns the string representation of the instance. + * @returns {string} + */ + toString() { + if (this.other === undefined) { + return this.operator + '(' + this.value + ')'; + } + return this.operator + '(' + this.value + ', ' + this.other + ')'; + } -/** - * Represents an operation. - * @constructor - */ -function P(operator, value, other) { - this.operator = operator; - this.value = value; - this.other = other; -} + and(arg) { + return new P('and', this, arg); + } -/** - * Returns the string representation of the instance. - * @returns {string} - */ -P.prototype.toString = function () { - if (this.other === undefined) { - return this.operator + '(' + this.value + ')'; + or(arg) { + return new P('or', this, arg); + } +<% pmethods.each{ method -> %> + /** @param {...Object} args */ + static <%= toJs.call(method) %>(args) { + return createP('<%= method %>', parseArgs.apply(null, arguments)); } - return this.operator + '(' + this.value + ', ' + this.other + ')'; -}; +<% } %> +} function createP(operator, args) { args.unshift(null, operator); return new (Function.prototype.bind.apply(P, args)); } -<% pmethods.each{ method -> %> -/** @param {...Object} args */ -P.<%= toJs.call(method) %> = function (args) { - return createP('<%= method %>', parseArgs.apply(null, arguments)); -}; -<% } %> -P.prototype.and = function (arg) { - return new P('and', this, arg); -}; - -P.prototype.or = function (arg) { - return new P('or', this, arg); -}; -function Traverser(object, bulk) { - this.object = object; - this.bulk = bulk || 1; +class Traverser { + constructor(object, bulk) { + this.object = object; + this.bulk = bulk || 1; + } } -function TraversalSideEffects() { +class TraversalSideEffects { } function toEnum(typeName, keys) { - var result = {}; - keys.split(' ').forEach(function (k) { - var jsKey = k; + const result = {}; + keys.split(' ').forEach(k => { + let jsKey = k; if (jsKey === jsKey.toUpperCase()) { jsKey = jsKey.toLowerCase(); } @@ -170,17 +177,19 @@ function toEnum(typeName, keys) { return result; } -function EnumValue(typeName, elementName) { - this.typeName = typeName; - this.elementName = elementName; +class EnumValue { + constructor(typeName, elementName) { + this.typeName = typeName; + this.elementName = elementName; + } } module.exports = { - EnumValue: EnumValue, - P: P, - Traversal: Traversal, - TraversalSideEffects: TraversalSideEffects, - Traverser: Traverser<% + EnumValue, + P, + Traversal, + TraversalSideEffects, + Traverser<% enums.each{ enumClass -> out.print ",\n " + decapitalize.call(enumClass.simpleName) + ": toEnum('" + enumClass.simpleName + "', '" + enumClass.getEnumConstants().sort { a, b -> a.name() <=> b.name() }.collect { toJs.call(it.name()) }.join(' ') + "')" http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87e02146/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js index 51661c3..ac3fb0f 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/driver-remote-connection.js @@ -22,165 +22,164 @@ */ 'use strict'; -var crypto = require('crypto'); -var WebSocket = require('ws'); -var util = require('util'); -var RemoteConnection = require('./remote-connection').RemoteConnection; -var utils = require('../utils'); -var serializer = require('../structure/io/graph-serializer'); -var inherits = utils.inherits; -var defaultMimeType = 'application/vnd.gremlin-v2.0+json'; -var responseStatusCode = { +const crypto = require('crypto'); +const WebSocket = require('ws'); +const util = require('util'); +const RemoteConnection = require('./remote-connection').RemoteConnection; +const utils = require('../utils'); +const serializer = require('../structure/io/graph-serializer'); +const responseStatusCode = { success: 200, noContent: 204, partialContent: 206 }; +const defaultMimeType = 'application/vnd.gremlin-v2.0+json'; -/** - * Creates a new instance of DriverRemoteConnection. - * @param {String} url The resource uri. - * @param {Object} [options] The connection options. - * @param {Array} [options.ca] Trusted certificates. - * @param {String|Array|Buffer} [options.cert] The certificate key. - * @param {String} [options.mimeType] The mime type to use. - * @param {String|Buffer} [options.pfx] The private key, certificate, and CA certs. - * @param {GraphSONReader} [options.reader] The reader to use. - * @param {Boolean} [options.rejectUnauthorized] Determines whether to verify or not the server certificate. - * @param {String} [options.traversalSource] The traversal source. Defaults to: 'g'. - * @param {GraphSONWriter} [options.writer] The writer to use. - * @constructor - */ -function DriverRemoteConnection(url, options) { - RemoteConnection.call(this, url); - options = options || {}; - this._ws = new WebSocket(url, { - ca: options.ca, - cert: options.cert, - pfx: options.pfx, - rejectUnauthorized: options.rejectUnauthorized - }); - var self = this; - this._ws.on('open', function opened () { - self.isOpen = true; - if (self._openCallback) { - self._openCallback(); - } - }); - this._ws.on('message', function incoming (data) { - self._handleMessage(data); - }); - // A map containing the request id and the handler - this._responseHandlers = {}; - this._reader = options.reader || new serializer.GraphSONReader(); - this._writer = options.writer || new serializer.GraphSONWriter(); - this._openPromise = null; - this._openCallback = null; - this._closePromise = null; - var mimeType = options.mimeType || defaultMimeType; - this._header = String.fromCharCode(mimeType.length) + mimeType; - this.isOpen = false; - this.traversalSource = options.traversalSource || 'g'; -} - -inherits(DriverRemoteConnection, RemoteConnection); +class DriverRemoteConnection extends RemoteConnection { + /** + * Creates a new instance of DriverRemoteConnection. + * @param {String} url The resource uri. + * @param {Object} [options] The connection options. + * @param {Array} [options.ca] Trusted certificates. + * @param {String|Array|Buffer} [options.cert] The certificate key. + * @param {String} [options.mimeType] The mime type to use. + * @param {String|Buffer} [options.pfx] The private key, certificate, and CA certs. + * @param {GraphSONReader} [options.reader] The reader to use. + * @param {Boolean} [options.rejectUnauthorized] Determines whether to verify or not the server certificate. + * @param {String} [options.traversalSource] The traversal source. Defaults to: 'g'. + * @param {GraphSONWriter} [options.writer] The writer to use. + * @constructor + */ + constructor(url, options) { + super(url); + options = options || {}; + this._ws = new WebSocket(url, { + ca: options.ca, + cert: options.cert, + pfx: options.pfx, + rejectUnauthorized: options.rejectUnauthorized + }); + var self = this; + this._ws.on('open', function opened () { + self.isOpen = true; + if (self._openCallback) { + self._openCallback(); + } + }); + this._ws.on('message', function incoming (data) { + self._handleMessage(data); + }); + // A map containing the request id and the handler + this._responseHandlers = {}; + this._reader = options.reader || new serializer.GraphSONReader(); + this._writer = options.writer || new serializer.GraphSONWriter(); + this._openPromise = null; + this._openCallback = null; + this._closePromise = null; + const mimeType = options.mimeType || defaultMimeType; + this._header = String.fromCharCode(mimeType.length) + mimeType; + this.isOpen = false; + this.traversalSource = options.traversalSource || 'g'; + } -/** - * Opens the connection, if its not already opened. - * @returns {Promise} - */ -DriverRemoteConnection.prototype.open = function (promiseFactory) { - if (this._closePromise) { + /** + * Opens the connection, if its not already opened. + * @returns {Promise} + */ + open(promiseFactory) { + if (this._closePromise) { + return this._openPromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { + callback(new Error('Connection has been closed')); + }); + } + if (this._openPromise) { + return this._openPromise; + } + const self = this; return this._openPromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { - callback(new Error('Connection has been closed')); + if (self.isOpen) { + return callback(); + } + // It will be invoked when opened + self._openCallback = callback; }); } - if (this._openPromise) { - return this._openPromise; + + /** @override */ + submit(bytecode, promiseFactory) { + const self = this; + return this.open().then(function () { + return utils.toPromise(promiseFactory, function promiseHandler(callback) { + const requestId = getUuid(); + self._responseHandlers[requestId] = { + callback: callback, + result: null + }; + const message = bufferFromString(self._header + JSON.stringify(self._getRequest(requestId, bytecode))); + self._ws.send(message); + }); + }); } - var self = this; - return this._openPromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { - if (self.isOpen) { - return callback(); - } - // It will be invoked when opened - self._openCallback = callback; - }); -}; -/** @override */ -DriverRemoteConnection.prototype.submit = function (bytecode, promiseFactory) { - var self = this; - return this.open().then(function () { - return utils.toPromise(promiseFactory, function promiseHandler(callback) { - var requestId = getUuid(); - self._responseHandlers[requestId] = { - callback: callback, - result: null - }; - var message = bufferFromString(self._header + JSON.stringify(self._getRequest(requestId, bytecode))); - self._ws.send(message); + _getRequest(id, bytecode) { + return ({ + 'requestId': { '@type': 'g:UUID', '@value': id }, + 'op': 'bytecode', + 'processor': 'traversal', + 'args': { + 'gremlin': this._writer.adaptObject(bytecode), + 'aliases': { 'g': this.traversalSource } + } }); - }); -}; + } -DriverRemoteConnection.prototype._getRequest = function (id, bytecode) { - return ({ - 'requestId': { '@type': 'g:UUID', '@value': id }, - 'op': 'bytecode', - 'processor': 'traversal', - 'args': { - 'gremlin': this._writer.adaptObject(bytecode), - 'aliases': { 'g': this.traversalSource } + _handleMessage(data) { + const response = this._reader.read(JSON.parse(data.toString())); + const handler = this._responseHandlers[response.requestId]; + if (response.status.code >= 400) { + // callback in error + return handler.callback( + new Error(util.format('Server error: %s (%d)', response.status.message, response.status.code))); } - }); -}; - -DriverRemoteConnection.prototype._handleMessage = function (data) { - var response = this._reader.read(JSON.parse(data.toString())); - var handler = this._responseHandlers[response.requestId]; - if (response.status.code >= 400) { - // callback in error - return handler.callback( - new Error(util.format('Server error: %s (%d)', response.status.message, response.status.code))); - } - switch (response.status.code) { - case responseStatusCode.noContent: - return handler.callback(null, { traversers: []}); - case responseStatusCode.partialContent: - handler.result = handler.result || []; - handler.result.push.apply(handler.result, response.result.data); - break; - default: - if (handler.result) { + switch (response.status.code) { + case responseStatusCode.noContent: + return handler.callback(null, { traversers: []}); + case responseStatusCode.partialContent: + handler.result = handler.result || []; handler.result.push.apply(handler.result, response.result.data); - } - else { - handler.result = response.result.data; - } - return handler.callback(null, { traversers: handler.result }); + break; + default: + if (handler.result) { + handler.result.push.apply(handler.result, response.result.data); + } + else { + handler.result = response.result.data; + } + return handler.callback(null, { traversers: handler.result }); + } } -}; -/** - * Closes the Connection. - * @return {Promise} - */ -DriverRemoteConnection.prototype.close = function (promiseFactory) { - if (this._closePromise) { - return this._closePromise; - } - var self = this; - return this._closePromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { - self._ws.on('close', function () { - self.isOpen = false; - callback(); + /** + * Closes the Connection. + * @return {Promise} + */ + close(promiseFactory) { + if (this._closePromise) { + return this._closePromise; + } + const self = this; + return this._closePromise = utils.toPromise(promiseFactory, function promiseHandler(callback) { + self._ws.on('close', function () { + self.isOpen = false; + callback(); + }); + self._ws.close(); }); - self._ws.close(); - }); -}; + } +} function getUuid() { - var buffer = crypto.randomBytes(16); + const buffer = crypto.randomBytes(16); //clear the version buffer[6] &= 0x0f; //set the version 4 @@ -189,7 +188,7 @@ function getUuid() { buffer[8] &= 0x3f; //set the IETF variant buffer[8] |= 0x80; - var hex = buffer.toString('hex'); + const hex = buffer.toString('hex'); return ( hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87e02146/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js index 235375a..37f3d0e 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/driver/remote-connection.js @@ -22,60 +22,54 @@ */ 'use strict'; -var t = require('../process/traversal'); -var TraversalStrategy = require('../process/traversal-strategy').TraversalStrategy; -var utils = require('../utils'); -var inherits = utils.inherits; +const t = require('../process/traversal'); +const TraversalStrategy = require('../process/traversal-strategy').TraversalStrategy; -function RemoteConnection(url) { - this.url = url; -} - -/** - * @abstract - * @param {Bytecode} bytecode - * @param {Function|undefined} promiseFactory - * @returns {Promise} - */ -RemoteConnection.prototype.submit = function (bytecode, promiseFactory) { - throw new Error('submit() was not implemented'); -}; +class RemoteConnection { + constructor(url) { + this.url = url; + } -/** - * @extends {Traversal} - * @constructor - */ -function RemoteTraversal(traversers, sideEffects) { - t.Traversal.call(this, null, null, null); - this.traversers = traversers; - this.sideEffects = sideEffects; + /** + * @abstract + * @param {Bytecode} bytecode + * @param {Function|undefined} promiseFactory + * @returns {Promise} + */ + submit(bytecode, promiseFactory) { + throw new Error('submit() was not implemented'); + }; } -inherits(RemoteTraversal, t.Traversal); - -/** - * - * @param {RemoteConnection} connection - * @extends {TraversalStrategy} - * @constructor - */ -function RemoteStrategy(connection) { - TraversalStrategy.call(this); - this.connection = connection; +class RemoteTraversal extends t.Traversal { + constructor(traversers, sideEffects) { + super(null, null, null); + this.traversers = traversers; + this.sideEffects = sideEffects; + } } -inherits(RemoteStrategy, TraversalStrategy); +class RemoteStrategy extends TraversalStrategy { + /** + * Creates a new instance of RemoteStrategy. + * @param {RemoteConnection} connection + */ + constructor(connection) { + super(); + this.connection = connection; + } -/** @override */ -RemoteStrategy.prototype.apply = function (traversal, promiseFactory) { - if (traversal.traversers) { - return utils.resolvedPromise(promiseFactory); + /** @override */ + apply(traversal, promiseFactory) { + if (traversal.traversers) { + return utils.resolvedPromise(promiseFactory); + } + return this.connection.submit(traversal.getBytecode(), promiseFactory).then(function (remoteTraversal) { + traversal.sideEffects = remoteTraversal.sideEffects; + traversal.traversers = remoteTraversal.traversers; + }); } - return this.connection.submit(traversal.getBytecode(), promiseFactory).then(function (remoteTraversal) { - traversal.sideEffects = remoteTraversal.sideEffects; - traversal.traversers = remoteTraversal.traversers; - }); -}; +} module.exports = { RemoteConnection: RemoteConnection, http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/87e02146/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/bytecode.js ---------------------------------------------------------------------- diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/bytecode.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/bytecode.js index 2909718..85a4fc2 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/bytecode.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/bytecode.js @@ -22,78 +22,77 @@ */ 'use strict'; -/** - * Creates a new instance of Bytecode - * @param {Bytecode} [toClone] - * @constructor - */ -function Bytecode(toClone) { - if (!toClone) { - this.sourceInstructions = []; - this.stepInstructions = []; - } - else { - this.sourceInstructions = toClone.sourceInstructions.slice(0); - this.stepInstructions = toClone.stepInstructions.slice(0); +class Bytecode { + /** + * Creates a new instance of Bytecode + * @param {Bytecode} [toClone] + */ + constructor(toClone) { + if (!toClone) { + this.sourceInstructions = []; + this.stepInstructions = []; + } + else { + this.sourceInstructions = toClone.sourceInstructions.slice(0); + this.stepInstructions = toClone.stepInstructions.slice(0); + } } -} -/** - * Adds a new source instructions - * @param {String} name - * @param {Array} values - * @returns {Bytecode} - */ -Bytecode.prototype.addSource = function (name, values) { - if (name === undefined) { - throw new Error('Name is not defined'); + /** + * Adds a new source instructions + * @param {String} name + * @param {Array} values + * @returns {Bytecode} + */ + addSource(name, values) { + if (name === undefined) { + throw new Error('Name is not defined'); + } + const instruction = new Array(values.length + 1); + instruction[0] = name; + for (let i = 0; i < values.length; ++i) { + instruction[i + 1] = values[i]; + } + this.sourceInstructions.push(Bytecode._generateInstruction(name, values)); + return this; } - var instruction = new Array(values.length + 1); - instruction[0] = name; - for (var i = 0; i < values.length; ++i) { - instruction[i + 1] = this._convertToArgument(values[i]); + + /** + * Adds a new step instructions + * @param {String} name + * @param {Array} values + * @returns {Bytecode} + */ + addStep(name, values) { + if (name === undefined) { + throw new Error('Name is not defined'); + } + this.stepInstructions.push(Bytecode._generateInstruction(name, values)); + return this; } - this.sourceInstructions.push(this._generateInstruction(name, values)); - return this; -}; -/** - * Adds a new step instructions - * @param {String} name - * @param {Array} values - * @returns {Bytecode} - */ -Bytecode.prototype.addStep = function (name, values) { - if (name === undefined) { - throw new Error('Name is not defined'); + static _generateInstruction(name, values) { + const length = (values ? values.length : 0) + 1; + const instruction = new Array(length); + instruction[0] = name; + for (let i = 1; i < length; i++) { + instruction[i] = values[i - 1]; + } + return instruction; } - this.stepInstructions.push(this._generateInstruction(name, values)); - return this; -}; -Bytecode.prototype._generateInstruction = function (name, values) { - var length = (values ? values.length : 0) + 1; - var instruction = new Array(length); - instruction[0] = name; - for (var i = 1; i < length; i++) { - instruction[i] = this._convertToArgument(values[i - 1]); + /** + * Returns the JSON representation of the source and step instructions + * @returns {String} + */ + toString() { + return ( + (this.sourceInstructions.length > 0 ? JSON.stringify(this.sourceInstructions) : '') + + (this.stepInstructions.length > 0 ? JSON.stringify(this.stepInstructions) : '') + ); } - return instruction; -}; +} -/** - * Returns the JSON representation of the source and step instructions - * @returns {String} - */ -Bytecode.prototype.toString = function () { - return ( - (this.sourceInstructions.length > 0 ? JSON.stringify(this.sourceInstructions) : '') + - (this.stepInstructions.length > 0 ? JSON.stringify(this.stepInstructions) : '') - ); -}; -Bytecode.prototype._convertToArgument = function (value) { - return value; -}; module.exports = Bytecode; \ No newline at end of file