-
Notifications
You must be signed in to change notification settings - Fork 76
/
Client.js
227 lines (197 loc) · 6.7 KB
/
Client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use strict';
var APIVersion = "2.7"
var btoa = require('btoa-lite');
var request = require('superagent');
var errors = require('./errors');
var query = require('./query');
var values = require('./values');
var json = require('./_json');
var RequestResult = require('./RequestResult');
var util = require('./_util');
var PageHelper = require('./PageHelper');
var Promise = require('es6-promise').Promise;
/**
* The callback that will be executed after every completed request.
*
* @callback Client~observerCallback
* @param {RequestResult} res
*/
/**
* A client for interacting with FaunaDB.
*
* Users will mainly call the {@link Client#query} method to execute queries.
*
* See the [FaunaDB Documentation](https://fauna.com/documentation) for detailed examples.
*
* All methods return promises containing a JSON object that represents the FaunaDB response.
* Literal types in the response object will remain as strings, Arrays, and objects.
* FaunaDB types, such as {@link Ref}, {@link SetRef}, {@link FaunaTime}, and {@link FaunaDate} will
* be converted into the appropriate object.
*
* (So if a response contains `{ "@ref": "collections/frogs/123" }`,
* it will be returned as `new Ref("collections/frogs/123")`.)
*
* @constructor
* @param {?Object} options
* Object that configures this FaunaDB client.
* @param {?string} options.domain
* Base URL for the FaunaDB server.
* @param {?('http'|'https')} options.scheme
* HTTP scheme to use.
* @param {?number} options.port
* Port of the FaunaDB server.
* @param {?string} options.secret FaunaDB secret (see [Reference Documentation](https://app.fauna.com/documentation/intro/security))
* @param {?number} options.timeout Read timeout in seconds.
* @param {?Client~observerCallback} options.observer
* Callback that will be called after every completed request.
*/
function Client(options) {
var isNodeEnv = typeof window === 'undefined';
var opts = util.applyDefaults(options, {
domain: 'db.fauna.com',
scheme: 'https',
port: null,
secret: null,
timeout: 60,
observer: null
});
if (opts.port === null) {
opts.port = opts.scheme === 'https' ? 443 : 80;
}
this._baseUrl = opts.scheme + '://' + opts.domain + ':' + opts.port;
this._timeout = Math.floor(opts.timeout * 1000);
this._secret = opts.secret;
this._observer = opts.observer;
this._lastSeen = null;
this._keepAlive = isNodeEnv
? new require(opts.scheme).Agent({ keepAlive: true })
: undefined
}
/**
* Executes a query via the FaunaDB Query API.
* See the [docs](https://app.fauna.com/documentation/reference/queryapi),
* and the query functions in this documentation.
* @param expression {Expr}
* The query to execute. Created from query functions such as {@link add}.
* @return {external:Promise<Object>} FaunaDB response object.
*/
Client.prototype.query = function (expression) {
return this._execute('POST', '', query.wrap(expression));
};
/**
* Returns a {@link PageHelper} for the given Query expression.
* This provides a helpful API for paginating over FaunaDB responses.
* @param expression {Expr}
* The Query expression to paginate over.
* @param params {Object}
* Options to be passed to the paginate function. See [paginate](https://app.fauna.com/documentation/reference/queryapi#read-functions).
* @returns {PageHelper} A PageHelper that wraps the provided expression.
*/
Client.prototype.paginate = function(expression, params) {
params = defaults(params, {});
return new PageHelper(this, expression, params);
};
/**
* Sends a `ping` request to FaunaDB.
* @return {external:Promise<string>} Ping response.
*/
Client.prototype.ping = function (scope, timeout) {
return this._execute('GET', 'ping', null, { scope: scope, timeout: timeout });
};
/**
* Get the freshest timestamp reported to this client.
* @returns {number} the last seen transaction time
*/
Client.prototype.getLastTxnTime = function() {
return this._lastSeen;
};
/**
* Sync the freshest timestamp seen by this client.
*
* This has no effect if staler than currently stored timestamp.
* WARNING: This should be used only when coordinating timestamps across
* multiple clients. Moving the timestamp arbitrarily forward into
* the future will cause transactions to stall.
* @param time {number} the last seen transaction time
*/
Client.prototype.syncLastTxnTime = function(time) {
if (this._lastSeen == null) {
this._lastSeen = time;
} else if (this._lastSeen < time) {
this._lastSeen = time;
}
};
Client.prototype._execute = function (action, path, data, query) {
query = defaults(query, null);
if (path instanceof values.Ref) {
path = path.value;
}
if (query !== null) {
query = util.removeUndefinedValues(query);
}
var startTime = Date.now();
var self = this;
return this._performRequest(action, path, data, query).then(function (response, rawQuery) {
var endTime = Date.now();
var responseObject = json.parseJSON(response.text);
var requestResult = new RequestResult(
self,
action, path, query, rawQuery, data,
response.text, responseObject, response.status, response.header,
startTime, endTime);
if ('x-txn-time' in response.header) {
var time = parseInt(response.header['x-txn-time'], 10);
self.syncLastTxnTime(time);
}
if (self._observer != null) {
self._observer(requestResult);
}
errors.FaunaHTTPError.raiseForStatusCode(requestResult);
return responseObject['resource'];
});
};
Client.prototype._performRequest = function (action, path, data, query) {
var rq = request(action, this._baseUrl + '/' + path);
if (query) {
rq.query(query);
}
var rawQuery = JSON.stringify(data);
rq.type('json');
rq.send(rawQuery);
if (this._secret) {
rq.set('Authorization', secretHeader(this._secret));
}
if (this._lastSeen) {
rq.set('X-Last-Seen-Txn', this._lastSeen);
}
if(this._keepAlive) {
rq.agent(this._keepAlive);
}
rq.set('X-FaunaDB-API-Version', APIVersion);
rq.timeout(this._timeout);
return new Promise(function (resolve, reject) {
rq.end(function (error, result) {
// superagent treates 4xx and 5xx status codes as exceptions. We'll handle those ourselves.
if (error && error.response === undefined) {
reject(error);
} else if (error &&
error.response &&
!(error.response.status >= 400 && error.response.status <= 599)) {
reject(error);
} else {
resolve(result, rawQuery);
}
});
});
};
function defaults(obj, def) {
if (obj === undefined) {
return def;
} else {
return obj;
}
}
function secretHeader(secret) {
return 'Basic ' + btoa(secret + ':');
}
module.exports = Client;