{"id":11366,"library":"msgpack-lite","title":"msgpack-lite","description":"msgpack-lite is a pure JavaScript implementation of the MessagePack serialization format, providing fast encoding and decoding capabilities for both Node.js and web browsers. As of version 0.1.26, it offers synchronous `encode` and `decode` functions, along with streaming interfaces via `createEncodeStream` and `createDecodeStream`. It differentiated itself by claiming performance superior to some C++ MessagePack libraries for Node.js v4, without requiring native C++ compilation (node-gyp). The library supports various input types for decoding, including Node.js `Buffer`, standard JavaScript `Array`, and `Uint8Array`. It was tested on older Node.js versions (v0.10 through v6) and a wide range of browsers, including IE8. The last release was over 8 years ago, indicating it is no longer actively maintained.","status":"abandoned","version":"0.1.26","language":"javascript","source_language":"en","source_url":"https://github.com/kawanet/msgpack-lite","tags":["javascript","arraybuffer","buffer","fluentd","messagepack","msgpack","serialize","stream","typedarray"],"install":[{"cmd":"npm install msgpack-lite","lang":"bash","label":"npm"},{"cmd":"yarn add msgpack-lite","lang":"bash","label":"yarn"},{"cmd":"pnpm add msgpack-lite","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library is CommonJS-only and does not provide native ES Module exports. Attempting to use `import` syntax will fail in Node.js environments unless transpiled or configured with a bundler for browser usage.","wrong":"import msgpack from 'msgpack-lite';","symbol":"msgpack","correct":"const msgpack = require('msgpack-lite');"},{"note":"While CommonJS allows destructuring `require` results, the primary export is the `msgpack` object, and accessing `encode` and `decode` as properties (`msgpack.encode`, `msgpack.decode`) is the intended and most robust pattern. Direct destructuring can sometimes lead to context issues or subtle bugs depending on the module loader.","wrong":"import { encode, decode } from 'msgpack-lite';","symbol":"encode, decode","correct":"const { encode, decode } = require('msgpack-lite'); // Not recommended for direct destructuring\n// Or, preferably:\nconst msgpack = require('msgpack-lite');\nconst encoded = msgpack.encode(data);"},{"note":"These streaming utilities are methods on the main `msgpack` export and are accessed after requiring the entire module. They are specifically for Node.js stream environments.","wrong":"import { createEncodeStream } from 'msgpack-lite';","symbol":"createEncodeStream, createDecodeStream","correct":"const msgpack = require('msgpack-lite');\nconst encodeStream = msgpack.createEncodeStream();"}],"quickstart":{"code":"const fs = require('fs');\nconst msgpack = require('msgpack-lite');\n\n// Example 1: Basic synchronous encoding and decoding\nconst dataToSend = { id: 1, message: 'Hello, MessagePack!', timestamp: Date.now() };\nconst buffer = msgpack.encode(dataToSend);\nconsole.log('Encoded buffer:', buffer.toString('hex'));\nconst decodedData = msgpack.decode(buffer);\nconsole.log('Decoded data:', decodedData);\n\n// Example 2: Streaming encoding and decoding\n// Prepare a dummy file path for demonstration\nconst filePath = 'temp_data.msp';\n\nconst writeStream = fs.createWriteStream(filePath);\nconst encodeStream = msgpack.createEncodeStream();\n\nencodeStream.pipe(writeStream);\n\nencodeStream.write({ event: 'start', time: Date.now() });\nencodeStream.write({ user: 'Alice', action: 'login' });\nencodeStream.write({ user: 'Bob', action: 'logout' });\n\n// Ensure stream closes after all data is written\nencodeStream.end(() => {\n  console.log(`\nSuccessfully wrote MessagePack data to ${filePath}`);\n\n  // Now read from the stream\n  const readStream = fs.createReadStream(filePath);\n  const decodeStream = msgpack.createDecodeStream();\n\n  console.log('Decoding stream data:');\n  readStream.pipe(decodeStream).on('data', (obj) => {\n    console.log('Stream decoded object:', obj);\n  });\n\n  decodeStream.on('end', () => {\n    console.log('Finished decoding stream.');\n    fs.unlinkSync(filePath); // Clean up the dummy file\n  });\n\n  decodeStream.on('error', (err) => {\n    console.error('Error decoding stream:', err);\n    fs.unlinkSync(filePath); // Clean up on error\n  });\n});\n\nwriteStream.on('error', (err) => {\n  console.error('Error writing to stream:', err);\n  fs.unlinkSync(filePath); // Clean up on error\n});\n","lang":"javascript","description":"This quickstart demonstrates both synchronous MessagePack encoding/decoding and the use of streaming APIs to write and read multiple MessagePack objects to/from a file, including proper error handling and cleanup for the file system operations."},"warnings":[{"fix":"Migrate to an actively maintained MessagePack library (e.g., `npm install @msgpack/msgpack` or `npm install msgpackr`). Be aware that API changes will require code refactoring.","message":"The `msgpack-lite` package is no longer actively maintained, with its last publish occurring over 8 years ago. This means there will be no updates for new features, bug fixes, or critical security vulnerabilities. Users should consider migrating to actively maintained MessagePack libraries like `@msgpack/msgpack` or `msgpackr` for modern applications.","severity":"breaking","affected_versions":"0.1.x"},{"fix":"Always use `const msgpack = require('msgpack-lite');` for Node.js. For browser environments, use the provided `msgpack.min.js` directly via a `<script>` tag or configure a bundler (like Browserify, as suggested in the old README) to handle CommonJS modules.","message":"This library is designed for CommonJS (`require`) environments and does not natively support ES Modules (`import`). Using `import` syntax directly in a Node.js ESM context or without proper bundler configuration for browsers will result in module resolution errors.","severity":"gotcha","affected_versions":"0.1.x"},{"fix":"When creating `Buffer` instances from raw data, use `Buffer.from(array)` or `Buffer.from(string, encoding)` instead of `new Buffer()`. When allocating new, uninitialized buffers, use `Buffer.alloc()` or `Buffer.allocUnsafe()`.","message":"The library primarily deals with Node.js `Buffer` objects for binary data. While it accepts `Uint8Array` for decoding, its core operations in Node.js often produce and expect `Buffer` instances. In modern Node.js, `new Buffer()` is deprecated in favor of `Buffer.from()`, `Buffer.alloc()`, or `Buffer.allocUnsafe()`. Older code examples or existing data might use `new Buffer()`, which could lead to runtime warnings or security issues if not updated.","severity":"gotcha","affected_versions":"0.1.x"},{"fix":"For high-performance or modern Node.js/browser applications, benchmark `msgpack-lite` against current alternatives like `@msgpack/msgpack` or `msgpackr` within your specific use case to determine actual performance characteristics.","message":"Due to its age and lack of maintenance, `msgpack-lite`'s performance claims (being faster than C++ based `msgpack` on Node.js v4) are likely outdated. Modern MessagePack implementations and V8 engine optimizations have significantly advanced.","severity":"gotcha","affected_versions":"0.1.x"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `const msgpack = require('msgpack-lite');` and then calling `msgpack.encode()` or `msgpack.decode()`. The module does not provide named exports for ES Modules.","cause":"This typically occurs when trying to destructure the `msgpack` object incorrectly or when using `import { encode } from 'msgpack-lite';` in an unsupported environment.","error":"TypeError: msgpack.encode is not a function"},{"fix":"Ensure the input to `msgpack.decode()` is a `Buffer` (in Node.js), a `Uint8Array`, or a simple `Array` containing byte values (e.g., `[0x81, 0xA3, ...]`). Do not pass raw strings or other object types.","cause":"The `decode` function received an argument that is not a valid binary type (Node.js Buffer, standard JavaScript Array of numbers, or Uint8Array).","error":"TypeError: data must be a Buffer or an Array or a Uint8Array"},{"fix":"Verify that your stream pipeline is correctly configured. Ensure `encodeStream.end()` is called only once all data is written and that subsequent operations respect the stream's state. For reading, ensure `pipe()` is set up before data is expected, and handle `end` events to signal completion.","cause":"This error can occur when trying to read from a stream that has already ended or when stream piping/handling is incorrectly set up, leading to attempts to process data from a closed stream.","error":"Error: read after end"}],"ecosystem":"npm"}