/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ import { version } from "../package.json" ; import { isBrowser , isEmpty } from "./utils" ; const uuidv1 = require ( "uuid/v1" ) ; const OPC_REQUEST_ID_HEADER = "opc-request-id" ; const USER_AGENT_HEADER = "User-Agent" ; const BROWSER_USER_AGENT_HEADER = "X-Orcl-User-Agent" ; const PLATFORM_KEY = "platform" ; const RELEASE_KEY = "relase" ; const VERSION_KEY = "nodeVersion" ; let NODE_PLATFORM_HASH = new Map < string , string > ( ) ; export function addOpcRequestId ( headers ) { if ( ! hasOpcRequestId ( headers ) ) { headers . append ( OPC_REQUEST_ID_HEADER , generateRequestId ( ) ) ; } } function hasOpcRequestId ( headers ) { if ( headers . has ( OPC_REQUEST_ID_HEADER ) ) { return true ; } return false ; } function generateRequestId ( ) { return uuidv1 ( ) . replace ( "-" , "" ) . toLocaleUpperCase ( "en-US" ) ; } export function addUserAgent ( headers ) { const appendUserAgent = process . env . OCI_SDK_APPEND_USER_AGENT ; const clientInfo = `Oracle-TypeScriptSDK/${ version }` ; if ( ! isBrowser ( ) ) { if ( isEmpty ( NODE_PLATFORM_HASH ) ) { populatePlatformHash ( ) ; } const platform = NODE_PLATFORM_HASH . get ( PLATFORM_KEY ) ; const release = NODE_PLATFORM_HASH . get ( RELEASE_KEY ) ; const nodeVersion = NODE_PLATFORM_HASH . get ( VERSION_KEY ) ; let userAgent = appendUserAgent ? `${ clientInfo } (${ platform }/${ release }; Node/${ nodeVersion }) ${ appendUserAgent }` : `${ clientInfo } (${ platform }/${ release }; Node/${ nodeVersion })` ; headers . append ( USER_AGENT_HEADER , userAgent ) ; return ; } headers . append ( BROWSER_USER_AGENT_HEADER , clientInfo ) ; } function populatePlatformHash ( ) { const os = require ( "os" ) ; NODE_PLATFORM_HASH . set ( PLATFORM_KEY , os . platform ) ; NODE_PLATFORM_HASH . set ( RELEASE_KEY , os . release ) ; NODE_PLATFORM_HASH . set ( VERSION_KEY , process . version ) ; }