{"id":13724877,"url":"https://github.com/kawanet/msgpack-lite","last_synced_at":"2025-04-11T03:28:40.050Z","repository":{"id":35381143,"uuid":"39644464","full_name":"kawanet/msgpack-lite","owner":"kawanet","description":"Fast Pure JavaScript MessagePack Encoder and Decoder / msgpack.org[JavaScript]","archived":false,"fork":false,"pushed_at":"2022-12-02T12:23:38.000Z","size":481,"stargazers_count":990,"open_issues_count":57,"forks_count":128,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-10-23T12:45:45.578Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/msgpack-lite","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kawanet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-24T16:18:19.000Z","updated_at":"2024-10-19T07:11:32.000Z","dependencies_parsed_at":"2022-09-15T13:20:35.638Z","dependency_job_id":null,"html_url":"https://github.com/kawanet/msgpack-lite","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawanet%2Fmsgpack-lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawanet%2Fmsgpack-lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawanet%2Fmsgpack-lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kawanet%2Fmsgpack-lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kawanet","download_url":"https://codeload.github.com/kawanet/msgpack-lite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248335137,"owners_count":21086523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:02:05.441Z","updated_at":"2025-04-11T03:28:40.017Z","avatar_url":"https://github.com/kawanet.png","language":"JavaScript","readme":"# msgpack-lite [![npm version](https://badge.fury.io/js/msgpack-lite.svg)](http://badge.fury.io/js/msgpack-lite) [![Build Status](https://travis-ci.org/kawanet/msgpack-lite.svg?branch=master)](https://travis-ci.org/kawanet/msgpack-lite)\n\nFast Pure JavaScript MessagePack Encoder and Decoder\n\n[![Sauce Test Status](https://saucelabs.com/browser-matrix/msgpack-lite.svg)](https://saucelabs.com/u/msgpack-lite)\n\nOnline demo: [http://kawanet.github.io/msgpack-lite/](http://kawanet.github.io/msgpack-lite/)\n\n### Features\n\n- Pure JavaScript only (No node-gyp nor gcc required)\n- Faster than any other pure JavaScript libraries on node.js v4\n- Even faster than node-gyp C++ based [msgpack](https://www.npmjs.com/package/msgpack) library (**90% faster** on encoding)\n- Streaming encoding and decoding interface is also available. It's more faster.\n- Ready for [Web browsers](https://saucelabs.com/u/msgpack-lite) including Chrome, Firefox, Safari and even IE8\n- [Tested](https://travis-ci.org/kawanet/msgpack-lite) on Node.js v0.10, v0.12, v4, v5 and v6 as well as Web browsers\n\n### Encoding and Decoding MessagePack\n\n```js\nvar msgpack = require(\"msgpack-lite\");\n\n// encode from JS Object to MessagePack (Buffer)\nvar buffer = msgpack.encode({\"foo\": \"bar\"});\n\n// decode from MessagePack (Buffer) to JS Object\nvar data = msgpack.decode(buffer); // =\u003e {\"foo\": \"bar\"}\n\n// if encode/decode receives an invalid argument an error is thrown\n```\n\n### Writing to MessagePack Stream\n\n```js\nvar fs = require(\"fs\");\nvar msgpack = require(\"msgpack-lite\");\n\nvar writeStream = fs.createWriteStream(\"test.msp\");\nvar encodeStream = msgpack.createEncodeStream();\nencodeStream.pipe(writeStream);\n\n// send multiple objects to stream\nencodeStream.write({foo: \"bar\"});\nencodeStream.write({baz: \"qux\"});\n\n// call this once you're done writing to the stream.\nencodeStream.end();\n```\n\n### Reading from MessagePack Stream\n\n```js\nvar fs = require(\"fs\");\nvar msgpack = require(\"msgpack-lite\");\n\nvar readStream = fs.createReadStream(\"test.msp\");\nvar decodeStream = msgpack.createDecodeStream();\n\n// show multiple objects decoded from stream\nreadStream.pipe(decodeStream).on(\"data\", console.warn);\n```\n\n### Decoding MessagePack Bytes Array\n\n```js\nvar msgpack = require(\"msgpack-lite\");\n\n// decode() accepts Buffer instance per default\nmsgpack.decode(Buffer([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));\n\n// decode() also accepts Array instance\nmsgpack.decode([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);\n\n// decode() accepts raw Uint8Array instance as well\nmsgpack.decode(new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]));\n```\n\n### Command Line Interface\n\nA CLI tool bin/msgpack converts data stream from JSON to MessagePack and vice versa.\n\n```sh\n$ echo '{\"foo\": \"bar\"}' | ./bin/msgpack -Jm | od -tx1\n0000000    81  a3  66  6f  6f  a3  62  61  72\n\n$ echo '{\"foo\": \"bar\"}' | ./bin/msgpack -Jm | ./bin/msgpack -Mj\n{\"foo\":\"bar\"}\n```\n\n### Installation\n\n```sh\n$ npm install --save msgpack-lite\n```\n\n### Tests\n\nRun tests on node.js:\n\n```sh\n$ make test\n```\n\nRun tests on browsers:\n\n```sh\n$ make test-browser-local\nopen the following url in a browser:\nhttp://localhost:4000/__zuul\n```\n\n### Browser Build\n\nBrowser version [msgpack.min.js](https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js) is also available. 50KB minified, 14KB gziped.\n\n```html\n\u003c!--[if lte IE 9]\u003e\n\u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.10/es5-shim.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js\"\u003e\u003c/script\u003e\n\u003c![endif]--\u003e\n\u003cscript src=\"https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n// encode from JS Object to MessagePack (Uint8Array)\nvar buffer = msgpack.encode({foo: \"bar\"});\n\n// decode from MessagePack (Uint8Array) to JS Object\nvar array = new Uint8Array([0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72]);\nvar data = msgpack.decode(array);\n\u003c/script\u003e\n```\n\n### MessagePack With Browserify\n\nStep #1: write some code at first.\n\n```js\nvar msgpack = require(\"msgpack-lite\");\nvar buffer = msgpack.encode({\"foo\": \"bar\"});\nvar data = msgpack.decode(buffer);\nconsole.warn(data); // =\u003e {\"foo\": \"bar\"}\n```\n\nProceed to the next steps if you prefer faster browserify compilation time.\n\nStep #2: add `browser` property on `package.json` in your project. This refers the global `msgpack` object instead of including whole of `msgpack-lite` source code.\n\n```json\n{\n  \"dependencies\": {\n    \"msgpack-lite\": \"*\"\n  },\n  \"browser\": {\n    \"msgpack-lite\": \"msgpack-lite/global\"\n  }\n}\n```\n\nStep #3: compile it with [browserify](https://www.npmjs.com/package/browserify) and [uglifyjs](https://www.npmjs.com/package/uglify-js).\n\n```sh\nbrowserify src/main.js -o tmp/main.browserify.js -s main\nuglifyjs tmp/main.browserify.js -m -c -o js/main.min.js\ncp node_modules/msgpack-lite/dist/msgpack.min.js js/msgpack.min.js\n```\n\nStep #4: load [msgpack.min.js](https://rawgit.com/kawanet/msgpack-lite/master/dist/msgpack.min.js) before your code.\n\n```html\n\u003cscript src=\"js/msgpack.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"js/main.min.js\"\u003e\u003c/script\u003e\n```\n\n### Interoperability\n\nIt is tested to have basic compatibility with other Node.js MessagePack modules below:\n\n- [https://www.npmjs.com/package/msgpack](https://www.npmjs.com/package/msgpack) (1.0.2)\n- [https://www.npmjs.com/package/msgpack-js](https://www.npmjs.com/package/msgpack-js) (0.3.0)\n- [https://www.npmjs.com/package/msgpack-js-v5](https://www.npmjs.com/package/msgpack-js-v5) (0.3.0-v5)\n- [https://www.npmjs.com/package/msgpack-unpack](https://www.npmjs.com/package/msgpack-unpack) (2.1.1)\n- [https://github.com/msgpack/msgpack-javascript](https://github.com/msgpack/msgpack-javascript) (msgpack.codec)\n- [https://www.npmjs.com/package/msgpack5](https://www.npmjs.com/package/msgpack5) (3.3.0)\n- [https://www.npmjs.com/package/notepack](https://www.npmjs.com/package/notepack) (0.0.2)\n\n### Benchmarks\n\nA benchmark tool `lib/benchmark.js` is available to compare encoding/decoding speed\n(operation per second) with other MessagePack modules.\nIt counts operations of [1KB JSON document](https://github.com/kawanet/msgpack-lite/blob/master/test/example.json) in 10 seconds.\n\n```sh\n$ npm install msgpack msgpack-js msgpack-js-v5 msgpack-unpack msgpack5 notepack\n$ npm run benchmark 10\n```\n\noperation                                                 |   op   |   ms  |  op/s \n--------------------------------------------------------- | -----: | ----: | -----:\nbuf = Buffer(JSON.stringify(obj));                        | 1055200 | 10000 | 105520\nobj = JSON.parse(buf);                                    | 863800 | 10000 |  86380\nbuf = require(\"msgpack-lite\").encode(obj);                | 969100 | 10000 |  96910\nobj = require(\"msgpack-lite\").decode(buf);                | 600300 | 10000 |  60030\nbuf = require(\"msgpack\").pack(obj);                       | 503500 | 10001 |  50344\nobj = require(\"msgpack\").unpack(buf);                     | 560200 | 10001 |  56014\nbuf = Buffer(require(\"msgpack.codec\").msgpack.pack(obj)); | 653500 | 10000 |  65349\nobj = require(\"msgpack.codec\").msgpack.unpack(buf);       | 367500 | 10001 |  36746\nbuf = require(\"msgpack-js-v5\").encode(obj);               | 189500 | 10002 |  18946\nobj = require(\"msgpack-js-v5\").decode(buf);               | 408900 | 10000 |  40890\nbuf = require(\"msgpack-js\").encode(obj);                  | 189200 | 10000 |  18920\nobj = require(\"msgpack-js\").decode(buf);                  | 375600 | 10002 |  37552\nbuf = require(\"msgpack5\")().encode(obj);                  | 110500 | 10009 |  11040\nobj = require(\"msgpack5\")().decode(buf);                  | 165500 | 10000 |  16550\nbuf = require(\"notepack\")().encode(obj);                  | 847800 | 10000 |  84780\nobj = require(\"notepack\")().decode(buf);                  | 599800 | 10000 |  59980\nobj = require(\"msgpack-unpack\").decode(buf);              |  48100 | 10002 |   4809\n\nStreaming benchmark tool `lib/benchmark-stream.js` is also available.\nIt counts milliseconds for 1,000,000 operations of 30 bytes fluentd msgpack fragment.\nThis shows streaming encoding and decoding are super faster.\n\n```sh\n$ npm run benchmark-stream 2\n```\n\noperation (1000000 x 2)                          |   op    |  ms   |  op/s \n------------------------------------------------ | ------: | ----: | -----:\nstream.write(msgpack.encode(obj));               | 1000000 |  3027 | 330360\nstream.write(notepack.encode(obj));              | 1000000 |  2012 | 497017\nmsgpack.Encoder().on(\"data\",ondata).encode(obj); | 1000000 |  2956 | 338294\nmsgpack.createEncodeStream().write(obj);         | 1000000 |  1888 | 529661\nstream.write(msgpack.decode(buf));               | 1000000 |  2020 | 495049\nstream.write(notepack.decode(buf));              | 1000000 |  1794 | 557413\nmsgpack.Decoder().on(\"data\",ondata).decode(buf); | 1000000 |  2744 | 364431\nmsgpack.createDecodeStream().write(buf);         | 1000000 |  1341 | 745712\n\nTest environment: msgpack-lite 0.1.14, Node v4.2.3, Intel(R) Xeon(R) CPU E5-2666 v3 @ 2.90GHz\n\n### MessagePack Mapping Table\n\nThe following table shows how JavaScript objects (value) will be mapped to \n[MessagePack formats](https://github.com/msgpack/msgpack/blob/master/spec.md)\nand vice versa.\n\nSource Value|MessagePack Format|Value Decoded\n----|----|----\nnull, undefined|nil format family|null\nBoolean (true, false)|bool format family|Boolean (true, false)\nNumber (32bit int)|int format family|Number (int or double)\nNumber (64bit double)|float format family|Number (double)\nString|str format family|String\nBuffer|bin format family|Buffer\nArray|array format family|Array\nMap|map format family|Map (if `usemap=true`)\nObject (plain object)|map format family|Object (or Map if `usemap=true`)\nObject (see below)|ext format family|Object (see below)\n\nNote that both `null` and `undefined` are mapped to nil `0xC1` type.\nThis means `undefined` value will be *upgraded* to `null` in other words.\n\n### Extension Types\n\nThe MessagePack specification allows 128 application-specific extension types. \nThe library uses the following types to make round-trip conversion possible\nfor JavaScript native objects.\n\nType|Object|Type|Object\n----|----|----|----\n0x00||0x10|\n0x01|EvalError|0x11|Int8Array\n0x02|RangeError|0x12|Uint8Array\n0x03|ReferenceError|0x13|Int16Array\n0x04|SyntaxError|0x14|Uint16Array\n0x05|TypeError|0x15|Int32Array\n0x06|URIError|0x16|Uint32Array\n0x07||0x17|Float32Array\n0x08||0x18|Float64Array\n0x09||0x19|Uint8ClampedArray\n0x0A|RegExp|0x1A|ArrayBuffer\n0x0B|Boolean|0x1B|Buffer\n0x0C|String|0x1C|\n0x0D|Date|0x1D|DataView\n0x0E|Error|0x1E|\n0x0F|Number|0x1F|\n\nOther extension types are mapped to built-in ExtBuffer object.\n\n### Custom Extension Types (Codecs)\n\nRegister a custom extension type number to serialize/deserialize your own class instances.\n\n```js\nvar msgpack = require(\"msgpack-lite\");\n\nvar codec = msgpack.createCodec();\ncodec.addExtPacker(0x3F, MyVector, myVectorPacker);\ncodec.addExtUnpacker(0x3F, myVectorUnpacker);\n\nvar data = new MyVector(1, 2);\nvar encoded = msgpack.encode(data, {codec: codec});\nvar decoded = msgpack.decode(encoded, {codec: codec});\n\nfunction MyVector(x, y) {\n  this.x = x;\n  this.y = y;\n}\n\nfunction myVectorPacker(vector) {\n  var array = [vector.x, vector.y];\n  return msgpack.encode(array); // return Buffer serialized\n}\n\nfunction myVectorUnpacker(buffer) {\n  var array = msgpack.decode(buffer);\n  return new MyVector(array[0], array[1]); // return Object deserialized\n}\n```\n\nThe first argument of `addExtPacker` and `addExtUnpacker` should be an integer within the range of 0 and 127 (0x0 and 0x7F). `myClassPacker` is a function that accepts an instance of `MyClass`, and should return a buffer representing that instance. `myClassUnpacker` is the opposite: it accepts a buffer and should return an instance of `MyClass`.\n\nIf you pass an array of functions to `addExtPacker` or `addExtUnpacker`, the value to be encoded/decoded will pass through each one in order. This allows you to do things like this:\n\n```js\ncodec.addExtPacker(0x00, Date, [Number, msgpack.encode]);\n```\n\nYou can also pass the `codec` option to `msgpack.Decoder(options)`, `msgpack.Encoder(options)`, `msgpack.createEncodeStream(options)`, and `msgpack.createDecodeStream(options)`.\n\nIf you wish to modify the default built-in codec, you can access it at `msgpack.codec.preset`.\n\n### Custom Codec Options\n\n`msgpack.createCodec()` function accepts some options.\n\nIt does NOT have the preset extension types defined when no options given.\n\n```js\nvar codec = msgpack.createCodec();\n```\n\n`preset`: It has the preset extension types described above.\n\n```js\nvar codec = msgpack.createCodec({preset: true});\n```\n\n`safe`: It runs a validation of the value before writing it into buffer. This is the default behavior for some old browsers which do not support `ArrayBuffer` object.\n\n```js\nvar codec = msgpack.createCodec({safe: true});\n```\n\n`useraw`: It uses `raw` formats instead of `bin` and `str`.\n\n```js\nvar codec = msgpack.createCodec({useraw: true});\n```\n\n`int64`: It decodes msgpack's `int64`/`uint64` formats with [int64-buffer](https://github.com/kawanet/int64-buffer) object.\n\n```js\nvar codec = msgpack.createCodec({int64: true});\n```\n\n`binarraybuffer`: It ties msgpack's `bin` format with `ArrayBuffer` object, instead of `Buffer` object.\n\n```js\nvar codec = msgpack.createCodec({binarraybuffer: true, preset: true});\n```\n\n`uint8array`: It returns Uint8Array object when encoding, instead of `Buffer` object.\n\n```js\nvar codec = msgpack.createCodec({uint8array: true});\n```\n\n`usemap`: Uses the global JavaScript Map type, if available, to unpack\nMessagePack map elements.\n\n```js\nvar codec = msgpack.createCodec({usemap: true});\n```\n\n### Compatibility Mode\n\nThe [compatibility mode](https://github.com/kawanet/msgpack-lite/issues/22) respects for [msgpack's old spec](https://github.com/msgpack/msgpack/blob/master/spec-old.md). Set `true` to `useraw`.\n\n```js\n// default mode handles both str and bin formats individually\nmsgpack.encode(\"Aa\"); // =\u003e \u003cBuffer a2 41 61\u003e (str format)\nmsgpack.encode(new Buffer([0x41, 0x61])); // =\u003e \u003cBuffer c4 02 41 61\u003e (bin format)\n\nmsgpack.decode(new Buffer([0xa2, 0x41, 0x61])); // =\u003e 'Aa' (String)\nmsgpack.decode(new Buffer([0xc4, 0x02, 0x41, 0x61])); // =\u003e \u003cBuffer 41 61\u003e (Buffer)\n\n// compatibility mode handles only raw format both for String and Buffer\nvar options = {codec: msgpack.createCodec({useraw: true})};\nmsgpack.encode(\"Aa\", options); // =\u003e \u003cBuffer a2 41 61\u003e (raw format)\nmsgpack.encode(new Buffer([0x41, 0x61]), options); // =\u003e \u003cBuffer a2 41 61\u003e (raw format)\n\nmsgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options); // =\u003e \u003cBuffer 41 61\u003e (Buffer)\nmsgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options).toString(); // =\u003e 'Aa' (String)\n```\n\n### Repository\n\n- [https://github.com/kawanet/msgpack-lite](https://github.com/kawanet/msgpack-lite)\n\n### See Also\n\n- [http://msgpack.org/](http://msgpack.org/)\n\n### License\n\nThe MIT License (MIT)\n\nCopyright (c) 2015-2016 Yusuke Kawasaki\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":["JavaScript","Packages"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkawanet%2Fmsgpack-lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkawanet%2Fmsgpack-lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkawanet%2Fmsgpack-lite/lists"}