{"name":"cbor","version":"10.0.9","keywords":["coap","cbor","json","rfc7049","rfc8949"],"author":{"name":"Joe Hildebrand","email":"joe-github@cursive.net"},"license":"MIT","_id":"cbor@10.0.9","maintainers":[{"name":"paroga","email":"paroga@paroga.com"},{"name":"dotcypress","email":"dotcypress@gmail.com"},{"name":"hildjj","email":"joe-github@cursive.net"}],"contributors":[{"url":"http://paroga.com/","name":"Patrick Gansterer","email":"paroga@paroga.com"},{"url":"http://scorpi.org/","name":"Artyom Yagilev","email":"github@scorpi.org"},{"url":"http://lapaev.me/","name":"Denis Lapaev","email":"den@lapaev.me"},{"name":"Ruben Bridgewater","email":"ruben@bridgewater.de"},{"name":"Burt Harris","email":"Burt_Harris_cbor@azxs.33mail.com"},{"url":"https://jakubarbet.me/","name":"Jakub Arbet","email":"hi@jakubarbet.me"},{"url":"https://github.com/rouzwelt","name":"Rouzbeh Karimi","email":"rouzweltt@gmail.com"}],"homepage":"https://github.com/hildjj/node-cbor/tree/main/packages/cbor","bugs":{"url":"https://github.com/hildjj/node-cbor/issues"},"dist":{"shasum":"a0ef66aac57cf176e928bdc70d37ae305f2db2f8","tarball":"http://registry-npm.ywwl.com/cbor/download/cbor-10.0.9.tgz","fileCount":30,"integrity":"sha512-KEWYehb/vJkRmigctVQLsz73Us2RNnITo/wOwQV5AtZpLGH1r2PPlsNHdsX460YuHZCyhLklbYzAOuJfOeg34Q==","signatures":[{"sig":"MEUCIQDvH4//Qo2ulgKWceoOOCTet+NVcD9Y9zxm++qqObC8wwIgOsCjrTIJQoezbfQu2QBEnL3rGTArCLxPvHlzoCTr7xI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/cbor@10.0.9","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":166609,"size":42738,"noattachment":false,"key":"/cbor/-/cbor-10.0.9.tgz"},"main":"./lib/cbor.js","types":"./types/lib/cbor.d.ts","browser":{"fs":false},"engines":{"node":">=20"},"gitHead":"107f057e7b4b1ce29b7af1db4a96bf77d93c462b","_npmUser":{"name":"hildjj","actor":{"name":"hildjj","type":"user","email":"joe-github@cursive.net"},"email":"joe-github@cursive.net"},"repository":{"url":"git+ssh://git@github.com/hildjj/node-cbor.git","type":"git","directory":"packages/cbor"},"_npmVersion":"10.9.2","description":"Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).","directories":{"lib":"lib"},"_nodeVersion":"22.16.0","dependencies":{"nofilter":"^3.0.2"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/cbor_10.0.9_1751297417702_0.07557356802304893","host":"s3://npm-registry-packages-npm-production"},"readme":"# cbor\n\nEncode and parse data in the Concise Binary Object Representation (CBOR) data format ([RFC8949](https://www.rfc-editor.org/rfc/rfc8949.html)).\n\n## MOVE TO CBOR2\n\n**NOTE**\n\nAll new users and most existing users of this library should move to the\n[cbor2](https://github.com/hildjj/cbor2) library.  It is where most\nmaintenance and support and all new features are happening.\n\n*Only* catastrophic bugs will be fixed in this library going forward.\n\n## Supported Node.js versions\n\nThis project now only supports versions of Node that the Node team is\n[currently supporting](https://github.com/nodejs/Release#release-schedule).\nAva's [support\nstatement](https://github.com/avajs/ava/blob/main/docs/support-statement.md)\nis what we will be using as well.  Currently, that means Node `18`+ is\nrequired.  If you need to support an older version of Node (back to version\n6), use cbor version 5.2.x, which will get nothing but security updates from\nhere on out.\n\n## Installation:\n\n```bash\n$ npm install --save cbor\n```\n\n**NOTE**\nIf you are going to use this on the web, use [cbor-web](../cbor-web/) instead.\n\nIf you need support for encoding and decoding BigDecimal fractions (tag 4) or\nBigFloats (tag 5), please see [cbor-bigdecimal](../cbor-bigdecimal/).\n\n## Documentation:\n\nSee the full API [documentation](http://hildjj.github.io/node-cbor/).\n\nFor a command-line interface, see [cbor-cli](../cbor-cli/).\n\nExample:\n```js\nconst cbor = require('cbor');\nconst assert = require('node:assert');\n\nlet encoded = cbor.encode(true); // Returns <Buffer f5>\ncbor.decodeFirst(encoded, (error, obj) => {\n  // If there was an error, error != null\n  // obj is the unpacked object\n  assert.ok(obj === true);\n});\n\n// Use integers as keys?\nconst m = new Map();\nm.set(1, 2);\nencoded = cbor.encode(m); // <Buffer a1 01 02>\n```\n\nAllows streaming as well:\n\n```js\nconst cbor = require('cbor');\nconst fs = require('node:fs');\n\nconst d = new cbor.Decoder();\nd.on('data', obj => {\n  console.log(obj);\n});\n\nconst s = fs.createReadStream('foo');\ns.pipe(d);\n\nconst d2 = new cbor.Decoder({input: '00', encoding: 'hex'});\nd.on('data', obj => {\n  console.log(obj);\n});\n```\n\nThere is also support for synchronous decodes:\n\n```js\ntry {\n  console.log(cbor.decodeFirstSync('02')); // 2\n  console.log(cbor.decodeAllSync('0202')); // [2, 2]\n} catch (e) {\n  // Throws on invalid input\n}\n```\n\nThe sync encoding and decoding are exported as a\n[leveldb encoding](https://github.com/Level/levelup#custom_encodings), as\n`cbor.leveldb`.\n\n## highWaterMark\n\nThe synchronous routines for encoding and decoding will have problems with\nobjects that are larger than 16kB, which the default buffer size for Node\nstreams.  There are a few ways to fix this:\n\n1) pass in a `highWaterMark` option with the value of the largest buffer size you think you will need:\n\n```js\ncbor.encodeOne(new ArrayBuffer(40000), {highWaterMark: 65535});\n```\n\n2) use stream mode.  Catch the `data`, `finish`, and `error` events.  Make sure to call `end()` when you're done.\n\n```js\nconst enc = new cbor.Encoder();\nenc.on('data', buf => /* Send the data somewhere */ null);\nenc.on('error', console.error);\nenc.on('finish', () => /* Tell the consumer we are finished */ null);\n\nenc.end(['foo', 1, false]);\n```\n\n3) use `encodeAsync()`, which uses the approach from approach 2 to return a memory-inefficient promise for a Buffer.\n\n## Supported types\n\nThe following types are supported for encoding:\n\n* boolean\n* number (including -0, NaN, and ±Infinity)\n* string\n* Array, Set (encoded as Array)\n* Object (including null), Map\n* undefined\n* Buffer\n* Date,\n* RegExp\n* URL\n* TypedArrays, ArrayBuffer, DataView\n* Map, Set\n* BigInt\n\nDecoding supports the above types, including the following CBOR tag numbers:\n\n| Tag | Generated Type      |\n|-----|---------------------|\n| 0   | Date                |\n| 1   | Date                |\n| 2   | BigInt              |\n| 3   | BigInt              |\n| 21  | Tagged, with toJSON |\n| 22  | Tagged, with toJSON |\n| 23  | Tagged, with toJSON |\n| 32  | URL                 |\n| 33  | Tagged              |\n| 34  | Tagged              |\n| 35  | RegExp              |\n| 64  | Uint8Array          |\n| 65  | Uint16Array         |\n| 66  | Uint32Array         |\n| 67  | BigUint64Array      |\n| 68  | Uint8ClampedArray   |\n| 69  | Uint16Array         |\n| 70  | Uint32Array         |\n| 71  | BigUint64Array      |\n| 72  | Int8Array           |\n| 73  | Int16Array          |\n| 74  | Int32Array          |\n| 75  | BigInt64Array       |\n| 77  | Int16Array          |\n| 78  | Int32Array          |\n| 79  | BigInt64Array       |\n| 81  | Float32Array        |\n| 82  | Float64Array        |\n| 85  | Float32Array        |\n| 86  | Float64Array        |\n| 258 | Set                 |\n\n## Adding new Encoders\n\nThere are several ways to add a new encoder:\n\n### `encodeCBOR` method\n\nThis is the easiest approach, if you can modify the class being encoded.  Add an\n`encodeCBOR` method to your class, which takes a single parameter of the encoder\ncurrently being used.  Your method should return `true` on success, else `false`.\nYour method may call `encoder.push(buffer)` or `encoder.pushAny(any)` as needed.\n\nFor example:\n\n```js\nclass Foo {\n  constructor() {\n    this.one = 1;\n    this.two = 2;\n  }\n\n  encodeCBOR(encoder) {\n    const tagged = new Tagged(64000, [this.one, this.two]);\n    return encoder.pushAny(tagged);\n  }\n}\n```\n\nYou can also modify an existing type by monkey-patching an `encodeCBOR` function\nonto its prototype, but this isn't recommended.\n\n### `addSemanticType`\n\nSometimes, you want to support an existing type without modification to that\ntype.  In this case, call `addSemanticType(type, encodeFunction)` on an existing\n`Encoder` instance. The `encodeFunction` takes an encoder and an object to\nencode, for example:\n\n```js\nclass Bar {\n  constructor() {\n    this.three = 3;\n  }\n}\nconst enc = new Encoder();\nenc.addSemanticType(Bar, (encoder, b) => {\n  encoder.pushAny(b.three);\n});\n```\n\n## Adding new decoders\n\nMost of the time, you will want to add support for decoding a new tag type.  If\nthe Decoder class encounters a tag it doesn't support, it will generate a `Tagged`\ninstance that you can handle or ignore as needed.  To have a specific type\ngenerated instead, pass a `tags` option to the `Decoder`'s constructor, consisting\nof an object with tag number keys and function values.  The function will be\npassed the decoded value associated with the tag, and should return the decoded\nvalue.  For the `Foo` example above, this might look like:\n\n```js\nconst d = new Decoder({\n  tags: {\n    64000: val => {\n      // Check val to make sure it's an Array as expected, etc.\n      const foo = new Foo();\n      [foo.one, foo.two] = val;\n      return foo;\n    },\n  },\n});\n```\n\nYou can also replace the default decoders by passing in an appropriate tag\nfunction.  For example:\n\n```js\ncbor.decodeFirstSync(input, {\n  tags: {\n    // Replace the Tag 0 (RFC3339 Date/Time string) decoder.\n    // See https://tc39.es/proposal-temporal/docs/ for the upcoming\n    // Temporal built-in, which supports nanosecond time:\n    0: x => Temporal.Instant.from(x),\n  },\n});\n```\n\nDevelopers\n----------\n\nThe tests for this package use a set of test vectors from RFC 8949 appendix A\nby importing a machine readable version of them from\nhttps://github.com/cbor/test-vectors. For these tests to work, you will need\nto use the command `git submodule update --init` after cloning or pulling this\ncode.   See https://gist.github.com/gitaarik/8735255#file-git_submodules-md\nfor more information.\n\nGet a list of build steps with `npm run`.  I use `npm run dev`, which rebuilds,\nruns tests, and refreshes a browser window with coverage metrics every time I\nsave a `.js` file.  If you don't want to run the fuzz tests every time, set\na `NO_GARBAGE` environment variable:\n\n```\nenv NO_GARBAGE=1 npm run dev\n```\n\n[![Build Status](https://github.com/hildjj/node-cbor/workflows/Tests/badge.svg)](https://github.com/hildjj/node-cbor/actions?query=workflow%3ATests)\n[![codecov](https://codecov.io/github/hildjj/node-cbor/graph/badge.svg?token=HEGszEFpTc)](https://codecov.io/github/hildjj/node-cbor)\n","publish_time":1751297417901,"_cnpm_publish_time":1751297417901,"dist-tags":{"latest":"10.0.12"}}