{"_id":"culvert","_rev":"225846","name":"culvert","description":"Channel for easy streaming of work between complex logics.","dist-tags":{"latest":"0.1.2"},"maintainers":[{"name":"creationix","email":"tim@creationix.com"}],"time":{"modified":"2021-06-03T15:42:41.000Z","created":"2014-07-19T04:28:38.716Z","0.1.2":"2014-07-25T21:25:28.688Z","0.1.1":"2014-07-19T05:44:15.035Z","0.1.0":"2014-07-19T04:28:38.716Z"},"users":{},"author":{"name":"Tim Caswell","email":"tim@creationix.com"},"repository":{"type":"git","url":"git://github.com/creationix/culvert.git"},"versions":{"0.1.2":{"name":"culvert","version":"0.1.2","description":"Channel for easy streaming of work between complex logics.","main":"channel.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git://github.com/creationix/culvert.git"},"keywords":["channel","stream","csp","js-git"],"author":{"name":"Tim Caswell","email":"tim@creationix.com"},"license":"MIT","bugs":{"url":"https://github.com/creationix/culvert/issues"},"homepage":"https://github.com/creationix/culvert","_id":"culvert@0.1.2","_shasum":"9502f5f0154a2d5a22a023e79f71cc936fa6ef6f","_from":".","_npmVersion":"1.4.9","_npmUser":{"name":"creationix","email":"tim@creationix.com"},"maintainers":[{"name":"creationix","email":"tim@creationix.com"}],"dist":{"shasum":"9502f5f0154a2d5a22a023e79f71cc936fa6ef6f","size":2750,"noattachment":false,"key":"/culvert/-/culvert-0.1.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/culvert/download/culvert-0.1.2.tgz"},"directories":{},"publish_time":1406323528688,"_cnpm_publish_time":1406323528688,"_hasShrinkwrap":false},"0.1.1":{"name":"culvert","version":"0.1.1","description":"Channel for easy streaming of work between complex logics.","main":"channel.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git://github.com/creationix/culvert.git"},"keywords":["channel","stream","csp","js-git"],"author":{"name":"Tim Caswell","email":"tim@creationix.com"},"license":"MIT","bugs":{"url":"https://github.com/creationix/culvert/issues"},"homepage":"https://github.com/creationix/culvert","_id":"culvert@0.1.1","dist":{"shasum":"857094c1ae8e1e6e23b4d6da2878289d2e66e50c","size":2654,"noattachment":false,"key":"/culvert/-/culvert-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/culvert/download/culvert-0.1.1.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"creationix","email":"tim@creationix.com"},"maintainers":[{"name":"creationix","email":"tim@creationix.com"}],"directories":{},"publish_time":1405748655035,"_cnpm_publish_time":1405748655035,"_hasShrinkwrap":false},"0.1.0":{"name":"culvert","version":"0.1.0","description":"Channel for easy streaming of work between complex logics.","main":"channel.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git://github.com/creationix/culvert.git"},"keywords":["channel","stream","csp","js-git"],"author":{"name":"Tim Caswell","email":"tim@creationix.com"},"license":"MIT","bugs":{"url":"https://github.com/creationix/culvert/issues"},"homepage":"https://github.com/creationix/culvert","_id":"culvert@0.1.0","dist":{"shasum":"b782677dc1c82b7572598180eebf291a68c4be5e","size":2643,"noattachment":false,"key":"/culvert/-/culvert-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/culvert/download/culvert-0.1.0.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"creationix","email":"tim@creationix.com"},"maintainers":[{"name":"creationix","email":"tim@creationix.com"}],"directories":{},"publish_time":1405744118716,"_cnpm_publish_time":1405744118716,"_hasShrinkwrap":false}},"readme":"Culvert\n=======\n\nChannel for easy streaming of work between complex logics.\n\nThis is used in place of streams for CSP style flow.  I use it in js-git for network and file streams.\n\nUsually, you'll want to split sides to create a duplex channel.\n\n```js\nvar makeChannel = require('culvert');\n\nvar serverChannel = makeChannel();\nvar clientChannel = makeChannel();\n\nfunction connect(host, port) {\n\n  // This represents the server-side of the duplex pipe\n  var socket = {\n    put: serverChannel.put,\n    drain: serverChannel.drain,\n    take: cientChannel.drain\n  };\n\n  // When we want to send data to the consumer...\n  socket.put(someData);\n\n  // When we want to read from the consumer...\n  socket.take(function (err, item) {});\n\n  // Return the client's end of the pipe\n  return {\n    put: clientChannel.put,\n    drain: clientChannel.drain,\n    take: serverChannel.take\n  };\n}\n```\n\nIf you want/need to preserve back-pressure and honor the buffer limit,\nmake sure to wait for drain when `put` returns false.\n\n```js\n// Start a read\nsocket.take(onData);\n\nfunction onData(err, item) {\n  if (err) throw err;\n  if (item === undefined) {\n    // End stream when nothing comes out\n    console.log(\"done\");\n  }\n  else if (socket.put(item)) {\n    // If put returned true, keep reading\n    socket.take(onData);\n  }\n  else {\n    // Otherwise pause and wait for drain\n    socket.drain(onDrain);\n  }\n}\n\nfunction onDrain(err) {\n  if (err) throw err;\n  // Resume reading\n  socket.take(onData);\n}\n```\n\nIf you're using continuables and generators, it's much nicer syntax.\n\n```js\nvar item;\nwhile (item = yield socket.take, item !== undefined) {\n  if (!socket.put(item)) yield socket.drain;\n}\nconsole.log(\"done\");\n```\n\nAlso the continuable version won't blow the stack if lots of events come in on the same tick.\n\n## makeChannel(bufferSize, monitor)\n\nCreate a new channel.\n\nThe optional bufferSize is how many items can be in the queue and still be considered not full.\n\nThe optional monitor function will get called with `(type, item)` where `type` is either \"put\" or \"take\" and `item` is the value being put or taken.\n\n## channel.put(item) -> more\n\nThis is a sync function.  You can add as many items to the channel as you want and it will queue them up.\n\nThis returns `true` when the queue is smaller than bufferSize, it returns false if you should wait for drain.\n\n## channel.drain(callback)\n\nDrain is a reusable continuable.  Use this when you want to wait for the buffer to be below the bufferSize mark.\n\n## channel.take(callback)\n\nTake is for reading.  The callback will have the next item.  It may call sync or it may be later.\n","_attachments":{},"homepage":"https://github.com/creationix/culvert","bugs":{"url":"https://github.com/creationix/culvert/issues"},"license":"MIT"}