From 6009915b95b4e6d5249018b98bd8eded3dacf39e Mon Sep 17 00:00:00 2001 From: Johnathan Martell <286711055+johnmtll@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:03:42 -0500 Subject: [PATCH] fix: first pass on fixing metadatadocument insertion order 1. Orders os.type before platform, since it's not a documented truncatable. 2. Name is appended after os.type 3. OS is appended after name 4. Agent is appended after OS 5. The rest of env is appended after Agent --- src/cmap/handshake/client_metadata.ts | 93 +++++++++++---------------- 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/src/cmap/handshake/client_metadata.ts b/src/cmap/handshake/client_metadata.ts index 6cef14f90a..294c07fd84 100644 --- a/src/cmap/handshake/client_metadata.ts +++ b/src/cmap/handshake/client_metadata.ts @@ -127,10 +127,19 @@ export async function makeClientMetadata( const { os } = await runtime; const metadataDocument = new LimitedSizeDocument(512); + // The document keeps a reference to whatever it accepts, so a Map group is copied. An empty group is skipped. + const fit = (key: string, value: Record | string): boolean => { + if (!(value instanceof Map)) return metadataDocument.ifItFitsItSits(key, value); + return value.size === 0 || metadataDocument.ifItFitsItSits(key, new Map(value)); + }; + + const osInfo = new Map().set('type', os.type()); + const envInfo = new Map(); + // Add app name first, it must be sent if (appName.length > 0) { const name = DriverStringUtils.truncateStringBytes(appName, APP_NAME_TRUNCATION_LIMIT_BYTES); - metadataDocument.ifItFitsItSits('application', { name }); + fit('application', { name }); } const driverInfo = { @@ -148,12 +157,15 @@ export async function makeClientMetadata( } } - if (!metadataDocument.ifItFitsItSits('driver', driverInfo)) { + if (!fit('driver', driverInfo)) { throw new MongoInvalidArgumentError( 'Unable to include driverInfo name and version, metadata cannot exceed 512 bytes' ); } + // os.type is never omitted, so it is added before platform (the spec truncates platform last) + fit('os', osInfo); + let runtimeInfo = getRuntimeInfo(); // This is where we handle additional driver info added after client construction. for (const { platform = '' } of driverInfoList) { @@ -162,68 +174,41 @@ export async function makeClientMetadata( } } - if (!metadataDocument.ifItFitsItSits('platform', runtimeInfo)) { + if (!fit('platform', runtimeInfo)) { throw new MongoInvalidArgumentError( 'Unable to include driverInfo platform, metadata cannot exceed 512 bytes' ); } - // Note: order matters, os.type is last so it will be removed last if we're at maxSize - const osInfo = new Map() - .set('name', os.platform()) - .set('architecture', os.arch()) - .set('version', os.release()) - .set('type', os.type()); - - if (!metadataDocument.ifItFitsItSits('os', osInfo)) { - for (const key of osInfo.keys()) { - osInfo.delete(key); - if (osInfo.size === 0) break; - if (metadataDocument.ifItFitsItSits('os', osInfo)) break; - } - } - - // Env has an order of precedence for data truncation, and order matters. - // We append in the order of delete preference. Per the spec, 'agent' is omitted before 'name'. - - // TODO(NODE-7851): Truncation does not yet follow the spec's cumulative order in two ways: - // 1. `env.container` is appended last, so it is effectively deleted after `env.name`. If the - // FaaS fields don't fit but `container` does, we send `env` with `container` and no `name`. - // 2. `os` is fitted before `env`, so it is truncated too early. The spec trims `env` down to - // name & agent, then omits agent, then trims `os` down to `os.type`, and only then omits `env`. - - // A placeholder document that contains all groups of data to be appended to the metadata document - // which are furnished from environment variables (agent, FAAS, container) - const fullEnv = new Map(); - - // FAAS fields (other than name) are set first, then agent, then name last + // Add the remaining os and env fields in the reverse of the spec's omission order, so the fields + // the spec omits first are added last. Stop at the first group that does not fit to keep the + // truncation cumulative, and only gather a group's values once the earlier groups have fit. + // Append order (os.type is added before platform): os.type, platform, env.name, ...os, env.agent, ...env const faasEnv = getFAASEnv(); - for (const [k, v] of faasEnv) { - // delay setting name, it needs to be done *after* agent - if (k !== 'name') { - fullEnv.set(k, v); - } - } - const agentEnv = getAgentEnv(); - if (agentEnv.length > 0) fullEnv.set('agent', agentEnv); - if (faasEnv.has('name')) { - fullEnv.set('name', faasEnv.get('name')); + if (faasEnv.has('name')) envInfo.set('name', faasEnv.get('name')); + let fits = fit('env', envInfo); + + if (fits) { + osInfo + .set('name', os.platform()) + .set('architecture', os.arch()) + .set('version', os.release()); + fits = fit('os', osInfo); } - // Fits to the metadata document preferentially setting by property order - if (fullEnv.size > 0 && !metadataDocument.ifItFitsItSits('env', fullEnv)) { - for (const key of fullEnv.keys()) { - fullEnv.delete(key); - if (fullEnv.size === 0) break; - if (metadataDocument.ifItFitsItSits('env', fullEnv)) break; - } + if (fits) { + const agentEnv = getAgentEnv(); + if (agentEnv.length > 0) envInfo.set('agent', agentEnv); + fits = fit('env', envInfo); } - // Finally, append container - const containerMetadata = await getContainerMetadata(); - if (containerMetadata.size > 0) { - const newEnv = { ...Object.fromEntries(fullEnv), container: containerMetadata }; - metadataDocument.ifItFitsItSits('env', newEnv); + if (fits) { + for (const [k, v] of faasEnv) { + if (k !== 'name') envInfo.set(k, v); + } + const containerMetadata = await getContainerMetadata(); + if (containerMetadata.size > 0) envInfo.set('container', containerMetadata); + fit('env', envInfo); } return metadataDocument.toObject() as ClientMetadata;