{"id":"effd8b727856824a","repo":"mongodb/node-mongodb-native","slug":"unable-to-include-driverinfo-platform-metadata-ca","errorCode":null,"errorMessage":"Unable to include driverInfo platform, metadata cannot exceed 512 bytes","messagePattern":"Unable to include driverInfo platform, metadata cannot exceed 512 bytes","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cmap/handshake/client_metadata.ts","lineNumber":154,"sourceCode":"    }\n  }\n\n  if (!metadataDocument.ifItFitsItSits('driver', driverInfo)) {\n    throw new MongoInvalidArgumentError(\n      'Unable to include driverInfo name and version, metadata cannot exceed 512 bytes'\n    );\n  }\n\n  let runtimeInfo = getRuntimeInfo();\n  // This is where we handle additional driver info added after client construction.\n  for (const { platform = '' } of driverInfoList) {\n    if (platform.length > 0) {\n      runtimeInfo = `${runtimeInfo}|${platform}`;\n    }\n  }\n\n  if (!metadataDocument.ifItFitsItSits('platform', runtimeInfo)) {\n    throw new MongoInvalidArgumentError(\n      'Unable to include driverInfo platform, metadata cannot exceed 512 bytes'\n    );\n  }\n\n  // Note: order matters, os.type is last so it will be removed last if we're at maxSize\n  const osInfo = new Map()\n    .set('name', os.platform())\n    .set('architecture', os.arch())\n    .set('version', os.release())\n    .set('type', os.type());\n\n  if (!metadataDocument.ifItFitsItSits('os', osInfo)) {\n    for (const key of osInfo.keys()) {\n      osInfo.delete(key);\n      if (osInfo.size === 0) break;\n      if (metadataDocument.ifItFitsItSits('os', osInfo)) break;\n    }\n  }","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/handshake/client_metadata.ts#L136-L172","documentation":"Thrown while building client metadata when the platform field (driver runtime info plus any user-supplied driverInfo.platform strings joined with '|') would push the metadata document past the 512-byte BSON limit. Unlike os/env fields which the driver progressively omits to fit, the platform field is explicitly NOT truncated (per the spec comment in the source), so exceeding the limit is a hard error. Surfaced as MongoInvalidArgumentError during the handshake metadata build.","triggerScenarios":"One or more driverInfo entries supply a very long platform string; concatenated with the driver's own runtime info they push the cumulative metadata past 512 bytes after appName, driver, os, and env have been laid out. Because platform is never truncated, a single oversized platform string can trigger this even when other fields are small.","commonSituations":"Instrumentation/wrapper libraries that inject long platform identifiers (build URLs, commit SHAs, full environment descriptors) via driverInfo.platform. Stacking several such libraries multiplies the length. CI environments that embed long build metadata into platform.","solutions":["Shorten or remove the driverInfo.platform string(s) contributed by wrappers.","Consolidate multiple platform contributions into one concise value."],"exampleFix":"// before\nnew MongoClient(uri, { driverInfo: { platform: 'very-long-build-url-or-commit-string'.repeat(20) } });\n\n// after - keep platform concise\nnew MongoClient(uri, { driverInfo: { platform: 'svc-x' } });","handlingStrategy":"validation","validationCode":"// Validate the platform string length before passing it as driverInfo.\nfunction checkPlatform(driverInfoPlatform) {\n  const joined = `${getRuntimeInfoStub()}|${driverInfoPlatform ?? ''}`;\n  // platform is never truncated; keep total metadata (incl appName/driver/os/env) under 512 bytes.\n  if (Buffer.byteLength(joined, 'utf8') > 400) throw new Error('driverInfo.platform too long for 512-byte metadata limit');\n}","typeGuard":null,"tryCatchPattern":"try {\n  const client = new MongoClient(uri, { driverInfo: { platform } });\n  await client.connect();\n} catch (err) {\n  if (err instanceof MongoInvalidArgumentError && /driverInfo platform/.test(err.message)) {\n    // shorten the platform string and retry\n  }\n  throw err;\n}","preventionTips":["Keep driverInfo.platform concise (avoid long build URLs / commit strings).","Consolidate multiple platform contributions into one short value.","The platform field is never truncated, so treat the limit as hard."],"tags":["handshake","client-metadata","configuration","size-limit"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}