{"record":{"id":"8b07499954d26719","repo":"lionsoul2014/ip2region","slug":"invalid-bytes-ip-not-a-buffer","errorCode":null,"errorMessage":"invalid bytes ip, not a Buffer","messagePattern":"invalid bytes ip, not a Buffer","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"binding/javascript/util.js","lineNumber":220,"sourceCode":"        if (_.length == 0) {\n            _.push('');\n        }\n\n        _.push(''); // empty for double colon\n\n        // make sure there is an empty tail\n        if (i == ps.length && _.length < ps.length) {\n            _.push('');\n        }\n    }\n\n    return _.join(':');\n}\n\n// bytes ip to humen-readable string ip\nexport function ipToString(ipBytes, compress) {\n    if (!Buffer.isBuffer(ipBytes)) {\n        throw new Error('invalid bytes ip, not a Buffer');\n    }\n\n    if (ipBytes.length == 4) {\n        return _ipv4_to_string(ipBytes, compress);\n    } else if (ipBytes.length == 16) {\n        return _ipv6_to_string(ipBytes, compress);\n    } else {\n        throw new Error('invalid bytes ip with length not 4 or 16');\n    }\n}\n\nexport function ipBytesString(ipBytes) {\n    if (!Buffer.isBuffer(ipBytes)) {\n        throw new Error('invalid bytes ip, not a Buffer');\n    }\n\n    let ps = [];\n    for (var i = 0; i < ipBytes.length; i++) {","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/javascript/util.js#L202-L238","documentation":"ipToString converts a raw 4-byte or 16-byte Buffer into a human-readable IPv4/IPv6 string. The library requires the argument to be a Node Buffer; any other value (string, Uint8Array, Array, null) is rejected up front with this TypeError-like Error so downstream byte indexing cannot silently misbehave.","triggerScenarios":"Calling ipToString(ipBytes, compress) with a non-Buffer value: e.g. passing a hex string, a plain array of bytes from JSON, a Uint8Array from WebCrypto, or null/undefined when a lookup result field was misread.","commonSituations":"Developers deserialize IP bytes from an external store (DB blob, JSON) as arrays or base64 strings and pass them straight in; TypeScript users using Uint8Array instead of Buffer; forgetting to call Buffer.from() on data read from a file split at manual offsets.","solutions":["Wrap the value with Buffer.from(ipBytes) before calling ipToString (Buffer.from accepts Uint8Array/array/base64/hex strings with an encoding).","If the source is a base64/hex string, decode explicitly: Buffer.from(str, 'hex') or Buffer.from(str, 'base64').","Check that the bytes actually come from the xdb searcher's result object rather than a hand-built value.","In TypeScript, type the parameter as Buffer and let the compiler catch mismatches."],"exampleFix":"// before\nconst ip = ipToString(JSON.parse(raw).ipBytes); // array -> throws\n// after\nconst bytes = Buffer.from(JSON.parse(raw).ipBytes);\nconst ip = ipToString(bytes);","handlingStrategy":"type-guard","validationCode":"function isIpBuffer(b) { return Buffer.isBuffer(b) && (b.length === 4 || b.length === 16); }\nif (!isIpBuffer(ipBytes)) throw new TypeError('expected 4/16-byte Buffer');","typeGuard":"function isIpBytes(v) { return Buffer.isBuffer(v) && (v.length === 4 || v.length === 16); }","tryCatchPattern":"try {\n  const ip = ipToString(ipBytes);\n} catch (e) {\n  if (e.message.includes('invalid bytes ip')) {\n    ipBytes = Buffer.from(ipBytes ?? []);\n    // retry or log and skip\n  } else throw e;\n}","preventionTips":["Keep IP bytes as Buffers end-to-end; convert at system boundaries with Buffer.from.","Never send Buffers through JSON.stringify/parse; encode as base64/hex instead.","In TypeScript, type IP byte fields as Buffer, not Uint8Array."],"tags":["nodejs","type-error","buffer","ip2region"],"backgroundTag":"invalid-argument-value","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}