Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit ef710400ba0133996066fd47986c55df73043b01 0 parents
@fent authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) 2012 by Roly Fentanes
+
+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.
39 README.md
@@ -0,0 +1,39 @@
+# node-streamin [![Build Status](https://secure.travis-ci.org/fent/node-streamin.png)](http://travis-ci.org/fent/node-streamin)
+
+Provide a better streaming api in your app.
+
+
+# Usage
+
+```js
+var streamin = require('streamin');
+
+// `io` can be a path to a file, a url, or a stream
+var stream = streamin(io);
+```
+
+That's the gist of it. With this you can give your code a more conveniet API by allowing streams to be passed into your function and also consider the common use cases of files and urls as inputs or outputs.
+
+
+# API
+### streamin(io, [requestOptions])
+
+If `io` is a path to a file, it will return a stream that points to a local file. If it's a url, a stream from a remote request will be returned. If it detects that `io` is already a stream, it returns it.
+
+`requestOptions` can be a hash of options passed to [request](https://github.com/mikeal/request).
+
+
+# Install
+
+ npm install streamin
+
+
+# Tests
+Tests are written with [mocha](http://visionmedia.github.com/mocha/)
+
+```bash
+npm test
+```
+
+# License
+MIT
14 lib/file.js
@@ -0,0 +1,14 @@
+var fs = require('fs');
+var Streamify = require('streamify');
+
+
+/**
+ * A file that can either be read or written to.
+ *
+ * @param (string) path
+ * @param (Object) options
+ */
+module.exports = function file(path, options) {
+ var stream = new Streamify();
+ return stream;
+};
50 lib/index.js
@@ -0,0 +1,50 @@
+var Stream = require('stream').Stream;
+var fs = require('fs');
+var url = require('url');
+var request = require('request');
+var filed = require('filed');
+
+
+/**
+ * Returns true if `a` is a stream.
+ *
+ * @param (Stream|Object) io
+ * @return (boolean)
+ */
+function isStream(io) {
+ return io instanceof Stream;
+}
+
+
+/**
+ * Used to test if a string is a URL.
+ */
+var isURL = /^https?:/
+
+
+/**
+ * Allows `io` to be a path to a file, a url, or a stream.
+ *
+ * @param (string|Stream) io
+ * @param (!Object) requestOptions
+ * @return (Stream)
+ */
+module.exports = function streamin(io, requestOptions) {
+
+ if (isStream(io)) {
+ return io;
+
+ } else if (typeof io === 'string') {
+ if (isURL.test(io)) {
+ requestOptions = requestOptions || {};
+ requestOptions.url = io;
+ return request(requestOptions);
+
+ } else {
+ return filed(io);
+ }
+
+ } else {
+ throw new TypeError('`io` is not a stream or a string');
+ }
+};
29 package.json
@@ -0,0 +1,29 @@
+{
+ "name": "streamin",
+ "description": "Provide a better streaming api in your app.",
+ "keywords": ["stream", "input", "output", "file", "request", "path", "url"],
+ "version": "0.1.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/fent/node-streamin.git"
+ },
+ "author": "Roly Fentanes (https://github.com/fent)",
+ "main": "./lib/index.js",
+ "scripts": {
+ "test": "mocha -R spec test/*-test.js"
+ },
+ "directories": {
+ "lib": "./lib"
+ },
+ "dependencies": {
+ "request": "~2.9.203",
+ "filed": "~0.0.6"
+ },
+ "devDependencies": {
+ "mocha": "x"
+ },
+ "licenses": [ {
+ "type": "MIT",
+ "url" : "http://github.com/fent/node-streamin/raw/master/LICENSE"
+ }]
+}
33 test/main-test.js
@@ -0,0 +1,33 @@
+var streamin = require('..')
+ , assert = require('assert')
+ , fs = require('fs')
+ , Stream = require('stream').Stream
+ , NullStream = require('./nullstream');
+ ;
+
+
+describe('Pass a path to a file', function() {
+ it('Should return an instance of Stream', function() {
+ var stream = streamin(__filename);
+ stream.pipe(new NullStream());
+ assert.ok(stream instanceof Stream);
+ });
+});
+
+
+describe('Pass a url', function() {
+ it('Should return an instance of Stream', function() {
+ var stream = streamin('http://google.com');
+ stream.destroy();
+ assert.ok(stream instanceof Stream);
+ });
+});
+
+
+describe('Pass a stream', function() {
+ it('Should return an instance of Stream', function() {
+ var stream = streamin(fs.createReadStream(__filename));
+ stream.destroy();
+ assert.ok(stream instanceof Stream);
+ });
+});
25 test/nullstream.js
@@ -0,0 +1,25 @@
+var inherits = require('util').inherits;
+var Stream = require('stream').Stream;
+
+
+/**
+ * A writable stream that does nothing with the data it gets.
+ *
+ * @constructor
+ * @extends (Stream)
+ */
+var NullStream = module.exports = function NullStream() {
+ Stream.call(this);
+ this.writable = true;
+};
+
+inherits(NullStream, Stream);
+
+
+NullStream.prototype.write = function() {
+ return true;
+}
+
+NullStream.prototype.destroy = NullStream.prototype.end = function() {
+ this.writable = false;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.