neptune-lambda-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/index.js +108 -0
  4. package/package.json +31 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Stefano Vozza
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # neptune-lambda-client
2
+
3
+ ## Overview
4
+ A very simple Gremlin client to robustly query AWS Neptune from AWS Lambda. The client will automatically
5
+ reestablish a connection to the database if the web socket connection closes and will also automatically
6
+ retry (5 times) when it encounters `ConcurrentModificationException` and `ReadOnlyViolationException` errors.
7
+
8
+ ## Usage
9
+
10
+ This client is instantiated with a factory function and exposes a single function called `query`. `query` accepts
11
+ a single argument, which is a function that use the Gremlin `g` object.
12
+
13
+ ```js
14
+ const gremlinClient = require('neptune-lambda-client');
15
+
16
+ const {query} = gremlinClient.create({
17
+ host: 'neptune-db-url',
18
+ port: '8182',
19
+ useIam: true
20
+ });
21
+
22
+ async function getNode(id) {
23
+ return query(async g => g.V(id).next().then(x => x.value));
24
+ }
25
+ ```
package/index.js ADDED
@@ -0,0 +1,108 @@
1
+ const aws4 = require('aws4');
2
+ const gremlin = require('gremlin');
3
+ const retry = require('async-retry');
4
+
5
+ const traversal = gremlin.process.AnonymousTraversalSource.traversal;
6
+ const {driver: {DriverRemoteConnection}} = gremlin;
7
+
8
+ function createHeaders(host, port, path, options) {
9
+ if (!host || !port) {
10
+ throw new Error('Host and port are required');
11
+ }
12
+
13
+ const accessKeyId = options.accessKey || process.env.AWS_ACCESS_KEY_ID;
14
+ const secretAccessKey = options.secretKey || process.env.AWS_SECRET_ACCESS_KEY;
15
+ const sessionToken = options.sessionToken || process.env.AWS_SESSION_TOKEN;
16
+ const region = options.region || process.env.AWS_REGION;
17
+
18
+ if (!accessKeyId || !secretAccessKey || !region) {
19
+ throw new Error(
20
+ 'AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION are required'
21
+ );
22
+ }
23
+
24
+ const sigOptions = {
25
+ host: `${host}:${port}`,
26
+ region,
27
+ path,
28
+ service: 'neptune-db',
29
+ };
30
+
31
+ return aws4.sign(sigOptions, {
32
+ accessKeyId, secretAccessKey, sessionToken,
33
+ }).headers;
34
+ }
35
+
36
+ module.exports = {
37
+ create: function(host, port, {useIam = true} = {useIam: true}) {
38
+ let conn = null;
39
+ let g = null;
40
+
41
+ const path = "/gremlin"
42
+ const url = `wss://${host}:${port}${path}`
43
+
44
+ const createRemoteConnection = () => {
45
+
46
+ const c = new DriverRemoteConnection(
47
+ url,
48
+ {
49
+ mimeType: 'application/vnd.gremlin-v2.0+json',
50
+ pingEnabled: false,
51
+ headers: useIam ? createHeaders(host, port, path, {}) : {}
52
+ });
53
+
54
+ c._client._connection.on('log', message => {
55
+ console.info(`connection message - ${message}`);
56
+ });
57
+
58
+ c._client._connection.on('close', (code, message) => {
59
+ console.info(`close - ${code} ${message}`);
60
+ if (code == 1006){
61
+ console.error('Connection closed prematurely');
62
+ throw new Error('Connection closed prematurely');
63
+ }
64
+ });
65
+
66
+ return c;
67
+ };
68
+
69
+ const createGraphTraversalSource = conn => {
70
+ return traversal().withRemote(conn);
71
+ };
72
+
73
+ return {
74
+ query: async f => {
75
+ if (conn == null){
76
+ console.info('Initializing connection')
77
+ conn = createRemoteConnection();
78
+ g = createGraphTraversalSource(conn);
79
+ }
80
+
81
+ return retry(async (bail, count) => {
82
+ return f(g).catch(err => {
83
+ if(count > 0) console.log('Retry attempt no: ' + count);
84
+ if (err.message.startsWith('WebSocket is not open')){
85
+ console.warn('Reopening connection');
86
+ conn.close();
87
+ conn = createRemoteConnection();
88
+ g = createGraphTraversalSource(conn);
89
+ throw err;
90
+ } else if (err.message.includes('ConcurrentModificationException')){
91
+ console.warn('Retrying query because of ConcurrentModificationException');
92
+ throw err;
93
+ } else if (err.message.includes('ReadOnlyViolationException')){
94
+ console.warn('Retrying query because of ReadOnlyViolationException');
95
+ throw err;
96
+ } else {
97
+ console.warn('Unrecoverable error: ' + err);
98
+ return bail(err);
99
+ }
100
+ })
101
+ }, {
102
+ factor: 1,
103
+ retries: 5
104
+ });
105
+ }
106
+ }
107
+ }
108
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "neptune-lambda-client",
3
+ "version": "1.0.0",
4
+ "description": "Gremlin client to robustly query AWS Neptune from AWS Lambda",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "mocha"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/svozza/neptune-lambda-client.git"
12
+ },
13
+ "keywords": [
14
+ "aws",
15
+ "neptune",
16
+ "gremlin",
17
+ "graphdb",
18
+ "lambda"
19
+ ],
20
+ "author": "Stefano Vozza",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/svozza/neptune-lambda-client/issues"
24
+ },
25
+ "homepage": "https://github.com/svozza/neptune-lambda-client#readme",
26
+ "dependencies": {
27
+ "async-retry": "1.3.3",
28
+ "aws4": "1.11.0",
29
+ "gremlin": "3.5.2"
30
+ }
31
+ }