Skip to content

Commit

Permalink
Add support for tinkerpop gremlin server 3.3 (jbmusso#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctdio authored and jbmusso committed Jan 31, 2018
1 parent e9c4594 commit 137c104
Show file tree
Hide file tree
Showing 10 changed files with 610 additions and 512 deletions.
9 changes: 6 additions & 3 deletions gremlin-client/src/GremlinClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class GremlinClient extends EventEmitter {
rawScript,
rawBindings,
);

const { processor, op, accept, language, aliases } = this.options;

const baseArgs = { gremlin, bindings, accept, language, aliases };
Expand All @@ -242,8 +243,10 @@ class GremlinClient extends EventEmitter {

buildChallengeResponse(requestId) {
const { processor, op, accept, language, aliases } = this.options;
var saslbase64 = new Buffer('\0' + this.user + '\0' + this.password).toString('base64');
var args = { sasl: saslbase64 }
var saslbase64 = new Buffer(
'\0' + this.user + '\0' + this.password,
).toString('base64');
var args = { sasl: saslbase64 };

const message = {
requestId: requestId,
Expand Down Expand Up @@ -324,7 +327,7 @@ class GremlinClient extends EventEmitter {
// Create a local highland 'through' pipeline so we don't expose
// a Highland stream to the end user, but a standard Node.js Stream2
const through = _.pipeline(
_.map(({ result: { data } }) => data),
_.map(({ result: { data } }) => data['@value'] || data),
_.sequence(),
);

Expand Down
4 changes: 2 additions & 2 deletions gremlin-client/src/WebSocketGremlinConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export default class WebSocketGremlinConnection extends EventEmitter {
handleMessage(message) {
this.emit('message', message);
}

close() {
this.ws.terminate();
}

onClose(event) {
this.open = false;
this.emit('close', event);
Expand Down
9 changes: 4 additions & 5 deletions gremlin-client/src/bindings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ describe('Bindings', function() {
var stream = client.stream('g.V(x)', { x: 1 });

stream.on('data', function(result) {
result.id.should.equal(1);
const id = result.id || result['@value'].id['@value'];
id.should.equal(1);
});

stream.on('end', function() {
done();
});
});

it.skip('should give an error with reserved binding name in .exec', function(
done,
) {
it.skip('should give an error with reserved binding name in .exec', function(done) {
var client = gremlin.createClient();

// This is supposed to throw a NoSuchElementException in Gremlin Server:
Expand Down Expand Up @@ -58,7 +57,7 @@ describe('Bindings', function() {
client.execute('[foo, bar]', { foo: '', bar: 0 }, (err, [foo, bar]) => {
(err === null).should.be.true;
(foo === '').should.be.true;
(bar === 0).should.be.true;
(bar['@value'] === 0 || bar === 0).should.be.true;
done();
});
});
Expand Down
4 changes: 1 addition & 3 deletions gremlin-client/src/createClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ describe('.createClient()', function() {
client.options.aliases.should.eql({ h: 'g' });
});

it('should override a set `processor` option on a per request basis', function(
done,
) {
it('should override a set `processor` option on a per request basis', function(done) {
var client = gremlin.createClient({ op: 'foo' });

client.port.should.equal(8182);
Expand Down
21 changes: 14 additions & 7 deletions gremlin-client/src/execute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require('chai').should();
import { assert } from 'chai';
import gremlin, { statics } from './';

import { get } from 'lodash';

describe('.execute()', function() {
it('should return a result and a response', function(done) {
var client = gremlin.createClient();
Expand Down Expand Up @@ -75,7 +77,8 @@ describe('.execute()', function() {
client.execute(query, (err, result) => {
(err === null).should.be.true;
result.length.should.equal(1);
result[0].id.should.equal(1);
const id = get(result[0], '["@value"].id["@value"]') || result[0].id;
id.should.equal(1);
done();
});
});
Expand All @@ -92,8 +95,10 @@ describe('.execute()', function() {
client.execute(query, { second: 2 }, (err, result) => {
(err === null).should.be.true;
result.length.should.equal(2);
result[0].id.should.equal(1);
result[1].id.should.equal(2);
const id1 = get(result[0], '["@value"].id["@value"]') || result[0].id;
const id2 = get(result[1], '["@value"].id["@value"]') || result[1].id;
id1.should.equal(1);
id2.should.equal(2);
done();
});
});
Expand All @@ -109,9 +114,7 @@ describe('.execute()', function() {
});
});

it('should fire the callback with an empty array when handling a 204 NO_CONTENT code', function(
done,
) {
it('should fire the callback with an empty array when handling a 204 NO_CONTENT code', function(done) {
// @see https://github.com/jbmusso/gremlin-javascript/issues/17
var client = gremlin.createClient();
var script = 'g.V().limit(0)';
Expand Down Expand Up @@ -197,7 +200,11 @@ describe('.execute()', function() {
const g = client.traversalSource();
const { both } = statics;

const results = await g.V().repeat(both('created')).times(2).toPromise();
const results = await g
.V()
.repeat(both('created'))
.times(2)
.toPromise();
assert.equal(results.length, 16);
});
});
2 changes: 1 addition & 1 deletion gremlin-client/src/executeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import highland from 'highland';

const defaultExecuteHandler = (messageStream, callback) =>
highland(messageStream)
.map(({ result: { data } }) => data)
.map(({ result: { data } }) => data['@value'] || data)
.sequence()
.collect()
.toCallback(callback);
Expand Down
9 changes: 6 additions & 3 deletions gremlin-client/src/makeTemplateTag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require('chai').should();
import { createClient, makeTemplateTag } from './';
import { assert } from 'chai';

import { get } from 'lodash';

const client = createClient();
const gremlin = makeTemplateTag(client);

Expand All @@ -19,7 +21,7 @@ describe('Template tag', () => {
const [vertex] = vertices;

assert.lengthOf(vertices, 1);
assert.equal(vertex.id, id);
assert.equal(get(vertex, '["@value"].id["@value"]') || vertex.id, id);

done();
});
Expand All @@ -31,8 +33,9 @@ describe('Template tag', () => {
const [v1, v2] = vertices;

assert.lengthOf(vertices, 2);
assert.equal(v1.id, ids[0]);
assert.equal(v2.id, ids[1]);

assert.equal(get(v1, '["@value"].id["@value"]') || v1.id, ids[0]);
assert.equal(get(v2, '["@value"].id["@value"]') || v2.id, ids[1]);

done();
});
Expand Down
10 changes: 9 additions & 1 deletion gremlin-client/src/messageStream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ describe('.messageStream', function() {

s.on('data', function(message) {
message.status.code.should.be.within(200, 206);
message.result.data.should.be.an('array');
const { data } = message.result;

if (data['@type']) {
// tinkerpop 3.3
data['@value'].should.be.an('array');
} else {
// tinkerpop 3.2
data.should.be.an('array');
}
});

s.on('end', function() {
Expand Down
11 changes: 6 additions & 5 deletions gremlin-client/src/stream.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require('chai').should();
import gremlin from './';
import { get } from 'lodash';

describe('.stream()', function() {
it('should emit `data` events with a chunk of results and the raw response', function(
done,
) {
it('should emit `data` events with a chunk of results and the raw response', function(done) {
var client = gremlin.createClient();
var s = client.stream('g.V()');

Expand All @@ -25,7 +24,8 @@ describe('.stream()', function() {
var s = client.stream('g.V(x)', { x: 1 });

s.on('data', function(result) {
result.id.should.equal(1);
const id = get(result, '["@value"].id["@value"]') || result.id;
id.should.equal(1);
});

s.on('end', function() {
Expand All @@ -40,7 +40,8 @@ describe('.stream()', function() {
});

s.on('data', function(result) {
result.id.should.equal(1);
const id = get(result, '["@value"].id["@value"]') || result.id;
id.should.equal(1);
});

s.on('end', function() {
Expand Down
Loading

0 comments on commit 137c104

Please sign in to comment.