{"library":"safe-buffer","title":"Safer Node.js Buffer API Polyfill","description":"safe-buffer is a JavaScript library that provides a consistent and safer API for Node.js `Buffer` operations, particularly for older Node.js environments. It backports the modern `Buffer.from`, `Buffer.alloc`, and `Buffer.allocUnsafe` methods, which address security and stability concerns related to uninitialized memory in older `new Buffer(size)` constructors. For Node.js versions where these safer methods are natively available (v6.0.0 and above), `safe-buffer` transparently defers to the built-in implementation, acting as a compatibility layer. The current stable version is 5.2.1, with releases being infrequent given its role as a polyfill for established Node.js core APIs, with the last major update occurring approximately 6 years ago. Its primary differentiator is ensuring safe `Buffer` allocation across a wide range of Node.js versions without requiring conditional logic in application code.","language":"javascript","status":"maintenance","last_verified":"Tue Apr 21","install":{"commands":["npm install safe-buffer"],"cli":null},"imports":["import { Buffer } from 'safe-buffer';","const Buffer = require('safe-buffer').Buffer;","import { Buffer } from 'safe-buffer';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Buffer = require('safe-buffer').Buffer;\n\nconsole.log(\"Using safe-buffer for Buffer operations:\\n\");\n\n// The old, potentially unsafe way: new Buffer(size) or Buffer(size)\n// While safe-buffer provides compatibility, this can create uninitialized buffers\n// in older Node.js versions and is deprecated.\nconsole.log('Old (deprecated) uninitialized buffer via new Buffer(10):');\ntry {\n  const oldBuffer = new Buffer(10);\n  console.log(oldBuffer); // Might show garbage data in older Node.js\n} catch (e) {\n  console.log('  Caught error for new Buffer(10):', e.message);\n}\n\n// Modern, safe way: zero-filled buffer (recommended for new allocations)\nconsole.log('\\nSafe zero-filled buffer via Buffer.alloc(10):');\nconst safeBuffer = Buffer.alloc(10);\nconsole.log(safeBuffer);\n\n// Modern way: uninitialized buffer (faster, use if you immediately overwrite contents)\n// Be cautious: this memory may contain sensitive data if not fully overwritten.\nconsole.log('\\nPotentially unsafe uninitialized buffer via Buffer.allocUnsafe(10):');\nconst unsafeAllocBuffer = Buffer.allocUnsafe(10);\nconsole.log(unsafeAllocBuffer); // Contents are unpredictable\n\n// Creating a buffer from a string\nconsole.log('\\nBuffer from string via Buffer.from(\"Hello, safe-buffer!\"):');\nconst stringBuffer = Buffer.from('Hello, safe-buffer!', 'utf8');\nconsole.log(stringBuffer.toString('utf8'));\n\n// Creating a buffer from an array of octets\nconsole.log('\\nBuffer from array via Buffer.from([0x68, 0x65, 0x6C, 0x6C, 0x6F]):');\nconst arrayBuffer = Buffer.from([0x68, 0x65, 0x6C, 0x6C, 0x6F]);\nconsole.log(arrayBuffer.toString('utf8'));\n\n// Copying an existing buffer\nconsole.log('\\nCopying an existing buffer:');\nconst originalBuf = Buffer.from('original');\nconst copiedBuf = Buffer.from(originalBuf); // Creates a new buffer with copied data\noriginalBuf[0] = 0x58; // Modify the original buffer\nconsole.log('  Original after change:', originalBuf.toString());\nconsole.log('  Copied buffer (should remain \"original\"):', copiedBuf.toString());","lang":"javascript","description":"Demonstrates `safe-buffer`'s core functionality, including safe and unsafe buffer allocation methods (`Buffer.alloc`, `Buffer.allocUnsafe`), and creating buffers from strings or arrays using `Buffer.from`, contrasting them with the deprecated `new Buffer()`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}