{"_id":"@shinyoshiaki/binary-data","_rev":"1-edf9ff25e5bb8f5af677abb5bb0c5ef3","name":"@shinyoshiaki/binary-data","dist-tags":{"latest":"0.6.1"},"versions":{"0.6.0":{"name":"@shinyoshiaki/binary-data","version":"0.6.0","description":"Declarative binary data encoder / decoder.","main":"src/index.js","types":"./binary-data.d.ts","scripts":{"lint":"npx eslint .","cover":"npx jest --coverage","test":"npm run lint && npx jest","test-ci":"npm run lint && npm run cover"},"repository":{"type":"git","url":"git+https://github.com/reklatsmasters/binary-data.git"},"keywords":["binary","data","buffer","stream","decode","encode","bl","bin-protocol","restructure","varstruct"],"author":{"name":"Dmitry Tsvettsikh","email":"me@reklatsmasters.com"},"license":"MIT","bugs":{"url":"https://github.com/reklatsmasters/binary-data/issues"},"homepage":"https://github.com/reklatsmasters/binary-data#readme","engines":{"node":">=6"},"dependencies":{"generate-function":"^2.3.1","is-plain-object":"^2.0.3"},"devDependencies":{"@nodertc/eslint-config":"0.2.1","binary":"^0.3.0","bl":"^2.1.2","eslint":"^5.6.1","jest":"^23.6.0","prettier":"^1.14.3"},"jest":{"modulePaths":["<rootDir>","<rootDir>/src","<rootDir>/src/node_modules"],"testMatch":["**/test/**/*.js"],"testPathIgnorePatterns":["<rootDir>/node_modules/"],"coverageDirectory":"<rootDir>/coverage","collectCoverageFrom":["**/src/*.js","**/src/node_modules/**/*.js"],"coveragePathIgnorePatterns":["<rootDir>/node_modules/"],"cacheDirectory":".jest-cache","haste":{"providesModuleNodeModules":["internal","lib","streams","types"]}},"eslintConfig":{"extends":"@nodertc","overrides":[{"files":["test/**/*.js"],"env":{"jest":true},"settings":{"import/resolver":{"node":{"moduleDirectory":["node_modules","src","src/node_modules"]}}},"rules":{"require-jsdoc":"off"}},{"files":["benchmark/*.js","examples/*.js"],"rules":{"no-console":"off","require-jsdoc":"off","no-plusplus":"off","no-param-reassign":"off"}}]},"publishConfig":{"access":"public"},"_id":"@shinyoshiaki/binary-data@0.6.0","gitHead":"9d2d3df364e068ccf526f90cada5749a56b6a9c7","_nodeVersion":"21.5.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-Li2d1c3DosXsnOTglaRJoLkTKS0D7vTZPp+QlMO0qzGwEX6yaMPbhbia9IFX0WlRXFBgNjyU6UkZXq7DloscBQ==","shasum":"d92f265e27a42228fa74ec59e4dc9930c5dc1aa4","tarball":"https://registry.npmjs.org/@shinyoshiaki/binary-data/-/binary-data-0.6.0.tgz","fileCount":36,"unpackedSize":76331,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCM3FwB5DjwNA+picRz+3oH6M7cA8q9UtU9Jqg58v1Q2QIgbYzFKRCqcmrqYAWUCUkFmYhlqhdx5S2jeqWt7yTGFTg="}]},"_npmUser":{"name":"shinyoshiaki","email":"shinyoshiaki2012@gmail.com"},"directories":{},"maintainers":[{"name":"shinyoshiaki","email":"shinyoshiaki2012@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/binary-data_0.6.0_1710927165220_0.10812782577315794"},"_hasShrinkwrap":false},"0.6.1":{"name":"@shinyoshiaki/binary-data","version":"0.6.1","description":"Declarative binary data encoder / decoder.","main":"src/index.js","types":"./binary-data.d.ts","scripts":{"lint":"npx eslint .","cover":"npx jest --coverage","test":"npm run lint && npx jest","test-ci":"npm run lint && npm run cover"},"repository":{"type":"git","url":"git+https://github.com/reklatsmasters/binary-data.git"},"keywords":["binary","data","buffer","stream","decode","encode","bl","bin-protocol","restructure","varstruct"],"author":{"name":"Dmitry Tsvettsikh","email":"me@reklatsmasters.com"},"license":"MIT","bugs":{"url":"https://github.com/reklatsmasters/binary-data/issues"},"homepage":"https://github.com/reklatsmasters/binary-data#readme","engines":{"node":">=6"},"dependencies":{"generate-function":"^2.3.1","is-plain-object":"^2.0.3"},"devDependencies":{"@nodertc/eslint-config":"0.2.1","binary":"^0.3.0","bl":"^2.1.2","eslint":"^5.6.1","jest":"^23.6.0","prettier":"^1.14.3"},"jest":{"modulePaths":["<rootDir>","<rootDir>/src","<rootDir>/src/node_modules"],"testMatch":["**/test/**/*.js"],"testPathIgnorePatterns":["<rootDir>/node_modules/"],"coverageDirectory":"<rootDir>/coverage","collectCoverageFrom":["**/src/*.js","**/src/node_modules/**/*.js"],"coveragePathIgnorePatterns":["<rootDir>/node_modules/"],"cacheDirectory":".jest-cache","haste":{"providesModuleNodeModules":["internal","lib","streams","types"]}},"eslintConfig":{"extends":"@nodertc","overrides":[{"files":["test/**/*.js"],"env":{"jest":true},"settings":{"import/resolver":{"node":{"moduleDirectory":["node_modules","src","src/node_modules"]}}},"rules":{"require-jsdoc":"off"}},{"files":["benchmark/*.js","examples/*.js"],"rules":{"no-console":"off","require-jsdoc":"off","no-plusplus":"off","no-param-reassign":"off"}}]},"publishConfig":{"access":"public"},"_id":"@shinyoshiaki/binary-data@0.6.1","gitHead":"666b1e3253ecf402114ec7749f572498cb5b947d","_nodeVersion":"21.5.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-7HDb/fQAop2bCmvDIzU5+69i+UJaFgIVp99h1VzK1mpg1JwSODOkjbqD7ilTYnqlnadF8C4XjpwpepxDsGY6+w==","shasum":"98010e258d154ae83efeac14b9c0805dc8ffcea8","tarball":"https://registry.npmjs.org/@shinyoshiaki/binary-data/-/binary-data-0.6.1.tgz","fileCount":36,"unpackedSize":76312,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHPjWgfoiVNXLFbz8KzuUJJarlp29jnDKS0oEh0eCiE7AiEAhZkh94jxr1clguj9Ky+42KKbzmS6WEt9XBV8QSpQKEU="}]},"_npmUser":{"name":"shinyoshiaki","email":"shinyoshiaki2012@gmail.com"},"directories":{},"maintainers":[{"name":"shinyoshiaki","email":"shinyoshiaki2012@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/binary-data_0.6.1_1711270034175_0.4342479100304584"},"_hasShrinkwrap":false}},"time":{"created":"2024-03-20T09:32:45.094Z","0.6.0":"2024-03-20T09:32:45.368Z","modified":"2024-03-24T08:47:14.817Z","0.6.1":"2024-03-24T08:47:14.395Z"},"maintainers":[{"name":"shinyoshiaki","email":"shinyoshiaki2012@gmail.com"}],"description":"Declarative binary data encoder / decoder.","homepage":"https://github.com/reklatsmasters/binary-data#readme","keywords":["binary","data","buffer","stream","decode","encode","bl","bin-protocol","restructure","varstruct"],"repository":{"type":"git","url":"git+https://github.com/reklatsmasters/binary-data.git"},"author":{"name":"Dmitry Tsvettsikh","email":"me@reklatsmasters.com"},"bugs":{"url":"https://github.com/reklatsmasters/binary-data/issues"},"license":"MIT","readme":"# binary-data\n\n[![Build Status](https://travis-ci.org/reklatsmasters/binary-data.svg?branch=master)](https://travis-ci.org/reklatsmasters/binary-data)\n[![npm](https://img.shields.io/npm/v/binary-data.svg)](https://npmjs.org/package/binary-data)\n[![node](https://img.shields.io/node/v/binary-data.svg)](https://npmjs.org/package/binary-data)\n[![license](https://img.shields.io/npm/l/binary-data.svg)](https://npmjs.org/package/binary-data)\n[![downloads](https://img.shields.io/npm/dm/binary-data.svg)](https://npmjs.org/package/binary-data)\n[![Coverage Status](https://coveralls.io/repos/github/reklatsmasters/binary-data/badge.svg?branch=master)](https://coveralls.io/github/reklatsmasters/binary-data?branch=master)\n\nDeclarative binary data encoder / decoder. This module works almost like as [`binary`](https://www.npmjs.com/package/binary) or [`restructure`](https://www.npmjs.com/package/restructure) but provided modern and clean api. It inspired by [abstract-encoding](https://github.com/mafintosh/abstract-encoding) interface.\n\n### Support\n\n[![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png)](https://www.buymeacoffee.com/reklatsmasters)\n\n## Usage\n\n#### decode\n\n```js\nconst { decode, createDecode, types: { uint8, array, string } } = require('binary-data')\n\n// 1. Define your own schema as plain object\nconst protocol = {\n  type: uint8,\n  value: array(string(null), uint8)\n}\n\nconst message = Buffer.from([1, 2, 3, 4, 5, 6, 0]);\n\n// Just decode message\nconst packet = decode(message, protocol)\n\n// 2 Also you may decode messages from streams\nconst net = require('net');\n\nconst socket = net.createConnection({ port: 8124 });\nconst istream = createDecode(protocol);\n\nsocket.pipe(istream).on('data', packet => {\n  console.log(packet.type, packet.value);\n});\n```\n\n#### encode\n\n```js\nconst { encode, createEncode, types: { uint8, string } } = require('binary-data')\n\nconst protocol = {\n  type: uint8,\n  value: string(uint8)\n}\n\nconst hello = {\n  type: 12,\n  value: 'my random data'\n}\n\n// Just encode message\nconst ostream = encode(hello, protocol);\nconst packet = ostream.slice();\n\n// Or you may encode messages into a stream\nconst net = require('net');\n\nconst ostream = createEncode(protocol);\nconst socket = net.createConnection({ port: 8124 }, () => {\n  ostream.write(hello);\n});\n\nostream.pipe(socket);\n\n// You may combine multiple schemes into one stream\nconst ostream = createEncode();\n\nencode(obj1, ostream, protocol1);\nencode(obj2, ostream, protocol2);\nencode(obj3, ostream, protocol3);\n\nconst packet = ostream.slice();\n\n```\n\nSee [stun](https://github.com/nodertc/stun) or [dtls](https://github.com/nodertc/dtls) module for complete example.\n\n## Perfomance\n\nDecoding DTLS ClientHello packet, *nodejs 10.14.1 / Ubuntu 16.04 x64*\n\n|name|time|\n|---|---|\n|binary data|637.900ms|\n|binary|2229.218ms|\n\n## API\n\n* [`encode(obj: any, [target: BinaryStream], type: Object): BinaryStream`](#encode)\n* [`decode(source: BinaryStream|Buffer, type: Object): any`](#decode)\n* [`encodingLength(item: any, type: Object): Number`](#encoding-length)\n* [`createEncodeStream([type: Object]): BinaryStream`](#create-encode-stream)\n* [`createDecodeStream([type: Object|Buffer]): BinaryStream`](#create-decode-stream)\n* [`createEncode([type: Object]): BinaryStream`](#create-encode-stream)\n* [`createDecode([type: Object|Buffer]): BinaryStream`](#create-decode-stream)\n* [Types](#types)\n  * [`(u)int(8, 16, 24, 32, 40, 48)(be, le)`](#types-int)\n  * [`(double, float)(be, le)`](#types-float)\n  * [`array(type: Object, length: number|Object|Function, lengthType: string)`](#types-array)\n  * [`string(length)`](#types-string)\n  * [`bool(type: any)`](#types-bool)\n  * [`buffer(length)`](#types-buffer)\n  * [`reserved(type, count)`](#types-reserved)\n  * [`when(fn: function(context): boolean, type)`](#types-when)\n\n<a name='decode' />\n\n#### `decode(source: BinaryStream|Buffer, type: Object): any`\n\nReads any data from stream `rstream` using data type `type`. See examples above.\n\n<a name='encode' />\n\n#### `encode(obj: any, [target: BinaryStream], type: Object): BinaryStream`\n\nWrites any data `obj` to stream `target` using data type `type`. See examples above.\n\n<a name='encoding-length' />\n\n#### `encodingLength(item: any, type: Object): Number`\n\nReturn the amount of bytes needed to encode `item` using `type`.\n\n<a name='create-encode-stream' />\n\n#### `createEncodeStream([type: Object]): BinaryStream`\n#### `createEncode([type: Object]): BinaryStream`\n\nCreate instance of BinaryStream.\n\n<a name='create-decode-stream' />\n\n#### `createDecodeStream([type: Object|Buffer]): BinaryStream`\n#### `createDecode([type: Object|Buffer]): BinaryStream`\n\nCreate instance of BinaryStream.\n\n<a name='types' />\n\n#### `types: Object`\n\nContains all primitive data types.\n\n### Types\n\n<a name='types-int' />\n\n#### `(u)int(8, 16, 24, 32, 40, 48)(be, le)`\n\nLow-level integer types.\n\n```js\nconst schema = {\n  type: int8\n}\n\n// define int64 as buffer and use your loved library for big numbers\nconst int64 = buffer(8)\n```\n\n<a name='types-float' />\n\n#### `(double, float)(be, le)`\n\nLow-level floating-point types.\n\n```js\nconst schema = {\n  size: doublele\n}\n```\n\n<a name='types-array' />\n\n#### `array(type: Object, length: number|Object|Function, lengthType: string)`\n\nArray of low-level or user defined types. Argument `type` should be primitive type or user defined scheme. Argument `length` should be a number, number type or function. Argument `lengthType` should be `bytes` or `count` (default).\n\n```js\narray(uint8, 3) // 3 x uint8\n\nconst schema = {\n  length: uint8,\n  type: uint32be,\n  items: array(uint16, ({node}) => node.length, 'bytes')\n}\n\n// difference between `bytes` or `count`\n\narray(uint16, uint8)\n// |   0x2  | 0x0 0x1 | 0x0 0x2 |\n// | length | item 1  |  item 2 |\n// bytes = 1 + 4, length = 2\n\narray(uint16, uint8, 'bytes')\n// |   0x4  | 0x0 0x1 | 0x0 0x2 |\n// | length | item 1  |  item 2 |\n// bytes = 1 + 4, length = 4\n```\n\n<a name='types-string' />\n\n#### `string(length)`\n\nLow-level string type. Argument `length` can be _number_, _null_ for C - strings, _type_ for size-prefixed data or _function_.\n\n<a name='types-bool' />\n\n#### `bool(type: any)`\n\nConvert provided type to / from boolean. Argument `type` should be an number type.\n\n```js\nconst schema = bool(uint8)\nconst rstream = createDecodeStream(buf) // 0x01 0\n\ndecode(rstream, schema) // return true\ndecode(rstream, schema) // return false\n```\n\n<a name='types-buffer' />\n\n#### `buffer(length: Object|null|number)`\n\nLow-level buffer type. Argument `length` can be _number_, number _type_ for size-prefixed data, _function_ or _null_.\n\n```js\nbuffer(5) // buffer should be 5 bytes\n\nbuffer(uint8) // length prefixed buffer\n// |   0x3  | 0xa 0xb 0xc\n// | length | data\n\nconst packet = {\n  header: {\n    length: uint16be\n  },\n  data: buffer(({node}) => node.header.length % 2) // function should return actual length\n}\n```\n\n<a name='types-reserved' />\n\n#### `reserved(type, count)`\n\nSpecial type to skip any data. Argument `count` should be a number, number type or function.\n\n```js\nconst packet = {\n  type: uint8,\n  _padding: reserved(uint8, 3)\n}\n\ndecode(rstream, packet) // return { type }\n```\n\n<a name='types-when' />\n\n#### `when(fn: function(context): boolean, type)`\n\nSpecial type for conditions. Argument `fn` should be a function and should return boolean value. The `type`\nargument will be evaluated when the first one returns positive value.\n\n```js\nconst schema = {\n  type: uint8,\n  bytes: when(({ node }) => node.type === 1, string(uint16be)),\n  list: when(({ node }) => node.type === 2, array(uint32be, uint8))\n}\n```\n\n### `select(when, ..., defaultType)`\n\nThe second type for conditions. The same as `switch` operator in js. Argument `defaultType` may be any known\n*type* excluding user *schemas*.\n\n```js\nconst schema = {\n  id: uint8,\n  payload: select(when(({ node }) => node.id === 1, string(uint16be)), buffer(uint16be))\n}\n```\n\n## License\n\nMIT, 2017 (c) Dmitriy Tsvettsikh\n","readmeFilename":"README.md"}