Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE-2590] add client endpoint param #652

Merged
merged 6 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ var values = require('./values')
* @constructor
* @param {?Object} options
* Object that configures this FaunaDB client.
* @param {?string} options.endpoint
* Full URL for the FaunaDB server.
* @param {?string} options.domain
* Base URL for the FaunaDB server.
* @param {?{ string: string }} options.headers
* Base URL for the FaunaDB server.
* @param {?('http'|'https')} options.scheme
* HTTP scheme to use.
* @param {?number} options.port
Expand All @@ -152,14 +152,16 @@ var values = require('./values')
* Callback that will be called after every completed request.
* @param {?boolean} options.keepAlive
* Configures http/https keepAlive option (ignored in browser environments)
* @param {?{ string: string }} options.headers
* Optional headers to send with requests
* @param {?fetch} options.fetch
* a fetch compatible [API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for making a request
* @param {?number} options.queryTimeout
* Sets the maximum amount of time (in milliseconds) for query execution on the server
* @param {?number} options.http2SessionIdleTime
* Sets the maximum amount of time (in milliseconds) an HTTP2 session may live
* when there's no activity. Must be a non-negative integer, with a maximum value of 5000.
* If an invalid value is passed a default of 500 ms is applied. If a value
* If an invalid value is passed a default of 500 ms is applied. If a value
* exceeding 5000 ms is passed (e.g. Infinity) the maximum of 5000 ms is applied.
* Only applicable for NodeJS environment (when http2 module is used).
* can also be configured via the FAUNADB_HTTP2_SESSION_IDLE_TIME environment variable
Expand All @@ -177,6 +179,7 @@ function Client(options) {
if (options) options.http2SessionIdleTime = http2SessionIdleTime

options = util.applyDefaults(options, {
endpoint: null,
domain: 'db.fauna.com',
scheme: 'https',
port: null,
Expand Down Expand Up @@ -300,7 +303,14 @@ Client.prototype.queryWithMetrics = function(expression, options) {
return this._execute('POST', '', query.wrap(expression), null, options, true)
}

Client.prototype._execute = function(method, path, data, query, options, returnMetrics = false) {
Client.prototype._execute = function(
method,
path,
data,
query,
options,
returnMetrics = false
) {
query = util.defaults(query, null)

if (
Expand Down Expand Up @@ -357,10 +367,12 @@ Client.prototype._execute = function(method, path, data, query, options, returnM
if (returnMetrics) {
return {
value: responseObject['resource'],
metrics: Object.fromEntries(Array.from(Object.entries(response.headers)).
filter( ([k,v]) => metricsHeaders.includes(k) ).
map(([ k,v ]) => [k, parseInt(v)])
)}
metrics: Object.fromEntries(
Array.from(Object.entries(response.headers))
.filter(([k, v]) => metricsHeaders.includes(k))
.map(([k, v]) => [k, parseInt(v)])
),
}
} else {
return responseObject['resource']
}
Expand Down Expand Up @@ -394,9 +406,8 @@ function getHttp2SessionIdleTime(configuredIdleTime) {
// attemp to set the idle time to the env value and then the configured value
const values = [envIdleTime, configuredIdleTime]
for (const rawValue of values) {
const parsedValue = rawValue === 'Infinity'
? Number.MAX_SAFE_INTEGER
: parseInt(rawValue, 10)
const parsedValue =
rawValue === 'Infinity' ? Number.MAX_SAFE_INTEGER : parseInt(rawValue, 10)
const isNegative = parsedValue < 0
const isGreaterThanMax = parsedValue > maxIdleTime
// if we didn't get infinity or a positive integer move to the next value
Expand Down
8 changes: 7 additions & 1 deletion src/_http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ function HttpClient(options) {
fetch: options.fetch,
keepAlive: options.keepAlive,
})
this._baseUrl = options.scheme + '://' + options.domain + ':' + options.port

if (options.endpoint === null) {
this._baseUrl = options.scheme + '://' + options.domain + ':' + options.port
} else {
this._baseUrl = options.endpoint
}

this._secret = options.secret
this._headers = Object.assign({}, options.headers, getDefaultHeaders())
this._queryTimeout = options.queryTimeout
Expand Down
1 change: 1 addition & 0 deletions src/types/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type StreamEventFields = 'action' | 'document' | 'diff' | 'prev' | 'index'

export interface ClientConfig {
secret: string
endpoint?: string
domain?: string
scheme?: 'http' | 'https'
port?: number
Expand Down
1 change: 1 addition & 0 deletions test/afterComplete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config()
const query = require('../src/query')
const Client = require('../src/Client')
const util = require('../src/_util')
Expand Down
18 changes: 13 additions & 5 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ describe('Client', () => {
})
})

test('ping with client configured with "endpoint"', () => {
var client = util.getClientFromEndpoint()
return client.ping('node').then(function(res) {
expect(res).toEqual('Scope node is OK')
})
})

test("omits the port value if it's falsy", () => {
const client = new Client({
secret: 'FAKED',
Expand All @@ -47,7 +54,9 @@ describe('Client', () => {
})

test('the client does not support a metrics flag', async () => {
expect(() => util.getClient({ metrics: true })).toThrow(new Error('No such option metrics'))
expect(() => util.getClient({ metrics: true })).toThrow(
new Error('No such option metrics')
)
})

test('query does not support a metrics flag', async () => {
Expand All @@ -67,8 +76,7 @@ describe('Client', () => {

test('queryWithMetrics returns the metrics', async () => {
const response = await client.queryWithMetrics(query.Add(1, 1))
expect(Object.keys(response).sort()).
toEqual(['metrics', 'value'])
expect(Object.keys(response).sort()).toEqual(['metrics', 'value'])
})

test('paginates', () => {
Expand Down Expand Up @@ -454,7 +462,7 @@ describe('Client', () => {

process.env.FAUNADB_HTTP2_SESSION_IDLE_TIME = 'Cat'
client = util.getClient({
http2SessionIdleTime: "Cat",
http2SessionIdleTime: 'Cat',
})
internalIdleTime = client._http._adapter._http2SessionIdleTime
expect(internalIdleTime).toBe(defaultIdleTime)
Expand All @@ -475,7 +483,7 @@ describe('Client', () => {

process.env.FAUNADB_HTTP2_SESSION_IDLE_TIME = '-999'
client = util.getClient({
http2SessionIdleTime: "Infinity",
http2SessionIdleTime: 'Infinity',
})
internalIdleTime = client._http._adapter._http2SessionIdleTime
expect(internalIdleTime).toBe(maxIdleTime)
Expand Down
19 changes: 19 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ function getClient(opts) {
return new Client(objectAssign({ secret: clientSecret }, getCfg(), opts))
}

function getClientFromEndpoint(opts) {
var config = getCfg()
var endpoint = config.scheme + '://' + config.domain + ':' + config.port

return new Client(
objectAssign(
{
secret: clientSecret,
endpoint: endpoint,
scheme: 'bad scheme',
domain: 'bad domain',
port: 'bad port',
},
opts
)
)
}

function assertRejected(promise, errorType) {
var succeeded = false

Expand Down Expand Up @@ -154,6 +172,7 @@ module.exports = {
testConfig: testConfig,
getCfg: getCfg,
getClient: getClient,
getClientFromEndpoint: getClientFromEndpoint,
assertRejected: assertRejected,
client: client,
clientSecret: clientSecret,
Expand Down