Skip to content

Commit

Permalink
feature: rework driver env header (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
fireridlle authored Mar 15, 2021
1 parent 8e82dce commit 3f07a6f
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 104 deletions.
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"abort-controller": "^3.0.0",
"base64-js": "^1.2.0",
"browser-detect": "^0.2.28",
"btoa-lite": "^1.0.0",
"cross-fetch": "^3.0.6",
"dotenv": "^8.2.0",
Expand Down
107 changes: 20 additions & 87 deletions src/_http/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
var packageJson = require('../../package.json')
const { getBrowserOsDetails } = require('../_util')
var util = require('../_util')
var errors = require('./errors')

Expand Down Expand Up @@ -118,25 +119,31 @@ function secretHeader(secret) {

/** @ignore */
function getDefaultHeaders() {
var headers = {
'X-Fauna-Driver': 'Javascript',
'X-FaunaDB-API-Version': packageJson.apiVersion,
var driverEnv = {
driver: ['javascript', packageJson.version].join('-'),
}

if (util.isNodeEnv()) {
// TODO: should be at the browser as well. waiting to enable CORS headers
headers['X-Fauna-Driver-Version'] = packageJson.version
headers['X-Runtime-Environment'] = getNodeRuntimeEnv()
headers['X-Runtime-Environment-OS'] = require('os').platform()
headers['X-NodeJS-Version'] = process.version
driverEnv.runtime = ['nodejs', process.version].join('-')
driverEnv.env = util.getNodeRuntimeEnv()
var os = require('os')
driverEnv.os = [os.platform(), os.release()].join('-')
} else {
// TODO: uncomment when CORS enabled
// var browser = require('browser-detect')()
// headers['X-Runtime-Environment'] = browser.name
// headers['X-Runtime-Environment-Version'] = browser.version
// headers['X-Runtime-Environment-OS'] = browser.os
driverEnv.runtime = util.getBrowserDetails()
driverEnv.env = 'unknown'
driverEnv.os = getBrowserOsDetails()
}

var headers = {
'X-FaunaDB-API-Version': packageJson.apiVersion,
}

// TODO: api cors must be enabled to accept header X-Driver-Env
if (util.isNodeEnv()) {
headers['X-Driver-Env'] = Object.keys(driverEnv)
.map(key => [key, driverEnv[key].toLowerCase()].join('='))
.join('; ')
}
return headers
}

Expand All @@ -150,80 +157,6 @@ function isHttp2Supported() {
}
}

/**
* For checking process.env always use `hasOwnProperty`
* Some providers could throw an error when trying to access env variables that does not exists
* @ignore */
function getNodeRuntimeEnv() {
var runtimeEnvs = [
{
name: 'Netlify',
check: () => process.env.hasOwnProperty('NETLIFY_IMAGES_CDN_DOMAIN'),
},
{
name: 'Vercel',
check: () => process.env.hasOwnProperty('VERCEL'),
},
{
name: 'Heroku',
check: () =>
process.env.hasOwnProperty('PATH') &&
process.env.PATH.indexOf('.heroku') !== -1,
},
{
name: 'AWS Lambda',
check: () => process.env.hasOwnProperty('AWS_LAMBDA_FUNCTION_VERSION'),
},
{
name: 'GCP Cloud Functions',
check: () =>
process.env.hasOwnProperty('_') &&
process.env._.indexOf('google') !== -1,
},
{
name: 'GCP Compute Instances',
check: () => process.env.hasOwnProperty('GOOGLE_CLOUD_PROJECT'),
},
{
name: 'Azure Cloud Functions',
check: () =>
process.env.hasOwnProperty('WEBSITE_FUNCTIONS_AZUREMONITOR_CATEGORIES'),
},
{
name: 'Azure Compute',
check: () =>
process.env.hasOwnProperty('ORYX_ENV_TYPE') &&
process.env.hasOwnProperty('WEBSITE_INSTANCE_ID') &&
process.env.ORYX_ENV_TYPE === 'AppService',
},
{
name: 'Worker',
check: () => {
try {
return global instanceof ServiceWorkerGlobalScope
} catch (error) {
return false
}
},
},
{
name: 'Mongo Stitch',
check: () => typeof global.StitchError === 'function',
},
{
name: 'Render',
check: () => process.env.hasOwnProperty('RENDER_SERVICE_ID'),
},
{
name: 'Begin',
check: () => process.env.hasOwnProperty('BEGIN_DATA_SCOPE_ID'),
},
]
var detectedEnv = runtimeEnvs.find(env => env.check())

return detectedEnv ? detectedEnv.name : 'Unknown'
}

module.exports = {
HttpClient: HttpClient,
TimeoutError: errors.TimeoutError,
Expand Down
Loading

0 comments on commit 3f07a6f

Please sign in to comment.