|
| 1 | +import * as os from 'os'; |
| 2 | +import * as process from 'process'; |
| 3 | + |
| 4 | +import { BSON, Int32 } from '../../bson'; |
| 5 | +import { MongoInvalidArgumentError } from '../../error'; |
| 6 | +import type { MongoOptions } from '../../mongo_client'; |
| 7 | + |
| 8 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 9 | +const NODE_DRIVER_VERSION = require('../../../package.json').version; |
| 10 | + |
| 11 | +/** |
| 12 | + * @public |
| 13 | + * @see https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst#hello-command |
| 14 | + */ |
| 15 | +export interface ClientMetadata { |
| 16 | + driver: { |
| 17 | + name: string; |
| 18 | + version: string; |
| 19 | + }; |
| 20 | + os: { |
| 21 | + type: string; |
| 22 | + name?: NodeJS.Platform; |
| 23 | + architecture?: string; |
| 24 | + version?: string; |
| 25 | + }; |
| 26 | + platform: string; |
| 27 | + application?: { |
| 28 | + name: string; |
| 29 | + }; |
| 30 | + /** FaaS environment information */ |
| 31 | + env?: { |
| 32 | + name: 'aws.lambda' | 'gcp.func' | 'azure.func' | 'vercel'; |
| 33 | + timeout_sec?: Int32; |
| 34 | + memory_mb?: Int32; |
| 35 | + region?: string; |
| 36 | + url?: string; |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** @public */ |
| 41 | +export interface ClientMetadataOptions { |
| 42 | + driverInfo?: { |
| 43 | + name?: string; |
| 44 | + version?: string; |
| 45 | + platform?: string; |
| 46 | + }; |
| 47 | + appName?: string; |
| 48 | +} |
| 49 | + |
| 50 | +/** @internal */ |
| 51 | +export class LimitedSizeDocument { |
| 52 | + private document = new Map(); |
| 53 | + /** BSON overhead: Int32 + Null byte */ |
| 54 | + private documentSize = 5; |
| 55 | + constructor(private maxSize: number) {} |
| 56 | + |
| 57 | + /** Only adds key/value if the bsonByteLength is less than MAX_SIZE */ |
| 58 | + public ifItFitsItSits(key: string, value: Record<string, any> | string): boolean { |
| 59 | + // The BSON byteLength of the new element is the same as serializing it to its own document |
| 60 | + // subtracting the document size int32 and the null terminator. |
| 61 | + const newElementSize = BSON.serialize(new Map().set(key, value)).byteLength - 5; |
| 62 | + |
| 63 | + if (newElementSize + this.documentSize > this.maxSize) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + this.documentSize += newElementSize; |
| 68 | + |
| 69 | + this.document.set(key, value); |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + toObject(): ClientMetadata { |
| 75 | + return BSON.deserialize(BSON.serialize(this.document), { |
| 76 | + promoteLongs: false, |
| 77 | + promoteBuffers: false, |
| 78 | + promoteValues: false, |
| 79 | + useBigInt64: false |
| 80 | + }) as ClientMetadata; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type MakeClientMetadataOptions = Pick<MongoOptions, 'appName' | 'driverInfo'>; |
| 85 | +/** |
| 86 | + * From the specs: |
| 87 | + * Implementors SHOULD cumulatively update fields in the following order until the document is under the size limit: |
| 88 | + * 1. Omit fields from `env` except `env.name`. |
| 89 | + * 2. Omit fields from `os` except `os.type`. |
| 90 | + * 3. Omit the `env` document entirely. |
| 91 | + * 4. Truncate `platform`. -- special we do not truncate this field |
| 92 | + */ |
| 93 | +export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMetadata { |
| 94 | + const metadataDocument = new LimitedSizeDocument(512); |
| 95 | + |
| 96 | + const { appName = '' } = options; |
| 97 | + // Add app name first, it must be sent |
| 98 | + if (appName.length > 0) { |
| 99 | + const name = |
| 100 | + Buffer.byteLength(appName, 'utf8') <= 128 |
| 101 | + ? options.appName |
| 102 | + : Buffer.from(appName, 'utf8').subarray(0, 128).toString('utf8'); |
| 103 | + metadataDocument.ifItFitsItSits('application', { name }); |
| 104 | + } |
| 105 | + |
| 106 | + const { name = '', version = '', platform = '' } = options.driverInfo; |
| 107 | + |
| 108 | + const driverInfo = { |
| 109 | + name: name.length > 0 ? `nodejs|${name}` : 'nodejs', |
| 110 | + version: version.length > 0 ? `${NODE_DRIVER_VERSION}|${version}` : NODE_DRIVER_VERSION |
| 111 | + }; |
| 112 | + |
| 113 | + if (!metadataDocument.ifItFitsItSits('driver', driverInfo)) { |
| 114 | + throw new MongoInvalidArgumentError( |
| 115 | + 'Unable to include driverInfo name and version, metadata cannot exceed 512 bytes' |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + const platformInfo = |
| 120 | + platform.length > 0 |
| 121 | + ? `Node.js ${process.version}, ${os.endianness()}|${platform}` |
| 122 | + : `Node.js ${process.version}, ${os.endianness()}`; |
| 123 | + |
| 124 | + if (!metadataDocument.ifItFitsItSits('platform', platformInfo)) { |
| 125 | + throw new MongoInvalidArgumentError( |
| 126 | + 'Unable to include driverInfo platform, metadata cannot exceed 512 bytes' |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + // Note: order matters, os.type is last so it will be removed last if we're at maxSize |
| 131 | + const osInfo = new Map() |
| 132 | + .set('name', process.platform) |
| 133 | + .set('architecture', process.arch) |
| 134 | + .set('version', os.release()) |
| 135 | + .set('type', os.type()); |
| 136 | + |
| 137 | + if (!metadataDocument.ifItFitsItSits('os', osInfo)) { |
| 138 | + for (const key of osInfo.keys()) { |
| 139 | + osInfo.delete(key); |
| 140 | + if (osInfo.size === 0) break; |
| 141 | + if (metadataDocument.ifItFitsItSits('os', osInfo)) break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + const faasEnv = getFAASEnv(); |
| 146 | + if (faasEnv != null) { |
| 147 | + if (!metadataDocument.ifItFitsItSits('env', faasEnv)) { |
| 148 | + for (const key of faasEnv.keys()) { |
| 149 | + faasEnv.delete(key); |
| 150 | + if (faasEnv.size === 0) break; |
| 151 | + if (metadataDocument.ifItFitsItSits('env', faasEnv)) break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return metadataDocument.toObject(); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Collects FaaS metadata. |
| 161 | + * - `name` MUST be the last key in the Map returned. |
| 162 | + */ |
| 163 | +export function getFAASEnv(): Map<string, string | Int32> | null { |
| 164 | + const { |
| 165 | + AWS_EXECUTION_ENV = '', |
| 166 | + AWS_LAMBDA_RUNTIME_API = '', |
| 167 | + FUNCTIONS_WORKER_RUNTIME = '', |
| 168 | + K_SERVICE = '', |
| 169 | + FUNCTION_NAME = '', |
| 170 | + VERCEL = '', |
| 171 | + AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '', |
| 172 | + AWS_REGION = '', |
| 173 | + FUNCTION_MEMORY_MB = '', |
| 174 | + FUNCTION_REGION = '', |
| 175 | + FUNCTION_TIMEOUT_SEC = '', |
| 176 | + VERCEL_REGION = '' |
| 177 | + } = process.env; |
| 178 | + |
| 179 | + const isAWSFaaS = AWS_EXECUTION_ENV.length > 0 || AWS_LAMBDA_RUNTIME_API.length > 0; |
| 180 | + const isAzureFaaS = FUNCTIONS_WORKER_RUNTIME.length > 0; |
| 181 | + const isGCPFaaS = K_SERVICE.length > 0 || FUNCTION_NAME.length > 0; |
| 182 | + const isVercelFaaS = VERCEL.length > 0; |
| 183 | + |
| 184 | + // Note: order matters, name must always be the last key |
| 185 | + const faasEnv = new Map(); |
| 186 | + |
| 187 | + // When isVercelFaaS is true so is isAWSFaaS; Vercel inherits the AWS env |
| 188 | + if (isVercelFaaS && !(isAzureFaaS || isGCPFaaS)) { |
| 189 | + if (VERCEL_REGION.length > 0) { |
| 190 | + faasEnv.set('region', VERCEL_REGION); |
| 191 | + } |
| 192 | + |
| 193 | + faasEnv.set('name', 'vercel'); |
| 194 | + return faasEnv; |
| 195 | + } |
| 196 | + |
| 197 | + if (isAWSFaaS && !(isAzureFaaS || isGCPFaaS || isVercelFaaS)) { |
| 198 | + if (AWS_REGION.length > 0) { |
| 199 | + faasEnv.set('region', AWS_REGION); |
| 200 | + } |
| 201 | + |
| 202 | + if ( |
| 203 | + AWS_LAMBDA_FUNCTION_MEMORY_SIZE.length > 0 && |
| 204 | + Number.isInteger(+AWS_LAMBDA_FUNCTION_MEMORY_SIZE) |
| 205 | + ) { |
| 206 | + faasEnv.set('memory_mb', new Int32(AWS_LAMBDA_FUNCTION_MEMORY_SIZE)); |
| 207 | + } |
| 208 | + |
| 209 | + faasEnv.set('name', 'aws.lambda'); |
| 210 | + return faasEnv; |
| 211 | + } |
| 212 | + |
| 213 | + if (isAzureFaaS && !(isGCPFaaS || isAWSFaaS || isVercelFaaS)) { |
| 214 | + faasEnv.set('name', 'azure.func'); |
| 215 | + return faasEnv; |
| 216 | + } |
| 217 | + |
| 218 | + if (isGCPFaaS && !(isAzureFaaS || isAWSFaaS || isVercelFaaS)) { |
| 219 | + if (FUNCTION_REGION.length > 0) { |
| 220 | + faasEnv.set('region', FUNCTION_REGION); |
| 221 | + } |
| 222 | + |
| 223 | + if (FUNCTION_MEMORY_MB.length > 0 && Number.isInteger(+FUNCTION_MEMORY_MB)) { |
| 224 | + faasEnv.set('memory_mb', new Int32(FUNCTION_MEMORY_MB)); |
| 225 | + } |
| 226 | + |
| 227 | + if (FUNCTION_TIMEOUT_SEC.length > 0 && Number.isInteger(+FUNCTION_TIMEOUT_SEC)) { |
| 228 | + faasEnv.set('timeout_sec', new Int32(FUNCTION_TIMEOUT_SEC)); |
| 229 | + } |
| 230 | + |
| 231 | + faasEnv.set('name', 'gcp.func'); |
| 232 | + return faasEnv; |
| 233 | + } |
| 234 | + |
| 235 | + return null; |
| 236 | +} |
0 commit comments