Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 763d7f98b590542d1ff51766fe5638412fcb464b @robotlolita robotlolita committed Mar 31, 2013
Showing with 368 additions and 0 deletions.
  1. +22 −0 .hgignore
  2. +3 −0 .travis.yml
  3. +21 −0 LICENCE
  4. +65 −0 README.md
  5. +45 −0 package.json
  6. +144 −0 src/index.ls
  7. +2 −0 test/mocha.opts
  8. +66 −0 test/suite.ls
@@ -0,0 +1,22 @@
+syntax:glob
+
+# Usual Emacs backup stuff
+*~
+\#*\#
+.\#*
+
+# Built things that shouldn't be part of the source tree
+dist
+.git
+ignore
+doctrees
+.buildinfo
+build
+npm-debug.log
+node_modules
+docs/api
+.calliope
+lib
+lib-test
+syntax: glob
+test/browser/suite-bundle.js
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - "0.10"
21 LICENCE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2013 Quildreen "Sorella" Motta <quildreen@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,65 @@
+# Noisyo [![Build Status](https://travis-ci.org/killdream/noisyo.png)](https://travis-ci.org/killdream/noisyo) ![Dependencies Status](https://david-dm.org/killdream/noisyo.png)
+
+**Note that this only works in Node 0.10 now, using the new Streams.**
+
+Drinks and vomits Stream contents. Promised to be really noisy.
+
+As you could probably guess, this is totes influenced by the [Clojure core
+library](http://clojuredocs.org/clojure_core/clojure.core/slurp). But more
+general :D
+
+
+## Example
+
+```js
+var noisyo = require('noisyo')
+
+// Slurp takes a Stream and returns a Promise of its contents.
+// Spit takes a Stream, and some contents, and returns a Promise of the
+// eventual draining.
+noisyo.slurp(process.stdin).then(noisyo.spit(process.stdout))
+
+// So, why not just `.pipe()` you faggot?
+// Well, these work with both Strings and Streams interchangeably.
+var input = noisyo.slurp(process.stdin)
+input.then(function(data) {
+ console.log(data)
+ noisyo.spit(fs.createWriteStream('foo.txt'), data)
+})
+
+// Obviously, you *will* want to use Streams directly if you're interested in
+// piping between them, this is not a substitute for Streams, just a nice,
+// high-level way of getting to its contents in one-shot quickly and
+// asynchronously. But it will buffer the whole thing in memory, which might be
+// *bad* in certain cases.
+```
+
+
+## Installing
+
+Just grab it from NPM:
+
+ $ npm install noisyo
+
+
+## Documentation
+
+A quick reference of the API can be built using [Calliope][]:
+
+ $ npm install -g calliope
+ $ calliope build
+
+
+## Tests
+
+You can run all tests using Mocha:
+
+ $ npm test
+
+
+## Licence
+
+MIT/X11. ie.: do whatever you want.
+
+[Calliope]: https://github.com/killdream/calliope
+[es5-shim]: https://github.com/kriskowal/es5-shim
@@ -0,0 +1,45 @@
+{
+ "name": "noisyo",
+ "version": "0.1.0",
+ "description": "Drinks and vomits Stream contents. Promised to be really noisy.",
+ "main": "./lib/index.js",
+ "devDependencies": {
+ "LiveScript": "~1.1.1",
+ "mocha": "~1.8.1",
+ "mocha-as-promised": "~1.2.1",
+ "chai-as-promised": "~3.2.5",
+ "chai": "~1.5.0"
+ },
+ "scripts": {
+ "cc": "./node_modules/.bin/lsc -o lib -c src/*.ls",
+ "test-cc": "./node_modules/.bin/lsc -o lib-test -c test/*.ls",
+ "pretest": "npm run-script cc && npm run-script test-cc",
+ "test": "./node_modules/.bin/mocha lib-test/*.js",
+ "pretest-browser": "npm run-script pretest && ./node_modules/.bin/browserify -o test/browser/suite-bundle.js -e test/browser/suite.js",
+ "prepublish": "npm run-script cc"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/killdream/noisyo.git"
+ },
+ "author": "Quildreen \"Sorella\" Motta <quildreen@gmail.com>",
+ "license": "MIT",
+ "readmeFilename": "README.md",
+ "calliope": {
+ "output": "docs/api",
+ "copyright": "© 2013 Quildreen \"Sorella\" Motta",
+ "packages": [
+ {
+ "files": [
+ "src/*.ls"
+ ]
+ }
+ ]
+ },
+ "engines": {
+ "node": ">=0.10.0"
+ },
+ "dependencies": {
+ "pinky": "~0.1.2"
+ }
+}
@@ -0,0 +1,144 @@
+## Module noisyo
+#
+# Drinks and vomits Stream contents. Promised to be really noisy.
+#
+#
+# Copyright (c) 2013 Quildreen "Sorella" Motta <quildreen@gmail.com>
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation files
+# (the "Software"), to deal in the Software without restriction,
+# including without limitation the rights to use, copy, modify, merge,
+# publish, distribute, sublicense, and/or sell copies of the Software,
+# and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+### -- Dependencies ----------------------------------------------------
+pinky = require 'pinky'
+stream = require 'stream'
+
+
+### -- Helpers ---------------------------------------------------------
+
+#### λ is-stream
+# Checks if something is a Stream.
+#
+# :: a -> Bool
+is-stream = (a) -> typeof a?.pipe is 'function'
+
+#### λ is-standard-output
+# Checks if a Stream is a standard output stream.
+#
+# :: a -> Bool
+is-standard-output = (a) -> (a is process.stdout) or (a is process.stderr)
+
+
+### -- Helpers for writing to Streams ----------------------------------
+
+#### λ pipe-from
+# Pipes a Stream from source to destination.
+#
+# :: Stream -> Stream -> Promise Stream Error
+pipe-from = (source, dest) ->
+ promise = pinky!
+
+ # Node doesn't close standard output streams when piping to them, so
+ # we just assume things will be correctly written by the time we
+ # finished draining the input.
+ if is-standard-output dest => source.on 'end' fulfill-read
+ else => dest.on 'finish' fulfill
+
+ source.on 'error' handle-errors
+ source.pipe dest
+
+ return promise
+
+ function handle-errors(err)
+ promise.reject err
+
+ function cleanup()
+ source.remove-listener 'error' handle-errors
+
+ function fulfill-read()
+ promise.fulfill dest
+ source.remove-listener 'end' fulfill-read
+
+ function fulfill()
+ promise.fulfill dest
+ dest.remove-listener 'finish' fulfill
+
+
+#### λ write-to
+# Writes plain strings/buffers to the output Stream.
+#
+# :: Stream -> String | Buffer -> Encoding -> Promise Stream Error
+write-to = (dest, source, encoding) ->
+ promise = pinky!
+
+ dest.write source, encoding, (err) ->
+ | err => promise.reject err
+ | _ => promise.fulfill dest
+
+ return promise
+
+
+
+
+### -- Core implementation ---------------------------------------------
+
+#### λ slurp
+# Drains the contents of a Stream and returns a promise for it.
+#
+# :: Stream -> Promise String Error
+slurp = (source) ->
+ promise = pinky!
+ data = (source.read! || '').to-string!
+
+ source.on 'readable' grab-chunk
+ source.on 'error' handle-failure
+ source.on 'end' fulfill
+ source.on 'end' clean-up
+
+ return promise
+
+ function grab-chunk()
+ chunk = source.read!
+ if chunk isnt null => data += chunk.to-string!
+
+ function handle-failure(err)
+ promise.reject err
+
+ function fulfill()
+ promise.fulfill data
+
+ function clean-up()
+ source.remove-listener 'readable' grab-chunk
+ source.remove-listener 'end' fulfill
+ source.remove-listener 'end' clean-up
+
+
+#### λ spit
+# Vomits some contents (String or Stream) into a Stream.
+#
+# :: Stream -> String -> Promise Stream Error
+# :: Stream -> Stream -> Promise Stream Error
+spit = (dest, source) -->
+ | is-stream source => pipe-from source, dest
+ | otherwise => write-to dest, source, arguments[2]
+
+
+
+
+### -- Exports ---------------------------------------------------------
+module.exports = { slurp, spit }
@@ -0,0 +1,2 @@
+--ui bdd
+--reporter spec
@@ -0,0 +1,66 @@
+stream = require 'stream'
+{slurp, spit} = require '../lib'
+
+(require 'mocha-as-promised')!
+chai = require 'chai'
+chai.use (require 'chai-as-promised')
+{expect} = chai
+
+o = it
+x = xit
+
+duplex = ->
+ d = new stream.Duplex
+ data = ''
+ ready = false
+ d.write = (buf, enc, f) ->
+ data := buf
+ process.next-tick -> do
+ ready := true
+ if f => f!
+ d.emit 'readable'
+
+
+
+ d.read = ->
+ if ready
+ process.next-tick -> d.emit 'end'
+ ready := false
+ return data
+ else
+ return null
+
+ return d
+
+d = null
+before-each -> d := duplex!
+
+text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
+
+
+describe 'λ slurp' ->
+ o 'Should fulfill with the full contents of the stream.' ->
+ d.write text
+ expect (slurp d) .to.become text
+
+ x 'Should fail if the stream errors.' ->
+ d.write text
+ d.haz-error = true
+ expect (slurp d) .to.be.rejected.with /no u/
+
+describe 'λ spit' ->
+ describe 'with Stream' ->
+ o 'Should fulfill after contents have been piped.' (done) ->
+ t = duplex!
+ d.write text
+ expect ((spit t, d).then slurp) .to.become text
+
+ o 'Should fulfill after source is read if it\'s a standard output stream.' ->
+ d.write text
+ expect (spit process.stdout, d) .to.be.fulfilled
+
+ describe 'with Strings' ->
+ o 'Should fulfill after writing to the source.' ->
+ expect ((spit d, text).then slurp) .to.become text
+
+

0 comments on commit 763d7f9

Please sign in to comment.