{"name":"stream-parser","version":"0.0.2","description":"Generic interruptible \"parser\" mixin for Transform & Writable streams","main":"lib/parser.js","scripts":{"test":"mocha --reporter spec"},"repository":{"type":"git","url":"git://github.com/TooTallNate/node-stream-parser.git"},"author":{"name":"Nathan Rajlich","email":"nathan@tootallnate.net","url":"http://tootallnate.net"},"license":"MIT","dependencies":{"debug":"~0.7.0"},"devDependencies":{"mocha":"*","readable-stream":"*"},"_id":"stream-parser@0.0.2","dist":{"shasum":"f145e5b0997c7a4fc31bfc8949e315fb617e1992","tarball":"http://registry-npm.ywwl.com/stream-parser/download/stream-parser-0.0.2.tgz","integrity":"sha512-Il+KUzY2Uo+izi1SPNOK165UdYmZqpp2iMbHoyAQ3irFQfJIGr/AWsc1dHALInjbrqsB2POZXzx4xgCw3Dn9kw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIELtNDFeGw2R6wlPTV8jycj0KMtz/3CtEtAD18tzNfeHAiBAcQVIGjuelE+UtS6tjiqxlDTFpLe1cfn9Yu0Kep+abw=="}],"size":4629,"noattachment":false,"key":"/stream-parser/-/stream-parser-0.0.2.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"tootallnate","email":"nathan@tootallnate.net"},"maintainers":[{"name":"tootallnate","email":"nathan@tootallnate.net"}],"directories":{},"readme":"node-stream-parser\n==================\n### Generic interruptible \"parser\" mixin for Transform & Writable streams\n[![Build Status](https://secure.travis-ci.org/TooTallNate/node-stream-parser.svg)](http://travis-ci.org/TooTallNate/node-stream-parser)\n\nThis module offers the `stream-parser` mixin, which provides an easy-to-use API\nfor parsing bytes from `Writable` and/or `Transform` stream instances. This module\nis great for implementing streaming parsers for standardized file formats.\n\nFor `Writable` streams, the parser takes control over the `_write` callback\nfunction. For `Transform` streams, the parser controls the `_transform` callback\nfunction.\n\nInstallation\n------------\n\n``` bash\n$ npm install stream-parser\n```\n\n\nExample\n-------\n\nLet's create a quick `Transform` stream subclass that utilizes the parser's\n`_bytes()` and `_passthrough()` functions to parse a theoretical file format that\nhas an 8-byte header we want to parse, and then pass through the rest of the data.\n\n``` javascript\nvar Parser = require('stream-parser');\nvar inherits = require('util').inherits;\nvar Transform = require('stream').Transform;\n\n// create a Transform stream subclass\nfunction MyParser () {\n  Transform.call(this);\n\n  // buffer the first 8 bytes written\n  this._bytes(8, this.onheader);\n}\ninherits(MyParser, Transform);\n\n// mixin stream-parser into MyParser's `prototype`\nParser(MyParser.prototype);\n\n// invoked when the first 8 bytes have been received\nMyParser.prototype.onheader = function (buffer, output) {\n  // parse the \"buffer\" into a useful \"header\" object\n  var header = {};\n  header.type = buffer.readUInt32LE(0);\n  header.name = buffer.toString('utf8', 4);\n  this.emit('header', header);\n\n  // it's usually a good idea to queue the next \"piece\" within the callback\n  this._passthrough(Infinity);\n};\n\n\n// now we can *use* it!\nvar parser = new MyParser();\nparser.on('header', function (header) {\n  console.error('got \"header\"', header);\n});\nprocess.stdin.pipe(parser).pipe(process.stdout);\n```\n\nHere's an example of manually creating a `Transform` stream and turning it into a\n\"pass through\" stream equivalent to the one built into node core:\n\n``` javascript\nvar Parser = require('stream-parser');\nvar Transform = require('stream').Transform;\n\n// create a Transform instance and extend it with \"stream-parser\"\nvar p = new Transform();\nParser(p);\n\n// pass through `Infinity` bytes... forever...\np._passthrough(Infinity);\n\n// now `p` is equivalent to a stream.PassThrough instance\nprocess.stdin.pipe(p).pipe(process.stdout);\n```\n\nSee the `test` directory for some more example code in the test cases.\n\nA list of known concrete implementations is here (send pull requests for more!):\n\n * [node-icecast][]\n * [node-throttle][]\n * [node-flv][]\n * [node-wav][]\n\nAPI\n---\n\n  - [Parser()](#parser)\n    - [._bytes(n, cb)](#_bytesn-cb)\n    - [._skipBytes(n, cb)](#_skipbytesn-cb)\n    - [._passthrough(n, cb)](#_passthroughn-cb)\n\n## Parser()\n\n  The `Parser` stream mixin works with either `Writable` or `Transform` stream\n  instances/subclasses. Provides a convenient generic \"parsing\" API:\n\n```js\n_bytes(n, cb) - buffers \"n\" bytes and then calls \"cb\" with the \"chunk\"\n_skipBytes(n, cb) - skips \"n\" bytes and then calls \"cb\" when done\n```\n\n  If you extend a `Transform` stream, then the `_passthrough()` function is also\n  added:\n\n```js\n_passthrough(n, cb) - passes through \"n\" bytes untouched and then calls \"cb\"\n```\n\n### ._bytes(n, cb)\n\n  Buffers `n` bytes and then invokes `cb` once that amount has been collected.\n\n### ._skipBytes(n, cb)\n\n  Skips over the next `n` bytes and then invokes `cb` once that amount has been\n  discarded.\n\n### ._passthrough(n, cb)\n\n  Passes through `n` bytes to the readable side of this stream untouched,\n  then invokes `cb` once that amount has been passed through. This function is only defined\n  when stream-parser is extending a `Transform` stream.\n\n[node-icecast]: https://github.com/TooTallNate/node-icecast\n[node-throttle]: https://github.com/TooTallNate/node-throttle\n[node-flv]: https://github.com/TooTallNate/node-flv\n[node-wav]: https://github.com/TooTallNate/node-wav\n","publish_time":1360363051422,"_cnpm_publish_time":1360363051422,"dist-tags":{"latest":"0.3.1"}}