{"id":13525664,"url":"https://github.com/mcollina/msgpack5","last_synced_at":"2025-05-14T18:07:04.105Z","repository":{"id":18722049,"uuid":"21933168","full_name":"mcollina/msgpack5","owner":"mcollina","description":"A msgpack v5 implementation for node.js, with extension points / msgpack.org[Node]","archived":false,"fork":false,"pushed_at":"2024-02-12T09:03:13.000Z","size":663,"stargazers_count":496,"open_issues_count":15,"forks_count":77,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-13T10:58:03.140Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/mcollina.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-07-17T07:46:48.000Z","updated_at":"2025-03-24T09:40:16.000Z","dependencies_parsed_at":"2024-06-18T12:24:53.388Z","dependency_job_id":null,"html_url":"https://github.com/mcollina/msgpack5","commit_stats":{"total_commits":199,"total_committers":34,"mean_commits":5.852941176470588,"dds":0.6030150753768844,"last_synced_commit":"a690317941b6b3a7960aed928eb02fb266d3277d"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmsgpack5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmsgpack5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmsgpack5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Fmsgpack5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcollina","download_url":"https://codeload.github.com/mcollina/msgpack5/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254198515,"owners_count":22030966,"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-01T06:01:20.885Z","updated_at":"2025-05-14T18:06:59.094Z","avatar_url":"https://github.com/mcollina.png","language":"JavaScript","readme":"msgpack5\u0026nbsp;\u0026nbsp;[![CI](https://github.com/mcollina/msgpack5/workflows/CI/badge.svg)](https://github.com/mcollina/msgpack5/actions?query=workflow%3ACI)\n========\n\nA msgpack v5 implementation for node.js and the browser, with extension point support.\n\nInstall\n-------\n\n```bash\nnpm install msgpack5 --save\n```\n\n\nUsage\n-----\n\n```js\nvar msgpack = require('msgpack5')() // namespace our extensions\n  , a       = new MyType(2, 'a')\n  , encode  = msgpack.encode\n  , decode  = msgpack.decode\n\nmsgpack.register(0x42, MyType, mytipeEncode, mytipeDecode)\n\nconst hex = encode({ 'hello': 'world' }).toString('hex')\nconsole.log(hex)\n// 81a568656c6c6fa5776f726c64\nconst obj = decode(Buffer.from(hex, 'hex'))\nconsole.log(obj)\n// { hello: 'world' }\n\nconsole.log(decode(encode({ 'hello': 'world' })))\n// { hello: 'world' }\nconsole.log(encode(a).toString('hex'))\n// d5426161\nconsole.log(decode(encode(a)) instanceof MyType)\n// true\nconsole.log(decode(encode(a)))\n// { value: 'a', size: 2 }\n\nfunction MyType(size, value) {\n  this.value = value\n  this.size  = size\n}\n\nfunction mytipeEncode(obj) {\n  var buf = new Buffer(obj.size)\n  buf.fill(obj.value)\n  return buf\n}\n\nfunction mytipeDecode(data) {\n  var result = new MyType(data.length, data.toString('utf8', 0, 1))\n    , i\n\n  for (i = 0; i \u003c data.length; i++) {\n    if (data.readUInt8(0) != data.readUInt8(i)) {\n      throw new Error('should all be the same')\n    }\n  }\n\n  return result\n}\n```\n\nIn the Browser\n-----------\n\nThis library is compatible with [Browserify](http://npm.im/browserify).\n\nIf you want to use standalone, grab the file in the `dist` folder of\nthis repo, and use in your own HTML page, the module will expose a\n`msgpack5` global.\n\n\n```\n\u003cscript type=\"text/javascript\"\n        src=\"./msgpack5.min.js\"\u003e\n\u003c/script\u003e\n```\n\n### To build\n\n```\n\tnpm run build\n```\n\nAPI\n---\n\n\u003ca name=\"api\"\u003e\u003c/a\u003e\n\n## API\n\n  * \u003ca href=\"#msgpack\"\u003e\u003ccode\u003e\u003cb\u003emsgpack()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#encode\"\u003e\u003ccode\u003emsgpack().\u003cb\u003eencode()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#decode\"\u003e\u003ccode\u003emsgpack().\u003cb\u003edecode()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#registerEncoder\"\u003e\u003ccode\u003emsgpack().\u003cb\u003eregisterEncoder()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#registerDecoder\"\u003e\u003ccode\u003emsgpack().\u003cb\u003eregisterDecoder()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#register\"\u003e\u003ccode\u003emsgpack().\u003cb\u003eregister()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#encoder\"\u003e\u003ccode\u003emsgpack().\u003cb\u003eencoder()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#decoder\"\u003e\u003ccode\u003emsgpack().\u003cb\u003edecoder()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n\n-------------------------------------------------------\n\u003ca name=\"msgpack\"\u003e\u003c/a\u003e\n\n### msgpack(options(obj))\n\nCreates a new instance on which you can register new types for being\nencoded.\n\noptions:\n\n- `forceFloat64`, a boolean to that forces all floats to be encoded as 64-bits floats. Defaults to false.\n- `sortKeys`, a boolean to force a determinate keys order\n- `compatibilityMode`, a boolean that enables \"compatibility mode\" which doesn't use bin format family and str 8 format. Defaults to false.\n- `disableTimestampEncoding`, a boolean that when set disables the encoding of Dates into the [timestamp extension type](https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type). Defaults to false.\n- `preferMap`, a boolean that forces all maps to be decoded to `Map`s rather than plain objects. This ensures that `decode(encode(new Map())) instanceof Map` and that iteration order is preserved. Defaults to false.\n- `protoAction`, a string which can be `error|ignore|remove` that determines what happens when decoding a plain object with a `__proto__` property which would cause prototype poisoning. `error` (default) throws an error, `remove` removes the property, `ignore` (not recommended) allows the property, thereby causing prototype poisoning on the decoded object.\n\n-------------------------------------------------------\n\u003ca name=\"encode\"\u003e\u003c/a\u003e\n\n### encode(object)\n\nEncodes `object` in msgpack, returns a [bl](http://npm.im/bl).\n\n-------------------------------------------------------\n\u003ca name=\"decode\"\u003e\u003c/a\u003e\n\n### decode(buf)\n\nDecodes buf from in msgpack. `buf` can be a `Buffer` or a [bl](http://npm.im/bl) instance.\n\nIn order to support a stream interface, a user must pass in a [bl](http://npm.im/bl) instance.\n\n-------------------------------------------------------\n\u003ca name=\"registerEncoder\"\u003e\u003c/a\u003e\n\n### registerEncoder(check(obj), encode(obj))\n\nRegister a new custom object type for being automatically encoded.\nThe arguments are:\n\n- `check`, a function that will be called to check if the passed\n  object should be encoded with the `encode` function\n- `encode`, a function that will be called to encode an object in binary\n  form; this function __must__ return a `Buffer` which include the same type\n  for [registerDecoder](#registerDecoder).\n\n-------------------------------------------------------\n\u003ca name=\"registerDecoder\"\u003e\u003c/a\u003e\n\n### registerDecoder(type, decode(buf))\n\nRegister a new custom object type for being automatically decoded.\nThe arguments are:\n\n- `type`, is a greater than zero integer identificating the type once serialized\n- `decode`, a function that will be called to decode the object from\n  the passed `Buffer`\n\n\n-------------------------------------------------------\n\u003ca name=\"register\"\u003e\u003c/a\u003e\n\n### register(type, constructor, encode(obj), decode(buf))\n\nRegister a new custom object type for being automatically encoded and\ndecoded. The arguments are:\n\n- `type`, is a greater than zero integer identificating the type once serialized\n- `constructor`, the function that will be used to match the objects\n  with `instanceof`\n- `encode`, a function that will be called to encode an object in binary\n  form; this function __must__ return a `Buffer` that can be\n  deserialized by the `decode` function\n- `decode`, a function that will be called to decode the object from\n  the passed `Buffer`\n\nThis is just a commodity that calls\n[`registerEncoder`](#registerEncoder) and\n[`registerDecoder`](#registerDecoder) internally.\n\n-------------------------------------------------------\n\u003ca name=\"encoder\"\u003e\u003c/a\u003e\n\n### encoder(options)\n\nBuilds a stream in object mode that encodes msgpack.\n\nSupported options:\n* `wrap`, objects should be passed to encoder in wrapped object {value: data}. Wrap option should be used if you need to pass null to encoder.\n\n\n-------------------------------------------------------\n\u003ca name=\"decoder\"\u003e\u003c/a\u003e\n\n### decoder(options)\n\nBuilds a stream in object mode that decodes msgpack.\n\nSupported options:\n* `wrap`, decoded objects returned in wrapped object {value: data}. Wrap option should be used if stream contains msgpack nil.\n\n\nLevelUp Support\n---------------\n\n__msgpack5__ can be used as a LevelUp\n[`valueEncoding`](https://github.com/rvagg/node-levelup#leveluplocation-options-callback) straight away:\n\n```js\nvar level = require('level')\n  , pack  = msgpack()\n  , db    = level('foo', {\n      valueEncoding: pack\n    })\n  , obj   = { my: 'obj' }\n\ndb.put('hello', obj, function(err) {\n  db.get('hello', function(err, result) {\n    console.log(result)\n    db.close()\n  })\n})\n\n```\n\nRelated projects\n----------------\n\n- [msgpack5rpc](http://npmjs.com/package/msgpack5rpc): An implementation of the\n  [msgpack-rpc spec](https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md)\n  on top of this library.\n\nDisclaimer\n----------\n\nThis library is built fully on JS and on [bl](http://npm.im/bl) to\nsimplify the code. Every improvement that keeps the same API is welcome.\n\nAcknowledgements\n----------------\n\nThis project was kindly sponsored by [nearForm](http://nearform.com).\n\n\nThis library was originally built as the data format for\n[JSChan](http://npm.im/jschan).\n\nLicense\n-------\n\nMIT\n","funding_links":[],"categories":["JavaScript","file format (文件格式)"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Fmsgpack5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcollina%2Fmsgpack5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Fmsgpack5/lists"}